From 769764464c62d9ef1f16768b38282e88502efa48 Mon Sep 17 00:00:00 2001 From: MaLiN2223 Date: Sun, 24 Jan 2016 13:41:41 +0100 Subject: [PATCH] Corrected typo, corrected float/complex, added more tests. --- src/Numerics/Complex32.cs | 13 +++++++++---- src/UnitTests/ComplexTests/Complex32Test.cs | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Numerics/Complex32.cs b/src/Numerics/Complex32.cs index 6ccd0de3..18097c2f 100644 --- a/src/Numerics/Complex32.cs +++ b/src/Numerics/Complex32.cs @@ -641,7 +641,8 @@ namespace MathNet.Numerics } /// Division operator. Divides a complex number by another. - /// Enchanted Smith's algorithm for dividing two complex numbers + /// Enhanced Smith's algorithm for dividing two complex numbers + /// /// The result of the division. /// The dividend. /// The divisor. @@ -694,6 +695,8 @@ namespace MathNet.Numerics } /// Division operator. Divides a float value by a complex number. + /// Algorithm based on Smith's algorithm + /// /// The result of the division. /// The dividend. /// The divisor. @@ -708,9 +711,11 @@ namespace MathNet.Numerics { return PositiveInfinity; } - - var zmod = divisor.MagnitudeSquared; - return new Complex32(dividend * divisor._real / zmod, -dividend * divisor._imag / zmod); + float c = divisor.Real; + float d = divisor.Imaginary; + if (Math.Abs(d) <= Math.Abs(c)) + return InternalDiv(dividend, 0, c, d, false); + return InternalDiv(0, dividend, d, c, true); } /// Division operator. Divides a complex number by a float value. diff --git a/src/UnitTests/ComplexTests/Complex32Test.cs b/src/UnitTests/ComplexTests/Complex32Test.cs index d381a146..355f4fe2 100644 --- a/src/UnitTests/ComplexTests/Complex32Test.cs +++ b/src/UnitTests/ComplexTests/Complex32Test.cs @@ -436,7 +436,21 @@ namespace MathNet.Numerics.UnitTests.ComplexTests Assert.AreEqual(new Complex32(-(float)Math.Pow(10, 20), (float)Math.Pow(10, -14)), first / second); } + /// + /// Can divide float/complex without overflow + /// + [Test] + public void CanDodgeOverflowDivisionFloat() + { + + var first = new Complex32((float)Math.Pow(10, 25), (float)Math.Pow(10, -25)); + float second = (float)Math.Pow(10, -37); + float third = (float)Math.Pow(10, 37); + Assert.AreEqual(new Complex32(0, 0), second / first); // it's (10^-62,10^-112) thus overflow + Assert.AreEqual(new Complex32((float)Math.Pow(10, 12), (float)Math.Pow(10, -38)), third / first); + Assert.AreEqual(new Complex32((float)Math.Pow(10, -28), -(float)Math.Pow(10, 12)), third / new Complex32((float)Math.Pow(10, -25), (float)Math.Pow(10, 25))); + } /// /// Can multiple a complex number and a double using operators. /// @@ -574,7 +588,7 @@ namespace MathNet.Numerics.UnitTests.ComplexTests { Assert.AreEqual((float)Math.Sqrt(2) * float.Epsilon, new Complex32(float.Epsilon, float.Epsilon).Magnitude); Assert.AreEqual(float.Epsilon, new Complex32(0, float.Epsilon).Magnitude); - Assert.AreEqual((float)(Math.Pow(10,30) * Math.Sqrt(2)), new Complex32((float)Math.Pow(10, 30), (float)Math.Pow(10, 30)).Magnitude); + Assert.AreEqual((float)(Math.Pow(10, 30) * Math.Sqrt(2)), new Complex32((float)Math.Pow(10, 30), (float)Math.Pow(10, 30)).Magnitude); } ///