rfc:integer-rounding

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
Next revisionBoth sides next revision
rfc:integer-rounding [2024/02/25 16:52] – introduce force_float argument maberfc:integer-rounding [2024/03/09 06:54] – wording mabe
Line 1: Line 1:
 ====== PHP RFC: Rounding Integers as int ====== ====== PHP RFC: Rounding Integers as int ======
-  * Version: 0.1+  * Version: 0.2
   * Date: 2023-09-26   * Date: 2023-09-26
   * Author: Marc Bennewitz, php@mabe.berlin   * Author: Marc Bennewitz, php@mabe.berlin
Line 17: Line 17:
 In most cases this is sufficient but in cases of handling integer values above 2^53 you start to end up with unexpected results due to floating point arithmetic and precision loss. In most cases this is sufficient but in cases of handling integer values above 2^53 you start to end up with unexpected results due to floating point arithmetic and precision loss.
  
-''number_format'' on the other hand produces human readable numbers as ''string'' and since PHP 8.3 it also performs rounding integers without casting it to floating point numbers to fix the above issues mentioned.+''number_format'' on the other hand produces human readable numbers as ''string'' and since PHP 8.3 it also performs rounding integers without casting it to floating point numbers to fix the above issue mentioned.
  
  
Line 27: Line 27:
 For ''ceil'', ''floor'' and ''round'' with ''precision >= 0'' this means a given int gets returned as is. For ''ceil'', ''floor'' and ''round'' with ''precision >= 0'' this means a given int gets returned as is.
  
-An additional argument will be introduced `bool force_float`+An additional argument will be introduced ''bool force_float''
-In PHP 8.this will default to `force_float=trueto keep current behavior but the new behavior can already be used by passing `force_float=false`+In PHP 8.next this will default to ''force_float=true'' to keep current behavior but the new behavior can already be used by passing ''force_float=false''
-In PHP 9.0 the default will change to `force_float=trueto get the new behavior by default but the previous behavior can be forced with `force_float=true`.+In PHP 9.0 the default will change to ''force_float=true'' to get the new behavior by default but the previous behavior can be forced with ''force_float=true''.
  
  
Line 43: Line 43:
  
 // rounding integers with force_float=false will process and return int if possible // rounding integers with force_float=false will process and return int if possible
-// else with force_float=true it will process on int but cast the result to float to keep previous behavior 
 ceil(int, force_float=false): int ceil(int, force_float=false): int
 ceil(int, force_float=true): float ceil(int, force_float=true): float
Line 51: Line 50:
 round(int, precision: < 0, force_float=false): int|float // implicit cast to float only in case of integer under-/overflow round(int, precision: < 0, force_float=false): int|float // implicit cast to float only in case of integer under-/overflow
 round(int, force_float=true): float round(int, force_float=true): float
- 
 </code> </code>
  
Line 72: Line 70:
 var_dump(takeFloat(round(987654321098765432, precision: -3))); var_dump(takeFloat(round(987654321098765432, precision: -3)));
 var_dump(returnFloat(round(987654321098765432, precision: -3))); var_dump(returnFloat(round(987654321098765432, precision: -3)));
 +
 +echo "\n#########################\n";
 +
 +var_dump(round(1, force_float=true));
 +var_dump(round(1.0, force_float=true));
 +var_dump(round(987654321098765432, precision: -3, force_float=true));
 +var_dump(round(987654321098765432, precision: -4, force_float=true)); // integer overflow
 +var_dump(takeFloat(round(987654321098765432, precision: -3, force_float=true)));
 +var_dump(returnFloat(round(987654321098765432, precision: -3, force_float=true)));
 +
 +echo "\n#########################\n";
 +
 +var_dump(round(1, force_float=false));
 +var_dump(round(1.0, force_float=false));
 +var_dump(round(987654321098765432, precision: -3, force_float=false));
 +var_dump(round(987654321098765432, precision: -4, force_float=false)); // integer overflow
 +var_dump(takeFloat(round(987654321098765432, precision: -3, force_float=false)));
 +var_dump(returnFloat(round(987654321098765432, precision: -3, force_float=false)));
 </code> </code>
  
Line 82: Line 98:
 float(9.87654321098766E+17) float(9.87654321098766E+17)
 float(9.87654321098766E+17) float(9.87654321098766E+17)
 +#########################
 +Unknown named parameter $force_float
 </code> </code>
  
-8.4 behavior:+8.4 behavior (no change by default but possible to opt-in to new behavior):
 <code> <code>
 float(1) float(1)
 float(1) float(1)
 float(9.87654321098766E+17) float(9.87654321098766E+17)
 +float(9.8765432109877E+17)
 +float(9.87654321098766E+17)
 +float(9.87654321098766E+17)
 +#########################
 +float(1)
 +float(1)
 +float(9.87654321098766E+17)
 +float(9.8765432109877E+17)
 +float(9.87654321098766E+17)
 +float(9.87654321098766E+17)
 +#########################
 +int(1)
 +float(1)
 +int(987654321098765000)
 float(9.8765432109877E+17) float(9.8765432109877E+17)
 float(9.87654321098766E+17) float(9.87654321098766E+17)
Line 96: Line 128:
 9.0 behavior (Changed default behavior): 9.0 behavior (Changed default behavior):
 <code> <code>
 +int(1)
 +float(1)
 +int(987654321098765000)
 +float(9.8765432109877E+17)
 +float(9.87654321098766E+17)
 +float(9.87654321098766E+17)
 +#########################
 +float(1)
 +float(1)
 +float(9.87654321098766E+17)
 +float(9.8765432109877E+17)
 +float(9.87654321098766E+17)
 +float(9.87654321098766E+17)
 +#########################
 int(1) int(1)
 float(1) float(1)
Line 112: Line 158:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-  * Introduce new argument `force_float=truein PHP 8.next +  * Introduce new argument ''force_float=true'' in PHP 8.next 
-  * Change default to `force_float=falseas described in PHP 9.+  * Change default to ''force_float=false'' as described in PHP 9.
  
 ===== RFC Impact ===== ===== RFC Impact =====
rfc/integer-rounding.txt · Last modified: 2024/04/02 07:07 by mabe