| Home | Libraries | People | FAQ | More

## Examples

#### Jahnke-Emden-Lambda function

The following code uses `<boost/cstdfloat.hpp>` in combination with `<boost/math/special_functions.hpp>` to compute a simplified version of the Jahnke-Emden-Lambda function. Here, we specify a floating-point type with exactly 64 bits (i.e., `float64_t`). If we were to use, for instance, built-in `double`, then there would be no guarantee that the code would behave identically on all platforms. With `float64_t` from `<boost/cstdfloat.hpp>`, however, it is very likely to be identical.

Using `float64_t`, we know that this code is as portable as possible and uses a floating-point type with approximately 15 decimal digits of precision, regardless of the compiler or version or operating system.

    #include <boost/cstdfloat.hpp> // For float_64_t, float128_t. Must be first include!
    #include <cmath>  // for pow function.
    #include <boost/math/special_functions.hpp> // For gamma function.

    boost::float64_t jahnke_emden_lambda(boost::float64_t v, boost::float64_t x)
    {
      const boost::float64_t gamma_v_plus_one = boost::math::tgamma(v + 1);
      const boost::float64_t x_half_pow_v     = std::pow(x /2, v);

      return gamma_v_plus_one * boost::math::cyl_bessel_j(x, v) / x_half_pow_v;
    }

Ensure that all possibly significant digits (17) including trailing zeros are shown.

    std::cout.precision(std::numeric_limits<boost::float64_t>::max_digits10);
    std::cout.setf(std::ios::showpoint); // Show trailing zeros.

    try
    { // Always use try'n'catch blocks to ensure any error messages are displayed.

    // Evaluate and display an evaluation of the Jahnke-Emden lambda function:
    boost::float64_t v = 1.;
    boost::float64_t x = 1.;
    std::cout << jahnke_emden_lambda(v, x) << std::endl; // 0.88010117148986700

For details, see cstdfloat_example.cpp - a extensive example program.

#### Normal distribution table

This example shows printing tables of a normal distribution's PDF and CDF, using `boost::math` implementation of normal distribution.

A function templated on floating-point type prints a table for a range of standard variate z values.

The example shows use of the specified-width typedefs to either use a specific width, or to use the maximum available on the platform, perhaps a high as 128-bit.

The number of digits displayed is controlled by the precision of the type, so there are no spurious insignificant decimal digits:

    float_32_t    0    0.39894228
    float_128_t  0    0.398942280401432702863218082711682655

Some sample output for two different platforms is appended to the code at normal_tables.cpp.

    #ifdef BOOST_FLOAT32_C
        normal_table<boost::float32_t>(); // Usually type float
    #endif
        normal_table<boost::float64_t>(); // Uusually type double. Assume that float64_t is always available.
    #ifdef BOOST_FLOAT80_C
        normal_table<boost::float80_t>(); // Type long double on some X86 platforms.
    #endif
    #ifdef BOOST_FLOAT128_C
        normal_table<boost::float128_t>(); // Type _Quad on some Intel and __float128 on some GCC platforms.
    #endif
        normal_table<boost::floatmax_t>();