From 8008a4a9ef79efdaa9afd1b20b43df6569e05442 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 12:44:40 +0100 Subject: [PATCH] Fix incorrect return values for ComplexSqrt --- X10D/src/DoubleExtensions/DoubleExtensions.cs | 9 +++++++++ X10D/src/SingleExtensions/SingleExtensions.cs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/X10D/src/DoubleExtensions/DoubleExtensions.cs b/X10D/src/DoubleExtensions/DoubleExtensions.cs index 0fd1898..abc949b 100644 --- a/X10D/src/DoubleExtensions/DoubleExtensions.cs +++ b/X10D/src/DoubleExtensions/DoubleExtensions.cs @@ -19,6 +19,15 @@ public static class DoubleExtensions [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static Complex ComplexSqrt(this double value) { + switch (value) + { + case double.PositiveInfinity: + case double.NegativeInfinity: + return Complex.Infinity; + case double.NaN: + return Complex.NaN; + } + double absoluteSqrt = Math.Abs(value).Sqrt(); return new Complex(absoluteSqrt, value >= 0 ? 0 : 1); } diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index 87d5c23..9a98695 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -19,6 +19,15 @@ public static class SingleExtensions [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static Complex ComplexSqrt(this float value) { + switch (value) + { + case float.PositiveInfinity: + case float.NegativeInfinity: + return Complex.Infinity; + case float.NaN: + return Complex.NaN; + } + float absoluteSqrt = MathF.Abs(value).Sqrt(); return new Complex(absoluteSqrt, value >= 0 ? 0 : 1); }