MtxVec disables floating-point exceptions by default, so NAN and INF values propagate silently through calculations instead of raising exceptions.
Default behavior:
- Division by zero → INF (not an exception)
- 0/0 → NAN (not an exception)
- sqrt(-1) → NAN (not an exception)
- NAN in any operation → NAN result (propagates)
Checking for NAN/INF on individual values:
Use the Math387 scalar checks — they work on Double, Single, TCplx, and TSCplx:
if (Math387.IsNaN(value)) { ... }
if (Math387.IsInf(value)) { ... }
if (Math387.IsInfNan(value)) { ... }
Finding and cleaning NAN/INF across a vector or matrix:
MtxVec does not expose a HasNAN / HasInf property. Instead it provides two bulk operations that both detect and act on the bad values in a single pass:
ReplaceNAN(Value)onTDenseMtxVec— replaces everyNANelement withValuein-place. Use0.0(or any sentinel) to neutralize NANs before further processing.StripNanAndInf(Src)onTVec— copiesSrcinto self, dropping every NAN / INF element and compacting the result. The count overload returns the number of valid elements kept, so you can detect the presence of bad values by checking whether the returned length is shorter thanSrc.Length.
// Replace any NANs with a safe value
a.ReplaceNAN(0.0);
// Compact and detect contamination in one call
int kept = cleaned.StripNanAndInf(a, 0, 0, a.Length);
if (kept < a.Length) {
// `a` contained NAN or INF elements
}
If all you need is a fast "does this array contain any bad value" check without modifying the data, loop the scalar IsInfNan over the underlying Values1D array — in practice StripNanAndInf into a scratch buffer and compare lengths is almost always faster and more readable.
Why FP exceptions are disabled: SSE/AVX vectorized operations cannot raise exceptions mid-stream — they set status flags instead. MtxVec aligns with this hardware behavior. If exceptions were enabled, switching between scalar and vectorized code paths would produce inconsistent behavior.
Re-enabling FP exceptions (not recommended):
// C#: FP exceptions are controlled at the OS level.
// .NET does not raise FP exceptions by default — NAN/INF propagate silently.
// This matches MtxVec behavior, so no special configuration is needed.
Predefined constants:
| Constant | Type | Description |
|---|---|---|
NAN | Double | Not-a-number |
INF | Double | Positive infinity |
NEGINF | Double | Negative infinity |
CNAN | TCplx | Complex NaN |
CINF | TCplx | Complex infinity |
Best practice: Check for NAN/INF at algorithm boundaries (input validation, final output), not at every intermediate step. Let NAN/INF propagate through calculations — if the final result is NAN, trace back to find the source.