fpclassify.hpp 19 KB

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