Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121307 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75299 invoked from network); 14 Oct 2023 11:07:29 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 11:07:29 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 15A42180507 for ; Sat, 14 Oct 2023 04:07:29 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 14 Oct 2023 04:07:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1697281647; bh=Njpp9mUcpDMG7yXCIeJt1yt1uFVrYHug2Oh/T+nv7rs=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=GB5pjOM0lZAOiygTpC0PvePF4X5muASNrwNwdkGXIp/mObeyKVRP2NBD7r42FhEsX /1icJHEP873zQUS7AepTIgEGh4TgIfHaj+lDt7POAeQsUtgYoZxb5E43QSGP3B2QwT Ti9X1+Bc+ATIOtf9hAp2Wa0ap57LvRr5PShzOEot0N9pmk9H9IdjZPOPASNPWOegI1 LyuNT+h//q+e2lrWKqZh/0ETZN2aNiaa6FIm1lWpN36satRsASXIYr4c/Em0SZzjDC 8boqrvQuuzD3dxW50hgViXDW155hkdXV8V29jFDQWZ+m5OeSJsTVKXyoUQ4aG2t7V8 HIXbl29AP2Nig== Message-ID: <5eddad3f-1c9f-49fc-be05-bde79e3d98a7@bastelstu.be> Date: Sat, 14 Oct 2023 13:07:25 +0200 MIME-Version: 1.0 Content-Language: en-US To: Saki Takamachi , PHP internals References: <3D2EA682-0BAF-47C1-8650-5A7015F7F5BB@sakiot.com> In-Reply-To: <3D2EA682-0BAF-47C1-8650-5A7015F7F5BB@sakiot.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [Round] Change the edge case of Round() From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 10/13/23 11:59, Saki Takamachi wrote: > For example, `0.285` is internally `0.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 that `0.285` is 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. 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): > 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.28 Best regards Tim Düsterhus