-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Fix SIGN(NAN)
returning 1
#81464
Fix SIGN(NAN)
returning 1
#81464
Conversation
Have you confirmed that no code is affected by |
supersedes #81451 |
This is different from #81451 by not having a check for zero, if a value is neither positive nor negative, it is returned. After a few tries to check performance in Windows editor with this script: func _ready():
var _a
var start_time
var end_time
await get_tree().create_timer(1).timeout
start_time = Time.get_ticks_usec()
for y in range(100000000):
for x in range(40):
_a = sign(32.0)
for x in range(39):
_a = sign(-15.0)
for x in range(20):
_a = sign(0.0)
for x in range(1):
_a = sign(NAN)
end_time = Time.get_ticks_usec()
print("Running time: " + str(end_time - start_time) + "μs.") I did not manage to find a performance change higher than ~3% between this and current master. |
I did not find anything that explicitly does a NaN check before calling I thing somebody who knows Godot's code well should recheck that nothing breaks. |
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.
Makes sense to me. The old behavior was a bit weird to not be symmetric around 0. I think it's good to check >0 and <0 instead of ==0 and <0.
This does mean that there are are more than 3 possible return values, but I think this is necessary. NaN does not have a sign, and in most floating-point operations, is expected to propagate as NaN rather than be converted into a valid value.
As for -0.0, I think it's good to preserve that information. It should be rare that it breaks existing code because multiplying by the sign of a zero value will still give you zero and if you check if it's equal to zero then -0.0 will be true. But technically, some may consider this behavior change a compatibility breakage.
Note that |
The reason I thought that there might be
I think this should be corrected if there is no |
@AcatXIo To be clear, negative NaN does exist as a bit pattern, but since it is expected to interpret a NaN value as not a number, then conceptually speaking NaN does not have a sign, even though you can have |
c79b8db
to
4a85145
Compare
Gonna reiterate my point and argue that this might be better to restrict to the GDScript side, but if we can ensure that it won't break existing behavior I guess it makes a kind of sense, I'd say it doesn't make much sense to take the sign of This change doesn't change the performance however so I'd say it's safe from that perspective, but I am still not convinced that this doesn't risk having unforseen side-effects from having a method that didn't ever return If we are to change |
I'm not sure what did you mean; while the |
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 share the same concern as @AThousandShips. While sign(NAN)
returning NAN
is better, expanding the value range means breaking compatibility.
What I meant was that we already do clamping to results where the standard versions would return Also because of the nature of For example: if a audio player has an invalid transform, or has an origin that's at infinity, all sound will be muted, due to |
Fixes #79036. sign(NAN) now returns 0. This should not impact performance much in any way. Adds a test for the NAN case. Updates the documentation to clarify the new behavior.
4a85145
to
7d69a5b
Compare
OK, thank you. I've changed all 3 files, feel free to check them out again. |
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.
LGTM and makes sense while being safe
Thanks! |
Fixes #79036. sign(NAN) now returns 0.0.
This should not impact performance much in any way.
Adds a test for the NAN case. Updates the documentation to clarify the new behavior.