Mathematical functions needed by GeographicLib.
More...
#include <Math.hpp>
|
template<typename T > |
static T | AngDiff (T x, T y, T &e) |
|
template<typename T > |
static T | AngDiff (T x, T y) |
|
template<typename T > |
static T | AngNormalize (T x) |
|
template<typename T > |
static T | AngRound (T x) |
|
template<typename T > |
static T | asinh (T x) |
|
template<typename T > |
static T | atan2d (T y, T x) |
|
template<typename T > |
static T | atand (T x) |
|
template<typename T > |
static T | atanh (T x) |
|
template<typename T > |
static T | cbrt (T x) |
|
template<typename T > |
static T | copysign (T x, T y) |
|
template<typename T > |
static T | cosd (T x) |
|
template<typename T > |
static T | degree () |
|
static real | degree () |
|
static int | digits () |
|
static int | digits10 () |
|
template<typename T > |
static T | eatanhe (T x, T es) |
|
template<typename T > |
static T | expm1 (T x) |
|
static int | extra_digits () |
|
template<typename T > |
static T | fma (T x, T y, T z) |
|
template<typename T > |
static T | hypot (T x, T y) |
|
template<typename T > |
static T | infinity () |
|
static real | infinity () |
|
template<typename T > |
static bool | isfinite (T x) |
|
template<typename T > |
static bool | isnan (T x) |
|
template<typename T > |
static T | LatFix (T x) |
|
template<typename T > |
static T | log1p (T x) |
|
template<typename T > |
static T | NaN () |
|
static real | NaN () |
|
template<typename T > |
static void | norm (T &x, T &y) |
|
template<typename T > |
static T | pi () |
|
static real | pi () |
|
template<typename T > |
static T | polyval (int N, const T p[], T x) |
|
static int | set_digits (int ndigits) |
|
template<typename T > |
static void | sincosd (T x, T &sinx, T &cosx) |
|
template<typename T > |
static T | sind (T x) |
|
template<typename T > |
static T | sq (T x) |
|
template<typename T > |
static T | sum (T u, T v, T &t) |
|
template<typename T > |
static T | swab (T x) |
|
template<typename T > |
static T | tand (T x) |
|
template<typename T > |
static T | tauf (T taup, T es) |
|
template<typename T > |
static T | taupf (T tau, T es) |
|
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:
#include <iostream>
#include <exception>
try {
}
catch (const exception& e) {
cerr << "Caught exception: " << e.what() << "\n";
return 1;
}
}
Definition at line 102 of file Math.hpp.
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 129 of file Math.hpp.
GeographicLib::Math::Math |
( |
| ) |
|
|
private |
template<typename T >
static T GeographicLib::Math::AngDiff |
( |
T |
x, |
|
|
T |
y, |
|
|
T & |
e |
|
) |
| |
|
inlinestatic |
The exact difference of two angles reduced to (−180°, 180°].
- Template Parameters
-
T | the type of the arguments and returned value. |
- Parameters
-
[in] | x | the first angle in degrees. |
[in] | y | the second angle in degrees. |
[out] | e | the error term in degrees. |
- Returns
- d, the truncated value of y − x.
This computes z = y − x exactly, reduced to (−180°, 180°]; and then sets z = d + e where d is the nearest representable number to z and e is the truncation error. If d = −180, then e > 0; If d = 180, then e ≤ 0.
Definition at line 486 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::AngDiff |
( |
T |
x, |
|
|
T |
y |
|
) |
| |
|
inlinestatic |
Difference of two angles reduced to [−180°, 180°]
- Template Parameters
-
T | the type of the arguments and returned value. |
- Parameters
-
[in] | x | the first angle in degrees. |
[in] | y | the second angle in degrees. |
- Returns
- y − x, reduced to the range [−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 517 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::AngNormalize |
( |
T |
x | ) |
|
|
inlinestatic |
Normalize an angle.
- Template Parameters
-
T | the type of the argument and returned value. |
- Parameters
-
[in] | x | the angle in degrees. |
- Returns
- the angle reduced to the range([−180°, 180°].
The range of x is unrestricted.
Definition at line 440 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::AngRound |
( |
T |
x | ) |
|
|
inlinestatic |
Coarsen a value close to zero.
- Template Parameters
-
T | the type of the argument and returned value. |
- Parameters
-
- Returns
- the coarsened value.
The makes the smallest gap in x = 1/16 - nextafter(1/16, 0) = 1/257 for reals = 0.7 pm on the earth if x is an angle in degrees. (This is about 1000 times more resolution than we get with angles around 90°.) We use this to avoid having to deal with near singular cases when x is non-zero but tiny (e.g., 10−200). This converts -0 to +0; however tiny negative numbers get converted to -0.
Definition at line 535 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::asinh |
( |
T |
x | ) |
|
|
inlinestatic |
The inverse hyperbolic sine function.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- asinh(x).
Definition at line 311 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::atan2d |
( |
T |
y, |
|
|
T |
x |
|
) |
| |
|
inlinestatic |
Evaluate the atan2 function with the result in degrees
- Template Parameters
-
T | the type of the arguments and the returned value. |
- Parameters
-
- Returns
- atan2(y, x) in degrees.
The result is in the range (−180° 180°]. N.B., atan2d(±0, −1) = +180°; atan2d(−ε, −1) = −180°, for ε positive and tiny; atan2d(±0, +1) = ±0°.
Definition at line 691 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::atand |
( |
T |
x | ) |
|
|
inlinestatic |
Evaluate the atan function with the result in degrees
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- atan(x) in degrees.
Definition at line 723 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::atanh |
( |
T |
x | ) |
|
|
inlinestatic |
The inverse hyperbolic tangent function.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- atanh(x).
Definition at line 328 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::cbrt |
( |
T |
x | ) |
|
|
inlinestatic |
The cube root function.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- the real cube root of x.
Definition at line 345 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::copysign |
( |
T |
x, |
|
|
T |
y |
|
) |
| |
|
inlinestatic |
Copy the sign.
- Template Parameters
-
T | the type of the argument. |
- Parameters
-
[in] | x | gives the magitude of the result. |
[in] | y | gives the sign of the result. |
- Returns
- value with the magnitude of x and with the sign of y.
This routine correctly handles the case y = −0, returning &minus|x|.
Definition at line 751 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::cosd |
( |
T |
x | ) |
|
|
inlinestatic |
Evaluate the cosine function with the argument in degrees
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- cos(x).
Definition at line 639 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::degree |
( |
| ) |
|
|
inlinestatic |
- Template Parameters
-
T | the type of the returned value. |
- Returns
- the number of radians in a degree.
Definition at line 216 of file Math.hpp.
static real GeographicLib::Math::degree |
( |
| ) |
|
|
inlinestatic |
static int GeographicLib::Math::digits |
( |
| ) |
|
|
inlinestatic |
- Returns
- the number of bits of precision in a real number.
Definition at line 145 of file Math.hpp.
static int GeographicLib::Math::digits10 |
( |
| ) |
|
|
inlinestatic |
- Returns
- the number of decimal digits of precision in a real number.
Definition at line 175 of file Math.hpp.
void GeographicLib::Math::dummy |
( |
| ) |
|
|
inlineprivate |
template<typename T >
T GeographicLib::Math::eatanhe |
( |
T |
x, |
|
|
T |
es |
|
) |
| |
|
static |
Evaluate e atanh(e x)
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
[in] | x | |
[in] | es | the signed eccentricity = sign(e2) sqrt(|e2|) |
- Returns
- e atanh(e x)
If e2 is negative (e is imaginary), the expression is evaluated in terms of atan.
Definition at line 21 of file Math.cpp.
template<typename T >
static T GeographicLib::Math::expm1 |
( |
T |
x | ) |
|
|
inlinestatic |
exp(x) − 1 accurate near x = 0.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- exp(x) − 1.
Definition at line 265 of file Math.hpp.
static int GeographicLib::Math::extra_digits |
( |
| ) |
|
|
inlinestatic |
Number of additional decimal digits of precision for real relative to double (0 for float).
Definition at line 187 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::fma |
( |
T |
x, |
|
|
T |
y, |
|
|
T |
z |
|
) |
| |
|
inlinestatic |
Fused multiply and add.
- Template Parameters
-
T | the type of the arguments and the returned value. |
- Parameters
-
- Returns
- xy + z, correctly rounded (on those platforms with support for the
fma
instruction).
On platforms without the fma
instruction, no attempt is made to improve on the result of a rounded multiplication followed by a rounded addition.
Definition at line 369 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::hypot |
( |
T |
x, |
|
|
T |
y |
|
) |
| |
|
inlinestatic |
The hypotenuse function avoiding underflow and overflow.
- Template Parameters
-
T | the type of the arguments and the returned value. |
- Parameters
-
- Returns
- sqrt(x2 + y2).
Definition at line 243 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::infinity |
( |
| ) |
|
|
inlinestatic |
Infinity
- Template Parameters
-
T | the type of the returned value. |
- Returns
- infinity if available, otherwise return the max real.
Definition at line 867 of file Math.hpp.
static real GeographicLib::Math::infinity |
( |
| ) |
|
|
inlinestatic |
template<typename T >
static bool GeographicLib::Math::isfinite |
( |
T |
x | ) |
|
|
inlinestatic |
Test for finiteness.
- Template Parameters
-
T | the type of the argument. |
- Parameters
-
- Returns
- true if number is finite, false if NaN or infinite.
Definition at line 806 of file Math.hpp.
template<typename T >
static bool GeographicLib::Math::isnan |
( |
T |
x | ) |
|
|
inlinestatic |
Test for NaN.
- Template Parameters
-
T | the type of the argument. |
- Parameters
-
- Returns
- true if argument is a NaN.
Definition at line 853 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::LatFix |
( |
T |
x | ) |
|
|
inlinestatic |
Normalize a latitude.
- Template Parameters
-
T | the type of the argument and returned value. |
- Parameters
-
[in] | x | the angle in degrees. |
- Returns
- x if it is in the range [−90°, 90°], otherwise return NaN.
Definition at line 467 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::log1p |
( |
T |
x | ) |
|
|
inlinestatic |
log(1 + x) accurate near x = 0.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- log(1 + x).
Definition at line 288 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::NaN |
( |
| ) |
|
|
inlinestatic |
The NaN (not a number)
- Template Parameters
-
T | the type of the returned value. |
- Returns
- NaN if available, otherwise return the max real of type T.
Definition at line 830 of file Math.hpp.
static real GeographicLib::Math::NaN |
( |
| ) |
|
|
inlinestatic |
template<typename T >
static void GeographicLib::Math::norm |
( |
T & |
x, |
|
|
T & |
y |
|
) |
| |
|
inlinestatic |
Normalize a two-vector.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
[in,out] | x | on output set to x/hypot(x, y). |
[in,out] | y | on output set to y/hypot(x, y). |
Definition at line 384 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::pi |
( |
| ) |
|
|
inlinestatic |
- Template Parameters
-
T | the type of the returned value. |
- Returns
- π.
Definition at line 202 of file Math.hpp.
static real GeographicLib::Math::pi |
( |
| ) |
|
|
inlinestatic |
template<typename T >
static T GeographicLib::Math::polyval |
( |
int |
N, |
|
|
const T |
p[], |
|
|
T |
x |
|
) |
| |
|
inlinestatic |
Evaluate a polynomial.
- Template Parameters
-
T | the type of the arguments and returned value. |
- Parameters
-
[in] | N | the order of the polynomial. |
[in] | p | the coefficient array (of size N + 1). |
[in] | x | the variable. |
- Returns
- the value of the polynomial.
Evaluate y = ∑n=0..N pn xN−n. Return 0 if N < 0. Return p0, if N = 0 (even if x is infinite or a nan). The evaluation uses Horner's method.
Definition at line 425 of file Math.hpp.
static int GeographicLib::Math::set_digits |
( |
int |
ndigits | ) |
|
|
inlinestatic |
Set the binary precision of a real number.
- Parameters
-
[in] | ndigits | the number of bits of precision. |
- Returns
- the resulting number of bits of precision.
This only has an effect when GEOGRAPHICLIB_PRECISION = 5. See also Utility::set_digits for caveats about when this routine should be called.
Definition at line 163 of file Math.hpp.
template<typename T >
static void GeographicLib::Math::sincosd |
( |
T |
x, |
|
|
T & |
sinx, |
|
|
T & |
cosx |
|
) |
| |
|
inlinestatic |
Evaluate the sine and cosine function with the argument in degrees
- Template Parameters
-
T | the type of the arguments. |
- Parameters
-
[in] | x | in degrees. |
[out] | sinx | sin(x). |
[out] | cosx | cos(x). |
The results obey exactly the elementary properties of the trigonometric functions, e.g., sin 9° = cos 81° = − sin 123456789°. If x = −0, then sinx = −0; this is the only case where −0 is returned.
Definition at line 558 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::sind |
( |
T |
x | ) |
|
|
inlinestatic |
Evaluate the sine function with the argument in degrees
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- sin(x).
Definition at line 609 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::sq |
( |
T |
x | ) |
|
|
inlinestatic |
Square a number.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- x2.
Definition at line 232 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::sum |
( |
T |
u, |
|
|
T |
v, |
|
|
T & |
t |
|
) |
| |
|
inlinestatic |
The error-free sum of two numbers.
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
[in] | u | |
[in] | v | |
[out] | t | the 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 399 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::swab |
( |
T |
x | ) |
|
|
inlinestatic |
Swap the bytes of a quantity
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- x with its bytes swapped.
Definition at line 890 of file Math.hpp.
template<typename T >
static T GeographicLib::Math::tand |
( |
T |
x | ) |
|
|
inlinestatic |
Evaluate the tangent function with the argument in degrees
- Template Parameters
-
T | the type of the argument and the returned value. |
- Parameters
-
- Returns
- tan(x).
If x = ±90°, then a suitably large (but finite) value is returned.
Definition at line 671 of file Math.hpp.
template<typename T >
T GeographicLib::Math::tauf |
( |
T |
taup, |
|
|
T |
es |
|
) |
| |
|
static |
template<typename T >
T GeographicLib::Math::taupf |
( |
T |
tau, |
|
|
T |
es |
|
) |
| |
|
static |
true if the machine is big-endian.
Definition at line 196 of file Math.hpp.
The documentation for this class was generated from the following files: