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

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