From 39d5b006063f27effd4bf96a0a40f02aded7c8f5 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 15 Feb 2024 12:18:50 +0100 Subject: Math::sign should return 0 for 0 --- src/Math/Functions.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Math/Functions.hpp b/src/Math/Functions.hpp index 0cda6bd..e1e44e0 100644 --- a/src/Math/Functions.hpp +++ b/src/Math/Functions.hpp @@ -31,9 +31,11 @@ auto mod(A a, B b) -> decltype(a % b) { return (a % b + b) % b; } +// Returns the sign of a number. +// -1 if x < 0, 1 if x > 0, 0 if x == 0. template R sign(T x) { - return x < 0 ? -1 : 1; + return (x > 0) - (x < 0); } // III. Utility functions. -- cgit 1.4.1