Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too big float to int cast results nonsense #17081

Open
mvorisek opened this issue Dec 7, 2024 · 4 comments
Open

Too big float to int cast results nonsense #17081

mvorisek opened this issue Dec 7, 2024 · 4 comments

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Dec 7, 2024

Description

repro: https://3v4l.org/0FJkO

I discovered this in library where big timeout was converted to milliseconds like $timeoutFloat * 1000 and later casted into integer which resulted very dangerous 0 without any warning.

I would expect some warning like https://3v4l.org/4C3ru when too big float is impossible to be casted meaningfully into int.

@TimWolla
Copy link
Member

TimWolla commented Dec 9, 2024

I would expect some warning like https://3v4l.org/4C3ru when too big float is impossible to be casted meaningfully into int.

That's an implicit conversion. An explicit cast does not warn: https://3v4l.org/F9Ir7.

Similarly implicitly converting this large float to an int will already throw an error: https://3v4l.org/6NnZ6

In any case, given that this affects the core language functionality, this requires an RFC.

@mvorisek
Copy link
Contributor Author

mvorisek commented Dec 9, 2024

As the first step of this/my feature request, I would expect PHP_INT_MAX for float larger than the max. integer value.

The warning is second priority.

@iluuu1994
Copy link
Member

Casting huge ints to floats also loses precision. Deciding what should warn and what shouldn't is not trivial. I also think this should be discussed first.

@mvorisek
Copy link
Contributor Author

mvorisek commented Dec 9, 2024

In this issue I want to focus solely on https://3v4l.org/4gVYb behaviour which is very dangerous and not much expected from user POV. I would expect the resulting integer to be capped to PHP_INT_MAX/PHP_INT_MIN with the correct sign.

@mvorisek mvorisek changed the title Too big float to int cast should emit warning Too big float to int cast results nonsense Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants