Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SpecialFunctions.Hypotenuse when elements are double.NaN #1072

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,18 @@ public void Log1p(double x, double y)
{
AssertHelpers.AlmostEqualRelative(y, SpecialFunctions.Log1p(x), 14);
}

[Test]
public void HypotenuseWhenArgumentIsNanReturnesNan()
{
Assert.True(double.IsNaN(SpecialFunctions.Hypotenuse(double.NaN, 0)));
}

[Test]
public void L2NormOfVectorWhenElementsNanReturnsNan()
{
var v = LinearAlgebra.Vector<double>.Build.Dense(new[] { double.NaN, 0 });
Assert.True(double.IsNaN(v.L2Norm()));
}
}
}
5 changes: 5 additions & 0 deletions src/Numerics/SpecialFunctions/Stability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public static Complex32 Hypotenuse(Complex32 a, Complex32 b)
/// <returns>Returns <code>sqrt(a<sup>2</sup> + b<sup>2</sup>)</code> without underflow/overflow.</returns>
public static double Hypotenuse(double a, double b)
{
if (double.IsNaN(a) || double.IsNaN(b))
{
return double.NaN;
}

if (Math.Abs(a) > Math.Abs(b))
{
double r = b/a;
Expand Down