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 #if __cplusplus >= 20103L
43 #define ADOBE_HAS_C99_STD_MATH_H
44 #else
45 #define ADOBE_HAS_C99_MATH_H
46 #endif
47 #include <cmath>
48 
49 #endif
50 
51 #elif defined(_MSC_VER)
52 #include <cmath>
53 /*
54  The currently supported version of VC++ has no C99 extensions.
55 */
56 
57 #if _MSC_VER > 1600
58 #error "Unknown MSC compiler configureation for cmath (last knownversion is VC++ 10.0)."
59 #endif
60 
61 #define ADOBE_HAS_CPP_CMATH
62 
63 #else
64 #error "Unknown compiler configuration for cmath."
65 #endif
66 
67 /*************************************************************************************************/
68 
69 #if defined(ADOBE_HAS_C99_STD_MATH_H)
70 
71 namespace adobe {
72 
73 using std::float_t;
74 using std::double_t;
75 
76 using std::round;
77 using std::lround;
78 using std::trunc;
79 
80 } // namespace adobe
81 
82 /*************************************************************************************************/
83 
84 #elif defined(ADOBE_HAS_CPP_CMATH)
85 
86 namespace adobe {
87 
88 typedef float float_t;
89 typedef double double_t;
90 
91 /*************************************************************************************************/
92 
93 inline float trunc(float x)
94 { return x < 0.0f ? std::ceil(x) : std::floor(x); }
95 
96 inline double trunc(double x)
97 { return x < 0.0 ? std::ceil(x) : std::floor(x); }
98 
99 /*************************************************************************************************/
100 
101 inline float round(float x)
102 { return trunc(x + (x < 0.0f ? -0.5f : 0.5f)); }
103 
104 inline double round(double x)
105 { return trunc(x + (x < 0.0 ? -0.5 : 0.5)); }
106 
107 /*************************************************************************************************/
108 
109 inline long lround(float x)
110 { return static_cast<long>(x + (x < 0.0f ? -0.5f : 0.5f)); }
111 
112 inline long lround(double x)
113 { return static_cast<long>(x + (x < 0.0 ? -0.5 : 0.5)); }
114 
115 /*************************************************************************************************/
116 
117 } // namespace adobe
118 
119 /*************************************************************************************************/
120 
121 #elif defined(ADOBE_HAS_C99_MATH_H)
122 
123 #include <math.h>
124 
125 namespace adobe {
126 
129 
130 /*************************************************************************************************/
131 
135 
136 inline float round(float x) { return ::roundf(x); }
137 inline long lround(float x) { return ::lroundf(x); }
138 inline float trunc(float x) { return ::truncf(x); }
139 
140 /*************************************************************************************************/
141 
142 } // namespace adobe
143 
144 #elif defined(ADOBE_NO_DOCUMENTATION)
145 
146 namespace adobe {
147 
158 typedef Float double_t;
159 typedef Float float_t;
160 
161 double round(double x);
162 float round(float x);
163 long lround(double x);
164 long lround(float x);
165 double trunc(double x);
166 float trunc(float x);
169 } // namespace adobe
170 
171 #endif
172 
173 /*************************************************************************************************/
174 
175 namespace adobe {
176 
177 /*************************************************************************************************/
178 
179 template <typename A, typename R> struct nearest_cast_fn;
180 
181 /*************************************************************************************************/
182 
183 inline double round_half_up(double x)
184 { return std::floor(x + 0.5); }
185 
186 inline float round_half_up(float x)
187 { return std::floor(x + 0.5f); }
188 
189 inline long lround_half_up(double x)
190 { return static_cast<long>(std::floor(x + 0.5)); }
191 
192 inline long lround_half_up(float x)
193 { return static_cast<long>(std::floor(x + 0.5f)); }
194 
195 /*
196  REVISIT (sparent) : Should complete the rounding modes by providing a round_half_even()
197  function.
198 
199  Names are borrowed from the EDA rounding modes:
200 
201  <http://www.gobosoft.com/eiffel/gobo/math/decimal/>
202 */
203 
204 /*************************************************************************************************/
205 
206 template <typename R, typename A>
207 inline R nearest_cast(const A& x)
208 { return nearest_cast_fn<A, R>()(x); }
209 
210 /*************************************************************************************************/
211 
212 template <typename A, typename R>
213 struct nearest_cast_fn : std::unary_function<A, R>
214 {
215  R operator()(const A& x) const { return static_cast<R>(round_half_up(x)); }
216 };
217 
218 template <typename A>
219 struct nearest_cast_fn<A, float> : std::unary_function<A, float>
220 {
221  float operator()(const A& x) const { return static_cast<float>(x); }
222 };
223 
224 template <typename A>
225 struct nearest_cast_fn<A, double> : std::unary_function<A, double>
226 {
227  double operator()(const A& x) const { return static_cast<double>(x); }
228 };
229 
230 /*************************************************************************************************/
231 
232 } // namespace adobe
233 
234 /*************************************************************************************************/
235 
236 #endif
237 
238 /*************************************************************************************************/
float trunc(float x)
double round(double x)
Float double_t
Definition: cmath.hpp:158
R operator()(const A &x) const
Definition: cmath.hpp:215
STL namespace.
float operator()(const A &x) const
Definition: cmath.hpp:221
long lround(double x)
double trunc(double x)
double operator()(const A &x) const
Definition: cmath.hpp:227
R nearest_cast(const A &x)
Definition: cmath.hpp:207
long lround_half_up(double x)
Definition: cmath.hpp:189
Float float_t
Definition: cmath.hpp:159
float round(float x)
long lround(float x)
double round_half_up(double x)
Definition: cmath.hpp:183

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