Decimal vs float

Fredrik Lundh fredrik at pythonware.com
Thu Jan 19 08:59:56 EST 2006


Kay Schluehr wrote:

> This is interesting. If we define
>
> def f():
>    print str(1.1)
>
> and disassemble the function, we get:
>
> dis.dis(f)
>  2           0 LOAD_GLOBAL              0 (str)
>              3 LOAD_CONST               1 (1.1000000000000001)      # huh?

huh huh?

>>> str(1.1)
'1.1'
>>> repr(1.1)
'1.1000000000000001'
>>> "%.10g" % 1.1
'1.1'
>>> "%.20g" % 1.1
'1.1000000000000001'
>>> "%.30g" % 1.1
'1.1000000000000001'
>>> "%.10f" % 1.1
'1.1000000000'
>>> "%.20f" % 1.1
'1.10000000000000010000'
>>> "%.30f" % 1.1
'1.100000000000000100000000000000'

more here: http://docs.python.org/tut/node16.html

</F> 






More information about the Python-list mailing list