sum for sequences?

Mel mwilson at
Tue Mar 30 15:53:52 CEST 2010

Patrick Maupin wrote:

> Because sum() is the obvious way to sum floats; now the existence of
> math.fsum() means there are TWO obvious ways to sum floats.  Is that
> really that hard to understand?  How can you misconstrue this so badly
> that you write something that can be (easily) interpreted to mean that
> you think that I think that once math.fsum() exists, sum() doesn't
> even exist any more????

floats are nasty -- as evidence the recent thread on comparing floats for 
equality.  People use floats when they have to.  fsum exists because of 

mwilson at tecumseth:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import fsum
>>> a=(1.0e200, 156.0, -1.0e200)
>>> sum(a)
>>> fsum(a)

You could generalize sum, but after that, there's a case that even fsum 
can't handle:

>>> ni=1.0e200+1.0j
>>> nj=1.0+1.0e200j
>>> ai=(ni, nj, 156.0+651.0j, -ni, -nj)
>>> sum(ai)
>>> fsum(ai)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float; use abs(z)


More information about the Python-list mailing list