-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Add is_representable_as_float()
and is_representable_as_int
#19308
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
base: master
Are you sure you want to change the base?
[RFC] Add is_representable_as_float()
and is_representable_as_int
#19308
Conversation
is_representable_as_float()
and is_representable_as_int
is_representable_as_float()
and is_representable_as_int
44a9c53
to
7e80d90
Compare
ext/standard/type.c
Outdated
#if SIZEOF_ZEND_LONG == 4 | ||
return true; | ||
#else | ||
if (lval >= -9007199254740992LL && lval <= 9007199254740992LL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
if (lval >= -9007199254740992LL && lval <= 9007199254740992LL) { | |
if (lval >= -(INT64_C(1)<<53) && lval <= INT64_C(1)<<53) { |
and similarly for other literals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be outdated if #19308 (comment) is validated 🙂
ext/standard/type.c
Outdated
return true; | ||
} | ||
|
||
if (abs_val > 9007199254740992ULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: condition is always true?
ext/standard/type.c
Outdated
return true; | ||
} | ||
|
||
zend_ulong abs_val = (zend_ulong)(lval < 0 ? -lval : lval); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is undefined behavior with lval == ZEND_LONG_MIN
ext/standard/type.c
Outdated
#if SIZEOF_ZEND_LONG == 4 | ||
return true; | ||
#else | ||
if (lval >= -9007199254740992LL && lval <= 9007199254740992LL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What am I missing? Can't this entire else block just be return (zend_long) (double) lval == lval;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I guess UB... Can't we workaround that, the one liner is so much easier and prettier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually changed the function with the oneliner. I also added a test case with PHP_INT_MIN. I think it's actually good? I think the undefined behavior is not relevant anymore with the one-liner as we don't manipulate things manually
7e80d90
to
7b6ede1
Compare
7b6ede1
to
028761a
Compare
RFC: https://wiki.php.net/rfc/is-representable-as-float-int