Hello internals!
I would like to propose introducing two new modes to the function
round()
: PHP_ROUND_DOWN and PHP_ROUND_UP.
Those modes are highly requested as you can see by the comments on round
documentation page. First comment mentioning about those modes has 309
votes as of today. Introducing those 2 modes would be complementing to the
rounding in this function.
Round documentation: https://www.php.net/manual/en/function.round.php
My implementation: https://github.com/php/php-src/pull/11741
I'm not sure if such minor improvement requires RFC, but as someone may
have some concerns I create this thread to get your feedback.
Kind regards,
Jorg
Hey Jorg,
What is the reason for using this over these?
Hello internals!
I would like to propose introducing two new modes to the function
round()
: PHP_ROUND_DOWN and PHP_ROUND_UP.Those modes are highly requested as you can see by the comments on round
documentation page. First comment mentioning about those modes has 309
votes as of today. Introducing those 2 modes would be complementing to the
rounding in this function.Round documentation: https://www.php.net/manual/en/function.round.php
My implementation: https://github.com/php/php-src/pull/11741I'm not sure if such minor improvement requires RFC, but as someone may
have some concerns I create this thread to get your feedback.Kind regards,
Jorg
Hey Jorg,
What is the reason for using this over these?
floor()
and ceil()
don't have a $precision parameter.
What could be even more useful, however, would be to add modes which always round towards / away from zero. I suspect the commenter intending to use these modes in accounting would prefer these semantics over an implementation (like theirs) which always rounds to numerically higher / lower values.
It might also be appropriate to use more specific terminology for these modes, e.g. PHP_ROUND_FLOOR/_CEIL and PHP_ROUND_TO_ZERO/_AWAY_FROM_ZERO, rather than the ambiguous PHP_ROUND_DOWN.
Hey Jorg,
What is the reason for using this over these?
floor()
andceil()
don't have a $precision parameter.
Thanks for clarifying 👍
Thank you for your suggestions. I added two remaining modes and I think
it's complete now.
I changed the names to PHP_ROUND_CEILING
and PHP_ROUND_FLOOR
to be
consisted with rounding modes in number_format()
function. I'm not sure
about PHP_TOWARD_ZERO
and 'PHP_AWAY_FROM_ZEROas in there it's
ROUND_DOWNand
ROUND_UP`, which I also find too vague.
However, I could find names UP and DOWN in other documentations. And sadly
it refers to away from zero and toward zero, so there is big mismatch in
existing naming.
https://developer.apple.com/documentation/foundation/numberformatter/roundingmode/down
https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/math/RoundingMode.html
Kind regards,
Jorg
<https://developer.apple.com/documentation/foundation/numberformatter/roundingmode/down
Thank you for your suggestions. I added two remaining modes and I think
it's complete now.I changed the names to
PHP_ROUND_CEILING
andPHP_ROUND_FLOOR
to be
consisted with rounding modes innumber_format()
function. I'm not sure
aboutPHP_TOWARD_ZERO
and 'PHP_AWAY_FROM_ZEROas in there it's
ROUND_DOWNand
ROUND_UP`, which I also find too vague.However, I could find names UP and DOWN in other documentations. And sadly
it refers to away from zero and toward zero, so there is big mismatch in
existing naming.https://developer.apple.com/documentation/foundation/numberformatter/roundingmode/down
https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/math/RoundingMode.html
I had to deal with this issue when I was doing the rounding modes for my
math library in PHP. I ended up providing the following rounding modes:
- Half Up (Positive Infinity)
- Half Down (Negative Infinity)
- Half Even
- Half Odd
- Half Zero
- Half Infinity (Closest)
- Half Random (50% chance of either direction)
- Half Alternating (Each subsequent call to round alternates rounding
direction) - Stochastic (Chance of direction based on proximity... i.e. 1.7 rounds to
2 70% of the time, and rounds to 1 30% of the time)
The only reason I used "Half Up" and "Half Down" is because I also used
"Half Zero" and "Half Infinity" so it was more clear there were
differences. Stochastic is probably the one that sounds weirdest to most
people, but is insanely useful for applications like Machine Learning.
Jordan
Hey Jorg,
What is the reason for using this over these?
Hey Marco
floor and ceil convert a float to the next int.
round can also convert to the next decimal-,level.
round(16, -1) will round to 20 whereas round(12, -1) will be 10. And tuere's no way currently to make the first one 10 and the second one 20.
Cheers
Andreas
Hello internals!
I would like to propose introducing two new modes to the function
round()
: PHP_ROUND_DOWN and PHP_ROUND_UP.Those modes are highly requested as you can see by the comments on round
documentation page. First comment mentioning about those modes has 309
votes as of today. Introducing those 2 modes would be complementing to the
rounding in this function.Round documentation: https://www.php.net/manual/en/function.round.php
My implementation: https://github.com/php/php-src/pull/11741I'm not sure if such minor improvement requires RFC, but as someone may
have some concerns I create this thread to get your feedback.Kind regards,
Jorg
--
Andreas Heigl
Once upon a many years ago: https://github.com/php/php-src/pull/1658
It will be nice to see it finally get and implementation
Hello internals!
I would like to propose introducing two new modes to the function
round()
: PHP_ROUND_DOWN and PHP_ROUND_UP.Those modes are highly requested as you can see by the comments on round
documentation page. First comment mentioning about those modes has 309
votes as of today. Introducing those 2 modes would be complementing to the
rounding in this function.Round documentation: https://www.php.net/manual/en/function.round.php
My implementation: https://github.com/php/php-src/pull/11741I'm not sure if such minor improvement requires RFC, but as someone may
have some concerns I create this thread to get your feedback.Kind regards,
Jorg
--
Mark Baker
I had no idea about your PR Mark. It's pity to see that many improvements
(even simple) like this eventually was not implemented, despite the effort
authors put into.
I have created PR with the change. https://github.com/php/php-src/pull/12056
If you think I should proceed with RFC please let me know. Of course it
will not be merged into the PHP 8.3, but the next version.
Kind regards,
Jorg
Hello everyone,
As I am not sure what I should next I decided to follow the formal way and
I created the RFC for the change. I will start voting soon. Please let me
know if you have any suggestions.
RFC under discussion:
https://wiki.php.net/rfc/new_rounding_modes_to_round_function
Hello everyone,
As I am not sure what I should next I decided to follow the formal way and
I created the RFC for the change. I will start voting soon. Please let me
know if you have any suggestions.RFC under discussion:
https://wiki.php.net/rfc/new_rounding_modes_to_round_function
Hey Jorg
You should announce the start of the discussion in a new email thread instead of reusing your old one.
The subject should be something like "[RFC] [Discussion] Add 4 new rounding modes to round()
function".
This is done for maximal visibility.
Also, I believe the link for "PHP_ROUND_CEILING" is wrong in your RFC.
Kind regards
Niels