Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123126 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 7FD091A009C for ; Sun, 14 Apr 2024 14:33:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1713105238; bh=JQgPFo5YG/9eQHxlA6bNEbnJPcuYAJ8fo1L5jHpISUc=; h=Date:To:From:Cc:Subject:From; b=Pey2OFlJ8IFT0ps6J0jsVohEkWIPvGNrTPYeOnZkpUewZ/99VB7s/m0k8A0Q1Wfm1 WFgCtJX43955JDIzjCLZQT4WO12kNnmAATR2FYHgLca66rQhmyc9SKEXJ8c23fPE6x eMSil/3lTqqM2maujK2IKfq+hM1FVLkYycwuJpQj/47jBW7zH7vRkW4W+hgI7yfMLv Y4ik+he5ipZ0UMSHwu/M1gEfsH3LSQoEG+TsRGWXpp9dUPKT+M1TGtCbOkSgCwjJtO suYWRp2wkKRew5jHfe1BcP0/AgGhKeaZDCoWxJfQ2+/np1LXz3FhwlwYtdeLBDETx8 vLeclfD0HC0lQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 778CA180077 for ; Sun, 14 Apr 2024 14:33:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-4022.proton.ch (mail-4022.proton.ch [185.70.40.22]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 14 Apr 2024 14:33:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gpb.moe; s=protonmail; t=1713105198; x=1713364398; bh=t1KDOEOenw4XEHPtzQsMTWzpSoclAGgAWEnCkTuG8wM=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=YgvFVWFcanhlgXuKQ5IxoogcWmhSRRlkORqakNg0RUJC0cX3WyiaXAWN1q0gbehZu 6VcCDELv3BzpOVsIffvM4w82MDTvtFWk/YE8dQmLJedopaoy9Mfhh4Jwyux6cwFHoU pdBugIrPt8+UM4mlQ95BBLA2Yh4+Bnpk4x6jcSEu04OgZ/Pk6bBMVDGLxlB2ESC8VT bTUnEXcWxyz8NbfVA1WlADoR6CVgGOt37OXOMYQDqBqEUf6Aby/3BMKLu3I2aPf89j m0MV6F7UUqG0Bgnr7ehbEpe95NJH6Zy+LubaJ4NYaCVJuy3zD5t8IFea/7nYoCD6yw VLGIkyN0fOIzw== Date: Sun, 14 Apr 2024 14:33:10 +0000 To: PHP internals Cc: =?utf-8?Q?Tim_D=C3=BCsterhus?= Subject: [PHP-DEV] Incorrect terminology usage for rounding modes of round() Message-ID: Feedback-ID: 96993444:user:proton Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: internals@gpb.moe ("Gina P. Banyard") Hello internals, While reviewing the PR for the "Adding bcround, bcfloor and bcceil to BCMat= h" RFC [1] with the different rounding modes, I was made aware of the unfortunate wrong terminology usage of PHP_ROUND_HA= LF_UP and PHP_ROUND_HALF_DOWN. Indeed, PHP_ROUND_HALF_UP is implemented as "rounding half away from zero" = and not "rounding half up" (which is also called rounding toward positive i= nfinity). The behaviour for positive numbers is the same, however for negative number= s the rounding *is* different. In the same vein, PHP_ROUND_HALD_DOWN is implemented as "rounding half towa= rd zero" and not "rounding half down" (/round half toward negative infinity= ). Taking -1.5 as our number: - Rounding half-up: -1 - Rounding half away from zero: -2 - Rounding half-down: -2 - Rounding half towards zero: -1 For a detailed explanation about rounding, the Wikipedia page is of great u= se. [2] And the following rounding calculator on Calculator Soup is useful to check= the differences and behaviour of different rounding modes. [3] It should be noted that PHP is in good company about being wrong about thos= e two rounding modes, Java, Python, and Ruby (and probably others) are also= wrong in this regard. Considering that PHP 8.4 is also adding new rounding modes via the "Add 4 n= ew rounding modes to round() function" RFC [4] attempting to solve this iss= ue in this next version of PHP seems like a good idea. In my discussions with Saki about this issue, it seems that her and Tim hav= e thought about creating a new enum for rounding modes, looking something l= ike this: enum RoundingMode { case HalfAwayFromZero; case HalfTowardsZero; case HalfEven; case HalfOdd; case TowardsZero; case AwayFromZero; case NegativeInfinity; // or case Floor; case PositiveInfinity; // or case Ceiling; } and change the signature of round from: round(int|float $num, int $precision =3D 0, int $mode =3D PHP_ROUND_HALF_UP= ): float to round(int|float $num, int $precision =3D 0, RoundingMode $mode =3D Rounding= Mode::HalfAwayFromZero): float and changing the definition of the existing constants to effectively be: define('PHP_ROUND_HALF_UP', RoundingMode::HalfAwayFromZero); define('PHP_ROUND_HALF_DOWN', RoundingMode::HalfTowardsZero); define('PHP_ROUND_HALF_EVEN', RoundingMode::HalfEven); define('PHP_ROUND_HALF_ODD', RoundingMode::HalfOdd); This should not cause any BC breaks, while allowing us to potentially imple= ment the half up/down rounding modes properly, and deprecate the existing r= ounding constants in the future to get rid of the confusing names. I wanted to know if anyone has any object to introducing this new enum and = signature change. The only thing I could think of is if this enum should be in a new Maths (o= r Math or just Mathematics to not need to deal with regional difference in = the short spelling of "Mathematics") namespace. Best regards, Gina P. Banyard [1] https://wiki.php.net/rfc/adding_bcround_bcfloor_bcceil_to_bcmath [2] https://en.wikipedia.org/wiki/Rounding [3] https://www.calculatorsoup.com/calculators/math/rounding-methods-calcul= ator.php [4] https://wiki.php.net/rfc/new_rounding_modes_to_round_function