[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

too long float

J. Peng

1/18/2008 5:55:00 AM

hello,

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.

this is my python version:

>>> sys.version
'2.3.4 (#1, Feb 6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'
7 Answers

Chris

1/18/2008 6:13:00 AM

0

On Jan 18, 7:55 am, "J. Peng" <peng....@gmail.com> wrote:
> hello,
>
> 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.
>
> this is my python version:
>
> >>> sys.version
>
> '2.3.4 (#1, Feb 6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'


>>> 3.9
3.8999999999999999
>>> 3.9 == 3.8999999999999999
True

http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inac...

Dennis Lee Bieber

1/18/2008 7:49:00 AM

0

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/

J. Peng

1/18/2008 8:05:00 AM

0

thanks all!

On Jan 18, 2008 3:49 PM, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> 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/
>
> --
> http://mail.python.org/mailman/listinfo/p...
>

Bart Ogryczak

1/20/2008 3:42:00 PM

0

On 2008-01-18, citizen J. Peng testified:
> hello,
>
> why this happened on my python?

>>>> a=3.9
>>>> a
> 3.8999999999999999

>>> a = 3.9
>>> print a
3.9

bart
--
"PLEASE DO *NOT* EDIT or poldek will hate you." - packages.dir (PLD)
http://candajon.azorrag... http://azorragarse.cand...

Albert van der Horst

2/2/2008 4:38:00 PM

0

In article <slrnfp6ri0.lip.B.Ogryczak@localhost.localdomain>,
Bart Ogryczak <z-niusow@bart.w-wa.pl> wrote:
>On 2008-01-18, citizen J. Peng testified:
>> hello,
>>
>> why this happened on my python?
>
>>>>> a=3.9
>>>>> a
>> 3.8999999999999999
>
>>>> a = 3.9
>>>> print a
>3.9

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/floa...

Groetjes Albert


>
>bart
>--
>"PLEASE DO *NOT* EDIT or poldek will hate you." - packages.dir (PLD)
>http://candajon.azorrag... http://azorragarse.cand...


--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van...

Steve Holden

2/2/2008 7:15:00 PM

0

Albert van der Horst wrote:
> In article <slrnfp6ri0.lip.B.Ogryczak@localhost.localdomain>,
> Bart Ogryczak <z-niusow@bart.w-wa.pl> wrote:
>> On 2008-01-18, citizen J. Peng testified:
>>> hello,
>>>
>>> why this happened on my python?
>>>>>> a=3.9
>>>>>> a
>>> 3.8999999999999999
>>>>> a = 3.9
>>>>> print a
>> 3.9
>
> 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/floa...
>
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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Grant Edwards

2/2/2008 7:23:00 PM

0

On 2008-02-02, Steve Holden <steve@holdenweb.com> wrote:

>> Apparently you don't know the first thing about floating point
>> numbers. I suggest reading the wikipedia entry.
>> http://en.wikipedia/wiki/floa...
>
> 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.

--
Grant Edwards grante Yow! I love ROCK 'N ROLL!
at I memorized the all WORDS
visi.com to "WIPE-OUT" in 1965!!