rfc:add_bcdivmod_to_bcmath

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:add_bcdivmod_to_bcmath [2024/06/30 13:59] – created sakirfc:add_bcdivmod_to_bcmath [2024/10/24 13:12] (current) saki
Line 3: Line 3:
   * Date: 2024-06-30   * Date: 2024-06-30
   * Author: Saki Takamachi (saki@php.net)   * Author: Saki Takamachi (saki@php.net)
-  * Status: Under Discussion+  * Status: Implemented
   * First Published at: https://wiki.php.net/rfc/add_bcdivmod_to_bcmath   * First Published at: https://wiki.php.net/rfc/add_bcdivmod_to_bcmath
  
 ===== Introduction ===== ===== Introduction =====
-BCMath has <php>bcdiv()</php> and <php>bcmod()</php>, and the operation must be performed twice to obtain both the quotient and remainder. This is also true for the <php>BCMath\Number</php> class. Due to BCMath's division mechanism, the quotient and remainder can be obtained in one operation, but the current API is not designed that way.+BCMath has <php>bcdiv()</php> and <php>bcmod()</php>, and the operation must be performed twice to obtain both the quotient and remainder. This is also true for the <php>BcMath\Number</php> class. Due to BCMath's division mechanism, the quotient and remainder can be obtained in one operation, but the current API is not designed that way.
  
 ===== Proposal ===== ===== Proposal =====
-This RFC proposes the addition of a new function <php>bcdivmod()</php> that can obtain the quotient and remainder in one operation, and the corresponding method <php>divmod()</php> of the <php>BCMath\Number</php> class.+This RFC proposes the addition of a new function <php>bcdivmod()</php> that can obtain the quotient and remainder in one operation, and the corresponding method <php>divmod()</php> of the <php>BcMath\Number</php> class.
  
 This is clearly faster than doing the two operations separately. This is clearly faster than doing the two operations separately.
Line 31: Line 31:
  
 <PHP> <PHP>
-$results = bcdivmod('123', '2'); +[$quot, $rem] = bcdivmod('123', '2'); 
-// $result[0] is '61' + 
-// $result[1] is '1'+// $quot is '61' 
 +// $rem is '1
 +</PHP> 
 + 
 +<PHP> 
 +$slicesOfPizza = new BcMath\Number(8); 
 +$mouthsToFeed = new BcMath\Number(3); 
 +[$perMouth, $slicesLeft= $slicesOfPizza->divmod($mouthsToFeed); 
 + 
 +// $perMouth->value is '2' 
 +// $slicesLeft->value is '2'
 </PHP> </PHP>
  
Line 39: Line 49:
  
 Regarding return values, it was proposed in the mailing list discussion to return them as objects that have these as properties instead of an array. However, since BCMath has significantly better performance in PHP8.4, the overhead of creating the object is probably more significant, and I would still suggest returning the value in an array. Regarding return values, it was proposed in the mailing list discussion to return them as objects that have these as properties instead of an array. However, since BCMath has significantly better performance in PHP8.4, the overhead of creating the object is probably more significant, and I would still suggest returning the value in an array.
 +
 +There was an idea to return the value by reference, but it was not adopted because current PHP tends to avoid such implementation.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 73: Line 85:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
 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 2024-07-17 and will end on 2024-08-01 00:00 GMT.
 +
 +<doodle title="Add bcdivmod to BCMath" auth="Saki Takamachi" voteType="single" closed="false" closeon="2024-08-01T00:00:00Z">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/add_bcdivmod_to_bcmath.1719755988.txt.gz · Last modified: 2024/06/30 13:59 by saki