1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/numinquire.h Numeric inquiry functions |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
8 | * |
---|
9 | * This file is a part of Blitz. |
---|
10 | * |
---|
11 | * Blitz is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License |
---|
13 | * as published by the Free Software Foundation, either version 3 |
---|
14 | * of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * Blitz is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public |
---|
22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
26 | * |
---|
27 | * For more information, please see the Blitz++ Home Page: |
---|
28 | * https://sourceforge.net/projects/blitz/ |
---|
29 | * |
---|
30 | ***************************************************************************/ |
---|
31 | |
---|
32 | /* |
---|
33 | * These numeric inquiry functions are provided as an alternative |
---|
34 | * to the somewhat klunky numeric_limits<T>::yadda_yadda syntax. |
---|
35 | * Where a similar Fortran 90 function exists, the same name has |
---|
36 | * been used. |
---|
37 | * |
---|
38 | * The argument in all cases is a dummy of the appropriate type |
---|
39 | * (double, int, etc.) |
---|
40 | * |
---|
41 | * These functions assume that numeric_limits<T> has been specialized |
---|
42 | * for the appropriate case. If not, the results are not useful. |
---|
43 | */ |
---|
44 | |
---|
45 | #ifndef BZ_NUMINQUIRE_H |
---|
46 | #define BZ_NUMINQUIRE_H |
---|
47 | |
---|
48 | #ifndef BZ_BLITZ_H |
---|
49 | #include <blitz/blitz.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifndef BZ_HAVE_NUMERIC_LIMITS |
---|
53 | #include <blitz/limits-hack.h> |
---|
54 | #else |
---|
55 | #include <limits> |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifndef BZ_RANGE_H |
---|
59 | #include <blitz/range.h> |
---|
60 | #endif |
---|
61 | |
---|
62 | BZ_NAMESPACE(blitz) |
---|
63 | |
---|
64 | /* |
---|
65 | * This traits class provides zero and one values for numeric |
---|
66 | * types. This was previously a template function with specializations, |
---|
67 | * but the specializations were causing multiply-defined symbols |
---|
68 | * at link time. TV 980226 |
---|
69 | */ |
---|
70 | |
---|
71 | template<typename T_numtype> |
---|
72 | struct _bz_OneZeroTraits { |
---|
73 | static inline T_numtype zero() { return 0; } |
---|
74 | static inline T_numtype one() { return 1; } |
---|
75 | }; |
---|
76 | |
---|
77 | #ifdef BZ_HAVE_COMPLEX |
---|
78 | |
---|
79 | template<> |
---|
80 | struct _bz_OneZeroTraits<complex<float> > { |
---|
81 | static inline complex<float> zero() { return complex<float>(0.0f,0.0f); } |
---|
82 | static inline complex<float> one() { return complex<float>(1.0f,0.0f); } |
---|
83 | }; |
---|
84 | |
---|
85 | template<> |
---|
86 | struct _bz_OneZeroTraits<complex<double> > { |
---|
87 | static inline complex<double> zero() { return complex<double>(0.0,0.0); } |
---|
88 | static inline complex<double> one() { return complex<double>(1.0,0.0); } |
---|
89 | }; |
---|
90 | |
---|
91 | template<> |
---|
92 | struct _bz_OneZeroTraits<complex<long double> > { |
---|
93 | static inline complex<long double> zero() |
---|
94 | { return complex<long double>(0.0,0.0); } |
---|
95 | |
---|
96 | static inline complex<long double> one() |
---|
97 | { return complex<long double>(1.0,0.0); } |
---|
98 | }; |
---|
99 | |
---|
100 | #endif // BZ_HAVE_COMPLEX |
---|
101 | |
---|
102 | template<typename T> |
---|
103 | inline T zero(T) |
---|
104 | { |
---|
105 | return _bz_OneZeroTraits<T>::zero(); |
---|
106 | } |
---|
107 | |
---|
108 | template<typename T> |
---|
109 | inline T one(T) |
---|
110 | { |
---|
111 | return _bz_OneZeroTraits<T>::one(); |
---|
112 | } |
---|
113 | |
---|
114 | template<typename T> |
---|
115 | inline int digits(T) |
---|
116 | { |
---|
117 | return numeric_limits<T>::digits; |
---|
118 | } |
---|
119 | |
---|
120 | template<typename T> |
---|
121 | inline int digits10(T) |
---|
122 | { |
---|
123 | return numeric_limits<T>::digits10; |
---|
124 | } |
---|
125 | |
---|
126 | template<typename T> |
---|
127 | inline T epsilon(T) BZ_THROW |
---|
128 | { |
---|
129 | return numeric_limits<T>::epsilon(); |
---|
130 | } |
---|
131 | |
---|
132 | // neghuge() by Theodore Papadopoulo, to fix a problem with |
---|
133 | // max() reductions. |
---|
134 | template<typename T> |
---|
135 | inline T neghuge(T) BZ_THROW |
---|
136 | { |
---|
137 | return numeric_limits<T>::is_integer ? (numeric_limits<T>::min)() |
---|
138 | : - (numeric_limits<T>::max)(); |
---|
139 | } |
---|
140 | |
---|
141 | template<typename T> |
---|
142 | inline T huge(T) BZ_THROW |
---|
143 | { |
---|
144 | return (numeric_limits<T>::max)(); |
---|
145 | } |
---|
146 | |
---|
147 | template<typename T> |
---|
148 | inline T tiny(T) BZ_THROW |
---|
149 | { |
---|
150 | return (numeric_limits<T>::min)(); |
---|
151 | } |
---|
152 | |
---|
153 | template<typename T> |
---|
154 | inline int max_exponent(T) |
---|
155 | { |
---|
156 | return numeric_limits<T>::max_exponent; |
---|
157 | } |
---|
158 | |
---|
159 | template<typename T> |
---|
160 | inline int min_exponent(T) |
---|
161 | { |
---|
162 | return numeric_limits<T>::min_exponent; |
---|
163 | } |
---|
164 | |
---|
165 | template<typename T> |
---|
166 | inline int min_exponent10(T) |
---|
167 | { |
---|
168 | return numeric_limits<T>::min_exponent10; |
---|
169 | } |
---|
170 | |
---|
171 | template<typename T> |
---|
172 | inline int max_exponent10(T) |
---|
173 | { |
---|
174 | return numeric_limits<T>::max_exponent10; |
---|
175 | } |
---|
176 | |
---|
177 | template<typename T> |
---|
178 | inline int precision(T) |
---|
179 | { |
---|
180 | return numeric_limits<T>::digits10; |
---|
181 | } |
---|
182 | |
---|
183 | template<typename T> |
---|
184 | inline int radix(T) |
---|
185 | { |
---|
186 | return numeric_limits<T>::radix; |
---|
187 | } |
---|
188 | |
---|
189 | template<typename T> |
---|
190 | inline Range range(T) |
---|
191 | { |
---|
192 | return Range(numeric_limits<T>::min_exponent10, |
---|
193 | numeric_limits<T>::max_exponent10); |
---|
194 | } |
---|
195 | |
---|
196 | template<typename T> |
---|
197 | inline bool is_signed(T) { |
---|
198 | return numeric_limits<T>::is_signed; |
---|
199 | } |
---|
200 | |
---|
201 | template<typename T> |
---|
202 | inline bool is_integer(T) { |
---|
203 | return numeric_limits<T>::is_integer; |
---|
204 | } |
---|
205 | |
---|
206 | template<typename T> |
---|
207 | inline bool is_exact(T) { |
---|
208 | return numeric_limits<T>::is_exact; |
---|
209 | } |
---|
210 | |
---|
211 | template<typename T> |
---|
212 | inline T round_error(T) BZ_THROW |
---|
213 | { |
---|
214 | return numeric_limits<T>::round_error(); |
---|
215 | } |
---|
216 | |
---|
217 | template<typename T> |
---|
218 | inline bool has_infinity(T) { |
---|
219 | return numeric_limits<T>::has_infinity; |
---|
220 | } |
---|
221 | |
---|
222 | template<typename T> |
---|
223 | inline bool has_quiet_NaN(T) { |
---|
224 | return numeric_limits<T>::has_quiet_NaN; |
---|
225 | } |
---|
226 | |
---|
227 | template<typename T> |
---|
228 | inline bool has_signaling_NaN(T) { |
---|
229 | return numeric_limits<T>::has_signaling_NaN; |
---|
230 | } |
---|
231 | |
---|
232 | // Provided for non-US english users |
---|
233 | template<typename T> |
---|
234 | inline bool has_signalling_NaN(T) { |
---|
235 | return numeric_limits<T>::has_signaling_NaN; |
---|
236 | } |
---|
237 | |
---|
238 | template<typename T> |
---|
239 | inline bool has_denorm(T) { |
---|
240 | return numeric_limits<T>::has_denorm; |
---|
241 | } |
---|
242 | |
---|
243 | template<typename T> |
---|
244 | inline bool has_denorm_loss(T) { |
---|
245 | return numeric_limits<T>::has_denorm_loss; |
---|
246 | } |
---|
247 | |
---|
248 | template<typename T> |
---|
249 | inline T infinity(T) BZ_THROW |
---|
250 | { |
---|
251 | return numeric_limits<T>::infinity(); |
---|
252 | } |
---|
253 | |
---|
254 | template<typename T> |
---|
255 | inline T quiet_NaN(T) BZ_THROW |
---|
256 | { |
---|
257 | return numeric_limits<T>::quiet_NaN(); |
---|
258 | } |
---|
259 | |
---|
260 | template<typename T> |
---|
261 | inline T signaling_NaN(T) BZ_THROW |
---|
262 | { |
---|
263 | return numeric_limits<T>::signaling_NaN(); |
---|
264 | } |
---|
265 | |
---|
266 | template<typename T> |
---|
267 | inline T signalling_NaN(T) BZ_THROW |
---|
268 | { |
---|
269 | return numeric_limits<T>::signaling_NaN(); |
---|
270 | } |
---|
271 | |
---|
272 | template<typename T> |
---|
273 | inline T denorm_min(T) BZ_THROW |
---|
274 | { |
---|
275 | return numeric_limits<T>::denorm_min(); |
---|
276 | } |
---|
277 | |
---|
278 | template<typename T> |
---|
279 | inline bool is_iec559(T) { |
---|
280 | return numeric_limits<T>::is_iec559; |
---|
281 | } |
---|
282 | |
---|
283 | template<typename T> |
---|
284 | inline bool is_bounded(T) { |
---|
285 | return numeric_limits<T>::is_bounded; |
---|
286 | } |
---|
287 | |
---|
288 | template<typename T> |
---|
289 | inline bool is_modulo(T) { |
---|
290 | return numeric_limits<T>::is_modulo; |
---|
291 | } |
---|
292 | |
---|
293 | template<typename T> |
---|
294 | inline bool traps(T) { |
---|
295 | return numeric_limits<T>::traps; |
---|
296 | } |
---|
297 | |
---|
298 | template<typename T> |
---|
299 | inline bool tinyness_before(T) { |
---|
300 | return numeric_limits<T>::tinyness_before; |
---|
301 | } |
---|
302 | |
---|
303 | template<typename T> |
---|
304 | inline BZ_STD_SCOPE(float_round_style) round_style(T) |
---|
305 | { |
---|
306 | return numeric_limits<T>::round_style; |
---|
307 | } |
---|
308 | |
---|
309 | BZ_NAMESPACE_END |
---|
310 | |
---|
311 | #endif // BZ_NUMINQUIRE_H |
---|
312 | |
---|