ext/standard: Add more tests to round() (#13399)

This commit is contained in:
Saki Takamachi 2024-02-17 22:44:55 +09:00 committed by GitHub
parent ae5beff61b
commit ed1c9d8954
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,146 @@
--TEST--
Feature GH-12143: Extend the maximum precision round can handle by one digit
--INI--
serialize_precisiion=17
--FILE--
<?php
$testCases = [
'PHP_ROUND_HALF_UP' => [
[0.12345678901234565, 16],
[-0.12345678901234565, 16],
[12345678901234565, -1],
[-12345678901234565, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_HALF_DOWN' => [
[0.12345678901234565, 16],
[-0.12345678901234565, 16],
[12345678901234565, -1],
[-12345678901234565, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_HALF_EVEN' => [
[0.12345678901234565, 16],
[-0.12345678901234565, 16],
[12345678901234565, -1],
[-12345678901234565, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_HALF_ODD' => [
[0.12345678901234565, 16],
[-0.12345678901234565, 16],
[12345678901234565, -1],
[-12345678901234565, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_AWAY_FROM_ZERO' => [
[0.12345678901234560, 16],
[-0.12345678901234560, 16],
[12345678901234567, -1],
[-12345678901234567, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_TOWARD_ZERO' => [
[0.12345678901234566, 16],
[-0.12345678901234566, 16],
[12345678901234565, -1],
[-12345678901234565, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_CEILING' => [
[0.12345678901234560, 16],
[-0.12345678901234560, 16],
[12345678901234564, -1],
[-12345678901234564, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
'PHP_ROUND_FLOOR' => [
[0.12345678901234560, 16],
[-0.12345678901234560, 16],
[12345678901234564, -1],
[-12345678901234564, -1],
[4503599627370495.5, 0],
[-4503599627370495.5, 0],
],
];
foreach ($testCases as $mode => $cases) {
echo "========== {$mode} ==========\n";
foreach ($cases as [$val, $precision]) {
var_dump(round($val, $precision, constant($mode)));
}
echo "\n";
}
?>
--EXPECT--
========== PHP_ROUND_HALF_UP ==========
float(0.12345678901234565)
float(-0.12345678901234565)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_HALF_DOWN ==========
float(0.12345678901234565)
float(-0.12345678901234565)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_HALF_EVEN ==========
float(0.12345678901234565)
float(-0.12345678901234565)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_HALF_ODD ==========
float(0.12345678901234565)
float(-0.12345678901234565)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_AWAY_FROM_ZERO ==========
float(0.1234567890123456)
float(-0.1234567890123456)
float(12345678901234568)
float(-12345678901234568)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_TOWARD_ZERO ==========
float(0.12345678901234566)
float(-0.12345678901234566)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_CEILING ==========
float(0.1234567890123456)
float(-0.1234567890123456)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)
========== PHP_ROUND_FLOOR ==========
float(0.1234567890123456)
float(-0.1234567890123456)
float(12345678901234564)
float(-12345678901234564)
float(4503599627370495.5)
float(-4503599627370495.5)