question about cx_Oracle .thanks

C

coolmenu

Hi
i hava a db ORACLE 10G,a table valid_card
(card_no,varchar2(10),now_balance number (12,2))

its'some record in table ('7188','42055.66')

i use cx_Oracle to select now_balance from table

curobj.execute("select loan_amount from valid_card where
card_no='7181930166881974'");
(42505.660000000003)

why 42505.66---->42505.6600000000003???
thanks
 
D

David Fraser

coolmenu said:
Hi
i hava a db ORACLE 10G,a table valid_card
(card_no,varchar2(10),now_balance number (12,2))

its'some record in table ('7188','42055.66')

i use cx_Oracle to select now_balance from table

curobj.execute("select loan_amount from valid_card where
card_no='7181930166881974'");
[ said:
tuple=curobj.fetchone()
tuple

(42505.660000000003)

why 42505.66---->42505.6600000000003???
thanks

This is because of the conversion of the float to decimal. Float objects
have limited accuracy. I don't think it's much to do with cx_Oracle

David
 
C

coolmenu

David Fraser said:
coolmenu said:
Hi
i hava a db ORACLE 10G,a table valid_card
(card_no,varchar2(10),now_balance number (12,2))

its'some record in table ('7188','42055.66')

i use cx_Oracle to select now_balance from table

curobj.execute("select loan_amount from valid_card where
card_no='7181930166881974'");
[ said:
tuple=curobj.fetchone()
tuple

(42505.660000000003)

why 42505.66---->42505.6600000000003???
thanks

This is because of the conversion of the float to decimal. Float objects
have limited accuracy. I don't think it's much to do with cx_Oracle

David


Can someone give me a advice? how can i do?
donnt select number from oracle?
 
P

Paul Watson

coolmenu said:
David Fraser <[email protected]> wrote in message
coolmenu said:
Hi
i hava a db ORACLE 10G,a table valid_card
(card_no,varchar2(10),now_balance number (12,2))

its'some record in table ('7188','42055.66')

i use cx_Oracle to select now_balance from table

curobj.execute("select loan_amount from valid_card where
card_no='7181930166881974'");
[<NumberVar object at 0x013A6920>]

tuple=curobj.fetchone()
tuple

(42505.660000000003)

why 42505.66---->42505.6600000000003???
thanks

This is because of the conversion of the float to decimal. Float objects
have limited accuracy. I don't think it's much to do with cx_Oracle

David


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.
the answer is 42055.66
 
S

Steve Holden

Paul said:
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.

42055.660000000003

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:
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!
 
P

Peter Hansen

Steve said:
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):
'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):
(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:

42055.660000000003

All very true. ;-)

-Peter
 
S

Steve Holden

Peter Hansen wrote:
[...]
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):

(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!
Just to make things even more complicated, of course, we must also
remember that the delta between two adjacent floating-point numbers
isn't constant, but will vary according to the magnitude of the numbers.
All very true. ;-)
And it just goes to show that floating-point is quite confusing enough
to make many a beginner run away in fright!

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top