From 5b89dd2f3e35e740d49ac0ffafe59bc8a6925926 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Fri, 25 Aug 2023 23:15:10 +0200 Subject: [PATCH 01/25] Add 4 new rounding modes to round() function --- NEWS | 4 +- ext/standard/basic_functions.stub.php | 20 ++ ext/standard/basic_functions_arginfo.h | 18 +- ext/standard/math.c | 92 ++++--- ext/standard/php_math.h | 18 +- ext/standard/tests/math/round_modes.phpt | 314 +++++++++++++++++++++++ 6 files changed, 422 insertions(+), 44 deletions(-) diff --git a/NEWS b/NEWS index b815dd441ea2c..b7afa5de16afa 100644 --- a/NEWS +++ b/NEWS @@ -106,7 +106,9 @@ Standard: (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) + the precision is not lost. (Marc Bennewitz) + . Added 4 new rounding modes to the round(). (Jorg Sowa) + XML: . Added XML_OPTION_PARSE_HUGE parser option. (nielsdos) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 8592d7c0b8810..851fc0ade4191 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -375,6 +375,26 @@ * @cvalue PHP_ROUND_HALF_ODD */ const PHP_ROUND_HALF_ODD = UNKNOWN; +/** + * @var int + * @cvalue PHP_ROUND_CEILING + */ +const PHP_ROUND_CEILING = UNKNOWN; +/** + * @var int + * @cvalue PHP_ROUND_FLOOR + */ +const PHP_ROUND_FLOOR = UNKNOWN; +/** + * @var int + * @cvalue PHP_ROUND_TOWARD_ZERO + */ +const PHP_ROUND_TOWARD_ZERO = UNKNOWN; +/** + * @var int + * @cvalue PHP_ROUND_AWAY_FROM_ZERO + */ +const PHP_ROUND_AWAY_FROM_ZERO = UNKNOWN; /* crypt.c */ diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 48202e78079ac..39500b2b582dc 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: c9edbe45bb7a2b00b413fb3c56683bb8377a725f */ + * Stub hash: 487cee0751d47b18bf0a8fbdb050313783f1b369 */ 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) @@ -720,7 +720,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_syslog, 0, 2, IS_TRUE, 0) ZEND_END_ARG_INFO() #endif -#define arginfo_inet_ntop arginfo_gethostbyaddr +#if defined(HAVE_INET_NTOP) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_inet_ntop, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) + ZEND_ARG_TYPE_INFO(0, ip, IS_STRING, 0) +ZEND_END_ARG_INFO() +#endif #if defined(HAVE_INET_PTON) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_inet_pton, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) @@ -1826,7 +1830,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_context_create, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, params, IS_ARRAY, 1, "null") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_params, 0, 2, IS_TRUE, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_params, 0, 2, _IS_BOOL, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_INFO(0, params, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -1835,14 +1839,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_get_params, 0, 1, ZEND_ARG_INFO(0, context) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_option, 0, 2, IS_TRUE, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_option, 0, 2, _IS_BOOL, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_MASK(0, wrapper_or_options, MAY_BE_ARRAY|MAY_BE_STRING, NULL) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, option_name, IS_STRING, 1, "null") ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_options, 0, 2, IS_TRUE, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_options, 0, 2, _IS_BOOL, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_INFO(0, options, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -2411,7 +2415,9 @@ ZEND_FUNCTION(closelog); #if defined(HAVE_SYSLOG_H) ZEND_FUNCTION(syslog); #endif +#if defined(HAVE_INET_NTOP) ZEND_FUNCTION(inet_ntop); +#endif #if defined(HAVE_INET_PTON) ZEND_FUNCTION(inet_pton); #endif @@ -3044,7 +3050,9 @@ static const zend_function_entry ext_functions[] = { #if defined(HAVE_SYSLOG_H) ZEND_FE(syslog, arginfo_syslog) #endif +#if defined(HAVE_INET_NTOP) ZEND_FE(inet_ntop, arginfo_inet_ntop) +#endif #if defined(HAVE_INET_PTON) ZEND_FE(inet_pton, arginfo_inet_pton) #endif diff --git a/ext/standard/math.c b/ext/standard/math.c index 377c573033f69..82f91c856a1ce 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -101,10 +101,10 @@ static inline double php_round_helper(double value, int mode) { * the absolute value of the fractional part (which will not result * in branches in the assembly) to make the following cases simpler. */ - fractional = fabs(modf(value, &integral)); switch (mode) { case PHP_ROUND_HALF_UP: + fractional = fabs(modf(value, &integral)); if (fractional >= 0.5) { /* We must increase the magnitude of the integral part * (rounding up / towards infinity). copysign(1.0, integral) @@ -120,13 +120,35 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_DOWN: + fractional = fabs(modf(value, &integral)); if (fractional > 0.5) { return integral + copysign(1.0, integral); } return integral; + case PHP_ROUND_CEILING: + return ceil(value); + + case PHP_ROUND_FLOOR: + return floor(value); + + case PHP_ROUND_TOWARD_ZERO: + if (value >= 0.0) { + return floor(value); + } else { + return = ceil(value); + } + + case PHP_ROUND_AWAY_FROM_ZERO: + if (value >= 0.0) { + return = ceil(value); + } else { + return = floor(value); + } + case PHP_ROUND_HALF_EVEN: + fractional = fabs(modf(value, &integral)); if (fractional > 0.5) { return integral + copysign(1.0, integral); } @@ -143,7 +165,9 @@ static inline double php_round_helper(double value, int mode) { } return integral; + case PHP_ROUND_HALF_ODD: + fractional = fabs(modf(value, &integral)); if (fractional > 0.5) { return integral + copysign(1.0, integral); } @@ -157,6 +181,7 @@ static inline double php_round_helper(double value, int mode) { } return integral; + EMPTY_SWITCH_DEFAULT_CASE(); } // FIXME: GCC bug, branch is considered reachable. @@ -260,16 +285,16 @@ PHP_FUNCTION(abs) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { - RETURN_DOUBLE(-(double)ZEND_LONG_MIN); - } else { - RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); - } - case IS_DOUBLE: - RETURN_DOUBLE(fabs(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); + if (Z_TYPE_P(value) == IS_DOUBLE) { + RETURN_DOUBLE(fabs(Z_DVAL_P(value))); + } else if (Z_TYPE_P(value) == IS_LONG) { + if (Z_LVAL_P(value) == ZEND_LONG_MIN) { + RETURN_DOUBLE(-(double)ZEND_LONG_MIN); + } else { + RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); + } + } else { + ZEND_ASSERT(0 && "Unexpected type"); } } /* }}} */ @@ -283,12 +308,12 @@ PHP_FUNCTION(ceil) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(ceil(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); + if (Z_TYPE_P(value) == IS_DOUBLE) { + RETURN_DOUBLE(ceil(Z_DVAL_P(value))); + } else if (Z_TYPE_P(value) == IS_LONG) { + RETURN_DOUBLE(zval_get_double(value)); + } else { + ZEND_ASSERT(0 && "Unexpected type"); } } /* }}} */ @@ -302,12 +327,12 @@ PHP_FUNCTION(floor) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(floor(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); + if (Z_TYPE_P(value) == IS_DOUBLE) { + RETURN_DOUBLE(floor(Z_DVAL_P(value))); + } else if (Z_TYPE_P(value) == IS_LONG) { + RETURN_DOUBLE(zval_get_double(value)); + } else { + ZEND_ASSERT(0 && "Unexpected type"); } } /* }}} */ @@ -319,6 +344,7 @@ PHP_FUNCTION(round) int places = 0; zend_long precision = 0; zend_long mode = PHP_ROUND_HALF_UP; + double return_val; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_NUMBER(value) @@ -335,29 +361,21 @@ PHP_FUNCTION(round) } } - switch (mode) { - case PHP_ROUND_HALF_UP: - case PHP_ROUND_HALF_DOWN: - case PHP_ROUND_HALF_EVEN: - case PHP_ROUND_HALF_ODD: - break; - default: - zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); - RETURN_THROWS(); - } - 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)); + RETURN_DOUBLE((double) Z_LVAL_P(value)); } ZEND_FALLTHROUGH; case IS_DOUBLE: - RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int)places, (int)mode)); + return_val = (Z_TYPE_P(value) == IS_LONG) ? (double)Z_LVAL_P(value) : Z_DVAL_P(value); + return_val = _php_math_round(return_val, (int)places, (int)mode); + RETURN_DOUBLE(return_val); + break; - EMPTY_SWITCH_DEFAULT_CASE(); + EMPTY_SWITCH_DEFAULT_CASE() } } /* }}} */ diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 1cdba0fe9d0a6..6ff895cce3c8d 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -103,7 +103,7 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base); #endif #ifndef PHP_ROUND_HALF_DOWN -#define PHP_ROUND_HALF_DOWN 0x02 /* Down == towards zero */ +#define PHP_ROUND_HALF_DOWN 0x02 /* Arithmetic rounding, down == towards zero */ #endif #ifndef PHP_ROUND_HALF_EVEN @@ -114,4 +114,20 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base); #define PHP_ROUND_HALF_ODD 0x04 #endif +#ifndef PHP_ROUND_CEILING +#define PHP_ROUND_CEILING 0x05 +#endif + +#ifndef PHP_ROUND_FLOOR +#define PHP_ROUND_FLOOR 0x06 +#endif + +#ifndef PHP_ROUND_TOWARD_ZERO +#define PHP_ROUND_TOWARD_ZERO 0x07 +#endif + +#ifndef PHP_ROUND_AWAY_FROM_ZERO +#define PHP_ROUND_AWAY_FROM_ZERO 0x08 +#endif + #endif /* PHP_MATH_H */ diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index 61a697bc2d73b..139b668222be9 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -7,6 +7,10 @@ $modes = [ "PHP_ROUND_HALF_DOWN" => PHP_ROUND_HALF_DOWN, "PHP_ROUND_HALF_EVEN" => PHP_ROUND_HALF_EVEN, "PHP_ROUND_HALF_ODD" => PHP_ROUND_HALF_ODD, + "PHP_ROUND_CEILING" => PHP_ROUND_CEILING, + "PHP_ROUND_FLOOR" => PHP_ROUND_FLOOR, + "PHP_ROUND_TOWARD_ZERO" => PHP_ROUND_TOWARD_ZERO, + "PHP_ROUND_AWAY_FROM_ZERO" => PHP_ROUND_AWAY_FROM_ZERO, ]; $numbers = [ @@ -18,6 +22,8 @@ $numbers = [ -7, 0.61, 0.69, + 0.65, + -0.65, 0, 1.9999, -1.9999, @@ -52,6 +58,8 @@ mode: PHP_ROUND_HALF_UP -7 => -7 0.61 => 1 0.69 => 1 + 0.65 => 1 + -0.65 => -1 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -67,6 +75,8 @@ mode: PHP_ROUND_HALF_UP -7 => -7 0.61 => 0.6 0.69 => 0.7 + 0.65 => 0.7 + -0.65 => -0.7 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -82,6 +92,8 @@ mode: PHP_ROUND_HALF_UP -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -97,6 +109,8 @@ mode: PHP_ROUND_HALF_UP -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 @@ -113,6 +127,8 @@ mode: PHP_ROUND_HALF_DOWN -7 => -7 0.61 => 1 0.69 => 1 + 0.65 => 1 + -0.65 => -1 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -128,6 +144,8 @@ mode: PHP_ROUND_HALF_DOWN -7 => -7 0.61 => 0.6 0.69 => 0.7 + 0.65 => 0.6 + -0.65 => -0.6 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -143,6 +161,8 @@ mode: PHP_ROUND_HALF_DOWN -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -158,6 +178,8 @@ mode: PHP_ROUND_HALF_DOWN -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 @@ -174,6 +196,8 @@ mode: PHP_ROUND_HALF_EVEN -7 => -7 0.61 => 1 0.69 => 1 + 0.65 => 1 + -0.65 => -1 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -189,6 +213,8 @@ mode: PHP_ROUND_HALF_EVEN -7 => -7 0.61 => 0.6 0.69 => 0.7 + 0.65 => 0.6 + -0.65 => -0.6 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -204,6 +230,8 @@ mode: PHP_ROUND_HALF_EVEN -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -219,6 +247,8 @@ mode: PHP_ROUND_HALF_EVEN -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 @@ -235,6 +265,8 @@ mode: PHP_ROUND_HALF_ODD -7 => -7 0.61 => 1 0.69 => 1 + 0.65 => 1 + -0.65 => -1 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -250,6 +282,8 @@ mode: PHP_ROUND_HALF_ODD -7 => -7 0.61 => 0.6 0.69 => 0.7 + 0.65 => 0.7 + -0.65 => -0.7 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -265,6 +299,8 @@ mode: PHP_ROUND_HALF_ODD -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 2 -1.9999 => -2 @@ -280,6 +316,284 @@ mode: PHP_ROUND_HALF_ODD -7 => -7 0.61 => 0.61 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.9999 + -1.9999 => -1.9999 + 0.0001 => 0.0001 + -0.0001 => -0.0001 + +mode: PHP_ROUND_CEILING + precision: 0 + 2.5 => 3 + -2.5 => -2 + 3.5 => 4 + -3.5 => -3 + 7 => 7 + -7 => -7 + 0.61 => 1 + 0.69 => 1 + 0.65 => 1 + -0.65 => -0 + 0 => 0 + 1.9999 => 2 + -1.9999 => -1 + 0.0001 => 1 + -0.0001 => -0 + + precision: 1 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.7 + 0.69 => 0.7 + 0.65 => 0.7 + -0.65 => -0.6 + 0 => 0 + 1.9999 => 2 + -1.9999 => -1.9 + 0.0001 => 0.1 + -0.0001 => -0 + + precision: 2 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 2 + -1.9999 => -1.99 + 0.0001 => 0.01 + -0.0001 => -0 + + precision: 10 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.9999 + -1.9999 => -1.9999 + 0.0001 => 0.0001 + -0.0001 => -0.0001 + +mode: PHP_ROUND_FLOOR + precision: 0 + 2.5 => 2 + -2.5 => -3 + 3.5 => 3 + -3.5 => -4 + 7 => 7 + -7 => -7 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -1 + 0 => 0 + 1.9999 => 1 + -1.9999 => -2 + 0.0001 => 0 + -0.0001 => -1 + + precision: 1 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.6 + 0.69 => 0.6 + 0.65 => 0.6 + -0.65 => -0.7 + 0 => 0 + 1.9999 => 1.9 + -1.9999 => -2 + 0.0001 => 0 + -0.0001 => -0.1 + + precision: 2 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.99 + -1.9999 => -2 + 0.0001 => 0 + -0.0001 => -0.01 + + precision: 10 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.9999 + -1.9999 => -1.9999 + 0.0001 => 0.0001 + -0.0001 => -0.0001 + +mode: PHP_ROUND_TOWARD_ZERO + precision: 0 + 2.5 => 2 + -2.5 => -2 + 3.5 => 3 + -3.5 => -3 + 7 => 7 + -7 => -7 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 1 + -1.9999 => -1 + 0.0001 => 0 + -0.0001 => -0 + + precision: 1 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.6 + 0.69 => 0.6 + 0.65 => 0.6 + -0.65 => -0.6 + 0 => 0 + 1.9999 => 1.9 + -1.9999 => -1.9 + 0.0001 => 0 + -0.0001 => -0 + + precision: 2 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.99 + -1.9999 => -1.99 + 0.0001 => 0 + -0.0001 => -0 + + precision: 10 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 1.9999 + -1.9999 => -1.9999 + 0.0001 => 0.0001 + -0.0001 => -0.0001 + +mode: PHP_ROUND_AWAY_FROM_ZERO + precision: 0 + 2.5 => 3 + -2.5 => -3 + 3.5 => 4 + -3.5 => -4 + 7 => 7 + -7 => -7 + 0.61 => 1 + 0.69 => 1 + 0.65 => 1 + -0.65 => -1 + 0 => 0 + 1.9999 => 2 + -1.9999 => -2 + 0.0001 => 1 + -0.0001 => -1 + + precision: 1 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.7 + 0.69 => 0.7 + 0.65 => 0.7 + -0.65 => -0.7 + 0 => 0 + 1.9999 => 2 + -1.9999 => -2 + 0.0001 => 0.1 + -0.0001 => -0.1 + + precision: 2 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 + 0 => 0 + 1.9999 => 2 + -1.9999 => -2 + 0.0001 => 0.01 + -0.0001 => -0.01 + + precision: 10 + 2.5 => 2.5 + -2.5 => -2.5 + 3.5 => 3.5 + -3.5 => -3.5 + 7 => 7 + -7 => -7 + 0.61 => 0.61 + 0.69 => 0.69 + 0.65 => 0.65 + -0.65 => -0.65 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 From d0d76a08a0826de5b511ea21aff77e74dbe4aa1d Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Tue, 5 Sep 2023 00:01:06 +0200 Subject: [PATCH 02/25] Added tests for consistency with ceil() and floor() functions --- .../math/round_modes_ceiling_and_floor.phpt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ext/standard/tests/math/round_modes_ceiling_and_floor.phpt diff --git a/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt b/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt new file mode 100644 index 0000000000000..6c52f8269a263 --- /dev/null +++ b/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt @@ -0,0 +1,69 @@ +--TEST-- +round() with different rounding modes +--FILE-- + +--EXPECT-- +mode PHP_ROUND_CEILING +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) + +mode PHP_ROUND_FLOOR +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) From 2c853a8f2ff78d3e97c1231e7fbeba6debfd29dd Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Thu, 7 Sep 2023 23:12:36 +0200 Subject: [PATCH 03/25] Extend test cases for round modes --- ext/standard/tests/math/round_modes.phpt | 352 ++++++++++++++++++----- 1 file changed, 285 insertions(+), 67 deletions(-) diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index 139b668222be9..abca684c5d076 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -18,8 +18,10 @@ $numbers = [ -2.5, 3.5, -3.5, - 7, - -7, + 15.51, + -15.51, + 5, + -5, 0.61, 0.69, 0.65, @@ -30,7 +32,7 @@ $numbers = [ 0.0001, -0.0001, ]; -$precisions = [0, 1, 2, 10]; +$precisions = [-1, 0, 1, 2, 10]; foreach ($modes as $modeKey => $mode) { echo "mode: $modeKey\n"; @@ -49,13 +51,34 @@ foreach ($modes as $modeKey => $mode) { ?> --EXPECT-- mode: PHP_ROUND_HALF_UP + precision: -1 + 2.5 => 0 + -2.5 => -0 + 3.5 => 0 + -3.5 => -0 + 15.51 => 20 + -15.51 => -20 + 5 => 10 + -5 => -10 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 0 + -1.9999 => -0 + 0.0001 => 0 + -0.0001 => -0 + precision: 0 2.5 => 3 -2.5 => -3 3.5 => 4 -3.5 => -4 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -71,8 +94,10 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.7 0.65 => 0.7 @@ -88,8 +113,10 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -105,8 +132,10 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -118,13 +147,34 @@ mode: PHP_ROUND_HALF_UP -0.0001 => -0.0001 mode: PHP_ROUND_HALF_DOWN + precision: -1 + 2.5 => 0 + -2.5 => -0 + 3.5 => 0 + -3.5 => -0 + 15.51 => 20 + -15.51 => -20 + 5 => 0 + -5 => -0 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 0 + -1.9999 => -0 + 0.0001 => 0 + -0.0001 => -0 + precision: 0 2.5 => 2 -2.5 => -2 3.5 => 3 -3.5 => -3 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -140,8 +190,10 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.7 0.65 => 0.6 @@ -157,8 +209,10 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -174,8 +228,10 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -187,13 +243,34 @@ mode: PHP_ROUND_HALF_DOWN -0.0001 => -0.0001 mode: PHP_ROUND_HALF_EVEN + precision: -1 + 2.5 => 0 + -2.5 => -0 + 3.5 => 0 + -3.5 => -0 + 15.51 => 20 + -15.51 => -20 + 5 => 0 + -5 => 0 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 0 + -1.9999 => -0 + 0.0001 => 0 + -0.0001 => -0 + precision: 0 2.5 => 2 -2.5 => -2 3.5 => 4 -3.5 => -4 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -209,8 +286,10 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.7 0.65 => 0.6 @@ -226,8 +305,10 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -243,8 +324,10 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -256,13 +339,34 @@ mode: PHP_ROUND_HALF_EVEN -0.0001 => -0.0001 mode: PHP_ROUND_HALF_ODD + precision: -1 + 2.5 => 0 + -2.5 => -0 + 3.5 => 0 + -3.5 => -0 + 15.51 => 20 + -15.51 => -20 + 5 => 10 + -5 => -10 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 0 + -1.9999 => -0 + 0.0001 => 0 + -0.0001 => -0 + precision: 0 2.5 => 3 -2.5 => -3 3.5 => 3 -3.5 => -3 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -278,8 +382,10 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.7 0.65 => 0.7 @@ -295,8 +401,10 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -312,8 +420,10 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -325,13 +435,34 @@ mode: PHP_ROUND_HALF_ODD -0.0001 => -0.0001 mode: PHP_ROUND_CEILING + precision: -1 + 2.5 => 10 + -2.5 => -0 + 3.5 => 10 + -3.5 => -0 + 15.51 => 20 + -15.51 => -10 + 5 => 10 + -5 => -0 + 0.61 => 10 + 0.69 => 10 + 0.65 => 10 + -0.65 => -0 + 0 => 0 + 1.9999 => 10 + -1.9999 => -0 + 0.0001 => 10 + -0.0001 => -0 + precision: 0 2.5 => 3 -2.5 => -2 3.5 => 4 -3.5 => -3 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -15 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -347,8 +478,10 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.6 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.7 0.69 => 0.7 0.65 => 0.7 @@ -364,8 +497,10 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -381,8 +516,10 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -394,13 +531,34 @@ mode: PHP_ROUND_CEILING -0.0001 => -0.0001 mode: PHP_ROUND_FLOOR + precision: -1 + 2.5 => 0 + -2.5 => -10 + 3.5 => 0 + -3.5 => -10 + 15.51 => 10 + -15.51 => -20 + 5 => 0 + -5 => -10 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -10 + 0 => 0 + 1.9999 => 0 + -1.9999 => -10 + 0.0001 => 0 + -0.0001 => -10 + precision: 0 2.5 => 2 -2.5 => -3 3.5 => 3 -3.5 => -4 - 7 => 7 - -7 => -7 + 15.51 => 15 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 0 0.69 => 0 0.65 => 0 @@ -416,8 +574,10 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.6 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.6 0.65 => 0.6 @@ -433,8 +593,10 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -450,8 +612,10 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -463,13 +627,34 @@ mode: PHP_ROUND_FLOOR -0.0001 => -0.0001 mode: PHP_ROUND_TOWARD_ZERO + precision: -1 + 2.5 => 0 + -2.5 => -0 + 3.5 => 0 + -3.5 => -0 + 15.51 => 10 + -15.51 => -10 + 5 => 0 + -5 => -0 + 0.61 => 0 + 0.69 => 0 + 0.65 => 0 + -0.65 => -0 + 0 => 0 + 1.9999 => 0 + -1.9999 => -0 + 0.0001 => 0 + -0.0001 => -0 + precision: 0 2.5 => 2 -2.5 => -2 3.5 => 3 -3.5 => -3 - 7 => 7 - -7 => -7 + 15.51 => 15 + -15.51 => -15 + 5 => 5 + -5 => -5 0.61 => 0 0.69 => 0 0.65 => 0 @@ -485,8 +670,10 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.5 + -15.51 => -15.5 + 5 => 5 + -5 => -5 0.61 => 0.6 0.69 => 0.6 0.65 => 0.6 @@ -502,8 +689,10 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -519,8 +708,10 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -532,13 +723,34 @@ mode: PHP_ROUND_TOWARD_ZERO -0.0001 => -0.0001 mode: PHP_ROUND_AWAY_FROM_ZERO + precision: -1 + 2.5 => 10 + -2.5 => -10 + 3.5 => 10 + -3.5 => -10 + 15.51 => 20 + -15.51 => -20 + 5 => 10 + -5 => -10 + 0.61 => 10 + 0.69 => 10 + 0.65 => 10 + -0.65 => -10 + 0 => 0 + 1.9999 => 10 + -1.9999 => -10 + 0.0001 => 10 + -0.0001 => -10 + precision: 0 2.5 => 3 -2.5 => -3 3.5 => 4 -3.5 => -4 - 7 => 7 - -7 => -7 + 15.51 => 16 + -15.51 => -16 + 5 => 5 + -5 => -5 0.61 => 1 0.69 => 1 0.65 => 1 @@ -554,8 +766,10 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.6 + -15.51 => -15.6 + 5 => 5 + -5 => -5 0.61 => 0.7 0.69 => 0.7 0.65 => 0.7 @@ -571,8 +785,10 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 @@ -588,8 +804,10 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 - 7 => 7 - -7 => -7 + 15.51 => 15.51 + -15.51 => -15.51 + 5 => 5 + -5 => -5 0.61 => 0.61 0.69 => 0.69 0.65 => 0.65 From 087ebba32b5e346956cfa55de267c0d51f7d3d30 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Wed, 4 Oct 2023 01:07:32 +0200 Subject: [PATCH 04/25] Fix returning rounded number for switch --- ext/standard/math.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 82f91c856a1ce..110a860843f6b 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -137,14 +137,14 @@ static inline double php_round_helper(double value, int mode) { if (value >= 0.0) { return floor(value); } else { - return = ceil(value); + return ceil(value); } case PHP_ROUND_AWAY_FROM_ZERO: if (value >= 0.0) { - return = ceil(value); + return ceil(value); } else { - return = floor(value); + return floor(value); } case PHP_ROUND_HALF_EVEN: From 5be2200fa3d03f62434083678ac837f1601017d7 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Wed, 4 Oct 2023 20:19:33 +0200 Subject: [PATCH 05/25] Clear the code after rebase --- ext/standard/math.c | 69 ++++++++++++++---------- ext/standard/tests/math/round_modes.phpt | 2 +- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 110a860843f6b..217491d9bf462 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -285,16 +285,16 @@ PHP_FUNCTION(abs) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - if (Z_TYPE_P(value) == IS_DOUBLE) { - RETURN_DOUBLE(fabs(Z_DVAL_P(value))); - } else if (Z_TYPE_P(value) == IS_LONG) { - if (Z_LVAL_P(value) == ZEND_LONG_MIN) { - RETURN_DOUBLE(-(double)ZEND_LONG_MIN); - } else { - RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); - } - } else { - ZEND_ASSERT(0 && "Unexpected type"); + switch (Z_TYPE_P(value)) { + case IS_LONG: + if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { + RETURN_DOUBLE(-(double)ZEND_LONG_MIN); + } else { + RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); + } + case IS_DOUBLE: + RETURN_DOUBLE(fabs(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); } } /* }}} */ @@ -308,12 +308,12 @@ PHP_FUNCTION(ceil) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - if (Z_TYPE_P(value) == IS_DOUBLE) { - RETURN_DOUBLE(ceil(Z_DVAL_P(value))); - } else if (Z_TYPE_P(value) == IS_LONG) { - RETURN_DOUBLE(zval_get_double(value)); - } else { - ZEND_ASSERT(0 && "Unexpected type"); + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(ceil(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); } } /* }}} */ @@ -327,12 +327,12 @@ PHP_FUNCTION(floor) Z_PARAM_NUMBER(value) ZEND_PARSE_PARAMETERS_END(); - if (Z_TYPE_P(value) == IS_DOUBLE) { - RETURN_DOUBLE(floor(Z_DVAL_P(value))); - } else if (Z_TYPE_P(value) == IS_LONG) { - RETURN_DOUBLE(zval_get_double(value)); - } else { - ZEND_ASSERT(0 && "Unexpected type"); + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(floor(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); } } /* }}} */ @@ -344,7 +344,6 @@ PHP_FUNCTION(round) int places = 0; zend_long precision = 0; zend_long mode = PHP_ROUND_HALF_UP; - double return_val; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_NUMBER(value) @@ -361,21 +360,33 @@ PHP_FUNCTION(round) } } + switch (mode) { + case PHP_ROUND_HALF_UP: + case PHP_ROUND_HALF_DOWN: + case PHP_ROUND_HALF_EVEN: + case PHP_ROUND_HALF_ODD: + case PHP_ROUND_AWAY_FROM_ZERO: + case PHP_ROUND_TOWARD_ZERO: + case PHP_ROUND_CEILING: + case PHP_ROUND_FLOOR: + break; + default: + zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); + RETURN_THROWS(); + } + switch (Z_TYPE_P(value)) { case IS_LONG: /* Simple case - long that doesn't need to be rounded. */ if (places >= 0) { - RETURN_DOUBLE((double) Z_LVAL_P(value)); + RETURN_DOUBLE(zval_get_double(value)); } ZEND_FALLTHROUGH; case IS_DOUBLE: - return_val = (Z_TYPE_P(value) == IS_LONG) ? (double)Z_LVAL_P(value) : Z_DVAL_P(value); - return_val = _php_math_round(return_val, (int)places, (int)mode); - RETURN_DOUBLE(return_val); - break; + RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int)places, (int)mode)); - EMPTY_SWITCH_DEFAULT_CASE() + EMPTY_SWITCH_DEFAULT_CASE(); } } /* }}} */ diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index abca684c5d076..e78f14810f4f5 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -251,7 +251,7 @@ mode: PHP_ROUND_HALF_EVEN 15.51 => 20 -15.51 => -20 5 => 0 - -5 => 0 + -5 => -0 0.61 => 0 0.69 => 0 0.65 => 0 From 40b93696ca964b40c1b5717e55f7015e06ce7dd3 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Wed, 4 Oct 2023 23:53:51 +0200 Subject: [PATCH 06/25] Add aliases and deprecate constants ROUND_DOWN and ROUND_UP --- ext/intl/formatter/formatter.stub.php | 4 ++ ext/intl/tests/formatter/rounding_modes.phpt | 41 ++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 ext/intl/tests/formatter/rounding_modes.phpt diff --git a/ext/intl/formatter/formatter.stub.php b/ext/intl/formatter/formatter.stub.php index 430c02bd60a67..51b2912ea7a11 100644 --- a/ext/intl/formatter/formatter.stub.php +++ b/ext/intl/formatter/formatter.stub.php @@ -44,6 +44,10 @@ class NumberFormatter public const int ROUND_DOWN = UNKNOWN; /** @cvalue UNUM_ROUND_UP */ public const int ROUND_UP = UNKNOWN; + /** @cvalue UNUM_ROUND_DOWN */ + public const ROUND_TOWARD_ZERO = UNKNOWN; + /** @cvalue UNUM_ROUND_UP */ + public const ROUND_AWAY_FROM_ZERO = UNKNOWN; /** @cvalue UNUM_ROUND_HALFEVEN */ public const int ROUND_HALFEVEN = UNKNOWN; /** @cvalue UNUM_ROUND_HALFDOWN */ diff --git a/ext/intl/tests/formatter/rounding_modes.phpt b/ext/intl/tests/formatter/rounding_modes.phpt new file mode 100644 index 0000000000000..32204bc8bb089 --- /dev/null +++ b/ext/intl/tests/formatter/rounding_modes.phpt @@ -0,0 +1,41 @@ +--TEST-- +IntlBreakIterator: clone handler +--EXTENSIONS-- +intl +--FILE-- + +--EXPECTF-- +int(0) +int(1) + +Deprecated: Constant NumberFormatter::ROUND_DOWN is deprecated in %s +int(2) +int(2) + +Deprecated: Constant NumberFormatter::ROUND_UP is deprecated in %s +int(3) +int(3) +int(4) +int(5) +int(6) + +Deprecated: Constant NumberFormatter::ROUND_DOWN is deprecated in %s +bool(true) + +Deprecated: Constant NumberFormatter::ROUND_UP is deprecated in %s +bool(true) \ No newline at end of file From 962f7521daa4677cb1a038ccee268e45cb579301 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Thu, 5 Oct 2023 00:12:18 +0200 Subject: [PATCH 07/25] Added information about constants from Intl extension to NEWS --- NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS b/NEWS index b7afa5de16afa..ee7101e55bad1 100644 --- a/NEWS +++ b/NEWS @@ -30,6 +30,9 @@ Intl: . Added IntlDateFormatter::PATTERN constant. (David Carlier) . Fixed Numberformatter::__construct when the locale is invalid, now throws an exception. (David Carlier) + . Deprecated constants ROUND_DOWN and ROUND_UP for the consistency with new + rounding modes. Added instead aliases ROUND_TOWARD_ZERO and + ROUND_AWAY_FROM_ZERO. (Jorg Sowa) MBString: . Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada) From 823025dcdb35ceb2c10d3c385a407a3c1aad3bc6 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Thu, 5 Oct 2023 22:28:15 +0200 Subject: [PATCH 08/25] Undeprecate the constants --- NEWS | 5 ++--- ext/intl/tests/formatter/rounding_modes.phpt | 12 ++---------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/NEWS b/NEWS index ee7101e55bad1..bedfb9db0d482 100644 --- a/NEWS +++ b/NEWS @@ -30,9 +30,8 @@ Intl: . Added IntlDateFormatter::PATTERN constant. (David Carlier) . Fixed Numberformatter::__construct when the locale is invalid, now throws an exception. (David Carlier) - . Deprecated constants ROUND_DOWN and ROUND_UP for the consistency with new - rounding modes. Added instead aliases ROUND_TOWARD_ZERO and - ROUND_AWAY_FROM_ZERO. (Jorg Sowa) + . Created aliases ROUND_TOWARD_ZERO and ROUND_AWAY_FROM_ZERO for constants + ROUND_DOWN and ROUND_UP. (Jorg Sowa) MBString: . Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada) diff --git a/ext/intl/tests/formatter/rounding_modes.phpt b/ext/intl/tests/formatter/rounding_modes.phpt index 32204bc8bb089..9edde4ac7fbd6 100644 --- a/ext/intl/tests/formatter/rounding_modes.phpt +++ b/ext/intl/tests/formatter/rounding_modes.phpt @@ -1,5 +1,5 @@ --TEST-- -IntlBreakIterator: clone handler +NumberFormatter: rounding modes --EXTENSIONS-- intl --FILE-- @@ -22,20 +22,12 @@ var_dump(NumberFormatter::ROUND_UP === NumberFormatter::ROUND_AWAY_FROM_ZERO); --EXPECTF-- int(0) int(1) - -Deprecated: Constant NumberFormatter::ROUND_DOWN is deprecated in %s int(2) int(2) - -Deprecated: Constant NumberFormatter::ROUND_UP is deprecated in %s int(3) int(3) int(4) int(5) int(6) - -Deprecated: Constant NumberFormatter::ROUND_DOWN is deprecated in %s bool(true) - -Deprecated: Constant NumberFormatter::ROUND_UP is deprecated in %s -bool(true) \ No newline at end of file +bool(true) From 6790846143d33127a425135e0e9d5bfee9a19213 Mon Sep 17 00:00:00 2001 From: Jorg Adam Sowa Date: Thu, 5 Oct 2023 22:29:00 +0200 Subject: [PATCH 09/25] Refactored rounding function --- ext/standard/math.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 217491d9bf462..13dbeb861f2f8 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -93,7 +93,6 @@ static inline double php_intpow10(int power) { /* {{{ php_round_helper Actually performs the rounding of a value to integer in a certain mode */ static inline double php_round_helper(double value, int mode) { - double integral, fractional; /* Split the input value into the integral and fractional part. * @@ -101,11 +100,12 @@ static inline double php_round_helper(double value, int mode) { * the absolute value of the fractional part (which will not result * in branches in the assembly) to make the following cases simpler. */ + double integral; + double fractional = modf(value, &integral); switch (mode) { case PHP_ROUND_HALF_UP: - fractional = fabs(modf(value, &integral)); - if (fractional >= 0.5) { + if (fractional >= 0.5 || fractional <= -0.5) { /* We must increase the magnitude of the integral part * (rounding up / towards infinity). copysign(1.0, integral) * will either result in 1.0 or -1.0 depending on the sign @@ -120,40 +120,36 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_DOWN: - fractional = fabs(modf(value, &integral)); - if (fractional > 0.5) { + if (fractional > 0.5 || fractional < -0.5) { return integral + copysign(1.0, integral); } return integral; case PHP_ROUND_CEILING: - return ceil(value); + if (value > 0.0) { + return fractional == 0 ? integral : integral + 1.0; + } + return integral; case PHP_ROUND_FLOOR: - return floor(value); + if (value < 0.0) { + return fractional == 0 ? integral : integral - 1.0; + } + return integral; case PHP_ROUND_TOWARD_ZERO: - if (value >= 0.0) { - return floor(value); - } else { - return ceil(value); - } + return integral; case PHP_ROUND_AWAY_FROM_ZERO: - if (value >= 0.0) { - return ceil(value); - } else { - return floor(value); - } + return fractional == 0 ? integral : integral + copysign(1.0, integral); case PHP_ROUND_HALF_EVEN: - fractional = fabs(modf(value, &integral)); - if (fractional > 0.5) { + if (fractional > 0.5 || fractional < -0.5) { return integral + copysign(1.0, integral); } - if (UNEXPECTED(fractional == 0.5)) { + if (UNEXPECTED(fractional == 0.5 || fractional == -0.5)) { bool even = !fmod(integral, 2.0); /* If the integral part is not even we can make it even @@ -167,12 +163,11 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_ODD: - fractional = fabs(modf(value, &integral)); - if (fractional > 0.5) { + if (fractional > 0.5 || fractional < -0.5) { return integral + copysign(1.0, integral); } - if (UNEXPECTED(fractional == 0.5)) { + if (UNEXPECTED(fractional == 0.5 || fractional == -0.5)) { bool even = !fmod(integral, 2.0); if (even) { From 88446999c4ba3aa64849a3e98d02d7afed50ba97 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 14 Nov 2023 23:44:55 +0100 Subject: [PATCH 10/25] Generate stubs --- ext/intl/formatter/formatter.stub.php | 4 ++-- ext/intl/formatter/formatter_arginfo.h | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ext/intl/formatter/formatter.stub.php b/ext/intl/formatter/formatter.stub.php index 51b2912ea7a11..4e31b7dc22c01 100644 --- a/ext/intl/formatter/formatter.stub.php +++ b/ext/intl/formatter/formatter.stub.php @@ -45,9 +45,9 @@ class NumberFormatter /** @cvalue UNUM_ROUND_UP */ public const int ROUND_UP = UNKNOWN; /** @cvalue UNUM_ROUND_DOWN */ - public const ROUND_TOWARD_ZERO = UNKNOWN; + public const int ROUND_TOWARD_ZERO = UNKNOWN; /** @cvalue UNUM_ROUND_UP */ - public const ROUND_AWAY_FROM_ZERO = UNKNOWN; + public const int ROUND_AWAY_FROM_ZERO = UNKNOWN; /** @cvalue UNUM_ROUND_HALFEVEN */ public const int ROUND_HALFEVEN = UNKNOWN; /** @cvalue UNUM_ROUND_HALFDOWN */ diff --git a/ext/intl/formatter/formatter_arginfo.h b/ext/intl/formatter/formatter_arginfo.h index 8d406350aef5f..fc50aadeb9496 100644 --- a/ext/intl/formatter/formatter_arginfo.h +++ b/ext/intl/formatter/formatter_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 6178ab530b85aca5c90fb7369c5019bb5cbfe8a7 */ + * Stub hash: 5d5421c72894a77894872efdf060ec058d275c13 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter___construct, 0, 0, 2) ZEND_ARG_TYPE_INFO(0, locale, IS_STRING, 0) @@ -226,6 +226,18 @@ static zend_class_entry *register_class_NumberFormatter(void) zend_declare_typed_class_constant(class_entry, const_ROUND_UP_name, &const_ROUND_UP_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(const_ROUND_UP_name); + zval const_ROUND_TOWARD_ZERO_value; + ZVAL_LONG(&const_ROUND_TOWARD_ZERO_value, UNUM_ROUND_DOWN); + zend_string *const_ROUND_TOWARD_ZERO_name = zend_string_init_interned("ROUND_TOWARD_ZERO", sizeof("ROUND_TOWARD_ZERO") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_ROUND_TOWARD_ZERO_name, &const_ROUND_TOWARD_ZERO_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_ROUND_TOWARD_ZERO_name); + + zval const_ROUND_AWAY_FROM_ZERO_value; + ZVAL_LONG(&const_ROUND_AWAY_FROM_ZERO_value, UNUM_ROUND_UP); + zend_string *const_ROUND_AWAY_FROM_ZERO_name = zend_string_init_interned("ROUND_AWAY_FROM_ZERO", sizeof("ROUND_AWAY_FROM_ZERO") - 1, 1); + zend_declare_typed_class_constant(class_entry, const_ROUND_AWAY_FROM_ZERO_name, &const_ROUND_AWAY_FROM_ZERO_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + zend_string_release(const_ROUND_AWAY_FROM_ZERO_name); + zval const_ROUND_HALFEVEN_value; ZVAL_LONG(&const_ROUND_HALFEVEN_value, UNUM_ROUND_HALFEVEN); zend_string *const_ROUND_HALFEVEN_name = zend_string_init_interned("ROUND_HALFEVEN", sizeof("ROUND_HALFEVEN") - 1, 1); From f4fa18c5b7c1707d3cab47164abe94ca4cc2561c Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 15 Nov 2023 00:13:43 +0100 Subject: [PATCH 11/25] use fabs method for specific rounding modes --- ext/standard/math.c | 1216 +++++++++++++++++++++++-------------------- 1 file changed, 649 insertions(+), 567 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 13dbeb861f2f8..48fe3f67d0840 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -105,7 +105,7 @@ static inline double php_round_helper(double value, int mode) { switch (mode) { case PHP_ROUND_HALF_UP: - if (fractional >= 0.5 || fractional <= -0.5) { + if (fabs(fractional) >= 0.5) { /* We must increase the magnitude of the integral part * (rounding up / towards infinity). copysign(1.0, integral) * will either result in 1.0 or -1.0 depending on the sign @@ -120,7 +120,7 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_DOWN: - if (fractional > 0.5 || fractional < -0.5) { + if (fabs(fractional) > 0.5) { return integral + copysign(1.0, integral); } @@ -145,11 +145,12 @@ static inline double php_round_helper(double value, int mode) { return fractional == 0 ? integral : integral + copysign(1.0, integral); case PHP_ROUND_HALF_EVEN: - if (fractional > 0.5 || fractional < -0.5) { + fractional = fabs(fractional); + if (fractional > 0.5) { return integral + copysign(1.0, integral); } - if (UNEXPECTED(fractional == 0.5 || fractional == -0.5)) { + if (UNEXPECTED(fractional == 0.5)) { bool even = !fmod(integral, 2.0); /* If the integral part is not even we can make it even @@ -163,11 +164,12 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_ODD: - if (fractional > 0.5 || fractional < -0.5) { + fractional = fabs(fractional); + if (fractional > 0.5) { return integral + copysign(1.0, integral); } - if (UNEXPECTED(fractional == 0.5 || fractional == -0.5)) { + if (UNEXPECTED(fractional == 0.5)) { bool even = !fmod(integral, 2.0); if (even) { @@ -296,8 +298,8 @@ PHP_FUNCTION(abs) /* {{{ Returns the next highest integer value of the number */ PHP_FUNCTION(ceil) -{ - zval *value; + { + zval * value; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) @@ -347,26 +349,26 @@ PHP_FUNCTION(round) Z_PARAM_LONG(mode) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() >= 2) { - if (precision >= 0) { - places = ZEND_LONG_INT_OVFL(precision) ? INT_MAX : (int)precision; - } else { - places = ZEND_LONG_INT_UDFL(precision) ? INT_MIN : (int)precision; + if (ZEND_NUM_ARGS() >= 2) { + if (precision >= 0) { + places = ZEND_LONG_INT_OVFL(precision) ? INT_MAX : (int) precision; + } else { + places = ZEND_LONG_INT_UDFL(precision) ? INT_MIN : (int) precision; + } } - } - switch (mode) { - case PHP_ROUND_HALF_UP: - case PHP_ROUND_HALF_DOWN: - case PHP_ROUND_HALF_EVEN: - case PHP_ROUND_HALF_ODD: - case PHP_ROUND_AWAY_FROM_ZERO: - case PHP_ROUND_TOWARD_ZERO: - case PHP_ROUND_CEILING: - case PHP_ROUND_FLOOR: - break; - default: - zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); + switch (mode) { + case PHP_ROUND_HALF_UP: + case PHP_ROUND_HALF_DOWN: + case PHP_ROUND_HALF_EVEN: + case PHP_ROUND_HALF_ODD: + case PHP_ROUND_AWAY_FROM_ZERO: + case PHP_ROUND_TOWARD_ZERO: + case PHP_ROUND_CEILING: + case PHP_ROUND_FLOOR: + break; + default: + zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); RETURN_THROWS(); } @@ -378,411 +380,422 @@ PHP_FUNCTION(round) } ZEND_FALLTHROUGH; - case IS_DOUBLE: - RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int)places, (int)mode)); + case IS_DOUBLE: + RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int) places, (int) mode)); - EMPTY_SWITCH_DEFAULT_CASE(); - } -} + EMPTY_SWITCH_DEFAULT_CASE(); + } + } /* }}} */ /* {{{ Returns the sine of the number in radians */ PHP_FUNCTION(sin) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sin(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(sin(num)); + } /* }}} */ /* {{{ Returns the cosine of the number in radians */ PHP_FUNCTION(cos) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(cos(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(cos(num)); + } /* }}} */ /* {{{ Returns the tangent of the number in radians */ PHP_FUNCTION(tan) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(tan(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(tan(num)); + } /* }}} */ /* {{{ Returns the arc sine of the number in radians */ PHP_FUNCTION(asin) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(asin(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(asin(num)); + } /* }}} */ /* {{{ Return the arc cosine of the number in radians */ PHP_FUNCTION(acos) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(acos(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(acos(num)); + } /* }}} */ /* {{{ Returns the arc tangent of the number in radians */ PHP_FUNCTION(atan) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atan(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atan(num)); + } /* }}} */ /* {{{ Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */ PHP_FUNCTION(atan2) -{ - double num1, num2; + { + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atan2(num1, num2)); -} + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atan2(num1, num2)); + } /* }}} */ /* {{{ Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */ PHP_FUNCTION(sinh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sinh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(sinh(num)); + } /* }}} */ /* {{{ Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */ PHP_FUNCTION(cosh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(cosh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(cosh(num)); + } /* }}} */ /* {{{ Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */ PHP_FUNCTION(tanh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(tanh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(tanh(num)); + } /* }}} */ /* {{{ Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number */ PHP_FUNCTION(asinh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(asinh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(asinh(num)); + } /* }}} */ /* {{{ Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number */ PHP_FUNCTION(acosh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(acosh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(acosh(num)); + } /* }}} */ /* {{{ Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number */ PHP_FUNCTION(atanh) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atanh(num)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atanh(num)); + } /* }}} */ /* {{{ Returns an approximation of pi */ PHP_FUNCTION(pi) -{ - ZEND_PARSE_PARAMETERS_NONE(); + { + ZEND_PARSE_PARAMETERS_NONE(); - RETURN_DOUBLE(M_PI); -} + RETURN_DOUBLE(M_PI); + } /* }}} */ /* {{{ Returns whether argument is finite */ PHP_FUNCTION(is_finite) -{ - double dval; + { + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_finite(dval)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_finite(dval)); + } /* }}} */ /* {{{ Returns whether argument is infinite */ PHP_FUNCTION(is_infinite) -{ - double dval; + { + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_isinf(dval)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_isinf(dval)); + } /* }}} */ /* {{{ Returns whether argument is not a number */ PHP_FUNCTION(is_nan) -{ - double dval; + { + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_isnan(dval)); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_isnan(dval)); + } /* }}} */ /* {{{ Returns base raised to the power of exponent. Returns integer result when possible */ PHP_FUNCTION(pow) -{ - zval *zbase, *zexp; + { + zval * zbase, *zexp; - ZEND_PARSE_PARAMETERS_START(2, 2) + ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ZVAL(zbase) Z_PARAM_ZVAL(zexp) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - pow_function(return_value, zbase, zexp); -} + pow_function(return_value, zbase, zexp); + } /* }}} */ /* {{{ Returns e raised to the power of the number */ PHP_FUNCTION(exp) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(exp(num)); -} + RETURN_DOUBLE(exp(num)); + } /* }}} */ /* {{{ Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero */ PHP_FUNCTION(expm1) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(expm1(num)); -} + RETURN_DOUBLE(expm1(num)); + } /* }}} */ /* {{{ Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero */ PHP_FUNCTION(log1p) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(log1p(num)); -} + RETURN_DOUBLE(log1p(num)); + } /* }}} */ /* {{{ Returns the natural logarithm of the number, or the base log if base is specified */ PHP_FUNCTION(log) -{ - double num, base = 0; + { + double num, base = 0; - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_DOUBLE(num) - Z_PARAM_OPTIONAL - Z_PARAM_DOUBLE(base) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_DOUBLE(num) + Z_PARAM_OPTIONAL + Z_PARAM_DOUBLE(base) + ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 1) { - RETURN_DOUBLE(log(num)); - } + if (ZEND_NUM_ARGS() == 1) { + RETURN_DOUBLE(log(num)); + } - if (base == 2.0) { - RETURN_DOUBLE(log2(num)); - } + if (base == 2.0) { + RETURN_DOUBLE(log2(num)); + } - if (base == 10.0) { - RETURN_DOUBLE(log10(num)); - } + if (base == 10.0) { + RETURN_DOUBLE(log10(num)); + } - if (base == 1.0) { - RETURN_DOUBLE(ZEND_NAN); - } + if (base == 1.0) { + RETURN_DOUBLE(ZEND_NAN); + } - if (base <= 0.0) { - zend_argument_value_error(2, "must be greater than 0"); - RETURN_THROWS(); - } + if (base <= 0.0) { + zend_argument_value_error(2, "must be greater than 0"); + RETURN_THROWS(); + } - RETURN_DOUBLE(log(num) / log(base)); -} + RETURN_DOUBLE(log(num) / log(base)); + } /* }}} */ /* {{{ Returns the base-10 logarithm of the number */ PHP_FUNCTION(log10) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(log10(num)); -} + RETURN_DOUBLE(log10(num)); + } /* }}} */ /* {{{ Returns the square root of the number */ PHP_FUNCTION(sqrt) -{ - double num; + { + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sqrt(num)); -} + RETURN_DOUBLE(sqrt(num)); + } /* }}} */ /* {{{ Returns sqrt(num1*num1 + num2*num2) */ PHP_FUNCTION(hypot) -{ - double num1, num2; + { + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(hypot(num1, num2)); -} + RETURN_DOUBLE(hypot(num1, num2)); + } /* }}} */ /* {{{ Converts the number in degrees to the radian equivalent */ PHP_FUNCTION(deg2rad) -{ - double deg; + { + double deg; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(deg) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE((deg / 180.0) * M_PI); -} + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(deg) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE((deg / 180.0) * M_PI); + } /* }}} */ /* {{{ Converts the radian number to the equivalent number in degrees */ PHP_FUNCTION(rad2deg) -{ - double rad; + { + double rad; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(rad) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(rad) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE((rad / M_PI) * 180); -} + RETURN_DOUBLE((rad / M_PI) * 180); + } /* }}} */ /* {{{ _php_math_basetolong */ /* * Convert a string representation of a base(2-36) number to a long. */ -PHPAPI zend_long _php_math_basetolong(zval *arg, int base) +PHPAPI zend_long +_php_math_basetolong(zval +*arg, +int base +) { - zend_long num = 0, digit, onum; - zend_long i; - char c, *s; - - if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) { - return 0; - } +zend_long num = 0, digit, onum; +zend_long i; +char c, *s; + +if ( +Z_TYPE_P(arg) +!= IS_STRING || base < 2 || base > 36) { +return 0; +} - s = Z_STRVAL_P(arg); +s = Z_STRVAL_P(arg); - for (i = Z_STRLEN_P(arg); i > 0; i--) { - c = *s++; +for ( +i = Z_STRLEN_P(arg); +i > 0; i--) { +c = *s++; - digit = (c >= '0' && c <= '9') ? c - '0' - : (c >= 'A' && c <= 'Z') ? c - 'A' + 10 - : (c >= 'a' && c <= 'z') ? c - 'a' + 10 - : base; +digit = (c >= '0' && c <= '9') ? c - '0' + : (c >= 'A' && c <= 'Z') ? c - 'A' + 10 + : (c >= 'a' && c <= 'z') ? c - 'a' + 10 + : base; - if (digit >= base) { - continue; - } +if (digit >= base) { +continue; +} - onum = num; - num = num * base + digit; - if (num > onum) - continue; +onum = num; +num = num * base + digit; +if (num > onum) +continue; - { +{ - php_error_docref(NULL, E_WARNING, "Number %s is too big to fit in long", s); - return ZEND_LONG_MAX; - } - } +php_error_docref(NULL, E_WARNING, +"Number %s is too big to fit in long", s); +return +ZEND_LONG_MAX; +} +} - return num; +return +num; } /* }}} */ @@ -790,8 +803,7 @@ PHPAPI zend_long _php_math_basetolong(zval *arg, int base) /* * Convert a string representation of a base(2-36) number to a zval. */ -PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) -{ +PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) { zend_long num = 0; double fnum = 0; int mode = 0; @@ -806,7 +818,7 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) /* Skip leading whitespace */ while (s < e && isspace(*s)) s++; /* Skip trailing whitespace */ - while (s < e && isspace(*(e-1))) e--; + while (s < e && isspace(*(e - 1))) e--; if (e - s >= 2) { if (base == 16 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2; @@ -838,17 +850,17 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) } switch (mode) { - case 0: /* Integer */ - if (num < cutoff || (num == cutoff && c <= cutlim)) { - num = num * base + c; - break; - } else { - fnum = (double)num; - mode = 1; - } - ZEND_FALLTHROUGH; - case 1: /* Float */ - fnum = fnum * base + c; + case 0: /* Integer */ + if (num < cutoff || (num == cutoff && c <= cutlim)) { + num = num * base + c; + break; + } else { + fnum = (double) num; + mode = 1; + } + ZEND_FALLTHROUGH; + case 1: /* Float */ + fnum = fnum * base + c; } } @@ -869,29 +881,43 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) * Convert a long to a string containing a base(2-36) representation of * the number. */ -PHPAPI zend_string * _php_math_longtobase(zend_long arg, int base) +PHPAPI zend_string +* +_php_math_longtobase(zend_long +arg, +int base +) { - static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - char buf[(sizeof(zend_ulong) << 3) + 1]; - char *ptr, *end; - zend_ulong value; +static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +char buf[(sizeof(zend_ulong) << 3) + 1]; +char *ptr, *end; +zend_ulong value; - if (base < 2 || base > 36) { - return ZSTR_EMPTY_ALLOC(); - } +if (base < 2 || base > 36) { +return + +ZSTR_EMPTY_ALLOC(); + +} - value = arg; +value = arg; - end = ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; +end = ptr = buf + sizeof(buf) - 1; +* +ptr = '\0'; - do { - ZEND_ASSERT(ptr > buf); - *--ptr = digits[value % base]; - value /= base; - } while (value); +do { +ZEND_ASSERT(ptr +> buf); +*-- +ptr = digits[value % base]; +value /= +base; +} while (value); - return zend_string_init(ptr, end - ptr, 0); +return +zend_string_init(ptr, end +- ptr, 0); } /* }}} */ @@ -900,33 +926,45 @@ PHPAPI zend_string * _php_math_longtobase(zend_long arg, int base) * Convert a long to a string containing a base(2,4,6,16,32) representation of * the number. */ -static zend_always_inline zend_string * _php_math_longtobase_pwr2(zend_long arg, int base_log2) +static zend_always_inline zend_string +* +_php_math_longtobase_pwr2(zend_long +arg, +int base_log2 +) { - static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - zend_ulong value; - size_t len; - zend_string *ret; - char *ptr; - - value = arg; - - if (value == 0) { - len = 1; - } else { - len = ((sizeof(value) * 8 - zend_ulong_nlz(value)) + (base_log2 - 1)) / base_log2; - } - - ret = zend_string_alloc(len, 0); - ptr = ZSTR_VAL(ret) + len; - *ptr = '\0'; - - do { - ZEND_ASSERT(ptr > ZSTR_VAL(ret)); - *--ptr = digits[value & ((1 << base_log2) - 1)]; - value >>= base_log2; - } while (value); +static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +zend_ulong value; +size_t len; +zend_string *ret; +char *ptr; + +value = arg; + +if (value == 0) { +len = 1; +} else { +len = ((sizeof(value) * 8 - zend_ulong_nlz(value)) + (base_log2 - 1)) / base_log2; +} - return ret; +ret = zend_string_alloc(len, 0); +ptr = ZSTR_VAL(ret) + len; +* +ptr = '\0'; + +do { +ZEND_ASSERT(ptr +> +ZSTR_VAL(ret) +); +*-- +ptr = digits[value & ((1 << base_log2) - 1)]; +value >>= +base_log2; +} while (value); + +return +ret; } /* }}} */ @@ -935,162 +973,192 @@ static zend_always_inline zend_string * _php_math_longtobase_pwr2(zend_long arg, * Convert a zval to a string containing a base(2-36) representation of * the number. */ -PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base) +PHPAPI zend_string +* +_php_math_zvaltobase(zval +*arg, +int base +) { - static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) { - return ZSTR_EMPTY_ALLOC(); - } +if (( +Z_TYPE_P(arg) +!= IS_LONG && +Z_TYPE_P(arg) +!= IS_DOUBLE) || base < 2 || base > 36) { +return - if (Z_TYPE_P(arg) == IS_DOUBLE) { - double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */ - char *ptr, *end; - char buf[(sizeof(double) << 3) + 1]; +ZSTR_EMPTY_ALLOC(); - /* Don't try to convert +/- infinity */ - if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) { - zend_value_error("An infinite value cannot be converted to base %d", base); - return NULL; - } +} + +if ( +Z_TYPE_P(arg) +== IS_DOUBLE) { +double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */ +char *ptr, *end; +char buf[(sizeof(double) << 3) + 1]; + +/* Don't try to convert +/- infinity */ +if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) { +zend_value_error("An infinite value cannot be converted to base %d", base); +return +NULL; +} - end = ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; +end = ptr = buf + sizeof(buf) - 1; +* +ptr = '\0'; + +do { +*-- +ptr = digits[(int) fmod(fvalue, base)]; +fvalue /= +base; +} while (ptr > buf && +fabs(fvalue) +>= 1); + +return +zend_string_init(ptr, end +- ptr, 0); +} - do { - *--ptr = digits[(int) fmod(fvalue, base)]; - fvalue /= base; - } while (ptr > buf && fabs(fvalue) >= 1); +return - return zend_string_init(ptr, end - ptr, 0); - } +_php_math_longtobase(Z_LVAL_P(arg), base - return _php_math_longtobase(Z_LVAL_P(arg), base); +); } /* }}} */ /* {{{ Returns the decimal equivalent of the binary number */ PHP_FUNCTION(bindec) -{ - zend_string *arg; + { + zend_string * arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 2, return_value); -} + _php_math_basetozval(arg, 2, return_value); + } /* }}} */ /* {{{ Returns the decimal equivalent of the hexadecimal number */ PHP_FUNCTION(hexdec) -{ - zend_string *arg; + { + zend_string * arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 16, return_value); -} + _php_math_basetozval(arg, 16, return_value); + } /* }}} */ /* {{{ Returns the decimal equivalent of an octal string */ PHP_FUNCTION(octdec) -{ - zend_string *arg; + { + zend_string * arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 8, return_value); -} + _php_math_basetozval(arg, 8, return_value); + } /* }}} */ /* {{{ Returns a string containing a binary representation of the number */ PHP_FUNCTION(decbin) -{ - zend_long arg; + { + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 1)); -} + RETURN_STR(_php_math_longtobase_pwr2(arg, 1)); + } /* }}} */ /* {{{ Returns a string containing an octal representation of the given number */ PHP_FUNCTION(decoct) -{ - zend_long arg; + { + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 3)); -} + RETURN_STR(_php_math_longtobase_pwr2(arg, 3)); + } /* }}} */ /* {{{ Returns a string containing a hexadecimal representation of the given number */ PHP_FUNCTION(dechex) -{ - zend_long arg; + { + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 4)); -} + RETURN_STR(_php_math_longtobase_pwr2(arg, 4)); + } /* }}} */ /* {{{ Converts a number in a string from any base <= 36 to any base <= 36 */ PHP_FUNCTION(base_convert) -{ - zval temp; - zend_string *number; - zend_long frombase, tobase; - zend_string *result; + { + zval temp; + zend_string *number; + zend_long frombase, tobase; + zend_string *result; - ZEND_PARSE_PARAMETERS_START(3, 3) + ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_STR(number) Z_PARAM_LONG(frombase) Z_PARAM_LONG(tobase) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (frombase < 2 || frombase > 36) { - zend_argument_value_error(2, "must be between 2 and 36 (inclusive)"); - RETURN_THROWS(); - } - if (tobase < 2 || tobase > 36) { - zend_argument_value_error(3, "must be between 2 and 36 (inclusive)"); - RETURN_THROWS(); - } + if (frombase < 2 || frombase > 36) { + zend_argument_value_error(2, "must be between 2 and 36 (inclusive)"); + RETURN_THROWS(); + } + if (tobase < 2 || tobase > 36) { + zend_argument_value_error(3, "must be between 2 and 36 (inclusive)"); + RETURN_THROWS(); + } - _php_math_basetozval(number, (int)frombase, &temp); - result = _php_math_zvaltobase(&temp, (int)tobase); - if (!result) { - RETURN_THROWS(); - } + _php_math_basetozval(number, (int)frombase, &temp); + result = _php_math_zvaltobase(&temp, (int)tobase); + if (!result) { + RETURN_THROWS(); + } - RETVAL_STR(result); -} + RETVAL_STR(result); + } /* }}} */ /* {{{ _php_math_number_format */ -PHPAPI zend_string *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep) -{ +PHPAPI zend_string +* + +_php_math_number_format(double d, int dec, char dec_point, char thousand_sep) { return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1); } -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) -{ - zend_string *res; - zend_string *tmpbuf; +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) { + zend_string * res; + zend_string * tmpbuf; char *s, *t; /* source, target */ char *dp; size_t integral; @@ -1108,7 +1176,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de tmpbuf = strpprintf(0, "%.*F", dec, d); if (tmpbuf == NULL) { return NULL; - } else if (!isdigit((int)ZSTR_VAL(tmpbuf)[0])) { + } else if (!isdigit((int) ZSTR_VAL(tmpbuf)[0])) { return tmpbuf; } @@ -1134,7 +1202,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de /* allow for thousand separators */ if (thousand_sep) { - integral = zend_safe_addmult((integral-1)/3, thousand_sep_len, integral, "number formatting"); + integral = zend_safe_addmult((integral - 1) / 3, thousand_sep_len, integral, "number formatting"); } reslen = integral; @@ -1162,7 +1230,9 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de * we requested due to internal buffer limitations */ if (dec) { size_t declen = (dp ? s - dp : 0); - size_t topad = (size_t)dec > declen ? dec - declen : 0; + size_t + topad = (size_t) + dec > declen ? dec - declen : 0; /* pad with '0's */ while (topad--) { @@ -1188,7 +1258,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de * separator every three digits */ while (s >= ZSTR_VAL(tmpbuf)) { *t-- = *s--; - if (thousand_sep && (++count%3)==0 && s >= ZSTR_VAL(tmpbuf)) { + if (thousand_sep && (++count % 3) == 0 && s >= ZSTR_VAL(tmpbuf)) { t -= thousand_sep_len; memcpy(t + 1, thousand_sep, thousand_sep_len); } @@ -1204,10 +1274,18 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de return res; } -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) +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) { - static const zend_ulong powers[] = { +static const zend_ulong powers[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, #if SIZEOF_ZEND_LONG == 8 @@ -1216,154 +1294,158 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, zend_long dec, c #elif SIZEOF_ZEND_LONG > 8 # error "Unknown SIZEOF_ZEND_LONG" #endif - }; - - int is_negative = 0; - zend_ulong tmpnum; - zend_ulong power; - zend_ulong power_half; - zend_ulong rest; - - zend_string *tmpbuf; - zend_string *res; - size_t reslen; - char *s, *t; /* source, target */ - int count = 0; - size_t topad; - - // unsigned absolute number and memorize negative sign - if (num < 0) { - is_negative = 1; - tmpnum = ((zend_ulong)-(num + 1)) + 1; - } else { - tmpnum = (zend_ulong)num; - } - - // rounding the number - if (dec < 0) { - // Check rounding to more negative places than possible - if (dec < -(sizeof(powers) / sizeof(powers[0]) - 1)) { - tmpnum = 0; - } else { - power = powers[-dec]; - power_half = power / 2; - rest = tmpnum % power; - tmpnum = tmpnum / power; +}; + +int is_negative = 0; +zend_ulong tmpnum; +zend_ulong power; +zend_ulong power_half; +zend_ulong rest; + +zend_string *tmpbuf; +zend_string *res; +size_t reslen; +char *s, *t; /* source, target */ +int count = 0; +size_t topad; + +// unsigned absolute number and memorize negative sign +if (num < 0) { +is_negative = 1; +tmpnum = ((zend_ulong) - (num + 1)) + 1; +} else { +tmpnum = (zend_ulong) num; +} - if (rest >= power_half) { - tmpnum = tmpnum * power + power; - } else { - tmpnum = tmpnum * power; - } - } +// rounding the number +if (dec < 0) { +// Check rounding to more negative places than possible +if (dec < -(sizeof(powers) / sizeof(powers[0]) - 1)) { +tmpnum = 0; +} else { +power = powers[-dec]; +power_half = power / 2; +rest = tmpnum % power; +tmpnum = tmpnum / power; + +if (rest >= power_half) { +tmpnum = tmpnum * power + power; +} else { +tmpnum = tmpnum * power; +} +} - // prevent resulting in negative zero - if (tmpnum == 0) { - is_negative = 0; - } - } +// prevent resulting in negative zero +if (tmpnum == 0) { +is_negative = 0; +} +} - tmpbuf = strpprintf(0, ZEND_ULONG_FMT, tmpnum); - reslen = ZSTR_LEN(tmpbuf); +tmpbuf = strpprintf(0, ZEND_ULONG_FMT, tmpnum); +reslen = ZSTR_LEN(tmpbuf); - /* allow for thousand separators */ - if (thousand_sep) { - reslen = zend_safe_addmult((reslen-1)/3, thousand_sep_len, reslen, "number formatting"); - } +/* allow for thousand separators */ +if (thousand_sep) { +reslen = zend_safe_addmult((reslen - 1) / 3, thousand_sep_len, reslen, "number formatting"); +} - reslen += is_negative; +reslen += +is_negative; - if (dec > 0) { - reslen += dec; +if (dec > 0) { +reslen += +dec; - if (dec_point) { - reslen = zend_safe_addmult(reslen, 1, dec_point_len, "number formatting"); - } - } +if (dec_point) { +reslen = zend_safe_addmult(reslen, 1, dec_point_len, "number formatting"); +} +} - res = zend_string_alloc(reslen, 0); +res = zend_string_alloc(reslen, 0); - s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1; - t = ZSTR_VAL(res) + reslen; - *t-- = '\0'; +s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1; +t = ZSTR_VAL(res) + reslen; +*t-- = '\0'; - /* copy the decimal places. */ - if (dec > 0) { - topad = (size_t)dec; +/* copy the decimal places. */ +if (dec > 0) { +topad = (size_t) +dec; - /* pad with '0's */ - while (topad--) { - *t-- = '0'; - } +/* pad with '0's */ +while (topad--) { +*t-- = '0'; +} - /* add decimal point */ - if (dec_point) { - t -= dec_point_len; - memcpy(t + 1, dec_point, dec_point_len); - } - } +/* add decimal point */ +if (dec_point) { +t -= +dec_point_len; +memcpy(t ++ 1, dec_point, dec_point_len); +} +} - /* copy the numbers before the decimal point, adding thousand - * separator every three digits */ - while (s >= ZSTR_VAL(tmpbuf)) { - *t-- = *s--; - if (thousand_sep && (++count % 3) == 0 && s >= ZSTR_VAL(tmpbuf)) { - t -= thousand_sep_len; - memcpy(t + 1, thousand_sep, thousand_sep_len); - } - } +/* copy the numbers before the decimal point, adding thousand + * separator every three digits */ +while (s >= +ZSTR_VAL(tmpbuf) +) { +*t-- = *s--; +if (thousand_sep && (++count % 3) == 0 && s >= +ZSTR_VAL(tmpbuf) +) { +t -= +thousand_sep_len; +memcpy(t ++ 1, thousand_sep, thousand_sep_len); +} +} - if (is_negative) { - *t-- = '-'; - } +if (is_negative) { +*t-- = '-'; +} - ZSTR_LEN(res) = reslen; - zend_string_release_ex(tmpbuf, 0); - return res; +ZSTR_LEN(res) = reslen; +zend_string_release_ex(tmpbuf, +0); +return +res; } /* {{{ Formats a number with grouped thousands */ PHP_FUNCTION(number_format) -{ - zval* num; - zend_long dec = 0; - int dec_int; - char *thousand_sep = NULL, *dec_point = NULL; - size_t thousand_sep_len = 0, dec_point_len = 0; + { + zval * num; + zend_long dec = 0; + int dec_int; + char *thousand_sep = NULL, *dec_point = NULL; + size_t thousand_sep_len = 0, dec_point_len = 0; - ZEND_PARSE_PARAMETERS_START(1, 4) + ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_NUMBER(num) Z_PARAM_OPTIONAL Z_PARAM_LONG(dec) Z_PARAM_STRING_OR_NULL(dec_point, dec_point_len) Z_PARAM_STRING_OR_NULL(thousand_sep, thousand_sep_len) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (dec_point == NULL) { - dec_point = "."; - dec_point_len = 1; - } - if (thousand_sep == NULL) { - thousand_sep = ","; - thousand_sep_len = 1; - } + if (dec_point == NULL) { + dec_point = "."; + dec_point_len = 1; + } + if (thousand_sep == NULL) { + thousand_sep = ","; + thousand_sep_len = 1; + } - switch (Z_TYPE_P(num)) { - case IS_LONG: - RETURN_STR(_php_math_number_format_long(Z_LVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len)); + switch (Z_TYPE_P(num)) { + case IS_LONG: + RETURN_STR(_php_math_number_format_long(Z_LVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, + thousand_sep_len)); 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 { @@ -1372,23 +1454,23 @@ PHP_FUNCTION(number_format) RETURN_STR(_php_math_number_format_ex(Z_DVAL_P(num), dec_int, dec_point, dec_point_len, thousand_sep, thousand_sep_len)); break; - EMPTY_SWITCH_DEFAULT_CASE() - } -} + EMPTY_SWITCH_DEFAULT_CASE() + } + } /* }}} */ /* {{{ Returns the remainder of dividing x by y as a float */ PHP_FUNCTION(fmod) -{ - double num1, num2; + { + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(fmod(num1, num2)); -} + RETURN_DOUBLE(fmod(num1, num2)); + } /* }}} */ /* {{{ Perform floating-point division of dividend / divisor @@ -1397,38 +1479,38 @@ PHP_FUNCTION(fmod) __attribute__((no_sanitize("float-divide-by-zero"))) #endif PHP_FUNCTION(fdiv) -{ - double dividend, divisor; + { + double dividend, divisor; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(dividend) - Z_PARAM_DOUBLE(divisor) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(dividend) + Z_PARAM_DOUBLE(divisor) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(dividend / divisor); -} + RETURN_DOUBLE(dividend / divisor); + } /* }}} */ /* {{{ Returns the integer quotient of the division of dividend by divisor */ PHP_FUNCTION(intdiv) -{ - zend_long dividend, divisor; + { + zend_long dividend, divisor; - ZEND_PARSE_PARAMETERS_START(2, 2) + ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(dividend) Z_PARAM_LONG(divisor) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (divisor == 0) { - zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero"); - RETURN_THROWS(); - } else if (divisor == -1 && dividend == ZEND_LONG_MIN) { - /* Prevent overflow error/crash ... really should not happen: - We don't return a float here as that violates function contract */ - zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Division of PHP_INT_MIN by -1 is not an integer"); - RETURN_THROWS(); - } + if (divisor == 0) { + zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero"); + RETURN_THROWS(); + } else if (divisor == -1 && dividend == ZEND_LONG_MIN) { + /* Prevent overflow error/crash ... really should not happen: + We don't return a float here as that violates function contract */ + zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Division of PHP_INT_MIN by -1 is not an integer"); + RETURN_THROWS(); + } - RETURN_LONG(dividend / divisor); -} + RETURN_LONG(dividend / divisor); + } /* }}} */ From 8b774e79ba3b0098f22a4644ad8e70abb9197a80 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 15 Nov 2023 22:20:22 +0100 Subject: [PATCH 12/25] Revert logic of fabs in rounding function --- ext/standard/math.c | 128 ++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 65 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 48fe3f67d0840..b7314be640bd0 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -36,7 +36,7 @@ static inline int php_intlog10abs(double value) { value = fabs(value); if (value < 1e-8 || value > 1e22) { - return (int)floor(log10(value)); + return (int) floor(log10(value)); } else { /* Do a binary search with 5 steps */ int result = 15; @@ -79,7 +79,7 @@ static inline int php_intlog10abs(double value) { static inline double php_intpow10(int power) { /* Not in lookup table */ if (power < 0 || power > 22) { - return pow(10.0, (double)power); + return pow(10.0, (double) power); } static const double powers[] = { @@ -101,11 +101,11 @@ static inline double php_round_helper(double value, int mode) { * in branches in the assembly) to make the following cases simpler. */ double integral; - double fractional = modf(value, &integral); + double fractional = fabs(modf(value, &integral)); switch (mode) { case PHP_ROUND_HALF_UP: - if (fabs(fractional) >= 0.5) { + if (fractional >= 0.5) { /* We must increase the magnitude of the integral part * (rounding up / towards infinity). copysign(1.0, integral) * will either result in 1.0 or -1.0 depending on the sign @@ -120,7 +120,7 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_DOWN: - if (fabs(fractional) > 0.5) { + if (fractional > 0.5) { return integral + copysign(1.0, integral); } @@ -145,7 +145,6 @@ static inline double php_round_helper(double value, int mode) { return fractional == 0 ? integral : integral + copysign(1.0, integral); case PHP_ROUND_HALF_EVEN: - fractional = fabs(fractional); if (fractional > 0.5) { return integral + copysign(1.0, integral); } @@ -164,7 +163,6 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_HALF_ODD: - fractional = fabs(fractional); if (fractional > 0.5) { return integral + copysign(1.0, integral); } @@ -179,7 +177,7 @@ static inline double php_round_helper(double value, int mode) { return integral; - EMPTY_SWITCH_DEFAULT_CASE(); + EMPTY_SWITCH_DEFAULT_CASE(); } // FIXME: GCC bug, branch is considered reachable. ZEND_UNREACHABLE(); @@ -200,7 +198,7 @@ PHPAPI double _php_math_round(double value, int places, int mode) { return value; } - places = places < INT_MIN+1 ? INT_MIN+1 : places; + places = places < INT_MIN + 1 ? INT_MIN + 1 : places; precision_places = 14 - php_intlog10abs(value); f1 = php_intpow10(abs(places)); @@ -209,9 +207,9 @@ PHPAPI double _php_math_round(double value, int places, int mode) { the requested places BUT is small enough to make sure a non-zero value is returned, pre-round the result to the precision */ if (precision_places > places && precision_places - 15 < places) { - int64_t use_precision = precision_places < INT_MIN+1 ? INT_MIN+1 : precision_places; + int64_t use_precision = precision_places < INT_MIN + 1 ? INT_MIN + 1 : precision_places; - f2 = php_intpow10(abs((int)use_precision)); + f2 = php_intpow10(abs((int) use_precision)); if (use_precision >= 0) { tmp_value = value * f2; } else { @@ -222,9 +220,9 @@ PHPAPI double _php_math_round(double value, int places, int mode) { tmp_value = php_round_helper(tmp_value, mode); use_precision = places - precision_places; - use_precision = use_precision < INT_MIN+1 ? INT_MIN+1 : use_precision; + use_precision = use_precision < INT_MIN + 1 ? INT_MIN + 1 : use_precision; /* now correctly move the decimal point */ - f2 = php_intpow10(abs((int)use_precision)); + f2 = php_intpow10(abs((int) use_precision)); /* because places < precision_places */ tmp_value = tmp_value / f2; } else { @@ -275,25 +273,25 @@ PHPAPI double _php_math_round(double value, int places, int mode) { /* {{{ Return the absolute value of the number */ PHP_FUNCTION(abs) -{ - zval *value; + { + zval * value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { - RETURN_DOUBLE(-(double)ZEND_LONG_MIN); - } else { - RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); - } - case IS_DOUBLE: - RETURN_DOUBLE(fabs(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } -} + switch (Z_TYPE_P(value)) { + case IS_LONG: + if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { + RETURN_DOUBLE(-(double) ZEND_LONG_MIN); + } else { + RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); + } + case IS_DOUBLE: + RETURN_DOUBLE(fabs(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } + } /* }}} */ /* {{{ Returns the next highest integer value of the number */ @@ -301,53 +299,53 @@ PHP_FUNCTION(ceil) { zval * value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(ceil(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } -} + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(ceil(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } + } /* }}} */ /* {{{ Returns the next lowest integer value from the number */ PHP_FUNCTION(floor) -{ - zval *value; + { + zval * value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(floor(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } -} + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(floor(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } + } /* }}} */ /* {{{ Returns the number rounded to specified precision */ PHP_FUNCTION(round) -{ - zval *value; - int places = 0; - zend_long precision = 0; - zend_long mode = PHP_ROUND_HALF_UP; + { + zval * value; + int places = 0; + zend_long precision = 0; + zend_long mode = PHP_ROUND_HALF_UP; - ZEND_PARSE_PARAMETERS_START(1, 3) + ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_NUMBER(value) Z_PARAM_OPTIONAL Z_PARAM_LONG(precision) Z_PARAM_LONG(mode) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); if (ZEND_NUM_ARGS() >= 2) { if (precision >= 0) { @@ -370,14 +368,14 @@ PHP_FUNCTION(round) default: zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); RETURN_THROWS(); - } + } - 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)); - } + 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)); + } ZEND_FALLTHROUGH; case IS_DOUBLE: From fd1ee38fdea7a1d6750d91fb1ace404f25deabb1 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 15 Nov 2023 22:42:39 +0100 Subject: [PATCH 13/25] Revert formatting of the math.c --- ext/standard/math.c | 1295 ++++++++++++++++++++----------------------- 1 file changed, 602 insertions(+), 693 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index b7314be640bd0..85c100b5c3eae 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -176,8 +176,7 @@ static inline double php_round_helper(double value, int mode) { } return integral; - - EMPTY_SWITCH_DEFAULT_CASE(); + EMPTY_SWITCH_DEFAULT_CASE(); } // FIXME: GCC bug, branch is considered reachable. ZEND_UNREACHABLE(); @@ -273,527 +272,516 @@ PHPAPI double _php_math_round(double value, int places, int mode) { /* {{{ Return the absolute value of the number */ PHP_FUNCTION(abs) - { - zval * value; +{ + zval *value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); - - switch (Z_TYPE_P(value)) { - case IS_LONG: - if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { - RETURN_DOUBLE(-(double) ZEND_LONG_MIN); - } else { - RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); - } - case IS_DOUBLE: - RETURN_DOUBLE(fabs(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } - } + ZEND_PARSE_PARAMETERS_END(); + + switch (Z_TYPE_P(value)) { + case IS_LONG: + if (UNEXPECTED(Z_LVAL_P(value) == ZEND_LONG_MIN)) { + RETURN_DOUBLE(-(double)ZEND_LONG_MIN); + } else { + RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value)); + } + case IS_DOUBLE: + RETURN_DOUBLE(fabs(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } +} /* }}} */ /* {{{ Returns the next highest integer value of the number */ PHP_FUNCTION(ceil) - { - zval * value; +{ + zval *value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(ceil(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } - } + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(ceil(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } +} /* }}} */ /* {{{ Returns the next lowest integer value from the number */ PHP_FUNCTION(floor) - { - zval * value; +{ + zval *value; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_NUMBER(value) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - switch (Z_TYPE_P(value)) { - case IS_LONG: - RETURN_DOUBLE(zval_get_double(value)); - case IS_DOUBLE: - RETURN_DOUBLE(floor(Z_DVAL_P(value))); - EMPTY_SWITCH_DEFAULT_CASE(); - } - } + switch (Z_TYPE_P(value)) { + case IS_LONG: + RETURN_DOUBLE(zval_get_double(value)); + case IS_DOUBLE: + RETURN_DOUBLE(floor(Z_DVAL_P(value))); + EMPTY_SWITCH_DEFAULT_CASE(); + } +} /* }}} */ /* {{{ Returns the number rounded to specified precision */ PHP_FUNCTION(round) - { - zval * value; - int places = 0; - zend_long precision = 0; - zend_long mode = PHP_ROUND_HALF_UP; +{ + zval *value; + int places = 0; + zend_long precision = 0; + zend_long mode = PHP_ROUND_HALF_UP; - ZEND_PARSE_PARAMETERS_START(1, 3) + ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_NUMBER(value) Z_PARAM_OPTIONAL Z_PARAM_LONG(precision) Z_PARAM_LONG(mode) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() >= 2) { - if (precision >= 0) { - places = ZEND_LONG_INT_OVFL(precision) ? INT_MAX : (int) precision; - } else { - places = ZEND_LONG_INT_UDFL(precision) ? INT_MIN : (int) precision; - } + if (ZEND_NUM_ARGS() >= 2) { + if (precision >= 0) { + places = ZEND_LONG_INT_OVFL(precision) ? INT_MAX : (int)precision; + } else { + places = ZEND_LONG_INT_UDFL(precision) ? INT_MIN : (int)precision; } + } - switch (mode) { - case PHP_ROUND_HALF_UP: - case PHP_ROUND_HALF_DOWN: - case PHP_ROUND_HALF_EVEN: - case PHP_ROUND_HALF_ODD: + switch (mode) { + case PHP_ROUND_HALF_UP: + case PHP_ROUND_HALF_DOWN: + case PHP_ROUND_HALF_EVEN: + case PHP_ROUND_HALF_ODD: case PHP_ROUND_AWAY_FROM_ZERO: case PHP_ROUND_TOWARD_ZERO: case PHP_ROUND_CEILING: case PHP_ROUND_FLOOR: - break; - default: - zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); + break; + default: + zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); RETURN_THROWS(); - } + } - 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)); - } + 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)); + } ZEND_FALLTHROUGH; - case IS_DOUBLE: - RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int) places, (int) mode)); + case IS_DOUBLE: + RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int)places, (int)mode)); - EMPTY_SWITCH_DEFAULT_CASE(); - } - } + EMPTY_SWITCH_DEFAULT_CASE(); + } +} /* }}} */ /* {{{ Returns the sine of the number in radians */ PHP_FUNCTION(sin) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sin(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(sin(num)); +} /* }}} */ /* {{{ Returns the cosine of the number in radians */ PHP_FUNCTION(cos) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(cos(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(cos(num)); +} /* }}} */ /* {{{ Returns the tangent of the number in radians */ PHP_FUNCTION(tan) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(tan(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(tan(num)); +} /* }}} */ /* {{{ Returns the arc sine of the number in radians */ PHP_FUNCTION(asin) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(asin(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(asin(num)); +} /* }}} */ /* {{{ Return the arc cosine of the number in radians */ PHP_FUNCTION(acos) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(acos(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(acos(num)); +} /* }}} */ /* {{{ Returns the arc tangent of the number in radians */ PHP_FUNCTION(atan) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atan(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atan(num)); +} /* }}} */ /* {{{ Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */ PHP_FUNCTION(atan2) - { - double num1, num2; +{ + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atan2(num1, num2)); - } + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atan2(num1, num2)); +} /* }}} */ /* {{{ Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */ PHP_FUNCTION(sinh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sinh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(sinh(num)); +} /* }}} */ /* {{{ Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */ PHP_FUNCTION(cosh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(cosh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(cosh(num)); +} /* }}} */ /* {{{ Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */ PHP_FUNCTION(tanh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(tanh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(tanh(num)); +} /* }}} */ /* {{{ Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number */ PHP_FUNCTION(asinh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(asinh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(asinh(num)); +} /* }}} */ /* {{{ Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number */ PHP_FUNCTION(acosh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(acosh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(acosh(num)); +} /* }}} */ /* {{{ Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number */ PHP_FUNCTION(atanh) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(atanh(num)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE(atanh(num)); +} /* }}} */ /* {{{ Returns an approximation of pi */ PHP_FUNCTION(pi) - { - ZEND_PARSE_PARAMETERS_NONE(); +{ + ZEND_PARSE_PARAMETERS_NONE(); - RETURN_DOUBLE(M_PI); - } + RETURN_DOUBLE(M_PI); +} /* }}} */ /* {{{ Returns whether argument is finite */ PHP_FUNCTION(is_finite) - { - double dval; +{ + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_finite(dval)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_finite(dval)); +} /* }}} */ /* {{{ Returns whether argument is infinite */ PHP_FUNCTION(is_infinite) - { - double dval; +{ + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_isinf(dval)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_isinf(dval)); +} /* }}} */ /* {{{ Returns whether argument is not a number */ PHP_FUNCTION(is_nan) - { - double dval; +{ + double dval; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(dval) - ZEND_PARSE_PARAMETERS_END(); - RETURN_BOOL(zend_isnan(dval)); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(dval) + ZEND_PARSE_PARAMETERS_END(); + RETURN_BOOL(zend_isnan(dval)); +} /* }}} */ /* {{{ Returns base raised to the power of exponent. Returns integer result when possible */ PHP_FUNCTION(pow) - { - zval * zbase, *zexp; +{ + zval *zbase, *zexp; - ZEND_PARSE_PARAMETERS_START(2, 2) + ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ZVAL(zbase) Z_PARAM_ZVAL(zexp) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - pow_function(return_value, zbase, zexp); - } + pow_function(return_value, zbase, zexp); +} /* }}} */ /* {{{ Returns e raised to the power of the number */ PHP_FUNCTION(exp) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(exp(num)); - } + RETURN_DOUBLE(exp(num)); +} /* }}} */ /* {{{ Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero */ PHP_FUNCTION(expm1) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(expm1(num)); - } + RETURN_DOUBLE(expm1(num)); +} /* }}} */ /* {{{ Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero */ PHP_FUNCTION(log1p) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(log1p(num)); - } + RETURN_DOUBLE(log1p(num)); +} /* }}} */ /* {{{ Returns the natural logarithm of the number, or the base log if base is specified */ PHP_FUNCTION(log) - { - double num, base = 0; +{ + double num, base = 0; - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_DOUBLE(num) - Z_PARAM_OPTIONAL - Z_PARAM_DOUBLE(base) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_DOUBLE(num) + Z_PARAM_OPTIONAL + Z_PARAM_DOUBLE(base) + ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 1) { - RETURN_DOUBLE(log(num)); - } + if (ZEND_NUM_ARGS() == 1) { + RETURN_DOUBLE(log(num)); + } - if (base == 2.0) { - RETURN_DOUBLE(log2(num)); - } + if (base == 2.0) { + RETURN_DOUBLE(log2(num)); + } - if (base == 10.0) { - RETURN_DOUBLE(log10(num)); - } + if (base == 10.0) { + RETURN_DOUBLE(log10(num)); + } - if (base == 1.0) { - RETURN_DOUBLE(ZEND_NAN); - } + if (base == 1.0) { + RETURN_DOUBLE(ZEND_NAN); + } - if (base <= 0.0) { - zend_argument_value_error(2, "must be greater than 0"); - RETURN_THROWS(); - } + if (base <= 0.0) { + zend_argument_value_error(2, "must be greater than 0"); + RETURN_THROWS(); + } - RETURN_DOUBLE(log(num) / log(base)); - } + RETURN_DOUBLE(log(num) / log(base)); +} /* }}} */ /* {{{ Returns the base-10 logarithm of the number */ PHP_FUNCTION(log10) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(log10(num)); - } + RETURN_DOUBLE(log10(num)); +} /* }}} */ /* {{{ Returns the square root of the number */ PHP_FUNCTION(sqrt) - { - double num; +{ + double num; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(num) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(num) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(sqrt(num)); - } + RETURN_DOUBLE(sqrt(num)); +} /* }}} */ /* {{{ Returns sqrt(num1*num1 + num2*num2) */ PHP_FUNCTION(hypot) - { - double num1, num2; +{ + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(hypot(num1, num2)); - } + RETURN_DOUBLE(hypot(num1, num2)); +} /* }}} */ /* {{{ Converts the number in degrees to the radian equivalent */ PHP_FUNCTION(deg2rad) - { - double deg; +{ + double deg; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(deg) - ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE((deg / 180.0) * M_PI); - } + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(deg) + ZEND_PARSE_PARAMETERS_END(); + RETURN_DOUBLE((deg / 180.0) * M_PI); +} /* }}} */ /* {{{ Converts the radian number to the equivalent number in degrees */ PHP_FUNCTION(rad2deg) - { - double rad; +{ + double rad; - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_DOUBLE(rad) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_DOUBLE(rad) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE((rad / M_PI) * 180); - } + RETURN_DOUBLE((rad / M_PI) * 180); +} /* }}} */ /* {{{ _php_math_basetolong */ /* * Convert a string representation of a base(2-36) number to a long. */ -PHPAPI zend_long -_php_math_basetolong(zval -*arg, -int base -) +PHPAPI zend_long _php_math_basetolong(zval *arg, int base) { -zend_long num = 0, digit, onum; -zend_long i; -char c, *s; - -if ( -Z_TYPE_P(arg) -!= IS_STRING || base < 2 || base > 36) { -return 0; -} + zend_long num = 0, digit, onum; + zend_long i; + char c, *s; -s = Z_STRVAL_P(arg); + if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) { + return 0; + } -for ( -i = Z_STRLEN_P(arg); -i > 0; i--) { -c = *s++; + s = Z_STRVAL_P(arg); -digit = (c >= '0' && c <= '9') ? c - '0' - : (c >= 'A' && c <= 'Z') ? c - 'A' + 10 - : (c >= 'a' && c <= 'z') ? c - 'a' + 10 - : base; + for (i = Z_STRLEN_P(arg); i > 0; i--) { + c = *s++; -if (digit >= base) { -continue; -} + digit = (c >= '0' && c <= '9') ? c - '0' + : (c >= 'A' && c <= 'Z') ? c - 'A' + 10 + : (c >= 'a' && c <= 'z') ? c - 'a' + 10 + : base; -onum = num; -num = num * base + digit; -if (num > onum) -continue; + if (digit >= base) { + continue; + } -{ + onum = num; + num = num * base + digit; + if (num > onum) + continue; -php_error_docref(NULL, E_WARNING, -"Number %s is too big to fit in long", s); -return -ZEND_LONG_MAX; -} -} + { + + php_error_docref(NULL, E_WARNING, "Number %s is too big to fit in long", s); + return ZEND_LONG_MAX; + } + } -return -num; + return num; } /* }}} */ @@ -801,7 +789,8 @@ num; /* * Convert a string representation of a base(2-36) number to a zval. */ -PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) { +PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) +{ zend_long num = 0; double fnum = 0; int mode = 0; @@ -816,7 +805,7 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) { /* Skip leading whitespace */ while (s < e && isspace(*s)) s++; /* Skip trailing whitespace */ - while (s < e && isspace(*(e - 1))) e--; + while (s < e && isspace(*(e-1))) e--; if (e - s >= 2) { if (base == 16 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2; @@ -848,17 +837,17 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) { } switch (mode) { - case 0: /* Integer */ - if (num < cutoff || (num == cutoff && c <= cutlim)) { - num = num * base + c; - break; - } else { - fnum = (double) num; - mode = 1; - } - ZEND_FALLTHROUGH; - case 1: /* Float */ - fnum = fnum * base + c; + case 0: /* Integer */ + if (num < cutoff || (num == cutoff && c <= cutlim)) { + num = num * base + c; + break; + } else { + fnum = (double)num; + mode = 1; + } + ZEND_FALLTHROUGH; + case 1: /* Float */ + fnum = fnum * base + c; } } @@ -879,43 +868,29 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret) { * Convert a long to a string containing a base(2-36) representation of * the number. */ -PHPAPI zend_string -* -_php_math_longtobase(zend_long -arg, -int base -) +PHPAPI zend_string * _php_math_longtobase(zend_long arg, int base) { -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -char buf[(sizeof(zend_ulong) << 3) + 1]; -char *ptr, *end; -zend_ulong value; - -if (base < 2 || base > 36) { -return + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + char buf[(sizeof(zend_ulong) << 3) + 1]; + char *ptr, *end; + zend_ulong value; -ZSTR_EMPTY_ALLOC(); - -} + if (base < 2 || base > 36) { + return ZSTR_EMPTY_ALLOC(); + } -value = arg; + value = arg; -end = ptr = buf + sizeof(buf) - 1; -* -ptr = '\0'; + end = ptr = buf + sizeof(buf) - 1; + *ptr = '\0'; -do { -ZEND_ASSERT(ptr -> buf); -*-- -ptr = digits[value % base]; -value /= -base; -} while (value); + do { + ZEND_ASSERT(ptr > buf); + *--ptr = digits[value % base]; + value /= base; + } while (value); -return -zend_string_init(ptr, end -- ptr, 0); + return zend_string_init(ptr, end - ptr, 0); } /* }}} */ @@ -924,45 +899,33 @@ zend_string_init(ptr, end * Convert a long to a string containing a base(2,4,6,16,32) representation of * the number. */ -static zend_always_inline zend_string -* -_php_math_longtobase_pwr2(zend_long -arg, -int base_log2 -) +static zend_always_inline zend_string * _php_math_longtobase_pwr2(zend_long arg, int base_log2) { -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -zend_ulong value; -size_t len; -zend_string *ret; -char *ptr; - -value = arg; - -if (value == 0) { -len = 1; -} else { -len = ((sizeof(value) * 8 - zend_ulong_nlz(value)) + (base_log2 - 1)) / base_log2; -} + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + zend_ulong value; + size_t len; + zend_string *ret; + char *ptr; + + value = arg; + + if (value == 0) { + len = 1; + } else { + len = ((sizeof(value) * 8 - zend_ulong_nlz(value)) + (base_log2 - 1)) / base_log2; + } -ret = zend_string_alloc(len, 0); -ptr = ZSTR_VAL(ret) + len; -* -ptr = '\0'; - -do { -ZEND_ASSERT(ptr -> -ZSTR_VAL(ret) -); -*-- -ptr = digits[value & ((1 << base_log2) - 1)]; -value >>= -base_log2; -} while (value); - -return -ret; + ret = zend_string_alloc(len, 0); + ptr = ZSTR_VAL(ret) + len; + *ptr = '\0'; + + do { + ZEND_ASSERT(ptr > ZSTR_VAL(ret)); + *--ptr = digits[value & ((1 << base_log2) - 1)]; + value >>= base_log2; + } while (value); + + return ret; } /* }}} */ @@ -971,192 +934,162 @@ ret; * Convert a zval to a string containing a base(2-36) representation of * the number. */ -PHPAPI zend_string -* -_php_math_zvaltobase(zval -*arg, -int base -) +PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base) { -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - -if (( -Z_TYPE_P(arg) -!= IS_LONG && -Z_TYPE_P(arg) -!= IS_DOUBLE) || base < 2 || base > 36) { -return + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -ZSTR_EMPTY_ALLOC(); + if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) { + return ZSTR_EMPTY_ALLOC(); + } -} + if (Z_TYPE_P(arg) == IS_DOUBLE) { + double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */ + char *ptr, *end; + char buf[(sizeof(double) << 3) + 1]; -if ( -Z_TYPE_P(arg) -== IS_DOUBLE) { -double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */ -char *ptr, *end; -char buf[(sizeof(double) << 3) + 1]; - -/* Don't try to convert +/- infinity */ -if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) { -zend_value_error("An infinite value cannot be converted to base %d", base); -return -NULL; -} + /* Don't try to convert +/- infinity */ + if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) { + zend_value_error("An infinite value cannot be converted to base %d", base); + return NULL; + } -end = ptr = buf + sizeof(buf) - 1; -* -ptr = '\0'; - -do { -*-- -ptr = digits[(int) fmod(fvalue, base)]; -fvalue /= -base; -} while (ptr > buf && -fabs(fvalue) ->= 1); - -return -zend_string_init(ptr, end -- ptr, 0); -} + end = ptr = buf + sizeof(buf) - 1; + *ptr = '\0'; -return + do { + *--ptr = digits[(int) fmod(fvalue, base)]; + fvalue /= base; + } while (ptr > buf && fabs(fvalue) >= 1); -_php_math_longtobase(Z_LVAL_P(arg), base + return zend_string_init(ptr, end - ptr, 0); + } -); + return _php_math_longtobase(Z_LVAL_P(arg), base); } /* }}} */ /* {{{ Returns the decimal equivalent of the binary number */ PHP_FUNCTION(bindec) - { - zend_string * arg; +{ + zend_string *arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 2, return_value); - } + _php_math_basetozval(arg, 2, return_value); +} /* }}} */ /* {{{ Returns the decimal equivalent of the hexadecimal number */ PHP_FUNCTION(hexdec) - { - zend_string * arg; +{ + zend_string *arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 16, return_value); - } + _php_math_basetozval(arg, 16, return_value); +} /* }}} */ /* {{{ Returns the decimal equivalent of an octal string */ PHP_FUNCTION(octdec) - { - zend_string * arg; +{ + zend_string *arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_STR(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - _php_math_basetozval(arg, 8, return_value); - } + _php_math_basetozval(arg, 8, return_value); +} /* }}} */ /* {{{ Returns a string containing a binary representation of the number */ PHP_FUNCTION(decbin) - { - zend_long arg; +{ + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 1)); - } + RETURN_STR(_php_math_longtobase_pwr2(arg, 1)); +} /* }}} */ /* {{{ Returns a string containing an octal representation of the given number */ PHP_FUNCTION(decoct) - { - zend_long arg; +{ + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 3)); - } + RETURN_STR(_php_math_longtobase_pwr2(arg, 3)); +} /* }}} */ /* {{{ Returns a string containing a hexadecimal representation of the given number */ PHP_FUNCTION(dechex) - { - zend_long arg; +{ + zend_long arg; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(arg) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - RETURN_STR(_php_math_longtobase_pwr2(arg, 4)); - } + RETURN_STR(_php_math_longtobase_pwr2(arg, 4)); +} /* }}} */ /* {{{ Converts a number in a string from any base <= 36 to any base <= 36 */ PHP_FUNCTION(base_convert) - { - zval temp; - zend_string *number; - zend_long frombase, tobase; - zend_string *result; +{ + zval temp; + zend_string *number; + zend_long frombase, tobase; + zend_string *result; - ZEND_PARSE_PARAMETERS_START(3, 3) + ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_STR(number) Z_PARAM_LONG(frombase) Z_PARAM_LONG(tobase) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (frombase < 2 || frombase > 36) { - zend_argument_value_error(2, "must be between 2 and 36 (inclusive)"); - RETURN_THROWS(); - } - if (tobase < 2 || tobase > 36) { - zend_argument_value_error(3, "must be between 2 and 36 (inclusive)"); - RETURN_THROWS(); - } + if (frombase < 2 || frombase > 36) { + zend_argument_value_error(2, "must be between 2 and 36 (inclusive)"); + RETURN_THROWS(); + } + if (tobase < 2 || tobase > 36) { + zend_argument_value_error(3, "must be between 2 and 36 (inclusive)"); + RETURN_THROWS(); + } - _php_math_basetozval(number, (int)frombase, &temp); - result = _php_math_zvaltobase(&temp, (int)tobase); - if (!result) { - RETURN_THROWS(); - } + _php_math_basetozval(number, (int)frombase, &temp); + result = _php_math_zvaltobase(&temp, (int)tobase); + if (!result) { + RETURN_THROWS(); + } - RETVAL_STR(result); - } + RETVAL_STR(result); +} /* }}} */ /* {{{ _php_math_number_format */ -PHPAPI zend_string -* - -_php_math_number_format(double d, int dec, char dec_point, char thousand_sep) { +PHPAPI zend_string *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep) +{ return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1); } -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) { - zend_string * res; - zend_string * tmpbuf; +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) +{ + zend_string *res; + zend_string *tmpbuf; char *s, *t; /* source, target */ char *dp; size_t integral; @@ -1174,7 +1107,7 @@ _php_math_number_format_ex(double d, int dec, const char *dec_point, tmpbuf = strpprintf(0, "%.*F", dec, d); if (tmpbuf == NULL) { return NULL; - } else if (!isdigit((int) ZSTR_VAL(tmpbuf)[0])) { + } else if (!isdigit((int)ZSTR_VAL(tmpbuf)[0])) { return tmpbuf; } @@ -1200,7 +1133,7 @@ _php_math_number_format_ex(double d, int dec, const char *dec_point, /* allow for thousand separators */ if (thousand_sep) { - integral = zend_safe_addmult((integral - 1) / 3, thousand_sep_len, integral, "number formatting"); + integral = zend_safe_addmult((integral-1)/3, thousand_sep_len, integral, "number formatting"); } reslen = integral; @@ -1228,9 +1161,7 @@ _php_math_number_format_ex(double d, int dec, const char *dec_point, * we requested due to internal buffer limitations */ if (dec) { size_t declen = (dp ? s - dp : 0); - size_t - topad = (size_t) - dec > declen ? dec - declen : 0; + size_t topad = (size_t)dec > declen ? dec - declen : 0; /* pad with '0's */ while (topad--) { @@ -1256,7 +1187,7 @@ _php_math_number_format_ex(double d, int dec, const char *dec_point, * separator every three digits */ while (s >= ZSTR_VAL(tmpbuf)) { *t-- = *s--; - if (thousand_sep && (++count % 3) == 0 && s >= ZSTR_VAL(tmpbuf)) { + if (thousand_sep && (++count%3)==0 && s >= ZSTR_VAL(tmpbuf)) { t -= thousand_sep_len; memcpy(t + 1, thousand_sep, thousand_sep_len); } @@ -1272,18 +1203,10 @@ _php_math_number_format_ex(double d, int dec, const char *dec_point, return res; } -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) +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) { -static const zend_ulong powers[] = { + static const zend_ulong powers[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, #if SIZEOF_ZEND_LONG == 8 @@ -1292,155 +1215,141 @@ static const zend_ulong powers[] = { #elif SIZEOF_ZEND_LONG > 8 # error "Unknown SIZEOF_ZEND_LONG" #endif -}; - -int is_negative = 0; -zend_ulong tmpnum; -zend_ulong power; -zend_ulong power_half; -zend_ulong rest; - -zend_string *tmpbuf; -zend_string *res; -size_t reslen; -char *s, *t; /* source, target */ -int count = 0; -size_t topad; - -// unsigned absolute number and memorize negative sign -if (num < 0) { -is_negative = 1; -tmpnum = ((zend_ulong) - (num + 1)) + 1; -} else { -tmpnum = (zend_ulong) num; -} + }; -// rounding the number -if (dec < 0) { -// Check rounding to more negative places than possible -if (dec < -(sizeof(powers) / sizeof(powers[0]) - 1)) { -tmpnum = 0; -} else { -power = powers[-dec]; -power_half = power / 2; -rest = tmpnum % power; -tmpnum = tmpnum / power; - -if (rest >= power_half) { -tmpnum = tmpnum * power + power; -} else { -tmpnum = tmpnum * power; -} -} + int is_negative = 0; + zend_ulong tmpnum; + zend_ulong power; + zend_ulong power_half; + zend_ulong rest; + + zend_string *tmpbuf; + zend_string *res; + size_t reslen; + char *s, *t; /* source, target */ + int count = 0; + size_t topad; -// prevent resulting in negative zero -if (tmpnum == 0) { -is_negative = 0; -} -} + // unsigned absolute number and memorize negative sign + if (num < 0) { + is_negative = 1; + tmpnum = ((zend_ulong)-(num + 1)) + 1; + } else { + tmpnum = (zend_ulong)num; + } -tmpbuf = strpprintf(0, ZEND_ULONG_FMT, tmpnum); -reslen = ZSTR_LEN(tmpbuf); + // rounding the number + if (dec < 0) { + // Check rounding to more negative places than possible + if (dec < -(sizeof(powers) / sizeof(powers[0]) - 1)) { + tmpnum = 0; + } else { + power = powers[-dec]; + power_half = power / 2; + rest = tmpnum % power; + tmpnum = tmpnum / power; -/* allow for thousand separators */ -if (thousand_sep) { -reslen = zend_safe_addmult((reslen - 1) / 3, thousand_sep_len, reslen, "number formatting"); -} + if (rest >= power_half) { + tmpnum = tmpnum * power + power; + } else { + tmpnum = tmpnum * power; + } + } -reslen += -is_negative; + // prevent resulting in negative zero + if (tmpnum == 0) { + is_negative = 0; + } + } -if (dec > 0) { -reslen += -dec; + tmpbuf = strpprintf(0, ZEND_ULONG_FMT, tmpnum); + reslen = ZSTR_LEN(tmpbuf); -if (dec_point) { -reslen = zend_safe_addmult(reslen, 1, dec_point_len, "number formatting"); -} -} + /* allow for thousand separators */ + if (thousand_sep) { + reslen = zend_safe_addmult((reslen-1)/3, thousand_sep_len, reslen, "number formatting"); + } -res = zend_string_alloc(reslen, 0); + reslen += is_negative; -s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1; -t = ZSTR_VAL(res) + reslen; -*t-- = '\0'; + if (dec > 0) { + reslen += dec; -/* copy the decimal places. */ -if (dec > 0) { -topad = (size_t) -dec; + if (dec_point) { + reslen = zend_safe_addmult(reslen, 1, dec_point_len, "number formatting"); + } + } -/* pad with '0's */ -while (topad--) { -*t-- = '0'; -} + res = zend_string_alloc(reslen, 0); -/* add decimal point */ -if (dec_point) { -t -= -dec_point_len; -memcpy(t -+ 1, dec_point, dec_point_len); -} -} + s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1; + t = ZSTR_VAL(res) + reslen; + *t-- = '\0'; -/* copy the numbers before the decimal point, adding thousand - * separator every three digits */ -while (s >= -ZSTR_VAL(tmpbuf) -) { -*t-- = *s--; -if (thousand_sep && (++count % 3) == 0 && s >= -ZSTR_VAL(tmpbuf) -) { -t -= -thousand_sep_len; -memcpy(t -+ 1, thousand_sep, thousand_sep_len); -} -} + /* copy the decimal places. */ + if (dec > 0) { + topad = (size_t)dec; -if (is_negative) { -*t-- = '-'; -} + /* pad with '0's */ + while (topad--) { + *t-- = '0'; + } + + /* add decimal point */ + if (dec_point) { + t -= dec_point_len; + memcpy(t + 1, dec_point, dec_point_len); + } + } + + /* copy the numbers before the decimal point, adding thousand + * separator every three digits */ + while (s >= ZSTR_VAL(tmpbuf)) { + *t-- = *s--; + if (thousand_sep && (++count % 3) == 0 && s >= ZSTR_VAL(tmpbuf)) { + t -= thousand_sep_len; + memcpy(t + 1, thousand_sep, thousand_sep_len); + } + } + + if (is_negative) { + *t-- = '-'; + } -ZSTR_LEN(res) = reslen; -zend_string_release_ex(tmpbuf, -0); -return -res; + ZSTR_LEN(res) = reslen; + zend_string_release_ex(tmpbuf, 0); + return res; } /* {{{ Formats a number with grouped thousands */ PHP_FUNCTION(number_format) - { - zval * num; - zend_long dec = 0; - int dec_int; - char *thousand_sep = NULL, *dec_point = NULL; - size_t thousand_sep_len = 0, dec_point_len = 0; +{ + zval* num; + zend_long dec = 0; + int dec_int; + char *thousand_sep = NULL, *dec_point = NULL; + size_t thousand_sep_len = 0, dec_point_len = 0; - ZEND_PARSE_PARAMETERS_START(1, 4) + ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_NUMBER(num) Z_PARAM_OPTIONAL Z_PARAM_LONG(dec) Z_PARAM_STRING_OR_NULL(dec_point, dec_point_len) Z_PARAM_STRING_OR_NULL(thousand_sep, thousand_sep_len) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_END(); - if (dec_point == NULL) { - dec_point = "."; - dec_point_len = 1; - } - if (thousand_sep == NULL) { - thousand_sep = ","; - thousand_sep_len = 1; - } + if (dec_point == NULL) { + dec_point = "."; + dec_point_len = 1; + } + if (thousand_sep == NULL) { + thousand_sep = ","; + thousand_sep_len = 1; + } - switch (Z_TYPE_P(num)) { - case IS_LONG: - RETURN_STR(_php_math_number_format_long(Z_LVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, - thousand_sep_len)); + switch (Z_TYPE_P(num)) { + case IS_LONG: + RETURN_STR(_php_math_number_format_long(Z_LVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len)); break; case IS_DOUBLE: @@ -1452,23 +1361,23 @@ PHP_FUNCTION(number_format) RETURN_STR(_php_math_number_format_ex(Z_DVAL_P(num), dec_int, dec_point, dec_point_len, thousand_sep, thousand_sep_len)); break; - EMPTY_SWITCH_DEFAULT_CASE() - } - } + EMPTY_SWITCH_DEFAULT_CASE() + } +} /* }}} */ /* {{{ Returns the remainder of dividing x by y as a float */ PHP_FUNCTION(fmod) - { - double num1, num2; +{ + double num1, num2; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(num1) - Z_PARAM_DOUBLE(num2) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(num1) + Z_PARAM_DOUBLE(num2) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(fmod(num1, num2)); - } + RETURN_DOUBLE(fmod(num1, num2)); +} /* }}} */ /* {{{ Perform floating-point division of dividend / divisor @@ -1477,38 +1386,38 @@ PHP_FUNCTION(fmod) __attribute__((no_sanitize("float-divide-by-zero"))) #endif PHP_FUNCTION(fdiv) - { - double dividend, divisor; +{ + double dividend, divisor; - ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_DOUBLE(dividend) - Z_PARAM_DOUBLE(divisor) - ZEND_PARSE_PARAMETERS_END(); + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_DOUBLE(dividend) + Z_PARAM_DOUBLE(divisor) + ZEND_PARSE_PARAMETERS_END(); - RETURN_DOUBLE(dividend / divisor); - } + RETURN_DOUBLE(dividend / divisor); +} /* }}} */ /* {{{ Returns the integer quotient of the division of dividend by divisor */ PHP_FUNCTION(intdiv) - { - zend_long dividend, divisor; +{ + zend_long dividend, divisor; - ZEND_PARSE_PARAMETERS_START(2, 2) + ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(dividend) Z_PARAM_LONG(divisor) - ZEND_PARSE_PARAMETERS_END(); - - if (divisor == 0) { - zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero"); - RETURN_THROWS(); - } else if (divisor == -1 && dividend == ZEND_LONG_MIN) { - /* Prevent overflow error/crash ... really should not happen: - We don't return a float here as that violates function contract */ - zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Division of PHP_INT_MIN by -1 is not an integer"); - RETURN_THROWS(); - } + ZEND_PARSE_PARAMETERS_END(); + + if (divisor == 0) { + zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero"); + RETURN_THROWS(); + } else if (divisor == -1 && dividend == ZEND_LONG_MIN) { + /* Prevent overflow error/crash ... really should not happen: + We don't return a float here as that violates function contract */ + zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Division of PHP_INT_MIN by -1 is not an integer"); + RETURN_THROWS(); + } - RETURN_LONG(dividend / divisor); - } + RETURN_LONG(dividend / divisor); +} /* }}} */ From 76ab816e04fb96c1591b197e2ced5711ba4409e1 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 15 Nov 2023 22:44:11 +0100 Subject: [PATCH 14/25] Removed indentation of switch branches --- ext/standard/math.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 85c100b5c3eae..1fe4a111889ca 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -359,10 +359,10 @@ PHP_FUNCTION(round) case PHP_ROUND_HALF_DOWN: case PHP_ROUND_HALF_EVEN: case PHP_ROUND_HALF_ODD: - case PHP_ROUND_AWAY_FROM_ZERO: - case PHP_ROUND_TOWARD_ZERO: - case PHP_ROUND_CEILING: - case PHP_ROUND_FLOOR: + case PHP_ROUND_AWAY_FROM_ZERO: + case PHP_ROUND_TOWARD_ZERO: + case PHP_ROUND_CEILING: + case PHP_ROUND_FLOOR: break; default: zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); From c085eeeed4298f5d5c3f3a0b2c718ed8f1a98049 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 00:37:49 +0100 Subject: [PATCH 15/25] Removed unnecessary formatting changes --- ext/standard/math.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 1fe4a111889ca..8a48bf8c361e9 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -36,7 +36,7 @@ static inline int php_intlog10abs(double value) { value = fabs(value); if (value < 1e-8 || value > 1e22) { - return (int) floor(log10(value)); + return (int)floor(log10(value)); } else { /* Do a binary search with 5 steps */ int result = 15; @@ -79,7 +79,7 @@ static inline int php_intlog10abs(double value) { static inline double php_intpow10(int power) { /* Not in lookup table */ if (power < 0 || power > 22) { - return pow(10.0, (double) power); + return pow(10.0, (double)power); } static const double powers[] = { @@ -197,7 +197,7 @@ PHPAPI double _php_math_round(double value, int places, int mode) { return value; } - places = places < INT_MIN + 1 ? INT_MIN + 1 : places; + places = places < INT_MIN+1 ? INT_MIN+1 : places; precision_places = 14 - php_intlog10abs(value); f1 = php_intpow10(abs(places)); @@ -206,9 +206,9 @@ PHPAPI double _php_math_round(double value, int places, int mode) { the requested places BUT is small enough to make sure a non-zero value is returned, pre-round the result to the precision */ if (precision_places > places && precision_places - 15 < places) { - int64_t use_precision = precision_places < INT_MIN + 1 ? INT_MIN + 1 : precision_places; + int64_t use_precision = precision_places < INT_MIN+1 ? INT_MIN+1 : precision_places; - f2 = php_intpow10(abs((int) use_precision)); + f2 = php_intpow10(abs((int)use_precision)); if (use_precision >= 0) { tmp_value = value * f2; } else { @@ -219,9 +219,9 @@ PHPAPI double _php_math_round(double value, int places, int mode) { tmp_value = php_round_helper(tmp_value, mode); use_precision = places - precision_places; - use_precision = use_precision < INT_MIN + 1 ? INT_MIN + 1 : use_precision; + use_precision = use_precision < INT_MIN+1 ? INT_MIN+1 : use_precision; /* now correctly move the decimal point */ - f2 = php_intpow10(abs((int) use_precision)); + f2 = php_intpow10(abs((int)use_precision)); /* because places < precision_places */ tmp_value = tmp_value / f2; } else { @@ -1353,6 +1353,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 { From b8e39330d31505076e00feca3bd93e9669bae5b5 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 00:48:50 +0100 Subject: [PATCH 16/25] Updated stub for basic_functions --- ext/standard/basic_functions_arginfo.h | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 39500b2b582dc..966ab01b0f73b 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: 7b14f7e9eb57bc1f2aff20097fddd14d1fe0494e */ 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) @@ -720,11 +720,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_syslog, 0, 2, IS_TRUE, 0) ZEND_END_ARG_INFO() #endif -#if defined(HAVE_INET_NTOP) -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_inet_ntop, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) - ZEND_ARG_TYPE_INFO(0, ip, IS_STRING, 0) -ZEND_END_ARG_INFO() -#endif +#define arginfo_inet_ntop arginfo_gethostbyaddr #if defined(HAVE_INET_PTON) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_inet_pton, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) @@ -1830,7 +1826,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_context_create, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, params, IS_ARRAY, 1, "null") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_params, 0, 2, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_params, 0, 2, IS_TRUE, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_INFO(0, params, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -1839,14 +1835,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_get_params, 0, 1, ZEND_ARG_INFO(0, context) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_option, 0, 2, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_option, 0, 2, IS_TRUE, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_MASK(0, wrapper_or_options, MAY_BE_ARRAY|MAY_BE_STRING, NULL) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, option_name, IS_STRING, 1, "null") ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_options, 0, 2, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stream_context_set_options, 0, 2, IS_TRUE, 0) ZEND_ARG_INFO(0, context) ZEND_ARG_TYPE_INFO(0, options, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -2415,9 +2411,7 @@ ZEND_FUNCTION(closelog); #if defined(HAVE_SYSLOG_H) ZEND_FUNCTION(syslog); #endif -#if defined(HAVE_INET_NTOP) ZEND_FUNCTION(inet_ntop); -#endif #if defined(HAVE_INET_PTON) ZEND_FUNCTION(inet_pton); #endif @@ -3050,9 +3044,7 @@ static const zend_function_entry ext_functions[] = { #if defined(HAVE_SYSLOG_H) ZEND_FE(syslog, arginfo_syslog) #endif -#if defined(HAVE_INET_NTOP) ZEND_FE(inet_ntop, arginfo_inet_ntop) -#endif #if defined(HAVE_INET_PTON) ZEND_FE(inet_pton, arginfo_inet_pton) #endif @@ -3577,6 +3569,10 @@ static void register_basic_functions_symbols(int module_number) REGISTER_LONG_CONSTANT("PHP_ROUND_HALF_DOWN", PHP_ROUND_HALF_DOWN, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PHP_ROUND_HALF_EVEN", PHP_ROUND_HALF_EVEN, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PHP_ROUND_HALF_ODD", PHP_ROUND_HALF_ODD, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_ROUND_CEILING", PHP_ROUND_CEILING, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_ROUND_FLOOR", PHP_ROUND_FLOOR, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_ROUND_TOWARD_ZERO", PHP_ROUND_TOWARD_ZERO, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_ROUND_AWAY_FROM_ZERO", PHP_ROUND_AWAY_FROM_ZERO, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_STD_DES", 1, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_EXT_DES", 1, CONST_PERSISTENT); From 7acc226602e073ddbbdf72f46a40575a49378bcb Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 00:55:56 +0100 Subject: [PATCH 17/25] Merged if conditions --- ext/standard/math.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 8a48bf8c361e9..ffec5b64e2234 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -127,14 +127,14 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_CEILING: - if (value > 0.0) { - return fractional == 0 ? integral : integral + 1.0; + if (value > 0.0 && fractional > 0.0 ) { + return integral + 1.0; } return integral; case PHP_ROUND_FLOOR: - if (value < 0.0) { - return fractional == 0 ? integral : integral - 1.0; + if (value < 0.0 && fractional > 0.0) { + return integral - 1.0; } return integral; From 2a53d44a109db3f382a7d82c10929f669a8bfcb4 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 01:04:52 +0100 Subject: [PATCH 18/25] Used if condition for PHP_ROUND_AWAY_FROM_ZERO mode --- ext/standard/math.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index ffec5b64e2234..adf8b25c3b509 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -142,7 +142,10 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_AWAY_FROM_ZERO: - return fractional == 0 ? integral : integral + copysign(1.0, integral); + if (fractional > 0.0) { + return integral + copysign(1.0, integral); + } + return integral: case PHP_ROUND_HALF_EVEN: if (fractional > 0.5) { From 4b919d8f6345fe23c73627c4fb72016423591840 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 01:13:35 +0100 Subject: [PATCH 19/25] Replaced missing colon --- ext/standard/math.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index adf8b25c3b509..77cd44a0a16ed 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -145,7 +145,7 @@ static inline double php_round_helper(double value, int mode) { if (fractional > 0.0) { return integral + copysign(1.0, integral); } - return integral: + return integral; case PHP_ROUND_HALF_EVEN: if (fractional > 0.5) { From 17eda271e3bd4ce8a819a63b67a300344568d7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 19 Dec 2023 20:47:18 +0100 Subject: [PATCH 20/25] [ci skip] Formatting --- ext/standard/math.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 77cd44a0a16ed..4ee5f9482909d 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -93,6 +93,7 @@ static inline double php_intpow10(int power) { /* {{{ php_round_helper Actually performs the rounding of a value to integer in a certain mode */ static inline double php_round_helper(double value, int mode) { + double integral, fractional; /* Split the input value into the integral and fractional part. * @@ -100,8 +101,7 @@ static inline double php_round_helper(double value, int mode) { * the absolute value of the fractional part (which will not result * in branches in the assembly) to make the following cases simpler. */ - double integral; - double fractional = fabs(modf(value, &integral)); + fractional = fabs(modf(value, &integral)); switch (mode) { case PHP_ROUND_HALF_UP: @@ -127,15 +127,17 @@ static inline double php_round_helper(double value, int mode) { return integral; case PHP_ROUND_CEILING: - if (value > 0.0 && fractional > 0.0 ) { + if (value > 0.0 && fractional > 0.0) { return integral + 1.0; } + return integral; case PHP_ROUND_FLOOR: if (value < 0.0 && fractional > 0.0) { return integral - 1.0; } + return integral; case PHP_ROUND_TOWARD_ZERO: @@ -145,6 +147,7 @@ static inline double php_round_helper(double value, int mode) { if (fractional > 0.0) { return integral + copysign(1.0, integral); } + return integral; case PHP_ROUND_HALF_EVEN: @@ -179,6 +182,7 @@ static inline double php_round_helper(double value, int mode) { } return integral; + EMPTY_SWITCH_DEFAULT_CASE(); } // FIXME: GCC bug, branch is considered reachable. From db8ffaaca7eda4d7e2fec2c9348d1f1a715b157c Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Tue, 19 Dec 2023 21:29:52 +0100 Subject: [PATCH 21/25] [skip ci] Renamed title of test round_modes_ceiling_and_floor.phpt --- ext/standard/tests/math/round_modes_ceiling_and_floor.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt b/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt index 6c52f8269a263..1b3a34f0f98ec 100644 --- a/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt +++ b/ext/standard/tests/math/round_modes_ceiling_and_floor.phpt @@ -1,5 +1,5 @@ --TEST-- -round() with different rounding modes +round() with modes PHP_ROUND_CEILING and PHP_ROUND_FLOOR --FILE-- Date: Wed, 20 Dec 2023 20:15:34 +0100 Subject: [PATCH 22/25] Added tests of round() for zeroes --- .../tests/math/round_modes_zeros.phpt | 230 ++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 ext/standard/tests/math/round_modes_zeros.phpt diff --git a/ext/standard/tests/math/round_modes_zeros.phpt b/ext/standard/tests/math/round_modes_zeros.phpt new file mode 100644 index 0000000000000..cdf97395dd9bf --- /dev/null +++ b/ext/standard/tests/math/round_modes_zeros.phpt @@ -0,0 +1,230 @@ +--TEST-- +round() with different rounding modes for zeros +--FILE-- + PHP_ROUND_HALF_UP, + "PHP_ROUND_HALF_DOWN" => PHP_ROUND_HALF_DOWN, + "PHP_ROUND_HALF_EVEN" => PHP_ROUND_HALF_EVEN, + "PHP_ROUND_HALF_ODD" => PHP_ROUND_HALF_ODD, + "PHP_ROUND_CEILING" => PHP_ROUND_CEILING, + "PHP_ROUND_FLOOR" => PHP_ROUND_FLOOR, + "PHP_ROUND_TOWARD_ZERO" => PHP_ROUND_TOWARD_ZERO, + "PHP_ROUND_AWAY_FROM_ZERO" => PHP_ROUND_AWAY_FROM_ZERO, +]; + +$precisions = [-1, 0, 1, 2]; + +foreach ($modes as $modeKey => $mode) { + echo "mode: $modeKey\n"; + foreach ($precisions as $precision) { + echo "\tprecision: $precision\n"; + echo "\t\t 0 => " . round(0, $precision, $mode) . "\n"; + echo "\t\t -0 => " . round(-0, $precision, $mode) . "\n"; + echo "\t\t 0.0 => " . round(0.0, $precision, $mode) . "\n"; + echo "\t\t -0.0 => " . round(-0.0, $precision, $mode) . "\n"; + echo "\n"; + } +} + +?> +--EXPECT-- +mode: PHP_ROUND_HALF_UP + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_HALF_DOWN + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_HALF_EVEN + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_HALF_ODD + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_CEILING + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_FLOOR + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_TOWARD_ZERO + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + +mode: PHP_ROUND_AWAY_FROM_ZERO + precision: -1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 0 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 1 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 + + precision: 2 + 0 => 0 + -0 => 0 + 0.0 => 0 + -0.0 => -0 From be9e7ec5d7c17c01879ad3579cf7c913a5d114a3 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 20 Dec 2023 20:15:50 +0100 Subject: [PATCH 23/25] Removed zeroes from round() standard tests --- ext/standard/tests/math/round_modes.phpt | 41 ------------------------ 1 file changed, 41 deletions(-) diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index e78f14810f4f5..52b8f9f0c30fb 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -26,7 +26,6 @@ $numbers = [ 0.69, 0.65, -0.65, - 0, 1.9999, -1.9999, 0.0001, @@ -64,7 +63,6 @@ mode: PHP_ROUND_HALF_UP 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 0 -1.9999 => -0 0.0001 => 0 @@ -83,7 +81,6 @@ mode: PHP_ROUND_HALF_UP 0.69 => 1 0.65 => 1 -0.65 => -1 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -102,7 +99,6 @@ mode: PHP_ROUND_HALF_UP 0.69 => 0.7 0.65 => 0.7 -0.65 => -0.7 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -121,7 +117,6 @@ mode: PHP_ROUND_HALF_UP 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -140,7 +135,6 @@ mode: PHP_ROUND_HALF_UP 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -160,7 +154,6 @@ mode: PHP_ROUND_HALF_DOWN 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 0 -1.9999 => -0 0.0001 => 0 @@ -179,7 +172,6 @@ mode: PHP_ROUND_HALF_DOWN 0.69 => 1 0.65 => 1 -0.65 => -1 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -198,7 +190,6 @@ mode: PHP_ROUND_HALF_DOWN 0.69 => 0.7 0.65 => 0.6 -0.65 => -0.6 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -217,7 +208,6 @@ mode: PHP_ROUND_HALF_DOWN 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -236,7 +226,6 @@ mode: PHP_ROUND_HALF_DOWN 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -256,7 +245,6 @@ mode: PHP_ROUND_HALF_EVEN 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 0 -1.9999 => -0 0.0001 => 0 @@ -275,7 +263,6 @@ mode: PHP_ROUND_HALF_EVEN 0.69 => 1 0.65 => 1 -0.65 => -1 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -294,7 +281,6 @@ mode: PHP_ROUND_HALF_EVEN 0.69 => 0.7 0.65 => 0.6 -0.65 => -0.6 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -313,7 +299,6 @@ mode: PHP_ROUND_HALF_EVEN 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -332,7 +317,6 @@ mode: PHP_ROUND_HALF_EVEN 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -352,7 +336,6 @@ mode: PHP_ROUND_HALF_ODD 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 0 -1.9999 => -0 0.0001 => 0 @@ -371,7 +354,6 @@ mode: PHP_ROUND_HALF_ODD 0.69 => 1 0.65 => 1 -0.65 => -1 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -390,7 +372,6 @@ mode: PHP_ROUND_HALF_ODD 0.69 => 0.7 0.65 => 0.7 -0.65 => -0.7 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -409,7 +390,6 @@ mode: PHP_ROUND_HALF_ODD 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0 @@ -428,7 +408,6 @@ mode: PHP_ROUND_HALF_ODD 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -448,7 +427,6 @@ mode: PHP_ROUND_CEILING 0.69 => 10 0.65 => 10 -0.65 => -0 - 0 => 0 1.9999 => 10 -1.9999 => -0 0.0001 => 10 @@ -467,7 +445,6 @@ mode: PHP_ROUND_CEILING 0.69 => 1 0.65 => 1 -0.65 => -0 - 0 => 0 1.9999 => 2 -1.9999 => -1 0.0001 => 1 @@ -486,7 +463,6 @@ mode: PHP_ROUND_CEILING 0.69 => 0.7 0.65 => 0.7 -0.65 => -0.6 - 0 => 0 1.9999 => 2 -1.9999 => -1.9 0.0001 => 0.1 @@ -505,7 +481,6 @@ mode: PHP_ROUND_CEILING 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -1.99 0.0001 => 0.01 @@ -524,7 +499,6 @@ mode: PHP_ROUND_CEILING 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -544,7 +518,6 @@ mode: PHP_ROUND_FLOOR 0.69 => 0 0.65 => 0 -0.65 => -10 - 0 => 0 1.9999 => 0 -1.9999 => -10 0.0001 => 0 @@ -563,7 +536,6 @@ mode: PHP_ROUND_FLOOR 0.69 => 0 0.65 => 0 -0.65 => -1 - 0 => 0 1.9999 => 1 -1.9999 => -2 0.0001 => 0 @@ -582,7 +554,6 @@ mode: PHP_ROUND_FLOOR 0.69 => 0.6 0.65 => 0.6 -0.65 => -0.7 - 0 => 0 1.9999 => 1.9 -1.9999 => -2 0.0001 => 0 @@ -601,7 +572,6 @@ mode: PHP_ROUND_FLOOR 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.99 -1.9999 => -2 0.0001 => 0 @@ -620,7 +590,6 @@ mode: PHP_ROUND_FLOOR 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -640,7 +609,6 @@ mode: PHP_ROUND_TOWARD_ZERO 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 0 -1.9999 => -0 0.0001 => 0 @@ -659,7 +627,6 @@ mode: PHP_ROUND_TOWARD_ZERO 0.69 => 0 0.65 => 0 -0.65 => -0 - 0 => 0 1.9999 => 1 -1.9999 => -1 0.0001 => 0 @@ -678,7 +645,6 @@ mode: PHP_ROUND_TOWARD_ZERO 0.69 => 0.6 0.65 => 0.6 -0.65 => -0.6 - 0 => 0 1.9999 => 1.9 -1.9999 => -1.9 0.0001 => 0 @@ -697,7 +663,6 @@ mode: PHP_ROUND_TOWARD_ZERO 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.99 -1.9999 => -1.99 0.0001 => 0 @@ -716,7 +681,6 @@ mode: PHP_ROUND_TOWARD_ZERO 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 @@ -736,7 +700,6 @@ mode: PHP_ROUND_AWAY_FROM_ZERO 0.69 => 10 0.65 => 10 -0.65 => -10 - 0 => 0 1.9999 => 10 -1.9999 => -10 0.0001 => 10 @@ -755,7 +718,6 @@ mode: PHP_ROUND_AWAY_FROM_ZERO 0.69 => 1 0.65 => 1 -0.65 => -1 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 1 @@ -774,7 +736,6 @@ mode: PHP_ROUND_AWAY_FROM_ZERO 0.69 => 0.7 0.65 => 0.7 -0.65 => -0.7 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0.1 @@ -793,7 +754,6 @@ mode: PHP_ROUND_AWAY_FROM_ZERO 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 2 -1.9999 => -2 0.0001 => 0.01 @@ -812,7 +772,6 @@ mode: PHP_ROUND_AWAY_FROM_ZERO 0.69 => 0.69 0.65 => 0.65 -0.65 => -0.65 - 0 => 0 1.9999 => 1.9999 -1.9999 => -1.9999 0.0001 => 0.0001 From 7f8b0a3a965986f2e07f6421ca80bc0bd2206577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 21 Dec 2023 13:13:07 +0100 Subject: [PATCH 24/25] Re-add 7/-7 to round_modes.phpt --- ext/standard/tests/math/round_modes.phpt | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/ext/standard/tests/math/round_modes.phpt b/ext/standard/tests/math/round_modes.phpt index 52b8f9f0c30fb..06a793211b3a7 100644 --- a/ext/standard/tests/math/round_modes.phpt +++ b/ext/standard/tests/math/round_modes.phpt @@ -18,6 +18,8 @@ $numbers = [ -2.5, 3.5, -3.5, + 7, + -7, 15.51, -15.51, 5, @@ -55,6 +57,8 @@ mode: PHP_ROUND_HALF_UP -2.5 => -0 3.5 => 0 -3.5 => -0 + 7 => 10 + -7 => -10 15.51 => 20 -15.51 => -20 5 => 10 @@ -73,6 +77,8 @@ mode: PHP_ROUND_HALF_UP -2.5 => -3 3.5 => 4 -3.5 => -4 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -16 5 => 5 @@ -91,6 +97,8 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.5 5 => 5 @@ -109,6 +117,8 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -127,6 +137,8 @@ mode: PHP_ROUND_HALF_UP -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -146,6 +158,8 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -0 3.5 => 0 -3.5 => -0 + 7 => 10 + -7 => -10 15.51 => 20 -15.51 => -20 5 => 0 @@ -164,6 +178,8 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2 3.5 => 3 -3.5 => -3 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -16 5 => 5 @@ -182,6 +198,8 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.5 5 => 5 @@ -200,6 +218,8 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -218,6 +238,8 @@ mode: PHP_ROUND_HALF_DOWN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -237,6 +259,8 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -0 3.5 => 0 -3.5 => -0 + 7 => 10 + -7 => -10 15.51 => 20 -15.51 => -20 5 => 0 @@ -255,6 +279,8 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2 3.5 => 4 -3.5 => -4 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -16 5 => 5 @@ -273,6 +299,8 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.5 5 => 5 @@ -291,6 +319,8 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -309,6 +339,8 @@ mode: PHP_ROUND_HALF_EVEN -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -328,6 +360,8 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -0 3.5 => 0 -3.5 => -0 + 7 => 10 + -7 => -10 15.51 => 20 -15.51 => -20 5 => 10 @@ -346,6 +380,8 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -3 3.5 => 3 -3.5 => -3 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -16 5 => 5 @@ -364,6 +400,8 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.5 5 => 5 @@ -382,6 +420,8 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -400,6 +440,8 @@ mode: PHP_ROUND_HALF_ODD -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -419,6 +461,8 @@ mode: PHP_ROUND_CEILING -2.5 => -0 3.5 => 10 -3.5 => -0 + 7 => 10 + -7 => -0 15.51 => 20 -15.51 => -10 5 => 10 @@ -437,6 +481,8 @@ mode: PHP_ROUND_CEILING -2.5 => -2 3.5 => 4 -3.5 => -3 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -15 5 => 5 @@ -455,6 +501,8 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.6 -15.51 => -15.5 5 => 5 @@ -473,6 +521,8 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -491,6 +541,8 @@ mode: PHP_ROUND_CEILING -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -510,6 +562,8 @@ mode: PHP_ROUND_FLOOR -2.5 => -10 3.5 => 0 -3.5 => -10 + 7 => 0 + -7 => -10 15.51 => 10 -15.51 => -20 5 => 0 @@ -528,6 +582,8 @@ mode: PHP_ROUND_FLOOR -2.5 => -3 3.5 => 3 -3.5 => -4 + 7 => 7 + -7 => -7 15.51 => 15 -15.51 => -16 5 => 5 @@ -546,6 +602,8 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.6 5 => 5 @@ -564,6 +622,8 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -582,6 +642,8 @@ mode: PHP_ROUND_FLOOR -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -601,6 +663,8 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -0 3.5 => 0 -3.5 => -0 + 7 => 0 + -7 => -0 15.51 => 10 -15.51 => -10 5 => 0 @@ -619,6 +683,8 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2 3.5 => 3 -3.5 => -3 + 7 => 7 + -7 => -7 15.51 => 15 -15.51 => -15 5 => 5 @@ -637,6 +703,8 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.5 -15.51 => -15.5 5 => 5 @@ -655,6 +723,8 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -673,6 +743,8 @@ mode: PHP_ROUND_TOWARD_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -692,6 +764,8 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -10 3.5 => 10 -3.5 => -10 + 7 => 10 + -7 => -10 15.51 => 20 -15.51 => -20 5 => 10 @@ -710,6 +784,8 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -3 3.5 => 4 -3.5 => -4 + 7 => 7 + -7 => -7 15.51 => 16 -15.51 => -16 5 => 5 @@ -728,6 +804,8 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.6 -15.51 => -15.6 5 => 5 @@ -746,6 +824,8 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 @@ -764,6 +844,8 @@ mode: PHP_ROUND_AWAY_FROM_ZERO -2.5 => -2.5 3.5 => 3.5 -3.5 => -3.5 + 7 => 7 + -7 => -7 15.51 => 15.51 -15.51 => -15.51 5 => 5 From fd8248707b51c408cddb94974677a976cc1c8153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 21 Dec 2023 13:26:27 +0100 Subject: [PATCH 25/25] UPGRADING / NEWS --- NEWS | 7 +++---- UPGRADING | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index bedfb9db0d482..e6c3ea5522f97 100644 --- a/NEWS +++ b/NEWS @@ -30,8 +30,8 @@ Intl: . Added IntlDateFormatter::PATTERN constant. (David Carlier) . Fixed Numberformatter::__construct when the locale is invalid, now throws an exception. (David Carlier) - . Created aliases ROUND_TOWARD_ZERO and ROUND_AWAY_FROM_ZERO for constants - ROUND_DOWN and ROUND_UP. (Jorg Sowa) + . Added NumberFormatter::ROUND_TOWARD_ZERO and ::ROUND_AWAY_FROM_ZERO as + aliases for ::ROUND_DOWN and ::ROUND_UP. (Jorg Sowa) MBString: . Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada) @@ -109,8 +109,7 @@ Standard: . 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) - . Added 4 new rounding modes to the round(). (Jorg Sowa) - + . Add support for 4 new rounding modes to the round() function. (Jorg Sowa) XML: . Added XML_OPTION_PARSE_HUGE parser option. (nielsdos) diff --git a/UPGRADING b/UPGRADING index 27504fc5c9850..f202e8cb5399f 100644 --- a/UPGRADING +++ b/UPGRADING @@ -216,6 +216,11 @@ PHP 8.4 UPGRADE NOTES - Intl: . IntlDateFormatter::__construct() throws a ValueError if the locale is invalid. . NumberFormatter::__construct() throws a ValueError if the locale is invalid. + . NumberFormatter::ROUND_TOWARD_ZERO and NumberFormatter::ROUND_AWAY_FROM_ZERO + have been added as aliases for NumberFormatter::ROUND_DOWN and + NumberFormatter::ROUND_UP to be consistent with the new PHP_ROUND_* modes. + + RFC: https://wiki.php.net/rfc/new_rounding_modes_to_round_function - MBString: . The behavior of mb_strcut is more consistent now on invalid UTF-8 and UTF-16 @@ -250,6 +255,10 @@ PHP 8.4 UPGRADE NOTES has been increased from '10' to '12'. RFC: https://wiki.php.net/rfc/bcrypt_cost_2023 + . Four new modes have been added to the round() function: PHP_ROUND_CEILING, + PHP_ROUND_FLOOR, PHP_ROUND_TOWARD_ZERO, PHP_ROUND_AWAY_FROM_ZERO. + + RFC: https://wiki.php.net/rfc/new_rounding_modes_to_round_function ======================================== 6. New Functions