WPILibC++ 2024.3.2
Unit Math

Defines a collection of unit-enabled, strongly-typed versions of <cmath> functions. More...

## Functions

template<class AngleUnit >
dimensionless::scalar_t units::math::cos (const AngleUnit angle) noexcept
Compute cosine. More...

template<class AngleUnit >
dimensionless::scalar_t units::math::sin (const AngleUnit angle) noexcept
Compute sine. More...

template<class AngleUnit >
dimensionless::scalar_t units::math::tan (const AngleUnit angle) noexcept
Compute tangent. More...

template<class ScalarUnit >
angle::radian_t units::math::acos (const ScalarUnit x) noexcept
Compute arc cosine. More...

template<class ScalarUnit >
angle::radian_t units::math::asin (const ScalarUnit x) noexcept
Compute arc sine. More...

template<class ScalarUnit >
angle::radian_t units::math::atan (const ScalarUnit x) noexcept
Compute arc tangent. More...

template<class Y , class X >
angle::radian_t units::math::atan2 (const Y y, const X x) noexcept
Compute arc tangent with two parameters. More...

template<class AngleUnit >
dimensionless::scalar_t units::math::cosh (const AngleUnit angle) noexcept
Compute hyperbolic cosine. More...

template<class AngleUnit >
dimensionless::scalar_t units::math::sinh (const AngleUnit angle) noexcept
Compute hyperbolic sine. More...

template<class AngleUnit >
dimensionless::scalar_t units::math::tanh (const AngleUnit angle) noexcept
Compute hyperbolic tangent. More...

template<class ScalarUnit >
angle::radian_t units::math::acosh (const ScalarUnit x) noexcept
Compute arc hyperbolic cosine. More...

template<class ScalarUnit >
angle::radian_t units::math::asinh (const ScalarUnit x) noexcept
Compute arc hyperbolic sine. More...

template<class ScalarUnit >
angle::radian_t units::math::atanh (const ScalarUnit x) noexcept
Compute arc hyperbolic tangent. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::exp (const ScalarUnit x) noexcept
Compute exponential function. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::log (const ScalarUnit x) noexcept
Compute natural logarithm. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::log10 (const ScalarUnit x) noexcept
Compute common logarithm. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::modf (const ScalarUnit x, ScalarUnit *intpart) noexcept
Break into fractional and integral parts. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::exp2 (const ScalarUnit x) noexcept
Compute binary exponential function. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::expm1 (const ScalarUnit x) noexcept
Compute exponential minus one. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::log1p (const ScalarUnit x) noexcept
Compute logarithm plus one. More...

template<class ScalarUnit >
dimensionless::scalar_t units::math::log2 (const ScalarUnit x) noexcept
Compute binary logarithm. More...

template<class UnitType , std::enable_if_t< units::traits::has_linear_scale< UnitType >::value, int > = 0>
auto units::math::sqrt (const UnitType &value) noexcept -> unit_t< square_root< typename units::traits::unit_t_traits< UnitType >::unit_type >, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the square root of value More...

template<class UnitTypeLhs , class UnitTypeRhs , std::enable_if_t< units::traits::has_linear_scale< UnitTypeLhs, UnitTypeRhs >::value, int > = 0>
UnitTypeLhs units::math::hypot (const UnitTypeLhs &x, const UnitTypeRhs &y)
Computes the square root of the sum-of-squares of x and y. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::ceil (const UnitType x) noexcept
Round up value. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::floor (const UnitType x) noexcept
Round down value. More...

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
UnitTypeLhs units::math::fmod (const UnitTypeLhs numer, const UnitTypeRhs denom) noexcept
Compute remainder of division. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::trunc (const UnitType x) noexcept
Truncate value. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::round (const UnitType x) noexcept
Round to nearest. More...

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
UnitTypeLhs units::math::copysign (const UnitTypeLhs x, const UnitTypeRhs y) noexcept
Copy sign. More...

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
UnitTypeLhs units::math::fdim (const UnitTypeLhs x, const UnitTypeRhs y) noexcept
Positive difference. More...

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
UnitTypeLhs units::math::fmax (const UnitTypeLhs x, const UnitTypeRhs y) noexcept
Maximum value. More...

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
UnitTypeLhs units::math::fmin (const UnitTypeLhs x, const UnitTypeRhs y) noexcept
Minimum value. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::fabs (const UnitType x) noexcept
Compute absolute value. More...

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
UnitType units::math::abs (const UnitType x) noexcept
Compute absolute value. More...

template<class UnitTypeLhs , class UnitMultiply , class UnitAdd , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitMultiply>::value && traits::is_unit_t<UnitAdd>::value>>
auto units::math::fma (const UnitTypeLhs x, const UnitMultiply y, const UnitAdd z) noexcept -> decltype(x *y)

## Detailed Description

Defines a collection of unit-enabled, strongly-typed versions of <cmath> functions.

Includes most c++11 extensions.

## ◆ abs()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::abs ( const UnitType x )
noexcept

Compute absolute value.

Returns the absolute value of x, i.e. |x|.

Parameters
 [in] x Value whose absolute value is returned.
Returns
The absolute value of x.

## ◆ acos()

template<class ScalarUnit >
 angle::radian_t units::math::acos ( const ScalarUnit x )
noexcept

Compute arc cosine.

Returns the principal value of the arc cosine of x, expressed in radians.

Parameters
 [in] x Value whose arc cosine is computed, in the interval [-1,+1].
Returns
Principal arc cosine of x, in the interval [0,pi] radians.

## ◆ acosh()

template<class ScalarUnit >
 angle::radian_t units::math::acosh ( const ScalarUnit x )
noexcept

Compute arc hyperbolic cosine.

Returns the nonnegative arc hyperbolic cosine of x, expressed in radians.

Parameters
 [in] x Value whose arc hyperbolic cosine is computed. If the argument is less than 1, a domain error occurs.
Returns
Nonnegative arc hyperbolic cosine of x, in the interval [0,+INFINITY] radians.

## ◆ asin()

template<class ScalarUnit >
 angle::radian_t units::math::asin ( const ScalarUnit x )
noexcept

Compute arc sine.

Returns the principal value of the arc sine of x, expressed in radians.

Parameters
 [in] x Value whose arc sine is computed, in the interval [-1,+1].
Returns
Principal arc sine of x, in the interval [-pi/2,+pi/2] radians.

## ◆ asinh()

template<class ScalarUnit >
 angle::radian_t units::math::asinh ( const ScalarUnit x )
noexcept

Compute arc hyperbolic sine.

Returns the arc hyperbolic sine of x, expressed in radians.

Parameters
 [in] x Value whose arc hyperbolic sine is computed.
Returns
Arc hyperbolic sine of x, in radians.

## ◆ atan()

template<class ScalarUnit >
 angle::radian_t units::math::atan ( const ScalarUnit x )
noexcept

Compute arc tangent.

Returns the principal value of the arc tangent of x, expressed in radians. Notice that because of the sign ambiguity, the function cannot determine with certainty in which quadrant the angle falls only by its tangent value. See atan2 for an alternative that takes a fractional argument instead.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] x Value whose arc tangent is computed, in the interval [-1,+1].
Returns
Principal arc tangent of x, in the interval [-pi/2,+pi/2] radians.

## ◆ atan2()

template<class Y , class X >
 angle::radian_t units::math::atan2 ( const Y y, const X x )
noexcept

Compute arc tangent with two parameters.

To compute the value, the function takes into account the sign of both arguments in order to determine the quadrant.

Parameters
 [in] y y-component of the triangle expressed. [in] x x-component of the triangle expressed.
Returns
Returns the principal value of the arc tangent of y/x, expressed in radians.

## ◆ atanh()

template<class ScalarUnit >
 angle::radian_t units::math::atanh ( const ScalarUnit x )
noexcept

Compute arc hyperbolic tangent.

Returns the arc hyperbolic tangent of x, expressed in radians.

Parameters
 [in] x Value whose arc hyperbolic tangent is computed, in the interval [-1,+1]. If the argument is out of this interval, a domain error occurs. For values of -1 and +1, a pole error may occur.
Returns

## ◆ ceil()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::ceil ( const UnitType x )
noexcept

Round up value.

Rounds x upward, returning the smallest integral value that is not less than x.

Parameters
 [in] x Unit value to round up.
Returns
The smallest integral value that is not less than x.

## ◆ copysign()

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
 UnitTypeLhs units::math::copysign ( const UnitTypeLhs x, const UnitTypeRhs y )
noexcept

Copy sign.

Returns a value with the magnitude and dimension of x, and the sign of y. Values x and y do not have to be compatible units.

Parameters
 [in] x Value with the magnitude of the resulting value. [in] y Value with the sign of the resulting value.
Returns
value with the magnitude and dimension of x, and the sign of y.

## ◆ cos()

template<class AngleUnit >
 dimensionless::scalar_t units::math::cos ( const AngleUnit angle )
noexcept

Compute cosine.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the cosine of
Returns
Returns the cosine of angle

## ◆ cosh()

template<class AngleUnit >
 dimensionless::scalar_t units::math::cosh ( const AngleUnit angle )
noexcept

Compute hyperbolic cosine.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the hyperbolic cosine of
Returns
Returns the hyperbolic cosine of angle

## ◆ exp()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::exp ( const ScalarUnit x )
noexcept

Compute exponential function.

Returns the base-e exponential function of x, which is e raised to the power x: ex.

Parameters
 [in] x scalar value of the exponent.
Returns
Exponential value of x. If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the proper sign, and an overflow range error occurs.

## ◆ exp2()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::exp2 ( const ScalarUnit x )
noexcept

Compute binary exponential function.

Returns the base-2 exponential function of x, which is 2 raised to the power x: 2^x. 2param[in] x Value of the exponent.

Returns
2 raised to the power of x.

## ◆ expm1()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::expm1 ( const ScalarUnit x )
noexcept

Compute exponential minus one.

Returns e raised to the power x minus one: e^x-1. For small magnitude values of x, expm1 may be more accurate than exp(x)-1.

Parameters
 [in] x Value of the exponent.
Returns
e raised to the power of x, minus one.

## ◆ fabs()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::fabs ( const UnitType x )
noexcept

Compute absolute value.

Returns the absolute value of x, i.e. |x|.

Parameters
 [in] x Value whose absolute value is returned.
Returns
The absolute value of x.

## ◆ fdim()

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
 UnitTypeLhs units::math::fdim ( const UnitTypeLhs x, const UnitTypeRhs y )
noexcept

Positive difference.

The function returns x-y if x>y, and zero otherwise, in the same units as x. Values x and y do not have to be the same type of units, but they do have to be compatible.

Parameters
 [in] x Values whose difference is calculated. [in] y Values whose difference is calculated.
Returns
The positive difference between x and y.

## ◆ floor()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::floor ( const UnitType x )
noexcept

Round down value.

Rounds x downward, returning the largest integral value that is not greater than x.

Parameters
 [in] x Unit value to round down.
Returns
The value of x rounded downward.

## ◆ fma()

template<class UnitTypeLhs , class UnitMultiply , class UnitAdd , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitMultiply>::value && traits::is_unit_t<UnitAdd>::value>>
 auto units::math::fma ( const UnitTypeLhs x, const UnitMultiply y, const UnitAdd z ) -> decltype(x * y)
noexcept

Returns x*y+z. The function computes the result without losing precision in any intermediate result. The resulting unit type is a compound unit of x* y.

Parameters
 [in] x Values to be multiplied. [in] y Values to be multiplied. [in] z Value to be added.
Returns
The result of x*y+z

## ◆ fmax()

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
 UnitTypeLhs units::math::fmax ( const UnitTypeLhs x, const UnitTypeRhs y )
noexcept

Maximum value.

Returns the larger of its arguments: either x or y, in the same units as x. Values x and y do not have to be the same type of units, but they do have to be compatible.

Parameters
 [in] x Values among which the function selects a maximum. [in] y Values among which the function selects a maximum.
Returns
The maximum numeric value of its arguments.

## ◆ fmin()

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
 UnitTypeLhs units::math::fmin ( const UnitTypeLhs x, const UnitTypeRhs y )
noexcept

Minimum value.

Returns the smaller of its arguments: either x or y, in the same units as x. If one of the arguments in a NaN, the other is returned. Values x and y do not have to be the same type of units, but they do have to be compatible.

Parameters
 [in] x Values among which the function selects a minimum. [in] y Values among which the function selects a minimum.
Returns
The minimum numeric value of its arguments.

## ◆ fmod()

template<class UnitTypeLhs , class UnitTypeRhs , class = std::enable_if_t<traits::is_unit_t<UnitTypeLhs>::value && traits::is_unit_t<UnitTypeRhs>::value>>
 UnitTypeLhs units::math::fmod ( const UnitTypeLhs numer, const UnitTypeRhs denom )
noexcept

Compute remainder of division.

Returns the floating-point remainder of numer/denom (rounded towards zero).

Parameters
 [in] numer Value of the quotient numerator. [in] denom Value of the quotient denominator.
Returns
The remainder of dividing the arguments.

## ◆ hypot()

template<class UnitTypeLhs , class UnitTypeRhs , std::enable_if_t< units::traits::has_linear_scale< UnitTypeLhs, UnitTypeRhs >::value, int > = 0>
 UnitTypeLhs units::math::hypot ( const UnitTypeLhs & x, const UnitTypeRhs & y )
inline

Computes the square root of the sum-of-squares of x and y.

Only implemented for linear_scale units.

Parameters
 [in] x unit_t type value [in] y unit_t type value
Returns
square root of the sum-of-squares of x and y in the same units as x.

## ◆ log()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::log ( const ScalarUnit x )
noexcept

Compute natural logarithm.

Returns the natural logarithm of x.

Parameters
 [in] x scalar value whose logarithm is calculated. If the argument is negative, a domain error occurs.
log10 for more common base-10 logarithms
Returns
Natural logarithm of x.

## ◆ log10()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::log10 ( const ScalarUnit x )
noexcept

Compute common logarithm.

Returns the common (base-10) logarithm of x.

Parameters
 [in] x Value whose logarithm is calculated. If the argument is negative, a domain error occurs.
Returns
Common logarithm of x.

## ◆ log1p()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::log1p ( const ScalarUnit x )
noexcept

Compute logarithm plus one.

Returns the natural logarithm of one plus x. For small magnitude values of x, logp1 may be more accurate than log(1+x).

Parameters
 [in] x Value whose logarithm is calculated. If the argument is less than -1, a domain error occurs.
Returns
The natural logarithm of (1+x).

## ◆ log2()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::log2 ( const ScalarUnit x )
noexcept

Compute binary logarithm.

Returns the binary (base-2) logarithm of x.

Parameters
 [in] x Value whose logarithm is calculated. If the argument is negative, a domain error occurs.
Returns
The binary logarithm of x: log2x.

## ◆ modf()

template<class ScalarUnit >
 dimensionless::scalar_t units::math::modf ( const ScalarUnit x, ScalarUnit * intpart )
noexcept

Break into fractional and integral parts.

The integer part is stored in the object pointed by intpart, and the fractional part is returned by the function. Both parts have the same sign as x.

Parameters
 [in] x scalar value to break into parts. [in] intpart Pointer to an object (of the same type as x) where the integral part is stored with the same sign as x.
Returns
The fractional part of x, with the same sign.

## ◆ round()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::round ( const UnitType x )
noexcept

Round to nearest.

Returns the integral value that is nearest to x, with halfway cases rounded away from zero.

Parameters
 [in] x value to round.
Returns
The value of x rounded to the nearest integral.

## ◆ sin()

template<class AngleUnit >
 dimensionless::scalar_t units::math::sin ( const AngleUnit angle )
noexcept

Compute sine.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the since of
Returns
Returns the sine of angle

## ◆ sinh()

template<class AngleUnit >
 dimensionless::scalar_t units::math::sinh ( const AngleUnit angle )
noexcept

Compute hyperbolic sine.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the hyperbolic sine of
Returns
Returns the hyperbolic sine of angle

## ◆ sqrt()

template<class UnitType , std::enable_if_t< units::traits::has_linear_scale< UnitType >::value, int > = 0>
 auto units::math::sqrt ( const UnitType & value ) -> unit_t< square_root::unit_type>, typename units::traits::unit_t_traits::underlying_type, linear_scale>
inlinenoexcept

computes the square root of value

Only implemented for linear_scale units.

Parameters
 [in] value unit_t derived type to compute the square root of.
Returns
new unit_t, whose units are the square root of value's. E.g. if values had units of square_meter, then the return type will have units of meter.
Note
sqrt provides a rational approximation of the square root of value. In some cases, both the returned value and conversion factor of the returned unit type may have errors no larger than 1e-10.

## ◆ tan()

template<class AngleUnit >
 dimensionless::scalar_t units::math::tan ( const AngleUnit angle )
noexcept

Compute tangent.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the tangent of
Returns
Returns the tangent of angle

## ◆ tanh()

template<class AngleUnit >
 dimensionless::scalar_t units::math::tanh ( const AngleUnit angle )
noexcept

Compute hyperbolic tangent.

The input value can be in any unit of angle, including radians or degrees.

Template Parameters
 AngleUnit any unit_t type of category::angle_unit.
Parameters
 [in] angle angle to compute the hyperbolic tangent of
Returns
Returns the hyperbolic tangent of angle

## ◆ trunc()

template<class UnitType , class = std::enable_if_t<traits::is_unit_t<UnitType>::value>>
 UnitType units::math::trunc ( const UnitType x )
noexcept

Truncate value.

Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x. Effectively rounds towards 0.

Parameters
 [in] x Value to truncate
Returns
The nearest integral value that is not larger in magnitude than x.