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

gh-106498: Fix an extreme case in colorsys.rgb_to_hls #106530

Closed

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Jul 7, 2023

colorsys.rgb_to_hls(1, 1, 0.9999999999999999) raises ZeroDivisionError now (this probably is the ONLY case). Not sure if it's worth fixing, but I think "correct" in all cases is more important than "very slightly faster in theory".

This optimization is originally introduced in #23306, using the benchmark given in the PR, there's no observable performance regression with the extra check.

Benchmark Code
# /usr/bin/env python

import timeit
import statistics
from Lib.test.test_colorsys import ColorsysTest


if __name__ == '__main__':
    number = 100
    repeat = 100
    results = timeit.repeat('ColorsysTest().test_hls_roundtrip()',
                    setup='from Lib.test.test_colorsys import ColorsysTest',
                    number=number,
                    repeat=repeat)

    running_times = list(sorted(results))

    mean = statistics.mean(running_times)
    std = statistics.stdev(running_times)
    print(f"Running time: {mean} ± {std} ms.  "
    f"(number, repeat) = ({number}, {repeat})")

Another possible implementation is to do a try-except block for the division. As try is almost no cost now, it would be theoretically faster than the current one. The return value would be different (a near white color can be represented in many forms in hls space). I prefer the current solution as it's easy to understand with the comment, but I do not oppose the other way.

@gaogaotiantian
Copy link
Member Author

@terryjreedy as the reviewer of the related PR, could you share thoughts on this matter? Thanks!

Copy link

@nedveder nedveder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minc == maxc and sumc == 2.0 are checking for the same thing - preventing division by zero. Another possible fix would be to check if l==1 on line 91.

@terryjreedy
Copy link
Member

@gaogaotiantian Thank you for finding the change that caused the regression (which I merged, not just reviewed). As explained on this issue, the fix is incorrect. I am preparing a new PR with a different test addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants