number_format: cast large floats within range of int to int

This prevents loosing precision for numbers above 2^52.

Closes GH-12333
This commit is contained in:
Marc Bennewitz 2023-09-30 16:28:02 +02:00 committed by Jakub Zelenka
parent 19eb727b6a
commit b3f259d623
No known key found for this signature in database
GPG Key ID: 1C0779DC5C0A9DE4
3 changed files with 43 additions and 7 deletions

2
NEWS
View File

@ -104,6 +104,8 @@ Standard:
. Fixed bug GH-12592 (strcspn() odd behaviour with NUL bytes and empty mask).
(nielsdos)
. Removed the deprecated inet_ntoa call support. (David Carlier)
. Cast large floats that are within int range to int in number_format so
the precision is not lost. (Marc Bennewitz)
XML:
. Added XML_OPTION_PARSE_HUGE parser option. (nielsdos)

View File

@ -1330,6 +1330,16 @@ PHP_FUNCTION(number_format)
break;
case IS_DOUBLE:
// double values of >= 2^52 can not have fractional digits anymore
// Casting to long on 64bit will not loose precision on rounding
if (UNEXPECTED(
(Z_DVAL_P(num) >= 4503599627370496.0 || Z_DVAL_P(num) <= -4503599627370496.0)
&& ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(num))
)) {
RETURN_STR(_php_math_number_format_long((zend_long)Z_DVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len));
break;
}
if (dec >= 0) {
dec_int = ZEND_LONG_INT_OVFL(dec) ? INT_MAX : (int)dec;
} else {

View File

@ -12,10 +12,12 @@ define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);
$longVals = array(
$numbers = array(
MAX_64Bit, MIN_64Bit, MAX_32Bit, MIN_32Bit, MAX_64Bit - MAX_32Bit, MIN_64Bit - MIN_32Bit,
MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2, (MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1,
MAX_64Bit -1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1
MAX_64Bit - 1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1,
// floats rounded as int
MAX_64Bit - 1024.0, MIN_64Bit + 1024.0
);
$precisions = array(
@ -31,12 +33,12 @@ $precisions = array(
PHP_INT_MIN,
);
foreach ($longVals as $longVal) {
foreach ($numbers as $number) {
echo "--- testing: ";
var_dump($longVal);
var_dump($number);
foreach ($precisions as $precision) {
echo "... with precision " . $precision . ": ";
var_dump(number_format($longVal, $precision));
var_dump(number_format($number, $precision));
}
}
@ -199,8 +201,30 @@ foreach ($longVals as $longVal) {
--- testing: float(-9.223372036854776E+18)
... with precision 5: string(32) "-9,223,372,036,854,775,808.00000"
... with precision 0: string(26) "-9,223,372,036,854,775,808"
... with precision -1: string(26) "-9,223,372,036,854,775,808"
... with precision -5: string(26) "-9,223,372,036,854,800,384"
... with precision -1: string(26) "-9,223,372,036,854,775,810"
... with precision -5: string(26) "-9,223,372,036,854,800,000"
... with precision -10: string(26) "-9,223,372,040,000,000,000"
... with precision -11: string(26) "-9,223,372,000,000,000,000"
... with precision -17: string(26) "-9,200,000,000,000,000,000"
... with precision -19: string(27) "-10,000,000,000,000,000,000"
... with precision -20: string(1) "0"
... with precision -9223372036854775808: string(1) "0"
--- testing: float(9.223372036854775E+18)
... with precision 5: string(31) "9,223,372,036,854,774,784.00000"
... with precision 0: string(25) "9,223,372,036,854,774,784"
... with precision -1: string(25) "9,223,372,036,854,774,780"
... with precision -5: string(25) "9,223,372,036,854,800,000"
... with precision -10: string(25) "9,223,372,040,000,000,000"
... with precision -11: string(25) "9,223,372,000,000,000,000"
... with precision -17: string(25) "9,200,000,000,000,000,000"
... with precision -19: string(26) "10,000,000,000,000,000,000"
... with precision -20: string(1) "0"
... with precision -9223372036854775808: string(1) "0"
--- testing: float(-9.223372036854775E+18)
... with precision 5: string(32) "-9,223,372,036,854,774,784.00000"
... with precision 0: string(26) "-9,223,372,036,854,774,784"
... with precision -1: string(26) "-9,223,372,036,854,774,780"
... with precision -5: string(26) "-9,223,372,036,854,800,000"
... with precision -10: string(26) "-9,223,372,040,000,000,000"
... with precision -11: string(26) "-9,223,372,000,000,000,000"
... with precision -17: string(26) "-9,200,000,000,000,000,000"