From 44f5e645e134d8738ae608e623b55be0b0fb4b5e Mon Sep 17 00:00:00 2001 From: SakiTakamachi Date: Wed, 10 Jan 2024 00:07:31 +0900 Subject: [PATCH 01/18] RFC: Add bcfloor, bcceil and bcround to BCMath --- Zend/Optimizer/zend_func_infos.h | 3 + ext/bcmath/bcmath.c | 90 +++ ext/bcmath/bcmath.stub.php | 9 + ext/bcmath/bcmath_arginfo.h | 20 +- ext/bcmath/config.m4 | 4 +- ext/bcmath/config.w32 | 3 +- ext/bcmath/libbcmath/src/bcmath.h | 8 +- ext/bcmath/libbcmath/src/floor_or_ceil.c | 70 ++ ext/bcmath/libbcmath/src/round.c | 164 +++++ ext/bcmath/php_bcmath.h | 2 + ext/bcmath/tests/bcceil.phpt | 78 +++ ext/bcmath/tests/bcfloor.phpt | 78 +++ ext/bcmath/tests/bcround.phpt | 827 +++++++++++++++++++++++ 13 files changed, 1351 insertions(+), 5 deletions(-) create mode 100644 ext/bcmath/libbcmath/src/floor_or_ceil.c create mode 100644 ext/bcmath/libbcmath/src/round.c create mode 100644 ext/bcmath/tests/bcceil.phpt create mode 100644 ext/bcmath/tests/bcfloor.phpt create mode 100644 ext/bcmath/tests/bcround.phpt diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h index 76628104528fe..538326baf4959 100644 --- a/Zend/Optimizer/zend_func_infos.h +++ b/Zend/Optimizer/zend_func_infos.h @@ -27,6 +27,9 @@ static const func_info_t func_infos[] = { F1("bcpowmod", MAY_BE_STRING), F1("bcpow", MAY_BE_STRING), F1("bcsqrt", MAY_BE_STRING), + F1("bcfloor", MAY_BE_STRING), + F1("bcceil", MAY_BE_STRING), + F1("bcround", MAY_BE_STRING), FN("bzopen", MAY_BE_RESOURCE|MAY_BE_FALSE), F1("bzerror", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING), F1("cal_from_jd", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL), diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index f53032fabc35c..7be9d4b01528a 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -643,6 +643,96 @@ PHP_FUNCTION(bccomp) } /* }}} */ +/* {{{ floor or ceil */ +static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor) +{ + zend_string *numstr; + bc_num num, result; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_STR(numstr) + ZEND_PARSE_PARAMETERS_END(); + + bc_init_num(&num); + bc_init_num(&result); + + if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) { + zend_argument_value_error(1, "is not well-formed"); + goto cleanup; + } + + bc_floor_or_ceil(num, is_floor, &result); + RETVAL_STR(bc_num2str_ex(result, 0)); + + cleanup: { + bc_free_num(&num); + bc_free_num(&result); + }; +} +/* }}} */ + +/* {{{ Returns floor of num */ +PHP_FUNCTION(bcfloor) +{ + bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, true); +} +/* }}} */ + +/* {{{ Returns ceil of num */ +PHP_FUNCTION(bcceil) +{ + bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, false); +} +/* }}} */ + +/* {{{ Returns num rounded to the digits specified by precision. */ +PHP_FUNCTION(bcround) +{ + zend_string *numstr; + zend_long precision = 0; + zend_long mode = PHP_ROUND_HALF_UP; + bc_num num, result; + + ZEND_PARSE_PARAMETERS_START(1, 3) + Z_PARAM_STR(numstr) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(precision) + Z_PARAM_LONG(mode) + ZEND_PARSE_PARAMETERS_END(); + + 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_CEILING: + case PHP_ROUND_FLOOR: + case PHP_ROUND_TOWARD_ZERO: + case PHP_ROUND_AWAY_FROM_ZERO: + break; + default: + zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); + return; + } + + bc_init_num(&num); + bc_init_num(&result); + + if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) { + zend_argument_value_error(1, "is not well-formed"); + goto cleanup; + } + + bc_round(num, precision, mode, &result); + RETVAL_STR(bc_num2str_ex(result, result->n_scale)); + + cleanup: { + bc_free_num(&num); + bc_free_num(&result); + }; +} +/* }}} */ + /* {{{ Sets default scale parameter for all bc math functions */ PHP_FUNCTION(bcscale) { diff --git a/ext/bcmath/bcmath.stub.php b/ext/bcmath/bcmath.stub.php index 7335b2c399be2..5f5e60d3804a8 100644 --- a/ext/bcmath/bcmath.stub.php +++ b/ext/bcmath/bcmath.stub.php @@ -29,3 +29,12 @@ function bcsqrt(string $num, ?int $scale = null): string {} function bccomp(string $num1, string $num2, ?int $scale = null): int {} function bcscale(?int $scale = null): int {} + +/** @refcount 1 */ +function bcfloor(string $num): string {} + +/** @refcount 1 */ +function bcceil(string $num): string {} + +/** @refcount 1 */ +function bcround(string $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): string {} diff --git a/ext/bcmath/bcmath_arginfo.h b/ext/bcmath/bcmath_arginfo.h index 858885f70f66a..91ab4fd156477 100644 --- a/ext/bcmath/bcmath_arginfo.h +++ b/ext/bcmath/bcmath_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: f28dafc2a279f5421cd0d0e668fde0032e996ebc */ + * Stub hash: cd3d182e13cb0ca22b27c13a8d0a86c20fde5b76 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcadd, 0, 2, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, num1, IS_STRING, 0) @@ -43,6 +43,18 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcscale, 0, 0, IS_LONG, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null") ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcfloor, 0, 1, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, num, IS_STRING, 0) +ZEND_END_ARG_INFO() + +#define arginfo_bcceil arginfo_bcfloor + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcround, 0, 1, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, num, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, precision, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mode, IS_LONG, 0, "PHP_ROUND_HALF_UP") +ZEND_END_ARG_INFO() + ZEND_FUNCTION(bcadd); ZEND_FUNCTION(bcsub); ZEND_FUNCTION(bcmul); @@ -53,6 +65,9 @@ ZEND_FUNCTION(bcpow); ZEND_FUNCTION(bcsqrt); ZEND_FUNCTION(bccomp); ZEND_FUNCTION(bcscale); +ZEND_FUNCTION(bcfloor); +ZEND_FUNCTION(bcceil); +ZEND_FUNCTION(bcround); static const zend_function_entry ext_functions[] = { ZEND_FE(bcadd, arginfo_bcadd) @@ -65,5 +80,8 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(bcsqrt, arginfo_bcsqrt) ZEND_FE(bccomp, arginfo_bccomp) ZEND_FE(bcscale, arginfo_bcscale) + ZEND_FE(bcfloor, arginfo_bcfloor) + ZEND_FE(bcceil, arginfo_bcceil) + ZEND_FE(bcround, arginfo_bcround) ZEND_FE_END }; diff --git a/ext/bcmath/config.m4 b/ext/bcmath/config.m4 index ac654aba00770..6053b72ec1fda 100644 --- a/ext/bcmath/config.m4 +++ b/ext/bcmath/config.m4 @@ -7,8 +7,8 @@ if test "$PHP_BCMATH" != "no"; then PHP_NEW_EXTENSION(bcmath, bcmath.c \ libbcmath/src/add.c libbcmath/src/div.c libbcmath/src/init.c libbcmath/src/neg.c libbcmath/src/raisemod.c libbcmath/src/sub.c \ libbcmath/src/compare.c libbcmath/src/divmod.c libbcmath/src/int2num.c libbcmath/src/num2long.c libbcmath/src/output.c libbcmath/src/recmul.c \ -libbcmath/src/sqrt.c libbcmath/src/zero.c libbcmath/src/doaddsub.c libbcmath/src/nearzero.c libbcmath/src/num2str.c libbcmath/src/raise.c \ -libbcmath/src/rmzero.c libbcmath/src/str2num.c, +libbcmath/src/sqrt.c libbcmath/src/zero.c libbcmath/src/doaddsub.c libbcmath/src/floor_or_ceil.c libbcmath/src/nearzero.c libbcmath/src/num2str.c \ +libbcmath/src/raise.c libbcmath/src/rmzero.c libbcmath/src/round.c libbcmath/src/str2num.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_ADD_BUILD_DIR($ext_builddir/libbcmath/src) AC_DEFINE(HAVE_BCMATH, 1, [Whether you have bcmath]) diff --git a/ext/bcmath/config.w32 b/ext/bcmath/config.w32 index 4f242daa8a5f8..be6b5a06b2bfe 100644 --- a/ext/bcmath/config.w32 +++ b/ext/bcmath/config.w32 @@ -7,7 +7,8 @@ if (PHP_BCMATH == "yes") { ADD_SOURCES("ext/bcmath/libbcmath/src", "add.c div.c init.c neg.c \ raisemod.c sub.c compare.c divmod.c int2num.c \ num2long.c output.c recmul.c sqrt.c zero.c doaddsub.c \ - nearzero.c num2str.c raise.c rmzero.c str2num.c", "bcmath"); + floor_or_ceil.c nearzero.c num2str.c raise.c rmzero.c str2num.c" \ + round.c, "bcmath"); AC_DEFINE('HAVE_BCMATH', 1, 'Have BCMATH library'); } diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index de51ee7457110..27800d3425736 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -58,7 +58,9 @@ typedef struct bc_struct { #include "zend.h" #include #include "zend_string.h" -#include "../../php_bcmath.h" /* Needed for BCG() macro */ + +/* Needed for BCG() macro and PHP_ROUND_XXX */ +#include "../../php_bcmath.h" /* The base used in storing the numbers in n_value above. Currently, this MUST be 10. */ @@ -125,6 +127,10 @@ bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale); bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale); +void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result); + +void bc_round(bc_num num, zend_long places, zend_long mode, bc_num *result); + typedef enum { OK, BASE_HAS_FRACTIONAL, diff --git a/ext/bcmath/libbcmath/src/floor_or_ceil.c b/ext/bcmath/libbcmath/src/floor_or_ceil.c new file mode 100644 index 0000000000000..7716d07dd2d9f --- /dev/null +++ b/ext/bcmath/libbcmath/src/floor_or_ceil.c @@ -0,0 +1,70 @@ +/* floor_or_ceil.c: bcmath library file. */ +/* + Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc. + Copyright (C) 2000 Philip A. Nelson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. (LICENSE) + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to: + + The Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307 USA. + + You may contact the author by: + e-mail: philnelson@acm.org + us-mail: Philip A. Nelson + Computer Science Department, 9062 + Western Washington University + Bellingham, WA 98226-9062 + +*************************************************************************/ + +#include "bcmath.h" +#include "private.h" +#include + +void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) +{ + bc_num fractional; + + /* clear result */ + bc_free_num(result); + + /* Initialize result */ + *result = bc_new_num(num->n_len, 0); + (*result)->n_sign = num->n_sign; + + /* copy integer part */ + memcpy((*result)->n_value, num->n_value, num->n_len); + + if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) { + return; + } + + /* copy fractional part */ + fractional = bc_new_num(0, num->n_scale); + memcpy(fractional->n_value, num->n_value + num->n_len, num->n_scale); + + if (bc_is_zero(fractional)) { + goto cleanup; + } + + /* add/sub 1 to/from result */ + bc_num tmp = _bc_do_add(*result, BCG(_one_), 0); + tmp->n_sign = (*result)->n_sign; + bc_free_num(result); + *result = tmp; + +cleanup: + bc_free_num(&fractional); +} diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c new file mode 100644 index 0000000000000..e483053264b59 --- /dev/null +++ b/ext/bcmath/libbcmath/src/round.c @@ -0,0 +1,164 @@ +/* round.c: bcmath library file. */ +/* + Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc. + Copyright (C) 2000 Philip A. Nelson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. (LICENSE) + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to: + + The Free Software Foundation, Inc. + 59 Temple Place, Suite 330 + Boston, MA 02111-1307 USA. + + You may contact the author by: + e-mail: philnelson@acm.org + us-mail: Philip A. Nelson + Computer Science Department, 9062 + Western Washington University + Bellingham, WA 98226-9062 + +*************************************************************************/ + +#include "bcmath.h" +#include "private.h" +#include + +void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) +{ + /* clear result */ + bc_free_num(result); + + /* + * The following three cases result in an early return: + * + * 1. When rounding to an integer part, when trying to round to a digit + * larger than the num. + * 2. If try to round to the finest digit of num or finer than that, + * num is returned unchanged. + * 3. If the fraction part is all 0 from the beginning or middle to the + * end, the 0's are omitted and the number of digits in num is reduced. + * In that case, may end up in the same situation as 2. + */ + if ((int) num->n_len + precision <= 0) { + *result = bc_copy_num(BCG(_zero_)); + return; + } + if (precision >= 0 && num->n_scale <= precision) { + *result = bc_copy_num(num); + return; + } + + /* Initialize result */ + *result = bc_new_num(num->n_len, precision > 0 ? precision : 0); + (*result)->n_sign = num->n_sign; + /* + * If the calculation result is a negative value, there is an early return, + * so no underflow will occur. + */ + size_t rounded_len = num->n_len + precision; + memcpy((*result)->n_value, num->n_value, rounded_len); + + char *nptr = num->n_value + rounded_len; + + /* Check cases that can be determined without looping. */ + switch (mode) { + case PHP_ROUND_HALF_UP: + if (*nptr >= 5) { + goto up; + } else if (*nptr < 5) { + return; + } + break; + + case PHP_ROUND_HALF_DOWN: + case PHP_ROUND_HALF_EVEN: + case PHP_ROUND_HALF_ODD: + if (*nptr > 5) { + goto up; + } else if (*nptr < 5) { + return; + } + /* if *nptr == 5, a loop is required for judgment. */ + break; + + case PHP_ROUND_CEILING: + if (num->n_sign != PLUS) { + return; + } else if (*nptr > 0) { + goto up; + } + /* if *nptr == 0, a loop is required for judgment. */ + break; + + case PHP_ROUND_FLOOR: + if (num->n_sign != MINUS) { + return; + } else if (*nptr > 0) { + goto up; + } + /* if *nptr == 0, a loop is required for judgment. */ + break; + + case PHP_ROUND_TOWARD_ZERO: + return; + + case PHP_ROUND_AWAY_FROM_ZERO: + if (*nptr > 0) { + goto up; + } + /* if *nptr == 0, a loop is required for judgment. */ + break; + } + + /* Loop through the remaining digits. */ + size_t count = num->n_len + num->n_scale - rounded_len - 1; + nptr++; + while ((count > 0) && (*nptr++ == 0)) count--; + + if (count > 0) { + goto up; + } + + switch (mode) { + case PHP_ROUND_HALF_DOWN: + case PHP_ROUND_CEILING: + case PHP_ROUND_FLOOR: + case PHP_ROUND_AWAY_FROM_ZERO: + return; + + case PHP_ROUND_HALF_EVEN: + if (num->n_value[rounded_len - 1] % 2 == 0) { + return; + } + break; + + case PHP_ROUND_HALF_ODD: + if (num->n_value[rounded_len - 1] % 2 == 1) { + return; + } + break; + } + +up: + { + bc_num scaled_one = bc_new_num((*result)->n_len, (*result)->n_scale); + scaled_one->n_value[rounded_len - 1] = 1; + + bc_num tmp = _bc_do_add(*result, scaled_one, (*result)->n_scale); + tmp->n_sign = (*result)->n_sign; + + bc_free_num(&scaled_one); + bc_free_num(result); + *result = tmp; + } +} diff --git a/ext/bcmath/php_bcmath.h b/ext/bcmath/php_bcmath.h index 1045019d64ca9..7ecc920c398c1 100644 --- a/ext/bcmath/php_bcmath.h +++ b/ext/bcmath/php_bcmath.h @@ -19,6 +19,8 @@ #include "libbcmath/src/bcmath.h" #include "zend_API.h" +#include "php.h" +#include "ext/standard/php_math.h" extern zend_module_entry bcmath_module_entry; #define phpext_bcmath_ptr &bcmath_module_entry diff --git a/ext/bcmath/tests/bcceil.phpt b/ext/bcmath/tests/bcceil.phpt new file mode 100644 index 0000000000000..36a0afcae2b0c --- /dev/null +++ b/ext/bcmath/tests/bcceil.phpt @@ -0,0 +1,78 @@ +--TEST-- +bcceil() function +--EXTENSIONS-- +bcmath +--FILE-- + '.bcceil($num)."\n\n"; +} +?> +--EXPECT-- +0 +=> 0 + +0.00 +=> 0 + +-0 +=> 0 + +-0.00 +=> 0 + +0.01 +=> 1 + +0.000000000000000000000000000000000000000001 +=> 1 + +-0.01 +=> 0 + +-0.000000000000000000000000000000000000000001 +=> 0 + +1 +=> 1 + +1.0000 +=> 1 + +1.0001 +=> 2 + +100000.000000000000000000000000000000000000000001 +=> 100001 + +-1 +=> -1 + +-1.0000 +=> -1 + +-1.0001 +=> -1 + +-100000.000000000000000000000000000000000000000001 +=> -100000 diff --git a/ext/bcmath/tests/bcfloor.phpt b/ext/bcmath/tests/bcfloor.phpt new file mode 100644 index 0000000000000..b163a2ba19bca --- /dev/null +++ b/ext/bcmath/tests/bcfloor.phpt @@ -0,0 +1,78 @@ +--TEST-- +bcfloor() function +--EXTENSIONS-- +bcmath +--FILE-- + '.bcfloor($num)."\n\n"; +} +?> +--EXPECT-- +0 +=> 0 + +0.00 +=> 0 + +-0 +=> 0 + +-0.00 +=> 0 + +0.01 +=> 0 + +0.000000000000000000000000000000000000000001 +=> 0 + +-0.01 +=> -1 + +-0.000000000000000000000000000000000000000001 +=> -1 + +1 +=> 1 + +1.0000 +=> 1 + +1.0001 +=> 1 + +100000.000000000000000000000000000000000000000001 +=> 100000 + +-1 +=> -1 + +-1.0000 +=> -1 + +-1.0001 +=> -2 + +-100000.000000000000000000000000000000000000000001 +=> -100001 diff --git a/ext/bcmath/tests/bcround.phpt b/ext/bcmath/tests/bcround.phpt new file mode 100644 index 0000000000000..2d545ec21c063 --- /dev/null +++ b/ext/bcmath/tests/bcround.phpt @@ -0,0 +1,827 @@ +--TEST-- +bcround() function +--EXTENSIONS-- +bcmath +--FILE-- + ".bcround($num, $precision, constant($mode))."\n"; +}; + +$early_return_cases = [ + ['123', -3], + ['123', -4], + ['123.123456', -3], + ['123.123456', -4], + ['123', 1], + ['123.5', 1], + ['123.5', 2], + ['123.0000000000000000000001', 22], + ['123.0000000000000000000001', 23], + ['-123', -3], + ['-123', -4], + ['-123.123456', -3], + ['-123.123456', -4], + ['-123', 1], + ['-123.5', 1], + ['-123.5', 2], + ['-123.0000000000000000000001', 22], + ['-123.0000000000000000000001', 23], + ['0', 0], + ['0.0', 0], + ['0.0000', 0], + ['-0', 0], + ['-0.0', 0], + ['-0.0000', 0], +]; + +echo "========== early return ==========\n"; +foreach ($early_return_cases as [$num, $precision]) { + $printResult($num, $precision, 34, 'PHP_ROUND_HALF_UP'); +} +echo "\n"; + +$not_boundary_value_cases = [ + ['1.1', 0], + ['1.2', 0], + ['1.3', 0], + ['1.4', 0], + ['1.6', 0], + ['1.7', 0], + ['1.8', 0], + ['1.9', 0], + ['-1.1', 0], + ['-1.2', 0], + ['-1.3', 0], + ['-1.4', 0], + ['-1.6', 0], + ['-1.7', 0], + ['-1.8', 0], + ['-1.9', 0], +]; + +echo "========== not boundary values ==========\n"; +foreach ($modes as $mode) { + echo "------ {$mode} ------\n"; + foreach ($not_boundary_value_cases as [$num, $precision]) { + $printResult($num, $precision, 20, $mode); + } + echo "\n"; +} + +$minus_precision_cases = [ + ['1230', -1], + ['1235', -1], + ['-1230', -1], + ['-1235', -1], + ['3400.0000', -2], + ['3400.0001', -2], + ['3450.0000', -2], + ['3450.0001', -2], + ['-3400.0000', -2], + ['-3400.0001', -2], + ['-3450.0000', -2], + ['-3450.0001', -2], +]; + +echo "========== minus precision ==========\n"; +foreach ($modes as $mode) { + echo "------ {$mode} ------\n"; + foreach ($minus_precision_cases as [$num, $precision]) { + $printResult($num, $precision, 20, $mode); + } + echo "\n"; +} + +$zero_precision_cases = [ + ['1235', 0], + ['1235.0', 0], + ['1235.000001', 0], + ['1235.5', 0], + ['1235.500001', 0], + ['-1235', 0], + ['-1235.0', 0], + ['-1235.000001', 0], + ['-1235.5', 0], + ['-1235.500001', 0], + ['0.0001', 0], + ['0.5', 0], + ['0.5000', 0], + ['0.5001', 0], + ['-0.0001', 0], + ['-0.5', 0], + ['-0.5000', 0], + ['-0.5001', 0], +]; + +echo "========== zero precision ==========\n"; +foreach ($modes as $mode) { + echo "------ {$mode} ------\n"; + foreach ($zero_precision_cases as [$num, $precision]) { + $printResult($num, $precision, 20, $mode); + } + echo "\n"; +} + +$plus_precision_cases = [ + ['28.40', 1], + ['28.4000001', 1], + ['28.45', 1], + ['28.4500001', 1], + ['-28.40', 1], + ['-28.4000001', 1], + ['-28.45', 1], + ['-28.4500001', 1], + ['153.90', 1], + ['153.9000001', 1], + ['153.95', 1], + ['153.9500001', 1], + ['-153.90', 1], + ['-153.9000001', 1], + ['-153.95', 1], + ['-153.9500001', 1], + ['0.000001', 3], + ['0.0005', 3], + ['0.000500', 3], + ['0.000501', 3], + ['-0.000001', 3], + ['-0.0005', 3], + ['-0.000500', 3], + ['-0.000501', 3], +]; + +echo "========== plus precision ==========\n"; +foreach ($modes as $mode) { + echo "------ {$mode} ------\n"; + foreach ($plus_precision_cases as [$num, $precision]) { + $printResult($num, $precision, 20, $mode); + } + echo "\n"; +} +?> +--EXPECT-- +========== early return ========== +[123, -3] => 0 +[123, -4] => 0 +[123.123456, -3] => 0 +[123.123456, -4] => 0 +[123, 1] => 123 +[123.5, 1] => 123.5 +[123.5, 2] => 123.5 +[123.0000000000000000000001, 22] => 123.0000000000000000000001 +[123.0000000000000000000001, 23] => 123.0000000000000000000001 +[-123, -3] => 0 +[-123, -4] => 0 +[-123.123456, -3] => 0 +[-123.123456, -4] => 0 +[-123, 1] => -123 +[-123.5, 1] => -123.5 +[-123.5, 2] => -123.5 +[-123.0000000000000000000001, 22] => -123.0000000000000000000001 +[-123.0000000000000000000001, 23] => -123.0000000000000000000001 +[0, 0] => 0 +[0.0, 0] => 0 +[0.0000, 0] => 0 +[-0, 0] => 0 +[-0.0, 0] => 0 +[-0.0000, 0] => 0 + +========== not boundary values ========== +------ PHP_ROUND_HALF_UP ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +------ PHP_ROUND_HALF_DOWN ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +------ PHP_ROUND_HALF_EVEN ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +------ PHP_ROUND_HALF_ODD ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +------ PHP_ROUND_CEILING ------ +[1.1, 0] => 2 +[1.2, 0] => 2 +[1.3, 0] => 2 +[1.4, 0] => 2 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -1 +[-1.7, 0] => -1 +[-1.8, 0] => -1 +[-1.9, 0] => -1 + +------ PHP_ROUND_FLOOR ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 1 +[1.7, 0] => 1 +[1.8, 0] => 1 +[1.9, 0] => 1 +[-1.1, 0] => -2 +[-1.2, 0] => -2 +[-1.3, 0] => -2 +[-1.4, 0] => -2 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +------ PHP_ROUND_TOWARD_ZERO ------ +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 1 +[1.7, 0] => 1 +[1.8, 0] => 1 +[1.9, 0] => 1 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -1 +[-1.7, 0] => -1 +[-1.8, 0] => -1 +[-1.9, 0] => -1 + +------ PHP_ROUND_AWAY_FROM_ZERO ------ +[1.1, 0] => 2 +[1.2, 0] => 2 +[1.3, 0] => 2 +[1.4, 0] => 2 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -2 +[-1.2, 0] => -2 +[-1.3, 0] => -2 +[-1.4, 0] => -2 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +------ PHP_ROUND_HALF_UP ------ +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +------ PHP_ROUND_HALF_DOWN ------ +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3500 + +------ PHP_ROUND_HALF_EVEN ------ +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3500 + +------ PHP_ROUND_HALF_ODD ------ +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +------ PHP_ROUND_CEILING ------ +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3500 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3400 + +------ PHP_ROUND_FLOOR ------ +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3400 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3500 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +------ PHP_ROUND_TOWARD_ZERO ------ +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3400 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3400 + +------ PHP_ROUND_AWAY_FROM_ZERO ------ +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3500 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3500 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +------ PHP_ROUND_HALF_UP ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +------ PHP_ROUND_HALF_DOWN ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => -1 + +------ PHP_ROUND_HALF_EVEN ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => -1 + +------ PHP_ROUND_HALF_ODD ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +------ PHP_ROUND_CEILING ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1236 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1235 +[0.0001, 0] => 1 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => 0 + +------ PHP_ROUND_FLOOR ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1235 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1236 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 0 +[-0.0001, 0] => -1 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +------ PHP_ROUND_TOWARD_ZERO ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1235 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1235 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 0 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => 0 + +------ PHP_ROUND_AWAY_FROM_ZERO ------ +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1236 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1236 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 1 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => -1 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +========== plus precision ========== +------ PHP_ROUND_HALF_UP ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 + +------ PHP_ROUND_HALF_DOWN ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => -0.001 + +------ PHP_ROUND_HALF_EVEN ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => -0.001 + +------ PHP_ROUND_HALF_ODD ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 + +------ PHP_ROUND_CEILING ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.5 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.4 +[153.90, 1] => 153.9 +[153.9000001, 1] => 154.0 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -153.9 +[0.000001, 3] => 0.001 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => 0.000 + +------ PHP_ROUND_FLOOR ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.4 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.5 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 153.9 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -154.0 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.000 +[-0.000001, 3] => -0.001 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 + +------ PHP_ROUND_TOWARD_ZERO ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.4 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.4 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 153.9 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -153.9 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.000 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => 0.000 + +------ PHP_ROUND_AWAY_FROM_ZERO ------ +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.5 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.5 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 154.0 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -154.0 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.001 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => -0.001 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 From 72b73a6c2923d84f932f307b1c44d6a30ef2e091 Mon Sep 17 00:00:00 2001 From: SakiTakamachi Date: Sat, 13 Jan 2024 23:49:54 +0900 Subject: [PATCH 02/18] add value error case to tests --- ext/bcmath/tests/bcceil.phpt | 17 +++++++++++++++++ ext/bcmath/tests/bcfloor.phpt | 17 +++++++++++++++++ ext/bcmath/tests/bcround.phpt | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/ext/bcmath/tests/bcceil.phpt b/ext/bcmath/tests/bcceil.phpt index 36a0afcae2b0c..a3284d07a0cdb 100644 --- a/ext/bcmath/tests/bcceil.phpt +++ b/ext/bcmath/tests/bcceil.phpt @@ -27,6 +27,19 @@ foreach ($nums as $num) { echo $num."\n"; echo '=> '.bcceil($num)."\n\n"; } + +echo "========== value error ==========\n"; +try { + bcceil('hoge'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} + +try { + bcceil('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} ?> --EXPECT-- 0 @@ -76,3 +89,7 @@ foreach ($nums as $num) { -100000.000000000000000000000000000000000000000001 => -100000 + +========== value error ========== +bcceil(): Argument #1 ($num) is not well-formed +bcceil(): Argument #1 ($num) is not well-formed diff --git a/ext/bcmath/tests/bcfloor.phpt b/ext/bcmath/tests/bcfloor.phpt index b163a2ba19bca..d2cd4121a6b06 100644 --- a/ext/bcmath/tests/bcfloor.phpt +++ b/ext/bcmath/tests/bcfloor.phpt @@ -27,6 +27,19 @@ foreach ($nums as $num) { echo $num."\n"; echo '=> '.bcfloor($num)."\n\n"; } + +echo "========== value error ==========\n"; +try { + bcfloor('hoge'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} + +try { + bcfloor('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} ?> --EXPECT-- 0 @@ -76,3 +89,7 @@ foreach ($nums as $num) { -100000.000000000000000000000000000000000000000001 => -100001 + +========== value error ========== +bcfloor(): Argument #1 ($num) is not well-formed +bcfloor(): Argument #1 ($num) is not well-formed diff --git a/ext/bcmath/tests/bcround.phpt b/ext/bcmath/tests/bcround.phpt index 2d545ec21c063..35f7e98e18060 100644 --- a/ext/bcmath/tests/bcround.phpt +++ b/ext/bcmath/tests/bcround.phpt @@ -170,6 +170,19 @@ foreach ($modes as $mode) { } echo "\n"; } + +echo "========== value error ==========\n"; +try { + bcround('hoge'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} + +try { + bcround('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} ?> --EXPECT-- ========== early return ========== @@ -825,3 +838,7 @@ foreach ($modes as $mode) { [-0.0005, 3] => -0.001 [-0.000500, 3] => -0.001 [-0.000501, 3] => -0.001 + +========== value error ========== +bcround(): Argument #1 ($num) is not well-formed +bcround(): Argument #1 ($num) is not well-formed From 2f3f2e5689ea4d7769a225019b23bf3c6869bebd Mon Sep 17 00:00:00 2001 From: SakiTakamachi Date: Sun, 14 Jan 2024 11:28:12 +0900 Subject: [PATCH 03/18] Address review comments --- ext/bcmath/libbcmath/src/floor_or_ceil.c | 16 ++++++---------- ext/bcmath/libbcmath/src/round.c | 2 +- ext/bcmath/tests/bcround.phpt | 7 +++++++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ext/bcmath/libbcmath/src/floor_or_ceil.c b/ext/bcmath/libbcmath/src/floor_or_ceil.c index 7716d07dd2d9f..f46345f05cf91 100644 --- a/ext/bcmath/libbcmath/src/floor_or_ceil.c +++ b/ext/bcmath/libbcmath/src/floor_or_ceil.c @@ -35,8 +35,6 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) { - bc_num fractional; - /* clear result */ bc_free_num(result); @@ -51,12 +49,13 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) return; } - /* copy fractional part */ - fractional = bc_new_num(0, num->n_scale); - memcpy(fractional->n_value, num->n_value + num->n_len, num->n_scale); + /* check fractional part. */ + size_t count = num->n_scale; + char *nptr = num->n_value + num->n_len; + while ((count > 0) && (*nptr++ == 0)) count--; - if (bc_is_zero(fractional)) { - goto cleanup; + if (count == 0) { + return; } /* add/sub 1 to/from result */ @@ -64,7 +63,4 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) tmp->n_sign = (*result)->n_sign; bc_free_num(result); *result = tmp; - -cleanup: - bc_free_num(&fractional); } diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index e483053264b59..b4f6ac7cc62e1 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -49,7 +49,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) * end, the 0's are omitted and the number of digits in num is reduced. * In that case, may end up in the same situation as 2. */ - if ((int) num->n_len + precision <= 0) { + if (precision < 0 && num->n_len <= labs(precision)) { *result = bc_copy_num(BCG(_zero_)); return; } diff --git a/ext/bcmath/tests/bcround.phpt b/ext/bcmath/tests/bcround.phpt index 35f7e98e18060..a8a800866901d 100644 --- a/ext/bcmath/tests/bcround.phpt +++ b/ext/bcmath/tests/bcround.phpt @@ -183,6 +183,12 @@ try { } catch (Throwable $e) { echo $e->getMessage()."\n"; } + +try { + bcround('0.001', 0, 1000); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} ?> --EXPECT-- ========== early return ========== @@ -842,3 +848,4 @@ try { ========== value error ========== bcround(): Argument #1 ($num) is not well-formed bcround(): Argument #1 ($num) is not well-formed +bcround(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) From 7549f57d23454b0401ab3c57b6d613f0f78e6881 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 10 Mar 2024 11:26:08 +0900 Subject: [PATCH 04/18] Address comments 1 --- ext/bcmath/libbcmath/src/floor_or_ceil.c | 11 ++++++++--- ext/bcmath/libbcmath/src/round.c | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ext/bcmath/libbcmath/src/floor_or_ceil.c b/ext/bcmath/libbcmath/src/floor_or_ceil.c index f46345f05cf91..ee4db8e8f4049 100644 --- a/ext/bcmath/libbcmath/src/floor_or_ceil.c +++ b/ext/bcmath/libbcmath/src/floor_or_ceil.c @@ -45,20 +45,25 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) /* copy integer part */ memcpy((*result)->n_value, num->n_value, num->n_len); + /* If the number is positive and we are flooring, then nothing else needs to be done. + * Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */ if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) { return; } /* check fractional part. */ size_t count = num->n_scale; - char *nptr = num->n_value + num->n_len; - while ((count > 0) && (*nptr++ == 0)) count--; + const char *nptr = num->n_value + num->n_len; + while ((count > 0) && (*nptr++ == 0)) { + count--; + } + /* If all digits past the decimal point are 0 */ if (count == 0) { return; } - /* add/sub 1 to/from result */ + /* Increment the absolute value of the result by 1 and add sign information */ bc_num tmp = _bc_do_add(*result, BCG(_one_), 0); tmp->n_sign = (*result)->n_sign; bc_free_num(result); diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index b4f6ac7cc62e1..71a687c80ae15 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -49,7 +49,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) * end, the 0's are omitted and the number of digits in num is reduced. * In that case, may end up in the same situation as 2. */ - if (precision < 0 && num->n_len <= labs(precision)) { + if (precision < 0 && num->n_len <= (size_t) (-(precision + Z_L(1))) + 1) { *result = bc_copy_num(BCG(_zero_)); return; } @@ -68,7 +68,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) size_t rounded_len = num->n_len + precision; memcpy((*result)->n_value, num->n_value, rounded_len); - char *nptr = num->n_value + rounded_len; + const char *nptr = num->n_value + rounded_len; /* Check cases that can be determined without looping. */ switch (mode) { @@ -88,7 +88,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) } else if (*nptr < 5) { return; } - /* if *nptr == 5, a loop is required for judgment. */ + /* if *nptr == 5, we need to look-up further digits before making a decision. */ break; case PHP_ROUND_CEILING: @@ -118,12 +118,16 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) } /* if *nptr == 0, a loop is required for judgment. */ break; + + EMPTY_SWITCH_DEFAULT_CASE() } /* Loop through the remaining digits. */ size_t count = num->n_len + num->n_scale - rounded_len - 1; nptr++; - while ((count > 0) && (*nptr++ == 0)) count--; + while ((count > 0) && (*nptr++ == 0)) { + count--; + } if (count > 0) { goto up; @@ -147,6 +151,8 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) return; } break; + + EMPTY_SWITCH_DEFAULT_CASE() } up: From ad5247fd3655445c772803356a9fd92c33573ab1 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 10 Mar 2024 13:45:29 +0900 Subject: [PATCH 05/18] splitted the test --- ext/bcmath/tests/bcceil.phpt | 83 +- ext/bcmath/tests/bcceil_error.phpt | 21 + ext/bcmath/tests/bcfloor.phpt | 83 +- ext/bcmath/tests/bcfloor_error.phpt | 21 + ext/bcmath/tests/bcround.phpt | 851 ------------------- ext/bcmath/tests/bcround_away_from_zero.phpt | 87 ++ ext/bcmath/tests/bcround_ceiling.phpt | 87 ++ ext/bcmath/tests/bcround_early_return.phpt | 62 ++ ext/bcmath/tests/bcround_error.phpt | 28 + ext/bcmath/tests/bcround_floor.phpt | 87 ++ ext/bcmath/tests/bcround_half_down.phpt | 87 ++ ext/bcmath/tests/bcround_half_even.phpt | 87 ++ ext/bcmath/tests/bcround_half_odd.phpt | 87 ++ ext/bcmath/tests/bcround_half_up.phpt | 87 ++ ext/bcmath/tests/bcround_test_helper.inc | 106 +++ ext/bcmath/tests/bcround_toward_zero.phpt | 87 ++ 16 files changed, 968 insertions(+), 983 deletions(-) create mode 100644 ext/bcmath/tests/bcceil_error.phpt create mode 100644 ext/bcmath/tests/bcfloor_error.phpt delete mode 100644 ext/bcmath/tests/bcround.phpt create mode 100644 ext/bcmath/tests/bcround_away_from_zero.phpt create mode 100644 ext/bcmath/tests/bcround_ceiling.phpt create mode 100644 ext/bcmath/tests/bcround_early_return.phpt create mode 100644 ext/bcmath/tests/bcround_error.phpt create mode 100644 ext/bcmath/tests/bcround_floor.phpt create mode 100644 ext/bcmath/tests/bcround_half_down.phpt create mode 100644 ext/bcmath/tests/bcround_half_even.phpt create mode 100644 ext/bcmath/tests/bcround_half_odd.phpt create mode 100644 ext/bcmath/tests/bcround_half_up.phpt create mode 100644 ext/bcmath/tests/bcround_test_helper.inc create mode 100644 ext/bcmath/tests/bcround_toward_zero.phpt diff --git a/ext/bcmath/tests/bcceil.phpt b/ext/bcmath/tests/bcceil.phpt index a3284d07a0cdb..a960e3ef2674b 100644 --- a/ext/bcmath/tests/bcceil.phpt +++ b/ext/bcmath/tests/bcceil.phpt @@ -24,72 +24,23 @@ $nums = [ ]; foreach ($nums as $num) { - echo $num."\n"; - echo '=> '.bcceil($num)."\n\n"; -} - -echo "========== value error ==========\n"; -try { - bcceil('hoge'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; -} - -try { - bcceil('0.00.1'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; + echo str_pad("$num", 50), ' => ', bcceil($num), "\n"; } ?> --EXPECT-- -0 -=> 0 - -0.00 -=> 0 - --0 -=> 0 - --0.00 -=> 0 - -0.01 -=> 1 - -0.000000000000000000000000000000000000000001 -=> 1 - --0.01 -=> 0 - --0.000000000000000000000000000000000000000001 -=> 0 - -1 -=> 1 - -1.0000 -=> 1 - -1.0001 -=> 2 - -100000.000000000000000000000000000000000000000001 -=> 100001 - --1 -=> -1 - --1.0000 -=> -1 - --1.0001 -=> -1 - --100000.000000000000000000000000000000000000000001 -=> -100000 - -========== value error ========== -bcceil(): Argument #1 ($num) is not well-formed -bcceil(): Argument #1 ($num) is not well-formed +0 => 0 +0.00 => 0 +-0 => 0 +-0.00 => 0 +0.01 => 1 +0.000000000000000000000000000000000000000001 => 1 +-0.01 => 0 +-0.000000000000000000000000000000000000000001 => 0 +1 => 1 +1.0000 => 1 +1.0001 => 2 +100000.000000000000000000000000000000000000000001 => 100001 +-1 => -1 +-1.0000 => -1 +-1.0001 => -1 +-100000.000000000000000000000000000000000000000001 => -100000 diff --git a/ext/bcmath/tests/bcceil_error.phpt b/ext/bcmath/tests/bcceil_error.phpt new file mode 100644 index 0000000000000..fd4c51ff6c586 --- /dev/null +++ b/ext/bcmath/tests/bcceil_error.phpt @@ -0,0 +1,21 @@ +--TEST-- +bcceil() function with error +--EXTENSIONS-- +bcmath +--FILE-- +getMessage()."\n"; +} + +try { + bcceil('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +bcceil(): Argument #1 ($num) is not well-formed +bcceil(): Argument #1 ($num) is not well-formed diff --git a/ext/bcmath/tests/bcfloor.phpt b/ext/bcmath/tests/bcfloor.phpt index d2cd4121a6b06..1f245f0fe84bc 100644 --- a/ext/bcmath/tests/bcfloor.phpt +++ b/ext/bcmath/tests/bcfloor.phpt @@ -24,72 +24,23 @@ $nums = [ ]; foreach ($nums as $num) { - echo $num."\n"; - echo '=> '.bcfloor($num)."\n\n"; -} - -echo "========== value error ==========\n"; -try { - bcfloor('hoge'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; -} - -try { - bcfloor('0.00.1'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; + echo str_pad("$num", 50), ' => ', bcfloor($num), "\n"; } ?> --EXPECT-- -0 -=> 0 - -0.00 -=> 0 - --0 -=> 0 - --0.00 -=> 0 - -0.01 -=> 0 - -0.000000000000000000000000000000000000000001 -=> 0 - --0.01 -=> -1 - --0.000000000000000000000000000000000000000001 -=> -1 - -1 -=> 1 - -1.0000 -=> 1 - -1.0001 -=> 1 - -100000.000000000000000000000000000000000000000001 -=> 100000 - --1 -=> -1 - --1.0000 -=> -1 - --1.0001 -=> -2 - --100000.000000000000000000000000000000000000000001 -=> -100001 - -========== value error ========== -bcfloor(): Argument #1 ($num) is not well-formed -bcfloor(): Argument #1 ($num) is not well-formed +0 => 0 +0.00 => 0 +-0 => 0 +-0.00 => 0 +0.01 => 0 +0.000000000000000000000000000000000000000001 => 0 +-0.01 => -1 +-0.000000000000000000000000000000000000000001 => -1 +1 => 1 +1.0000 => 1 +1.0001 => 1 +100000.000000000000000000000000000000000000000001 => 100000 +-1 => -1 +-1.0000 => -1 +-1.0001 => -2 +-100000.000000000000000000000000000000000000000001 => -100001 diff --git a/ext/bcmath/tests/bcfloor_error.phpt b/ext/bcmath/tests/bcfloor_error.phpt new file mode 100644 index 0000000000000..7578a5afe3b8d --- /dev/null +++ b/ext/bcmath/tests/bcfloor_error.phpt @@ -0,0 +1,21 @@ +--TEST-- +bcfloor() function with error +--EXTENSIONS-- +bcmath +--FILE-- +getMessage()."\n"; +} + +try { + bcfloor('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +bcfloor(): Argument #1 ($num) is not well-formed +bcfloor(): Argument #1 ($num) is not well-formed diff --git a/ext/bcmath/tests/bcround.phpt b/ext/bcmath/tests/bcround.phpt deleted file mode 100644 index a8a800866901d..0000000000000 --- a/ext/bcmath/tests/bcround.phpt +++ /dev/null @@ -1,851 +0,0 @@ ---TEST-- -bcround() function ---EXTENSIONS-- -bcmath ---FILE-- - ".bcround($num, $precision, constant($mode))."\n"; -}; - -$early_return_cases = [ - ['123', -3], - ['123', -4], - ['123.123456', -3], - ['123.123456', -4], - ['123', 1], - ['123.5', 1], - ['123.5', 2], - ['123.0000000000000000000001', 22], - ['123.0000000000000000000001', 23], - ['-123', -3], - ['-123', -4], - ['-123.123456', -3], - ['-123.123456', -4], - ['-123', 1], - ['-123.5', 1], - ['-123.5', 2], - ['-123.0000000000000000000001', 22], - ['-123.0000000000000000000001', 23], - ['0', 0], - ['0.0', 0], - ['0.0000', 0], - ['-0', 0], - ['-0.0', 0], - ['-0.0000', 0], -]; - -echo "========== early return ==========\n"; -foreach ($early_return_cases as [$num, $precision]) { - $printResult($num, $precision, 34, 'PHP_ROUND_HALF_UP'); -} -echo "\n"; - -$not_boundary_value_cases = [ - ['1.1', 0], - ['1.2', 0], - ['1.3', 0], - ['1.4', 0], - ['1.6', 0], - ['1.7', 0], - ['1.8', 0], - ['1.9', 0], - ['-1.1', 0], - ['-1.2', 0], - ['-1.3', 0], - ['-1.4', 0], - ['-1.6', 0], - ['-1.7', 0], - ['-1.8', 0], - ['-1.9', 0], -]; - -echo "========== not boundary values ==========\n"; -foreach ($modes as $mode) { - echo "------ {$mode} ------\n"; - foreach ($not_boundary_value_cases as [$num, $precision]) { - $printResult($num, $precision, 20, $mode); - } - echo "\n"; -} - -$minus_precision_cases = [ - ['1230', -1], - ['1235', -1], - ['-1230', -1], - ['-1235', -1], - ['3400.0000', -2], - ['3400.0001', -2], - ['3450.0000', -2], - ['3450.0001', -2], - ['-3400.0000', -2], - ['-3400.0001', -2], - ['-3450.0000', -2], - ['-3450.0001', -2], -]; - -echo "========== minus precision ==========\n"; -foreach ($modes as $mode) { - echo "------ {$mode} ------\n"; - foreach ($minus_precision_cases as [$num, $precision]) { - $printResult($num, $precision, 20, $mode); - } - echo "\n"; -} - -$zero_precision_cases = [ - ['1235', 0], - ['1235.0', 0], - ['1235.000001', 0], - ['1235.5', 0], - ['1235.500001', 0], - ['-1235', 0], - ['-1235.0', 0], - ['-1235.000001', 0], - ['-1235.5', 0], - ['-1235.500001', 0], - ['0.0001', 0], - ['0.5', 0], - ['0.5000', 0], - ['0.5001', 0], - ['-0.0001', 0], - ['-0.5', 0], - ['-0.5000', 0], - ['-0.5001', 0], -]; - -echo "========== zero precision ==========\n"; -foreach ($modes as $mode) { - echo "------ {$mode} ------\n"; - foreach ($zero_precision_cases as [$num, $precision]) { - $printResult($num, $precision, 20, $mode); - } - echo "\n"; -} - -$plus_precision_cases = [ - ['28.40', 1], - ['28.4000001', 1], - ['28.45', 1], - ['28.4500001', 1], - ['-28.40', 1], - ['-28.4000001', 1], - ['-28.45', 1], - ['-28.4500001', 1], - ['153.90', 1], - ['153.9000001', 1], - ['153.95', 1], - ['153.9500001', 1], - ['-153.90', 1], - ['-153.9000001', 1], - ['-153.95', 1], - ['-153.9500001', 1], - ['0.000001', 3], - ['0.0005', 3], - ['0.000500', 3], - ['0.000501', 3], - ['-0.000001', 3], - ['-0.0005', 3], - ['-0.000500', 3], - ['-0.000501', 3], -]; - -echo "========== plus precision ==========\n"; -foreach ($modes as $mode) { - echo "------ {$mode} ------\n"; - foreach ($plus_precision_cases as [$num, $precision]) { - $printResult($num, $precision, 20, $mode); - } - echo "\n"; -} - -echo "========== value error ==========\n"; -try { - bcround('hoge'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; -} - -try { - bcround('0.00.1'); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; -} - -try { - bcround('0.001', 0, 1000); -} catch (Throwable $e) { - echo $e->getMessage()."\n"; -} -?> ---EXPECT-- -========== early return ========== -[123, -3] => 0 -[123, -4] => 0 -[123.123456, -3] => 0 -[123.123456, -4] => 0 -[123, 1] => 123 -[123.5, 1] => 123.5 -[123.5, 2] => 123.5 -[123.0000000000000000000001, 22] => 123.0000000000000000000001 -[123.0000000000000000000001, 23] => 123.0000000000000000000001 -[-123, -3] => 0 -[-123, -4] => 0 -[-123.123456, -3] => 0 -[-123.123456, -4] => 0 -[-123, 1] => -123 -[-123.5, 1] => -123.5 -[-123.5, 2] => -123.5 -[-123.0000000000000000000001, 22] => -123.0000000000000000000001 -[-123.0000000000000000000001, 23] => -123.0000000000000000000001 -[0, 0] => 0 -[0.0, 0] => 0 -[0.0000, 0] => 0 -[-0, 0] => 0 -[-0.0, 0] => 0 -[-0.0000, 0] => 0 - -========== not boundary values ========== ------- PHP_ROUND_HALF_UP ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - ------- PHP_ROUND_HALF_DOWN ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - ------- PHP_ROUND_HALF_EVEN ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - ------- PHP_ROUND_HALF_ODD ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - ------- PHP_ROUND_CEILING ------ -[1.1, 0] => 2 -[1.2, 0] => 2 -[1.3, 0] => 2 -[1.4, 0] => 2 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -1 -[-1.7, 0] => -1 -[-1.8, 0] => -1 -[-1.9, 0] => -1 - ------- PHP_ROUND_FLOOR ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 1 -[1.7, 0] => 1 -[1.8, 0] => 1 -[1.9, 0] => 1 -[-1.1, 0] => -2 -[-1.2, 0] => -2 -[-1.3, 0] => -2 -[-1.4, 0] => -2 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - ------- PHP_ROUND_TOWARD_ZERO ------ -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 1 -[1.7, 0] => 1 -[1.8, 0] => 1 -[1.9, 0] => 1 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -1 -[-1.7, 0] => -1 -[-1.8, 0] => -1 -[-1.9, 0] => -1 - ------- PHP_ROUND_AWAY_FROM_ZERO ------ -[1.1, 0] => 2 -[1.2, 0] => 2 -[1.3, 0] => 2 -[1.4, 0] => 2 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -2 -[-1.2, 0] => -2 -[-1.3, 0] => -2 -[-1.4, 0] => -2 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 - -========== minus precision ========== ------- PHP_ROUND_HALF_UP ------ -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 - ------- PHP_ROUND_HALF_DOWN ------ -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3500 - ------- PHP_ROUND_HALF_EVEN ------ -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3500 - ------- PHP_ROUND_HALF_ODD ------ -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 - ------- PHP_ROUND_CEILING ------ -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3500 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3400 - ------- PHP_ROUND_FLOOR ------ -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3400 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3500 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 - ------- PHP_ROUND_TOWARD_ZERO ------ -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3400 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3400 - ------- PHP_ROUND_AWAY_FROM_ZERO ------ -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3500 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3500 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 - -========== zero precision ========== ------- PHP_ROUND_HALF_UP ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1236 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 - ------- PHP_ROUND_HALF_DOWN ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => -1 - ------- PHP_ROUND_HALF_EVEN ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1236 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => -1 - ------- PHP_ROUND_HALF_ODD ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 - ------- PHP_ROUND_CEILING ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1236 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 -[-1235.500001, 0] => -1235 -[0.0001, 0] => 1 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => 0 - ------- PHP_ROUND_FLOOR ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1235 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1236 -[-1235.5, 0] => -1236 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 0 -[-0.0001, 0] => -1 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 - ------- PHP_ROUND_TOWARD_ZERO ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1235 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 -[-1235.500001, 0] => -1235 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 0 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => 0 - ------- PHP_ROUND_AWAY_FROM_ZERO ------ -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1236 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 -[-1235.000001, 0] => -1236 -[-1235.5, 0] => -1236 -[-1235.500001, 0] => -1236 -[0.0001, 0] => 1 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => -1 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 - -========== plus precision ========== ------- PHP_ROUND_HALF_UP ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -154.0 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 - ------- PHP_ROUND_HALF_DOWN ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => -0.001 - ------- PHP_ROUND_HALF_EVEN ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -154.0 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => -0.001 - ------- PHP_ROUND_HALF_ODD ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 - ------- PHP_ROUND_CEILING ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.5 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.4 -[153.90, 1] => 153.9 -[153.9000001, 1] => 154.0 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 -[-153.9500001, 1] => -153.9 -[0.000001, 3] => 0.001 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => 0.000 - ------- PHP_ROUND_FLOOR ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.4 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.5 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 153.9 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -154.0 -[-153.95, 1] => -154.0 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.000 -[-0.000001, 3] => -0.001 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 - ------- PHP_ROUND_TOWARD_ZERO ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.4 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.4 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 153.9 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 -[-153.9500001, 1] => -153.9 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.000 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => 0.000 - ------- PHP_ROUND_AWAY_FROM_ZERO ------ -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.5 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.5 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 154.0 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 -[-153.9000001, 1] => -154.0 -[-153.95, 1] => -154.0 -[-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.001 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => -0.001 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 - -========== value error ========== -bcround(): Argument #1 ($num) is not well-formed -bcround(): Argument #1 ($num) is not well-formed -bcround(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) diff --git a/ext/bcmath/tests/bcround_away_from_zero.phpt b/ext/bcmath/tests/bcround_away_from_zero.phpt new file mode 100644 index 0000000000000..ce9e7dc3a98de --- /dev/null +++ b/ext/bcmath/tests/bcround_away_from_zero.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_AWAY_FROM_ZERO +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 2 +[1.2, 0] => 2 +[1.3, 0] => 2 +[1.4, 0] => 2 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -2 +[-1.2, 0] => -2 +[-1.3, 0] => -2 +[-1.4, 0] => -2 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3500 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3500 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1236 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1236 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 1 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => -1 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.5 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.5 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 154.0 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -154.0 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.001 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => -0.001 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_ceiling.phpt b/ext/bcmath/tests/bcround_ceiling.phpt new file mode 100644 index 0000000000000..edda077cdbfdc --- /dev/null +++ b/ext/bcmath/tests/bcround_ceiling.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_CEILING +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 2 +[1.2, 0] => 2 +[1.3, 0] => 2 +[1.4, 0] => 2 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -1 +[-1.7, 0] => -1 +[-1.8, 0] => -1 +[-1.9, 0] => -1 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3500 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3400 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1236 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1235 +[0.0001, 0] => 1 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => 0 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.5 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.4 +[153.90, 1] => 153.9 +[153.9000001, 1] => 154.0 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -153.9 +[0.000001, 3] => 0.001 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => 0.000 diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt new file mode 100644 index 0000000000000..6e5d1dbb030e9 --- /dev/null +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -0,0 +1,62 @@ +--TEST-- +bcround() function with early return +--EXTENSIONS-- +bcmath +--FILE-- + ", bcround($num, $precision, PHP_ROUND_HALF_UP), "\n"; +} +?> +--EXPECT-- +[123, -3] => 0 +[123, -4] => 0 +[123.123456, -3] => 0 +[123.123456, -4] => 0 +[123, 1] => 123 +[123.5, 1] => 123.5 +[123.5, 2] => 123.5 +[123.0000000000000000000001, 22] => 123.0000000000000000000001 +[123.0000000000000000000001, 23] => 123.0000000000000000000001 +[-123, -3] => 0 +[-123, -4] => 0 +[-123.123456, -3] => 0 +[-123.123456, -4] => 0 +[-123, 1] => -123 +[-123.5, 1] => -123.5 +[-123.5, 2] => -123.5 +[-123.0000000000000000000001, 22] => -123.0000000000000000000001 +[-123.0000000000000000000001, 23] => -123.0000000000000000000001 +[0, 0] => 0 +[0.0, 0] => 0 +[0.0000, 0] => 0 +[-0, 0] => 0 +[-0.0, 0] => 0 +[-0.0000, 0] => 0 diff --git a/ext/bcmath/tests/bcround_error.phpt b/ext/bcmath/tests/bcround_error.phpt new file mode 100644 index 0000000000000..d411763b49326 --- /dev/null +++ b/ext/bcmath/tests/bcround_error.phpt @@ -0,0 +1,28 @@ +--TEST-- +bcround() function with error +--EXTENSIONS-- +bcmath +--FILE-- +getMessage()."\n"; +} + +try { + bcround('0.00.1'); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} + +try { + bcround('0.001', 0, 1000); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +bcround(): Argument #1 ($num) is not well-formed +bcround(): Argument #1 ($num) is not well-formed +bcround(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) diff --git a/ext/bcmath/tests/bcround_floor.phpt b/ext/bcmath/tests/bcround_floor.phpt new file mode 100644 index 0000000000000..dc45a66b3df83 --- /dev/null +++ b/ext/bcmath/tests/bcround_floor.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_FLOOR +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 1 +[1.7, 0] => 1 +[1.8, 0] => 1 +[1.9, 0] => 1 +[-1.1, 0] => -2 +[-1.2, 0] => -2 +[-1.3, 0] => -2 +[-1.4, 0] => -2 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3400 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3500 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1235 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1236 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 0 +[-0.0001, 0] => -1 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.4 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.5 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 153.9 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -154.0 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.000 +[-0.000001, 3] => -0.001 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_down.phpt b/ext/bcmath/tests/bcround_half_down.phpt new file mode 100644 index 0000000000000..7b626079a1d17 --- /dev/null +++ b/ext/bcmath/tests/bcround_half_down.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_HALF_DOWN +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_even.phpt b/ext/bcmath/tests/bcround_half_even.phpt new file mode 100644 index 0000000000000..e9548ed166962 --- /dev/null +++ b/ext/bcmath/tests/bcround_half_even.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_HALF_EVEN +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_odd.phpt b/ext/bcmath/tests/bcround_half_odd.phpt new file mode 100644 index 0000000000000..9e1d11e3726ce --- /dev/null +++ b/ext/bcmath/tests/bcround_half_odd.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_HALF_ODD +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_up.phpt b/ext/bcmath/tests/bcround_half_up.phpt new file mode 100644 index 0000000000000..66aeeba45f169 --- /dev/null +++ b/ext/bcmath/tests/bcround_half_up.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_HALF_UP +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 2 +[1.7, 0] => 2 +[1.8, 0] => 2 +[1.9, 0] => 2 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -2 +[-1.7, 0] => -2 +[-1.8, 0] => -2 +[-1.9, 0] => -2 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1240 +[-1230, -1] => -1230 +[-1235, -1] => -1240 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3500 +[3450.0001, -2] => 3500 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3500 +[-3450.0001, -2] => -3500 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1236 +[1235.500001, 0] => 1236 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1236 +[-1235.500001, 0] => -1236 +[0.0001, 0] => 0 +[0.5, 0] => 1 +[0.5000, 0] => 1 +[0.5001, 0] => 1 +[-0.0001, 0] => 0 +[-0.5, 0] => -1 +[-0.5000, 0] => -1 +[-0.5001, 0] => -1 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.5 +[28.4500001, 1] => 28.5 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.5 +[-28.4500001, 1] => -28.5 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 154.0 +[153.9500001, 1] => 154.0 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -154.0 +[-153.9500001, 1] => -154.0 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.001 +[0.000500, 3] => 0.001 +[0.000501, 3] => 0.001 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => -0.001 +[-0.000500, 3] => -0.001 +[-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_test_helper.inc b/ext/bcmath/tests/bcround_test_helper.inc new file mode 100644 index 0000000000000..92439bbfb7e7c --- /dev/null +++ b/ext/bcmath/tests/bcround_test_helper.inc @@ -0,0 +1,106 @@ + ", bcround($num, $precision, $mode), "\n"; + } + echo "\n"; +} + +function run_round_test(int $mode) +{ + $non_boundary_value_cases = [ + ['1.1', 0], + ['1.2', 0], + ['1.3', 0], + ['1.4', 0], + ['1.6', 0], + ['1.7', 0], + ['1.8', 0], + ['1.9', 0], + ['-1.1', 0], + ['-1.2', 0], + ['-1.3', 0], + ['-1.4', 0], + ['-1.6', 0], + ['-1.7', 0], + ['-1.8', 0], + ['-1.9', 0], + ]; + + $minus_precision_cases = [ + ['1230', -1], + ['1235', -1], + ['-1230', -1], + ['-1235', -1], + ['3400.0000', -2], + ['3400.0001', -2], + ['3450.0000', -2], + ['3450.0001', -2], + ['-3400.0000', -2], + ['-3400.0001', -2], + ['-3450.0000', -2], + ['-3450.0001', -2], + ]; + + $zero_precision_cases = [ + ['1235', 0], + ['1235.0', 0], + ['1235.000001', 0], + ['1235.5', 0], + ['1235.500001', 0], + ['-1235', 0], + ['-1235.0', 0], + ['-1235.000001', 0], + ['-1235.5', 0], + ['-1235.500001', 0], + ['0.0001', 0], + ['0.5', 0], + ['0.5000', 0], + ['0.5001', 0], + ['-0.0001', 0], + ['-0.5', 0], + ['-0.5000', 0], + ['-0.5001', 0], + ]; + + $plus_precision_cases = [ + ['28.40', 1], + ['28.4000001', 1], + ['28.45', 1], + ['28.4500001', 1], + ['-28.40', 1], + ['-28.4000001', 1], + ['-28.45', 1], + ['-28.4500001', 1], + ['153.90', 1], + ['153.9000001', 1], + ['153.95', 1], + ['153.9500001', 1], + ['-153.90', 1], + ['-153.9000001', 1], + ['-153.95', 1], + ['-153.9500001', 1], + ['0.000001', 3], + ['0.0005', 3], + ['0.000500', 3], + ['0.000501', 3], + ['-0.000001', 3], + ['-0.0005', 3], + ['-0.000500', 3], + ['-0.000501', 3], + ]; + + echo "========== non-boundary value ==========\n"; + printResult($non_boundary_value_cases, $mode); + + echo "========== minus precision ==========\n"; + printResult($minus_precision_cases, $mode); + + echo "========== zero precision ==========\n"; + printResult($zero_precision_cases, $mode); + + echo "========== plus precision ==========\n"; + printResult($plus_precision_cases, $mode); +} diff --git a/ext/bcmath/tests/bcround_toward_zero.phpt b/ext/bcmath/tests/bcround_toward_zero.phpt new file mode 100644 index 0000000000000..12c22901c8025 --- /dev/null +++ b/ext/bcmath/tests/bcround_toward_zero.phpt @@ -0,0 +1,87 @@ +--TEST-- +bcround() function PHP_ROUND_TOWARD_ZERO +--EXTENSIONS-- +bcmath +--FILE-- + +--EXPECT-- +========== non-boundary value ========== +[1.1, 0] => 1 +[1.2, 0] => 1 +[1.3, 0] => 1 +[1.4, 0] => 1 +[1.6, 0] => 1 +[1.7, 0] => 1 +[1.8, 0] => 1 +[1.9, 0] => 1 +[-1.1, 0] => -1 +[-1.2, 0] => -1 +[-1.3, 0] => -1 +[-1.4, 0] => -1 +[-1.6, 0] => -1 +[-1.7, 0] => -1 +[-1.8, 0] => -1 +[-1.9, 0] => -1 + +========== minus precision ========== +[1230, -1] => 1230 +[1235, -1] => 1230 +[-1230, -1] => -1230 +[-1235, -1] => -1230 +[3400.0000, -2] => 3400 +[3400.0001, -2] => 3400 +[3450.0000, -2] => 3400 +[3450.0001, -2] => 3400 +[-3400.0000, -2] => -3400 +[-3400.0001, -2] => -3400 +[-3450.0000, -2] => -3400 +[-3450.0001, -2] => -3400 + +========== zero precision ========== +[1235, 0] => 1235 +[1235.0, 0] => 1235 +[1235.000001, 0] => 1235 +[1235.5, 0] => 1235 +[1235.500001, 0] => 1235 +[-1235, 0] => -1235 +[-1235.0, 0] => -1235 +[-1235.000001, 0] => -1235 +[-1235.5, 0] => -1235 +[-1235.500001, 0] => -1235 +[0.0001, 0] => 0 +[0.5, 0] => 0 +[0.5000, 0] => 0 +[0.5001, 0] => 0 +[-0.0001, 0] => 0 +[-0.5, 0] => 0 +[-0.5000, 0] => 0 +[-0.5001, 0] => 0 + +========== plus precision ========== +[28.40, 1] => 28.4 +[28.4000001, 1] => 28.4 +[28.45, 1] => 28.4 +[28.4500001, 1] => 28.4 +[-28.40, 1] => -28.4 +[-28.4000001, 1] => -28.4 +[-28.45, 1] => -28.4 +[-28.4500001, 1] => -28.4 +[153.90, 1] => 153.9 +[153.9000001, 1] => 153.9 +[153.95, 1] => 153.9 +[153.9500001, 1] => 153.9 +[-153.90, 1] => -153.9 +[-153.9000001, 1] => -153.9 +[-153.95, 1] => -153.9 +[-153.9500001, 1] => -153.9 +[0.000001, 3] => 0.000 +[0.0005, 3] => 0.000 +[0.000500, 3] => 0.000 +[0.000501, 3] => 0.000 +[-0.000001, 3] => 0.000 +[-0.0005, 3] => 0.000 +[-0.000500, 3] => 0.000 +[-0.000501, 3] => 0.000 From c5cbc18bbf3b0355caf82d474db49c21a668fb1e Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 12 Mar 2024 23:38:32 +0900 Subject: [PATCH 06/18] Changed expected value to STR_PAD_LEFT --- ext/bcmath/tests/bcceil.phpt | 32 ++--- ext/bcmath/tests/bcfloor.phpt | 32 ++--- ext/bcmath/tests/bcround_away_from_zero.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_ceiling.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_early_return.phpt | 46 +++---- ext/bcmath/tests/bcround_floor.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_half_down.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_half_even.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_half_odd.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_half_up.phpt | 132 +++++++++---------- ext/bcmath/tests/bcround_test_helper.inc | 2 +- 11 files changed, 518 insertions(+), 518 deletions(-) diff --git a/ext/bcmath/tests/bcceil.phpt b/ext/bcmath/tests/bcceil.phpt index a960e3ef2674b..117a3bb53fdee 100644 --- a/ext/bcmath/tests/bcceil.phpt +++ b/ext/bcmath/tests/bcceil.phpt @@ -24,23 +24,23 @@ $nums = [ ]; foreach ($nums as $num) { - echo str_pad("$num", 50), ' => ', bcceil($num), "\n"; + echo str_pad("$num", 50, ' ', STR_PAD_LEFT), ' => ', bcceil($num), "\n"; } ?> --EXPECT-- -0 => 0 -0.00 => 0 --0 => 0 --0.00 => 0 -0.01 => 1 -0.000000000000000000000000000000000000000001 => 1 --0.01 => 0 --0.000000000000000000000000000000000000000001 => 0 -1 => 1 -1.0000 => 1 -1.0001 => 2 -100000.000000000000000000000000000000000000000001 => 100001 --1 => -1 --1.0000 => -1 --1.0001 => -1 +0 => 0 + 0.00 => 0 + -0 => 0 + -0.00 => 0 + 0.01 => 1 + 0.000000000000000000000000000000000000000001 => 1 + -0.01 => 0 + -0.000000000000000000000000000000000000000001 => 0 + 1 => 1 + 1.0000 => 1 + 1.0001 => 2 + 100000.000000000000000000000000000000000000000001 => 100001 + -1 => -1 + -1.0000 => -1 + -1.0001 => -1 -100000.000000000000000000000000000000000000000001 => -100000 diff --git a/ext/bcmath/tests/bcfloor.phpt b/ext/bcmath/tests/bcfloor.phpt index 1f245f0fe84bc..4d10943db5b68 100644 --- a/ext/bcmath/tests/bcfloor.phpt +++ b/ext/bcmath/tests/bcfloor.phpt @@ -24,23 +24,23 @@ $nums = [ ]; foreach ($nums as $num) { - echo str_pad("$num", 50), ' => ', bcfloor($num), "\n"; + echo str_pad("$num", 50, ' ', STR_PAD_LEFT), ' => ', bcfloor($num), "\n"; } ?> --EXPECT-- -0 => 0 -0.00 => 0 --0 => 0 --0.00 => 0 -0.01 => 0 -0.000000000000000000000000000000000000000001 => 0 --0.01 => -1 --0.000000000000000000000000000000000000000001 => -1 -1 => 1 -1.0000 => 1 -1.0001 => 1 -100000.000000000000000000000000000000000000000001 => 100000 --1 => -1 --1.0000 => -1 --1.0001 => -2 +0 => 0 + 0.00 => 0 + -0 => 0 + -0.00 => 0 + 0.01 => 0 + 0.000000000000000000000000000000000000000001 => 0 + -0.01 => -1 + -0.000000000000000000000000000000000000000001 => -1 + 1 => 1 + 1.0000 => 1 + 1.0001 => 1 + 100000.000000000000000000000000000000000000000001 => 100000 + -1 => -1 + -1.0000 => -1 + -1.0001 => -2 -100000.000000000000000000000000000000000000000001 => -100001 diff --git a/ext/bcmath/tests/bcround_away_from_zero.phpt b/ext/bcmath/tests/bcround_away_from_zero.phpt index ce9e7dc3a98de..b962cc8b10c75 100644 --- a/ext/bcmath/tests/bcround_away_from_zero.phpt +++ b/ext/bcmath/tests/bcround_away_from_zero.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_AWAY_FROM_ZERO); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 2 -[1.2, 0] => 2 -[1.3, 0] => 2 -[1.4, 0] => 2 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -2 -[-1.2, 0] => -2 -[-1.3, 0] => -2 -[-1.4, 0] => -2 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 2 + [1.2, 0] => 2 + [1.3, 0] => 2 + [1.4, 0] => 2 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -2 + [-1.2, 0] => -2 + [-1.3, 0] => -2 + [-1.4, 0] => -2 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3500 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3500 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1240 + [-1230, -1] => -1230 + [-1235, -1] => -1240 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3500 + [3450.0000, -2] => 3500 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3500 + [-3450.0000, -2] => -3500 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1236 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1236 + [1235.5, 0] => 1236 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1236 -[-1235.5, 0] => -1236 + [-1235.5, 0] => -1236 [-1235.500001, 0] => -1236 -[0.0001, 0] => 1 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => -1 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 + [0.0001, 0] => 1 + [0.5, 0] => 1 + [0.5000, 0] => 1 + [0.5001, 0] => 1 + [-0.0001, 0] => -1 + [-0.5, 0] => -1 + [-0.5000, 0] => -1 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.5 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.5 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 154.0 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.5 + [28.45, 1] => 28.5 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.5 + [-28.45, 1] => -28.5 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 154.0 + [153.95, 1] => 154.0 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -154.0 -[-153.95, 1] => -154.0 + [-153.95, 1] => -154.0 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.001 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => -0.001 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.001 + [0.0005, 3] => 0.001 + [0.000500, 3] => 0.001 + [0.000501, 3] => 0.001 + [-0.000001, 3] => -0.001 + [-0.0005, 3] => -0.001 + [-0.000500, 3] => -0.001 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_ceiling.phpt b/ext/bcmath/tests/bcround_ceiling.phpt index edda077cdbfdc..962072408f016 100644 --- a/ext/bcmath/tests/bcround_ceiling.phpt +++ b/ext/bcmath/tests/bcround_ceiling.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_CEILING); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 2 -[1.2, 0] => 2 -[1.3, 0] => 2 -[1.4, 0] => 2 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -1 -[-1.7, 0] => -1 -[-1.8, 0] => -1 -[-1.9, 0] => -1 + [1.1, 0] => 2 + [1.2, 0] => 2 + [1.3, 0] => 2 + [1.4, 0] => 2 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -1 + [-1.7, 0] => -1 + [-1.8, 0] => -1 + [-1.9, 0] => -1 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3500 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3400 + [1230, -1] => 1230 + [1235, -1] => 1240 + [-1230, -1] => -1230 + [-1235, -1] => -1230 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3500 + [3450.0000, -2] => 3500 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3400 + [-3450.0001, -2] => -3400 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1236 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1236 + [1235.5, 0] => 1236 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 + [-1235.5, 0] => -1235 [-1235.500001, 0] => -1235 -[0.0001, 0] => 1 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => 0 + [0.0001, 0] => 1 + [0.5, 0] => 1 + [0.5000, 0] => 1 + [0.5001, 0] => 1 + [-0.0001, 0] => 0 + [-0.5, 0] => 0 + [-0.5000, 0] => 0 + [-0.5001, 0] => 0 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.5 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.4 -[153.90, 1] => 153.9 -[153.9000001, 1] => 154.0 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.5 + [28.45, 1] => 28.5 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.4 + [-28.4500001, 1] => -28.4 + [153.90, 1] => 153.9 + [153.9000001, 1] => 154.0 + [153.95, 1] => 154.0 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 + [-153.95, 1] => -153.9 [-153.9500001, 1] => -153.9 -[0.000001, 3] => 0.001 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => 0.000 + [0.000001, 3] => 0.001 + [0.0005, 3] => 0.001 + [0.000500, 3] => 0.001 + [0.000501, 3] => 0.001 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => 0.000 + [-0.000500, 3] => 0.000 + [-0.000501, 3] => 0.000 diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt index 6e5d1dbb030e9..713b64da81169 100644 --- a/ext/bcmath/tests/bcround_early_return.phpt +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -32,31 +32,31 @@ $early_return_cases = [ ]; foreach ($early_return_cases as [$num, $precision]) { - echo str_pad("[{$num}, {$precision}]", 33), " => ", bcround($num, $precision, PHP_ROUND_HALF_UP), "\n"; + echo str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT), " => ", bcround($num, $precision, PHP_ROUND_HALF_UP), "\n"; } ?> --EXPECT-- -[123, -3] => 0 -[123, -4] => 0 -[123.123456, -3] => 0 -[123.123456, -4] => 0 -[123, 1] => 123 -[123.5, 1] => 123.5 -[123.5, 2] => 123.5 -[123.0000000000000000000001, 22] => 123.0000000000000000000001 -[123.0000000000000000000001, 23] => 123.0000000000000000000001 -[-123, -3] => 0 -[-123, -4] => 0 -[-123.123456, -3] => 0 -[-123.123456, -4] => 0 -[-123, 1] => -123 -[-123.5, 1] => -123.5 -[-123.5, 2] => -123.5 +[123, -3] => 0 + [123, -4] => 0 + [123.123456, -3] => 0 + [123.123456, -4] => 0 + [123, 1] => 123 + [123.5, 1] => 123.5 + [123.5, 2] => 123.5 + [123.0000000000000000000001, 22] => 123.0000000000000000000001 + [123.0000000000000000000001, 23] => 123.0000000000000000000001 + [-123, -3] => 0 + [-123, -4] => 0 + [-123.123456, -3] => 0 + [-123.123456, -4] => 0 + [-123, 1] => -123 + [-123.5, 1] => -123.5 + [-123.5, 2] => -123.5 [-123.0000000000000000000001, 22] => -123.0000000000000000000001 [-123.0000000000000000000001, 23] => -123.0000000000000000000001 -[0, 0] => 0 -[0.0, 0] => 0 -[0.0000, 0] => 0 -[-0, 0] => 0 -[-0.0, 0] => 0 -[-0.0000, 0] => 0 + [0, 0] => 0 + [0.0, 0] => 0 + [0.0000, 0] => 0 + [-0, 0] => 0 + [-0.0, 0] => 0 + [-0.0000, 0] => 0 diff --git a/ext/bcmath/tests/bcround_floor.phpt b/ext/bcmath/tests/bcround_floor.phpt index dc45a66b3df83..a9ea30b6fa317 100644 --- a/ext/bcmath/tests/bcround_floor.phpt +++ b/ext/bcmath/tests/bcround_floor.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_FLOOR); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 1 -[1.7, 0] => 1 -[1.8, 0] => 1 -[1.9, 0] => 1 -[-1.1, 0] => -2 -[-1.2, 0] => -2 -[-1.3, 0] => -2 -[-1.4, 0] => -2 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 1 + [1.7, 0] => 1 + [1.8, 0] => 1 + [1.9, 0] => 1 + [-1.1, 0] => -2 + [-1.2, 0] => -2 + [-1.3, 0] => -2 + [-1.4, 0] => -2 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3400 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3500 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1230 + [-1230, -1] => -1230 + [-1235, -1] => -1240 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3400 + [3450.0001, -2] => 3400 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3500 + [-3450.0000, -2] => -3500 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1235 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1235 + [1235.500001, 0] => 1235 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1236 -[-1235.5, 0] => -1236 + [-1235.5, 0] => -1236 [-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 0 -[-0.0001, 0] => -1 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 + [0.0001, 0] => 0 + [0.5, 0] => 0 + [0.5000, 0] => 0 + [0.5001, 0] => 0 + [-0.0001, 0] => -1 + [-0.5, 0] => -1 + [-0.5000, 0] => -1 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.4 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.5 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 153.9 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.4 + [28.4500001, 1] => 28.4 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.5 + [-28.45, 1] => -28.5 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 153.9 + [153.9500001, 1] => 153.9 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -154.0 -[-153.95, 1] => -154.0 + [-153.95, 1] => -154.0 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.000 -[-0.000001, 3] => -0.001 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.000 + [0.000500, 3] => 0.000 + [0.000501, 3] => 0.000 + [-0.000001, 3] => -0.001 + [-0.0005, 3] => -0.001 + [-0.000500, 3] => -0.001 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_down.phpt b/ext/bcmath/tests/bcround_half_down.phpt index 7b626079a1d17..d8ab9037bc96a 100644 --- a/ext/bcmath/tests/bcround_half_down.phpt +++ b/ext/bcmath/tests/bcround_half_down.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_HALF_DOWN); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1230 + [-1230, -1] => -1230 + [-1235, -1] => -1230 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3400 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3400 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1235 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 + [-1235.5, 0] => -1235 [-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => -1 + [0.0001, 0] => 0 + [0.5, 0] => 0 + [0.5000, 0] => 0 + [0.5001, 0] => 1 + [-0.0001, 0] => 0 + [-0.5, 0] => 0 + [-0.5000, 0] => 0 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.4 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.4 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 153.9 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 + [-153.95, 1] => -153.9 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.000 + [0.000500, 3] => 0.000 + [0.000501, 3] => 0.001 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => 0.000 + [-0.000500, 3] => 0.000 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_even.phpt b/ext/bcmath/tests/bcround_half_even.phpt index e9548ed166962..1c74f103c386f 100644 --- a/ext/bcmath/tests/bcround_half_even.phpt +++ b/ext/bcmath/tests/bcround_half_even.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_HALF_EVEN); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1240 + [-1230, -1] => -1230 + [-1235, -1] => -1240 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3400 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3400 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1236 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1236 + [-1235.5, 0] => -1236 [-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => -1 + [0.0001, 0] => 0 + [0.5, 0] => 0 + [0.5000, 0] => 0 + [0.5001, 0] => 1 + [-0.0001, 0] => 0 + [-0.5, 0] => 0 + [-0.5000, 0] => 0 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.4 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.4 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 154.0 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -154.0 + [-153.95, 1] => -154.0 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.000 + [0.000500, 3] => 0.000 + [0.000501, 3] => 0.001 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => 0.000 + [-0.000500, 3] => 0.000 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_odd.phpt b/ext/bcmath/tests/bcround_half_odd.phpt index 9e1d11e3726ce..71f1fe7178008 100644 --- a/ext/bcmath/tests/bcround_half_odd.phpt +++ b/ext/bcmath/tests/bcround_half_odd.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_HALF_ODD); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1230 + [-1230, -1] => -1230 + [-1235, -1] => -1230 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3500 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3500 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1235 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 + [-1235.5, 0] => -1235 [-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 + [0.0001, 0] => 0 + [0.5, 0] => 1 + [0.5000, 0] => 1 + [0.5001, 0] => 1 + [-0.0001, 0] => 0 + [-0.5, 0] => -1 + [-0.5000, 0] => -1 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.5 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.5 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 153.9 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 + [-153.95, 1] => -153.9 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.001 + [0.000500, 3] => 0.001 + [0.000501, 3] => 0.001 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => -0.001 + [-0.000500, 3] => -0.001 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_half_up.phpt b/ext/bcmath/tests/bcround_half_up.phpt index 66aeeba45f169..f0a6482375892 100644 --- a/ext/bcmath/tests/bcround_half_up.phpt +++ b/ext/bcmath/tests/bcround_half_up.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_HALF_UP); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 2 -[1.7, 0] => 2 -[1.8, 0] => 2 -[1.9, 0] => 2 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -2 -[-1.7, 0] => -2 -[-1.8, 0] => -2 -[-1.9, 0] => -2 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 2 + [1.7, 0] => 2 + [1.8, 0] => 2 + [1.9, 0] => 2 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -2 + [-1.7, 0] => -2 + [-1.8, 0] => -2 + [-1.9, 0] => -2 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1240 -[-1230, -1] => -1230 -[-1235, -1] => -1240 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3500 -[3450.0001, -2] => 3500 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3500 -[-3450.0001, -2] => -3500 + [1230, -1] => 1230 + [1235, -1] => 1240 + [-1230, -1] => -1230 + [-1235, -1] => -1240 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3500 + [3450.0001, -2] => 3500 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3500 + [-3450.0001, -2] => -3500 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1236 -[1235.500001, 0] => 1236 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1236 + [1235.500001, 0] => 1236 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1236 + [-1235.5, 0] => -1236 [-1235.500001, 0] => -1236 -[0.0001, 0] => 0 -[0.5, 0] => 1 -[0.5000, 0] => 1 -[0.5001, 0] => 1 -[-0.0001, 0] => 0 -[-0.5, 0] => -1 -[-0.5000, 0] => -1 -[-0.5001, 0] => -1 + [0.0001, 0] => 0 + [0.5, 0] => 1 + [0.5000, 0] => 1 + [0.5001, 0] => 1 + [-0.0001, 0] => 0 + [-0.5, 0] => -1 + [-0.5000, 0] => -1 + [-0.5001, 0] => -1 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.5 -[28.4500001, 1] => 28.5 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.5 -[-28.4500001, 1] => -28.5 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 154.0 -[153.9500001, 1] => 154.0 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.5 + [28.4500001, 1] => 28.5 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.5 + [-28.4500001, 1] => -28.5 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 154.0 + [153.9500001, 1] => 154.0 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -154.0 + [-153.95, 1] => -154.0 [-153.9500001, 1] => -154.0 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.001 -[0.000500, 3] => 0.001 -[0.000501, 3] => 0.001 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => -0.001 -[-0.000500, 3] => -0.001 -[-0.000501, 3] => -0.001 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.001 + [0.000500, 3] => 0.001 + [0.000501, 3] => 0.001 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => -0.001 + [-0.000500, 3] => -0.001 + [-0.000501, 3] => -0.001 diff --git a/ext/bcmath/tests/bcround_test_helper.inc b/ext/bcmath/tests/bcround_test_helper.inc index 92439bbfb7e7c..25d5d2a6acee5 100644 --- a/ext/bcmath/tests/bcround_test_helper.inc +++ b/ext/bcmath/tests/bcround_test_helper.inc @@ -3,7 +3,7 @@ function printResult (array $cases, int $mode) { foreach ($cases as [$num, $precision]) { - echo str_pad("[{$num}, {$precision}]", 17), " => ", bcround($num, $precision, $mode), "\n"; + echo str_pad("[{$num}, {$precision}]", 17, ' ', STR_PAD_LEFT), " => ", bcround($num, $precision, $mode), "\n"; } echo "\n"; } From c5a87bfa66b3460d2d197ee74f7d6060a0b01903 Mon Sep 17 00:00:00 2001 From: Saki Takamachi <34942839+SakiTakamachi@users.noreply.github.com> Date: Tue, 12 Mar 2024 23:39:55 +0900 Subject: [PATCH 07/18] Fixed the comment Co-authored-by: Gina Peter Banyard --- ext/bcmath/libbcmath/src/round.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index 71a687c80ae15..c96930efb98f2 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -39,15 +39,14 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) bc_free_num(result); /* - * The following three cases result in an early return: + * The following cases result in an early return: * - * 1. When rounding to an integer part, when trying to round to a digit - * larger than the num. - * 2. If try to round to the finest digit of num or finer than that, - * num is returned unchanged. - * 3. If the fraction part is all 0 from the beginning or middle to the - * end, the 0's are omitted and the number of digits in num is reduced. - * In that case, may end up in the same situation as 2. + * - When rounding to an integer part which is larger than the number + * e.g. Rounding 21.123 to 3 digits before the decimal point. + * - When rounding to a greater decimal precision then the number has, the number is unchanged + * e.g. Rounding 21.123 to 4 digits after the decimal point. + * - If the fractional part ends with zeros, the zeros are omitted and the number of digits in num is reduced. + * Meaning we might end up in the previous case. */ if (precision < 0 && num->n_len <= (size_t) (-(precision + Z_L(1))) + 1) { *result = bc_copy_num(BCG(_zero_)); From caae7c71d86d180d2974c12096a681587d7fe6a3 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 12 Mar 2024 23:47:26 +0900 Subject: [PATCH 08/18] Changed to PHP license --- ext/bcmath/libbcmath/src/floor_or_ceil.c | 43 ++++++++---------------- ext/bcmath/libbcmath/src/round.c | 43 ++++++++---------------- 2 files changed, 28 insertions(+), 58 deletions(-) diff --git a/ext/bcmath/libbcmath/src/floor_or_ceil.c b/ext/bcmath/libbcmath/src/floor_or_ceil.c index ee4db8e8f4049..fe0224c6711bf 100644 --- a/ext/bcmath/libbcmath/src/floor_or_ceil.c +++ b/ext/bcmath/libbcmath/src/floor_or_ceil.c @@ -1,33 +1,18 @@ -/* floor_or_ceil.c: bcmath library file. */ /* - Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc. - Copyright (C) 2000 Philip A. Nelson - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. (LICENSE) - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to: - - The Free Software Foundation, Inc. - 59 Temple Place, Suite 330 - Boston, MA 02111-1307 USA. - - You may contact the author by: - e-mail: philnelson@acm.org - us-mail: Philip A. Nelson - Computer Science Department, 9062 - Western Washington University - Bellingham, WA 98226-9062 - -*************************************************************************/ + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Saki Takamachi | + +----------------------------------------------------------------------+ +*/ #include "bcmath.h" #include "private.h" diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index c96930efb98f2..8c72eb87e1465 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -1,33 +1,18 @@ -/* round.c: bcmath library file. */ /* - Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc. - Copyright (C) 2000 Philip A. Nelson - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. (LICENSE) - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to: - - The Free Software Foundation, Inc. - 59 Temple Place, Suite 330 - Boston, MA 02111-1307 USA. - - You may contact the author by: - e-mail: philnelson@acm.org - us-mail: Philip A. Nelson - Computer Science Department, 9062 - Western Washington University - Bellingham, WA 98226-9062 - -*************************************************************************/ + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Saki Takamachi | + +----------------------------------------------------------------------+ +*/ #include "bcmath.h" #include "private.h" From 71fdefe4b47bc005f6fb7df1fcd6df81f52e9420 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Wed, 13 Mar 2024 00:37:30 +0900 Subject: [PATCH 09/18] Fixed forgetting to correct the test --- ext/bcmath/tests/bcround_toward_zero.phpt | 132 +++++++++++----------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/ext/bcmath/tests/bcround_toward_zero.phpt b/ext/bcmath/tests/bcround_toward_zero.phpt index 12c22901c8025..feec4a3463cb7 100644 --- a/ext/bcmath/tests/bcround_toward_zero.phpt +++ b/ext/bcmath/tests/bcround_toward_zero.phpt @@ -9,79 +9,79 @@ run_round_test(PHP_ROUND_TOWARD_ZERO); ?> --EXPECT-- ========== non-boundary value ========== -[1.1, 0] => 1 -[1.2, 0] => 1 -[1.3, 0] => 1 -[1.4, 0] => 1 -[1.6, 0] => 1 -[1.7, 0] => 1 -[1.8, 0] => 1 -[1.9, 0] => 1 -[-1.1, 0] => -1 -[-1.2, 0] => -1 -[-1.3, 0] => -1 -[-1.4, 0] => -1 -[-1.6, 0] => -1 -[-1.7, 0] => -1 -[-1.8, 0] => -1 -[-1.9, 0] => -1 + [1.1, 0] => 1 + [1.2, 0] => 1 + [1.3, 0] => 1 + [1.4, 0] => 1 + [1.6, 0] => 1 + [1.7, 0] => 1 + [1.8, 0] => 1 + [1.9, 0] => 1 + [-1.1, 0] => -1 + [-1.2, 0] => -1 + [-1.3, 0] => -1 + [-1.4, 0] => -1 + [-1.6, 0] => -1 + [-1.7, 0] => -1 + [-1.8, 0] => -1 + [-1.9, 0] => -1 ========== minus precision ========== -[1230, -1] => 1230 -[1235, -1] => 1230 -[-1230, -1] => -1230 -[-1235, -1] => -1230 -[3400.0000, -2] => 3400 -[3400.0001, -2] => 3400 -[3450.0000, -2] => 3400 -[3450.0001, -2] => 3400 -[-3400.0000, -2] => -3400 -[-3400.0001, -2] => -3400 -[-3450.0000, -2] => -3400 -[-3450.0001, -2] => -3400 + [1230, -1] => 1230 + [1235, -1] => 1230 + [-1230, -1] => -1230 + [-1235, -1] => -1230 + [3400.0000, -2] => 3400 + [3400.0001, -2] => 3400 + [3450.0000, -2] => 3400 + [3450.0001, -2] => 3400 + [-3400.0000, -2] => -3400 + [-3400.0001, -2] => -3400 + [-3450.0000, -2] => -3400 + [-3450.0001, -2] => -3400 ========== zero precision ========== -[1235, 0] => 1235 -[1235.0, 0] => 1235 -[1235.000001, 0] => 1235 -[1235.5, 0] => 1235 -[1235.500001, 0] => 1235 -[-1235, 0] => -1235 -[-1235.0, 0] => -1235 + [1235, 0] => 1235 + [1235.0, 0] => 1235 + [1235.000001, 0] => 1235 + [1235.5, 0] => 1235 + [1235.500001, 0] => 1235 + [-1235, 0] => -1235 + [-1235.0, 0] => -1235 [-1235.000001, 0] => -1235 -[-1235.5, 0] => -1235 + [-1235.5, 0] => -1235 [-1235.500001, 0] => -1235 -[0.0001, 0] => 0 -[0.5, 0] => 0 -[0.5000, 0] => 0 -[0.5001, 0] => 0 -[-0.0001, 0] => 0 -[-0.5, 0] => 0 -[-0.5000, 0] => 0 -[-0.5001, 0] => 0 + [0.0001, 0] => 0 + [0.5, 0] => 0 + [0.5000, 0] => 0 + [0.5001, 0] => 0 + [-0.0001, 0] => 0 + [-0.5, 0] => 0 + [-0.5000, 0] => 0 + [-0.5001, 0] => 0 ========== plus precision ========== -[28.40, 1] => 28.4 -[28.4000001, 1] => 28.4 -[28.45, 1] => 28.4 -[28.4500001, 1] => 28.4 -[-28.40, 1] => -28.4 -[-28.4000001, 1] => -28.4 -[-28.45, 1] => -28.4 -[-28.4500001, 1] => -28.4 -[153.90, 1] => 153.9 -[153.9000001, 1] => 153.9 -[153.95, 1] => 153.9 -[153.9500001, 1] => 153.9 -[-153.90, 1] => -153.9 + [28.40, 1] => 28.4 + [28.4000001, 1] => 28.4 + [28.45, 1] => 28.4 + [28.4500001, 1] => 28.4 + [-28.40, 1] => -28.4 + [-28.4000001, 1] => -28.4 + [-28.45, 1] => -28.4 + [-28.4500001, 1] => -28.4 + [153.90, 1] => 153.9 + [153.9000001, 1] => 153.9 + [153.95, 1] => 153.9 + [153.9500001, 1] => 153.9 + [-153.90, 1] => -153.9 [-153.9000001, 1] => -153.9 -[-153.95, 1] => -153.9 + [-153.95, 1] => -153.9 [-153.9500001, 1] => -153.9 -[0.000001, 3] => 0.000 -[0.0005, 3] => 0.000 -[0.000500, 3] => 0.000 -[0.000501, 3] => 0.000 -[-0.000001, 3] => 0.000 -[-0.0005, 3] => 0.000 -[-0.000500, 3] => 0.000 -[-0.000501, 3] => 0.000 + [0.000001, 3] => 0.000 + [0.0005, 3] => 0.000 + [0.000500, 3] => 0.000 + [0.000501, 3] => 0.000 + [-0.000001, 3] => 0.000 + [-0.0005, 3] => 0.000 + [-0.000500, 3] => 0.000 + [-0.000501, 3] => 0.000 From 34415a13fa56e2556825970998eaf15a674bcf49 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 17 Mar 2024 10:56:45 +0900 Subject: [PATCH 10/18] Fixed test expectations --- ext/bcmath/tests/bcceil.phpt | 2 +- ext/bcmath/tests/bcfloor.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/bcmath/tests/bcceil.phpt b/ext/bcmath/tests/bcceil.phpt index 117a3bb53fdee..54def154fbd72 100644 --- a/ext/bcmath/tests/bcceil.phpt +++ b/ext/bcmath/tests/bcceil.phpt @@ -28,7 +28,7 @@ foreach ($nums as $num) { } ?> --EXPECT-- -0 => 0 + 0 => 0 0.00 => 0 -0 => 0 -0.00 => 0 diff --git a/ext/bcmath/tests/bcfloor.phpt b/ext/bcmath/tests/bcfloor.phpt index 4d10943db5b68..d274ef44ba16a 100644 --- a/ext/bcmath/tests/bcfloor.phpt +++ b/ext/bcmath/tests/bcfloor.phpt @@ -28,7 +28,7 @@ foreach ($nums as $num) { } ?> --EXPECT-- -0 => 0 + 0 => 0 0.00 => 0 -0 => 0 -0.00 => 0 From 94c38300dfafeff113255cec4041ae6c9161ad36 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 31 Mar 2024 07:56:29 +0900 Subject: [PATCH 11/18] Separated round mode into separate header file --- ext/bcmath/php_bcmath.h | 3 +- ext/standard/php_math.h | 34 +-------------------- ext/standard/php_math_round_mode.h | 49 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 ext/standard/php_math_round_mode.h diff --git a/ext/bcmath/php_bcmath.h b/ext/bcmath/php_bcmath.h index 7ecc920c398c1..af8745cdba74f 100644 --- a/ext/bcmath/php_bcmath.h +++ b/ext/bcmath/php_bcmath.h @@ -19,8 +19,7 @@ #include "libbcmath/src/bcmath.h" #include "zend_API.h" -#include "php.h" -#include "ext/standard/php_math.h" +#include "ext/standard/php_math_round_mode.h" extern zend_module_entry bcmath_module_entry; #define phpext_bcmath_ptr &bcmath_module_entry diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 6ff895cce3c8d..9ebd600537fdb 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -28,6 +28,7 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret); PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base); #include +#include "php_math_round_mode.h" #ifndef M_E #define M_E 2.7182818284590452354 /* e */ @@ -97,37 +98,4 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base); #define M_SQRT3 1.73205080756887729352 /* sqrt(3) */ #endif -/* Define rounding modes (all are round-to-nearest) */ -#ifndef PHP_ROUND_HALF_UP -#define PHP_ROUND_HALF_UP 0x01 /* Arithmetic rounding, up == away from zero */ -#endif - -#ifndef PHP_ROUND_HALF_DOWN -#define PHP_ROUND_HALF_DOWN 0x02 /* Arithmetic rounding, down == towards zero */ -#endif - -#ifndef PHP_ROUND_HALF_EVEN -#define PHP_ROUND_HALF_EVEN 0x03 /* Banker's rounding */ -#endif - -#ifndef PHP_ROUND_HALF_ODD -#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/php_math_round_mode.h b/ext/standard/php_math_round_mode.h new file mode 100644 index 0000000000000..d4c11a8312fb2 --- /dev/null +++ b/ext/standard/php_math_round_mode.h @@ -0,0 +1,49 @@ +/* + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Jim Winstead | + | Stig Sæther Bakken | + +----------------------------------------------------------------------+ +*/ + +/* Define rounding modes (all are round-to-nearest) */ +#ifndef PHP_ROUND_HALF_UP +#define PHP_ROUND_HALF_UP 0x01 /* Arithmetic rounding, up == away from zero */ +#endif + +#ifndef PHP_ROUND_HALF_DOWN +#define PHP_ROUND_HALF_DOWN 0x02 /* Arithmetic rounding, down == towards zero */ +#endif + +#ifndef PHP_ROUND_HALF_EVEN +#define PHP_ROUND_HALF_EVEN 0x03 /* Banker's rounding */ +#endif + +#ifndef PHP_ROUND_HALF_ODD +#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 From 58b2657dfd470f4f8380e40623b4745daf17acce Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 14 Apr 2024 22:26:00 +0900 Subject: [PATCH 12/18] add pad --- ext/bcmath/tests/bcround_early_return.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt index 713b64da81169..dc74546dd60aa 100644 --- a/ext/bcmath/tests/bcround_early_return.phpt +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -36,7 +36,7 @@ foreach ($early_return_cases as [$num, $precision]) { } ?> --EXPECT-- -[123, -3] => 0 + [123, -3] => 0 [123, -4] => 0 [123.123456, -3] => 0 [123.123456, -4] => 0 From 2e2dd10733f781bc02e8f6b2c14f730d21f1352e Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 14 Apr 2024 22:27:54 +0900 Subject: [PATCH 13/18] address comment --- ext/bcmath/libbcmath/src/floor_or_ceil.c | 3 ++- ext/bcmath/libbcmath/src/round.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/bcmath/libbcmath/src/floor_or_ceil.c b/ext/bcmath/libbcmath/src/floor_or_ceil.c index fe0224c6711bf..5c3b17c01b610 100644 --- a/ext/bcmath/libbcmath/src/floor_or_ceil.c +++ b/ext/bcmath/libbcmath/src/floor_or_ceil.c @@ -39,8 +39,9 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result) /* check fractional part. */ size_t count = num->n_scale; const char *nptr = num->n_value + num->n_len; - while ((count > 0) && (*nptr++ == 0)) { + while ((count > 0) && (*nptr == 0)) { count--; + nptr++; } /* If all digits past the decimal point are 0 */ diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index 8c72eb87e1465..5bbbafbae21e9 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -109,8 +109,9 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) /* Loop through the remaining digits. */ size_t count = num->n_len + num->n_scale - rounded_len - 1; nptr++; - while ((count > 0) && (*nptr++ == 0)) { + while ((count > 0) && (*nptr == 0)) { count--; + nptr++; } if (count > 0) { From 963a6b4669880711bac2a686fc6b5572b42eecd7 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Sun, 14 Apr 2024 22:44:14 +0900 Subject: [PATCH 14/18] fixed config.w32 --- ext/bcmath/config.w32 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/bcmath/config.w32 b/ext/bcmath/config.w32 index be6b5a06b2bfe..a0c2aa51728ad 100644 --- a/ext/bcmath/config.w32 +++ b/ext/bcmath/config.w32 @@ -7,8 +7,8 @@ if (PHP_BCMATH == "yes") { ADD_SOURCES("ext/bcmath/libbcmath/src", "add.c div.c init.c neg.c \ raisemod.c sub.c compare.c divmod.c int2num.c \ num2long.c output.c recmul.c sqrt.c zero.c doaddsub.c \ - floor_or_ceil.c nearzero.c num2str.c raise.c rmzero.c str2num.c" \ - round.c, "bcmath"); + floor_or_ceil.c nearzero.c num2str.c raise.c rmzero.c str2num.c \ + round.c", "bcmath"); AC_DEFINE('HAVE_BCMATH', 1, 'Have BCMATH library'); } From 7c900f0173155ddf89fc484b5244fdf5535116a2 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Mon, 15 Apr 2024 09:03:01 +0900 Subject: [PATCH 15/18] address comment --- ext/bcmath/tests/bcround_early_return.phpt | 47 +++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt index dc74546dd60aa..beb182fb5f8c1 100644 --- a/ext/bcmath/tests/bcround_early_return.phpt +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -4,6 +4,16 @@ bcround() function with early return bcmath --FILE-- [], + 'PHP_ROUND_HALF_DOWN' => [], + 'PHP_ROUND_HALF_EVEN' => [], + 'PHP_ROUND_HALF_ODD' => [], + 'PHP_ROUND_FLOOR' => [], + 'PHP_ROUND_CEIL' => [], + 'PHP_ROUND_AWAY_FROM_ZERO' => [], + 'PHP_ROUND_TOWARD_ZERO' => [], +]; foreach ($early_return_cases as [$num, $precision]) { - echo str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT), " => ", bcround($num, $precision, PHP_ROUND_HALF_UP), "\n"; + $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, PHP_ROUND_HALF_UP) . "\n"; + echo $result; + $results['PHP_ROUND_HALF_UP'][] = $result; +} + +echo "\n"; + +foreach ($otherModes as $mode) { + foreach ($early_return_cases as [$num, $precision]) { + $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, constant($mode)) . "\n"; + $results[$mode][] = $result; + } + + if ($results['PHP_ROUND_HALF_UP'] === $results[$mode]) { + echo str_pad($mode, 24, ' ', STR_PAD_LEFT) . ": result is same to PHP_ROUND_HALF_UP\n"; + } else { + echo str_pad($mode, 24, ' ', STR_PAD_LEFT) . ": result is not same to PHP_ROUND_HALF_UP, failed\n"; + } } ?> --EXPECT-- @@ -60,3 +97,11 @@ foreach ($early_return_cases as [$num, $precision]) { [-0, 0] => 0 [-0.0, 0] => 0 [-0.0000, 0] => 0 + + PHP_ROUND_HALF_DOWN: result is same to PHP_ROUND_HALF_UP + PHP_ROUND_HALF_EVEN: result is same to PHP_ROUND_HALF_UP + PHP_ROUND_HALF_ODD: result is same to PHP_ROUND_HALF_UP + PHP_ROUND_FLOOR: result is same to PHP_ROUND_HALF_UP + PHP_ROUND_CEILING: result is same to PHP_ROUND_HALF_UP +PHP_ROUND_AWAY_FROM_ZERO: result is same to PHP_ROUND_HALF_UP + PHP_ROUND_TOWARD_ZERO: result is same to PHP_ROUND_HALF_UP From d227a94a58ffb26a744c4ed7d16dafa89b29ece6 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 16 Apr 2024 00:10:23 +0900 Subject: [PATCH 16/18] Fix the test case and the test fails Added boundary value test case. Also removed test cases that should not be returned early from early return tests --- ext/bcmath/tests/bcround_away_from_zero.phpt | 2 ++ ext/bcmath/tests/bcround_ceiling.phpt | 2 ++ ext/bcmath/tests/bcround_early_return.phpt | 8 -------- ext/bcmath/tests/bcround_floor.phpt | 2 ++ ext/bcmath/tests/bcround_half_down.phpt | 2 ++ ext/bcmath/tests/bcround_half_even.phpt | 2 ++ ext/bcmath/tests/bcround_half_odd.phpt | 2 ++ ext/bcmath/tests/bcround_half_up.phpt | 2 ++ ext/bcmath/tests/bcround_test_helper.inc | 2 ++ ext/bcmath/tests/bcround_toward_zero.phpt | 2 ++ 10 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ext/bcmath/tests/bcround_away_from_zero.phpt b/ext/bcmath/tests/bcround_away_from_zero.phpt index b962cc8b10c75..cfaa15002585e 100644 --- a/ext/bcmath/tests/bcround_away_from_zero.phpt +++ b/ext/bcmath/tests/bcround_away_from_zero.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_AWAY_FROM_ZERO); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 100 + [-50, -2] => -100 [1230, -1] => 1230 [1235, -1] => 1240 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_ceiling.phpt b/ext/bcmath/tests/bcround_ceiling.phpt index 962072408f016..a3f6b50e1f989 100644 --- a/ext/bcmath/tests/bcround_ceiling.phpt +++ b/ext/bcmath/tests/bcround_ceiling.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_CEILING); [-1.9, 0] => -1 ========== minus precision ========== + [50, -2] => 100 + [-50, -2] => 0 [1230, -1] => 1230 [1235, -1] => 1240 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt index beb182fb5f8c1..cfe7d66a58b52 100644 --- a/ext/bcmath/tests/bcround_early_return.phpt +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -15,18 +15,14 @@ $otherModes = [ ]; $early_return_cases = [ - ['123', -3], ['123', -4], - ['123.123456', -3], ['123.123456', -4], ['123', 1], ['123.5', 1], ['123.5', 2], ['123.0000000000000000000001', 22], ['123.0000000000000000000001', 23], - ['-123', -3], ['-123', -4], - ['-123.123456', -3], ['-123.123456', -4], ['-123', 1], ['-123.5', 1], @@ -73,18 +69,14 @@ foreach ($otherModes as $mode) { } ?> --EXPECT-- - [123, -3] => 0 [123, -4] => 0 - [123.123456, -3] => 0 [123.123456, -4] => 0 [123, 1] => 123 [123.5, 1] => 123.5 [123.5, 2] => 123.5 [123.0000000000000000000001, 22] => 123.0000000000000000000001 [123.0000000000000000000001, 23] => 123.0000000000000000000001 - [-123, -3] => 0 [-123, -4] => 0 - [-123.123456, -3] => 0 [-123.123456, -4] => 0 [-123, 1] => -123 [-123.5, 1] => -123.5 diff --git a/ext/bcmath/tests/bcround_floor.phpt b/ext/bcmath/tests/bcround_floor.phpt index a9ea30b6fa317..88d7fcd9dbb29 100644 --- a/ext/bcmath/tests/bcround_floor.phpt +++ b/ext/bcmath/tests/bcround_floor.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_FLOOR); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 0 + [-50, -2] => -100 [1230, -1] => 1230 [1235, -1] => 1230 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_half_down.phpt b/ext/bcmath/tests/bcround_half_down.phpt index d8ab9037bc96a..03d3580248111 100644 --- a/ext/bcmath/tests/bcround_half_down.phpt +++ b/ext/bcmath/tests/bcround_half_down.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_HALF_DOWN); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 0 + [-50, -2] => 0 [1230, -1] => 1230 [1235, -1] => 1230 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_half_even.phpt b/ext/bcmath/tests/bcround_half_even.phpt index 1c74f103c386f..955f0e14dcfd1 100644 --- a/ext/bcmath/tests/bcround_half_even.phpt +++ b/ext/bcmath/tests/bcround_half_even.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_HALF_EVEN); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 0 + [-50, -2] => 0 [1230, -1] => 1230 [1235, -1] => 1240 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_half_odd.phpt b/ext/bcmath/tests/bcround_half_odd.phpt index 71f1fe7178008..941da75843f29 100644 --- a/ext/bcmath/tests/bcround_half_odd.phpt +++ b/ext/bcmath/tests/bcround_half_odd.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_HALF_ODD); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 100 + [-50, -2] => -100 [1230, -1] => 1230 [1235, -1] => 1230 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_half_up.phpt b/ext/bcmath/tests/bcround_half_up.phpt index f0a6482375892..17c02c717e3a2 100644 --- a/ext/bcmath/tests/bcround_half_up.phpt +++ b/ext/bcmath/tests/bcround_half_up.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_HALF_UP); [-1.9, 0] => -2 ========== minus precision ========== + [50, -2] => 100 + [-50, -2] => -100 [1230, -1] => 1230 [1235, -1] => 1240 [-1230, -1] => -1230 diff --git a/ext/bcmath/tests/bcround_test_helper.inc b/ext/bcmath/tests/bcround_test_helper.inc index 25d5d2a6acee5..c51efc46581b3 100644 --- a/ext/bcmath/tests/bcround_test_helper.inc +++ b/ext/bcmath/tests/bcround_test_helper.inc @@ -30,6 +30,8 @@ function run_round_test(int $mode) ]; $minus_precision_cases = [ + ['50', -2], + ['-50', -2], ['1230', -1], ['1235', -1], ['-1230', -1], diff --git a/ext/bcmath/tests/bcround_toward_zero.phpt b/ext/bcmath/tests/bcround_toward_zero.phpt index feec4a3463cb7..e9d0e277efe6e 100644 --- a/ext/bcmath/tests/bcround_toward_zero.phpt +++ b/ext/bcmath/tests/bcround_toward_zero.phpt @@ -27,6 +27,8 @@ run_round_test(PHP_ROUND_TOWARD_ZERO); [-1.9, 0] => -1 ========== minus precision ========== + [50, -2] => 0 + [-50, -2] => 0 [1230, -1] => 1230 [1235, -1] => 1230 [-1230, -1] => -1230 From a3d08e5c82b5e3ccb4dd6b72330de4dcbba3cd1f Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 16 Apr 2024 09:10:48 +0900 Subject: [PATCH 17/18] Fixed round.c The comparison conditions for determining early returns were incorrect and have been corrected. Added processing when the rounding result is carried forward or becomes 0. --- ext/bcmath/libbcmath/src/round.c | 41 ++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index 5bbbafbae21e9..388f6dec2c0c1 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -33,7 +33,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) * - If the fractional part ends with zeros, the zeros are omitted and the number of digits in num is reduced. * Meaning we might end up in the previous case. */ - if (precision < 0 && num->n_len <= (size_t) (-(precision + Z_L(1))) + 1) { + if (precision < 0 && num->n_len < (size_t) (-(precision + Z_L(1))) + 1) { *result = bc_copy_num(BCG(_zero_)); return; } @@ -42,15 +42,24 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) return; } - /* Initialize result */ - *result = bc_new_num(num->n_len, precision > 0 ? precision : 0); - (*result)->n_sign = num->n_sign; /* * If the calculation result is a negative value, there is an early return, * so no underflow will occur. */ size_t rounded_len = num->n_len + precision; - memcpy((*result)->n_value, num->n_value, rounded_len); + + /* + * Initialize result + * For example, if rounded_len is 0, it means trying to round 50 to 100 or 0. + * If the result of rounding is carried over, it will be added later, so first set it to 0 here. + */ + if (rounded_len == 0) { + *result = bc_copy_num(BCG(_zero_)); + } else { + *result = bc_new_num(num->n_len, precision > 0 ? precision : 0); + memcpy((*result)->n_value, num->n_value, rounded_len); + } + (*result)->n_sign = num->n_sign; const char *nptr = num->n_value + rounded_len; @@ -126,13 +135,13 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) return; case PHP_ROUND_HALF_EVEN: - if (num->n_value[rounded_len - 1] % 2 == 0) { + if (rounded_len == 0 || num->n_value[rounded_len - 1] % 2 == 0) { return; } break; case PHP_ROUND_HALF_ODD: - if (num->n_value[rounded_len - 1] % 2 == 1) { + if (rounded_len != 0 && num->n_value[rounded_len - 1] % 2 == 1) { return; } break; @@ -142,13 +151,21 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) up: { - bc_num scaled_one = bc_new_num((*result)->n_len, (*result)->n_scale); - scaled_one->n_value[rounded_len - 1] = 1; + bc_num tmp; + + if (rounded_len == 0) { + tmp = bc_new_num(num->n_len + 1, 0); + tmp->n_value[0] = 1; + tmp->n_sign = num->n_sign; + } else { + bc_num scaled_one = bc_new_num((*result)->n_len, (*result)->n_scale); + scaled_one->n_value[rounded_len - 1] = 1; - bc_num tmp = _bc_do_add(*result, scaled_one, (*result)->n_scale); - tmp->n_sign = (*result)->n_sign; + tmp = _bc_do_add(*result, scaled_one, (*result)->n_scale); + tmp->n_sign = (*result)->n_sign; + bc_free_num(&scaled_one); + } - bc_free_num(&scaled_one); bc_free_num(result); *result = tmp; } From 9cf63f8e8469c4effe5b31707475af6669f2cd04 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 30 Apr 2024 02:31:32 +0900 Subject: [PATCH 18/18] When rounding to a precision finer than num, add 0 at the end --- ext/bcmath/libbcmath/src/round.c | 9 ++++++++- ext/bcmath/tests/bcround_early_return.phpt | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ext/bcmath/libbcmath/src/round.c b/ext/bcmath/libbcmath/src/round.c index 388f6dec2c0c1..a96822abd530d 100644 --- a/ext/bcmath/libbcmath/src/round.c +++ b/ext/bcmath/libbcmath/src/round.c @@ -37,8 +37,15 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) *result = bc_copy_num(BCG(_zero_)); return; } + /* Just like bcadd('1', '1', 4) becomes '2.0000', it pads with zeros at the end if necessary. */ if (precision >= 0 && num->n_scale <= precision) { - *result = bc_copy_num(num); + if (num->n_scale == precision) { + *result = bc_copy_num(num); + } else if(num->n_scale < precision) { + *result = bc_new_num(num->n_len, precision); + (*result)->n_sign = num->n_sign; + memcpy((*result)->n_value, num->n_value, num->n_len + num->n_scale); + } return; } diff --git a/ext/bcmath/tests/bcround_early_return.phpt b/ext/bcmath/tests/bcround_early_return.phpt index cfe7d66a58b52..372545a22ffa1 100644 --- a/ext/bcmath/tests/bcround_early_return.phpt +++ b/ext/bcmath/tests/bcround_early_return.phpt @@ -71,18 +71,18 @@ foreach ($otherModes as $mode) { --EXPECT-- [123, -4] => 0 [123.123456, -4] => 0 - [123, 1] => 123 + [123, 1] => 123.0 [123.5, 1] => 123.5 - [123.5, 2] => 123.5 + [123.5, 2] => 123.50 [123.0000000000000000000001, 22] => 123.0000000000000000000001 - [123.0000000000000000000001, 23] => 123.0000000000000000000001 + [123.0000000000000000000001, 23] => 123.00000000000000000000010 [-123, -4] => 0 [-123.123456, -4] => 0 - [-123, 1] => -123 + [-123, 1] => -123.0 [-123.5, 1] => -123.5 - [-123.5, 2] => -123.5 + [-123.5, 2] => -123.50 [-123.0000000000000000000001, 22] => -123.0000000000000000000001 -[-123.0000000000000000000001, 23] => -123.0000000000000000000001 +[-123.0000000000000000000001, 23] => -123.00000000000000000000010 [0, 0] => 0 [0.0, 0] => 0 [0.0000, 0] => 0