On 29/07/2025 13:52, Alexandre Daubois wrote:
https://wiki.php.net/rfc/is-representable-as-float-int
In general, I think the proposed semantics make sense. I'm not sure
about the names, but it's hard to think of a name that accurately
describes what they do.
The value is a float that is not NaN or infinity
It feels a bit odd to have a value where is_float($v) would be true, but
is_representable_as_float($v) would be false. I'd be interested to
understand the thinking behind this case.
Because of this, the function return value is also platform-dependent:
is_representable_as_float(PHP_INT_MAX); // true on 64-bit platforms, false on
32-bit platforms
This is, at best, misleading: it's not the function that's behaving
differently, it's just being given different input. By that reasoning,
strlen() is platform-dependent, because strlen(PHP_OS_FAMILY) returns 5
on Linux but 7 on Windows.
As written, it's also the wrong way around: on a 32-bit platform, you
are passing 2147483647, which is a "safe" value; on a 64-bit platform,
you are passing 9223372036854775807, which is an odd number outside the
"safe" range.
is_representable_as_float(2147483647) === true on any platform
is_representable_as_float(9223372036854775807) === false on a 64-bit build
Confusingly, passing that absolute value on a 32-bit system will appear
to return true, but that's because the loss of precision *has already
happened* in the compiler: 9223372036854775807 will actually be compiled
as 9223372036854775808.0, the nearest representable float. So again,
it's actually running the same function with different input, not
different behaviour inside the function.
I suggest removing that sentence and example completely. There is
nothing the function can or should do differently on different platforms.
Here also, the function return value is platform-dependent:
is_representable_as_int(2**31); // true on 64-bit platforms, false on 32-bit
platforms
This one is fair enough, but I'd suggest tweaking the example slightly
to avoid the same problem with different inputs: on a 64-bit build,
2**31 already is an integer, so it's trivially true. So either force a
floating point input:
is_representable_as_int(2.0**31); // true on 64-bit platforms, false on
32-bit platforms
or, perhaps clearer, use an out-of-range string input:
is_representable_as_int('2147483648'); // true on 64-bit platforms,
false on 32-bit platforms
Regards,
--
Rowan Tommins
[IMSoP]