Class NDHypotMath
Scalar kernel backing NumSharp's np.hypot binary ufunc — sqrt(x12 + x22)
computed without spurious overflow/underflow, the length of the hypotenuse of a right triangle.
Why a from-scratch algorithm instead of a library call. NumPy's hypot ufunc is
a scalar BINARY_LOOP that calls npy_hypot (npy_math_internal.h.src). On the
win-amd64 NumPy 2.4.2 wheel NPY_BLOCK_HYPOT is not defined (it is set only for MinGW
and 32-bit MSVC — npy_config.h), so npy_hypot forwards straight to the MSVC UCRT
hypot/hypotf. The BCL has no Math.Hypot, and the one hypot in the framework —
the private helper behind Abs(Complex) — is the naive
large·sqrt(1+(small/large)²) "block" form, which diverges from UCRT's hypot on ~19%
of inputs (1–2 ULP). So there is no managed call that reproduces the NumPy value, and the loop is
scalar-only either way (no SIMD hypot exists in NumPy on this platform).
Every float dtype is BIT-EXACT with NumPy. UCRT's hypot is only faithfully
rounded (it disagrees with the correctly rounded result on 8.7% of float64 inputs), because it is
the plain sqrt(x*x + y*y) — two products, a sum and a square root, each rounded — with an
exact power-of-two rescale only where x*x would overflow or lose y*y to underflow.
Probed through ucrtbase (parity QA-25): that model equals UCRT's hypot on every one
of 1.7M pairs spanning every exponent, subnormals and the corners, and np.hypot with it.
Hypot(double, double) is that model, so float64 np.hypot returns NumPy's bits —
including where they are 1 ULP from the correctly rounded value (np.hypot(0.1, 0.1) is
0x3fc21a1851ff630b). Until parity QA-25 this kernel was Borges' correctly rounded FMA
algorithm, more accurate than NumPy and excused in the oracle. The ff->f and (via float32,
astype e->f) ee->e loops round the double result down to float, which reproduces
UCRT's hypotf exactly (0 of 300,000 float32 pairs differ; a float operand's square is exact
in double, so the double sum leaves no room for a float32 rounding difference).
Decimal (no NumPy analog) computes the highest-precision result natively via Sqrt(decimal) with the overflow-safe scaled form.
public static class NDHypotMath
- Inheritance
-
NDHypotMath
- Inherited Members
Properties
SimdAvailable
Whether the Vector256 hypot fast path runs: AVX2-class x86 hosts (Vector256 acceleration; the float32 paths use AVX's float / double conversions). The FMA test is the gate the path shipped with — the UCRT core itself uses no fused multiply-add — and a host without it takes the scalar kernel, which returns the same bits.
public static bool SimdAvailable { get; }
Property Value
Methods
Hypot(double, double)
float64 np.hypot of one pair, NumPy's npy_hypot = UCRT's hypot on win-amd64:
sqrt(x² + y²) faithfully rounded the way UCRT rounds it (see the class remarks), without
spurious overflow or underflow. An infinite operand yields +inf even when the other is NaN;
otherwise the FIRST NaN operand comes back as it is (payload and sign, as UCRT returns it); the
result is otherwise non-negative.
public static double Hypot(double x, double y)
Parameters
Returns
- double
The hypotenuse, NumPy's bits.
HypotContiguousF32(float*, float*, float*, long)
public static void HypotContiguousF32(float* a, float* b, float* r, long n)
Parameters
HypotContiguousF64(double*, double*, double*, long)
public static void HypotContiguousF64(double* a, double* b, double* r, long n)
Parameters
HypotDecimal(decimal, decimal)
Decimal (no NumPy analog): the overflow-safe scaled form |x|·sqrt(1+(|y|/|x|)²) keeps
the radicand in [1,2] so only a genuine out-of-range result overflows (decimal has no infinity).
public static decimal HypotDecimal(decimal x, decimal y)
Parameters
Returns
HypotF(float, float)
hypotf (float). NumPy's ff->f loop; bit-exact with UCRT's hypotf because
rounding the double result down to float coincides with it (a float operand's square is exact in
double, so only the sum and the root round before the final narrowing: 0 of 300,000 probed pairs
differ from ucrtbase's _hypotf).
public static float HypotF(float x, float y)
Parameters
Returns
- float
The hypotenuse, NumPy's bits.
HypotHalf(Half, Half)
Half computes in float32, matching NumPy's ee->e loop (astype e->f: half→float,
hypotf, float→half). Bit-exact with NumPy.
public static Half HypotHalf(Half x, Half y)
Parameters
Returns
HypotScalarF32(float*, float, float*, long)
public static void HypotScalarF32(float* a, float scalar, float* r, long n)
Parameters
HypotScalarF64(double*, double, double*, long)
public static void HypotScalarF64(double* a, double scalar, double* r, long n)