]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/constants.h
cml version bump to 1.0.1
[chaz/yoink] / src / Moof / cml / constants.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief Useful constants.
11 */
12
13 #ifndef cml_constants_h
14 #define cml_constants_h
15
16 #include <cmath>
17
18 #if !defined(M_PI)
19 #define M_PI 3.14159265358979323846264338327950288
20 #endif
21
22 #if !defined(M_SQRT2)
23 #define M_SQRT2 1.41421356237309504880168872420969808
24 #endif
25
26 #if !defined(M_E)
27 #define M_E 2.71828182845904523536028747135266250
28 #endif
29
30 namespace cml {
31
32 #if 1
33
34 /** Templated constants struct.
35 *
36 * Either float or double can be used.
37 */
38 template<typename Float>
39 struct constants {
40 static Float pi() { return Float(M_PI); }
41 static Float two_pi() { return Float(2.*M_PI); }
42 static Float inv_pi() { return Float(1./M_PI); }
43 static Float inv_two_pi() { return Float(1./(2.*M_PI)); }
44 static Float pi_over_2() { return Float(M_PI/2.); }
45 static Float pi_over_4() { return Float(M_PI/4.); }
46 static Float deg_per_rad() { return Float(180./M_PI); }
47 static Float rad_per_deg() { return Float(M_PI/180.); }
48
49 static Float sqrt_2() { return Float(M_SQRT2); }
50 static Float sqrt_3() { return Float(1.732050807568877293527446341505); }
51 static Float sqrt_5() { return Float(2.236067977499789696409173668731); }
52 static Float sqrt_6() { return Float(2.449489742783178098197284074705); }
53
54 static Float e() { return Float(M_E); }
55 };
56
57 #else
58
59 /* XXX This version requires an explicit instantiation of *every* constant
60 * below, e.g.:
61 *
62 * template<typename F> const F cml::constants<F>::pi;
63 */
64 /** Templated constants struct.
65 *
66 * Either float or double can be used.
67 */
68 template<typename Float>
69 struct constants {
70 static const Float pi = M_PI;
71 static const Float two_pi = 2.*M_PI;
72 static const Float inv_pi = 1./M_PI; /* 1/pi */
73 static const Float inv_two_pi = 1./(2.*M_PI); /* 1/(2*pi) */
74 static const Float pi_over_2 = M_PI/2.; /* pi/2 */
75 static const Float pi_over_4 = M_PI/4.; /* pi/4 */
76 static const Float deg_per_rad = 180./M_PI;
77 static const Float rad_per_deg = M_PI/180.;
78 static const Float sqrt_2 = M_SQRT2;
79 static const Float sqrt_3 = 1.73205080756887729352744634150587237;
80 static const Float sqrt_5 = 2.23606797749978969640917366873127624;
81 static const Float sqrt_6 = 2.44948974278317809819728407470589139;
82 static const Float e = M_E;
83 };
84
85 #endif
86
87 } // namespace cml
88
89 #endif
This page took 0.036284 seconds and 4 git commands to generate.