[Numpy-discussion] problem with float64's str()

Will Lee lee.will at gmail.com
Fri Apr 4 10:56:28 EDT 2008


I understand the implication for the floating point comparison and the need
for allclose.  However, I think in a doctest context, this behavior makes
the doc much harder to read.  For example, if you have this in your doctest:

def doSomething(a):
    '''
    >>> print doSomething(0.0011)[0]
    >>> 0.0012
    '''
    return numpy.array([a]) + 0.0001

In the current numpy, you'll have to write:

def doSomething(a):
    '''
    >>> print doSomething(0.0011)[0]
    >>> 0.0011999999999999999
    '''
    return numpy.array([a]) + 0.0001

Using allclose, you'll need to write it like:

def doSomething(a):
    '''
    >>> print numpy.allclose(doSomething(0.0011)[0], 0.0012)
    >>> True
    '''
    return numpy.array([a]) + 0.0001

I don't think either cases are ideal.  You can also imagine if you're
printing out a string with a float in it (like 'You got 0.0012 back'), then
you can't really use allclose at all.

Also, it's somewhat odd that the behavior for str is different for a
numpy.float64 and python's float, since otherwise they're mostly the same.

Will

On Fri, Apr 4, 2008 at 8:52 AM, Dag Sverre Seljebotn <
dagss at student.matnat.uio.no> wrote:

> Bruce Southey wrote:
> > Hi,
> > This topic has come up many times and the only problem is the lack of
> > understanding how computers store numbers and computer numerical
> precision.
> >
> > The NumPy output is consistent with Python on my x86_64 linux system
> > with Python 2.5.1:
> >  >>> a=0.0012
> >  >>> a
> > 0.0011999999999999999
> >
> Wasn't this discussion about the repr vs. str functions?
>
>  >>> repr(0.0012)
> '0.0011999999999999999'
>  >>> str(0.0012)
> '0.0012'
>
>
>
> Dag Sverre
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080404/1a5cd4b4/attachment.html>


More information about the NumPy-Discussion mailing list