Hi again,

On 22.05.23 13:55, Robert Landers wrote:
New function(s) on the other hand sounds like an ugly solution as well
only helping people explicitly searching for it after they got bitten at
least once.
What about deprecating round() in favor of round_float() and
round_int()? or something.

Robert Landers
Software Engineer
Utrecht NL


I have done some research and worked on an implementation [1] even thought I'm not a C engineer.

There are some things I have noticed:

1. From PHP's type system perspective an int is completely compatible to an int also in strict mode - https://3v4l.org/sspOM

That means making round, ceil and floor returning an int should not be much less of an issue

2. The functions round, ceil and floor are documented to take an int|float as argument rather then a float only instead of a float only

For me that means there is already some kind of difference expected as if it would be documented as float only

3. Documenting it as input type matches the output type does not work in corner cases as rounding up/down but integer numbers could lead to integer over/under flows which needs to be handled and casted into a floating point number falling back current behavior.

4. I figured out one more function "number_format()" which casts everything into a floating point number even though it can unnecessary ending up in rounding errors.

Fixing this is not even a BC break at all and could go separately as a bug fix in my opinion.

5. It least in case of ceil and floor this would be a no-op instead of a cast + memory allocation for a new zval

In case of round as well but there are more things to do - haven't done any performance tests so far


I have the felling that this change is not that much of a BC break and serves the right purpose by reducing rounding errors as much as possible.

Still as these are very highly used functions I would target the next major just as there BC breaks are somehow expected even though I now think the only BC break is on explicitly checking the types after rounding.


Thanks,

Marc


[1] https://github.com/php/php-src/compare/master...marc-mabe:php-src:integer_rounding

Reply via email to