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

Bruce Southey bsouthey@gmail....
Fri Apr 4 08:42:52 CDT 2008


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

Simply put, in generally, you can not just compare floating point 
numbers because of the variable precision by storage precision (see the 
different types supported by NumPy: such as 'float', 'float128', 
'float32', 'float64'), OS and platform architecture. Thus, your tests 
are incorrectly implemented but hopefully something like NumPy's 
allclose() (http://www.scipy.org/Numpy_Example_List_With_Doc#allclose ) 
should be more appropriate.

Bruce




Stéfan van der Walt wrote:
> Hi Will
>
> On 03/04/2008, Will Lee <lee.will@gmail.com> wrote:
>   
>> I seem to have problem with floating point printing with the latest numpy,
>> python 2.5.2, gcc 4.1.4, and 64-bit linux:
>>
>> In [24]: print str(0.0012)
>> 0.0012
>>
>> In [25]: a = numpy.array([0.0012])
>>
>>  In [26]: print str(a[0])
>> 0.0011999999999999999
>>
>> In [27]: print numpy.__version__
>> 1.0.5.dev4950
>>
>> It seems like the str() behavior for float64 in the latest numpy's behavior
>> is different than Python's default behavior (and previous numpy's behavior).
>>
>>
>> As I have numerous doc tests, this seems to make many of them failed.
>> Should the float64's str() behavior be consistent with what's described in
>> http://docs.python.org/tut/node16.html?  Is there any way
>> to get around it so I can get to the default Python behavior?
>>     
>
> As a workaround, you can simply remove 'str' from your print statement:
>
> In [17]: print x[0]
> 0.0012
>
> I am not sure why str([x[0]) is equivalent to repr(0.0012).
>
> Regards
> Stéfan
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>   



More information about the Numpy-discussion mailing list