Functions & utilities
FUNCTIONSNamespace: CppUtils::Math
Public enums
Section titled “Public enums”HorizontalAlignment
Section titled “HorizontalAlignment”enum class HorizontalAlignment { Left = 0, Center = 1, Right = 2};Public types
Section titled “Public types”BigUInt
Section titled “BigUInt”using CppUtils::Math::BigUInt = typedef BasicBigInt<false>;BigInt
Section titled “BigInt”using CppUtils::Math::BigInt = typedef BasicBigInt<true>;Public functions
Section titled “Public functions”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 parameter | Description |
|---|---|
Float | A floating-point type satisfying std::floating_point. |
| Parameter | Description |
|---|---|
lhs | Left-hand operand to compare. |
rhs | Right-hand operand to compare. |
epsilon | Relative tolerance factor (defaults to machine epsilon std::numeric_limits<Float>::epsilon()). |
minimumEpsilon | Absolute 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.