2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2011-06-21 20:26:32 +10:00
|
|
|
* This file is part of libgim.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
|
|
|
* Waif 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.
|
|
|
|
*
|
|
|
|
* Waif 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
|
2011-06-21 20:26:32 +10:00
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
|
|
|
* Copyright 2010 Danny Robson <danny@blubinc.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MATHS_HPP
|
|
|
|
#define __MATHS_HPP
|
|
|
|
|
2011-07-16 15:53:53 +10:00
|
|
|
#include "annotations.hpp"
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
2011-07-16 15:53:53 +10:00
|
|
|
pow2 (T value) pure;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-08-29 14:31:22 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
is_pow2 (T value) pure;
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename T>
|
|
|
|
double
|
2011-07-16 15:53:53 +10:00
|
|
|
rootsquare (T a, T b) pure;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-08-29 14:31:22 +10:00
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
round_up (T value, T align) pure;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
round_pow2 (T value) pure;
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
/**
|
|
|
|
* 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 <typename T>
|
|
|
|
bool
|
2011-07-16 15:53:53 +10:00
|
|
|
almost_equal (const T &a, const T &b)
|
2011-05-23 17:18:52 +10:00
|
|
|
{ return a == b; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Ta, typename Tb>
|
|
|
|
bool
|
2011-07-16 15:53:53 +10:00
|
|
|
almost_equal (const Ta &a, const Tb &b) {
|
2011-05-23 17:18:52 +10:00
|
|
|
return almost_equal <decltype(a + b)> (static_cast<decltype(a + b)>(a),
|
|
|
|
static_cast<decltype(a + b)>(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
2011-07-16 15:53:53 +10:00
|
|
|
almost_equal (const float &a, const float &b);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
2011-07-16 15:53:53 +10:00
|
|
|
almost_equal (const double &a, const double &b);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
/// Variadic minimum
|
|
|
|
template <typename T>
|
|
|
|
const T&
|
|
|
|
min (const T &a)
|
2011-07-16 15:53:53 +10:00
|
|
|
{ return a; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
const T&
|
|
|
|
min (const T &a , const T &b , const Args &...args )
|
|
|
|
{ return min ( b < a ? b : a, args...); }
|
|
|
|
|
|
|
|
|
|
|
|
/// Variadic maximum
|
|
|
|
template <typename T>
|
|
|
|
const T&
|
|
|
|
max (const T &a)
|
2011-07-16 15:53:53 +10:00
|
|
|
{ return a; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
const T&
|
|
|
|
max (const T &a , const T &b , const Args &...args )
|
|
|
|
{ return max ( b > a ? b : a, args...); }
|
|
|
|
|
|
|
|
|
|
|
|
#endif // __MATHS_HPP
|