Re: [RFC] [Discussion] Add 4 new rounding modes to round() function

From: Date: Mon, 04 Sep 2023 18:23:13 +0000
Subject: Re: [RFC] [Discussion] Add 4 new rounding modes to round() function
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-120978@lists.php.net to get a copy of this message
Hi On 9/3/23 17:50, Alexandru Pătrănescu wrote:
I believe consistency is important and using UP for "away from zero" and DOWN for "towards zero" sounds good to me. That was so far used consistently everywhere, as far as I'm aware. Documentation is very clear about what they mean, and you can always think that both positive infinity and negative infinity are up while zero is down.
I believe that the code should be easy to understand without needing to consult the documentation. I don't find it obvious at all that "up" would be the infinity with the same signum rather than the positive infinity. This would likely be something I would need to look up all the time. In fact, let me quote from the Wikipedia article linked in the RFC: https://en.wikipedia.org/wiki/Rounding#Rounding_half_up
However, some programming languages (such as Java, Python) define their half up as round half away from zero here.
Rounding "up" in Wikipedia is defined as rounding towards positive infinity: https://en.wikipedia.org/wiki/Rounding#Rounding_up
round up (or take the ceiling, or round toward positive infinity): y is the smallest integer that is not less than x.
I'd also like to refer to this exchange of Mastodon regarding a very similar issue with PHP 8.3's new IntervalBoundary enum: https://phpc.social/@asgrim/109318512018298154 The terminology "closed" and "open" matches the standard terminology in maths, but without the necessary background in maths it is not obvious and indeed "inclusive" and "exclusive" might've been easier to understand for everyone, including mathematicians [1]. In this case of rounding the existing behavior of "up" meaning "towards the same-signum infinity" rather than "positive infinity" appears to even contradict the definition used by Wikipedia which I also expect to be the mathematical definition (but I'm happy to be corrected here). "Away from zero" should be clear to everyone with a basic understanding of English. Introducing a brand-new RoundingMode enum (using union types) I suggested in my first email would nicely solve the naming problem and would also make the API more obvious to use due to stronger typing in the long run.
Yes, we can have some extra constants for the four ones that contain "up" or "down" with "away from zero" and "towards zero", but I find it unnecessary.
Best regards Tim Düsterhus [1] Back then I didn't have the energy to create a follow-up RFC and now it's definitely too late. At least the naming is mathematically accurate.

« previous php.internals (#120978) next »