Skip to content

Mathematical functions

kevinchannon edited this page Oct 3, 2011 · 7 revisions

Basic typedefs and constants

These typedefs and constants are defined in Common/number.h

  • typedef double real
    This typedef is used extensively in GLS++, to abstract away the difference between single and double precision floating point values.

Const definitions for several common values

  • const real realZero = 0.0
  • const real realOne = 1.0
  • const real realEmpty = GSL_NAN
  • const real NaN = GSL_NAN
  • const real realPlusInf = GSL_POSINF
  • const real realMinusInf = GSL_NEGINF

Functions for testing validity of numbers

  • bool gsl::is_empty( real x )
  • bool gsl::is_nan( real x )
  • bool gsl::is_finite( real x)

Mathematical constants

  • gsl::e
    Base of natural logarithm
  • gsl::log2e
    Base two logarithm of e
  • gsl::log10e
    Base ten logarithm of e
  • gsl::sqrt2
    Square-root of two
  • gsl::sqrt_half
    Square-root of one-half
  • gsl::sqrt3
    Square-root of three
  • gsl::pi
    pi
  • gsl::half_pi
    pi / 2
  • gsl::quarter_pi
    pi / 4
  • gsl::sqrt_pi
    square-root of pi
  • gsl::two_over_sqrt_pi
    two divided by the square-root of pi
  • gsl::one_over_pi
    recprocal of pi
  • gsl::two_over_pi
    twice the reciprocal of pi
  • gsl::ln10
    Natural logarithm of ten
  • gsl::ln2
    Natural logarithm of two
  • gsl::ln_pi
    Natural logarithn of pi
  • gsl::euler
    Euler's constant

Floating point class

This class stores a floating point number and allows easy access to the exponent and mantissa, as well as conversion to and from real values. gsl::floating_point is defined in Common/number.h.

  • class floating_point
    The floating point class

  • gsl::floating_point::floating_point()
    Default constructor

  • gsl::floating_point::floating_point( real m, int e )
    Construct a floating point number from mantissa, m, and exponent, e

  • gsl::floating_point::floating_point( real x )
    Construct a floating point number from a real value, x

  • gsl::floating_point::operator real()
    Cast a floating point number to a real value

  • `gsl::floating_point& gsl::floating_point::operator=( const gsl::floating_point& right )
    Equality operator

  • gsl::sign_type gsl::floating_point::sign() const
    Determine the sign of the floating point number. Returns gsl::positive if the sign of the floating point value is positive and gsl::negative if it is negative

  • real& f()
    Returns a reference to the fraction component of the floating point number

  • const real& f() const
    returns a const reference to the fraction component of the floating point number

  • int& e()
    Returns a reference to the exponent of the floating point number

  • const int& e() const
    Returns a const reference to the exponent of the floating point number

Elementary functions

These routines provide wrappers to native GSL functions, which are in turn portable implementations of functions found in the BSD math library. If compiled with the option GSLPP_INLINE, these functions are in-line calls to the underlying GSL functions. These functions are defined in Common/math.h.

  • real gsl::log1p( real x )
    Computes the value of log(1 + x) in a way that is accurate for small values of x

  • real gsl::expm1( real x )
    Compute the value of exp(x) - 1 in a way that is accurate for small values of x

  • real gsl::hypot( real x, real y )
    Compute the value of sqrt(x2 + y2) in a way that avoids overflow

  • real gsl::ldexp( const gsl::floating_point& x )
    Compute the value of f × 2e

  • gsl::floating_point gsl::frexp( real x )
    Split the number x into its normalised fraction f and exponent e, such that x = f × 2e and 0.5 ≤ f < 0.5. If x = 0, both f & e are set to zero.

Small integer powers

These functions wrap the GSL small integer power functions. If the compilation option GSLPP_INLINE is defined, then the calls to the underlying GSL functions are in-lined. These functions are defined in Common/math.h.

  • real pow( real x, int n )
    Computes the power xn for integer n. The power is computed efficiently — for example, x8 is computed as ((x2)2)2, requiring only three multiplications

  • real gsl::pow_2( real x )

  • real gsl::pow_3( real x )

  • real gsl::pow_4( real x )

  • real gsl::pow_5( real x )

  • real gsl::pow_6( real x )

  • real gsl::pow_7( real x )

  • real gsl::pow_8( real x )

  • real gsl::pow_9( real x )

Testing the character of numbers

These functions are defined in Common/math.h.

Testing the sign of a value

  • gsl::sign_type sign( real x )
    Returns gsl::positive if x ≥ 0 and gsl::negative otherwise.

Testing for odd and even numbers

  • bool gsl::is_odd( int x )
  • bool gsl::is_even( int x )

Maximum and minimum functions

These functions just wrap std::min & std::max. If GSLPP__INLINE is defined, these functions are in-lined. These functions are defined in Common/math.h

  • template< typename T > T min( T x1, T x2 )
  • template< typename T > T max( T x1, T x2 )

Approximate comparison of floating point numbers

  • gsl::equality_type fcmp( real x, real y, real epsilon)
    Determine whether x & y are approximately equal to a relative accuracy ε. The relative accuracy is measured using an interval size 2δ, where δ = 2 and k is the maximum base-2 exponent of x & y, as computed by the function frexp. Returns gsl::equal_to if x & y are determined equal, gsl::less_than if x < y and gsl::greater_than if x > y