Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123129 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 64E901A009C for ; Sun, 14 Apr 2024 15:56:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1713110195; bh=prdcsAsp2gfV8DPwgmvk4grHP2kvMjLLfgHkioixN9E=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=jExkn4WiBEUVRyKUEegPwHVBUbrl8I2QWxayqr2YuiyeqdhygiK9IENnkIeoGk0Ju Qf6auhWmig1gwxMgnEFi2JGUlvvdWnBWAcP7aNkCrTM9GhuVPvEeA9jWJBQjHKEy9l O5TLFiMtOTujmB690ojqXjQSTWngBGKIYGXL9G3K95EwyFoldtbeVHxCbk7qG53RHz 7Mn+xhf9sa/d8oU6KRkG7ygU+f9rTeB+c+UM3PJWvKrsjyHG0ZkyZ6HqNASYv/Tj+n 5jp9a/nIvyvlVzcVZYK4Kdi/mTsdv5V4BsDHnAUD46lbq7ujIY/zGs4wv/aLi6F+Oc DmzsKbcnxFhwQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 33B0A1801E1 for ; Sun, 14 Apr 2024 15:56:34 +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,SPF_HELO_NONE, 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.sakiot.com (mail.sakiot.com [160.16.227.216]) (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 15:56:33 +0000 (UTC) Received: from smtpclient.apple (unknown [117.55.37.250]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.sakiot.com (Postfix) with ESMTPSA id 8B33F401D2; Mon, 15 Apr 2024 00:55:55 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sakiot.com; s=default; t=1713110155; bh=prdcsAsp2gfV8DPwgmvk4grHP2kvMjLLfgHkioixN9E=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=Q9U1onTl6Il6fKaGpyjJkGVhJ41sDUsP5ksHwcSHNoErxBddCUMSJ2Qstusvi4ptf k+3LHmKFQ74HNamARs/Wr6w1ywK5Kder/Syou1BSHJtLCdOfvrtR9YPf1Aq35kq3nY jt0t8YO8ANNMROkFxbKKtcnrbDctOrBTR6i+YqNI= Content-Type: text/plain; charset=us-ascii Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6.1.1\)) Subject: Re: [PHP-DEV] Incorrect terminology usage for rounding modes of round() In-Reply-To: Date: Mon, 15 Apr 2024 00:55:43 +0900 Cc: PHP internals , =?utf-8?Q?Tim_D=C3=BCsterhus?= Content-Transfer-Encoding: quoted-printable Message-ID: <690BDBA4-4D17-4BE7-BF76-D75F85403A0E@sakiot.com> References: To: "Gina P. Banyard" X-Mailer: Apple Mail (2.3731.700.6.1.1) From: saki@sakiot.com (Saki Takamachi) Hi Gina, > Hello internals, >=20 > While reviewing the PR for the "Adding bcround, bcfloor and bcceil to = BCMath" RFC [1] with the different rounding modes, > I was made aware of the unfortunate wrong terminology usage of = PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN. >=20 > 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 infinity). > The behaviour for positive numbers is the same, however for negative = numbers the rounding *is* different. > In the same vein, PHP_ROUND_HALD_DOWN is implemented as "rounding half = toward zero" and not "rounding half down" (/round half toward negative = infinity). >=20 > 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 >=20 > For a detailed explanation about rounding, the Wikipedia page is of = great use. [2] > And the following rounding calculator on Calculator Soup is useful to = check the differences and behaviour of different rounding modes. [3] >=20 > It should be noted that PHP is in good company about being wrong about = those two rounding modes, Java, Python, and Ruby (and probably others) = are also wrong in this regard. >=20 > Considering that PHP 8.4 is also adding new rounding modes via the = "Add 4 new rounding modes to round() function" RFC [4] attempting to = solve this issue 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 have thought about creating a new enum for rounding modes, looking = something like this: >=20 > enum RoundingMode { > case HalfAwayFromZero; > case HalfTowardsZero; > case HalfEven; > case HalfOdd; > case TowardsZero; > case AwayFromZero; > case NegativeInfinity; // or case Floor; > case PositiveInfinity; // or case Ceiling; > } >=20 > 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 = RoundingMode::HalfAwayFromZero): float >=20 > 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); >=20 > This should not cause any BC breaks, while allowing us to potentially = implement the half up/down rounding modes properly, and deprecate the = existing rounding constants in the future to get rid of the confusing = names. >=20 > 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 (or Math or just Mathematics to not need to deal with regional = difference in the short spelling of "Mathematics") namespace. >=20 > Best regards, >=20 > Gina P. Banyard >=20 > [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-calculato= r.php > [4] https://wiki.php.net/rfc/new_rounding_modes_to_round_function In my discussions with Tim, I want to make it clear that all ideas = derived are Tim's. :) Now, of course I agree with this, and agree with using namespaces. = However, if didn't use namespaces, that's fine by me. For reference, we = found approximately 100 pieces of code using the namespace `Math` on = Github Code Search. There are only a few cases for `Maths` and 0 for = `Mathematics`. Regards, Saki