Skip to content
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
2 changes: 1 addition & 1 deletion Decimal2D.Tests/Decimal2D.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Decimal2D/Circle2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion DecimalEx.Tests/DecimalEx.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 26 additions & 13 deletions DecimalEx.Tests/DecimalExTests/PowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -40,6 +47,12 @@ public void NegativeExponentOfZero()
{
Assert.Throws<OverflowException>(() => 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));
}
}
}
12 changes: 10 additions & 2 deletions DecimalEx/DecimalEx.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace DecimalMath
Expand Down Expand Up @@ -51,6 +50,15 @@ public static decimal Sqrt(decimal s)
return nextX;
}

/// <summary>
/// Returns a specified number raised to the power of 2 - squared.
/// </summary>
/// <param name="x">A number to be squared.</param>
public static decimal Sqr(decimal x)
{
return Pow(x, 2m);
}

/// <summary>
/// Returns a specified number raised to the specified power.
/// </summary>
Expand Down Expand Up @@ -111,7 +119,7 @@ public static decimal Pow(decimal x, decimal y)
/// </remarks>
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");

Expand Down
5 changes: 2 additions & 3 deletions DecimalEx/DecimalExTrig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;

namespace DecimalMath
{
Expand Down Expand Up @@ -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;
Expand Down