[New-bugs-announce] [issue27697] Obscure bug in the int() function

nathan snobelen report at bugs.python.org
Fri Aug 5 17:57:18 EDT 2016


New submission from nathan snobelen:

Hi, I've noticed a bug in int() when converting a specific range of numbers it will incorrectly round the last digit down.

We have some payment code which formats numbers for processing in our system and we noticed that the payment of 1108431.38 was dropped by a penny to 1108431.37.  I looked into it and found that it is dropped when it is multiplied by 100 (to remove the decimal) and then converted back to an int.

Note, this bug only applies to the following range of numbers: 1108431.38 - 1108431.41.  Any other number I tried was unaffected. 

The following code will replicate the bug:

import math

amount1 = 110843138.0
amount2 = 1108431.38 * 100

print "Orig amount1 " + str(amount1)
print "Orig amount2 " + str(amount2)

print "Converted amount1 " + str(int(amount1))
print "Converted amount2 " + str(int(amount2))

Try it, and you will see that "amount1" remains correct, but "amount2" is affected.  Multiplying by 100 seems to trigger it... however note that even after it has been multiplied by 100 it is still correct... it's only when you then apply the int() function that the penny drops.

So it would appear that something is wrong in the int() function.

Cheers,
Nathan

----------
components: Library (Lib)
messages: 272062
nosy: nathan snobelen
priority: normal
severity: normal
status: open
title: Obscure bug in the int() function
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27697>
_______________________________________


More information about the New-bugs-announce mailing list