mulDiv#

Fully qualified name: carb::math::mulDiv

Defined in carb/math/MulDiv.h

inline cpp::optional<double> carb::math::mulDiv(
double num,
double multiplier,
double divisor,
) noexcept#

Multiplies two 64-bit floating-point values and then divides the result by a third 64-bit floating-point value.

If the divisor is zero, carb::cpp::nullopt is returned to indicate the math error. Effectively performs:

if (divisor == 0.0)
    return cpp::nullopt;
return (num * multiplier) / divisor;

Note

This function rounds toward nearest neighbor, that is, results midway between representable values are rounded toward the nearest value with an even (0) least significant bit. This is the default for floating-point math. In order to use a different rounding mode, use std::fesetround() before calling this function, but be advised that compiler flags may be required and/or floating-point optimizations may need to be disabled.

Parameters:
  • num – The multiplicand.

  • multiplier – The multiplier, multiplied with num.

  • divisor – The divisor, used to divide the product of num X multiplier.

Returns:

A carb::cpp::optional containing the 64-bit result of the operation. If divisor is zero carb::cpp::nullopt is returned instead.