too long float

J. Peng peng.kyo at gmail.com
Fri Jan 18 03:04:31 EST 2008


thanks all!

On Jan 18, 2008 3:49 PM, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Fri, 18 Jan 2008 13:55:17 +0800, "J. Peng" <peng.kyo at 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 at ix.netcom.com           wulfraed at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-asst at bestiaria.com)
>                 HTTP://www.bestiaria.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list