Issue 18613: wrong float plus/minus op result (original) (raw)

Issue18613

Created on 2013-08-01 12:01 by Laurent.Fournier, last changed 2022-04-11 14:57 by admin. 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
2022-04-11 14:57:48 admin set github: 62813
2013-08-01 12:48:51 brett.cannon set status: open -> closednosy: + brett.cannonmessages: + resolution: not a bug
2013-08-01 12:16:30 Laurent.Fournier set messages: + title: wrong float minus op result -> wrong float plus/minus op result
2013-08-01 12:01:51 Laurent.Fournier create