Skip to content

Conversation

@Juneezee
Copy link
Contributor

Fixes #798.

This PR fixes a panic in the Palette method caused by a floating-point precision error, where the computed value v slightly exceeds the upper bound l.max.

Debugger screenshot

2025-03-22_16-50

Problem

As shown in the above screenshot, we have the following values:

  • l.min = 0.3402859786606234
  • l.max = 15.322841335211892
  • n = 15
  • delta = 1.0701825254679478
  • i = 14
  • v = 15.322841335211894 (note here v is 0.000000000000002 greater than l.max)

The expected calculation of delta:

delta = (l.max - l.min) / (n - 1)
      = (15.322841335211892 - 0.3402859786606234) / (15 - 1)
      = 1.0701825254679478

This confirms that delta is correct and has no floating-point error.

Next, the expected calculation of v:

v = l.min + (delta  * i)
  = 0.3402859786606234 + (1.0701825254679478 * 14)
  = 15.322841335211892

Ideally, v should be exactly equal to l.max. However, due to a small floating-point precision error of 0.000000000000002, v becomes slightly greater than l.max, triggering the panic in checkRange function:

if val > max {
return palette.ErrOverflow
}

Solution

Use the built-in min function to ensure v does not exceed l.max. If v becomes greater than l.max due to floating-point precision errors, it will be capped at l.max.

Fixes #798.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Copy link
Member

@sbinet sbinet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.
I just have minor requests.

See below:

Reference: #799 (review)
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
sbinet
sbinet previously approved these changes Mar 22, 2025
Copy link
Member

@sbinet sbinet left a comment

Choose a reason for hiding this comment

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

LGTM

thanks

@sbinet sbinet requested a review from kortschak March 22, 2025 10:29
Reference: #799 (review)
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 23, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.26%. Comparing base (c96920b) to head (1434e66).
Report is 100 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #799      +/-   ##
==========================================
+ Coverage   70.78%   72.26%   +1.48%     
==========================================
  Files          60       59       -1     
  Lines        5291     7353    +2062     
==========================================
+ Hits         3745     5314    +1569     
- Misses       1350     1852     +502     
+ Partials      196      187       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@kortschak kortschak left a comment

Choose a reason for hiding this comment

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

Thanks.

Please send a change to gonum/gonum with the commit message "A+C: add Eng Zer Jun" (please correct this if I got your name wrong) adding yourself to AUTHORS and CONTRIBUTORS.

@Juneezee
Copy link
Contributor Author

Thanks.

Please send a change to gonum/gonum with the commit message "A+C: add Eng Zer Jun" (please correct this if I got your name wrong) adding yourself to AUTHORS and CONTRIBUTORS.

@kortschak My name is already in the AUTHORS and CONTRIBUTORS file 😊

@sbinet sbinet merged commit 82c684a into gonum:master Mar 23, 2025
6 checks passed
@kortschak
Copy link
Member

@Juneezee Ah, thanks. You show up as first time contributor here, but this is because the repo is different. Thanks for the change.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

palette/moreland: possible panic for Palette method

4 participants