-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #75310 - Use DBL_EPSILON to avoid rounding errors in the range function #2804
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
Conversation
If at all, shouldn't we use |
@cmb69 thanks for the feedback. That makes it better indeed. |
ext/standard/array.c
Outdated
@@ -2160,7 +2161,7 @@ PHP_FUNCTION(range) | |||
RANGE_CHECK_DOUBLE_INIT_ARRAY(low, high); | |||
|
|||
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) { | |||
for (i = 0, element = low; i < size && element >= high; ++i, element = low - (i * step)) { | |||
for (i = 0, element = low; i < size && element >= (high - DBL_EPSILON); ++i, element = low - (i * step)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more correct approach of this check would look in pseudo code similar to abs(a - b) < threshold
.
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weltling, thanks for the feedback.
I've moved the constant to the LHS as it makes it more readable. Let me know if you prefer it differently.
@pmmaga thanks for checking. Nope, i literally meant to use abs() and the threshold is meant to be I've just tested more, your patch indeed improves the situation with cases as in the tests pushed, but perhaps you could also check cases like Thanks. |
Given that the change would not provide a consistent fix for all cases, it's better to leave the choice to users instead of trying (and sometimes failing) to help. |
Link for bugsnet: https://bugs.php.net/bug.php?id=75310
In #1695, this constant was removed. However, in cases like the one described in the bug report, this hack still makes a difference.