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

Can't compare INF or NAN reliably when used on an int var #33017

Open
vicguedez opened this issue Oct 23, 2019 · 3 comments
Open

Can't compare INF or NAN reliably when used on an int var #33017

vicguedez opened this issue Oct 23, 2019 · 3 comments

Comments

@vicguedez
Copy link

Godot version:
3.1.1.stable.official

OS/device including version:
Windows 7

Issue description:
If you assign an int var with INF, it gets initialized as the min int (-9223372036854775808) which is not useful as it is a negative value. If we are not meant to assign INF to an int then the docs should have a warning on it.

I haven't tested this using floats btw.

Steps to reproduce:

var int_inf : int = INF
var casted_int_inf = int(INF)
var as_inf = INF as int
var untyped_inf = INF

print(int_inf) # -9223372036854775808
print(casted_int_inf) # -9223372036854775808
print(as_inf) # -9223372036854775808
print(untyped_inf) # 1.#INF

print(1 > INF) # False. Ok
print(1 > int_inf) # True. Not ok.
print(1 > casted_int_inf) # True. Not ok.
print(1 > as_inf) # True. Not ok.
print(1 > untyped_inf) # False. Ok.

print(INF == INF) # True. Duh :P
print(INF == int_inf) # True. But what about if I wanted to check a negative INF ?
print(INF == casted_int_inf) # True. Same as above.
print(INF == as_inf) # True. Same as above.
print(INF == untyped_inf) # False. Huh?
@bruvzg
Copy link
Member

bruvzg commented Oct 23, 2019

If we are not meant to assign INF to an int then the docs should have a warning on it.

AFAIK this type of cast is passed directly to underlying C cast, and casting NaN to integer is undefined behavior in C. Debug builds probably should have some check.

6.3.1.4 Real floating and integer: When a finite value of real floating type is converted to an integer type other than_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value ofthe integral part cannot be represented by the integer type, the behavior is undefined.

print(INF == untyped_inf) # False. Huh?

That's correct, comparison with a NaN always return false when comparing with itself (or with anything else), there are is_nan and is_inf function to check if variable is Inf.

@vicguedez
Copy link
Author

@bruvzg Didn't know all of that. Thanks. I guess a notice about all of this on the docs could save some debug time.

@AcatXIo
Copy link
Contributor

AcatXIo commented Aug 25, 2023

print(int(NAN))

also outputs -9223372036854775808 as described in #80995.

@akien-mga akien-mga changed the title Can't compare INF reliably when used on an int var Can't compare INF or NAN reliably when used on an int var Aug 30, 2023
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

4 participants