too long float

J

J. Peng

hello,

why this happened on my python?
3.8999999999999999

I wanted 3.9 but got 3.89................
How to avoid it? thanks.

this is my python version:
'2.3.4 (#1, Feb 6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'
 
D

Dennis Lee Bieber

why this happened on my python?

3.8999999999999999

I wanted 3.9 but got 3.89................
How to avoid it? thanks.
Avoid it? You don't... You alleviate the concern by understanding
that floating point is only precise if the value is a fraction of 2: 1,
0.5, 0.25, 0.125...

Computer Science recommends that one does NOT compare two floats for
equality -- instead one should compare the absolute value of the
difference of the two floats against some required epsilon (ie, how far
apart two floats can be and still be considered equal...
abs(f1 - f2) < 0.000001
for example)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

J. Peng

thanks all!

Avoid it? You don't... You alleviate the concern by understanding
that floating point is only precise if the value is a fraction of 2: 1,
0.5, 0.25, 0.125...

Computer Science recommends that one does NOT compare two floats for
equality -- instead one should compare the absolute value of the
difference of the two floats against some required epsilon (ie, how far
apart two floats can be and still be considered equal...
abs(f1 - f2) < 0.000001
for example)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Steve Holden

Albert said:
This has nothing to do with python.

Apparently you don't know the first thing about floating point
numbers. I suggest reading the wikipedia entry.
http://en.wikipedia/wiki/floating_point
Don't think you are the first person to make this mistake, by the way.
Despite Albert's tone suggesting that *everybody* knows about floating
point this is a very common issue among new programmers.

regards
Steve
 
G

Grant Edwards

Don't think you are the first person to make this mistake, by the way.
Despite Albert's tone suggesting that *everybody* knows about floating
point this is a very common issue among new programmers.

And even among old programmers who've not used floating point
much.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top