diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 66b458897adb4..20e9b8dcffdd1 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -3069,13 +3069,13 @@ function mail(string $to, string $subject, string $message, array|string $additi function abs(int|float $num): int|float {} /** @compile-time-eval */ -function ceil(int|float $num): float {} +function ceil(int|float $num): int|float {} /** @compile-time-eval */ -function floor(int|float $num): float {} +function floor(int|float $num): int|float {} /** @compile-time-eval */ -function round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float {} +function round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): int|float {} /** @compile-time-eval */ function sin(float $num): float {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 39500b2b582dc..1df374d2fdbf9 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 487cee0751d47b18bf0a8fbdb050313783f1b369 */ + * Stub hash: 1e87218dea1bbe43e2bcab538844fb0afeb5531b */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -1604,13 +1604,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_abs, 0, 1, MAY_BE_LONG|MAY_BE_DO ZEND_ARG_TYPE_MASK(0, num, MAY_BE_LONG|MAY_BE_DOUBLE, NULL) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ceil, 0, 1, IS_DOUBLE, 0) - ZEND_ARG_TYPE_MASK(0, num, MAY_BE_LONG|MAY_BE_DOUBLE, NULL) -ZEND_END_ARG_INFO() +#define arginfo_ceil arginfo_abs -#define arginfo_floor arginfo_ceil +#define arginfo_floor arginfo_abs -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_round, 0, 1, IS_DOUBLE, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_round, 0, 1, MAY_BE_LONG|MAY_BE_DOUBLE) ZEND_ARG_TYPE_MASK(0, num, MAY_BE_LONG|MAY_BE_DOUBLE, NULL) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, precision, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mode, IS_LONG, 0, "PHP_ROUND_HALF_UP") diff --git a/ext/standard/math.c b/ext/standard/math.c index 734ebaba07564..2e479bae27b70 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -164,7 +164,7 @@ static inline double php_round_helper(double value, int mode) { /* {{{ _php_math_round */ /* - * Rounds a number to a certain number of decimal places in a certain rounding + * Rounds a floating point to a certain number of decimal places in a certain rounding * mode. For the specifics of the algorithm, see http://wiki.php.net/rfc/rounding */ PHPAPI double _php_math_round(double value, int places, int mode) { @@ -249,6 +249,108 @@ PHPAPI double _php_math_round(double value, int places, int mode) { } /* }}} */ +/* {{{ _php_math_round_long */ +/* + * Rounds a zend_long to a certain number of decimal places in a certain rounding + * mode. For the specifics of the algorithm, see https://wiki.php.net/rfc/integer-rounding + */ +PHPAPI zend_result _php_math_round_long(zend_long value, int places, int mode, zend_long *result) { + static const zend_long powers[] = { + 1, 10, 100, 1000, 10000, + 100000, 1000000, 10000000, 100000000, 1000000000, +#if SIZEOF_ZEND_LONG >= 8 + 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, + 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000 +#elif SIZEOF_ZEND_LONG > 8 +# error "Unknown SIZEOF_ZEND_LONG" +#endif + }; + + zend_long tmp_value = value; + zend_long power; + zend_long power_half; + zend_long rest; + + // Boolean if the number of places used matches the number of places possible + int max_places = 0; + + // Simple case - long that does not need to be rounded + if (places >= 0) { + *result = tmp_value; + return SUCCESS; + } + + if (UNEXPECTED(-places > sizeof(powers) / sizeof(powers[0]) - 1)) { + // Special case for rounding to the same number of places as max length possible + // as this would overflow the power of 10 + if (places == -MAX_LENGTH_OF_LONG + 1) { + max_places = 1; + rest = tmp_value; + tmp_value = 0; + power_half = powers[-places - 1] * 5; + } else { + // Rounding more places will always be zero + *result = 0; + return SUCCESS; + } + } else { + power = powers[-places]; + rest = tmp_value % power; + power_half = power / 2; + tmp_value = tmp_value / power; + } + + if (value >= 0) { + if ((mode == PHP_ROUND_HALF_UP && rest >= power_half) + || (mode == PHP_ROUND_HALF_DOWN && rest > power_half) + || (mode == PHP_ROUND_HALF_EVEN && (rest > power_half || (rest == power_half && tmp_value % 2 == 1))) + || (mode == PHP_ROUND_HALF_ODD && (rest > power_half || (rest == power_half && tmp_value % 2 == 0))) + ) { + if (UNEXPECTED(max_places)) { + return FAILURE; // would overflow + } + + tmp_value = tmp_value * power; + + if (UNEXPECTED(tmp_value > ZEND_LONG_MAX - power)) { + return FAILURE; // would overflow + } + tmp_value = tmp_value + power; + } else if (UNEXPECTED(max_places)) { + tmp_value = 0; + } else { + tmp_value = tmp_value * power; + } + } else { + if ((mode == PHP_ROUND_HALF_UP && rest <= -power_half) + || (mode == PHP_ROUND_HALF_DOWN && rest < -power_half) + || (mode == PHP_ROUND_HALF_EVEN && (rest < -power_half || (rest == -power_half && tmp_value % 2 == -1))) + || (mode == PHP_ROUND_HALF_ODD && (rest < -power_half || (rest == -power_half && tmp_value % 2 == 0))) + ) { + if (UNEXPECTED(max_places)) { + return FAILURE; // would underflow + } + + tmp_value = tmp_value * power; + + if (UNEXPECTED(tmp_value < ZEND_LONG_MIN + power)) { + return FAILURE; // would underflow + } + + tmp_value = tmp_value - power; + } else if (UNEXPECTED(max_places)) { + tmp_value = 0; + } else { + tmp_value = tmp_value * power; + } + } + + *result = tmp_value; + return SUCCESS; +} +/* }}} */ + + /* {{{ Return the absolute value of the number */ PHP_FUNCTION(abs) { @@ -283,7 +385,7 @@ PHP_FUNCTION(ceil) switch (Z_TYPE_P(value)) { case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); + RETURN_ZVAL(value, 1, 0); case IS_DOUBLE: RETURN_DOUBLE(ceil(Z_DVAL_P(value))); EMPTY_SWITCH_DEFAULT_CASE(); @@ -302,7 +404,7 @@ PHP_FUNCTION(floor) switch (Z_TYPE_P(value)) { case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); + RETURN_ZVAL(value, 1, 0); case IS_DOUBLE: RETURN_DOUBLE(floor(Z_DVAL_P(value))); EMPTY_SWITCH_DEFAULT_CASE(); @@ -317,6 +419,7 @@ PHP_FUNCTION(round) int places = 0; zend_long precision = 0; zend_long mode = PHP_ROUND_HALF_UP; + zend_long return_val_l; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_NUMBER(value) @@ -346,10 +449,10 @@ PHP_FUNCTION(round) switch (Z_TYPE_P(value)) { case IS_LONG: - /* Simple case - long that doesn't need to be rounded. */ - if (places >= 0) { - RETURN_DOUBLE(zval_get_double(value)); + if (_php_math_round_long(Z_LVAL_P(value), places, (int)mode, &return_val_l) == SUCCESS) { + RETURN_LONG(return_val_l); } + ZEND_FALLTHROUGH; case IS_DOUBLE: diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 1cdba0fe9d0a6..1f486818b05cb 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -19,6 +19,7 @@ #define PHP_MATH_H PHPAPI double _php_math_round(double value, int places, int mode); +PHPAPI zend_result _php_math_round_long(zend_long value, int places, int mode, zend_long *result); PHPAPI zend_string *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep); PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *dec_point, size_t dec_point_len, const char *thousand_sep, size_t thousand_sep_len); PHPAPI zend_string *_php_math_number_format_long(zend_long num, zend_long dec, const char *dec_point, size_t dec_point_len, const char *thousand_sep, size_t thousand_sep_len); diff --git a/ext/standard/tests/math/ceil_basic.phpt b/ext/standard/tests/math/ceil_basic.phpt index 7bdc87d16eaf5..d75523e1e3e91 100644 --- a/ext/standard/tests/math/ceil_basic.phpt +++ b/ext/standard/tests/math/ceil_basic.phpt @@ -7,10 +7,14 @@ precision=14 echo "*** Testing ceil() : basic functionality ***\n"; $values = array(0, -0, + 0.0, + -0.0, 0.5, -0.5, 1, -1, + 1.0, + -1.0, 1.5, -1.5, 2.6, @@ -35,25 +39,29 @@ for ($i = 0; $i < count($values); $i++) { ?> --EXPECTF-- *** Testing ceil() : basic functionality *** +int(0) +int(0) float(0) -float(0) +float(-0) float(1) float(-0) +int(1) +int(-1) float(1) float(-1) float(2) float(-1) float(3) float(-2) -float(31) -float(95) +int(31) +int(95) float(11) float(-10) float(3950) float(-3950) -float(39) -float(1) -float(0) +int(39) +int(1) +int(0) Deprecated: ceil(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) diff --git a/ext/standard/tests/math/ceil_basiclong_64bit.phpt b/ext/standard/tests/math/ceil_basiclong_64bit.phpt index 7cd9511b033a3..fa16096d4cc16 100644 --- a/ext/standard/tests/math/ceil_basiclong_64bit.phpt +++ b/ext/standard/tests/math/ceil_basiclong_64bit.phpt @@ -27,32 +27,32 @@ foreach ($longVals as $longVal) { ?> --EXPECT-- --- testing: 9223372036854775807 --- -float(9.223372036854776E+18) +int(9223372036854775807) --- testing: -9223372036854775808 --- -float(-9.223372036854776E+18) +int(-9223372036854775808) --- testing: 2147483647 --- -float(2147483647) +int(2147483647) --- testing: -2147483648 --- -float(-2147483648) +int(-2147483648) --- testing: 9223372034707292160 --- -float(9.223372034707292E+18) +int(9223372034707292160) --- testing: -9223372034707292160 --- -float(-9.223372034707292E+18) +int(-9223372034707292160) --- testing: 2147483648 --- -float(2147483648) +int(2147483648) --- testing: -2147483649 --- -float(-2147483649) +int(-2147483649) --- testing: 4294967294 --- -float(4294967294) +int(4294967294) --- testing: 4294967295 --- -float(4294967295) +int(4294967295) --- testing: 4294967293 --- -float(4294967293) +int(4294967293) --- testing: 9223372036854775806 --- -float(9.223372036854776E+18) +int(9223372036854775806) --- testing: 9.2233720368548E+18 --- float(9.223372036854776E+18) --- testing: -9223372036854775807 --- -float(-9.223372036854776E+18) +int(-9223372036854775807) --- testing: -9.2233720368548E+18 --- float(-9.223372036854776E+18) diff --git a/ext/standard/tests/math/ceil_variation1.phpt b/ext/standard/tests/math/ceil_variation1.phpt index 7751946ea9546..3286ebc4133e6 100644 --- a/ext/standard/tests/math/ceil_variation1.phpt +++ b/ext/standard/tests/math/ceil_variation1.phpt @@ -77,24 +77,24 @@ fclose($fp); -- Iteration 1 -- Deprecated: ceil(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 2 -- Deprecated: ceil(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 3 -- -float(1) +int(1) -- Iteration 4 -- -float(0) +int(0) -- Iteration 5 -- -float(1) +int(1) -- Iteration 6 -- -float(0) +int(0) -- Iteration 7 -- ceil(): Argument #1 ($num) must be of type int|float, string given @@ -120,12 +120,12 @@ ceil(): Argument #1 ($num) must be of type int|float, classA given -- Iteration 14 -- Deprecated: ceil(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 15 -- Deprecated: ceil(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 16 -- ceil(): Argument #1 ($num) must be of type int|float, resource given diff --git a/ext/standard/tests/math/floor_basic.phpt b/ext/standard/tests/math/floor_basic.phpt index 308a055f808a3..03f3b8ffd7201 100644 --- a/ext/standard/tests/math/floor_basic.phpt +++ b/ext/standard/tests/math/floor_basic.phpt @@ -7,10 +7,14 @@ precision=14 echo "*** Testing floor() : basic functionality ***\n"; $values = array(0, -0, + 0.0, + -0.0, 0.5, -0.5, 1, -1, + 1.0, + -1.0, 1.5, -1.5, 2.6, @@ -37,17 +41,29 @@ foreach($values as $value) { *** Testing floor() : basic functionality *** -- floor 0 -- -float(0) +int(0) + +-- floor 0 -- +int(0) -- floor 0 -- float(0) +-- floor -0 -- +float(-0) + -- floor 0.5 -- float(0) -- floor -0.5 -- float(-1) +-- floor 1 -- +int(1) + +-- floor -1 -- +int(-1) + -- floor 1 -- float(1) @@ -67,10 +83,10 @@ float(2) float(-3) -- floor 31 -- -float(31) +int(31) -- floor 95 -- -float(95) +int(95) -- floor 10.5 -- float(10) @@ -85,15 +101,15 @@ float(3950) float(-3950) -- floor 039 -- -float(39) +int(39) -- floor 1 -- -float(1) +int(1) -- floor -- -float(0) +int(0) -- floor -- Deprecated: floor(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) diff --git a/ext/standard/tests/math/floor_basiclong_64bit.phpt b/ext/standard/tests/math/floor_basiclong_64bit.phpt index 70c0e9210f7b7..1596c3849923e 100644 --- a/ext/standard/tests/math/floor_basiclong_64bit.phpt +++ b/ext/standard/tests/math/floor_basiclong_64bit.phpt @@ -27,32 +27,32 @@ foreach ($longVals as $longVal) { ?> --EXPECT-- --- testing: 9223372036854775807 --- -float(9.223372036854776E+18) +int(9223372036854775807) --- testing: -9223372036854775808 --- -float(-9.223372036854776E+18) +int(-9223372036854775808) --- testing: 2147483647 --- -float(2147483647) +int(2147483647) --- testing: -2147483648 --- -float(-2147483648) +int(-2147483648) --- testing: 9223372034707292160 --- -float(9.223372034707292E+18) +int(9223372034707292160) --- testing: -9223372034707292160 --- -float(-9.223372034707292E+18) +int(-9223372034707292160) --- testing: 2147483648 --- -float(2147483648) +int(2147483648) --- testing: -2147483649 --- -float(-2147483649) +int(-2147483649) --- testing: 4294967294 --- -float(4294967294) +int(4294967294) --- testing: 4294967295 --- -float(4294967295) +int(4294967295) --- testing: 4294967293 --- -float(4294967293) +int(4294967293) --- testing: 9223372036854775806 --- -float(9.223372036854776E+18) +int(9223372036854775806) --- testing: 9.2233720368548E+18 --- float(9.223372036854776E+18) --- testing: -9223372036854775807 --- -float(-9.223372036854776E+18) +int(-9223372036854775807) --- testing: -9.2233720368548E+18 --- float(-9.223372036854776E+18) diff --git a/ext/standard/tests/math/floor_variation1.phpt b/ext/standard/tests/math/floor_variation1.phpt index c9ec5e710c512..71bcef28165b6 100644 --- a/ext/standard/tests/math/floor_variation1.phpt +++ b/ext/standard/tests/math/floor_variation1.phpt @@ -77,24 +77,24 @@ fclose($fp); -- Iteration 1 -- Deprecated: floor(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 2 -- Deprecated: floor(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 3 -- -float(1) +int(1) -- Iteration 4 -- -float(0) +int(0) -- Iteration 5 -- -float(1) +int(1) -- Iteration 6 -- -float(0) +int(0) -- Iteration 7 -- floor(): Argument #1 ($num) must be of type int|float, string given @@ -120,12 +120,12 @@ floor(): Argument #1 ($num) must be of type int|float, classA given -- Iteration 14 -- Deprecated: floor(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 15 -- Deprecated: floor(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 16 -- floor(): Argument #1 ($num) must be of type int|float, resource given diff --git a/ext/standard/tests/math/floorceil.phpt b/ext/standard/tests/math/floorceil.phpt index f9594ce22c040..4d2bcb6dc5c78 100644 --- a/ext/standard/tests/math/floorceil.phpt +++ b/ext/standard/tests/math/floorceil.phpt @@ -19,26 +19,26 @@ Tests for floor en ceil var_dump ($a, $b, $c, $d, $e, $f); ?> --EXPECT-- -float(0) -float(-1) +int(0) +int(-1) float(-1) float(-1) float(-2) -float(0) -float(1) +int(0) float(1) +int(1) float(2) float(2) float(3) -float(0) -float(-1) +int(0) float(-1) +int(-1) float(-2) float(-2) float(-3) +int(0) float(0) -float(0) -float(1) +int(1) float(1) float(1) float(2) diff --git a/ext/standard/tests/math/round_basic.phpt b/ext/standard/tests/math/round_basic.phpt index f2a7447a37479..deae5c4d2e793 100644 --- a/ext/standard/tests/math/round_basic.phpt +++ b/ext/standard/tests/math/round_basic.phpt @@ -40,21 +40,21 @@ for ($i = 0; $i < count($values); $i++) { --EXPECTF-- *** Testing round() : basic functionality *** round: 123456789 -...with precision 2-> float(123456789) -...with precision 8-> float(123456789) -...with precision 3-> float(123456789) -...with precision 4-> float(123456789) +...with precision 2-> int(123456789) +...with precision 8-> int(123456789) +...with precision 3-> int(123456789) +...with precision 4-> int(123456789) Deprecated: Implicit conversion from float 3.6 to int loses precision in %s on line %d -...with precision 3.6-> float(123456789) -...with precision 2-> float(123456789) -...with precision 04-> float(123456789) +...with precision 3.6-> int(123456789) +...with precision 2-> int(123456789) +...with precision 04-> int(123456789) Deprecated: Implicit conversion from float-string "3.6" to int loses precision in %s on line %d -...with precision 3.6-> float(123456789) -...with precision 2.1e1-> float(123456789) -...with precision 1-> float(123456789) -...with precision -> float(123456789) +...with precision 3.6-> int(123456789) +...with precision 2.1e1-> int(123456789) +...with precision 1-> int(123456789) +...with precision -> int(123456789) round: 123.456789 ...with precision 2-> float(123.46) ...with precision 8-> float(123.456789) @@ -120,37 +120,37 @@ Deprecated: Implicit conversion from float-string "3.6" to int loses precision i ...with precision 1-> float(-4567) ...with precision -> float(-4567) round: 2311527 -...with precision 2-> float(2311527) -...with precision 8-> float(2311527) -...with precision 3-> float(2311527) -...with precision 4-> float(2311527) +...with precision 2-> int(2311527) +...with precision 8-> int(2311527) +...with precision 3-> int(2311527) +...with precision 4-> int(2311527) Deprecated: Implicit conversion from float 3.6 to int loses precision in %s on line %d -...with precision 3.6-> float(2311527) -...with precision 2-> float(2311527) -...with precision 04-> float(2311527) +...with precision 3.6-> int(2311527) +...with precision 2-> int(2311527) +...with precision 04-> int(2311527) Deprecated: Implicit conversion from float-string "3.6" to int loses precision in %s on line %d -...with precision 3.6-> float(2311527) -...with precision 2.1e1-> float(2311527) -...with precision 1-> float(2311527) -...with precision -> float(2311527) +...with precision 3.6-> int(2311527) +...with precision 2.1e1-> int(2311527) +...with precision 1-> int(2311527) +...with precision -> int(2311527) round: 14680063 -...with precision 2-> float(14680063) -...with precision 8-> float(14680063) -...with precision 3-> float(14680063) -...with precision 4-> float(14680063) +...with precision 2-> int(14680063) +...with precision 8-> int(14680063) +...with precision 3-> int(14680063) +...with precision 4-> int(14680063) Deprecated: Implicit conversion from float 3.6 to int loses precision in %s on line %d -...with precision 3.6-> float(14680063) -...with precision 2-> float(14680063) -...with precision 04-> float(14680063) +...with precision 3.6-> int(14680063) +...with precision 2-> int(14680063) +...with precision 04-> int(14680063) Deprecated: Implicit conversion from float-string "3.6" to int loses precision in %s on line %d -...with precision 3.6-> float(14680063) -...with precision 2.1e1-> float(14680063) -...with precision 1-> float(14680063) -...with precision -> float(14680063) +...with precision 3.6-> int(14680063) +...with precision 2.1e1-> int(14680063) +...with precision 1-> int(14680063) +...with precision -> int(14680063) round: 1.234567 ...with precision 2-> float(1.23) ...with precision 8-> float(1.234567) diff --git a/ext/standard/tests/math/round_basiclong_64bit.phpt b/ext/standard/tests/math/round_basiclong_64bit.phpt index c4ff1faf26261..c206cea099147 100644 --- a/ext/standard/tests/math/round_basiclong_64bit.phpt +++ b/ext/standard/tests/math/round_basiclong_64bit.phpt @@ -18,41 +18,160 @@ $longVals = array( MAX_64Bit -1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1 ); +$precisions = array( + 0, + -1, + -5, + -10, + -11, + -17, + -19, + -20, +); foreach ($longVals as $longVal) { - echo "--- testing: $longVal ---\n"; - var_dump(round($longVal)); + echo "--- testing: "; + var_dump($longVal); + foreach ($precisions as $precision) { + echo "... with precision " . $precision . ": "; + var_dump(round($longVal, $precision)); + } } ?> --EXPECT-- ---- testing: 9223372036854775807 --- -float(9.223372036854776E+18) ---- testing: -9223372036854775808 --- -float(-9.223372036854776E+18) ---- testing: 2147483647 --- -float(2147483647) ---- testing: -2147483648 --- -float(-2147483648) ---- testing: 9223372034707292160 --- -float(9.223372034707292E+18) ---- testing: -9223372034707292160 --- -float(-9.223372034707292E+18) ---- testing: 2147483648 --- -float(2147483648) ---- testing: -2147483649 --- -float(-2147483649) ---- testing: 4294967294 --- -float(4294967294) ---- testing: 4294967295 --- -float(4294967295) ---- testing: 4294967293 --- -float(4294967293) ---- testing: 9223372036854775806 --- -float(9.223372036854776E+18) ---- testing: 9.2233720368548E+18 --- -float(9.223372036854776E+18) ---- testing: -9223372036854775807 --- -float(-9.223372036854776E+18) ---- testing: -9.2233720368548E+18 --- -float(-9.223372036854776E+18) +--- testing: int(9223372036854775807) +... with precision 0: int(9223372036854775807) +... with precision -1: float(9.223372036854776E+18) +... with precision -5: float(9.2233720368548E+18) +... with precision -10: float(9.22337204E+18) +... with precision -11: int(9223372000000000000) +... with precision -17: int(9200000000000000000) +... with precision -19: float(1.0E+19) +... with precision -20: int(0) +--- testing: int(-9223372036854775808) +... with precision 0: int(-9223372036854775808) +... with precision -1: float(-9.223372036854776E+18) +... with precision -5: float(-9.2233720368548E+18) +... with precision -10: float(-9.22337204E+18) +... with precision -11: int(-9223372000000000000) +... with precision -17: int(-9200000000000000000) +... with precision -19: float(-1.0E+19) +... with precision -20: int(0) +--- testing: int(2147483647) +... with precision 0: int(2147483647) +... with precision -1: int(2147483650) +... with precision -5: int(2147500000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(-2147483648) +... with precision 0: int(-2147483648) +... with precision -1: int(-2147483650) +... with precision -5: int(-2147500000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(9223372034707292160) +... with precision 0: int(9223372034707292160) +... with precision -1: int(9223372034707292160) +... with precision -5: int(9223372034707300000) +... with precision -10: int(9223372030000000000) +... with precision -11: int(9223372000000000000) +... with precision -17: int(9200000000000000000) +... with precision -19: float(1.0E+19) +... with precision -20: int(0) +--- testing: int(-9223372034707292160) +... with precision 0: int(-9223372034707292160) +... with precision -1: int(-9223372034707292160) +... with precision -5: int(-9223372034707300000) +... with precision -10: int(-9223372030000000000) +... with precision -11: int(-9223372000000000000) +... with precision -17: int(-9200000000000000000) +... with precision -19: float(-1.0E+19) +... with precision -20: int(0) +--- testing: int(2147483648) +... with precision 0: int(2147483648) +... with precision -1: int(2147483650) +... with precision -5: int(2147500000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(-2147483649) +... with precision 0: int(-2147483649) +... with precision -1: int(-2147483650) +... with precision -5: int(-2147500000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(4294967294) +... with precision 0: int(4294967294) +... with precision -1: int(4294967290) +... with precision -5: int(4295000000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(4294967295) +... with precision 0: int(4294967295) +... with precision -1: int(4294967300) +... with precision -5: int(4295000000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(4294967293) +... with precision 0: int(4294967293) +... with precision -1: int(4294967290) +... with precision -5: int(4295000000) +... with precision -10: int(0) +... with precision -11: int(0) +... with precision -17: int(0) +... with precision -19: int(0) +... with precision -20: int(0) +--- testing: int(9223372036854775806) +... with precision 0: int(9223372036854775806) +... with precision -1: float(9.223372036854776E+18) +... with precision -5: float(9.2233720368548E+18) +... with precision -10: float(9.22337204E+18) +... with precision -11: int(9223372000000000000) +... with precision -17: int(9200000000000000000) +... with precision -19: float(1.0E+19) +... with precision -20: int(0) +--- testing: float(9.223372036854776E+18) +... with precision 0: float(9.223372036854776E+18) +... with precision -1: float(9.223372036854776E+18) +... with precision -5: float(9.2233720368548E+18) +... with precision -10: float(9.22337204E+18) +... with precision -11: float(9.223372E+18) +... with precision -17: float(9.2E+18) +... with precision -19: float(1.0E+19) +... with precision -20: float(0) +--- testing: int(-9223372036854775807) +... with precision 0: int(-9223372036854775807) +... with precision -1: float(-9.223372036854776E+18) +... with precision -5: float(-9.2233720368548E+18) +... with precision -10: float(-9.22337204E+18) +... with precision -11: int(-9223372000000000000) +... with precision -17: int(-9200000000000000000) +... with precision -19: float(-1.0E+19) +... with precision -20: int(0) +--- testing: float(-9.223372036854776E+18) +... with precision 0: float(-9.223372036854776E+18) +... with precision -1: float(-9.223372036854776E+18) +... with precision -5: float(-9.2233720368548E+18) +... with precision -10: float(-9.22337204E+18) +... with precision -11: float(-9.223372E+18) +... with precision -17: float(-9.2E+18) +... with precision -19: float(-1.0E+19) +... with precision -20: float(-0) diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index 61a697bc2d73b..677da55a4eda0 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -16,15 +16,22 @@ $numbers = [ -3.5, 7, -7, + 7.0, + -7.0, + 5555, + -5555, + 5555.5, + -5555.5, 0.61, 0.69, 0, + 0.0, 1.9999, -1.9999, 0.0001, -0.0001, ]; -$precisions = [0, 1, 2, 10]; +$precisions = [0, 1, 2, 10, -1, -10]; foreach ($modes as $modeKey => $mode) { echo "mode: $modeKey\n"; @@ -33,8 +40,8 @@ foreach ($modes as $modeKey => $mode) { foreach ($numbers as $number) { $result = round($number, $precision, $mode); echo "\t\t" . - str_pad($number, 7, " ", STR_PAD_LEFT) . - " => $result\n"; + str_pad(var_export($number, true), 7, " ", STR_PAD_LEFT) . + " => " . var_export($result, true) . "\n"; } echo "\n"; } @@ -44,19 +51,26 @@ foreach ($modes as $modeKey => $mode) { --EXPECT-- mode: PHP_ROUND_HALF_UP precision: 0 - 2.5 => 3 - -2.5 => -3 - 3.5 => 4 - -3.5 => -4 + 2.5 => 3.0 + -2.5 => -3.0 + 3.5 => 4.0 + -3.5 => -4.0 7 => 7 -7 => -7 - 0.61 => 1 - 0.69 => 1 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5556.0 + -5555.5 => -5556.0 + 0.61 => 1.0 + 0.69 => 1.0 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 1 2.5 => 2.5 @@ -65,13 +79,20 @@ mode: PHP_ROUND_HALF_UP -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.6 0.69 => 0.7 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 2 2.5 => 2.5 @@ -80,13 +101,20 @@ mode: PHP_ROUND_HALF_UP -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 10 2.5 => 2.5 @@ -95,29 +123,87 @@ mode: PHP_ROUND_HALF_UP -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 + 0.0 => 0.0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 -0.0001 => -0.0001 + precision: -1 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 10 + -7 => -10 + 7.0 => 10.0 + -7.0 => -10.0 + 5555 => 5560 + -5555 => -5560 + 5555.5 => 5560.0 + -5555.5 => -5560.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + + precision: -10 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 0 + -7 => 0 + 7.0 => 0.0 + -7.0 => -0.0 + 5555 => 0 + -5555 => 0 + 5555.5 => 0.0 + -5555.5 => -0.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + mode: PHP_ROUND_HALF_DOWN precision: 0 - 2.5 => 2 - -2.5 => -2 - 3.5 => 3 - -3.5 => -3 + 2.5 => 2.0 + -2.5 => -2.0 + 3.5 => 3.0 + -3.5 => -3.0 7 => 7 -7 => -7 - 0.61 => 1 - 0.69 => 1 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.0 + -5555.5 => -5555.0 + 0.61 => 1.0 + 0.69 => 1.0 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 1 2.5 => 2.5 @@ -126,13 +212,20 @@ mode: PHP_ROUND_HALF_DOWN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.6 0.69 => 0.7 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 2 2.5 => 2.5 @@ -141,13 +234,20 @@ mode: PHP_ROUND_HALF_DOWN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 10 2.5 => 2.5 @@ -156,29 +256,87 @@ mode: PHP_ROUND_HALF_DOWN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 + 0.0 => 0.0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 -0.0001 => -0.0001 + precision: -1 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 10 + -7 => -10 + 7.0 => 10.0 + -7.0 => -10.0 + 5555 => 5550 + -5555 => -5550 + 5555.5 => 5560.0 + -5555.5 => -5560.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + + precision: -10 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 0 + -7 => 0 + 7.0 => 0.0 + -7.0 => -0.0 + 5555 => 0 + -5555 => 0 + 5555.5 => 0.0 + -5555.5 => -0.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + mode: PHP_ROUND_HALF_EVEN precision: 0 - 2.5 => 2 - -2.5 => -2 - 3.5 => 4 - -3.5 => -4 + 2.5 => 2.0 + -2.5 => -2.0 + 3.5 => 4.0 + -3.5 => -4.0 7 => 7 -7 => -7 - 0.61 => 1 - 0.69 => 1 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5556.0 + -5555.5 => -5556.0 + 0.61 => 1.0 + 0.69 => 1.0 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 1 2.5 => 2.5 @@ -187,13 +345,20 @@ mode: PHP_ROUND_HALF_EVEN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.6 0.69 => 0.7 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 2 2.5 => 2.5 @@ -202,13 +367,20 @@ mode: PHP_ROUND_HALF_EVEN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 10 2.5 => 2.5 @@ -217,29 +389,87 @@ mode: PHP_ROUND_HALF_EVEN -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 + 0.0 => 0.0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 -0.0001 => -0.0001 + precision: -1 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 10 + -7 => -10 + 7.0 => 10.0 + -7.0 => -10.0 + 5555 => 5560 + -5555 => -5560 + 5555.5 => 5560.0 + -5555.5 => -5560.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + + precision: -10 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 0 + -7 => 0 + 7.0 => 0.0 + -7.0 => -0.0 + 5555 => 0 + -5555 => 0 + 5555.5 => 0.0 + -5555.5 => -0.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + mode: PHP_ROUND_HALF_ODD precision: 0 - 2.5 => 3 - -2.5 => -3 - 3.5 => 3 - -3.5 => -3 + 2.5 => 3.0 + -2.5 => -3.0 + 3.5 => 3.0 + -3.5 => -3.0 7 => 7 -7 => -7 - 0.61 => 1 - 0.69 => 1 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.0 + -5555.5 => -5555.0 + 0.61 => 1.0 + 0.69 => 1.0 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 1 2.5 => 2.5 @@ -248,13 +478,20 @@ mode: PHP_ROUND_HALF_ODD -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.6 0.69 => 0.7 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 2 2.5 => 2.5 @@ -263,13 +500,20 @@ mode: PHP_ROUND_HALF_ODD -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 - 1.9999 => 2 - -1.9999 => -2 - 0.0001 => 0 - -0.0001 => -0 + 0.0 => 0.0 + 1.9999 => 2.0 + -1.9999 => -2.0 + 0.0001 => 0.0 + -0.0001 => -0.0 precision: 10 2.5 => 2.5 @@ -278,10 +522,61 @@ mode: PHP_ROUND_HALF_ODD -3.5 => -3.5 7 => 7 -7 => -7 + 7.0 => 7.0 + -7.0 => -7.0 + 5555 => 5555 + -5555 => -5555 + 5555.5 => 5555.5 + -5555.5 => -5555.5 0.61 => 0.61 0.69 => 0.69 0 => 0 + 0.0 => 0.0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 -0.0001 => -0.0001 + + precision: -1 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 10 + -7 => -10 + 7.0 => 10.0 + -7.0 => -10.0 + 5555 => 5550 + -5555 => -5550 + 5555.5 => 5560.0 + -5555.5 => -5560.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 + + precision: -10 + 2.5 => 0.0 + -2.5 => -0.0 + 3.5 => 0.0 + -3.5 => -0.0 + 7 => 0 + -7 => 0 + 7.0 => 0.0 + -7.0 => -0.0 + 5555 => 0 + -5555 => 0 + 5555.5 => 0.0 + -5555.5 => -0.0 + 0.61 => 0.0 + 0.69 => 0.0 + 0 => 0 + 0.0 => 0.0 + 1.9999 => 0.0 + -1.9999 => -0.0 + 0.0001 => 0.0 + -0.0001 => -0.0 diff --git a/ext/standard/tests/math/round_variation1.phpt b/ext/standard/tests/math/round_variation1.phpt index 2a2adf27cae09..31082b3808b39 100644 --- a/ext/standard/tests/math/round_variation1.phpt +++ b/ext/standard/tests/math/round_variation1.phpt @@ -89,19 +89,19 @@ fclose($fp); *** Testing round() : usage variations *** -- Iteration 1 -- -float(0) +int(0) -- Iteration 2 -- -float(1) +int(1) -- Iteration 3 -- -float(12345) +int(12345) -- Iteration 4 -- -float(-2345) +int(-2345) -- Iteration 5 -- -float(2147483647) +int(2147483647) -- Iteration 6 -- float(10.5) @@ -121,24 +121,24 @@ float(0.5) -- Iteration 11 -- Deprecated: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 12 -- Deprecated: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 13 -- -float(1) +int(1) -- Iteration 14 -- -float(0) +int(0) -- Iteration 15 -- -float(1) +int(1) -- Iteration 16 -- -float(0) +int(0) -- Iteration 17 -- round(): Argument #1 ($num) must be of type int|float, string given @@ -164,12 +164,12 @@ round(): Argument #1 ($num) must be of type int|float, classA given -- Iteration 24 -- Deprecated: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 25 -- Deprecated: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in %s on line %d -float(0) +int(0) -- Iteration 26 -- round(): Argument #1 ($num) must be of type int|float, resource given