Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

round-off error

 [1/2] from: larry:ecotope at: 4-Jul-2001 21:05


Hi all, Joel mentioned in a recent post that floating-point round-off error can lead to different results when summing a list of numbers of widely varying magnitudes depending on the order in which they are summed. I found an extreme (and extremely artificial) example in a recent Scheme programming book. Consider the following list: janus: [31 2e+34 -1.2345678901235e+80 2749 2939234 -2e+33 3.2e+270 17 -2.4e+270 4.2344294738446e+170 1 -8e+269 0 99]
>> sum: 0 foreach item janus [sum: sum + item] sum
== -1.2345678901235E+80
>> sum: 0 foreach item head reverse janus [sum: sum + item] sum
== 99 Cheers -Larry

 [2/2] from: larry::ecotope::com at: 4-Jul-2001 21:17


Oops, Sorry, I wasn't paying attention to the destructive actions of reverse. The correct answers are 99 for the sum of the numbers from left to right as given in the definition of janus. The large negative value is the sum when they are added from right to left. Another interesting result shows the constructed nature of the list is:
>> sum: 0 foreach item sort janus [sum: sum + item] sum
== 0 -Larry