Chris@16
|
1 // Copyright John Maddock 2005-2008.
|
Chris@16
|
2 // Copyright (c) 2006-2008 Johan Rade
|
Chris@16
|
3 // Use, modification and distribution are subject to the
|
Chris@16
|
4 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_MATH_FPCLASSIFY_HPP
|
Chris@16
|
8 #define BOOST_MATH_FPCLASSIFY_HPP
|
Chris@16
|
9
|
Chris@16
|
10 #ifdef _MSC_VER
|
Chris@16
|
11 #pragma once
|
Chris@16
|
12 #endif
|
Chris@16
|
13
|
Chris@16
|
14 #include <math.h>
|
Chris@16
|
15 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
16 #include <boost/limits.hpp>
|
Chris@16
|
17 #include <boost/math/tools/real_cast.hpp>
|
Chris@16
|
18 #include <boost/type_traits/is_floating_point.hpp>
|
Chris@16
|
19 #include <boost/math/special_functions/math_fwd.hpp>
|
Chris@16
|
20 #include <boost/math/special_functions/detail/fp_traits.hpp>
|
Chris@16
|
21 /*!
|
Chris@16
|
22 \file fpclassify.hpp
|
Chris@16
|
23 \brief Classify floating-point value as normal, subnormal, zero, infinite, or NaN.
|
Chris@16
|
24 \version 1.0
|
Chris@16
|
25 \author John Maddock
|
Chris@16
|
26 */
|
Chris@16
|
27
|
Chris@16
|
28 /*
|
Chris@16
|
29
|
Chris@16
|
30 1. If the platform is C99 compliant, then the native floating point
|
Chris@16
|
31 classification functions are used. However, note that we must only
|
Chris@16
|
32 define the functions which call std::fpclassify etc if that function
|
Chris@16
|
33 really does exist: otherwise a compiler may reject the code even though
|
Chris@16
|
34 the template is never instantiated.
|
Chris@16
|
35
|
Chris@16
|
36 2. If the platform is not C99 compliant, and the binary format for
|
Chris@16
|
37 a floating point type (float, double or long double) can be determined
|
Chris@16
|
38 at compile time, then the following algorithm is used:
|
Chris@16
|
39
|
Chris@16
|
40 If all exponent bits, the flag bit (if there is one),
|
Chris@16
|
41 and all significand bits are 0, then the number is zero.
|
Chris@16
|
42
|
Chris@16
|
43 If all exponent bits and the flag bit (if there is one) are 0,
|
Chris@16
|
44 and at least one significand bit is 1, then the number is subnormal.
|
Chris@16
|
45
|
Chris@16
|
46 If all exponent bits are 1 and all significand bits are 0,
|
Chris@16
|
47 then the number is infinity.
|
Chris@16
|
48
|
Chris@16
|
49 If all exponent bits are 1 and at least one significand bit is 1,
|
Chris@16
|
50 then the number is a not-a-number.
|
Chris@16
|
51
|
Chris@16
|
52 Otherwise the number is normal.
|
Chris@16
|
53
|
Chris@16
|
54 This algorithm works for the IEEE 754 representation,
|
Chris@16
|
55 and also for several non IEEE 754 formats.
|
Chris@16
|
56
|
Chris@16
|
57 Most formats have the structure
|
Chris@16
|
58 sign bit + exponent bits + significand bits.
|
Chris@16
|
59
|
Chris@16
|
60 A few have the structure
|
Chris@16
|
61 sign bit + exponent bits + flag bit + significand bits.
|
Chris@16
|
62 The flag bit is 0 for zero and subnormal numbers,
|
Chris@16
|
63 and 1 for normal numbers and NaN.
|
Chris@16
|
64 It is 0 (Motorola 68K) or 1 (Intel) for infinity.
|
Chris@16
|
65
|
Chris@16
|
66 To get the bits, the four or eight most significant bytes are copied
|
Chris@16
|
67 into an uint32_t or uint64_t and bit masks are applied.
|
Chris@16
|
68 This covers all the exponent bits and the flag bit (if there is one),
|
Chris@16
|
69 but not always all the significand bits.
|
Chris@16
|
70 Some of the functions below have two implementations,
|
Chris@16
|
71 depending on whether all the significand bits are copied or not.
|
Chris@16
|
72
|
Chris@16
|
73 3. If the platform is not C99 compliant, and the binary format for
|
Chris@16
|
74 a floating point type (float, double or long double) can not be determined
|
Chris@16
|
75 at compile time, then comparison with std::numeric_limits values
|
Chris@16
|
76 is used.
|
Chris@16
|
77
|
Chris@16
|
78 */
|
Chris@16
|
79
|
Chris@16
|
80 #if defined(_MSC_VER) || defined(__BORLANDC__)
|
Chris@16
|
81 #include <float.h>
|
Chris@16
|
82 #endif
|
Chris@16
|
83
|
Chris@16
|
84 #ifdef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
85 namespace std{ using ::abs; using ::fabs; }
|
Chris@16
|
86 #endif
|
Chris@16
|
87
|
Chris@16
|
88 namespace boost{
|
Chris@16
|
89
|
Chris@16
|
90 //
|
Chris@16
|
91 // This must not be located in any namespace under boost::math
|
Chris@16
|
92 // otherwise we can get into an infinite loop if isnan is
|
Chris@16
|
93 // a #define for "isnan" !
|
Chris@16
|
94 //
|
Chris@16
|
95 namespace math_detail{
|
Chris@16
|
96
|
Chris@16
|
97 #ifdef BOOST_MSVC
|
Chris@16
|
98 #pragma warning(push)
|
Chris@16
|
99 #pragma warning(disable:4800)
|
Chris@16
|
100 #endif
|
Chris@16
|
101
|
Chris@16
|
102 template <class T>
|
Chris@16
|
103 inline bool is_nan_helper(T t, const boost::true_type&)
|
Chris@16
|
104 {
|
Chris@16
|
105 #ifdef isnan
|
Chris@16
|
106 return isnan(t);
|
Chris@16
|
107 #elif defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY) || !defined(BOOST_HAS_FPCLASSIFY)
|
Chris@16
|
108 (void)t;
|
Chris@16
|
109 return false;
|
Chris@16
|
110 #else // BOOST_HAS_FPCLASSIFY
|
Chris@16
|
111 return (BOOST_FPCLASSIFY_PREFIX fpclassify(t) == (int)FP_NAN);
|
Chris@16
|
112 #endif
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 #ifdef BOOST_MSVC
|
Chris@16
|
116 #pragma warning(pop)
|
Chris@16
|
117 #endif
|
Chris@16
|
118
|
Chris@16
|
119 template <class T>
|
Chris@16
|
120 inline bool is_nan_helper(T, const boost::false_type&)
|
Chris@16
|
121 {
|
Chris@16
|
122 return false;
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 namespace math{
|
Chris@16
|
128
|
Chris@16
|
129 namespace detail{
|
Chris@16
|
130
|
Chris@16
|
131 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
|
Chris@16
|
132 template <class T>
|
Chris@16
|
133 inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const native_tag&)
|
Chris@16
|
134 {
|
Chris@16
|
135 return (std::fpclassify)(t);
|
Chris@16
|
136 }
|
Chris@16
|
137 #endif
|
Chris@16
|
138
|
Chris@16
|
139 template <class T>
|
Chris@16
|
140 inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<true>&)
|
Chris@16
|
141 {
|
Chris@16
|
142 BOOST_MATH_INSTRUMENT_VARIABLE(t);
|
Chris@16
|
143
|
Chris@16
|
144 // whenever possible check for Nan's first:
|
Chris@16
|
145 #if defined(BOOST_HAS_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
|
Chris@16
|
146 if(::boost::math_detail::is_nan_helper(t, ::boost::is_floating_point<T>()))
|
Chris@16
|
147 return FP_NAN;
|
Chris@16
|
148 #elif defined(isnan)
|
Chris@16
|
149 if(boost::math_detail::is_nan_helper(t, ::boost::is_floating_point<T>()))
|
Chris@16
|
150 return FP_NAN;
|
Chris@16
|
151 #elif defined(_MSC_VER) || defined(__BORLANDC__)
|
Chris@16
|
152 if(::_isnan(boost::math::tools::real_cast<double>(t)))
|
Chris@16
|
153 return FP_NAN;
|
Chris@16
|
154 #endif
|
Chris@16
|
155 // std::fabs broken on a few systems especially for long long!!!!
|
Chris@16
|
156 T at = (t < T(0)) ? -t : t;
|
Chris@16
|
157
|
Chris@16
|
158 // Use a process of exclusion to figure out
|
Chris@16
|
159 // what kind of type we have, this relies on
|
Chris@16
|
160 // IEEE conforming reals that will treat
|
Chris@16
|
161 // Nan's as unordered. Some compilers
|
Chris@16
|
162 // don't do this once optimisations are
|
Chris@16
|
163 // turned on, hence the check for nan's above.
|
Chris@16
|
164 if(at <= (std::numeric_limits<T>::max)())
|
Chris@16
|
165 {
|
Chris@16
|
166 if(at >= (std::numeric_limits<T>::min)())
|
Chris@16
|
167 return FP_NORMAL;
|
Chris@16
|
168 return (at != 0) ? FP_SUBNORMAL : FP_ZERO;
|
Chris@16
|
169 }
|
Chris@16
|
170 else if(at > (std::numeric_limits<T>::max)())
|
Chris@16
|
171 return FP_INFINITE;
|
Chris@16
|
172 return FP_NAN;
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 template <class T>
|
Chris@16
|
176 inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<false>&)
|
Chris@16
|
177 {
|
Chris@16
|
178 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
179 if(std::numeric_limits<T>::is_specialized)
|
Chris@16
|
180 return fpclassify_imp(t, generic_tag<true>());
|
Chris@16
|
181 #endif
|
Chris@16
|
182 //
|
Chris@16
|
183 // An unknown type with no numeric_limits support,
|
Chris@16
|
184 // so what are we supposed to do we do here?
|
Chris@16
|
185 //
|
Chris@16
|
186 BOOST_MATH_INSTRUMENT_VARIABLE(t);
|
Chris@16
|
187
|
Chris@16
|
188 return t == 0 ? FP_ZERO : FP_NORMAL;
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 template<class T>
|
Chris@16
|
192 int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_all_bits_tag)
|
Chris@16
|
193 {
|
Chris@16
|
194 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
195
|
Chris@16
|
196 BOOST_MATH_INSTRUMENT_VARIABLE(x);
|
Chris@16
|
197
|
Chris@16
|
198 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
199 traits::get_bits(x,a);
|
Chris@16
|
200 BOOST_MATH_INSTRUMENT_VARIABLE(a);
|
Chris@16
|
201 a &= traits::exponent | traits::flag | traits::significand;
|
Chris@16
|
202 BOOST_MATH_INSTRUMENT_VARIABLE((traits::exponent | traits::flag | traits::significand));
|
Chris@16
|
203 BOOST_MATH_INSTRUMENT_VARIABLE(a);
|
Chris@16
|
204
|
Chris@16
|
205 if(a <= traits::significand) {
|
Chris@16
|
206 if(a == 0)
|
Chris@16
|
207 return FP_ZERO;
|
Chris@16
|
208 else
|
Chris@16
|
209 return FP_SUBNORMAL;
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 if(a < traits::exponent) return FP_NORMAL;
|
Chris@16
|
213
|
Chris@16
|
214 a &= traits::significand;
|
Chris@16
|
215 if(a == 0) return FP_INFINITE;
|
Chris@16
|
216
|
Chris@16
|
217 return FP_NAN;
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 template<class T>
|
Chris@16
|
221 int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_leading_bits_tag)
|
Chris@16
|
222 {
|
Chris@16
|
223 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
224
|
Chris@16
|
225 BOOST_MATH_INSTRUMENT_VARIABLE(x);
|
Chris@16
|
226
|
Chris@16
|
227 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
228 traits::get_bits(x,a);
|
Chris@16
|
229 a &= traits::exponent | traits::flag | traits::significand;
|
Chris@16
|
230
|
Chris@16
|
231 if(a <= traits::significand) {
|
Chris@16
|
232 if(x == 0)
|
Chris@16
|
233 return FP_ZERO;
|
Chris@16
|
234 else
|
Chris@16
|
235 return FP_SUBNORMAL;
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 if(a < traits::exponent) return FP_NORMAL;
|
Chris@16
|
239
|
Chris@16
|
240 a &= traits::significand;
|
Chris@16
|
241 traits::set_bits(x,a);
|
Chris@16
|
242 if(x == 0) return FP_INFINITE;
|
Chris@16
|
243
|
Chris@16
|
244 return FP_NAN;
|
Chris@16
|
245 }
|
Chris@16
|
246
|
Chris@16
|
247 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && (defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) || defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS))
|
Chris@16
|
248 inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
|
Chris@16
|
249 {
|
Chris@16
|
250 return boost::math::detail::fpclassify_imp(t, generic_tag<true>());
|
Chris@16
|
251 }
|
Chris@16
|
252 #endif
|
Chris@16
|
253
|
Chris@16
|
254 } // namespace detail
|
Chris@16
|
255
|
Chris@16
|
256 template <class T>
|
Chris@16
|
257 inline int fpclassify BOOST_NO_MACRO_EXPAND(T t)
|
Chris@16
|
258 {
|
Chris@16
|
259 typedef typename detail::fp_traits<T>::type traits;
|
Chris@16
|
260 typedef typename traits::method method;
|
Chris@16
|
261 typedef typename tools::promote_args_permissive<T>::type value_type;
|
Chris@16
|
262 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
263 if(std::numeric_limits<T>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0)))
|
Chris@16
|
264 return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
|
Chris@16
|
265 return detail::fpclassify_imp(static_cast<value_type>(t), method());
|
Chris@16
|
266 #else
|
Chris@16
|
267 return detail::fpclassify_imp(static_cast<value_type>(t), method());
|
Chris@16
|
268 #endif
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
Chris@16
|
272 template <>
|
Chris@16
|
273 inline int fpclassify<long double> BOOST_NO_MACRO_EXPAND(long double t)
|
Chris@16
|
274 {
|
Chris@16
|
275 typedef detail::fp_traits<long double>::type traits;
|
Chris@16
|
276 typedef traits::method method;
|
Chris@16
|
277 typedef long double value_type;
|
Chris@16
|
278 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
279 if(std::numeric_limits<long double>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0)))
|
Chris@16
|
280 return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
|
Chris@16
|
281 return detail::fpclassify_imp(static_cast<value_type>(t), method());
|
Chris@16
|
282 #else
|
Chris@16
|
283 return detail::fpclassify_imp(static_cast<value_type>(t), method());
|
Chris@16
|
284 #endif
|
Chris@16
|
285 }
|
Chris@16
|
286 #endif
|
Chris@16
|
287
|
Chris@16
|
288 namespace detail {
|
Chris@16
|
289
|
Chris@16
|
290 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
|
Chris@16
|
291 template<class T>
|
Chris@16
|
292 inline bool isfinite_impl(T x, native_tag const&)
|
Chris@16
|
293 {
|
Chris@16
|
294 return (std::isfinite)(x);
|
Chris@16
|
295 }
|
Chris@16
|
296 #endif
|
Chris@16
|
297
|
Chris@16
|
298 template<class T>
|
Chris@16
|
299 inline bool isfinite_impl(T x, generic_tag<true> const&)
|
Chris@16
|
300 {
|
Chris@16
|
301 return x >= -(std::numeric_limits<T>::max)()
|
Chris@16
|
302 && x <= (std::numeric_limits<T>::max)();
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 template<class T>
|
Chris@16
|
306 inline bool isfinite_impl(T x, generic_tag<false> const&)
|
Chris@16
|
307 {
|
Chris@16
|
308 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
309 if(std::numeric_limits<T>::is_specialized)
|
Chris@16
|
310 return isfinite_impl(x, generic_tag<true>());
|
Chris@16
|
311 #endif
|
Chris@101
|
312 (void)x; // warning suppression.
|
Chris@16
|
313 return true;
|
Chris@16
|
314 }
|
Chris@16
|
315
|
Chris@16
|
316 template<class T>
|
Chris@16
|
317 inline bool isfinite_impl(T x, ieee_tag const&)
|
Chris@16
|
318 {
|
Chris@16
|
319 typedef BOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits;
|
Chris@16
|
320 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
321 traits::get_bits(x,a);
|
Chris@16
|
322 a &= traits::exponent;
|
Chris@16
|
323 return a != traits::exponent;
|
Chris@16
|
324 }
|
Chris@16
|
325
|
Chris@16
|
326 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
|
Chris@16
|
327 inline bool isfinite_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
|
Chris@16
|
328 {
|
Chris@16
|
329 return boost::math::detail::isfinite_impl(t, generic_tag<true>());
|
Chris@16
|
330 }
|
Chris@16
|
331 #endif
|
Chris@16
|
332
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 template<class T>
|
Chris@16
|
336 inline bool (isfinite)(T x)
|
Chris@16
|
337 { //!< \brief return true if floating-point type t is finite.
|
Chris@16
|
338 typedef typename detail::fp_traits<T>::type traits;
|
Chris@16
|
339 typedef typename traits::method method;
|
Chris@16
|
340 // typedef typename boost::is_floating_point<T>::type fp_tag;
|
Chris@16
|
341 typedef typename tools::promote_args_permissive<T>::type value_type;
|
Chris@16
|
342 return detail::isfinite_impl(static_cast<value_type>(x), method());
|
Chris@16
|
343 }
|
Chris@16
|
344
|
Chris@16
|
345 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
Chris@16
|
346 template<>
|
Chris@16
|
347 inline bool (isfinite)(long double x)
|
Chris@16
|
348 { //!< \brief return true if floating-point type t is finite.
|
Chris@16
|
349 typedef detail::fp_traits<long double>::type traits;
|
Chris@16
|
350 typedef traits::method method;
|
Chris@101
|
351 //typedef boost::is_floating_point<long double>::type fp_tag;
|
Chris@16
|
352 typedef long double value_type;
|
Chris@16
|
353 return detail::isfinite_impl(static_cast<value_type>(x), method());
|
Chris@16
|
354 }
|
Chris@16
|
355 #endif
|
Chris@16
|
356
|
Chris@16
|
357 //------------------------------------------------------------------------------
|
Chris@16
|
358
|
Chris@16
|
359 namespace detail {
|
Chris@16
|
360
|
Chris@16
|
361 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
|
Chris@16
|
362 template<class T>
|
Chris@16
|
363 inline bool isnormal_impl(T x, native_tag const&)
|
Chris@16
|
364 {
|
Chris@16
|
365 return (std::isnormal)(x);
|
Chris@16
|
366 }
|
Chris@16
|
367 #endif
|
Chris@16
|
368
|
Chris@16
|
369 template<class T>
|
Chris@16
|
370 inline bool isnormal_impl(T x, generic_tag<true> const&)
|
Chris@16
|
371 {
|
Chris@16
|
372 if(x < 0) x = -x;
|
Chris@16
|
373 return x >= (std::numeric_limits<T>::min)()
|
Chris@16
|
374 && x <= (std::numeric_limits<T>::max)();
|
Chris@16
|
375 }
|
Chris@16
|
376
|
Chris@16
|
377 template<class T>
|
Chris@16
|
378 inline bool isnormal_impl(T x, generic_tag<false> const&)
|
Chris@16
|
379 {
|
Chris@16
|
380 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
381 if(std::numeric_limits<T>::is_specialized)
|
Chris@16
|
382 return isnormal_impl(x, generic_tag<true>());
|
Chris@16
|
383 #endif
|
Chris@16
|
384 return !(x == 0);
|
Chris@16
|
385 }
|
Chris@16
|
386
|
Chris@16
|
387 template<class T>
|
Chris@16
|
388 inline bool isnormal_impl(T x, ieee_tag const&)
|
Chris@16
|
389 {
|
Chris@16
|
390 typedef BOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits;
|
Chris@16
|
391 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
392 traits::get_bits(x,a);
|
Chris@16
|
393 a &= traits::exponent | traits::flag;
|
Chris@16
|
394 return (a != 0) && (a < traits::exponent);
|
Chris@16
|
395 }
|
Chris@16
|
396
|
Chris@16
|
397 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
|
Chris@16
|
398 inline bool isnormal_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
|
Chris@16
|
399 {
|
Chris@16
|
400 return boost::math::detail::isnormal_impl(t, generic_tag<true>());
|
Chris@16
|
401 }
|
Chris@16
|
402 #endif
|
Chris@16
|
403
|
Chris@16
|
404 }
|
Chris@16
|
405
|
Chris@16
|
406 template<class T>
|
Chris@16
|
407 inline bool (isnormal)(T x)
|
Chris@16
|
408 {
|
Chris@16
|
409 typedef typename detail::fp_traits<T>::type traits;
|
Chris@16
|
410 typedef typename traits::method method;
|
Chris@16
|
411 //typedef typename boost::is_floating_point<T>::type fp_tag;
|
Chris@16
|
412 typedef typename tools::promote_args_permissive<T>::type value_type;
|
Chris@16
|
413 return detail::isnormal_impl(static_cast<value_type>(x), method());
|
Chris@16
|
414 }
|
Chris@16
|
415
|
Chris@16
|
416 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
Chris@16
|
417 template<>
|
Chris@16
|
418 inline bool (isnormal)(long double x)
|
Chris@16
|
419 {
|
Chris@16
|
420 typedef detail::fp_traits<long double>::type traits;
|
Chris@16
|
421 typedef traits::method method;
|
Chris@101
|
422 //typedef boost::is_floating_point<long double>::type fp_tag;
|
Chris@16
|
423 typedef long double value_type;
|
Chris@16
|
424 return detail::isnormal_impl(static_cast<value_type>(x), method());
|
Chris@16
|
425 }
|
Chris@16
|
426 #endif
|
Chris@16
|
427
|
Chris@16
|
428 //------------------------------------------------------------------------------
|
Chris@16
|
429
|
Chris@16
|
430 namespace detail {
|
Chris@16
|
431
|
Chris@16
|
432 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
|
Chris@16
|
433 template<class T>
|
Chris@16
|
434 inline bool isinf_impl(T x, native_tag const&)
|
Chris@16
|
435 {
|
Chris@16
|
436 return (std::isinf)(x);
|
Chris@16
|
437 }
|
Chris@16
|
438 #endif
|
Chris@16
|
439
|
Chris@16
|
440 template<class T>
|
Chris@16
|
441 inline bool isinf_impl(T x, generic_tag<true> const&)
|
Chris@16
|
442 {
|
Chris@16
|
443 (void)x; // in case the compiler thinks that x is unused because std::numeric_limits<T>::has_infinity is false
|
Chris@16
|
444 return std::numeric_limits<T>::has_infinity
|
Chris@16
|
445 && ( x == std::numeric_limits<T>::infinity()
|
Chris@16
|
446 || x == -std::numeric_limits<T>::infinity());
|
Chris@16
|
447 }
|
Chris@16
|
448
|
Chris@16
|
449 template<class T>
|
Chris@16
|
450 inline bool isinf_impl(T x, generic_tag<false> const&)
|
Chris@16
|
451 {
|
Chris@16
|
452 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
453 if(std::numeric_limits<T>::is_specialized)
|
Chris@16
|
454 return isinf_impl(x, generic_tag<true>());
|
Chris@16
|
455 #endif
|
Chris@101
|
456 (void)x; // warning suppression.
|
Chris@16
|
457 return false;
|
Chris@16
|
458 }
|
Chris@16
|
459
|
Chris@16
|
460 template<class T>
|
Chris@16
|
461 inline bool isinf_impl(T x, ieee_copy_all_bits_tag const&)
|
Chris@16
|
462 {
|
Chris@16
|
463 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
464
|
Chris@16
|
465 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
466 traits::get_bits(x,a);
|
Chris@16
|
467 a &= traits::exponent | traits::significand;
|
Chris@16
|
468 return a == traits::exponent;
|
Chris@16
|
469 }
|
Chris@16
|
470
|
Chris@16
|
471 template<class T>
|
Chris@16
|
472 inline bool isinf_impl(T x, ieee_copy_leading_bits_tag const&)
|
Chris@16
|
473 {
|
Chris@16
|
474 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
475
|
Chris@16
|
476 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
477 traits::get_bits(x,a);
|
Chris@16
|
478 a &= traits::exponent | traits::significand;
|
Chris@16
|
479 if(a != traits::exponent)
|
Chris@16
|
480 return false;
|
Chris@16
|
481
|
Chris@16
|
482 traits::set_bits(x,0);
|
Chris@16
|
483 return x == 0;
|
Chris@16
|
484 }
|
Chris@16
|
485
|
Chris@16
|
486 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
|
Chris@16
|
487 inline bool isinf_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
|
Chris@16
|
488 {
|
Chris@16
|
489 return boost::math::detail::isinf_impl(t, generic_tag<true>());
|
Chris@16
|
490 }
|
Chris@16
|
491 #endif
|
Chris@16
|
492
|
Chris@16
|
493 } // namespace detail
|
Chris@16
|
494
|
Chris@16
|
495 template<class T>
|
Chris@16
|
496 inline bool (isinf)(T x)
|
Chris@16
|
497 {
|
Chris@16
|
498 typedef typename detail::fp_traits<T>::type traits;
|
Chris@16
|
499 typedef typename traits::method method;
|
Chris@16
|
500 // typedef typename boost::is_floating_point<T>::type fp_tag;
|
Chris@16
|
501 typedef typename tools::promote_args_permissive<T>::type value_type;
|
Chris@16
|
502 return detail::isinf_impl(static_cast<value_type>(x), method());
|
Chris@16
|
503 }
|
Chris@16
|
504
|
Chris@16
|
505 #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
Chris@16
|
506 template<>
|
Chris@16
|
507 inline bool (isinf)(long double x)
|
Chris@16
|
508 {
|
Chris@16
|
509 typedef detail::fp_traits<long double>::type traits;
|
Chris@16
|
510 typedef traits::method method;
|
Chris@101
|
511 //typedef boost::is_floating_point<long double>::type fp_tag;
|
Chris@16
|
512 typedef long double value_type;
|
Chris@16
|
513 return detail::isinf_impl(static_cast<value_type>(x), method());
|
Chris@16
|
514 }
|
Chris@16
|
515 #endif
|
Chris@16
|
516
|
Chris@16
|
517 //------------------------------------------------------------------------------
|
Chris@16
|
518
|
Chris@16
|
519 namespace detail {
|
Chris@16
|
520
|
Chris@16
|
521 #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
|
Chris@16
|
522 template<class T>
|
Chris@16
|
523 inline bool isnan_impl(T x, native_tag const&)
|
Chris@16
|
524 {
|
Chris@16
|
525 return (std::isnan)(x);
|
Chris@16
|
526 }
|
Chris@16
|
527 #endif
|
Chris@16
|
528
|
Chris@16
|
529 template<class T>
|
Chris@16
|
530 inline bool isnan_impl(T x, generic_tag<true> const&)
|
Chris@16
|
531 {
|
Chris@16
|
532 return std::numeric_limits<T>::has_infinity
|
Chris@16
|
533 ? !(x <= std::numeric_limits<T>::infinity())
|
Chris@16
|
534 : x != x;
|
Chris@16
|
535 }
|
Chris@16
|
536
|
Chris@16
|
537 template<class T>
|
Chris@16
|
538 inline bool isnan_impl(T x, generic_tag<false> const&)
|
Chris@16
|
539 {
|
Chris@16
|
540 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
Chris@16
|
541 if(std::numeric_limits<T>::is_specialized)
|
Chris@16
|
542 return isnan_impl(x, generic_tag<true>());
|
Chris@16
|
543 #endif
|
Chris@101
|
544 (void)x; // warning suppression
|
Chris@16
|
545 return false;
|
Chris@16
|
546 }
|
Chris@16
|
547
|
Chris@16
|
548 template<class T>
|
Chris@16
|
549 inline bool isnan_impl(T x, ieee_copy_all_bits_tag const&)
|
Chris@16
|
550 {
|
Chris@16
|
551 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
552
|
Chris@16
|
553 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
554 traits::get_bits(x,a);
|
Chris@16
|
555 a &= traits::exponent | traits::significand;
|
Chris@16
|
556 return a > traits::exponent;
|
Chris@16
|
557 }
|
Chris@16
|
558
|
Chris@16
|
559 template<class T>
|
Chris@16
|
560 inline bool isnan_impl(T x, ieee_copy_leading_bits_tag const&)
|
Chris@16
|
561 {
|
Chris@16
|
562 typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
|
Chris@16
|
563
|
Chris@16
|
564 BOOST_DEDUCED_TYPENAME traits::bits a;
|
Chris@16
|
565 traits::get_bits(x,a);
|
Chris@16
|
566
|
Chris@16
|
567 a &= traits::exponent | traits::significand;
|
Chris@16
|
568 if(a < traits::exponent)
|
Chris@16
|
569 return false;
|
Chris@16
|
570
|
Chris@16
|
571 a &= traits::significand;
|
Chris@16
|
572 traits::set_bits(x,a);
|
Chris@16
|
573 return x != 0;
|
Chris@16
|
574 }
|
Chris@16
|
575
|
Chris@16
|
576 } // namespace detail
|
Chris@16
|
577
|
Chris@16
|
578 template<class T>
|
Chris@16
|
579 inline bool (isnan)(T x)
|
Chris@16
|
580 { //!< \brief return true if floating-point type t is NaN (Not A Number).
|
Chris@16
|
581 typedef typename detail::fp_traits<T>::type traits;
|
Chris@16
|
582 typedef typename traits::method method;
|
Chris@16
|
583 // typedef typename boost::is_floating_point<T>::type fp_tag;
|
Chris@16
|
584 return detail::isnan_impl(x, method());
|
Chris@16
|
585 }
|
Chris@16
|
586
|
Chris@16
|
587 #ifdef isnan
|
Chris@16
|
588 template <> inline bool isnan BOOST_NO_MACRO_EXPAND<float>(float t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
|
Chris@16
|
589 template <> inline bool isnan BOOST_NO_MACRO_EXPAND<double>(double t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
|
Chris@16
|
590 template <> inline bool isnan BOOST_NO_MACRO_EXPAND<long double>(long double t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
|
Chris@16
|
591 #elif defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
|
Chris@16
|
592 template<>
|
Chris@16
|
593 inline bool (isnan)(long double x)
|
Chris@16
|
594 { //!< \brief return true if floating-point type t is NaN (Not A Number).
|
Chris@16
|
595 typedef detail::fp_traits<long double>::type traits;
|
Chris@16
|
596 typedef traits::method method;
|
Chris@101
|
597 //typedef boost::is_floating_point<long double>::type fp_tag;
|
Chris@16
|
598 return detail::isnan_impl(x, method());
|
Chris@16
|
599 }
|
Chris@16
|
600 #endif
|
Chris@16
|
601
|
Chris@16
|
602 } // namespace math
|
Chris@16
|
603 } // namespace boost
|
Chris@16
|
604
|
Chris@16
|
605 #endif // BOOST_MATH_FPCLASSIFY_HPP
|
Chris@16
|
606
|