[Numpy-discussion] problem with float64's str()
Fri Apr 4 09:55:13 CDT 2008
Note that at least under Python 2.5.1:
From Python docs, repr(): 'Return a string containing a printable
representation of an object' and str(): 'Return a nice string
representation of the object'. In this case the object is a Python
float that approximates the base 10 number of 0.0012. This is not
equivalent to convert the base 10 number 0.0012 to a string because
computer numbers are base 2. Thus, repr() converts a Python object into
a string but nothing about the numerical precision whereas str() tries
to do something about maing a 'nice string'.
The only consistent way to get 0.0012 (or any number that can not be
represented exactly) is to use a Python object that stores 0.0012
exactly. For example, the Decimal module was introduced in Python 2.4:
Also, see PEP 3141 'A Type Hierarchy for Numbers'
Dag Sverre Seljebotn wrote:
> Bruce Southey wrote:
>> 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
> Wasn't this discussion about the repr vs. str functions?
> >>> repr(0.0012)
> >>> str(0.0012)
> Dag Sverre
> Numpy-discussion mailing list
More information about the Numpy-discussion