classification
Title: calculation bug in long() function
Type: behavior
Components: None Versions: Python 2.5, Python 2.4, Python 2.3, Python 2.2.3, Python 2.2.2, Python 2.2.1, Python 2.2, Python 2.1.2, Python 2.1.1
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: facundobatista, must21
Priority: Keywords:

Created on 2008-02-18 12:18 by must21, last changed 2008-02-18 12:55 by must21.

Messages
msg62523 (view) Author: Markus Stoll (must21) Date: 2008-02-18 12:18
betrag = 146.95
betrag = float(betrag)
betrag = betrag * 100.0
betrag = long(betrag)
print betrag

gives 14694 as result rather than 14695
msg62524 (view) Author: Facundo Batista (facundobatista) Date: 2008-02-18 12:25
In short: 

>>> long(100 * 146.95)
14694L

This is NOT a bug, but a behaviour of binary floating point:

>>> 146.95
146.94999999999999

In binary you can not express this number exactly.

>>> 146.95 * 100
14694.999999999998

When you long() that, you truncate the number, so it goes to 14694.

Please address further discussion through python-list.

Thanks!
msg62526 (view) Author: Markus Stoll (must21) Date: 2008-02-18 12:55
thank you for fast reply

I see the point and do not want to argue about that (rounding is fine  
for me). I just think this behaviour makes the long() function pretty  
much useless.

Regards, Markus
Am 18.02.2008 um 13:25 schrieb Facundo Batista:

>
> Facundo Batista added the comment:
>
> In short:
>
>>>> long(100 * 146.95)
> 14694L
>
> This is NOT a bug, but a behaviour of binary floating point:
>
>>>> 146.95
> 146.94999999999999
>
> In binary you can not express this number exactly.
>
>>>> 146.95 * 100
> 14694.999999999998
>
> When you long() that, you truncate the number, so it goes to 14694.
>
> Please address further discussion through python-list.
>
> Thanks!
>
> ----------
> nosy: +facundobatista
> resolution:  -> invalid
> status: open -> closed
>
> __________________________________
> Tracker <report@bugs.python.org>
> <http://bugs.python.org/issue2140>;
> __________________________________
History
Date User Action Args
2008-02-18 12:55:36must21setmessages: + msg62526
2008-02-18 12:25:04facundobatistasetstatus: open -> closed
resolution: invalid
messages: + msg62524
nosy: + facundobatista
2008-02-18 12:18:18must21setmessages: + msg62523
2008-02-18 12:18:06must21create