godot-cpp
godot-cpp copied to clipboard
Behavior of Math::sign does not match GDScript treatment of 0
The current implementation of sign
https://github.com/godotengine/godot-cpp/blob/master/include/core/Math.hpp#L110 treats 0 as positive (similar to std::signbit
).
The Godot builtin treats 0 as a special case (returning 0): https://github.com/godotengine/godot/blob/8f7f5846397297fff6e8a08f89bc60ce658699cc/modules/visual_script/visual_script_builtin_funcs.cpp#L753
I'm not sure if this is an intentional difference or not, but it may be surprising for someone doing transcribing GDScript to C++.
The code you linked is for VisualScript, not GDScript.
In Godot 3.x, the sign function treats 0 as positive. In Godot 4.0, this will be changed to 0. See https://github.com/godotengine/godot/issues/44841
This repo targets Godot 3.x, so it should match the 3.x behavior. This should be changed only when godot-cpp
is ported to 4.0.
Ah, sorry for the mislink, I think https://github.com/godotengine/godot/blob/3.x/modules/gdscript/gdscript_functions.cpp#L289 is the correct implementation on 3.x. Also a quick test in GDScript in 3.x shows that sign(0)
returns 0 and I believe the docs state this behavior as well (as seen in the stable docs on https://docs.godotengine.org/en/stable/classes/[email protected] )
For clarity, this is not the vector sign method, but the global sign (as mentioned in the comment https://github.com/godotengine/godot/issues/44841#issuecomment-792337095 "sign which to me would mean that the user expects sign(0) = 0").
Ah yes, you're right. The thing that was treating 0 as positive was the SGN
macro, not GDScript's method. I guess this should be changed in GDNative to match GDScript, but it's also tempting to just not do it and leave this to 4.0, since teeechnically it breaks compatibility (and then users just need to be aware of the problem if working in 3.x).