1 #ifndef __VMML__MATH__HPP__
2 #define __VMML__MATH__HPP__
3 
4 #include <cmath>
5 
6 namespace vmml
7 {
8 
9 namespace math
10 {
11 // helpers for certain cmath functions
12 
13 template< typename T >
14 inline T
sine(const T & angle_in_radians)15 sine( const T& angle_in_radians )
16 {
17     return sin( angle_in_radians );
18 }
19 
20 template<>
21 inline float
sine(const float & angle_in_radians)22 sine( const float& angle_in_radians )
23 {
24     return sinf( angle_in_radians );
25 }
26 
27 
28 template< typename T >
29 inline T
cosine(const T & angle_in_radians)30 cosine( const T& angle_in_radians )
31 {
32     return cos( angle_in_radians );
33 }
34 
35 template<>
36 inline float
cosine(const float & angle_in_radians)37 cosine( const float& angle_in_radians )
38 {
39     return cosf( angle_in_radians );
40 }
41 
42 
43 template< typename T >
44 inline T
square_root(const T & value)45 square_root( const T& value )
46 {
47     return sqrt( value );
48 }
49 
50 
51 
52 template<>
53 inline float
square_root(const float & value)54 square_root( const float& value )
55 {
56     return sqrtf( value );
57 }
58 
59 
60 template< class T >
squared(const T a)61 inline T squared( const T a )
62 {
63     return ( a == 0.0 ) ? 0.0 : a * a;
64 }
65 
66 
67 
68 /*
69  * Computes (a2 + b2)1/2 without destructive underflow or overflow.
70  */
71 template< class T >
pythag(T a,T b)72 inline T pythag( T a, T b )
73 {
74     a = fabs(a);
75     b = fabs(b);
76     if ( a > b )
77         return a * sqrt( 1.0 + squared( b / a ) );
78     else
79         return ( b == 0.0 ) ? 0.0 : b * sqrt( 1.0 + squared( a / b ) );
80 }
81 
82 
83 
84 template< class T >
sign(T a,T b)85 inline T sign( T a, T b )
86 {
87     return ( b >= 0.0 ) ? fabs( a ) : -fabs( a );
88 }
89 
90 
91 } // namespace math
92 
93 } // namespace vmml
94 
95 #endif
96 
97