From 974d5907cfcdb810aa0c785ae1e25fdc24ff7b57 Mon Sep 17 00:00:00 2001 From: scidec Date: Wed, 14 Apr 2021 06:56:26 -0600 Subject: [PATCH] Update tests projects so they can be run. Add sqr function as an overload of power. Remove debug assets to improve performance. --- Decimal2D.Tests/Decimal2D.Tests.csproj | 2 +- Decimal2D/Circle2D.cs | 4 +-- DecimalEx.Tests/DecimalEx.Tests.csproj | 2 +- DecimalEx.Tests/DecimalExTests/PowTests.cs | 39 ++++++++++++++-------- DecimalEx/DecimalEx.cs | 12 +++++-- DecimalEx/DecimalExTrig.cs | 5 ++- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Decimal2D.Tests/Decimal2D.Tests.csproj b/Decimal2D.Tests/Decimal2D.Tests.csproj index 2249c0f..0d79a4a 100644 --- a/Decimal2D.Tests/Decimal2D.Tests.csproj +++ b/Decimal2D.Tests/Decimal2D.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.1 diff --git a/Decimal2D/Circle2D.cs b/Decimal2D/Circle2D.cs index 81d4b15..8d5db57 100644 --- a/Decimal2D/Circle2D.cs +++ b/Decimal2D/Circle2D.cs @@ -592,8 +592,8 @@ public LineSeg2D TangentAt(decimal degrees, decimal length, bool clockwise) decimal rad = DecimalEx.ToRad(degrees + r.AngleA); LineSeg2D ret = default(LineSeg2D); - Debug.Assert(r.AngleA == RightTriangle.GetAngleFromSides(length, _radius)); - Debug.Assert(r.Hypotenuse == RightTriangle.GetHypFromSides(length, _radius)); + //Debug.Assert(r.AngleA == RightTriangle.GetAngleFromSides(length, _radius)); + //Debug.Assert(r.Hypotenuse == RightTriangle.GetHypFromSides(length, _radius)); ret.Pt1 = PointAt(degrees); if (!clockwise) diff --git a/DecimalEx.Tests/DecimalEx.Tests.csproj b/DecimalEx.Tests/DecimalEx.Tests.csproj index 9ba40dd..deb8d7a 100644 --- a/DecimalEx.Tests/DecimalEx.Tests.csproj +++ b/DecimalEx.Tests/DecimalEx.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.1 diff --git a/DecimalEx.Tests/DecimalExTests/PowTests.cs b/DecimalEx.Tests/DecimalExTests/PowTests.cs index 033f042..6d53e6b 100644 --- a/DecimalEx.Tests/DecimalExTests/PowTests.cs +++ b/DecimalEx.Tests/DecimalExTests/PowTests.cs @@ -12,19 +12,26 @@ public class PowTests public static decimal[][] TestCases = { - new[] {1m, 0m, 1m, 0m}, - new[] {2m, 8m, 256m, 0m}, - new[] {3m, 5m, 243m, 0m}, - new[] {5m, 40m, 9094947017729282379150390625m, 0m}, - new[] {99m, 1m, 99m, 0m}, + new[] {1m, 0m, 1m, 0m}, + new[] {2m, 8m, 256m, 0m}, + new[] {3m, 5m, 243m, 0m}, + new[] {5m, 40m, 9094947017729282379150390625m, 0m}, + new[] {99m, 1m, 99m, 0m}, - // Fractional powers are going to be off a bit - new[] {5m, .5m, 2.2360679774997896964091736687m, FractionalTolerance}, - new[] {5m, 2.5m, 55.901699437494742410229341718m, FractionalTolerance}, - new[] {5m, 10.5m, 21836601.342771383753995836609m, FractionalTolerance}, - new[] {5m, 15.5m, 68239379196.160574231236989402m, FractionalTolerance}, - new[] {5m, 20.5m, 213248059988001.79447261559188m, FractionalTolerance}, - new[] {5m, 40.5m, 20336919783401660392056998432m, FractionalTolerance}, + // Fractional powers are going to be off a bit + new[] {5m, .5m, 2.2360679774997896964091736687m, FractionalTolerance}, + new[] {5m, 2.5m, 55.901699437494742410229341718m, FractionalTolerance}, + new[] {5m, 10.5m, 21836601.342771383753995836609m, FractionalTolerance}, + new[] {5m, 15.5m, 68239379196.160574231236989402m, FractionalTolerance}, + new[] {5m, 20.5m, 213248059988001.79447261559188m, FractionalTolerance}, + new[] {5m, 40.5m, 20336919783401660392056998432m, FractionalTolerance}, + }; + + public static decimal[][] SqrTestCases = + { + new[] {1m, 1m}, + new[] {8m, 64m}, + new[] {3m, 9m}, }; [TestCaseSource("TestCases")] @@ -40,6 +47,12 @@ public void NegativeExponentOfZero() { Assert.Throws(() => DecimalEx.Pow(0, -1)); } - } + [TestCaseSource("SqrTestCases")] + public void SqrSameAsPower2(decimal x, decimal expected) + { + Assert.That(DecimalEx.Pow(x, 2), Is.EqualTo(DecimalEx.Sqr(x))); + Assert.That(DecimalEx.Pow(x, 2), Is.EqualTo(expected)); + } + } } \ No newline at end of file diff --git a/DecimalEx/DecimalEx.cs b/DecimalEx/DecimalEx.cs index 9adaa65..933b1ce 100644 --- a/DecimalEx/DecimalEx.cs +++ b/DecimalEx/DecimalEx.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Linq; namespace DecimalMath @@ -51,6 +50,15 @@ public static decimal Sqrt(decimal s) return nextX; } + /// + /// Returns a specified number raised to the power of 2 - squared. + /// + /// A number to be squared. + public static decimal Sqr(decimal x) + { + return Pow(x, 2m); + } + /// /// Returns a specified number raised to the specified power. /// @@ -111,7 +119,7 @@ public static decimal Pow(decimal x, decimal y) /// private static decimal ExpBySquaring(decimal x, decimal y) { - Debug.Assert(y >= 0 && decimal.Truncate(y) == y, "Only non-negative, integer powers supported."); + //Debug.Assert(y >= 0 && decimal.Truncate(y) == y, "Only non-negative, integer powers supported."); if (y < 0) throw new ArgumentOutOfRangeException("y", "Negative exponents not supported!"); if (decimal.Truncate(y) != y) throw new ArgumentException("Exponent must be an integer!", "y"); diff --git a/DecimalEx/DecimalExTrig.cs b/DecimalEx/DecimalExTrig.cs index 44919ea..265e49f 100644 --- a/DecimalEx/DecimalExTrig.cs +++ b/DecimalEx/DecimalExTrig.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; namespace DecimalMath { @@ -118,8 +117,8 @@ public static decimal Sin(decimal x) nextAdd *= -1 * xSquared / (doubleIteration * doubleIteration + doubleIteration); } - Debug.WriteLine("{0:000}:{1,33:+0.0000000000000000000000000000;-0.0000000000000000000000000000} ->{2,33:+0.0000000000000000000000000000;-0.0000000000000000000000000000}", - doubleIteration / 2, nextAdd, result + nextAdd); + //Debug.WriteLine("{0:000}:{1,33:+0.0000000000000000000000000000;-0.0000000000000000000000000000} ->{2,33:+0.0000000000000000000000000000;-0.0000000000000000000000000000}", + // doubleIteration / 2, nextAdd, result + nextAdd); if (nextAdd == 0) break; result += nextAdd;