Skip to content

Commit

Permalink
number_format: cast large floats within range of int to int
Browse files Browse the repository at this point in the history
This prevents loosing precision for numbers above 2^52.

Closes phpGH-12333
  • Loading branch information
marc-mabe authored and bukka committed Dec 9, 2023
1 parent 19eb727 commit b3f259d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 31 additions & 7 deletions ext/standard/tests/math/number_format_basiclong_64bit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b3f259d

Please sign in to comment.