From 7d69a5ba50369dad5f75c14448f674f86b41c38d Mon Sep 17 00:00:00 2001 From: AcatXIo Date: Sat, 9 Sep 2023 11:30:11 +0200 Subject: [PATCH] Fix sign(NAN) returning 1. 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. --- core/typedefs.h | 2 +- doc/classes/@GlobalScope.xml | 7 +++++-- tests/core/math/test_math_funcs.h | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core/typedefs.h b/core/typedefs.h index 1dcba581886..24c247fd388 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -109,7 +109,7 @@ constexpr T ABS(T m_v) { template constexpr const T SIGN(const T m_v) { - return m_v == 0 ? 0.0f : (m_v < 0 ? -1.0f : +1.0f); + return m_v > 0 ? +1.0f : (m_v < 0 ? -1.0f : 0.0f); } template diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index fa3f6e434e2..2780ffbd849 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -1168,11 +1168,13 @@ - Returns the same type of [Variant] as [param x], with [code]-1[/code] for negative values, [code]1[/code] for positive values, and [code]0[/code] for zeros. Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. + Returns the same type of [Variant] as [param x], with [code]-1[/code] for negative values, [code]1[/code] for positive values, and [code]0[/code] for zeros. For [code]nan[/code] values it returns 0. + Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. [codeblock] sign(-6.0) # Returns -1 sign(0.0) # Returns 0 sign(6.0) # Returns 1 + sign(NAN) # Returns 0 sign(Vector3(-6.0, 0.0, 6.0)) # Returns (-1, 0, 1) [/codeblock] @@ -1183,11 +1185,12 @@ - Returns [code]-1.0[/code] if [param x] is negative, [code]1.0[/code] if [param x] is positive, and [code]0.0[/code] if [param x] is zero. + Returns [code]-1.0[/code] if [param x] is negative, [code]1.0[/code] if [param x] is positive, and [code]0.0[/code] if [param x] is zero. For [code]nan[/code] values of [param x] it returns 0.0. [codeblock] signf(-6.5) # Returns -1.0 signf(0.0) # Returns 0.0 signf(6.5) # Returns 1.0 + signf(NAN) # Returns 0.0 [/codeblock] diff --git a/tests/core/math/test_math_funcs.h b/tests/core/math/test_math_funcs.h index e3504ef1e51..d046656b0f7 100644 --- a/tests/core/math/test_math_funcs.h +++ b/tests/core/math/test_math_funcs.h @@ -54,6 +54,8 @@ TEST_CASE("[Math] C++ macros") { CHECK(SIGN(-5) == -1.0); CHECK(SIGN(0) == 0.0); CHECK(SIGN(5) == 1.0); + // Check that SIGN(NAN) returns 0.0. + CHECK(SIGN(NAN) == 0.0); } TEST_CASE("[Math] Power of two functions") {