File math.hpp

Functions

template<typename T>
static inline constexpr T divideOrZero(T numerator, T denominator)

Returns the result of the floating point division numerator / denominator or 0, if a division by zero occurs.

Template Parameters:

T – The type of the operands

Parameters:
  • numerator – The numerator

  • denominator – The denominator

Returns:

The result of the division or 0, if a division by zero occurred

template<typename T>
static inline constexpr T arithmeticMean(T small, T large)

Calculates the arithmetic mean of two values small and large, where small < large.

The mean is calculated as small + ((large - small) * 0.5, instead of (small + large) / 2, to prevent overflows.

Parameters:
  • small – The smaller of both values

  • large – The larger of both values

Returns:

The mean that has been calculated

template<typename T>
static inline constexpr T iterativeArithmeticMean(uint32 n, T x, T mean)

Allows to compute the arithmetic mean of several floating point values x_1, ..., x_n in an iterative manner, which prevents overflows.

This function must be invoked for each value as follows: mean_1 = iterativeArithmeticMean(1, x_1, 0); ...; mean_n = iterativeArithmeticMean(n, x_n, mean_n-1)

Template Parameters:

T – The type of the values

Parameters:
  • n – The index of the value, starting at 1

  • x – The n-th value

  • mean – The arithmetic mean of all previously provided values

Returns:

The arithmetic mean of all values provided so far

static inline uint32 calculateBoundedFraction(uint32 n, float32 fraction, uint32 minimum, uint32 maximum)

Calculates and returns the fraction of a given integer value fraction * n, such that a certain upper and lower bound is respected.

Parameters:
  • n – The value

  • fraction – The fraction. Must be in (0, 1)

  • minimum – The minimum

  • maximum – The maximum or a value < minimum, if no upper bound should be enforced