question about cx_Oracle .thanks

Steve Holden sholden at holdenweb.com
Fri Jun 25 08:27:27 EDT 2004


Paul Watson wrote:
> "coolmenu" <menucool at yahoo.com.cn> wrote in message
> news:a6a2957e.0406250233.402c2ddd at posting.google.com...
> 
>>David Fraser <davidf at sjsoft.com> wrote in message
> 
> news:<cbghcs$5m3$1 at ctb-nnrp2.saix.net>...
> 
>>>coolmenu wrote:
>>>
[...]
>>>>>>>tuple=curobj.fetchone()
>>>>>>>tuple
>>>>
>>>>(42505.660000000003)
>>>>
>>>>why 42505.66---->42505.6600000000003???
[...]

>>
>>Can someone give me a advice? how can i do?
>>donnt select number from oracle?
> 
> 
> This has nothing to do with Oracle.  This is the problem of representing
> floating point numbers on a binary system.  You may need to format the
> number for presentation.
> 
> 
>>>>x = 42055.66
>>>>x
> 
> 42055.660000000003
> 
>>>>print "the answer is %.2f" % (x)
> 
> the answer is 42055.66
> 
And, in fact, more generally, see:

http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate

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 
- this is an accurate decimal representation of the binary value:

 >>> print repr(42055.66)
42055.660000000003

Even more confusingly, when you try it on a SPARC processor you may get 
slightly different answers. Wise programmers always round monetary 
calculations at every step along the way[1].

regards
  Steve

[1] Yes, I know this is a simplification, please let's not have yet 
another repeat of the eternal thread. Allow some room for pedagogy here!



More information about the Python-list mailing list