Re: [Round] Change the edge case of Round()
| From: | Tim Düsterhus | Date: | Sat, 14 Oct 2023 11:07:25 +0000 |
| Subject: | Re: [Round] Change the edge case of Round() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121307@lists.php.net to get a copy of this message | ||
Hi
On 10/13/23 11:59, Saki Takamachi wrote:
For example,As I've previously said in the GitHub comments, I'm in favor of removing the complicated pre-rounding logic. Attempting to "guess" what the user might have wanted will only make the floating point imprecision worse than it already is. The internal representation 0.28499999999999998 is the one that is the "average" value of all the real numbers that map to it, thus minimizing bias. Interpreting it as 0.285 already adds a bias towards larger values and then rounding it to 2 decimal digits will result in 0.29, despite the majority of numbers that are internally represented as 0.28499999999999998 being correctly rounded to 0.28. I believe treating 0.28499999999999998 as 0.28499999999999998 and not 0.285 will be the least surprising behavior for folks that know how floating point numbers work. It would also be consistent with 'printf()' (testing with both 0.285 and 0.295 to show it doesn't just round to even):0.285is internally0.28499999999999998. Should this be considered an edge case? Or is it not an edge case? Regarding this issue, in 2008, the following RFC adopted the policy that0.285is considered an edge case''. https://wiki.php.net/rfc/rounding […] A new RFC is required to change what was determined in the previous RFC, so I would like to hear everyone's opinions first.
php > var_dump(round(0.295, 2)); php shell code:1: double(0.3) php > printf("%.2f", 0.295); 0.29 php > var_dump(round(0.285, 2)); php shell code:1: double(0.29) php > printf("%.2f", 0.285); 0.28Best regards Tim Düsterhus