fpclassify.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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_NO_STDC_NAMESPACE
  66. namespace std{ using ::abs; using ::fabs; }
  67. #endif
  68. namespace boost{
  69. //
  70. // This must not be located in any namespace under boost::math
  71. // otherwise we can get into an infinite loop if isnan is
  72. // a #define for "isnan" !
  73. //
  74. namespace math_detail{
  75. #ifdef BOOST_MSVC
  76. #pragma warning(push)
  77. #pragma warning(disable:4800)
  78. #endif
  79. template <class T>
  80. inline bool is_nan_helper(T t, const boost::true_type&)
  81. {
  82. #ifdef isnan
  83. return isnan(t);
  84. #elif defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY) || !defined(BOOST_HAS_FPCLASSIFY)
  85. (void)t;
  86. return false;
  87. #else // BOOST_HAS_FPCLASSIFY
  88. return (BOOST_FPCLASSIFY_PREFIX fpclassify(t) == (int)FP_NAN);
  89. #endif
  90. }
  91. #ifdef BOOST_MSVC
  92. #pragma warning(pop)
  93. #endif
  94. template <class T>
  95. inline bool is_nan_helper(T, const boost::false_type&)
  96. {
  97. return false;
  98. }
  99. }
  100. namespace math{
  101. namespace detail{
  102. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  103. template <class T>
  104. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const native_tag&)
  105. {
  106. return (std::fpclassify)(t);
  107. }
  108. #endif
  109. template <class T>
  110. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<true>&)
  111. {
  112. BOOST_MATH_INSTRUMENT_VARIABLE(t);
  113. // whenever possible check for Nan's first:
  114. #if defined(BOOST_HAS_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
  115. if(::boost::math_detail::is_nan_helper(t, ::boost::is_floating_point<T>()))
  116. return FP_NAN;
  117. #elif defined(isnan)
  118. if(boost::math_detail::is_nan_helper(t, ::boost::is_floating_point<T>()))
  119. return FP_NAN;
  120. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  121. if(::_isnan(boost::math::tools::real_cast<double>(t)))
  122. return FP_NAN;
  123. #endif
  124. // std::fabs broken on a few systems especially for long long!!!!
  125. T at = (t < T(0)) ? -t : t;
  126. // Use a process of exclusion to figure out
  127. // what kind of type we have, this relies on
  128. // IEEE conforming reals that will treat
  129. // Nan's as unordered. Some compilers
  130. // don't do this once optimisations are
  131. // turned on, hence the check for nan's above.
  132. if(at <= (std::numeric_limits<T>::max)())
  133. {
  134. if(at >= (std::numeric_limits<T>::min)())
  135. return FP_NORMAL;
  136. return (at != 0) ? FP_SUBNORMAL : FP_ZERO;
  137. }
  138. else if(at > (std::numeric_limits<T>::max)())
  139. return FP_INFINITE;
  140. return FP_NAN;
  141. }
  142. template <class T>
  143. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<false>&)
  144. {
  145. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  146. if(std::numeric_limits<T>::is_specialized)
  147. return fpclassify_imp(t, generic_tag<true>());
  148. #endif
  149. //
  150. // An unknown type with no numeric_limits support,
  151. // so what are we supposed to do we do here?
  152. //
  153. BOOST_MATH_INSTRUMENT_VARIABLE(t);
  154. return t == 0 ? FP_ZERO : FP_NORMAL;
  155. }
  156. template<class T>
  157. int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_all_bits_tag)
  158. {
  159. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  160. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  161. BOOST_DEDUCED_TYPENAME traits::bits a;
  162. traits::get_bits(x,a);
  163. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  164. a &= traits::exponent | traits::flag | traits::significand;
  165. BOOST_MATH_INSTRUMENT_VARIABLE((traits::exponent | traits::flag | traits::significand));
  166. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  167. if(a <= traits::significand) {
  168. if(a == 0)
  169. return FP_ZERO;
  170. else
  171. return FP_SUBNORMAL;
  172. }
  173. if(a < traits::exponent) return FP_NORMAL;
  174. a &= traits::significand;
  175. if(a == 0) return FP_INFINITE;
  176. return FP_NAN;
  177. }
  178. template<class T>
  179. int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_leading_bits_tag)
  180. {
  181. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  182. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  183. BOOST_DEDUCED_TYPENAME traits::bits a;
  184. traits::get_bits(x,a);
  185. a &= traits::exponent | traits::flag | traits::significand;
  186. if(a <= traits::significand) {
  187. if(x == 0)
  188. return FP_ZERO;
  189. else
  190. return FP_SUBNORMAL;
  191. }
  192. if(a < traits::exponent) return FP_NORMAL;
  193. a &= traits::significand;
  194. traits::set_bits(x,a);
  195. if(x == 0) return FP_INFINITE;
  196. return FP_NAN;
  197. }
  198. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && (defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) || defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS))
  199. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  200. {
  201. return boost::math::detail::fpclassify_imp(t, generic_tag<true>());
  202. }
  203. #endif
  204. } // namespace detail
  205. template <class T>
  206. inline int fpclassify BOOST_NO_MACRO_EXPAND(T t)
  207. {
  208. typedef typename detail::fp_traits<T>::type traits;
  209. typedef typename traits::method method;
  210. typedef typename tools::promote_args_permissive<T>::type value_type;
  211. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  212. if(std::numeric_limits<T>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0)))
  213. return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
  214. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  215. #else
  216. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  217. #endif
  218. }
  219. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  220. template <>
  221. inline int fpclassify<long double> BOOST_NO_MACRO_EXPAND(long double t)
  222. {
  223. typedef detail::fp_traits<long double>::type traits;
  224. typedef traits::method method;
  225. typedef long double value_type;
  226. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  227. if(std::numeric_limits<long double>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0)))
  228. return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
  229. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  230. #else
  231. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  232. #endif
  233. }
  234. #endif
  235. namespace detail {
  236. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  237. template<class T>
  238. inline bool isfinite_impl(T x, native_tag const&)
  239. {
  240. return (std::isfinite)(x);
  241. }
  242. #endif
  243. template<class T>
  244. inline bool isfinite_impl(T x, generic_tag<true> const&)
  245. {
  246. return x >= -(std::numeric_limits<T>::max)()
  247. && x <= (std::numeric_limits<T>::max)();
  248. }
  249. template<class T>
  250. inline bool isfinite_impl(T x, generic_tag<false> const&)
  251. {
  252. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  253. if(std::numeric_limits<T>::is_specialized)
  254. return isfinite_impl(x, generic_tag<true>());
  255. #endif
  256. (void)x; // warning supression.
  257. return true;
  258. }
  259. template<class T>
  260. inline bool isfinite_impl(T x, ieee_tag const&)
  261. {
  262. typedef BOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits;
  263. BOOST_DEDUCED_TYPENAME traits::bits a;
  264. traits::get_bits(x,a);
  265. a &= traits::exponent;
  266. return a != traits::exponent;
  267. }
  268. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  269. inline bool isfinite_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  270. {
  271. return boost::math::detail::isfinite_impl(t, generic_tag<true>());
  272. }
  273. #endif
  274. }
  275. template<class T>
  276. inline bool (isfinite)(T x)
  277. { //!< \brief return true if floating-point type t is finite.
  278. typedef typename detail::fp_traits<T>::type traits;
  279. typedef typename traits::method method;
  280. // typedef typename boost::is_floating_point<T>::type fp_tag;
  281. typedef typename tools::promote_args_permissive<T>::type value_type;
  282. return detail::isfinite_impl(static_cast<value_type>(x), method());
  283. }
  284. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  285. template<>
  286. inline bool (isfinite)(long double x)
  287. { //!< \brief return true if floating-point type t is finite.
  288. typedef detail::fp_traits<long double>::type traits;
  289. typedef traits::method method;
  290. typedef boost::is_floating_point<long double>::type fp_tag;
  291. typedef long double value_type;
  292. return detail::isfinite_impl(static_cast<value_type>(x), method());
  293. }
  294. #endif
  295. //------------------------------------------------------------------------------
  296. namespace detail {
  297. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  298. template<class T>
  299. inline bool isnormal_impl(T x, native_tag const&)
  300. {
  301. return (std::isnormal)(x);
  302. }
  303. #endif
  304. template<class T>
  305. inline bool isnormal_impl(T x, generic_tag<true> const&)
  306. {
  307. if(x < 0) x = -x;
  308. return x >= (std::numeric_limits<T>::min)()
  309. && x <= (std::numeric_limits<T>::max)();
  310. }
  311. template<class T>
  312. inline bool isnormal_impl(T x, generic_tag<false> const&)
  313. {
  314. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  315. if(std::numeric_limits<T>::is_specialized)
  316. return isnormal_impl(x, generic_tag<true>());
  317. #endif
  318. return !(x == 0);
  319. }
  320. template<class T>
  321. inline bool isnormal_impl(T x, ieee_tag const&)
  322. {
  323. typedef BOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits;
  324. BOOST_DEDUCED_TYPENAME traits::bits a;
  325. traits::get_bits(x,a);
  326. a &= traits::exponent | traits::flag;
  327. return (a != 0) && (a < traits::exponent);
  328. }
  329. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  330. inline bool isnormal_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  331. {
  332. return boost::math::detail::isnormal_impl(t, generic_tag<true>());
  333. }
  334. #endif
  335. }
  336. template<class T>
  337. inline bool (isnormal)(T x)
  338. {
  339. typedef typename detail::fp_traits<T>::type traits;
  340. typedef typename traits::method method;
  341. //typedef typename boost::is_floating_point<T>::type fp_tag;
  342. typedef typename tools::promote_args_permissive<T>::type value_type;
  343. return detail::isnormal_impl(static_cast<value_type>(x), method());
  344. }
  345. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  346. template<>
  347. inline bool (isnormal)(long double x)
  348. {
  349. typedef detail::fp_traits<long double>::type traits;
  350. typedef traits::method method;
  351. typedef boost::is_floating_point<long double>::type fp_tag;
  352. typedef long double value_type;
  353. return detail::isnormal_impl(static_cast<value_type>(x), method());
  354. }
  355. #endif
  356. //------------------------------------------------------------------------------
  357. namespace detail {
  358. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  359. template<class T>
  360. inline bool isinf_impl(T x, native_tag const&)
  361. {
  362. return (std::isinf)(x);
  363. }
  364. #endif
  365. template<class T>
  366. inline bool isinf_impl(T x, generic_tag<true> const&)
  367. {
  368. (void)x; // in case the compiler thinks that x is unused because std::numeric_limits<T>::has_infinity is false
  369. return std::numeric_limits<T>::has_infinity
  370. && ( x == std::numeric_limits<T>::infinity()
  371. || x == -std::numeric_limits<T>::infinity());
  372. }
  373. template<class T>
  374. inline bool isinf_impl(T x, generic_tag<false> const&)
  375. {
  376. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  377. if(std::numeric_limits<T>::is_specialized)
  378. return isinf_impl(x, generic_tag<true>());
  379. #endif
  380. (void)x; // warning supression.
  381. return false;
  382. }
  383. template<class T>
  384. inline bool isinf_impl(T x, ieee_copy_all_bits_tag const&)
  385. {
  386. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  387. BOOST_DEDUCED_TYPENAME traits::bits a;
  388. traits::get_bits(x,a);
  389. a &= traits::exponent | traits::significand;
  390. return a == traits::exponent;
  391. }
  392. template<class T>
  393. inline bool isinf_impl(T x, ieee_copy_leading_bits_tag const&)
  394. {
  395. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  396. BOOST_DEDUCED_TYPENAME traits::bits a;
  397. traits::get_bits(x,a);
  398. a &= traits::exponent | traits::significand;
  399. if(a != traits::exponent)
  400. return false;
  401. traits::set_bits(x,0);
  402. return x == 0;
  403. }
  404. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  405. inline bool isinf_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  406. {
  407. return boost::math::detail::isinf_impl(t, generic_tag<true>());
  408. }
  409. #endif
  410. } // namespace detail
  411. template<class T>
  412. inline bool (isinf)(T x)
  413. {
  414. typedef typename detail::fp_traits<T>::type traits;
  415. typedef typename traits::method method;
  416. // typedef typename boost::is_floating_point<T>::type fp_tag;
  417. typedef typename tools::promote_args_permissive<T>::type value_type;
  418. return detail::isinf_impl(static_cast<value_type>(x), method());
  419. }
  420. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  421. template<>
  422. inline bool (isinf)(long double x)
  423. {
  424. typedef detail::fp_traits<long double>::type traits;
  425. typedef traits::method method;
  426. typedef boost::is_floating_point<long double>::type fp_tag;
  427. typedef long double value_type;
  428. return detail::isinf_impl(static_cast<value_type>(x), method());
  429. }
  430. #endif
  431. //------------------------------------------------------------------------------
  432. namespace detail {
  433. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  434. template<class T>
  435. inline bool isnan_impl(T x, native_tag const&)
  436. {
  437. return (std::isnan)(x);
  438. }
  439. #endif
  440. template<class T>
  441. inline bool isnan_impl(T x, generic_tag<true> const&)
  442. {
  443. return std::numeric_limits<T>::has_infinity
  444. ? !(x <= std::numeric_limits<T>::infinity())
  445. : x != x;
  446. }
  447. template<class T>
  448. inline bool isnan_impl(T x, generic_tag<false> const&)
  449. {
  450. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  451. if(std::numeric_limits<T>::is_specialized)
  452. return isnan_impl(x, generic_tag<true>());
  453. #endif
  454. (void)x; // warning supression
  455. return false;
  456. }
  457. template<class T>
  458. inline bool isnan_impl(T x, ieee_copy_all_bits_tag const&)
  459. {
  460. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  461. BOOST_DEDUCED_TYPENAME traits::bits a;
  462. traits::get_bits(x,a);
  463. a &= traits::exponent | traits::significand;
  464. return a > traits::exponent;
  465. }
  466. template<class T>
  467. inline bool isnan_impl(T x, ieee_copy_leading_bits_tag const&)
  468. {
  469. typedef BOOST_DEDUCED_TYPENAME fp_traits<T>::type traits;
  470. BOOST_DEDUCED_TYPENAME traits::bits a;
  471. traits::get_bits(x,a);
  472. a &= traits::exponent | traits::significand;
  473. if(a < traits::exponent)
  474. return false;
  475. a &= traits::significand;
  476. traits::set_bits(x,a);
  477. return x != 0;
  478. }
  479. } // namespace detail
  480. template<class T>
  481. inline bool (isnan)(T x)
  482. { //!< \brief return true if floating-point type t is NaN (Not A Number).
  483. typedef typename detail::fp_traits<T>::type traits;
  484. typedef typename traits::method method;
  485. // typedef typename boost::is_floating_point<T>::type fp_tag;
  486. return detail::isnan_impl(x, method());
  487. }
  488. #ifdef isnan
  489. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<float>(float t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
  490. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<double>(double t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
  491. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<long double>(long double t){ return ::boost::math_detail::is_nan_helper(t, boost::true_type()); }
  492. #elif defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
  493. template<>
  494. inline bool (isnan)(long double x)
  495. { //!< \brief return true if floating-point type t is NaN (Not A Number).
  496. typedef detail::fp_traits<long double>::type traits;
  497. typedef traits::method method;
  498. typedef boost::is_floating_point<long double>::type fp_tag;
  499. return detail::isnan_impl(x, method());
  500. }
  501. #endif
  502. } // namespace math
  503. } // namespace boost
  504. #endif // BOOST_MATH_FPCLASSIFY_HPP