[code.view]

[top] / python / PyMOTW / math / math_fsum.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Calculating accurate sums of floating point values.
     """
     #end_pymotw_header
     
     import math
     
     values = [ 0.1 ] * 10
     
     print 'Input values:', values
     
     print 'sum()       : {:.20f}'.format(sum(values))
     
     s = 0.0
     for i in values:
         s += i
     print 'for-loop    : {:.20f}'.format(s)
         
     print 'math.fsum() : {:.20f}'.format(math.fsum(values))
     

[top] / python / PyMOTW / math / math_fsum.py

contact | logmethods.com