Floating point errors?
Des Small
madms at maelstrom.maths.bris.ac.uk
Thu May 10 10:54:05 EDT 2001
On Thu, 10 May 2001 14:36:59 GMT, Ben Allfree <cm at bldigital.com> wrote:
>Using Python 2.1 on Win32:
>
>int( (2.3 - 2) * 1000 ) = 299
>int( .3 * 1000 ) = 300
>
>What am I missing? The first one seems too simple to expect a rounding
>error :)
>
Not from here it doesn't:
>>> 2.3-2
0.29999999999999982
>>> (2.3-2)*1000
299.99999999999983
>>> int(299.99999999999983)
299
According to Learning Python 'int' truncates its argument, which may not round
the way you expect.
So try rounding first:
>>> round((2.3-2)*1000)
300.0
>>> int(round((2.3-2)*1000))
300
Cheers,
Des,
had an interpreter running.
More information about the Python-list
mailing list