question about cx_Oracle .thanks

Peter Hansen peter at engcorp.com
Fri Jun 25 09:02:34 EDT 2004


Steve Holden wrote:

> which you will (I hope) appreciate does not just apply to Python. Python 
> is confusing you by showing you the contents of your tuple as accurately 
> as possible: your computer actually stores 4205.66 as 42505.660000000003 

Or, more precisely, it stores it as the binary number represented
here by these hexadecimal digits (shown here in big-endian format):

 >>> binascii.hexlify(struct.pack('>d', 42055.66))
'40e488f51eb851ec'

Since it has a limited number of binary digits available, it can't
store precisely 42055.66, so 42055.6600000whatever is the next
closest.  To demonstrate, the nearest neighbours that can be
represented accurately are these (binary value one higher or lower):

 >>> struct.unpack('>d', binascii.unhexlify('40e488f51eb851ed'))
(42055.660000000011,)
 >>> struct.unpack('>d', binascii.unhexlify('40e488f51eb851eb'))
(42055.659999999996,)

So clearly the binary can capture only increments of about
0.000000000007 or 0.000000000008 at a time, and all of these
values must be treated as approximations... unless one was
trying to store 42055.660000000003 in the first place!

> - this is an accurate decimal representation of the binary value:
> 
>  >>> print repr(42055.66)
> 42055.660000000003

All very true. ;-)

-Peter



More information about the Python-list mailing list