[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Floating point bug?

robinsiebler@gmail.com

2/13/2008 11:01:00 PM

I've never had any call to use floating point numbers and now that I
want to, I can't!

*** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32
bit (Intel)] on win32. ***
>>> float (.3)
0.29999999999999999
>>> foo = 0.3
>>> foo
0.29999999999999999
>>>
215 Answers

marek.rocki

2/13/2008 11:13:00 PM

0

Not a bug. All languages implementing floating point numbers have the
same issue. Some just decide to hide it from you. Please read
http://docs.python.org/tut/n... and particularly
http://docs.python.org/tut/n...#SECTION0016100000000000000000

Regards,
Marek

Preston Landers

2/14/2008 12:35:00 AM

0

marek.rocki@wp.pl(marek.rocki@wp.pl)@2008.02.13 15:13:20 -0800:
> Not a bug. All languages implementing floating point numbers have the
> same issue. Some just decide to hide it from you. Please read
> http://docs.python.org/tut/n... and particularly
> http://docs.python.org/tut/n...#SECTION0016100000000000000000
>


This is true. Fortunately Python does provide a module which allows
you to work with exact floating point quantities.

http://docs.python.org/lib/module-de...

Of course the downside is that these are not quite as fast as the
built in float type, but probably good enough for most purposes.

Preston

Bjoern Schliessmann

2/14/2008 1:05:00 AM

0

robinsiebler@gmail.com wrote:

> I've never had any call to use floating point numbers and now that
> I want to, I can't!

Ever considered phrasing your actual problem so one can help, let
alone looking at the archive for many, many postings about this
topic?

Regards,


Björn

--
BOFH excuse #66:

bit bucket overflow

Jeff Schwab

2/14/2008 1:49:00 AM

0

robinsiebler@gmail.com wrote:
> I've never had any call to use floating point numbers and now that I
> want to, I can't!
>
> *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32
> bit (Intel)] on win32. ***
>>>> float (.3)
> 0.29999999999999999
>>>> foo = 0.3
>>>> foo
> 0.29999999999999999

A classic (if lengthy) read:
http://docs.sun.com/source/806-3568/ncg_gol...

If you just want to see a pretty representation:

>>> print 0.3
0.3

If you need a pretty string for use in code:

>>> def pretty_fp(fpnum, prec=8):
... return ('%.8f' % fpnum).rstrip('0')
...
>>> pretty_fp(0.3)
'0.3'


Diez B. Roggisch

2/14/2008 8:17:00 AM

0

Preston Landers schrieb:
> marek.rocki@wp.pl(marek.rocki@wp.pl)@2008.02.13 15:13:20 -0800:
>> Not a bug. All languages implementing floating point numbers have the
>> same issue. Some just decide to hide it from you. Please read
>> http://docs.python.org/tut/n... and particularly
>> http://docs.python.org/tut/n...#SECTION0016100000000000000000
>>
>
>
> This is true. Fortunately Python does provide a module which allows
> you to work with exact floating point quantities.

That's a misconception. The decimal-module has a different base (10
instead of 2), and higher precision. But that doesn't change the fact
that it will expose the same rounding-errors as floats do - just for
different numbers.

>>> import decimal as d
>>> d = d.Decimal
>>> d("1") / d("3") * d("3")
Decimal("0.9999999999999999999999999999")
>>>

The advantage is that the rounding errors are the ones expected in
monetary caluclations, which means that you can write correct programs
for such purposes.

Diez

Dennis Lee Bieber

2/14/2008 8:38:00 AM

0

On Wed, 13 Feb 2008 17:49:08 -0800, Jeff Schwab <jeff@schwabcenter.com>
declaimed the following in comp.lang.python:

>
> If you need a pretty string for use in code:
>
> >>> def pretty_fp(fpnum, prec=8):
> ... return ('%.8f' % fpnum).rstrip('0')
> ...
> >>> pretty_fp(0.3)
> '0.3'
>

What's wrong with just

str(0.3)

that's what "print" invokes, whereas the interpreter prompt is using

repr(0.3)

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

Jeff Schwab

2/14/2008 9:12:00 AM

0

Dennis Lee Bieber wrote:
> On Wed, 13 Feb 2008 17:49:08 -0800, Jeff Schwab <jeff@schwabcenter.com>
> declaimed the following in comp.lang.python:
>
>> If you need a pretty string for use in code:
>>
>> >>> def pretty_fp(fpnum, prec=8):
>> ... return ('%.8f' % fpnum).rstrip('0')
>> ...
>> >>> pretty_fp(0.3)
>> '0.3'
>>
>
> What's wrong with just
>
> str(0.3)

Nothing!

> that's what "print" invokes, whereas the interpreter prompt is using
>
> repr(0.3)

Thanks for pointing that out.

Christian Heimes

2/14/2008 9:21:00 AM

0

Dennis Lee Bieber wrote:
> What's wrong with just
>
> str(0.3)
>
> that's what "print" invokes, whereas the interpreter prompt is using
>
> repr(0.3)
>

No, print invokes the tp_print slot of the float type. Some core types
have a special handler for print. The tp_print slot is not available
from Python code and most people don't know about it. :]

Christian

Bruno Desthuilliers

2/14/2008 1:26:00 PM

0

Christian Heimes a écrit :
> Dennis Lee Bieber wrote:
>> What's wrong with just
>>
>> str(0.3)
>>
>> that's what "print" invokes, whereas the interpreter prompt is using
>>
>> repr(0.3)
>>
>
> No, print invokes the tp_print slot of the float type. Some core types
> have a special handler for print. The tp_print slot is not available
> from Python code and most people don't know about it. :]

???

bruno@bruno:~$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Toto(object):
.... def tp_print(self):
.... return "AHAHAHA"
....
>>> t = Toto()
>>> t
<__main__.Toto object at 0xb7db8d6c>
>>> print t
<__main__.Toto object at 0xb7db8d6c>
>>> (0.3).tp_print
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'tp_print'
>>> (0.3).__print__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute '__print__'
>>> (0.3).__tp_print__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute '__tp_print__'
>>>

I Must have miss something...

Christian Heimes

2/14/2008 2:36:00 PM

0

Bruno Desthuilliers wrote:
> I Must have miss something...

Yeah, You have missed the beginning of the third sentence: "The tp_print
slot is not available from Python code". The tp_print slot is only
available in C code and is part of the C definition of a type. Hence tp_
as type.

Search for float_print and tp_print in
http://svn.python.org/view/python/trunk/Objects/floatobject.c?rev=60567&...

Christian