diff --git a/maths.hpp b/maths.hpp index 71ae5577..285dabfc 100644 --- a/maths.hpp +++ b/maths.hpp @@ -29,9 +29,11 @@ abs (T value) { return value > 0 ? value : -value; } +//----------------------------------------------------------------------------- +// Exponentials template constexpr T -pow2 (T value) +pow2 [[gnu::pure]] (T value) { return value * value; } @@ -40,6 +42,8 @@ bool is_pow2 [[gnu::pure]] (T value); +//----------------------------------------------------------------------------- +// Logarithms template T log2 [[gnu::pure]] (T val); @@ -50,11 +54,15 @@ T log2up [[gnu::pure]] (T val); +//----------------------------------------------------------------------------- +// Roots template double rootsquare [[gnu::pure]] (T a, T b); +//----------------------------------------------------------------------------- +// Rounding template typename std::common_type::type align [[gnu::pure]] (T value, U size); @@ -65,29 +73,33 @@ T round_pow2 [[gnu::pure]] (T value); -template -bool -is_integer [[gnu::pure]] (const T& value); - - -template -unsigned -digits [[gnu::pure]] (const T& value); - - template T divup (const T a, const U b) { return (a + b - 1) / b; } -/** - * Check if two floating point numbers are approximately equal. Returns true - * if the difference is less than a percentage of each individual value. - * - * @e maximum percentage difference for equal values - */ +//----------------------------------------------------------------------------- +// Classification +template +bool +is_integer [[gnu::pure]] (const T& value); + +//----------------------------------------------------------------------------- +// Properties +template +unsigned +digits [[gnu::pure]] (const T& value); + + +//----------------------------------------------------------------------------- +template +int sign (T val); + + +//----------------------------------------------------------------------------- +// Comparisons template bool almost_equal (const T &a, const T &b) @@ -149,6 +161,7 @@ exactly_zero (T a) //----------------------------------------------------------------------------- constexpr double PI = 3.141592653589793238462643; +//----------------------------------------------------------------------------- constexpr double to_degrees (double radians) { return radians * 180 / PI; @@ -206,20 +219,18 @@ max (const T a, const U b, Args ...args) return max (a > b ? a : b, std::forward (args)...); } -//----------------------------------------------------------------------------- -template -int sign (T val); - //----------------------------------------------------------------------------- template T -limit (const T &&val, const U &&hi, const V &&lo) { +limit (const T &&val, const U &&hi, const V &&lo) +{ return val > hi ? hi: val < lo ? lo: std::move (val); } + #include "maths.ipp" #endif // __MATHS_HPP