Dennis Lee Bieber
1/18/2008 7:49:00 AM
On Fri, 18 Jan 2008 13:55:17 +0800, "J. Peng" <peng.kyo@gmail.com>
declaimed the following in comp.lang.python:
>
> why this happened on my python?
>
> >>> a=3.9
> >>> a
> 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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/