8000 colorsys.rgb_to_hls raises ZeroDivisionError on nearly-white inputs · Issue #106498 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
colorsys.rgb_to_hls raises ZeroDivisionError on nearly-white inputs #106498
Closed
@dfremont

Description

@dfremont

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 zero

The 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0