Quantcast
Viewing all articles
Browse latest Browse all 14126

The results of math.fsum is different from a simple loop sum in python

I'd like to understand why the two methods of summing a list of floating point numbers differs by exactly 32.0?

import mathnums = [26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.,26015151255025000.]math_fsum_result = math.fsum(nums)loop_result = 0.0for n in nums:    loop_result += nprint(format(math_fsum_result, '.1f'))print(format(loop_result, '.1f'))

Which produces:
234136361295224992.0
234136361295224960.0

It is understood that both are accumulating rounding errors but the inconsistency of exactly 32.0 seems weird.

For the record the correct value calculated with decimals is:
234136361295225000.0


Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>