Honeycomb  0.1
Component-Model Framework
Alge.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Math/Float.h"
5 #include "Honey/Math/Double.h"
6 #include "Honey/Math/Quad.h"
7 
8 namespace honey
9 {
10 
12 template<class Real>
14 {
15  typedef typename Numeral<Real>::Real_ Real_;
16  typedef typename Numeral<Real>::Int Int;
17  typedef typename std::make_unsigned<Int>::type UInt;
18 
19 public:
21  static Int abs(Int x) { return x >= 0 ? x : -x; }
23  static UInt abs(UInt x) { return x; }
25  static Real abs(Real x) { return Real_::abs(x); }
26 
28  static Int sign(Int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
29  static UInt sign(UInt x) { return x > 0 ? 1 : 0; }
30  static Real sign(Real x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
31 
33  static Real ceil(Real x) { return Real_::ceil(x); }
35  static Real floor(Real x) { return Real_::floor(x); }
37  static Real round(Real x) { return Real_::round(x); }
38 
40  static Real trunc(Real x) { return Real_::trunc(x); }
42  static Real frac(Real x) { return Real_::frac(x); }
43 
45  static Real mod(Real x, Real y) { return Real_::mod(x, y); }
47  static Real modNormalize(Real mod, Real val)
48  {
49  Real norm = Alge_::mod(val, mod*2);
50  return norm > mod ? -mod*2 + norm : norm < -mod ? mod*2 + norm : norm;
51  }
54  {
55  Real dist = y - x;
56  return abs(dist) > mod ? (dist >= 0 ? dist - mod*2 : dist + mod*2) : dist;
57  }
58 
60  template<class Num> static Num sqr(Num x) { return x*x; }
61 
63  static Real sqrt(Real x) { return Real_::sqrt(x); }
65  static Real sqrtInv(Real x) { return 1 / Real_::sqrt(x); }
66 
68  static Real exp(Real x) { return Real_::exp(x); }
70  static Real expm1(Real x);
71 
73  static Real pow(Real x, Real y) { return Real_::pow(x, y); }
74 
76  static const Real logMin;
78  static const Real logMax;
80  static Real log(Real x) { return Real_::log(x); }
82  static Real log(Real x, Real base) { return Real_::log(x) / Real_::log(base); }
84  static Real log1p(Real x);
85 
87  template<class Num, class Num2>
88  static typename std::common_type<Num, Num2>::type
89  min(Num a, Num2 b) { return a <= b ? a : b; }
90 
92  template<class Num, class Num2>
93  static typename std::common_type<Num, Num2>::type
94  max(Num a, Num2 b) { return a >= b ? a : b; }
95 
97  template<class Num, class Num2, class Num3>
98  static typename std::common_type<Num, Num2, Num3>::type
99  clamp(Num val, Num2 min, Num3 max) { return val < min ? min : val > max ? max : val; }
100 
102  static bool isNan(Real x) { return x != x; }
103 
105  static bool isNear(Int a, Int b, Int tol) { return abs(a - b) <= tol; }
106  static bool isNear(Real a, Real b, Real tol = Real_::zeroTol) { return abs(a - b) <= tol; }
108  static bool isNearZero(Real val, Real tol = Real_::zeroTol) { return abs(val) <= tol; }
109 
111  template<class Num, class Num2, class Num3>
112  static bool isInRange(Num val, Num2 min, Num3 max) { return val >= min && val <= max; }
113 
115  static Real hypot(Real a, Real b);
116 
118 
127  static tuple<bool,Real,Real> solve(Real a, Real b, Real c, Real d, Real u, Real v);
128 };
129 
134 
135 extern template class Alge_<Float>;
136 extern template class Alge_<Double>;
137 extern template class Alge_<Quad>;
138 
139 }
140 
static Real pow(Real x, Real y)
x raised to exponent y
Definition: Alge.h:73
Algebra.
Definition: Alge.h:13
static Real frac(Real x)
Remove the whole part, leaving just the fraction.
Definition: Alge.h:42
static Int sign(Int x)
Get sign of number {-1,0,1}.
Definition: Alge.h:28
Inherit to declare that class is not copyable.
Definition: Meta.h:286
static Real trunc(Real x)
Remove fractional part, leaving just the whole number.
Definition: Alge.h:40
static Real round(Real x)
Round to the nearest whole number.
Definition: Alge.h:37
static Real exp(Real x)
Euler's number e raised to exponent x (e^x)
Definition: Alge.h:68
static Real sqrtInv(Real x)
Inverse Square Root.
Definition: Alge.h:65
Alge_< Real > Alge
Definition: Alge.h:130
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
Alge_< Float > Alge_f
Definition: Alge.h:131
Value< int64,(val< 0)?-val:val > abs
Get the absolute value of a number.
Definition: Meta.h:307
static Real floor(Real x)
Round down to the nearest whole number towards -inf.
Definition: Alge.h:35
static Real expm1(Real x)
exp(x) - 1, more accurate than exp() for small values of x.
Definition: Alge.cpp:57
static bool isNan(Real x)
Returns true if real is not a number.
Definition: Alge.h:102
static Real ceil(Real x)
Round up to the nearest whole number towards +inf.
Definition: Alge.h:33
static std::common_type< Num, Num2 >::type max(Num a, Num2 b)
Get the maximum of two numbers.
Definition: Alge.h:94
Alge_< Double > Alge_d
Definition: Alge.h:132
static Real abs(Real x)
Get absolute value of real number.
Definition: Alge.h:25
static const Real logMin
The lowest negative value x for which exp(x) can be calucated without underflow.
Definition: Alge.h:76
Alge_< Quad > Alge_q
Definition: Alge.h:133
static Int abs(Int x)
Get absolute value of signed integer.
Definition: Alge.h:21
static std::common_type< Num, Num2 >::type min(Num a, Num2 b)
Get the minimum of two numbers.
Definition: Alge.h:89
static bool isNear(Int a, Int b, Int tol)
Check whether two numbers are near each other, given a tolerance.
Definition: Alge.h:105
static std::common_type< Num, Num2, Num3 >::type clamp(Num val, Num2 min, Num3 max)
Ensure that a number is within a range.
Definition: Alge.h:99
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
static bool isNearZero(Real val, Real tol=Real_::zeroTol)
Check whether a number is close to zero.
Definition: Alge.h:108
static bool isNear(Real a, Real b, Real tol=Real_::zeroTol)
Definition: Alge.h:106
static Real log1p(Real x)
log(1 + x), more accurate than log() for small values of x.
Definition: Alge.cpp:13
static Real modDistSigned(Real mod, Real x, Real y)
Calc smallest signed distance between two normalized values in a modular field.
Definition: Alge.h:53
static Num sqr(Num x)
Square.
Definition: Alge.h:60
static UInt sign(UInt x)
Definition: Alge.h:29
static const Real logMax
The highest value x for which exp(x) can be calucated without overflow.
Definition: Alge.h:78
static bool isInRange(Num val, Num2 min, Num3 max)
Check if value is within min/max inclusive range.
Definition: Alge.h:112
static tuple< bool, Real, Real > solve(Real a, Real b, Real c, Real d, Real u, Real v)
Solve an equation pair using Gauss-Jordan elimination.
Definition: Alge.cpp:102
static Real modNormalize(Real mod, Real val)
Get an equivalent value in the normalized modular interval [-mod, mod].
Definition: Alge.h:47
static Real hypot(Real a, Real b)
Get the hypotenuse of a right angle triangle with side lengths a and b. This method is more numerical...
Definition: Alge.cpp:84
static Real sign(Real x)
Definition: Alge.h:30
static UInt abs(UInt x)
Get absolute value of unsigned integer.
Definition: Alge.h:23
Global Honeycomb namespace.
static Real mod(Real x, Real y)
Modulo, same as x % y. Returns remainder of division: x/y.
Definition: Alge.h:45
static Real log(Real x, Real base)
Logarithm with base number.
Definition: Alge.h:82
static Real log(Real x)
Natural logarithm. ie. ln(x)
Definition: Alge.h:80