Closed
Description
Updated description:
<?php
$number = 0.49999999999999994;
var_dump($number, round($number, 0, PHP_ROUND_HALF_UP));
$number = 1.4999999999999998;
var_dump($number, round($number, 0, PHP_ROUND_HALF_UP));
$number = 4503599627370495.5;
var_dump($number, round($number, 0, PHP_ROUND_HALF_UP));
Resulted in this output:
float(0.49999999999999994)
float(1)
float(1.4999999999999998)
float(2)
float(4503599627370495.5)
float(4503599627370495.5)
But I expected this output instead:
float(0.49999999999999994)
float(0)
float(1.4999999999999998)
float(1)
float(4503599627370495.5)
float(4503599627370496)
Description
Old Description
The following code:
<?php
var_dump(round(0.49999999999999994, 0, PHP_ROUND_HALF_UP), 0.49999999999999994);
Resulted in this output:
float(1)
float(0.49999999999999994)
But I expected this output instead:
float(0)
float(0.49999999999999994)
Extracted from #12056 (comment) into a dedicated issue. The solution consists of using modf()
and then explicitly checking against 0.5
, instead of adding or subtracting 0.5
, since the result of the addition / subtraction might not be exactly representable.
/cc @jorgsowa Are you interested in developing the fix?
PHP Version
8.2.10
Operating System
No response