/* * This file is part of libgim. * * libgim is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * libgim is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with libgim. If not, see . * * Copyright 2010 Danny Robson */ #ifndef __MATHS_HPP #define __MATHS_HPP #include "annotations.hpp" #include template T pow2 (T value) pure; template bool is_pow2 (T value) pure; template double rootsquare (T a, T b) pure; template T round_up (T value, T align) pure; template T round_pow2 (T value) pure; template bool is_integer (const T& value) pure; /** * 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 */ template bool almost_equal (const T &a, const T &b) { return a == b; } template typename std::enable_if< std::is_arithmetic::value && std::is_arithmetic::value, bool >::type almost_equal (const Ta &a, const Tb &b) { return almost_equal (static_cast(a), static_cast(b)); } template typename std::enable_if< !std::is_arithmetic::value || !std::is_arithmetic::value, bool >::type almost_equal (const Ta &a, const Tb &b) { return a == b; } const double PI = 3.141592653589793238462643; inline double to_degrees (double radians) { return radians * 180 / PI; } inline double to_radians (double degrees) { return degrees / 180 * PI; } template <> bool almost_equal (const float &a, const float &b); template <> bool almost_equal (const double &a, const double &b); // Useful for explictly ignore equality warnings #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" template bool exactly_equal (const T &a, const T &b) { return a == b; } #pragma GCC diagnostic pop /// Variadic minimum template const T& min (const T &a) { return a; } template const T& min (const T &a , const T &b , const Args &...args ) { return min ( b < a ? b : a, args...); } /// Variadic maximum template const T& max (const T &a) { return a; } template const T& max (const T &a , const T &b , const Args &...args ) { return max ( b > a ? b : a, args...); } template int sign (T val); #endif // __MATHS_HPP