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

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