simple float numbers problem

V

Vio

I need to test for equality between simple 2 decimal numbers. For example:

if (10 + 15.99) == 25.99:
do some stuff...

The preceding sentence should be TRUE, but to Python it appears FALSE.
Which is wrong.

Perhaps because Python translates "25.99" to "25.98999999999999998" and
not "25.99", which may be the reason for this error (me guessing...). If
that's the case, how do I force Python to only use 2 decimal points, and
not "make up" superfluous decimals? Or if that's not the cause for the
problem, how do I make Python see my math expression as TRUE (as it
"should" be)?

Cheers,
Vio

PS. If it's of any help, I'm using Python2.3 (GCC 2.95.4 20011002
(Debian prerelease))
 
D

Dennis Lee Bieber

Vio fed this fish to the penguins on Friday 07 November 2003 05:34 am:
Perhaps because Python translates "25.99" to "25.98999999999999998"
and not "25.99", which may be the reason for this error (me
guessing...). If that's the case, how do I force Python to only use 2
decimal points, and not "make up" superfluous decimals? Or if that's
not the cause for the problem, how do I make Python see my math
expression as TRUE (as it "should" be)?
You don't... Computer floating point arithmetic is not precise. {I
seem to have misplaced my copy, but there is a book with a title
something like "Real Math Made Real" that discusses this}

The recommendation from my old computer classes is that you never test
floats for equality -- you test the difference between floats against
some acceptable error limit.

epsilon = 0.0000001
if abs( (10 + 15.99) - 25.99) < epsilon:
# considered equal

-=-=-=-=-=-=-
Python 2.2 (#1, Nov 5 2002, 15:43:24)
[GCC 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more information..... print a, " is equal to ", 25.99
....
25.99 is equal to 25.99


--
 
T

Tim Roberts

Vio said:
I need to test for equality between simple 2 decimal numbers. For example:

if (10 + 15.99) == 25.99:
do some stuff...

The preceding sentence should be TRUE, but to Python it appears FALSE.
Which is wrong.

And the same thing would happen if you wrote the same sentence in C. The
problem is that 15.99 in binary is an infinitely repeating decimal. It
cannot be represented exactly.
If that's the case, how do I force Python to only use 2 decimal points,
and not "make up" superfluous decimals?

There are two good solutions. One, as others have suggested, is to store
everything in units of cents. Your expression becomes:

if 1000 + 1599 == 2599:
do some stuff...

All you need to do is translate when you do input and output. This is the
approach Visual Basic takes with its Currency data type (although it
actually multiples by 1000, not 100).

Another solution is to use a "CloseTo" function to do the comparison:

def CloseTo(x,y):
return abs(x-y) < 0.005

if (10.00 + 15.99, 25.99):
do some thuff...
Or if that's not the cause for the problem, how do I make
Python see my math expression as TRUE (as it "should" be)?

In a binary computer, your statement is false. Just that simple. You need
to find a way to express your statement in a way that produces the results
you want.
 
B

Ben Finney

Another solution is to use a "CloseTo" function to do the comparison:

def CloseTo(x,y):
return abs(x-y) < 0.005

A modified CloseTo() that allows a varying epsilon:

def CloseTo( x, y, epsilon=0.005 ):
return ( abs( x - y ) < epsilon )
 
L

Ladvánszky Károly

Lisp does the right thing:
(= (+ 10 15.99) 25.99)
T

Does Lisp not use the math processor?
 
C

Christopher A. Craig

Ladvánszky Károly said:
Lisp does the right thing:
(= (+ 10 15.99) 25.99)
T

Does Lisp not use the math processor?

I don't know which Lisp you're speaking of here, but chances are very
good that 15.99 is not a float in this particular lisp. I'd guess
it's probably rational. You can do that with Python if you get a
rational library such as the one I mentioned in my previous post to
this thread. The problem is that rationals are notoriously slow. If
you tried to implement

a = 0
for i in range(1,5000):
a += 1./i

in this lisp, and I'm right, it should take longer than Python (but
will give the "right" answer instead of Python's approximate one)
 
L

Ladvánszky Károly

I'm running CLISP.
(type-of 25.99) gives SINGLE-FLOAT
(type-of (+ 10 15.99)) gives SINGLE-FLOAT
I'm going to post it on the List group to see what Lispers say about it.
By the way, Smalltalk also gives the right answer.
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top