GeographicLib  1.35
Public Types | Static Public Member Functions | Static Public Attributes | List of all members
GeographicLib::Math Class Reference

Mathematical functions needed by GeographicLib. More...

#include <GeographicLib/Math.hpp>

Public Types

typedef double extended
 
typedef double real
 

Static Public Member Functions

template<typename T >
static T pi () throw ()
 
static real pi () throw ()
 
template<typename T >
static T degree () throw ()
 
static real degree () throw ()
 
template<typename T >
static T sq (T x) throw ()
 
template<typename T >
static T hypot (T x, T y) throw ()
 
template<typename T >
static T expm1 (T x) throw ()
 
template<typename T >
static T log1p (T x) throw ()
 
template<typename T >
static T asinh (T x) throw ()
 
template<typename T >
static T atanh (T x) throw ()
 
template<typename T >
static T cbrt (T x) throw ()
 
template<typename T >
static T sum (T u, T v, T &t) throw ()
 
template<typename T >
static T AngNormalize (T x) throw ()
 
template<typename T >
static T AngNormalize2 (T x) throw ()
 
template<typename T >
static T AngDiff (T x, T y) throw ()
 
template<typename T >
static bool isfinite (T x) throw ()
 
template<typename T >
static T NaN () throw ()
 
static real NaN () throw ()
 
template<typename T >
static bool isnan (T x) throw ()
 
template<typename T >
static T infinity () throw ()
 
static real infinity () throw ()
 
template<typename T >
static T swab (T x)
 

Static Public Attributes

static const int extradigits
 
static const bool bigendian = WORDS_BIGENDIAN
 

Detailed Description

Mathematical functions needed by GeographicLib.

Define mathematical functions in order to localize system dependencies and to provide generic versions of the functions. In addition define a real type to be used by GeographicLib.

Example of use:

// Example of using the GeographicLib::Math class
#include <iostream>
#include <exception>
using namespace std;
using namespace GeographicLib;
int main() {
try {
cout << Math::pi() << " " << Math::sq(Math::pi()) << "\n";
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
return 0;
}

Definition at line 73 of file Math.hpp.

Member Typedef Documentation

Definition at line 90 of file Math.hpp.

typedef double GeographicLib::Math::real

The real type for GeographicLib. Nearly all the testing has been done with real = double. However, the algorithms should also work with float and long double (where available). (CAUTION: reasonable accuracy typically cannot be obtained using floats.)

Definition at line 100 of file Math.hpp.

Member Function Documentation

template<typename T >
static T GeographicLib::Math::pi ( )
throw (
)
inlinestatic
Template Parameters
Tthe type of the returned value.
Returns
π.

Definition at line 128 of file Math.hpp.

static real GeographicLib::Math::pi ( )
throw (
)
inlinestatic

A synonym for pi<real>().

Definition at line 133 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::degree ( )
throw (
)
inlinestatic
Template Parameters
Tthe type of the returned value.
Returns
the number of radians in a degree.

Definition at line 139 of file Math.hpp.

static real GeographicLib::Math::degree ( )
throw (
)
inlinestatic

A synonym for degree<real>().

Definition at line 144 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::sq ( x)
throw (
)
inlinestatic

Square a number.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
x2.

Definition at line 153 of file Math.hpp.

Referenced by GeographicLib::Ellipsoid::Area(), GeographicLib::SphericalEngine::Circle(), GeographicLib::MagneticModel::FieldComponents(), GeographicLib::Ellipsoid::FlatteningToSecondEccentricitySq(), GeographicLib::Ellipsoid::FlatteningToThirdEccentricitySq(), GeographicLib::PolarStereographic::Forward(), GeographicLib::TransverseMercator::Forward(), GeographicLib::AlbersEqualArea::Forward(), GeographicLib::LambertConformalConic::Forward(), GeographicLib::GeodesicExact::GenInverse(), GeographicLib::Geodesic::GenInverse(), GeographicLib::GeodesicLineExact::GenPosition(), GeographicLib::GeodesicLine::GenPosition(), GeographicLib::GeodesicLine::GeodesicLine(), GeographicLib::GeodesicLineExact::GeodesicLineExact(), GeographicLib::GravityModel::GravityModel(), GeographicLib::Ellipsoid::MeridionalCurvatureRadius(), GeographicLib::Ellipsoid::NormalCurvatureRadius(), GeographicLib::NormalGravity::Phi(), GeographicLib::EllipticFunction::Reset(), GeographicLib::PolarStereographic::Reverse(), GeographicLib::TransverseMercator::Reverse(), GeographicLib::AlbersEqualArea::Reverse(), GeographicLib::LambertConformalConic::Reverse(), GeographicLib::EllipticFunction::RG(), GeographicLib::EllipticFunction::RJ(), GeographicLib::AlbersEqualArea::SetScale(), GeographicLib::NormalGravity::SurfaceGravity(), GeographicLib::Ellipsoid::TransverseCurvatureRadius(), GeographicLib::TransverseMercator::TransverseMercator(), GeographicLib::NormalGravity::V0(), GeographicLib::SphericalEngine::Value(), and GeographicLib::Ellipsoid::Volume().

template<typename T >
static T GeographicLib::Math::hypot ( x,
y 
)
throw (
)
inlinestatic
template<typename T >
static T GeographicLib::Math::expm1 ( x)
throw (
)
inlinestatic

exp(x) − 1 accurate near x = 0. This is taken from N. J. Higham, Accuracy and Stability of Numerical Algorithms, 2nd Edition (SIAM, 2002), Sec 1.14.1, p 19.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
exp(x) − 1.

Definition at line 221 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::log1p ( x)
throw (
)
inlinestatic

log(1 + x) accurate near x = 0.

This is taken from D. Goldberg, What every computer scientist should know about floating-point arithmetic (1991), Theorem 4. See also, Higham (op. cit.), Answer to Problem 1.5, p 528.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
log(1 + x).

Definition at line 256 of file Math.hpp.

Referenced by GeographicLib::LambertConformalConic::Reverse().

template<typename T >
static T GeographicLib::Math::asinh ( x)
throw (
)
inlinestatic

The inverse hyperbolic sine function. This is defined in terms of Math::log1p(x) in order to maintain accuracy near x = 0. In addition, the odd parity of the function is enforced.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
asinh(x).

Definition at line 288 of file Math.hpp.

Referenced by GeographicLib::TransverseMercator::Forward(), GeographicLib::LambertConformalConic::Forward(), and GeographicLib::Ellipsoid::IsometricLatitude().

template<typename T >
static T GeographicLib::Math::atanh ( x)
throw (
)
inlinestatic

The inverse hyperbolic tangent function. This is defined in terms of Math::log1p(x) in order to maintain accuracy near x = 0. In addition, the odd parity of the function is enforced.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
atanh(x).

Definition at line 315 of file Math.hpp.

Referenced by GeographicLib::Ellipsoid::Area(), and GeographicLib::EllipticFunction::RC().

template<typename T >
static T GeographicLib::Math::cbrt ( x)
throw (
)
inlinestatic

The cube root function.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
the real cube root of x.

Definition at line 340 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::sum ( u,
v,
T &  t 
)
throw (
)
inlinestatic

The error-free sum of two numbers.

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]u
[in]v
[out]tthe exact error given by (u + v) - s.
Returns
s = round(u + v).

See D. E. Knuth, TAOCP, Vol 2, 4.2.2, Theorem B. (Note that t can be the same as one of the first two arguments.)

Definition at line 367 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::AngNormalize ( x)
throw (
)
inlinestatic
template<typename T >
static T GeographicLib::Math::AngNormalize2 ( x)
throw (
)
inlinestatic

Normalize an arbitrary angle.

Template Parameters
Tthe type of the argument and returned value.
Parameters
[in]xthe angle in degrees.
Returns
the angle reduced to the range [−180°, 180°).

The range of x is unrestricted.

Definition at line 400 of file Math.hpp.

Referenced by GeographicLib::GeodesicLineExact::GenPosition(), and GeographicLib::GeodesicLine::GenPosition().

template<typename T >
static T GeographicLib::Math::AngDiff ( x,
y 
)
throw (
)
inlinestatic

Difference of two angles reduced to [−180°, 180°]

Template Parameters
Tthe type of the arguments and returned value.
Parameters
[in]xthe first angle in degrees.
[in]ythe second angle in degrees.
Returns
yx, reduced to the range [−180°, 180°].

x and y must both lie in [−180°, 180°]. The result is equivalent to computing the difference exactly, reducing it to (−180°, 180°] and rounding the result. Note that this prescription allows −180° to be returned (e.g., if x is tiny and negative and y = 180°).

Definition at line 418 of file Math.hpp.

Referenced by GeographicLib::TransverseMercator::Forward(), GeographicLib::CassiniSoldner::Forward(), GeographicLib::TransverseMercatorExact::Forward(), GeographicLib::AlbersEqualArea::Forward(), GeographicLib::LambertConformalConic::Forward(), GeographicLib::GeodesicExact::GenInverse(), and GeographicLib::Geodesic::GenInverse().

template<typename T >
static bool GeographicLib::Math::isfinite ( x)
throw (
)
inlinestatic
template<typename T >
static T GeographicLib::Math::NaN ( )
throw (
)
inlinestatic

The NaN (not a number)

Template Parameters
Tthe type of the returned value.
Returns
NaN if available, otherwise return the max real of type T.

Definition at line 469 of file Math.hpp.

static real GeographicLib::Math::NaN ( )
throw (
)
inlinestatic

A synonym for NaN<real>().

Definition at line 477 of file Math.hpp.

template<typename T >
static bool GeographicLib::Math::isnan ( x)
throw (
)
inlinestatic

Test for NaN.

Template Parameters
Tthe type of the argument.
Parameters
[in]x
Returns
true if argument is a NaN.

Definition at line 486 of file Math.hpp.

Referenced by GeographicLib::Geohash::Forward(), GeographicLib::MGRS::Forward(), GeographicLib::GeoCoords::GeoRepresentation(), main(), GeographicLib::UTMUPS::Reverse(), and GeographicLib::UTMUPS::StandardZone().

template<typename T >
static T GeographicLib::Math::infinity ( )
throw (
)
inlinestatic

Infinity

Template Parameters
Tthe type of the returned value.
Returns
infinity if available, otherwise return the max real.

Definition at line 500 of file Math.hpp.

static real GeographicLib::Math::infinity ( )
throw (
)
inlinestatic

A synonym for infinity<real>().

Definition at line 508 of file Math.hpp.

template<typename T >
static T GeographicLib::Math::swab ( x)
inlinestatic

Swap the bytes of a quantity

Template Parameters
Tthe type of the argument and the returned value.
Parameters
[in]x
Returns
x with its bytes swapped.

Definition at line 517 of file Math.hpp.

Member Data Documentation

const int GeographicLib::Math::extradigits
static
Initial value:
=
std::numeric_limits<real>::digits10 >
std::numeric_limits<double>::digits10 ?
std::numeric_limits<real>::digits10 -
std::numeric_limits<double>::digits10 : 0

Number of additional decimal digits of precision of real relative to double (0 for float).

Definition at line 113 of file Math.hpp.

Referenced by GeographicLib::GeoCoords::DMSRepresentation(), GeographicLib::DMS::Encode(), GeographicLib::GeoCoords::GeoRepresentation(), and main().

const bool GeographicLib::Math::bigendian = WORDS_BIGENDIAN
static

true if the machine is big-endian.

Definition at line 122 of file Math.hpp.

Referenced by GeographicLib::Utility::readarray(), and GeographicLib::Utility::writearray().


The documentation for this class was generated from the following file: