# Mathematical Operations¶

In the following, the data types T refer to arithmetic data types on which the operation is defined mathematically. This includes the active XAD types as well as the standard passive types.

The functions listed here are defined in the xad namespace, and C++ ADL rules (argument-dependent lookup) typically find these functions automatically if they are applied to XAD types. However, for this the calls must be unqualified, i.e. without a namespace specifier.

Alternatively, fully qualified names work as usual (e.g. xad::sin(x)), also for float and double.

For convenience, if the header XAD/StdCompatibility.hpp is included, the XAD variables are imported into the std namespace, so that existing calls to std::sin and similar functions are working as expected.

## Absolute Values, Max, Min, and Rounding¶

T abs(T x)

Computes the absolute value of x.

Note that for defined second-order derivatives, this computes (x>0)-(x<0)

T max(T x, T y)

Returns the maximum of x and y.

Note that for well-defined second order derivative, this is implemented as (x + y + abs(x-y)) / 2

T fmax(T x, T y)

Synonym for max()

T min(T x, T y)

Returns the minimum of x and y.

Note that for well-defined second order derivative, this is implemented as (x + y - abs(x-y)) / 2

T fmin(T x, T y)

Synonym for min()

T floor(T x)

Rounds towards negative infinity

T ceil(T x)

Rounds towards positive infinity

T trunc(T x)

Rounds towards 0

T round(T x)

Round to the nearest integer value

long lround(T x)

Like round(), but converting the result to a long type.

long long llround(T x)

Like round(), but converting the result to a long long type.

T fmod(T x, T y)

The floating-point remainder of the division operation x/y, i.e. exactly the value x - n*y, where n is x/y with its fractional part truncated.

T remainder(T x, T y)

The IEEE floating-point remainder of the division operation x/y, i.e. exactly the value x - n*y, where the value n is the integral value nearest the exact value x/y. When abs(n-x/y) = 0.5, the value n is chosen to be even.

In contrast to fmod(), the returned value is not guaranteed to have the same sign as x.

T remquo(T x, T y, int *n)

Same as remainer(), but returns the integer factor n in addition.

T modf(T x, T *iptr)

Decomposes x into integral and fractional parts, each with the same type and sign as x. The integral part is stored in iptr.

T nextafter(T from, T to)

Returns the next representable value of from in the direction of to.

Mathmatically, the difference of from to the return value is very small. For derivatives, we therefore consider them both the same and calculate derivative accordingly.

## Trigonometric Functions¶

T degrees(T x)

Converts the given value in radians to degrees

Converts the given value in degrees to radians

T cos(T x)

Computes the cosine of x

T sin(T x)

Computes the sine of x

T tan(T x)

Computes the tangent of x

T asin(T x)

Computes the inverse sine of x

T acos(T x)

Computes the inverse cosine of x

T atan(T x)

Computes the inverse tangent of x

T atan2(T x, T y)

Computes the four-quadrant inverse tangent of a point located at (x, y).

T sinh(T x)

Computes the hyperbolic sine of x

T cosh(T x)

Computes the hyperbolic cosine of x

T tanh(T x)

Computes the tangent of x

T asinh(T x)

Computes the inverse hyperbolic sine of x

T acosh(T x)

Computes the inverse hyperbolic cosine of x

T atanh(T x)

Computes the inverse hyperbolic tangent of x

## Powers, Exponentials, and Logarithms¶

T log(T x)

Computes the natural logarithm of x

T log10(T x)

Computes the base 10 logarithm of x

T log2(T x)

Computes the base 2 logarithm of x

T exp(T x)

Computes the exponential of x (base e)

T expm1(T x)

Computes exp(x) - 1 with higher precision around 0

T exp2(T x)

Computes 2 to the power of x

T log1p(T x)

Computes log(1 + x) with higher precision around 0

T sqrt(T x)

Computes the square root of x

T cbrt(T x)

Computes the cubic root of x

T pow(T x, T y)

Computes x to the power of y

T ldexp(T x, int exp)

Multiplies x by two to the power of exp

T frexp(T arg, int *exp)

Decomposes the given floating point value arg into a normalised fraction and an integral power of two.

int ilogb(T arg)

Returns the integral part of the logarithm of abs(x), using FLT_RADIX as base for the log.

T scalbn(T arg, int exp)

## Error Functions¶

T erf(T x)

Computes the error function of x, if provided by the compiler’s math library.

T erfc(T x)

Computes the complementary error function of x, if provided by the compiler’s math library.

## Floating Point Classification¶

bool isinf(T x)

Check if value is infinity (positive or negative)

bool isnan(T x)

Check if value is NaN

bool isfinite(T x)

Check if value is finite (not infinite and not NaN)

bool signbit(T x)

Returns true if x is negative and false otherwise. Also detects sign bit of zeros.

bool isnormal(T x)

Checks if the value is a normal floating point number, i.e. not zero, subnormal, infinite, or NaN.

Last update: January 2023