rfc:new_rounding_modes_to_round_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:new_rounding_modes_to_round_function [2023/08/31 15:56] – Fix link in Preparation section jorg_sowarfc:new_rounding_modes_to_round_function [2023/12/21 15:21] (current) – Changed status to implemented jorg_sowa
Line 3: Line 3:
   * Target version: PHP 8.4   * Target version: PHP 8.4
   * Author: Jorg Sowa <jorg.sowa@gmail.com>   * Author: Jorg Sowa <jorg.sowa@gmail.com>
-  * Status: Under Discussion+  * Status: Implemented
   * Implementation: https://github.com/php/php-src/pull/12056   * Implementation: https://github.com/php/php-src/pull/12056
   * First Published at: http://wiki.php.net/rfc/new_rounding_modes_to_round_function   * First Published at: http://wiki.php.net/rfc/new_rounding_modes_to_round_function
Line 11: Line 11:
 The RFC proposes to add 4 new modes to the round() function. The RFC proposes to add 4 new modes to the round() function.
  
-  * [[https://en.wikipedia.org/wiki/Principle_of_least_astonishment|PHP_ROUND_CEILING]],+  * [[https://en.wikipedia.org/wiki/Rounding#Rounding_up|PHP_ROUND_CEILING]],
   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_down|PHP_ROUND_FLOOR]],   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_down|PHP_ROUND_FLOOR]],
   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero|PHP_ROUND_AWAY_FROM_ZERO]],   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero|PHP_ROUND_AWAY_FROM_ZERO]],
   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_toward_zero|PHP_ROUND_TOWARD_ZERO]].   * [[https://en.wikipedia.org/wiki/Rounding#Rounding_toward_zero|PHP_ROUND_TOWARD_ZERO]].
  
-Two first comments on the documentation page of the round() function are related to the missing modes of rounding. The first comment has an outstanding number of 309 votes.+Two first comments on the [[https://www.php.net/manual/en/function.round.php|documentation page of the round()]] function are related to the missing modes of rounding. The first comment has an outstanding number of 309 votes.
  
 Moreover, the [[https://www.php.net/manual/en/class.numberformatter.php|NumberFormatter]] already implements 4 new proposed modes known as ROUND_CEILING, ROUND_FLOOR, ROUND_DOWN, ROUND_UP. Moreover, the [[https://www.php.net/manual/en/class.numberformatter.php|NumberFormatter]] already implements 4 new proposed modes known as ROUND_CEILING, ROUND_FLOOR, ROUND_DOWN, ROUND_UP.
Line 27: Line 27:
   * PHP_ROUND_FLOOR - rounds num to the nearest integer lower than num,   * PHP_ROUND_FLOOR - rounds num to the nearest integer lower than num,
   * PHP_ROUND_AWAY_FROM_ZERO - rounds num away from zero,   * PHP_ROUND_AWAY_FROM_ZERO - rounds num away from zero,
-  * PHP_ROUND_TOWARD_ZERO - rounds num towards zero. +  * PHP_ROUND_TOWARD_ZERO - rounds num towards zero. 
 + 
 +Creating two aliases of constants from Intl extension to have consistent naming for rounding modes: 
 + 
 +  * ROUND_TOWARD_ZERO (equivalent of PHP_ROUND_TOWARD_ZERO) alias of [[https://www.php.net/manual/en/class.numberformatter.php#numberformatter.constants.round-down|ROUND_DOWN]] 
 +  * ROUND_AWAY_FROM_ZERO (equivalent of PHP_ROUND_AWAY_FROM_ZERO) alias of [[https://www.php.net/manual/en/class.numberformatter.php#numberformatter.constants.round-up|ROUND_UP]]
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 34: Line 39:
  
 ===== Proposed PHP Versions ===== ===== Proposed PHP Versions =====
 +
 8.4 8.4
  
Line 39: Line 45:
  
 ==== Why not use functions ceil() and floor()? ==== ==== Why not use functions ceil() and floor()? ====
-The functions ceil() and floor() round numbers to the full integer, while the function round() roundes the number by given precision.+The functions ceil() and floor() round numbers to the full integer, while the function round() rounds the number by given precision. Implementing new rounding modes we get equivalent of those functions using round() with 0 as precision. 
 +<PHP> 
 +ceil($number) === round($number, 0, PHP_ROUND_CEILING); //true 
 +floor($number) === round($number, 0, PHP_ROUND_FLOOR); //true 
 +</PHP>
  
-==== Why not the names PHP_ROUND_UP and PHP_ROUND_DOWN ==== +==== Why not the names PHP_ROUND_UP and PHP_ROUND_DOWN==== 
-Such constants already exist in the NumberFormatter and represent rounding modes 'away from zero' and 'toward zero'. However the names rounding up and down are not straightforward and thus I restrained from using them+Such constants already exist in the [[https://www.php.net/manual/en/class.numberformatter.php|NumberFormatter]] and represent rounding modes 'away from zero' and 'toward zero'. However the names rounding up and down are ambiguous. They may be easily confused with the rounding ceiling/floor, while the terms away from/toward zero are distinct. 
 + 
 +Those constants would be deprecated and replaced with their corresponding aliases: ROUND_TOWARD_ZERO and ROUND_AWAY_FROM_ZERO. 
 + 
 +==== Why not the enum implementation for the rounding modes? ==== 
 +As this is extension of the existing modes we should add complementary modes to the existing constants instead implementing new Enum. This way enum is an addition to the new constants and is not included into this RFC.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 48: Line 63:
 As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
  
-Voting started on 2023-09-and will end on 2023-09-x.+Voting started on 2023-11-15 and will end on 2023-11-30 00:00 GMT. 
 + 
 +<doodle title="Implement 4 new rounding modes to `round()` function" auth="jorg_sowa" voteType="single" closed="false" closeon="2023-11-30T00:00:00Z"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +---- 
 + 
 +If the first voting is not accepted the result of the second voting will be ignored. 
 + 
 +<doodle title="Create aliases for Intl extension constants ROUND_UP and ROUND_DOWN: ROUND_AWAY_FROM_ZERO and ROUND_TOWARD_ZERO" auth="jorg_sowa" voteType="single" closed="false" closeon="2023-11-30T00:00:00Z"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Implementation ===== ===== Implementation =====
rfc/new_rounding_modes_to_round_function.1693497360.txt.gz · Last modified: 2023/08/31 15:56 by jorg_sowa