Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
math.hpp
1 /*
2  Title --- math.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_MATH_HPP_
23 #define _RPI_HW_MATH_HPP_
24 
25 #include <cmath>
26 
27 namespace rpihw { // Begin main namespace
28 
29 /*!
30  @namespace rpihw::math
31  @brief Namespace of the mathematical functions.
32 */
33 namespace math { // Begin math namespace
34 
35 //! Pi, the ratio of a circle's circumference to its diameter.
36 const double PI = M_PI;
37 
38 //! The infinity constant.
39 const double INF = INFINITY;
40 
41 //! The natural logarithm of 2.
42 const double LOG2 = 0.6931471;
43 
44 //! Returns the absolute value of `x`.
45 template < typename T >
46 inline T abs( T x ) { return (x > 0) ? x : -x; }
47 
48 //! Returns the nearest integer not greater than `x`.
49 template < typename T >
50 inline T floor( T x ) { return static_cast< T >( std::floor( (double) x ) ); }
51 
52 //! Returns the nearest integer not less than `x`.
53 template < typename T >
54 inline T ceil( T x ) { return static_cast< T >( std::ceil( (double) x ) ); }
55 
56 //! Returns the nearest integer to `x`.
57 template < typename T >
58 inline T round( T x ) { return (x > 0.0) ? math::floor( x + 0.5 ) : math::floor( x - 0.5 ); }
59 
60 //! Returns the lesser of `x` and `y`
61 template < typename T >
62 inline T min( T x, T y ) { return (x < y) ? x : y; }
63 
64 //! Returns the greater of `x` and `y`
65 template < typename T >
66 inline T max( T x, T y ) { return (x > y) ? x : y; }
67 
68 //! Returns `x` between `y` and `z`
69 template < typename T >
70 inline T between( T x, T y, T z ) { return (x < y) ? y : ( (x > z) ? z : x ); }
71 
72 //! Returns the sign of `x`
73 template < typename T >
74 inline T sign( T x ) { return (x == 0) ? 0 : ( (x > 0) ? 1 : -1 ); }
75 
76 //! Returns -1 if `x` is less than `y`; 1 if `x` is greater than `y`, and 0 if they are equal
77 template < typename T >
78 inline T cmp( T x, T y ) { return (x == y) ? 0 : ( (x > y) ? 1 : -1 ); }
79 
80 //! Returns `x` raised to the power `y`
81 template < typename T >
82 inline T pow( T x, T y ) { return std::pow( (double) x, (double) y ); }
83 
84 //! Returns the square of `x`
85 template < typename T >
86 inline T square( T x ) { return (x * x); }
87 
88 //! Returns the cube of `x`
89 template < typename T >
90 inline T cube( T x ) { return (x * x * x); }
91 
92 //! Returns the square root of `x`
93 template < typename T >
94 inline T sqrt( T x ) { return static_cast< T >( std::sqrt( x ) ); }
95 
96 //! Returns the natural logarithm of `x`
97 template < typename T >
98 inline T log( T x ) { return static_cast< T >( std::log( x ) ); }
99 
100 //! Returns the base-2 logarithm of `x`
101 template < typename T >
102 inline T log2( T x ) { return static_cast< T >( math::log( (double) x ) / LOG2 ); }
103 
104 //! Returns the angle `x` expressed in radians
105 template < typename T >
106 inline T rad( T x ) { return x * PI / 180.0; }
107 
108 //! Returns the angle `x` expressed in degrees
109 template < typename T >
110 inline T deg( T x ) { return x * 180.0 / PI; }
111 
112 //! Returns the sine of an angle of `x` radians
113 template < typename T >
114 inline T sin( T x ) { return static_cast< T >( std::sin( (double) x ) ); }
115 
116 //! Returns the cosine of an angle of `x` radians
117 template < typename T >
118 inline T cos( T x ) { return static_cast< T >( std::cos( (double) x ) ); }
119 
120 //! Returns the tangent of an angle of `x` radians
121 template < typename T >
122 inline T tan( T x ) { return static_cast< T >( std::tan( (double) x ) ); }
123 
124 //! Returns the arctangent of `x`, expressed in radians
125 template < typename T >
126 inline T atan( T x ) { return static_cast< T >( std::atan( (double) x ) ); }
127 
128 /* FAST CALCULATIONS */
129 
130 //! Returns the base-2 logarithm of `x`
131 inline size_t fast_log2( size_t x ) {
132 
133  asm( "clz %1, %0" : "=r" (x) : "r" (x) );
134  return ( 31 - x );
135 }
136 
137 //! Returns 2 raised to the power `x`
138 inline size_t fast_pow2( size_t x ) { return 1 << x; }
139 
140 } // End of math namespace
141 
142 } // End of main namespace
143 
144 #endif /* _RPI_HW_MATH_HPP_ */
T between(T x, T y, T z)
Returns x between y and z
Definition: math.hpp:70
T pow(T x, T y)
Returns x raised to the power y
Definition: math.hpp:82
const double PI
Pi, the ratio of a circle&#39;s circumference to its diameter.
Definition: math.hpp:36
T rad(T x)
Returns the angle x expressed in radians.
Definition: math.hpp:106
T sin(T x)
Returns the sine of an angle of x radians.
Definition: math.hpp:114
T log2(T x)
Returns the base-2 logarithm of x
Definition: math.hpp:102
T sign(T x)
Returns the sign of x
Definition: math.hpp:74
const double LOG2
The natural logarithm of 2.
Definition: math.hpp:42
T atan(T x)
Returns the arctangent of x, expressed in radians.
Definition: math.hpp:126
T square(T x)
Returns the square of x
Definition: math.hpp:86
T round(T x)
Returns the nearest integer to x.
Definition: math.hpp:58
size_t fast_log2(size_t x)
Returns the base-2 logarithm of x
Definition: math.hpp:131
T tan(T x)
Returns the tangent of an angle of x radians.
Definition: math.hpp:122
T cmp(T x, T y)
Returns -1 if x is less than y; 1 if x is greater than y, and 0 if they are equal.
Definition: math.hpp:78
T min(T x, T y)
Returns the lesser of x and y
Definition: math.hpp:62
size_t fast_pow2(size_t x)
Returns 2 raised to the power x
Definition: math.hpp:138
T cos(T x)
Returns the cosine of an angle of x radians.
Definition: math.hpp:118
T abs(T x)
Returns the absolute value of x.
Definition: math.hpp:46
T floor(T x)
Returns the nearest integer not greater than x.
Definition: math.hpp:50
T max(T x, T y)
Returns the greater of x and y
Definition: math.hpp:66
T ceil(T x)
Returns the nearest integer not less than x.
Definition: math.hpp:54
T cube(T x)
Returns the cube of x
Definition: math.hpp:90
const double INF
The infinity constant.
Definition: math.hpp:39
T log(T x)
Returns the natural logarithm of x
Definition: math.hpp:98
T deg(T x)
Returns the angle x expressed in degrees.
Definition: math.hpp:110
T sqrt(T x)
Returns the square root of x
Definition: math.hpp:94