[New-bugs-announce] [issue9829] Unexpected Fraction.from_float() Behavior

bsidhom report at bugs.python.org
Sat Sep 11 07:06:26 CEST 2010


New submission from bsidhom <bsidhom at gmail.com>:

The Fractions.from_float() call returns an incorrect value when run on certain numbers. Some test runs have been listed below. Note that this problem does not exist when the same float is converted to a string and then passed to the standard Fraction() constructor; I do not know if this is an inherent problem due to the structure of the float itself or an implementation issue.

>>> for x in [1.23, 1.24, 1.25, 1.26, 1.27]:
	print(Fraction.from_float(x))
	print(Fraction(str(x)))

	
2769713770832855/2251799813685248
123/100
5584463537939415/4503599627370496
31/25
5/4
5/4
5674535530486825/4503599627370496
63/50
2859785763380265/2251799813685248
127/100

The problem does not exist if a string literal is passed either:

>>> print(Fraction('1.23'))
123/100

----------
components: Library (Lib)
messages: 116076
nosy: bsidhom
priority: normal
severity: normal
status: open
title: Unexpected Fraction.from_float() Behavior
type: behavior
versions: Python 3.1

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


More information about the New-bugs-announce mailing list