fpclassify.hpp 19 KB

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