[php-src] PR #12260: Fix GH-12143: Remove pre round

From: Date: Thu, 21 Sep 2023 07:48:25 +0000
Subject: [php-src] PR #12260: Fix GH-12143: Remove pre round
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-23654@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/12260 Author: SakiTakamachi This is an improved version of #12162. This PR fixes #12143 Simply removing the pre round caused the following problems. ``` var_dump (round (0.285, 2, PHP_ROUND_HALF_UP)); // float(0.28) ``` Therefore, first, adjust the digits so that the 15th digit, which is guaranteed to be accurate, is an integer. For example, 0.285(0.28499999999999998) to 285000000000000. Then, adjust again to the digit that should be calculated using the helper. For example, 285000000000000 to 28.5 Floating point numbers have some degree of error and try to approximate them as close to an integer as possible. Therefore, by setting the range that guarantees accuracy to an integer, we can approximate it in a way that is closer to our intuition. <details> <summary>this change</summary> ``` Breakpoint 1, _php_math_round (value=0.28499999999999998, places=2, mode=1) at /php-src/ext/standard/math.c:171 warning: Source file is more recent than executable. 171 PHPAPI double _php_math_round(double value, int places, int mode) { (gdb) n 176 if (!zend_finite(value) || value == 0.0) { (gdb) 180 places = places < INT_MIN+1 ? INT_MIN+1 : places; (gdb) 181 value_places = -php_intlog10abs(value); (gdb) 183 f1 = php_intpow10(abs(places)); (gdb) 184 if (value_places + 16 > places && value_places <= places) { (gdb) 185 int precision_places = value_places + 14; (gdb) 186 double f2 = php_intpow10(abs(precision_places)); (gdb) 188 if (precision_places >= 0) { (gdb) 189 tmp_value = value * f2; (gdb) 194 int use_precision = places - precision_places; (gdb) p tmp_value $1 = 285000000000000 (gdb) n 195 use_precision = use_precision < INT_MIN+1 ? INT_MIN+1 : use_precision; (gdb) 196 f2 = php_intpow10(abs(use_precision)); (gdb) 198 if (use_precision >= 0) { (gdb) 201 tmp_value = tmp_value / f2; (gdb) 184 if (value_places + 16 > places && value_places <= places) { (gdb) p tmp_value $2 = 28.5 ``` </details> <details> <summary>simply removed pre-round</summary> ``` Breakpoint 1, _php_math_round (value=0.28499999999999998, places=2, mode=1) at /php-src/ext/standard/math.c:127 127 PHPAPI double _php_math_round(double value, int places, int mode) { (gdb) n 131 if (!zend_finite(value) || value == 0.0) { (gdb) 135 places = places < INT_MIN+1 ? INT_MIN+1 : places; (gdb) 137 f1 = php_intpow10(abs(places)); (gdb) 140 if (places >= 0) { (gdb) 141 tmp_value = value * f1; (gdb) 146 if (fabs(tmp_value) >= 1e15) { (gdb) p tmp_value $1 = 28.499999999999996 ``` </details>

« previous php.git-pulls (#23654) next »