stlab.adobe.com Adobe Systems Incorporated
cmath.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 /*
10 
11 REVISIT (sparent) : Need to replicate the boost configuration tests to figure out when to fall
12 back to include math.h. This also needs to add any other C99 math.h extensions.
13 
14 */
15 
16 #ifndef ADOBE_CMATH_HPP
17 #define ADOBE_CMATH_HPP
18 
19 #include <adobe/config.hpp>
20 
21 #include <functional>
22 
23 /*************************************************************************************************/
24 
25 #if defined(__MWERKS__)
26 /*
27  Any (previously) supported version of metrowerks had the C99/TR1 cmath extensions in the
28  standard namespace in <cmath>.
29 */
30 #define ADOBE_HAS_C99_STD_MATH_H
31 #include <cmath>
32 #elif defined(__GNUC__)
33 
34 // Guessing at gcc 3 support
35 #if (__GNUC__ == 3) && (__GNUC_MINOR__ > 2)
36 
37 #define ADOBE_HAS_CPP_CMATH
38 
39 #elif __GNUC__ >= 4
40 
41 // GNUC has C99 extensions in math.h but not in <cmath> until C++11.
42 #define ADOBE_HAS_C99_MATH_H
43 #include <cmath>
44 
45 #endif
46 
47 #elif defined(_MSC_VER)
48 #include <cmath>
49 /*
50  The currently supported version of VC++ has no C99 extensions.
51 */
52 
53 #if _MSC_VER > 1600
54 #error "Unknown MSC compiler configureation for cmath (last knownversion is VC++ 10.0)."
55 #endif
56 
57 #define ADOBE_HAS_CPP_CMATH
58 
59 #else
60 #error "Unknown compiler configuration for cmath."
61 #endif
62 
63 /*************************************************************************************************/
64 
65 #if defined(ADOBE_HAS_C99_STD_MATH_H)
66 
67 namespace adobe {
68 
69 using std::float_t;
70 using std::double_t;
71 
72 using std::round;
73 using std::lround;
74 using std::trunc;
75 
76 } // namespace adobe
77 
78 /*************************************************************************************************/
79 
80 #elif defined(ADOBE_HAS_CPP_CMATH)
81 
82 namespace adobe {
83 
84 typedef float float_t;
85 typedef double double_t;
86 
87 /*************************************************************************************************/
88 
89 inline float trunc(float x)
90 { return x < 0.0f ? std::ceil(x) : std::floor(x); }
91 
92 inline double trunc(double x)
93 { return x < 0.0 ? std::ceil(x) : std::floor(x); }
94 
95 /*************************************************************************************************/
96 
97 inline float round(float x)
98 { return trunc(x + (x < 0.0f ? -0.5f : 0.5f)); }
99 
100 inline double round(double x)
101 { return trunc(x + (x < 0.0 ? -0.5 : 0.5)); }
102 
103 /*************************************************************************************************/
104 
105 inline long lround(float x)
106 { return static_cast<long>(x + (x < 0.0f ? -0.5f : 0.5f)); }
107 
108 inline long lround(double x)
109 { return static_cast<long>(x + (x < 0.0 ? -0.5 : 0.5)); }
110 
111 /*************************************************************************************************/
112 
113 } // namespace adobe
114 
115 /*************************************************************************************************/
116 
117 #elif defined(ADOBE_HAS_C99_MATH_H)
118 
119 #include <math.h>
120 
121 namespace adobe {
122 
125 
126 /*************************************************************************************************/
127 
131 
132 inline float round(float x) { return ::roundf(x); }
133 inline long lround(float x) { return ::lroundf(x); }
134 inline float trunc(float x) { return ::truncf(x); }
135 
136 /*************************************************************************************************/
137 
138 } // namespace adobe
139 
140 #elif defined(ADOBE_NO_DOCUMENTATION)
141 
142 namespace adobe {
143 
154 typedef Float double_t;
155 typedef Float float_t;
156 
157 double round(double x);
158 float round(float x);
159 long lround(double x);
160 long lround(float x);
161 double trunc(double x);
162 float trunc(float x);
165 } // namespace adobe
166 
167 #endif
168 
169 /*************************************************************************************************/
170 
171 namespace adobe {
172 
173 /*************************************************************************************************/
174 
175 template <typename A, typename R> struct nearest_cast_fn;
176 
177 /*************************************************************************************************/
178 
179 inline double round_half_up(double x)
180 { return std::floor(x + 0.5); }
181 
182 inline float round_half_up(float x)
183 { return std::floor(x + 0.5f); }
184 
185 inline long lround_half_up(double x)
186 { return static_cast<long>(std::floor(x + 0.5)); }
187 
188 inline long lround_half_up(float x)
189 { return static_cast<long>(std::floor(x + 0.5f)); }
190 
191 /*
192  REVISIT (sparent) : Should complete the rounding modes by providing a round_half_even()
193  function.
194 
195  Names are borrowed from the EDA rounding modes:
196 
197  <http://www.gobosoft.com/eiffel/gobo/math/decimal/>
198 */
199 
200 /*************************************************************************************************/
201 
202 template <typename R, typename A>
203 inline R nearest_cast(const A& x)
204 { return nearest_cast_fn<A, R>()(x); }
205 
206 /*************************************************************************************************/
207 
208 template <typename A, typename R>
209 struct nearest_cast_fn : std::unary_function<A, R>
210 {
211  R operator()(const A& x) const { return static_cast<R>(round_half_up(x)); }
212 };
213 
214 template <typename A>
215 struct nearest_cast_fn<A, float> : std::unary_function<A, float>
216 {
217  float operator()(const A& x) const { return static_cast<float>(x); }
218 };
219 
220 template <typename A>
221 struct nearest_cast_fn<A, double> : std::unary_function<A, double>
222 {
223  double operator()(const A& x) const { return static_cast<double>(x); }
224 };
225 
226 /*************************************************************************************************/
227 
228 } // namespace adobe
229 
230 /*************************************************************************************************/
231 
232 #endif
233 
234 /*************************************************************************************************/
float trunc(float x)
double round(double x)
Float double_t
Definition: cmath.hpp:154
R operator()(const A &x) const
Definition: cmath.hpp:211
STL namespace.
float operator()(const A &x) const
Definition: cmath.hpp:217
long lround(double x)
double trunc(double x)
double operator()(const A &x) const
Definition: cmath.hpp:223
R nearest_cast(const A &x)
Definition: cmath.hpp:203
long lround_half_up(double x)
Definition: cmath.hpp:185
Float float_t
Definition: cmath.hpp:155
float round(float x)
long lround(float x)
double round_half_up(double x)
Definition: cmath.hpp:179

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google