Skip to content
Welcome to the new CppUtils documentation!

Namespace: CppUtils::Math

enum class HorizontalAlignment {
Left = 0,
Center = 1,
Right = 2
};
using CppUtils::Math::BigUInt = typedef BasicBigInt<false>;
using CppUtils::Math::BigInt = typedef BasicBigInt<true>;
template <class T>
constexpr auto makeMask(std::size_t length, HorizontalAlignment alignment) noexcept -> T;
template <std::integral T>
constexpr auto toBigEndian(T value) -> T;
template <std::integral T>
constexpr auto toLittleEndian(T value) -> T;
template <std::integral Integer>
auto getRandomNumberInInterval(Integer min, Integer max) -> Integer;
constexpr auto absolute(auto number) -> decltype(auto);
template <std::floating_point Float>
constexpr auto isEqual(Float lhs, Float rhs, Float epsilon=std::numeric_limits<Float>::epsilon(), Float minimumEpsilon=std::numeric_limits<Float>::min()) noexcept -> bool;

Performs robust, magnitude-adaptive floating-point equality comparison.

Direct comparison of floating-point numbers with operator== is notoriously unreliable due to IEEE 754 binary representation errors and rounding artifacts. For instance, the expression (0.1 + 0.2) == 0.3 evaluates to false because in base-2 floating point, 0.1 + 0.2 results in 0.30000000000000004440892098500626....

Furthermore, naive approaches like std::abs(a - b) < fixed_epsilon fail for two major reasons: For large numbers, a fixed epsilon is far too tight (the gap between representable numbers exceeds the epsilon), causing false negatives.

Near zero (subnormal numbers), relative scaling collapses, causing underflow issues.

isEqual solves both problems by implementing an adaptive tolerance algorithm: |lhs - rhs| <= std::max(minimumEpsilon, epsilon * std::max(|lhs|, |rhs|))

Adaptive scaling: Scales tolerance proportionally to the operands’ magnitude (epsilon * max), maintaining accurate relative precision across small and arbitrarily large numbers.

Zero-proximity fallback: Protects comparisons near zero by falling back to minimumEpsilon (std::numeric_limits<Float>::min() by default).

Template parameterDescription
FloatA floating-point type satisfying std::floating_point.
ParameterDescription
lhsLeft-hand operand to compare.
rhsRight-hand operand to compare.
epsilonRelative tolerance factor (defaults to machine epsilon std::numeric_limits<Float>::epsilon()).
minimumEpsilonAbsolute tolerance floor for values close to zero (defaults to std::numeric_limits<Float>::min()).

Returns: true if the difference between operands falls within the adaptive tolerance, false otherwise.

template <Type::Numeric T>
constexpr auto isBetween(T value, T low, T high) noexcept -> bool;