cstdfloat.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. [book Standardized Floating-Point typedefs for C and C++
  2. [quickbook 1.7]
  3. [copyright 2014 Christopher Kormanyos, John Maddock, Paul A. Bristow]
  4. [license
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. [@http://www.boost.org/LICENSE_1_0.txt])
  8. ]
  9. [authors [Kormanyos, Christopher], [Maddock, John], [Bristow, Paul A.] ]
  10. [last-revision $Date$]
  11. [/version 1.8.3]
  12. ]
  13. [template tr1[] [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf Technical Report on C++ Library Extensions]]
  14. [template C99[] [@http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf C99 Standard ISO/IEC 9899:1999]]
  15. [def __gsl [@http://www.gnu.org/software/gsl/ GSL-1.9]]
  16. [def __glibc [@http://www.gnu.org/software/libc/ GNU C Lib]]
  17. [def __hpc [@http://docs.hp.com/en/B9106-90010/index.html HP-UX C Library]]
  18. [def __cephes [@http://www.netlib.org/cephes/ Cephes]]
  19. [def __NTL [@http://www.shoup.net/ntl/ NTL A Library for doing Number Theory]]
  20. [def __NTL_RR [@http://shoup.net/ntl/doc/RR.txt NTL::RR]]
  21. [def __NTL_quad_float [@http://shoup.net/ntl/doc/quad_float.txt NTL::quad_float]]
  22. [def __MPFR [@http://www.mpfr.org/ GNU MPFR library]]
  23. [def __GMP [@http://gmplib.org/ GNU Multiple Precision Arithmetic Library]]
  24. [def __multiprecision [@http://www.boost.org/doc/libs/1_53_0_beta1/libs/multiprecision/doc/html/index.html Boost.Multiprecision]]
  25. [def __cpp_dec_float [@http://www.boost.org/doc/libs/1_53_0_beta1/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_dec_float.html cpp_dec_float]]
  26. [def __R [@http://www.r-project.org/ The R Project for Statistical Computing]]
  27. [def __godfrey [link godfrey Godfrey]]
  28. [def __pugh [link pugh Pugh]]
  29. [def __NaN [@http://en.wikipedia.org/wiki/NaN NaN]]
  30. [def __errno [@http://en.wikipedia.org/wiki/Errno `::errno`]]
  31. [def __Mathworld [@http://mathworld.wolfram.com Wolfram MathWorld]]
  32. [def __Mathematica [@http://www.wolfram.com/products/mathematica/index.html Wolfram Mathematica]]
  33. [def __WolframAlpha [@http://www.wolframalpha.com/ Wolfram Alpha]]
  34. [def __TOMS748 [@http://portal.acm.org/citation.cfm?id=210111 TOMS Algorithm 748: enclosing zeros of continuous functions]]
  35. [def __TOMS910 [@http://portal.acm.org/citation.cfm?id=1916469 TOMS Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations]]
  36. [def __why_complements [link why_complements why complements?]]
  37. [def __complements [link math_toolkit.stat_tut.overview.complements complements]]
  38. [def __performance [link perf performance]]
  39. [def __building [link math_toolkit.building building libraries]]
  40. [def __e_float [@http://calgo.acm.org/910.zip e_float (TOMS Algorithm 910)]]
  41. [def __Abramowitz_Stegun M. Abramowitz and I. A. Stegun, Handbook of Mathematical Functions, NBS (1964)]
  42. [def __DMLF [@http://dlmf.nist.gov/ NIST Digital Library of Mathematical Functions]]
  43. [def __IEEE754 [@http://en.wikipedia.org/wiki/IEEE_floating_point IEEE_floating_point]]
  44. [def __N3626 [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3626.pdf N3626]]
  45. [def __N1703 [@http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1703.pdf N1703]]
  46. [/ Some composite templates]
  47. [template super[x]'''<superscript>'''[x]'''</superscript>''']
  48. [template sub[x]'''<subscript>'''[x]'''</subscript>''']
  49. [template floor[x]'''&#x230A;'''[x]'''&#x230B;''']
  50. [template floorlr[x][lfloor][x][rfloor]]
  51. [template ceil[x] '''&#x2308;'''[x]'''&#x2309;''']
  52. [/template header_file[file] [@../../../../[file] [file]]]
  53. [note A printer-friendly PDF version of this manual is also available.]
  54. [section:overview Overview]
  55. The header `<boost/cstdfloat.hpp>` provides optional standardized
  56. floating-point `typedef`s having specified widths.
  57. These are useful for writing portable code because they
  58. should behave identically on all platforms.
  59. All `typedef`s are in `namespace boost`.
  60. The `typedef`s include `float16_t, float32_t, float64_t, float128_t`,
  61. their corresponding least and fast types,
  62. and the corresponding maximum-width type.
  63. The `typedef`s are based on underlying built-in types
  64. such as `float`, `double`, or `long double`, or based on other compiler-specific
  65. non-standardized types such as `__float128`.
  66. The underlying types of these typedef's must conform with
  67. the corresponding specifications of binary16, binary32, binary64,
  68. and binary128 in __IEEE754 floating-point format
  69. [@http://en.wikipedia.org/wiki/IEEE_floating_point].
  70. The typedef's are based on __N3626
  71. proposed for a new C++14 standard header `<cstdfloat>` and
  72. __N1703 proposed for a new C language standard header `<stdfloat.h>`.
  73. The 128-bit floating-point type, of great interest in scientific and
  74. numeric programming, is not required in the boost header,
  75. and may not be supplied for all platforms/compilers, because compiler
  76. support for a 128-bit floating-point type is not mandated by either
  77. the C standard or the C++ standard.
  78. The following code uses `<boost/cstdfloat.hpp>` in combination with
  79. `<boost/math/special_functions.hpp>` to compute a simplified
  80. version of the Jahnke-Emden-Lambda function. Here, we use
  81. a floating-point type with exactly 64 bits (i.e., `float64_t`).
  82. If we were to use, for instance, built-in `double`,
  83. then there would be no guarantee that the code would
  84. behave identically on all platforms. With `float64_t` from
  85. `<boost/cstdfloat.hpp>`, however, this is very likely.
  86. Using `float64_t`, we know that
  87. this code is portable and uses a floating-point type
  88. with approximately 15 decimal digits of precision.
  89. #include <cmath>
  90. #include <boost/cstdfloat.hpp>
  91. #include <boost/math/special_functions.hpp>
  92. boost::float64_t jahnke_emden_lambda(boost::float64_t v, boost::float64_t x)
  93. {
  94. const boost::float64_t gamma_v_plus_one = boost::math::tgamma(v + 1);
  95. const boost::float64_t x_half_pow_v = std::pow(x / 2, v);
  96. return gamma_v_plus_one * boost::math::cyl_bessel_j(x, v) / x_half_pow_v;
  97. }
  98. See `cstdfloat_test.cpp` for a more detailed test program.
  99. [endsect] [/section:overview Overview]
  100. [section:rationale Rationale]
  101. The implementation of `<boost/cstdfloat.hpp>` is designed to utilize `<float.h>`,
  102. defined in the 1989 C standard. The preprocessor is used to query certain
  103. preprocessor definitions in `<float.h>` such as FLT_MAX, DBL_MAX, etc.
  104. Based on the results of these queries, an attempt is made to automatically
  105. detect the presence of built-in floating-point types having specified widths.
  106. An unequivocal test regarding conformance with __IEEE754 (IEC599) based on
  107. [@ http://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559 `std::numeric_limits<>::is_iec559`]
  108. is performed with `BOOST_STATIC_ASSERT`.
  109. The header `<boost/cstdfloat.hpp>` makes the standardized floating-point
  110. `typedef`s safely available in `namespace boost` without placing any names
  111. in `namespace std`. The intention is to complement rather than compete
  112. with a potential future C++ Standard Library that may contain these `typedef`s.
  113. Should some future C++ standard include `<stdfloat.h>` and `<cstdfloat>`,
  114. then `<boost/cstdfloat.hpp>` will continue to function, but will become redundant
  115. and may be safely deprecated.
  116. Because `<boost/cstdfloat.hpp>` is a boost header, its name conforms to the
  117. boost header naming conventions, not the C++ Standard Library header
  118. naming conventions.
  119. [note
  120. <boost/cstdfloat.hpp> [*cannot synthesize or create
  121. a `typedef` if the underlying type is not provided by the compiler].
  122. For example, if a compiler does not have an underlying floating-point
  123. type with 128 bits (highly sought-after in scientific and numeric programming),
  124. then `float128_t` and its corresponding least and fast types are not
  125. provided by `<boost/cstdfloat.hpp`>.]
  126. [warning
  127. As an implementation artifact, certain C macro names from `<float.h>`
  128. may possibly be visible to users of `<boost/cstdfloat.hpp>`.
  129. Don't rely on using these macros; they are not part of any Boost-specified interface.
  130. Use `std::numeric_limits<>` for floating-point ranges, etc. instead.]
  131. [endsect] [/section:rationale Rationale]
  132. [section:exact_typdefs Exact-Width Floating-Point `typedef`s]
  133. The `typedef float#_t`, with # replaced by the width, designates a
  134. floating-point type of exactly # bits. For example `float32_t` denotes
  135. a single-precision floating-point type with approximately
  136. 7 decimal digits of precision (equivalent to binary32 in __IEEE754).
  137. Floating-point types specified in C and C++ are allowed to have
  138. implementation-specific widths and formats.
  139. However, if a platform supports underlying floating-point types
  140. (conformant with __IEEE754) with widths of 16, 32, 64, 128 bits,
  141. or any combination thereof,
  142. then `<boost/cstdfloat.hpp>` does provide the corresponding `typedef`s
  143. `float16_t, float32_t, float64_t, float128_t,`
  144. their corresponding least and fast types,
  145. and the corresponding maximum-width type
  146. The absence of `float128_t` is indicated by the macro `BOOST_NO_FLOAT128_T`.
  147. [endsect] [/section:exact_typdefs Exact-Width Floating-Point `typedef`s]
  148. [section:fastest_typdefs Fastest minimum-width floating-point `typedef`s]
  149. The `typedef float_least#_t`, with # replaced by the width, designates a
  150. floating-point type with a [*width of at least # bits], such that no
  151. floating-point type with lesser size has at least the specified width.
  152. Thus, `float_least32_t` denotes the smallest floating-point type with
  153. a width of at least 32 bits.
  154. Minimum-width floating-point types are provided for all existing
  155. exact-width floating-point types on a given platform.
  156. For example, if a platfrom supports `float32_t` and `float64_t`,
  157. then `float_least32_t` and `float_least64_t` will also be supported, etc.
  158. [endsect] [/section:fastest_typdefs Fastest minimum-width floating-point `typedef`s]
  159. [section:fastest_typdefs Fastest minimum-width floating-point `typedef`s]
  160. The typedef `float_fast#_t`, with # replaced by the width, designates
  161. the [*fastest] floating-point type with a width of at least # bits.
  162. There is no absolute guarantee that these types are the fastest for all purposes.
  163. In any case, however, they satisfy the precision and width requirements.
  164. Fastest minimum-width floating-point types are provided for all existing
  165. exact-width floating-point types on a given platform.
  166. For example, if a platform supports `float32_t` and `float64_t`,
  167. then `float_fast32_t` and `float_fast64_t` will also be supported, etc.
  168. [endsect] [/section:fastest_typdefs Fastest minimum-width floating-point `typedef`s]
  169. [section:greatest_typdefs Greatest-width floating-point typedef]
  170. The `typedef floatmax_t` designates a floating-point type capable of representing
  171. any value of any floating-point type in a given platform.
  172. The greatest-width typedef is provided for all platforms.
  173. [endsect] [/section:greatest_typdefs Greatest-width floating-point typedef]
  174. [section:macros Floating-Point Constant Macros]
  175. All macros of the type `BOOST_FLOAT16_C, BOOST_FLOAT32_C, BOOST_FLOAT64_C,
  176. BOOST_FLOAT128_C, BOOST_FLOATMAX_C` are always defined after inclusion of
  177. `<boost/cstdfloat.hpp>`. These allow floating-point constants of at
  178. least the specified width to be declared.
  179. For example:
  180. #include <boost/cstdfloat.hpp>
  181. // Declare Pythagoras' constant with approximately 7 decimal digits of precision.
  182. static const boost::float32_t pi = BOOST_FLOAT32_C(3.1415926536);
  183. // Declare the Euler-gamma constant with approximately 34 decimal digits of precision.
  184. static const boost::float128_t euler = BOOST_FLOAT128_C(0.57721566490153286060651209008240243104216);
  185. [endsect] [/section:macros Floating-Point Constant Macros]
  186. [/ cstdfloat.qbk
  187. Copyright 2014 Christopher Kormanyos, John Maddock and Paul A. Bristow.
  188. Distributed under the Boost Software License, Version 1.0.
  189. (See accompanying file LICENSE_1_0.txt or copy at
  190. http://www.boost.org/LICENSE_1_0.txt).
  191. ]