Hi Robert,

On 13.10.23 13:44, Robert Landers wrote:
On Fri, Oct 13, 2023 at 1:26 PM Jakub Zelenka <bu...@php.net> wrote:
On Tue, Sep 26, 2023 at 11:39 AM Marc Bennewitz <marc@mabe.berlin> wrote:

Hi internals

I'd like to put a new RFC under discussion:
https://wiki.php.net/rfc/integer-rounding


I would personally prefer a new function for rounding integers if anyone
wants to round large integers.

The things is that the current round behaviour is consistent with a way how
floats to int conversion is done for strict types.

<?php
declare(strict_types=1);

function test(float $a) {
     echo $a;
}
test(987654321098765432);


So it won't really help that much if this function returns long for long in
cases where the result is passed to another function expecting float.

The main problem that I see with the current approach is that it changes
return type for an edge case that most people are not impacted with. For
example it is quite usual that people had a float value with 0 fraction
which gets json encode to int. When they decode it and use round, the
return type is suddenly int. Even though it's usually not a problem, there
might be some code that expects float and maybe even assert that with
is_float. Such code will break.

On the other hand I see use some case for rounding large integers with
negative precision. But for that to work I think it would be better to have
a special function.

If you really want to make such change to round, then I would be prefer
targeting just 9.0 without any deprecation as I don't think the deprecation
should be informational only and not fixable in the code.

Cheers

Jakub
Just to add some nuance, if you are going anywhere near the edges of
ints (e.g., custom encryption prototypes from papers), you generally
know that will happen -- it's math and pretty easy to verify. In those
cases, you likely will be using GMP or some other extension to handle
the large numbers. The point is, I highly doubt people are unknowingly
using round() with exceptionally large numbers. If they are doing so,
they probably know exactly what they are doing and already handle all
potential edge cases or they use an extension specifically for dealing
with large numbers.

This very much depends on where you define your edges.
Basically we are talking about 2^53 to 2^63 which is 1024 times higher number.

Forcing people into such extensions much earlier than necessary isn't very helpful either as they all come with it's own downsides as well.

Best,
Marc

Attachment: OpenPGP_0x3936ABF753BC88CE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to