SafeMath utilities¶
client/SafeMath.h provides lightweight guarded arithmetic to prevent
floating-point exceptions (SIGFPE) in numerical code. All functions live in
eonc::safemath.
Scalar functions¶
All are inline constexpr or inline (for <cmath> calls), and marked
[[nodiscard]]. Guard branches use [[unlikely]] to hint the branch
predictor.
Function |
Signature |
Behavior |
|---|---|---|
|
|
Returns |
|
|
|
|
|
|
|
|
|
|
|
Guarded |
The epsilon (1e-300) is well below any physically meaningful value but above
the FPE trap threshold.
Eigen templates¶
Available only when Eigen/Core is already included (guarded by
#if defined(EIGEN_CORE_H) || defined(EIGEN_CORE_MODULE_H) — Eigen 5 renamed
the Core guard). Always include Eigen headers before SafeMath.h.
Function |
Signature |
Behavior |
|---|---|---|
|
|
Returns zero matrix/vector when |
|
|
Sets |
Choosing between normalize variants¶
safe_normalized(v): Returns a copy. Use for expressions liketau = safe_normalized(tau * cos(a) + theta * sin(a)).safe_normalize_inplace(v): Modifies in place. Use to replace bare.normalize()calls likedirection.normalize().Bare
.normalize()/.normalized(): Only safe when preceded by an explicit norm check or when the vector is guaranteed nonzero.
Design principles¶
Fallback values match existing control flow. Each call site chooses a fallback that triggers the same branch the code would take for degenerate input (reset, skip, clamp, etc.). Valid inputs produce bit-identical results.
No overhead on the hot path. The guard is a single
abs(x) < epscomparison with[[unlikely]], which the branch predictor will learn is almost never taken.Header-only, no link dependencies. Just
#include "SafeMath.h"after your Eigen includes (if usingsafe_normalizedorsafe_normalize_inplace).