-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
colorsys.rgb_to_hls raises ZeroDivisionError on nearly-white inputs #106498
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.The issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
In Python 3.10 and 3.11, the colorsys.rgb_to_hls function divides by zero when given a valid color which is very close to but not exactly pure white.
Python 3.11.1 (main, Feb 8 2023, 15:39:14) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import colorsys
>>> colorsys.rgb_to_hls(1, 1, 0.9999999999999999)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/dfremont/.pyenv/versions/3.11.1/lib/python3.11/colorsys.py", line 86, in rgb_to_hls
s = rangec / (2.0-sumc)
~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: float division by zeroThe current code assumes that, given 3 numbers between 0 and 1, if their minimum and maximum are not equal, then the sum of the minimum and maximum must be strictly less than 2. That claim is true for real numbers but not for floating-point arithmetic. The example above fails because 1 + 0.9999999999999999 == 2.0.
Your environment
- CPython versions tested on: 3.8.8 (works); 3.9.13 (works); 3.10.3 (buggy); 3.11.1 (buggy)
- Operating system and architecture: macOS 12.6.6; x86_64
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.The issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error