470,855 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

question about cx_Oracle .thanks

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
Jul 18 '05 #1
7 1812
coolmenu wrote:
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
Jul 18 '05 #2
David Fraser <da****@sjsoft.com> wrote in message news:<cb**********@ctb-nnrp2.saix.net>...
coolmenu wrote:
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?
Jul 18 '05 #3

"coolmenu" <me******@yahoo.com.cn> wrote in message
news:a6**************************@posting.google.c om...
David Fraser <da****@sjsoft.com> wrote in message

news:<cb**********@ctb-nnrp2.saix.net>...
coolmenu wrote:
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.
x = 42055.66
x 42055.660000000003 print "the answer is %.2f" % (x)

the answer is 42055.66
Jul 18 '05 #4
Paul Watson wrote:
"coolmenu" <me******@yahoo.com.cn> wrote in message
news:a6**************************@posting.google.c om...
David Fraser <da****@sjsoft.com> wrote in message


news:<cb**********@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/genera...-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!
Jul 18 '05 #5
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
Jul 18 '05 #6
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):
>>> 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!

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.
- this is an accurate decimal representation of the binary value:
>>> print repr(42055.66)

42055.660000000003

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

Jul 18 '05 #7
Thanks all;-)

but how can i select number from oracle?
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by GrayGeek | last post: by
1 post views Thread by Greg Lindstrom | last post: by
1 post views Thread by jmdeschamps | last post: by
4 posts views Thread by infidel | last post: by
1 post views Thread by lblr33 | last post: by
4 posts views Thread by MooMaster | last post: by
4 posts views Thread by Richard Schulman | last post: by
7 posts views Thread by Carl K | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.