From 006b1ca93676695a56ef413f4f34722c0816959b Mon Sep 17 00:00:00 2001 From: Inkrementator <70717315+Inkrementator@users.noreply.github.com> Date: Tue, 18 Mar 2025 00:13:55 +0100 Subject: [PATCH] Logic: Move (huge,tiny) test up, supersedes testing for 0.0 --- std/math/algebraic.d | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/std/math/algebraic.d b/std/math/algebraic.d index 1b9e15b1f..9acb7a7fe 100644 --- a/std/math/algebraic.d +++ b/std/math/algebraic.d @@ -310,7 +310,6 @@ if (isFloatingPoint!T) // If both are tiny, avoid underflow by scaling by 2^^N. import core.math : fabs, sqrt; import std.math.traits : floatTraits, RealFormat, isNaN; - import std.algorithm.comparison : max; alias F = floatTraits!T; @@ -327,12 +326,6 @@ if (isFloatingPoint!T) } assert(!(u.isNaN || v.isNaN), "Comparison to NaN always fails, thus is is always handled in the branch above"); - const maxabs = max(u,v); - if (v == 0.0) - { - return u; - } - static if (F.realFormat == RealFormat.ieeeSingle) { enum SQRTMIN = 0x1p-60f; @@ -360,6 +353,13 @@ if (isFloatingPoint!T) else assert(0, "hypot not implemented"); + if (u * T.epsilon > v) + { + // hypot (huge, tiny) = huge + // also: hypot(x, 0) = x + return u; + } + // Now u >= v, or else one is NaN. T ratio = 1.0; if (v >= SQRTMAX) @@ -379,12 +379,6 @@ if (isFloatingPoint!T) v *= SCALE_UNDERFLOW; } - if (u * T.epsilon > v) - { - // hypot (huge, tiny) = huge - return u; - } - // both are in the normal range return ratio * sqrt(u^^2 + v^^2); } @@ -427,6 +421,7 @@ if (isFloatingPoint!T) enum small = 5.016556e-20f; assert(hypot(small, 0).isClose(small)); + assert(hypot(small, float.min_normal).isClose(small)); } @safe unittest