{"id":14541,"date":"2026-08-26T01:00:00","date_gmt":"2026-08-26T05:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=14541"},"modified":"2026-08-06T18:50:14","modified_gmt":"2026-08-06T22:50:14","slug":"trade-offs-in-programming","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=14541","title":{"rendered":"Trade-offs in programming"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"14541\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"wp-block-paragraph\">If you look closely at a program&#8217;s source code, you&#8217;ll see more than just lines of code. You&#8217;ll also find interesting decisions as <em>trade-offs<\/em> to balance code readability and system performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most programmers make these kinds of decisions, although they may not always be conscious decisions. Some are so ingrained by practice that they aren&#8217;t really <em>decisions<\/em> but <em>habits<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A recent example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can find one trade-off example in my FORTRAN 77 code to <a href=\"https:\/\/www.both.org\/?p=14481\">display the Mandelbrot Set<\/a>. In brief, you calculate the Mandelbrot Set by iterating the function <em>z<\/em>\u2004=\u2004<em>z<\/em><sup>2<\/sup>\u2005+\u2005<em>c<\/em> for different values of the complex number <em>c<\/em>, always starting the first calculation with <em>z<\/em>\u2004=\u20040. If <em>z<\/em> remains stable, then you draw a black pixel for that <em>c<\/em> value. Otherwise, you draw a colored pixel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In mathematics, it&#8217;s well-known that if the <em>absolute value<\/em> of <em>z<\/em> exceeds 2, the iteration of <em>z<\/em>\u2004=\u2004<em>z<\/em><sup>2<\/sup>\u2005+\u2005<em>c<\/em> will never become stable. However, my code sample to calculate the Mandelbrot Set did not directly reference &#8220;2&#8221; and did not use the <code>ABS<\/code> function to calculate the absolute value. Instead, my sample program used this calculation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10    Z=Z**2+C\n\n      DIST=REAL(Z)**2 + IMAG(Z)**2\n      COUNT=COUNT+1\n      IF ((DIST.LT.4.0).AND.(COUNT.LT.LIMIT)) GOTO 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Why did I choose to write a different method to calculate the absolute value? This was a programming trade-off that I learned long ago, and persists in my FORTRAN programs today.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Absolute value<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, let&#8217;s consider what the <em>absolute value<\/em> of a number really means. If you think about the absolute value of an ordinary number like <strong>-1<\/strong>, you probably think &#8220;just make it positive.&#8221; And that&#8217;s technically correct, as far as ordinary numbers are concerned.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another way to think about the absolute value of a number is <em>its &#8220;distance&#8221; from zero<\/em>. That is, <strong>-1<\/strong> is &#8220;1&#8221; away from zero. Similarly, <strong>3<\/strong> is &#8220;3&#8221; away from zero, and <strong>-4<\/strong> is &#8220;4&#8221; away from zero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now apply that to complex numbers. A complex number is a two-part value: a <em>real<\/em> part and an <em>imaginary<\/em> part. You can think of this as a coordinate on an <em>x,y<\/em> chart; the real part is on the <em>x<\/em> axis, and the imaginary part is on the <em>y<\/em> axis. The absolute value of a complex number is a <em>straight line<\/em> from that <em>x,y<\/em> coordinate to zero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can calculate the absolute value of a complex number using the Pythagorean theorem, which you probably learned in grade school. For any right triangle with sides of length <em>a<\/em> and <em>b<\/em>, you can calculate the length of the <em>hypotenuse<\/em> as <em>a<\/em><sup>2<\/sup>\u2005+\u2005<em>b<\/em><sup>2<\/sup>\u2004=\u2004<em>c<\/em><sup>2<\/sup>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"450\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2026\/08\/pythagoras.png\" alt=\"triangle with sides labeled a (bottom) and b (right) and c (angle)\" class=\"wp-image-14540\"\/><figcaption class=\"wp-element-caption\">The Pythagorean theorem of a triangle<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If <em>a<\/em> is the real part and <em>b<\/em> is the imaginary part, then you can calculate the hypotenuse as <em>c<\/em>. This is the absolute value of a complex number, the &#8220;distance&#8221; from zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Taking a shortcut<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I learned FORTRAN 77 programming in the early 1990s. Computers were measured in speeds of <em>megahertz<\/em> (MHz) and a typical &#8220;fast&#8221; computer at the time was the 80486DX (1991) which clocked in at 50 MHz. I had a slower 80386 CPU, which ran at about 33 MHz. By comparison, an &#8220;average&#8221; PC in 2026 has a 6-core CPU that runs at about 4.7 GHz. That&#8217;s roughly 4,700 MHz compared to my 33 MHz (about 140x faster) but with 6 cores instead of just one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">My university ran a MicroVAX system at the time. I&#8217;m making a guess (I don&#8217;t know for sure) but that might have been a <a href=\"https:\/\/en.wikipedia.org\/wiki\/VAX_4000\">VAX 4000<\/a> from 1990, which ran at about 35.71 MHz\u2014roughly comparable to my home computer at the time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With a slower CPU, I learned to avoid unnecessary calculations. To determine the absolute value of a complex number, a program actually needed to process four calculations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The square of the real value: <em>a<\/em><sup>2<\/sup><\/li>\n\n\n\n<li>The square of the imaginary value: <em>b<\/em><sup>2<\/sup><\/li>\n\n\n\n<li>The sum of the squares: <em>a<\/em><sup>2<\/sup>\u2005+\u2005<em>b<\/em><sup>2<\/sup><\/li>\n\n\n\n<li>The square root of the sum: <em>c<\/em><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Calculating the square of a number doesn&#8217;t take a lot of computer time; neither does adding two values. However, calculating the square root can be slow if the CPU is not fast. This will become more noticeable as you perform the same calculation <em>over and over<\/em> again, such as to iterate the function <em>z<\/em>\u2004=\u2004<em>z<\/em><sup>2<\/sup>\u2005+\u2005<em>c<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So I made a shortcut: if the absolute value of <em>z<\/em> remains less than 2, then that&#8217;s the same as <em>a<\/em><sup>2<\/sup>\u2005+\u2005<em>b<\/em><sup>2<\/sup> less than 4. That&#8217;s the calculation in the Mandelbrot Set program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10    Z=Z**2+C\n\n      DIST=REAL(Z)**2 + IMAG(Z)**2\n      COUNT=COUNT+1\n      IF ((DIST.LT.4.0).AND.(COUNT.LT.LIMIT)) GOTO 10<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Trade-offs in programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These days, faster CPUs mean the calculation is not noticeably slower if you replace the <code>DIST<\/code> calculation with the standard <code>ABS<\/code> function. This would be more readable for modern audiences:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10    Z=Z**2+C\n\n      COUNT=COUNT+1\n      IF ((ABS(Z).LT.2.0).AND.(COUNT.LT.LIMIT)) GOTO 10<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Programming isn&#8217;t all about code, sometimes it&#8217;s about the decisions you make<\/p>\n","protected":false},"author":33,"featured_media":2949,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[798,150],"tags":[522,152],"class_list":["post-14541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fortran-77","category-programming","tag-fortran77","tag-programming"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14541"}],"version-history":[{"count":4,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14541\/revisions"}],"predecessor-version":[{"id":14545,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/14541\/revisions\/14545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2949"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}