Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected Fraction.from_float() Behavior #54038

Closed
bsidhom mannequin opened this issue Sep 11, 2010 · 2 comments
Closed

Unexpected Fraction.from_float() Behavior #54038

bsidhom mannequin opened this issue Sep 11, 2010 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@bsidhom
Copy link
Mannequin

bsidhom mannequin commented Sep 11, 2010

BPO 9829
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-09-11.14:00:03.392>
created_at = <Date 2010-09-11.05:06:24.259>
labels = ['invalid', 'type-bug', 'library']
title = 'Unexpected Fraction.from_float() Behavior'
updated_at = <Date 2010-09-11.14:00:03.368>
user = 'https://bugs.python.org/bsidhom'

bugs.python.org fields:

activity = <Date 2010-09-11.14:00:03.368>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2010-09-11.14:00:03.392>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2010-09-11.05:06:24.259>
creator = 'bsidhom'
dependencies = []
files = []
hgrepos = []
issue_num = 9829
keywords = []
message_count = 2.0
messages = ['116076', '116096']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'bsidhom']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue9829'
versions = ['Python 3.1']

@bsidhom
Copy link
Mannequin Author

bsidhom mannequin commented Sep 11, 2010

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

@bsidhom bsidhom mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 11, 2010
@bitdancer
Copy link
Member

Please read about floating point arithmetic in the tutorial:

http://docs.python.org/tutorial/floatingpoint.html

Also observe that this works:

    >>> Fraction(Decimal('1.23'))
    Fraction(123, 100)

So yes, it is a limitation in how floating point is represented...in all languages and platforms that use binary floating point.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant