rfc:integer-rounding
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:integer-rounding [2023/09/26 20:19] – Proposed Voting Choices mabe | rfc: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 15: | Line 15: | ||
As a result of '' | As a result of '' | ||
- | In most cases this is sufficient but in cases of handling | + | 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. |
- | '' | + | '' |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes to perform rounding on given integer values | + | This RFC proposes to perform rounding on given int and return the resulting |
- | In case of integer under-/ | + | In case of integer under-/ |
- | For '' | + | For '' |
- | The '' | + | An additional argument will be introduced |
+ | In PHP 8.next this will default | ||
+ | In PHP 9.0 the default will change to '' | ||
- | Even if the returned | + | |
+ | The behavior will be as follows: | ||
+ | < | ||
+ | // No change, rounding floating point number will keep processing and returning float as it does now | ||
+ | ceil(float, force_float=false): | ||
+ | ceil(float, force_float=true): | ||
+ | floor(float, | ||
+ | floor(float, | ||
+ | round(float, | ||
+ | round(float, | ||
+ | |||
+ | // rounding integers with force_float=false will process and return int if possible | ||
+ | ceil(int, force_float=false): | ||
+ | ceil(int, force_float=true): | ||
+ | floor(int, force_float=false): | ||
+ | floor(int, force_float=true): | ||
+ | round(int, precision: >= 0, force_float=false): | ||
+ | round(int, precision: < 0, force_float=false): | ||
+ | round(int, force_float=true): | ||
+ | </ | ||
+ | |||
+ | This will result in less implicit casts and more precise rounding of integers above 2^53. | ||
+ | |||
+ | The '' | ||
As a result PHP will behave more precise on rounding integer values. | As a result PHP will behave more precise on rounding integer values. | ||
Line 42: | Line 67: | ||
var_dump(round(1.0)); | var_dump(round(1.0)); | ||
var_dump(round(987654321098765432, | var_dump(round(987654321098765432, | ||
- | var_dump(round(987654321098765432, | + | var_dump(round(987654321098765432, |
var_dump(takeFloat(round(987654321098765432, | var_dump(takeFloat(round(987654321098765432, | ||
var_dump(returnFloat(round(987654321098765432, | var_dump(returnFloat(round(987654321098765432, | ||
+ | |||
+ | echo " | ||
+ | |||
+ | var_dump(round(1, | ||
+ | var_dump(round(1.0, | ||
+ | var_dump(round(987654321098765432, | ||
+ | var_dump(round(987654321098765432, | ||
+ | var_dump(takeFloat(round(987654321098765432, | ||
+ | var_dump(returnFloat(round(987654321098765432, | ||
+ | |||
+ | echo " | ||
+ | |||
+ | var_dump(round(1, | ||
+ | var_dump(round(1.0, | ||
+ | var_dump(round(987654321098765432, | ||
+ | var_dump(round(987654321098765432, | ||
+ | var_dump(takeFloat(round(987654321098765432, | ||
+ | var_dump(returnFloat(round(987654321098765432, | ||
</ | </ | ||
Line 55: | Line 98: | ||
float(9.87654321098766E+17) | float(9.87654321098766E+17) | ||
float(9.87654321098766E+17) | float(9.87654321098766E+17) | ||
+ | ######################### | ||
+ | Unknown named parameter $force_float | ||
</ | </ | ||
- | 8.4 behavior (deprecation message): | + | 8.4 behavior (no change by default but possible to opt-in to new behavior): |
< | < | ||
- | Rounding an integer will return an integer instead of a float in the future, consider casting argument ' | ||
float(1) | float(1) | ||
float(1) | float(1) | ||
- | Rounding an integer will return an integer instead of a float in the future, consider casting argument ' | ||
float(9.87654321098766E+17) | float(9.87654321098766E+17) | ||
float(9.8765432109877E+17) | float(9.8765432109877E+17) | ||
- | Rounding an integer will return an integer instead of a float in the future, consider casting argument ' | ||
float(9.87654321098766E+17) | float(9.87654321098766E+17) | ||
- | Rounding an integer will return an integer instead of a float in the future, consider casting argument ' | + | 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.87654321098766E+17) | ||
float(9.87654321098766E+17) | float(9.87654321098766E+17) | ||
</ | </ | ||
- | 9.0 behavior (Changed behavior): | + | 9.0 behavior (Changed |
< | < | ||
+ | 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 83: | Line 152: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | A BC break happens on type reporting functions like '' | + | A BC break happens on type reporting functions like '' |
- | + | ||
- | As such PHP 8.4 will report a deprecation message in case '' | + | |
- | + | ||
- | The old implicit behavior can be forced | + | |
- | In PHP 9.0 the new behavior | + | The old behavior |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | * Add deprecation message | + | * Introduce new argument '' |
- | * Change | + | * Change |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 104: | Line 169: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. | + | none |
- | + | ||
- | Please explain how you have verified your RFC's compatibility with opcache. | + | |
==== New Constants ==== | ==== New Constants ==== | ||
Line 121: | Line 184: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This section details areas where the feature might be improved in future, | + | After PHP 9 it can be considered to deprecate and remove the `force_float` argument again but due to very long future this is not part of this RFC. |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 147: | Line 211: | ||
PHP is a loosely typed langue and as such it's extremely uncommon to have to call different rounding functions for rounding '' | PHP is a loosely typed langue and as such it's extremely uncommon to have to call different rounding functions for rounding '' | ||
Also the normal '' | Also the normal '' | ||
- | |||
- | === Add function argument === | ||
- | |||
- | Same as "Add new function" | ||
rfc/integer-rounding.txt · Last modified: 2024/04/02 07:07 by mabe