Skip to content

random: Randomizer::getFloat(): Fix check for empty open intervals #10185

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

Merged
merged 3 commits into from
Jan 10, 2023

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Dec 29, 2022

The check for invalid parameters for the IntervalBoundary::OpenOpen variant was not correct: If two consecutive doubles are passed as parameters, the resulting interval is empty, resulting in an uint64 underflow in the γ-section implementation.

Instead of checking whether $min < $max, we must check that there is at least one more double between $min and $max, i.e. it must hold that:

nextafter($min, $max) != $max

Instead of duplicating the comparatively complicated and expensive nextafter logic for a rare error case we instead return NAN from the γ-section implementation when the parameters result in an empty interval and thus underflow.

This allows us to reliably detect this specific error case after the fact, but without modifying the engine state. It also provides reliable error reporting for other internal functions that might use the γ-section implementation.

The check for invalid parameters for the IntervalBoundary::OpenOpen variant was
not correct: If two consecutive doubles are passed as parameters, the resulting
interval is empty, resulting in an uint64 underflow in the γ-section
implementation.

Instead of checking whether `$min < $max`, we must check that there is at least
one more double between `$min` and `$max`, i.e. it must hold that:

	nextafter($min, $max) != $max

Instead of duplicating the comparatively complicated and expensive `nextafter`
logic for a rare error case we instead return `NAN` from the γ-section
implementation when the parameters result in an empty interval and thus underflow.

This allows us to reliably detect this specific error case *after* the fact,
but without modifying the engine state. It also provides reliable error
reporting for other internal functions that might use the γ-section
implementation.
This extends the empty-interval check in the γ-section implementation with a
check that min is actually the smaller of the two parameters.
Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Thank you! This looks good to me, but please consider my comment below.

Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
Copy link
Contributor

@zeriyoshi zeriyoshi left a comment

Choose a reason for hiding this comment

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

These changes look good. However, please be aware that I lack knowledge of the floating point RNG, so my review will certainly be less accurate.

@TimWolla TimWolla merged commit 13b82ee into php:master Jan 10, 2023
@TimWolla TimWolla deleted the getfloat-open-open branch January 10, 2023 09:16
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.

4 participants