ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
Common mathematical functions

Math utilities, fixed-point arithmetic, and related types. More...

Functions

template<typename T>
constexpr T toy::math::abs (const T &value) noexcept
 Returns the absolute value of value.
template<typename T>
constexpr bool toy::math::isEqual (const T &a, const T &b, T absEpsilon=8 *numeric_limits< T >::epsilon(), T relEpsilon=64 *numeric_limits< T >::epsilon()) noexcept
 Compares two values for approximate equality.
template<typename T>
constexpr T toy::math::deg2rad (const T &angle) noexcept
 Converts angle from degrees to radians.
template<typename T>
constexpr T toy::math::rad2deg (const T &angle) noexcept
 Converts angle from radians to degrees.

Detailed Description

Math utilities, fixed-point arithmetic, and related types.

  • abs: Absolute value for signed integers, floating-point (float, double, long double), and fixed-point; branch-free for integral and common float types where applicable.
  • isEqual: Approximate equality with configurable absolute and relative epsilon (default 8× and 64× numeric_limits<T>::epsilon()); overloads for standard floating-point and fixed-point types.
  • deg2rad, rad2deg: Angle conversion (degrees ↔ radians) for floating-point and fixed-point.

Key Features

  • Constexpr support: Available where logically feasible.
  • Exception safety: Uses noexcept operations.
  • Type safety: Enforced through C++20 concepts.
  • Allocation-Free Core Types: No dynamic allocation in core math types.

Function Documentation

◆ abs()

template<typename T>
T toy::math::abs ( const T & value)
nodiscardconstexprnoexcept

Returns the absolute value of value.

Supports signed integers, floating-point, and fixed-point. Branch-free for signed integrals and fixed-point, for floating-point uses a conditional. Usable in constant expressions.

Template Parameters
TMust satisfy toy::math::signed_integral, toy::math::floating_point, or toy::math::fixed_point.
Parameters
valueThe value (signed integer, floating-point, or fixed-point).
Returns
The absolute value of value (unchanged if non-negative, negated if negative).

◆ deg2rad()

template<typename T>
T toy::math::deg2rad ( const T & angle)
nodiscardconstexprnoexcept

Converts angle from degrees to radians.

Template Parameters
TScalar type; must satisfy toy::math::floating_point or toy::math::fixed_point.
Parameters
angleAngle in degrees.
Returns
Angle in radians (angle × π / 180). Supports float, double, long double, and toy::math::fixed.
See also
rad2deg

◆ isEqual()

template<typename T>
bool toy::math::isEqual ( const T & a,
const T & b,
T absEpsilon = 8 *numeric_limits< T >::epsilon(),
T relEpsilon = 64 *numeric_limits< T >::epsilon() )
nodiscardconstexprnoexcept

Compares two values for approximate equality.

Combined absolute and relative epsilon test: small values by absEpsilon, large values by relEpsilon scaled by max(|a|, |b|). Supports floating-point and fixed-point. For floating-point, does not support NaN inputs (assert in debug).

Template Parameters
TMust satisfy toy::math::floating_point or toy::math::fixed_point.
Parameters
aThe first value.
bThe second value.
absEpsilonThe maximum absolute difference treated as equal (default: 8× numeric_limits<T>::epsilon()).
relEpsilonThe maximum relative difference (default: 64× numeric_limits<T>::epsilon()).
Returns
true if a and b are considered equal under the chosen tolerances, false otherwise.

◆ rad2deg()

template<typename T>
T toy::math::rad2deg ( const T & angle)
nodiscardconstexprnoexcept

Converts angle from radians to degrees.

Template Parameters
TScalar type; must satisfy toy::math::floating_point or toy::math::fixed_point.
Parameters
angleAngle in radians.
Returns
Angle in degrees (angle × 180 / π). Supports float, double, long double, and toy::math::fixed.
See also
deg2rad