Correct IFDRational.__float__() return value - #9676
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed
IFDRational.__float__returns the wrong value for a non-integral numerator.IFDRationaldelegates__eq__,__int__,__round__,__repr__and all arithmetic toself._val(the normalizedFraction), but never defined__float__. Sofloat()falls back tonumbers.Rational.__float__, which doesint(self.numerator) / int(self.denominator). For a non-integral numerator the stored_numerator/_denominatorare un-normalized, so the result disagrees with every other accessor:This is reachable via the public
IFDRational(value, denominator)constructor.Fix
Delegate
__float__toself._val, the same way__int__,__round__,__repr__and__eq__are delegated, sofloat()is consistent with the normalized fraction.Tests
Added
test_floattoTests/test_tiff_ifdrational.py: it assertsfloat(IFDRational(1.5, 3)) == 0.5, and that the integral and0/0(nan) cases stay correct. It fails onmain(0.333… != 0.5) and passes with the change;Tests/test_tiff_ifdrational.pyis green (6 passed).ruffandblackclean.