Skip to content

Commit 4bef5fe

Browse files
authored
Merge pull request #1072 from jkalias/master
Fix SpecialFunctions.Hypotenuse when elements are double.NaN
2 parents 14e7900 + 1128d3f commit 4bef5fe

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,18 @@ public void Log1p(double x, double y)
332332
{
333333
AssertHelpers.AlmostEqualRelative(y, SpecialFunctions.Log1p(x), 14);
334334
}
335+
336+
[Test]
337+
public void HypotenuseWhenArgumentIsNanReturnesNan()
338+
{
339+
Assert.True(double.IsNaN(SpecialFunctions.Hypotenuse(double.NaN, 0)));
340+
}
341+
342+
[Test]
343+
public void L2NormOfVectorWhenElementsNanReturnsNan()
344+
{
345+
var v = LinearAlgebra.Vector<double>.Build.Dense(new[] { double.NaN, 0 });
346+
Assert.True(double.IsNaN(v.L2Norm()));
347+
}
335348
}
336349
}

src/Numerics/SpecialFunctions/Stability.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public static Complex32 Hypotenuse(Complex32 a, Complex32 b)
9191
/// <returns>Returns <code>sqrt(a<sup>2</sup> + b<sup>2</sup>)</code> without underflow/overflow.</returns>
9292
public static double Hypotenuse(double a, double b)
9393
{
94+
if (double.IsNaN(a) || double.IsNaN(b))
95+
{
96+
return double.NaN;
97+
}
98+
9499
if (Math.Abs(a) > Math.Abs(b))
95100
{
96101
double r = b/a;

0 commit comments

Comments
 (0)