classification
Title: wrong float plus/minus op result
Type: behavior Stage:
Components: Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Laurent.Fournier, brett.cannon
Priority: normal Keywords:

Created on 2013-08-01 12:01 by Laurent.Fournier, last changed 2013-08-01 12:48 by brett.cannon. This issue is now closed.

Messages (3)
msg194050 - (view) Author: Laurent Fournier (Laurent.Fournier) Date: 2013-08-01 12:01
Python 3.3.1 (with GCC 4.7.3) on Linux

print (.3-.1)
0.199999999999999998
msg194053 - (view) Author: Laurent Fournier (Laurent.Fournier) Date: 2013-08-01 12:16
also with addition !

print (.05+.01)
0.0600000000000000005
msg194060 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-08-01 12:48
This is actually expected because that is just how floating point works in programming (see http://en.wikipedia.org/wiki/Floating_point for all the gnarly details).

If you want exact decimal arithmetic, use the decimal module (which got significantly faster in Python 3.3)::

>>> import decimal
>>> decimal.Decimal('.3') - decimal.Decimal('.1')
Decimal('0.2')
>>> decimal.Decimal('.05') + decimal.Decimal('.01')
Decimal('0.06')
History
Date User Action Args
2013-08-01 12:48:51brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg194060

resolution: not a bug
2013-08-01 12:16:30Laurent.Fourniersetmessages: + msg194053
title: wrong float minus op result -> wrong float plus/minus op result
2013-08-01 12:01:51Laurent.Fourniercreate