diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 42d06b89696..8cf48ba7dc6 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -1,5 +1,6 @@ from __future__ import annotations +import math from fractions import Fraction from pathlib import Path @@ -38,6 +39,18 @@ def test_sanity() -> None: _test_equal(7, 5, 1.4) +def test_float() -> None: + # float() must agree with ==, int() and repr(), which all use the + # normalized fraction + r = IFDRational(1.5, 3) + assert r == 0.5 + assert float(r) == 0.5 + + # Integer numerator and 0/0 (nan) cases + assert float(IFDRational(4, 2)) == 2.0 + assert math.isnan(float(IFDRational(0, 0))) + + def test_ranges() -> None: for num in range(1, 10): for denom in range(1, 10): diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 5094faa1325..02ce330d034 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -464,6 +464,7 @@ def __setstate__(self, state: list[float | Fraction | IntegralLike]) -> None: __ceil__ = _delegate("__ceil__") __floor__ = _delegate("__floor__") __round__ = _delegate("__round__") + __float__ = _delegate("__float__") # Python >= 3.11 if hasattr(Fraction, "__int__"): __int__ = _delegate("__int__")