From 4a745f870e9153e919cac4553c2cbfd768b752cb Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 3 May 2012 15:55:45 +1000 Subject: [PATCH] Use signbit for floating point sign --- maths.cpp | 25 +++++++++++++++++++++++++ maths.hpp | 6 ++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/maths.cpp b/maths.cpp index 976827b5..3c2aef59 100644 --- a/maths.cpp +++ b/maths.cpp @@ -109,3 +109,28 @@ template uint8_t round_pow2 (uint8_t); template uint16_t round_pow2 (uint16_t); template uint32_t round_pow2 (uint32_t); template uint64_t round_pow2 (uint64_t); + + +template +int +sign (T val) { + return val >= 0 ? 1 : -1; +} + + +template <> +int +sign (float val) { + return copysign (1.0, val); +} + + +template <> +int +sign (double val) { + return copysign (1.0, val); +} + + +template int sign (int); + diff --git a/maths.hpp b/maths.hpp index cb439066..e2fd2835 100644 --- a/maths.hpp +++ b/maths.hpp @@ -145,9 +145,7 @@ max (const T &a , const T &b , const Args &...args ) { return max ( b > a ? b : a, args...); } -inline double sign (double x) { return x >= 0 ? 1.0 : -1.0; } -inline float sign (float x) { return x >= 0 ? 1.0 : -1.0; } -inline int sign (int x) { return x >= 0 ? 1 : -1 ; } - +template +int sign (T val); #endif // __MATHS_HPP