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
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
2011-05-23 17:18:52 +10:00
|
|
|
* 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.
|
2014-07-07 15:16:04 +10:00
|
|
|
*
|
2012-04-11 15:18:26 +10:00
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
2011-05-23 17:18:52 +10:00
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
2014-07-07 15:16:04 +10:00
|
|
|
*
|
2011-05-23 17:18:52 +10:00
|
|
|
* 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
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MATHS_HPP
|
|
|
|
#define __MATHS_HPP
|
|
|
|
|
2011-07-16 15:53:53 +10:00
|
|
|
#include "annotations.hpp"
|
|
|
|
|
2012-04-20 18:02:54 +10:00
|
|
|
#include <type_traits>
|
2014-08-02 21:13:31 +10:00
|
|
|
#include <utility>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
template <typename T>
|
2014-07-07 15:16:04 +10:00
|
|
|
constexpr T
|
2012-07-30 16:31:19 +10:00
|
|
|
pow2 (T value)
|
|
|
|
{ return value * value; }
|
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
|
|
|
|
|
|
|
|
2014-08-01 20:44:58 +10:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename std::common_type<T, U>::type
|
|
|
|
round_up (T value, U align) pure;
|
2011-08-29 14:31:22 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
round_pow2 (T value) pure;
|
|
|
|
|
|
|
|
|
2012-04-20 18:04:40 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
is_integer (const T& value) pure;
|
|
|
|
|
|
|
|
|
2012-07-30 16:31:51 +10:00
|
|
|
template <typename T>
|
|
|
|
unsigned
|
|
|
|
digits (const T& value) pure;
|
|
|
|
|
2012-04-20 18:04:40 +10:00
|
|
|
|
2014-07-15 19:52:09 +10:00
|
|
|
template <typename T, typename U>
|
2014-04-16 19:17:15 +10:00
|
|
|
T
|
2014-07-15 19:52:09 +10:00
|
|
|
divup (const T a, const U b)
|
|
|
|
{ return (a + b - 1) / b; }
|
2014-04-16 19:17:15 +10:00
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
2012-05-26 18:02:38 +10:00
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
almost_equal (const float &a, const float &b);
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
almost_equal (const double &a, const double &b);
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
template <typename Ta, typename Tb>
|
2012-04-20 18:02:54 +10:00
|
|
|
typename std::enable_if<
|
|
|
|
std::is_arithmetic<Ta>::value && std::is_arithmetic<Tb>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
2012-05-26 18:02:38 +10:00
|
|
|
almost_equal (Ta a, 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-20 18:02:54 +10:00
|
|
|
template <typename Ta, typename Tb>
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_arithmetic<Ta>::value || !std::is_arithmetic<Tb>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
almost_equal (const Ta &a, const Tb &b)
|
|
|
|
{ return a == b; }
|
|
|
|
|
2012-05-03 18:11:42 +10:00
|
|
|
|
2012-05-08 16:39:58 +10:00
|
|
|
// Useful for explictly ignore equality warnings
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
2014-07-07 15:16:21 +10:00
|
|
|
template <typename T, typename U>
|
2012-05-08 16:39:58 +10:00
|
|
|
bool
|
2014-07-07 15:16:21 +10:00
|
|
|
exactly_equal (const T &a, const U &b)
|
2012-05-08 16:39:58 +10:00
|
|
|
{ return a == b; }
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
|
2012-05-03 18:11:42 +10:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
almost_zero (T a)
|
|
|
|
{ return almost_equal (a, 0); }
|
|
|
|
|
2012-05-08 16:39:58 +10:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
exactly_zero (T a)
|
|
|
|
{ return exactly_equal (a, static_cast<T> (0)); }
|
|
|
|
|
|
|
|
|
2011-10-29 23:13:47 +11:00
|
|
|
const double PI = 3.141592653589793238462643;
|
|
|
|
|
|
|
|
inline double
|
2012-05-03 15:56:21 +10:00
|
|
|
to_degrees (double radians) {
|
2011-10-29 23:13:47 +11:00
|
|
|
return radians * 180 / PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double
|
|
|
|
to_radians (double degrees) {
|
|
|
|
return degrees / 180 * PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-08-02 21:13:31 +10:00
|
|
|
template <typename T, typename U, typename ...Args>
|
|
|
|
typename std::common_type<T, U>::type
|
|
|
|
min (const T a , const U b , const Args &...args )
|
2011-05-23 17:18:52 +10:00
|
|
|
{ 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...); }
|
|
|
|
|
|
|
|
|
2012-05-03 15:55:45 +10:00
|
|
|
template <typename T>
|
|
|
|
int sign (T val);
|
2012-04-13 11:23:55 +10:00
|
|
|
|
2014-08-01 20:44:58 +10:00
|
|
|
|
2014-08-02 21:13:51 +10:00
|
|
|
template <typename T, typename U, typename V>
|
|
|
|
T
|
|
|
|
limit (const T &&val, const U &&hi, const V &&lo) {
|
|
|
|
return val > hi ? hi:
|
|
|
|
val < lo ? lo:
|
|
|
|
std::move (val);
|
|
|
|
}
|
|
|
|
|
2014-08-01 20:44:58 +10:00
|
|
|
#include "maths.ipp"
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __MATHS_HPP
|