Rounding modes use some rule to determine in which direction a supplied value should be rounded. While all of the rounding modes available in PHP right now are deterministic, there are other ways that rounding can occur.
This RFC proposes adding a stochastic rounding mode that uses a weighted random process to determine the rounding direction.
A stochastic rounding mode rounds with a probability that corresponds to the value being rounded. For instance:
$val = round(1.8, 0 \RoundingMode::Stochastic); echo $val; // 20% of the time, this will echo "1" // 80% of the time, this will echo "2"
This mode ensures that if the same value is rounded many times, the average value of the rounded numbers will approach the original number. This is mainly used in linear algebra, particularly machine learning applications.
The new rounding mode will only be available on the \RoundingMode
enum.
enum RoundingMode { // Existing Modes case HalfAwayFromZero; case HalfTowardsZero; case HalfEven; case HalfOdd; case TowardsZero; case AwayFromZero; case NegativeInfinity; case PositiveInfinity; // New Rounding Mode case Stochastic; }
None
Next PHP 8.x
None
None
None
Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.
The Intl
round function will be unaffected, as it does not accept the \RoundingMode
enum.
None
Add new stochastic rounding mode as described: yes/no. A 2/3 vote is required to pass.
Vote will start 2024-XX-XX and will end 2024-XX-XX.
The patch will be completed by the RFC author after acceptance.
None