diff --git a/BigDecimal/BigDecimal.Trigonometry.cs b/BigDecimal/BigDecimal.Trigonometry.cs
index 5ed3ce3..ffcc315 100644
--- a/BigDecimal/BigDecimal.Trigonometry.cs
+++ b/BigDecimal/BigDecimal.Trigonometry.cs
@@ -465,7 +465,7 @@ public static BigDecimal Arcsin(BigDecimal radians)
/// Arbitrary precision inverse sine function.
/// An angle, measured in radians, in the domain of -1 < x < 1
/// The desired precision in terms of the number of digits to the right of the decimal.
- /// The domain of is -1 < x < 1
+ /// The domain of is -1 < x < 1
/// The inverse sine of , in radians.
/// Argument outside the domain of -1 < x < 1.
public static BigDecimal Arcsin(BigDecimal radians, int precision)
diff --git a/BigDecimal/BigDecimal.csproj b/BigDecimal/BigDecimal.csproj
index 1911da3..fed83f0 100644
--- a/BigDecimal/BigDecimal.csproj
+++ b/BigDecimal/BigDecimal.csproj
@@ -1,6 +1,6 @@
- net45;net46;net472;net48;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0
+ netstandard2.0;netstandard2.1;net8.0;net9.0
AnyCPU
Library
annotations
@@ -82,13 +82,6 @@
-
-
- all
- runtime; build; native; contentfiles; analyzers; buildtransitive
-
-
-
True
diff --git a/TestBigDecimal/TestBigDecimal.csproj b/TestBigDecimal/TestBigDecimal.csproj
index c59540d..134921c 100644
--- a/TestBigDecimal/TestBigDecimal.csproj
+++ b/TestBigDecimal/TestBigDecimal.csproj
@@ -1,6 +1,6 @@
- net45;net46;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0
+ net9.0
AnyCPU
latest
latest
@@ -46,10 +46,10 @@
-
+
-
-
+
+
diff --git a/TestBigDecimal/TestBigDecimalConversion.cs b/TestBigDecimal/TestBigDecimalConversion.cs
index f0e4355..dcb112d 100644
--- a/TestBigDecimal/TestBigDecimalConversion.cs
+++ b/TestBigDecimal/TestBigDecimalConversion.cs
@@ -1,30 +1,27 @@
-using System;
-using System.Numerics;
+using System.Numerics;
using ExtendedNumerics;
using NUnit.Framework;
+using NUnit.Framework.Legacy;
-namespace TestBigDecimal
+namespace BigDecimalTests;
+
+[Parallelizable(ParallelScope.All)]
+[TestFixture]
+public class TestBigDecimalConversion
{
+ private const string LongNumbers =
+ "122685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853" +
+ "222685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853" +
+ "322685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853";
- [Parallelizable(ParallelScope.All)]
- [TestFixture]
- [Culture("en-US,ru-RU")]
- public class TestBigDecimalConversion
+ [Test]
+ public void TestConversionFromBigInteger()
{
- private const String LongNumbers =
- "122685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853" +
- "222685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853" +
- "322685077023934456384565345644576454645163485274775618673867785678763896936969078786987890789798927897383149150201282920942551781108927727789384397020382853";
-
- [Test]
- public void TestConversionFromBigInteger()
- {
- var expected = BigInteger.Parse(LongNumbers);
+ var expected = BigInteger.Parse(LongNumbers);
- var bigDecimal = BigDecimal.Parse(LongNumbers);
- var actual = bigDecimal.WholeValue;
+ var bigDecimal = BigDecimal.Parse(LongNumbers);
+ var actual = bigDecimal.WholeValue;
- Assert.AreEqual(expected, actual);
- }
+ ClassicAssert.AreEqual(expected, actual);
}
}
\ No newline at end of file
diff --git a/TestBigDecimal/TestBigDecimalCritical.cs b/TestBigDecimal/TestBigDecimalCritical.cs
index 326aa04..0d57753 100644
--- a/TestBigDecimal/TestBigDecimalCritical.cs
+++ b/TestBigDecimal/TestBigDecimalCritical.cs
@@ -1,468 +1,463 @@
-using System;
-using System.Globalization;
+using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Threading;
using ExtendedNumerics;
using NUnit.Framework;
+using NUnit.Framework.Legacy;
-namespace TestBigDecimal
+namespace BigDecimalTests;
+
+[NonParallelizable]
+[TestFixture]
+public class TestBigDecimalCritical
{
+ private NumberFormatInfo Format => Thread.CurrentThread.CurrentCulture.NumberFormat;
- [NonParallelizable]
- [TestFixture]
- [Culture("en-US,ru-RU")]
- public class TestBigDecimalCritical
+ [Test]
+ public void Test47()
{
+ var π1 = 1 * BigDecimal.π;
+ var π2 = 2 * BigDecimal.π;
+ var π4 = 4 * BigDecimal.π;
+ var π8 = 8 * BigDecimal.π;
+ var sum = π1 + π2 + π4 + π8;
+ var actual = sum.WholeValue;
+ var expected = (BigInteger)47;
+
+ ClassicAssert.AreEqual(expected, actual);
+ }
- private NumberFormatInfo Format { get { return Thread.CurrentThread.CurrentCulture.NumberFormat; } }
+ [Test]
+ public void TestConstructor0()
+ {
+ BigDecimal expected = 0;
+ var actual = new BigDecimal(0);
- [Test]
- public void Test47()
- {
- var π1 = 1 * BigDecimal.π;
- var π2 = 2 * BigDecimal.π;
- var π4 = 4 * BigDecimal.π;
- var π8 = 8 * BigDecimal.π;
- var sum = π1 + π2 + π4 + π8;
- var actual = sum.WholeValue;
- var expected = (BigInteger)47;
-
- Assert.AreEqual(expected, actual);
- }
+ ClassicAssert.AreEqual(expected, actual);
+ ClassicAssert.AreEqual("0", actual.ToString());
+ }
- [Test]
- public void TestConstructor0()
- {
- BigDecimal expected = 0;
- var actual = new BigDecimal(0);
+ [Test]
+ public void TestConstructor1()
+ {
+ BigDecimal expected = 0;
+ var actual = new BigDecimal(0, 0);
- Assert.AreEqual(expected, actual);
- Assert.AreEqual("0", actual.ToString());
- }
+ ClassicAssert.AreEqual(expected, actual);
+ ClassicAssert.AreEqual("0", actual.ToString());
+ }
- [Test]
- public void TestConstructor1()
- {
- BigDecimal expected = 0;
- var actual = new BigDecimal(0, 0);
+ [Test]
+ public void TestConstructor2()
+ {
+ BigDecimal expected = 0;
+ var actual = new BigDecimal(0, 1);
- Assert.AreEqual(expected, actual);
- Assert.AreEqual("0", actual.ToString());
- }
+ ClassicAssert.AreEqual(expected, actual);
+ ClassicAssert.AreEqual("0", actual.ToString());
+ }
- [Test]
- public void TestConstructor2()
- {
- BigDecimal expected = 0;
- var actual = new BigDecimal(0, 1);
+ [Test]
+ public void TestConstructor001D()
+ {
+ var val1 = TestBigDecimalHelper.PrepareValue("0.5", Format);
+ var val2 = TestBigDecimalHelper.PrepareValue("0.5", Format);
- Assert.AreEqual(expected, actual);
- Assert.AreEqual("0", actual.ToString());
- }
+ var i = BigDecimal.Parse(val1);
+ var j = BigDecimal.Parse(val2);
- [Test]
- public void TestConstructor001D()
- {
- var val1 = TestBigDecimalHelper.PrepareValue("0.5", this.Format);
- var val2 = TestBigDecimalHelper.PrepareValue("0.5", this.Format);
+ ClassicAssert.AreEqual(val1, i.ToString());
+ ClassicAssert.AreEqual(val2, j.ToString());
+ }
- var i = BigDecimal.Parse(val1);
- var j = BigDecimal.Parse(val2);
+ [Test]
+ public void TestConstructor_Decimal001()
+ {
+ string expected = "79193.074346525013163981460149";
+ decimal d = 79193.074346525013163981460149m;
+ BigDecimal bd = new(d);
+ string actual = bd.ToString();
+
+ TestContext.WriteLine($"string : {expected}");
+ TestContext.WriteLine($"decimal : {d}");
+ TestContext.WriteLine($"ToString(\"G17\"): {d:G17}");
+ TestContext.WriteLine($"BigDecimal : {bd}");
+ TestContext.WriteLine();
+ ClassicAssert.AreEqual(expected, actual, $"{expected} != {actual}");
+ }
- Assert.AreEqual(val1, i.ToString());
- Assert.AreEqual(val2, j.ToString());
- }
+ [Test]
+ public void TestConstructor_Float()
+ {
+ string expected1 = "0.3486328";
+ float d1 = 0.3486328125f;
+ TestContext.WriteLine($"R: \"{expected1}\" decimal: {d1:R}");
+ TestContext.WriteLine($"E: \"{expected1}\" decimal: {d1:E}");
+ TestContext.WriteLine($"G9: \"{expected1}\" decimal: {d1:G9}");
+ TestContext.WriteLine($"G10: \"{expected1}\" decimal: {d1:G10}");
+ TestContext.WriteLine($"F9: \"{expected1}\" decimal: {d1:F9}");
+ var actual1 = new BigDecimal(d1);
+
+ ClassicAssert.AreEqual(expected1, actual1.ToString());
+ TestContext.WriteLine($"{expected1} == {actual1}");
+ }
- [Test]
- public void TestConstructor_Decimal001()
+ [Test]
+ public void TestConstructor_Double()
+ {
+ static void TestConstructor_Double(string expected, double value)
{
- string expected = "79193.074346525013163981460149";
- decimal d = 79193.074346525013163981460149m;
- BigDecimal bd = new BigDecimal(d);
- string actual = bd.ToString();
-
- TestContext.WriteLine($"string : {expected}");
- TestContext.WriteLine($"decimal : {d}");
- TestContext.WriteLine($"ToString(\"G17\"): {d.ToString("G17")}");
- TestContext.WriteLine($"BigDecimal : {bd}");
- TestContext.WriteLine();
- Assert.AreEqual(expected, actual, $"{expected} != {actual}");
+ var actual = new BigDecimal(value);
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ TestContext.WriteLine($"{expected} == {actual}");
}
- [Test]
- public void TestConstructor_Float()
- {
- string expected1 = "0.3486328";
- float d1 = 0.3486328125f;
- TestContext.WriteLine($"R: \"{expected1}\" decimal: {d1.ToString("R")}");
- TestContext.WriteLine($"E: \"{expected1}\" decimal: {d1.ToString("E")}");
- TestContext.WriteLine($"G9: \"{expected1}\" decimal: {d1.ToString("G9")}");
- TestContext.WriteLine($"G10: \"{expected1}\" decimal: {d1.ToString("G10")}");
- TestContext.WriteLine($"F9: \"{expected1}\" decimal: {d1.ToString("F9")}");
- var actual1 = new BigDecimal(d1);
-
- Assert.AreEqual(expected1, actual1.ToString());
- TestContext.WriteLine($"{expected1} == {actual1}");
- }
+ //TestConstructor_Double("0", 0);
+ //TestConstructor_Double("0", 0.0);
+ ////TestConstructor_Double("0", -0.0);
+ //
+ //TestConstructor_Double("7976931348623157", 7976931348623157);
+ //TestConstructor_Double("-7976931348623157", -7976931348623157);
+ //
+ //TestConstructor_Double("1000000000000000", 1000000000000000);
+ //TestConstructor_Double("-1000000000000000", -1000000000000000);
- [Test]
- public void TestConstructor_Double()
- {
- //TestConstructor_Double("0", 0);
- //TestConstructor_Double("0", 0.0);
- ////TestConstructor_Double("0", -0.0);
- //
- //TestConstructor_Double("7976931348623157", 7976931348623157);
- //TestConstructor_Double("-7976931348623157", -7976931348623157);
- //
- //TestConstructor_Double("1000000000000000", 1000000000000000);
- //TestConstructor_Double("-1000000000000000", -1000000000000000);
-
- TestConstructor_Double("1.7976931348623157", 1.7976931348623157);
- TestConstructor_Double("-1.7976931348623157", -1.7976931348623157);
-
- TestConstructor_Double("0.0000000008623157", 0.0000000008623157);
- TestConstructor_Double("-0.0000000000623157", -0.0000000000623157);
-
- TestConstructor_Double("0.0101010101010101", 0.0101010101010101);
- TestConstructor_Double("-0.0101010101010101", -0.0101010101010101);
- }
+ TestConstructor_Double("1.7976931348623157", 1.7976931348623157);
+ TestConstructor_Double("-1.7976931348623157", -1.7976931348623157);
- private void TestConstructor_Double(string expected, Double value)
- {
- var actual = new BigDecimal(value);
- Assert.AreEqual(expected, actual.ToString());
- TestContext.WriteLine($"{expected} == {actual}");
- }
+ TestConstructor_Double("0.0000000008623157", 0.0000000008623157);
+ TestConstructor_Double("-0.0000000000623157", -0.0000000000623157);
- [Test]
- public void TestConstructor001WriteLineA()
- {
- var expected1 = TestBigDecimalHelper.PrepareValue("3.141592793238462", this.Format);
- var expected2 = TestBigDecimalHelper.PrepareValue("3.141592793238462", this.Format);
- var π = (BigDecimal)3.141592793238462m;
- var d = new BigDecimal(BigInteger.Parse("3141592793238462"), -15);
- var actual1 = π.ToString();
- var actual2 = d.ToString();
-
- TestContext.WriteLine("π = " + actual1);
- TestContext.WriteLine("d = " + actual2);
- Assert.AreEqual(expected1, actual1);
- Assert.AreEqual(expected2, actual2);
- }
+ TestConstructor_Double("0.0101010101010101", 0.0101010101010101);
+ TestConstructor_Double("-0.0101010101010101", -0.0101010101010101);
+ }
- [Test]
- public void TestCastingDecimal()
- {
- Decimal m = 0.0000000000000001m;
+ [Test]
+ public void TestConstructor001WriteLineA()
+ {
+ var expected1 = TestBigDecimalHelper.PrepareValue("3.141592793238462", Format);
+ var expected2 = TestBigDecimalHelper.PrepareValue("3.141592793238462", Format);
+ var π = (BigDecimal)3.141592793238462m;
+ var d = new BigDecimal(BigInteger.Parse("3141592793238462"), -15);
+ var actual1 = π.ToString();
+ var actual2 = d.ToString();
+
+ TestContext.WriteLine("π = " + actual1);
+ TestContext.WriteLine("d = " + actual2);
+ ClassicAssert.AreEqual(expected1, actual1);
+ ClassicAssert.AreEqual(expected2, actual2);
+ }
- var e = new BigDecimal(1000000000, -25);
- var h = (BigDecimal)m;
+ [Test]
+ public void TestCastingDecimal()
+ {
+ decimal m = 0.0000000000000001m;
- TestContext.WriteLine("m = " + m);
- TestContext.WriteLine("e = " + e);
- TestContext.WriteLine("h = " + h);
+ var e = new BigDecimal(1000000000, -25);
+ var h = (BigDecimal)m;
- Assert.AreEqual(h.ToString(), e.ToString());
- }
+ TestContext.WriteLine("m = " + m);
+ TestContext.WriteLine("e = " + e);
+ TestContext.WriteLine("h = " + h);
- [Test]
- public void TestCastingDouble()
- {
- Double m = 0.0000000000000001d;
+ ClassicAssert.AreEqual(h.ToString(), e.ToString());
+ }
- var e = new BigDecimal(1000000000, -25);
- var h = (BigDecimal)m;
+ [Test]
+ public void TestCastingDouble()
+ {
+ double m = 0.0000000000000001d;
- TestContext.WriteLine("m = " + m);
- TestContext.WriteLine("e = " + e);
- TestContext.WriteLine("h = " + h);
+ var e = new BigDecimal(1000000000, -25);
+ var h = (BigDecimal)m;
- Assert.AreEqual(h.ToString(), e.ToString());
- }
+ TestContext.WriteLine("m = " + m);
+ TestContext.WriteLine("e = " + e);
+ TestContext.WriteLine("h = " + h);
- [Test]
- public void TestConstructor002()
- {
- var f = new BigDecimal(-3, -2);
- var expected = TestBigDecimalHelper.PrepareValue("-0.03", this.Format);
- Assert.AreEqual(expected, f.ToString());
- }
+ ClassicAssert.AreEqual(h.ToString(), e.ToString());
+ }
- [Test]
- public void TestConstructor003()
- {
- var g = new BigDecimal(0, -1);
- Assert.AreEqual("0", g.ToString());
- }
+ [Test]
+ public void TestConstructor002()
+ {
+ var f = new BigDecimal(-3, -2);
+ var expected = TestBigDecimalHelper.PrepareValue("-0.03", Format);
+ ClassicAssert.AreEqual(expected, f.ToString());
+ }
- [Test]
- public void TestConstructor004()
- {
- var value = TestBigDecimalHelper.PrepareValue("-0.0012345", this.Format);
+ [Test]
+ public void TestConstructor003()
+ {
+ var g = new BigDecimal(0, -1);
+ ClassicAssert.AreEqual("0", g.ToString());
+ }
- var h = BigDecimal.Parse(value);
- Assert.AreEqual(value, h.ToString());
- }
+ [Test]
+ public void TestConstructor004()
+ {
+ var value = TestBigDecimalHelper.PrepareValue("-0.0012345", Format);
- [Test]
- public void TestConstructorToString123456789()
- {
- const Int32 numbers = 123456789;
- var expected = numbers.ToString();
- var actual = new BigDecimal(numbers).ToString();
+ var h = BigDecimal.Parse(value);
+ ClassicAssert.AreEqual(value, h.ToString());
+ }
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestConstructorToString123456789()
+ {
+ const int numbers = 123456789;
+ var expected = numbers.ToString();
+ var actual = new BigDecimal(numbers).ToString();
- [Test]
- public void TestNormalizeB()
- {
- var expected = "1000000";
- BigDecimal bigDecimal = new BigDecimal(1000000000, -3);
+ ClassicAssert.AreEqual(expected, actual);
+ }
- var actual = bigDecimal.ToString();
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestNormalizeB()
+ {
+ var expected = "1000000";
+ BigDecimal bigDecimal = new(1000000000, -3);
- [Test]
- public void TestParse001()
- {
- string expected = TestBigDecimalHelper.PrepareValue("0.00501", this.Format);
- var result = BigDecimal.Parse(expected);
- var actual = result.ToString();
+ var actual = bigDecimal.ToString();
+ ClassicAssert.AreEqual(expected, actual);
+ }
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestParse001()
+ {
+ string expected = TestBigDecimalHelper.PrepareValue("0.00501", Format);
+ var result = BigDecimal.Parse(expected);
+ var actual = result.ToString();
- [Test]
- public void TestParse002()
- {
- var result1 = BigDecimal.Parse("");
- Assert.AreEqual(result1, BigDecimal.Zero);
+ ClassicAssert.AreEqual(expected, actual);
+ }
- var result2 = BigDecimal.Parse("0");
- Assert.AreEqual(result2, BigDecimal.Zero);
+ [Test]
+ public void TestParse002()
+ {
+ var result1 = BigDecimal.Parse("");
+ ClassicAssert.AreEqual(result1, BigDecimal.Zero);
- var result3 = BigDecimal.Parse("-0");
- Assert.AreEqual(result3, BigDecimal.Zero);
- }
+ var result2 = BigDecimal.Parse("0");
+ ClassicAssert.AreEqual(result2, BigDecimal.Zero);
- [Test]
- public void TestParse0031()
- {
- string expected = "-123456789";
- var actual = BigDecimal.Parse(expected).ToString();
+ var result3 = BigDecimal.Parse("-0");
+ ClassicAssert.AreEqual(result3, BigDecimal.Zero);
+ }
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestParse0031()
+ {
+ string expected = "-123456789";
+ var actual = BigDecimal.Parse(expected).ToString();
- [Test]
- public void TestParse0032()
- {
- string expected = "123456789";
- var bigDecimal = BigDecimal.Parse(expected);
- var actual = bigDecimal.ToString();
- Assert.AreEqual(expected, actual);
- }
+ ClassicAssert.AreEqual(expected, actual);
+ }
- [Test]
- public void TestParse0033()
- {
- string expected = TestBigDecimalHelper.PrepareValue("1234.56789", this.Format);
- var actual = BigDecimal.Parse(expected).ToString();
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestParse0032()
+ {
+ string expected = "123456789";
+ var bigDecimal = BigDecimal.Parse(expected);
+ var actual = bigDecimal.ToString();
+ ClassicAssert.AreEqual(expected, actual);
+ }
- [Test]
- public void TestParseScientific()
- {
- string toParse = TestBigDecimalHelper.PrepareValue("123.123E-20", this.Format);
- string expected = TestBigDecimalHelper.PrepareValue("0.00000000000000000123123", this.Format);
- BigDecimal parsed = BigDecimal.Parse(toParse);
- string actual = parsed.ToString();
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestParse0033()
+ {
+ string expected = TestBigDecimalHelper.PrepareValue("1234.56789", Format);
+ var actual = BigDecimal.Parse(expected).ToString();
+ ClassicAssert.AreEqual(expected, actual);
+ }
- [Test]
- public void TestParseNegativeScientific()
- {
- string toParse = TestBigDecimalHelper.PrepareValue("-123.123E-20", this.Format);
- string expected = TestBigDecimalHelper.PrepareValue("-0.00000000000000000123123", this.Format);
- BigDecimal parsed = BigDecimal.Parse(toParse);
- string actual = parsed.ToString();
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestParseScientific()
+ {
+ string toParse = TestBigDecimalHelper.PrepareValue("123.123E-20", Format);
+ string expected = TestBigDecimalHelper.PrepareValue("0.00000000000000000123123", Format);
+ BigDecimal parsed = BigDecimal.Parse(toParse);
+ string actual = parsed.ToString();
+ ClassicAssert.AreEqual(expected, actual);
+ }
- [Test]
- public void TestParseEpsilon()
- {
- string expected = TestBigDecimalHelper.PrepareValue("4.9406564584124654E-324", this.Format);
- BigDecimal actual = BigDecimal.Parse(expected);
+ [Test]
+ public void TestParseNegativeScientific()
+ {
+ string toParse = TestBigDecimalHelper.PrepareValue("-123.123E-20", Format);
+ string expected = TestBigDecimalHelper.PrepareValue("-0.00000000000000000123123", Format);
+ BigDecimal parsed = BigDecimal.Parse(toParse);
+ string actual = parsed.ToString();
+ ClassicAssert.AreEqual(expected, actual);
+ }
- Assert.AreEqual(expected, BigDecimal.ToScientificENotation(actual));
- }
+ [Test]
+ public void TestParseEpsilon()
+ {
+ string expected = TestBigDecimalHelper.PrepareValue("4.9406564584124654E-324", Format);
+ BigDecimal actual = BigDecimal.Parse(expected);
+
+ ClassicAssert.AreEqual(expected, BigDecimal.ToScientificENotation(actual));
+ }
+
+ [Test]
+ public void TestParseLarge()
+ {
+ string expected = TestBigDecimalHelper.PrepareValue("4.9406564584124654E+324", Format);
+ BigDecimal actual = BigDecimal.Parse(expected);
+
+ ClassicAssert.AreEqual(expected, BigDecimal.ToScientificENotation(actual));
+ }
+
+
+ [Test]
+ public void TestParseRoundTrip()
+ {
+ double dval = 0.6822871999174d;
+ var val = TestBigDecimalHelper.PrepareValue("0.6822871999174", Format);
+ var actual = BigDecimal.Parse(val);
+ var expected = (BigDecimal)dval;
+ ClassicAssert.AreEqual(expected, actual);
+ }
- [Test]
- public void TestParseLarge()
+ [Test]
+ [NonParallelizable]
+ public void TestAlwaysTruncate()
+ {
+ var savePrecision = BigDecimal.Precision;
+ var expected1 = TestBigDecimalHelper.PrepareValue("3.1415926535", Format);
+ var expected2 = TestBigDecimalHelper.PrepareValue("-3.1415926535", Format);
+ var expected3 = TestBigDecimalHelper.PrepareValue("-0.0000031415", Format);
+ var expected4 = "-3";
+
+ var actual1 = "";
+ var actual2 = "";
+ var actual3 = "";
+ var actual4 = "";
+
+ try
{
- string expected = TestBigDecimalHelper.PrepareValue("4.9406564584124654E+324", this.Format);
- BigDecimal actual = BigDecimal.Parse(expected);
+ BigDecimal.Precision = 10;
+ BigDecimal.AlwaysTruncate = true;
- Assert.AreEqual(expected, BigDecimal.ToScientificENotation(actual));
- }
+ var val1 = TestBigDecimalHelper.PrepareValue("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", Format);
+ BigDecimal parsed1 = BigDecimal.Parse(val1);
+
+ var val2 = TestBigDecimalHelper.PrepareValue("-3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", Format);
+ BigDecimal parsed2 = BigDecimal.Parse(val2);
+ var val3 = TestBigDecimalHelper.PrepareValue("-0.00000314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", Format);
+ BigDecimal parsed3 = BigDecimal.Parse(val3);
- [Test]
- public void TestParseRoundTrip()
+ BigDecimal parsed4 = BigDecimal.Parse("-3");
+
+ actual1 = parsed1.ToString();
+ actual2 = parsed2.ToString();
+ actual3 = parsed3.ToString();
+ actual4 = parsed4.ToString();
+ }
+ finally
{
- double dval = 0.6822871999174d;
- var val = TestBigDecimalHelper.PrepareValue("0.6822871999174", this.Format);
- var actual = BigDecimal.Parse(val);
- var expected = (BigDecimal)dval;
- Assert.AreEqual(expected, actual);
+ BigDecimal.Precision = savePrecision;
+ BigDecimal.AlwaysTruncate = false;
}
- [Test]
- [NonParallelizable]
- public void TestAlwaysTruncate()
+ ClassicAssert.AreEqual(expected1, actual1, "#: 1");
+ ClassicAssert.AreEqual(expected2, actual2, "#: 2");
+ ClassicAssert.AreEqual(expected3, actual3, "#: 3");
+ ClassicAssert.AreEqual(expected4, actual4, "#: 4");
+ }
+
+ [Test]
+ [NonParallelizable]
+ public void TestTruncateOnAllArithmeticOperations()
+ {
+ var savePrecision = BigDecimal.Precision;
+
+ BigDecimal mod1 = BigDecimal.Parse("3141592653589793238462643383279502");
+ BigDecimal mod2 = BigDecimal.Parse("27182818284590452");
+ //BigDecimal neg1 = BigDecimal.Parse("-3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647");
+
+ var val1 = TestBigDecimalHelper.PrepareValue("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647", Format);
+ BigDecimal lrg1 = BigDecimal.Parse(val1);
+
+ var val2 = TestBigDecimalHelper.PrepareValue("2.718281828459045235360287471352662497757247093699959574966967", Format);
+ BigDecimal lrg2 = BigDecimal.Parse(val2);
+
+ var expected1 = TestBigDecimalHelper.PrepareValue("5.859874482", Format);
+ var expected2 = TestBigDecimalHelper.PrepareValue("0.423310825", Format);
+ var expected3 = TestBigDecimalHelper.PrepareValue("8.539734222", Format);
+ var expected4 = TestBigDecimalHelper.PrepareValue("0.865255979", Format);
+ var expected5 = TestBigDecimalHelper.PrepareValue("9.869604401", Format);
+ var expected6 = TestBigDecimalHelper.PrepareValue("148.4131591", Format);
+ var expected7 = "80030773195";
+ var expected8 = TestBigDecimalHelper.PrepareValue("-3.14159265", Format);
+ var expected9 = "3";
+ var expected10 = "4";
+ var expected11 = TestBigDecimalHelper.PrepareValue("3.141592653", Format);
+
+ var actual1 = "";
+ var actual2 = "";
+ var actual3 = "";
+ var actual4 = "";
+ var actual5 = "";
+ var actual6 = "";
+ var actual7 = "";
+ var actual8 = "";
+ var actual9 = "";
+ var actual10 = "";
+ var actual11 = "";
+
+ try
{
- var savePrecision = BigDecimal.Precision;
- var expected1 = TestBigDecimalHelper.PrepareValue("3.1415926535", this.Format);
- var expected2 = TestBigDecimalHelper.PrepareValue("-3.1415926535", this.Format);
- var expected3 = TestBigDecimalHelper.PrepareValue("-0.0000031415", this.Format);
- var expected4 = "-3";
-
- var actual1 = "";
- var actual2 = "";
- var actual3 = "";
- var actual4 = "";
-
- try
- {
- BigDecimal.Precision = 10;
- BigDecimal.AlwaysTruncate = true;
-
- var val1 = TestBigDecimalHelper.PrepareValue("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", this.Format);
- BigDecimal parsed1 = BigDecimal.Parse(val1);
-
- var val2 = TestBigDecimalHelper.PrepareValue("-3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", this.Format);
- BigDecimal parsed2 = BigDecimal.Parse(val2);
-
- var val3 = TestBigDecimalHelper.PrepareValue("-0.00000314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535", this.Format);
- BigDecimal parsed3 = BigDecimal.Parse(val3);
-
- BigDecimal parsed4 = BigDecimal.Parse("-3");
-
- actual1 = parsed1.ToString();
- actual2 = parsed2.ToString();
- actual3 = parsed3.ToString();
- actual4 = parsed4.ToString();
- }
- finally
- {
- BigDecimal.Precision = savePrecision;
- BigDecimal.AlwaysTruncate = false;
- }
-
- Assert.AreEqual(expected1, actual1, "#: 1");
- Assert.AreEqual(expected2, actual2, "#: 2");
- Assert.AreEqual(expected3, actual3, "#: 3");
- Assert.AreEqual(expected4, actual4, "#: 4");
+ BigDecimal.Precision = 50;
+ BigDecimal.AlwaysTruncate = false;
+
+ TestContext.WriteLine($"E = {BigDecimal.E}");
+ TestContext.WriteLine($"{new BigDecimal(lrg1.Mantissa, lrg1.Exponent)}");
+ TestContext.WriteLine($"{new BigDecimal(lrg2.Mantissa, lrg2.Exponent)}");
+
+ BigDecimal result1 = BigDecimal.Add(lrg1, lrg2);
+ BigDecimal result2 = BigDecimal.Subtract(lrg1, lrg2);
+ BigDecimal result3 = BigDecimal.Multiply(lrg1, lrg2);
+ BigDecimal result4 = BigDecimal.Divide(lrg2, lrg1);
+ BigDecimal result5 = BigDecimal.Pow(lrg1, 2);
+ BigDecimal result6 = BigDecimal.Exp(new BigInteger(5));
+ BigDecimal result7 = BigDecimal.Mod(mod1, mod2);
+ BigDecimal result8 = BigDecimal.Negate(lrg1);
+ BigDecimal result9 = BigDecimal.Floor(lrg1);
+ BigDecimal result10 = BigDecimal.Ceiling(lrg1);
+ BigDecimal result11 = BigDecimal.Abs(lrg1);
+
+ actual1 = new string(result1.ToString().Take(11).ToArray());
+ actual2 = new string(result2.ToString().Take(11).ToArray());
+ actual3 = new string(result3.ToString().Take(11).ToArray());
+ actual4 = new string(result4.ToString().Take(11).ToArray());
+ actual5 = new string(result5.ToString().Take(11).ToArray());
+ actual6 = new string(result6.ToString().Take(11).ToArray());
+ actual7 = new string(result7.ToString().Take(11).ToArray());
+ actual8 = new string(result8.ToString().Take(11).ToArray());
+ actual9 = new string(result9.ToString().Take(11).ToArray());
+ actual10 = new string(result10.ToString().Take(11).ToArray());
+ actual11 = new string(result11.ToString().Take(11).ToArray());
}
-
- [Test]
- [NonParallelizable]
- public void TestTruncateOnAllArithmeticOperations()
+ finally
{
- var savePrecision = BigDecimal.Precision;
-
- BigDecimal mod1 = BigDecimal.Parse("3141592653589793238462643383279502");
- BigDecimal mod2 = BigDecimal.Parse("27182818284590452");
- //BigDecimal neg1 = BigDecimal.Parse("-3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647");
-
- var val1 = TestBigDecimalHelper.PrepareValue("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647", this.Format);
- BigDecimal lrg1 = BigDecimal.Parse(val1);
-
- var val2 = TestBigDecimalHelper.PrepareValue("2.718281828459045235360287471352662497757247093699959574966967", this.Format);
- BigDecimal lrg2 = BigDecimal.Parse(val2);
-
- var expected1 = TestBigDecimalHelper.PrepareValue("5.859874482", this.Format);
- var expected2 = TestBigDecimalHelper.PrepareValue("0.423310825", this.Format);
- var expected3 = TestBigDecimalHelper.PrepareValue("8.539734222", this.Format);
- var expected4 = TestBigDecimalHelper.PrepareValue("0.865255979", this.Format);
- var expected5 = TestBigDecimalHelper.PrepareValue("9.869604401", this.Format);
- var expected6 = TestBigDecimalHelper.PrepareValue("148.4131591", this.Format);
- var expected7 = "80030773195";
- var expected8 = TestBigDecimalHelper.PrepareValue("-3.14159265", this.Format);
- var expected9 = "3";
- var expected10 = "4";
- var expected11 = TestBigDecimalHelper.PrepareValue("3.141592653", this.Format);
-
- var actual1 = "";
- var actual2 = "";
- var actual3 = "";
- var actual4 = "";
- var actual5 = "";
- var actual6 = "";
- var actual7 = "";
- var actual8 = "";
- var actual9 = "";
- var actual10 = "";
- var actual11 = "";
-
- try
- {
- BigDecimal.Precision = 50;
- BigDecimal.AlwaysTruncate = false;
-
- TestContext.WriteLine($"E = {BigDecimal.E}");
- TestContext.WriteLine($"{new BigDecimal(lrg1.Mantissa, lrg1.Exponent)}");
- TestContext.WriteLine($"{new BigDecimal(lrg2.Mantissa, lrg2.Exponent)}");
-
- BigDecimal result1 = BigDecimal.Add(lrg1, lrg2);
- BigDecimal result2 = BigDecimal.Subtract(lrg1, lrg2);
- BigDecimal result3 = BigDecimal.Multiply(lrg1, lrg2);
- BigDecimal result4 = BigDecimal.Divide(lrg2, lrg1);
- BigDecimal result5 = BigDecimal.Pow(lrg1, 2);
- BigDecimal result6 = BigDecimal.Exp(new BigInteger(5));
- BigDecimal result7 = BigDecimal.Mod(mod1, mod2);
- BigDecimal result8 = BigDecimal.Negate(lrg1);
- BigDecimal result9 = BigDecimal.Floor(lrg1);
- BigDecimal result10 = BigDecimal.Ceiling(lrg1);
- BigDecimal result11 = BigDecimal.Abs(lrg1);
-
- actual1 = new string(result1.ToString().Take(11).ToArray());
- actual2 = new string(result2.ToString().Take(11).ToArray());
- actual3 = new string(result3.ToString().Take(11).ToArray());
- actual4 = new string(result4.ToString().Take(11).ToArray());
- actual5 = new string(result5.ToString().Take(11).ToArray());
- actual6 = new string(result6.ToString().Take(11).ToArray());
- actual7 = new string(result7.ToString().Take(11).ToArray());
- actual8 = new string(result8.ToString().Take(11).ToArray());
- actual9 = new string(result9.ToString().Take(11).ToArray());
- actual10 = new string(result10.ToString().Take(11).ToArray());
- actual11 = new string(result11.ToString().Take(11).ToArray());
- }
- finally
- {
- BigDecimal.Precision = savePrecision;
- BigDecimal.AlwaysTruncate = false;
- }
-
- Assert.AreEqual(expected1, actual1, $"Test Truncate On All Arithmetic Operations - #1: ");
- Assert.AreEqual(expected2, actual2, $"Test Truncate On All Arithmetic Operations - #2: ");
- Assert.AreEqual(expected3, actual3, $"Test Truncate On All Arithmetic Operations - #3: ");
- Assert.AreEqual(expected4, actual4, $"Test Truncate On All Arithmetic Operations - #4: ");
- Assert.AreEqual(expected5, actual5, $"Test Truncate On All Arithmetic Operations - #5: ");
- StringAssert.StartsWith(expected6, actual6, $"Test Truncate On All Arithmetic Operations - #6: ");
- Assert.AreEqual(expected7, actual7, $"Test Truncate On All Arithmetic Operations - #7: ");
- Assert.AreEqual(expected8, actual8, $"Test Truncate On All Arithmetic Operations - #8: ");
- Assert.AreEqual(expected9, actual9, $"Test Truncate On All Arithmetic Operations - #9: ");
- Assert.AreEqual(expected10, actual10, $"Test Truncate On All Arithmetic Operations - #10: ");
- Assert.AreEqual(expected11, actual11, $"Test Truncate On All Arithmetic Operations - #11: ");
+ BigDecimal.Precision = savePrecision;
+ BigDecimal.AlwaysTruncate = false;
}
- }
+ ClassicAssert.AreEqual(expected1, actual1, $"Test Truncate On All Arithmetic Operations - #1: ");
+ ClassicAssert.AreEqual(expected2, actual2, $"Test Truncate On All Arithmetic Operations - #2: ");
+ ClassicAssert.AreEqual(expected3, actual3, $"Test Truncate On All Arithmetic Operations - #3: ");
+ ClassicAssert.AreEqual(expected4, actual4, $"Test Truncate On All Arithmetic Operations - #4: ");
+ ClassicAssert.AreEqual(expected5, actual5, $"Test Truncate On All Arithmetic Operations - #5: ");
+ StringAssert.StartsWith(expected6, actual6, $"Test Truncate On All Arithmetic Operations - #6: ");
+ ClassicAssert.AreEqual(expected7, actual7, $"Test Truncate On All Arithmetic Operations - #7: ");
+ ClassicAssert.AreEqual(expected8, actual8, $"Test Truncate On All Arithmetic Operations - #8: ");
+ ClassicAssert.AreEqual(expected9, actual9, $"Test Truncate On All Arithmetic Operations - #9: ");
+ ClassicAssert.AreEqual(expected10, actual10, $"Test Truncate On All Arithmetic Operations - #10: ");
+ ClassicAssert.AreEqual(expected11, actual11, $"Test Truncate On All Arithmetic Operations - #11: ");
+ }
}
\ No newline at end of file
diff --git a/TestBigDecimal/TestBigDecimalFunctions.cs b/TestBigDecimal/TestBigDecimalFunctions.cs
index 7ddf2ba..604a282 100644
--- a/TestBigDecimal/TestBigDecimalFunctions.cs
+++ b/TestBigDecimal/TestBigDecimalFunctions.cs
@@ -7,507 +7,505 @@
using ExtendedNumerics;
using ExtendedNumerics.Helpers;
using NUnit.Framework;
+using NUnit.Framework.Legacy;
-namespace TestBigDecimal
+namespace BigDecimalTests;
+
+[TestFixture]
+public class TestBigDecimalFunctions
{
+ private NumberFormatInfo Format => Thread.CurrentThread.CurrentCulture.NumberFormat;
+
+ [Test]
+ public void TestGCD()
+ {
+ var expected = BigDecimal.Parse("10");
+
+ BigDecimal result = new BigInteger[] {
+ 20, 30, 210, 310, 360, 5040, 720720
+ }.GCD();
+
+ ClassicAssert.AreEqual(expected, result);
+ }
+
+ [Test]
+ public void TestMin()
+ {
+ string expected = "30303";
+
+ var left = BigDecimal.Parse(expected);
+ var right1 = BigDecimal.Parse(expected);
+
+ var val = TestBigDecimalHelper.PrepareValue("30303.5", Format);
+ var right2 = BigDecimal.Parse(val);
+ var right3 = BigDecimal.Parse("30304");
+
+ var actual1 = BigDecimal.Min(left, right1);
+ var actual2 = BigDecimal.Min(left, right2);
+ var actual3 = BigDecimal.Min(left, right3);
+
+ ClassicAssert.AreEqual(expected, actual1.ToString());
+ ClassicAssert.AreEqual(expected, actual2.ToString());
+ ClassicAssert.AreEqual(expected, actual3.ToString());
+ }
+
+ [Test]
+ public void TestMax()
+ {
+ var expected = TestBigDecimalHelper.PrepareValue("30304.1", Format);
+
+ var left1 = BigDecimal.Parse("30304");
+
+ var val2 = TestBigDecimalHelper.PrepareValue("-30304.2", Format);
+ var left2 = BigDecimal.Parse(val2);
+
+ var val3 = TestBigDecimalHelper.PrepareValue("30304.01", Format);
+ var left3 = BigDecimal.Parse(val3);
+ var right = BigDecimal.Parse(expected);
+
+ var actual1 = BigDecimal.Max(left1, right);
+ var actual2 = BigDecimal.Max(left2, right);
+ var actual3 = BigDecimal.Max(left3, right);
+
+ ClassicAssert.AreEqual(expected, actual1.ToString());
+ ClassicAssert.AreEqual(expected, actual2.ToString());
+ ClassicAssert.AreEqual(expected, actual3.ToString());
+ }
- [TestFixture]
- [Culture("en-US,ru-RU")]
- public class TestBigDecimalFunctions
+ [Test]
+ public void TestGetLength()
{
- private NumberFormatInfo Format { get { return Thread.CurrentThread.CurrentCulture.NumberFormat; } }
+ var expected = BigDecimal.Parse("2268507702394854741827137539360680923314");
+ var value = new BigDecimal(BigInteger.Parse("22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
- [Test]
- public void TestGCD()
- {
- var expected = BigDecimal.Parse("10");
+ ClassicAssert.AreEqual(expected.Length, value.Length);
+ }
- BigDecimal result = new BigInteger[] {
- 20, 30, 210, 310, 360, 5040, 720720
- }.GCD();
+ [Test]
+ public void TestGetSign()
+ {
+ BigDecimal zero1 = 0;
+ ClassicAssert.AreEqual(0, zero1.Sign, "0");
- Assert.AreEqual(expected, result);
- }
+ var zero2 = BigDecimal.Zero;
+ ClassicAssert.AreEqual(0, zero2.Sign, "new BigDecimal()");
- [Test]
- public void TestMin()
- {
- string expected = "30303";
+ var zero3 = new BigDecimal(0);
+ ClassicAssert.AreEqual(0, zero3.Sign, "new BigDecimal(0);");
- var left = BigDecimal.Parse(expected);
- var right1 = BigDecimal.Parse(expected);
+ var zero4 = new BigDecimal(BigInteger.Zero);
+ ClassicAssert.AreEqual(0, zero4.Sign, "new BigDecimal(BigInteger.Zero)");
- var val = TestBigDecimalHelper.PrepareValue("30303.5", this.Format);
- var right2 = BigDecimal.Parse(val);
- var right3 = BigDecimal.Parse("30304");
+ var zero5 = new BigDecimal(0, -1);
+ ClassicAssert.AreEqual(0, zero5.Sign, "new BigDecimal(0, -1);");
- var actual1 = BigDecimal.Min(left, right1);
- var actual2 = BigDecimal.Min(left, right2);
- var actual3 = BigDecimal.Min(left, right3);
+ BigDecimal zero6 = BigInteger.Subtract(BigInteger.Add(BigInteger.Divide(2, 3), BigInteger.Multiply(-1, BigInteger.Divide(1, 3))), BigInteger.Divide(1, 3));
+ ClassicAssert.AreEqual(0, zero6.Sign, "2/3 -1/3 - 1/3");
- Assert.AreEqual(expected, actual1.ToString());
- Assert.AreEqual(expected, actual2.ToString());
- Assert.AreEqual(expected, actual3.ToString());
- }
+ var oneTenth = BigDecimal.Divide(BigDecimal.One, new BigDecimal(10));
+ BigDecimal pointZeroOne = 0.1m;
+ var zero7 = BigDecimal.Subtract(oneTenth, pointZeroOne);
+ ClassicAssert.AreEqual(0, zero7.Sign, "1/10 - 1/10");
- [Test]
- public void TestMax()
- {
- var expected = TestBigDecimalHelper.PrepareValue("30304.1", this.Format);
+ var zero8 = BigDecimal.Add(new BigDecimal(1, -1), -1m / 10m);
+ ClassicAssert.AreEqual(0, zero8.Sign, "1 + -1/10");
- var left1 = BigDecimal.Parse("30304");
+ var zero9 = new BigDecimal(15274, -7) * 0;
+ ClassicAssert.AreEqual(0, zero9.Sign, "0.0015274");
- var val2 = TestBigDecimalHelper.PrepareValue("-30304.2", this.Format);
- var left2 = BigDecimal.Parse(val2);
+ BigDecimal positive1 = 1;
+ ClassicAssert.AreEqual(1, positive1.Sign, "1");
- var val3 = TestBigDecimalHelper.PrepareValue("30304.01", this.Format);
- var left3 = BigDecimal.Parse(val3);
- var right = BigDecimal.Parse(expected);
+ BigDecimal positive2 = -1 * -1;
+ ClassicAssert.AreEqual(1, positive2.Sign, "-1 * 1");
- var actual1 = BigDecimal.Max(left1, right);
- var actual2 = BigDecimal.Max(left2, right);
- var actual3 = BigDecimal.Max(left3, right);
+ var negative1 = BigDecimal.Multiply(BigDecimal.One, BigDecimal.MinusOne);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, negative1.Sign, "1 * -1");
- Assert.AreEqual(expected, actual1.ToString());
- Assert.AreEqual(expected, actual2.ToString());
- Assert.AreEqual(expected, actual3.ToString());
- }
+ var negative2 = BigDecimal.Subtract(BigDecimal.Zero, 3);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, negative2.Sign, "0 - 3");
- [Test]
- public void TestGetLength()
- {
- var expected = BigDecimal.Parse("2268507702394854741827137539360680923314");
- var value = new BigDecimal(BigInteger.Parse("22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
+ BigDecimal negative3 = BigInteger.Subtract(0, 3);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, negative3.Sign, "BigInteger.Subtract(0, 3)");
- Assert.AreEqual(expected.Length, value.Length);
- }
+ BigDecimal negative4 = 10 * -1;
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, negative4.Sign, "10 * -1;");
- [Test]
- public void TestGetSign()
- {
- BigDecimal zero1 = 0;
- Assert.AreEqual(0, zero1.Sign, "0");
+ string largeNegativeString = "-22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853";
+ BigDecimal largeNegative = BigDecimal.Parse(largeNegativeString);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, largeNegative.Sign, largeNegativeString);
- var zero2 = BigDecimal.Zero;
- Assert.AreEqual(0, zero2.Sign, "new BigDecimal()");
+ string highPrecisionNegativeString = TestBigDecimalHelper.PrepareValue("-2.2685077023948547418271375393606809233149150201282920942551781108927727789384397020382853", Format);
+ BigDecimal highPrecisionNegative = BigDecimal.Parse(highPrecisionNegativeString);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, highPrecisionNegative.Sign, highPrecisionNegativeString);
- var zero3 = new BigDecimal(0);
- Assert.AreEqual(0, zero3.Sign, "new BigDecimal(0);");
+ string smallNegativeString = TestBigDecimalHelper.PrepareValue("-0.000000000000000000000000000022680000000000000000000000000000150201282920942551781108927727789384397020382853", Format);
+ BigDecimal smallNegative = BigDecimal.Parse(smallNegativeString);
+ ClassicAssert.AreEqual(BigInteger.MinusOne.Sign, smallNegative.Sign, smallNegativeString);
+ }
- var zero4 = new BigDecimal(BigInteger.Zero);
- Assert.AreEqual(0, zero4.Sign, "new BigDecimal(BigInteger.Zero)");
+ [Test]
+ public void TestGoldenIrrational()
+ {
+ var val = TestBigDecimalHelper.PrepareValue("1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492", Format);
+ var goldenRatio = BigDecimal.Parse(val);
- var zero5 = new BigDecimal(0, -1);
- Assert.AreEqual(0, zero5.Sign, "new BigDecimal(0, -1);");
+ TestContext.Write("GoldenRatio: ");
+ TestContext.WriteLine(goldenRatio.ToString());
+ }
- BigDecimal zero6 = BigInteger.Subtract(BigInteger.Add(BigInteger.Divide(2, 3), BigInteger.Multiply(-1, BigInteger.Divide(1, 3))), BigInteger.Divide(1, 3));
- Assert.AreEqual(0, zero6.Sign, "2/3 -1/3 - 1/3");
+ [Test]
+ public void TestLeastCommonDivisor()
+ {
+ var expected = "45319990731015";
- var oneTenth = BigDecimal.Divide(BigDecimal.One, new BigDecimal(10));
- BigDecimal pointZeroOne = 0.1m;
- var zero7 = BigDecimal.Subtract(oneTenth, pointZeroOne);
- Assert.AreEqual(0, zero7.Sign, "1/10 - 1/10");
+ BigDecimal actual = BigIntegerHelper.LCM(new BigInteger[] {
+ 3, 5, 7, 11, 13, 101, 307, 311, 313
+ });
+
+ // 15015, lcm(3, 5, 7, 11, 13, 101, 307, 311, 313) = 45319990731015 lcm(4973, 4292, 4978, 4968, 4297, 4287) = 2822891742340306560
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ public void TestRounding()
+ {
+ var up = new BigDecimal(0.50001d);
+ var down = new BigDecimal(0.49m);
- var zero8 = BigDecimal.Add(new BigDecimal(1, -1), -1m / 10m);
- Assert.AreEqual(0, zero8.Sign, "1 + -1/10");
+ var val = TestBigDecimalHelper.PrepareValue("1.5", Format);
+ var oneAndAhalf = BigDecimal.Parse(val);
- var zero9 = new BigDecimal(15274, -7) * 0;
- Assert.AreEqual(0, zero9.Sign, "0.0015274");
+ var negEightPointFive = new BigDecimal(-8.5d);
+ BigDecimal negNinePointFive = -9.5d;
- BigDecimal positive1 = 1;
- Assert.AreEqual(1, positive1.Sign, "1");
+ var threePointFourNine = new BigDecimal(3.49d);
+ var threePointFiveOne = new BigDecimal(3.51m);
+ var sixPointFive = new BigDecimal(6.5);
- BigDecimal positive2 = -1 * -1;
- Assert.AreEqual(1, positive2.Sign, "-1 * 1");
+ var zero = BigDecimal.Round(down);
+ ClassicAssert.AreEqual(BigInteger.Zero, zero);
- var negative1 = BigDecimal.Multiply(BigDecimal.One, BigDecimal.MinusOne);
- Assert.AreEqual(BigInteger.MinusOne.Sign, negative1.Sign, "1 * -1");
+ var one = BigDecimal.Round(up);
+ ClassicAssert.AreEqual(BigInteger.One, one);
+
+ var two = BigDecimal.Round(oneAndAhalf);
+ ClassicAssert.AreEqual(one + one, two);
+
+ var three = BigDecimal.Round(threePointFourNine);
+ ClassicAssert.AreEqual(one + two, three);
+
+ var four = BigDecimal.Round(threePointFiveOne);
+ ClassicAssert.AreEqual(one + three, four);
+ ClassicAssert.AreEqual(two + two, four);
+
+ var six = BigDecimal.Round(sixPointFive, MidpointRounding.ToEven);
+ ClassicAssert.AreEqual(two + four, six);
+
+ var negEight = BigDecimal.Round(negEightPointFive, MidpointRounding.ToEven);
+ ClassicAssert.AreEqual(-four + -four, negEight);
+
+ var negNine = BigDecimal.Round(negEightPointFive, MidpointRounding.AwayFromZero);
+ ClassicAssert.AreEqual(-four + -four - one, negNine);
+
+ var negTen = BigDecimal.Round(negNinePointFive, MidpointRounding.ToEven);
+ ClassicAssert.AreEqual(-four + -four - two, negTen);
+ }
+
+ [Test]
+ public void TestRounding_NegativePrecision001()
+ {
+ BigDecimal a = BigDecimal.Parse("31415.92654");
+
+ var result6 = BigDecimal.Round(a, 6).ToString();
+ var result5 = BigDecimal.Round(a, 5).ToString();
+ var result4 = BigDecimal.Round(a, 4).ToString();
+ var result3 = BigDecimal.Round(a, 3).ToString();
+ var result2 = BigDecimal.Round(a, 2).ToString();
+ var result1 = BigDecimal.Round(a, 1).ToString();
+ var result0 = BigDecimal.Round(a, 0).ToString();
+ var result_1 = BigDecimal.Round(a, -1).ToString();
+ var result_2 = BigDecimal.Round(a, -2).ToString();
+ var result_3 = BigDecimal.Round(a, -3).ToString();
+ var result_4 = BigDecimal.Round(a, -4).ToString();
+ var result_5 = BigDecimal.Round(a, -5).ToString();
+ var result_6 = BigDecimal.Round(a, -6).ToString();
+ var result_7 = BigDecimal.Round(a, -7).ToString();
+
+ TestContext.WriteLine($"{result6}");
+ TestContext.WriteLine($"{result5}");
+ TestContext.WriteLine($"{result4}");
+ TestContext.WriteLine($"{result3}");
+ TestContext.WriteLine($"{result2}");
+ TestContext.WriteLine($"{result1}");
+ TestContext.WriteLine($"{result0}");
+ TestContext.WriteLine($"{result_1}");
+ TestContext.WriteLine($"{result_2}");
+ TestContext.WriteLine($"{result_3}");
+ TestContext.WriteLine($"{result_4}");
+ TestContext.WriteLine($"{result_5}");
+ TestContext.WriteLine($"{result_6}");
+ TestContext.WriteLine($"{result_7}");
+
+ ClassicAssert.AreEqual("31415.92654", result6);
+ ClassicAssert.AreEqual("31415.92654", result5);
+ ClassicAssert.AreEqual("31415.9265", result4);
+ ClassicAssert.AreEqual("31415.926", result3);
+ ClassicAssert.AreEqual("31415.92", result2);
+ ClassicAssert.AreEqual("31415.9", result1);
+ ClassicAssert.AreEqual("31415", result0);
+ ClassicAssert.AreEqual("31410", result_1);
+ ClassicAssert.AreEqual("31400", result_2);
+ ClassicAssert.AreEqual("31000", result_3);
+ ClassicAssert.AreEqual("30000", result_4);
+ ClassicAssert.AreEqual("0", result_5);
+ ClassicAssert.AreEqual("0", result_6);
+ ClassicAssert.AreEqual("0", result_7);
+ }
+
+ [Test]
+ public void TestRoundingWithPrecision()
+ {
+ Test(7.54m, 1, RoundingStrategy.AwayFromZero, "7.5");
+ Test(7.56m, 1, RoundingStrategy.AwayFromZero, "7.6");
+ Test(7.55m, 1, RoundingStrategy.AwayFromZero, "7.6");
+
+ Test(-7.54m, 1, RoundingStrategy.AwayFromZero, "-7.5");
+ Test(-7.56m, 1, RoundingStrategy.AwayFromZero, "-7.6");
+ Test(-7.55m, 1, RoundingStrategy.AwayFromZero, "-7.6");
+
+ Test(7.54m, 1, RoundingStrategy.TowardZero, "7.5");
+ Test(7.56m, 1, RoundingStrategy.TowardZero, "7.6");
+ Test(7.55m, 1, RoundingStrategy.TowardZero, "7.5");
+
+ Test(-7.54m, 1, RoundingStrategy.TowardZero, "-7.5");
+ Test(-7.56m, 1, RoundingStrategy.TowardZero, "-7.6");
+ Test(-7.55m, 1, RoundingStrategy.TowardZero, "-7.5");
+
+ Test(-7.551m, 2, RoundingStrategy.AwayFromZero, "-7.55");
+ Test(-7.559m, 2, RoundingStrategy.AwayFromZero, "-7.56");
+ Test(-7.555m, 2, RoundingStrategy.AwayFromZero, "-7.56");
+ }
+
+ private static void Test(decimal value, int precision, RoundingStrategy roundingStrategy, string expected)
+ {
+ BigDecimal actual = BigDecimal.Round(value, precision, roundingStrategy);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"Value: {value}\tRounding: {Enum.GetName(roundingStrategy)}\tExpected: {expected}\tActual: {actual}");
+ }
+
+ [Test]
+ public void TestGetWholeValue()
+ {
+ var value1 = new BigDecimal(BigInteger.Parse("22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
+ BigDecimal result1 = value1.WholeValue;
+ var value2 = new BigDecimal(BigInteger.Parse("-22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
+ BigDecimal result2 = value2.WholeValue;
+
+ var value3 = new BigDecimal(3, -1);
+ BigDecimal result3 = value3.WholeValue;
+ var value4 = new BigDecimal(-3, -1);
+ BigDecimal result4 = value4.WholeValue;
+
+ var expected1 = "2268507702394854741827137539360680923314";
+ var actual1 = result1.ToString();
+
+ var expected2 = "-2268507702394854741827137539360680923314";
+ var actual2 = result2.ToString();
+
+ var expected3 = "0";
+ var actual3 = result3.ToString();
+
+ var expected4 = "0";
+ var actual4 = result4.ToString();
+
+ ClassicAssert.AreEqual(expected1, actual1);
+ ClassicAssert.AreEqual(expected2, actual2);
+ ClassicAssert.AreEqual(expected3, actual3);
+ ClassicAssert.AreEqual(expected4, actual4);
+ }
+
+ [Test]
+ public void TestGetFractionalPart()
+ {
+ BigDecimal parsed1 = new(BigInteger.Parse("31415926535"), -10);
+ BigDecimal parsed2 = new(BigInteger.Parse("-31415926535"), -10);
+ BigDecimal parsed3 = new(BigInteger.Parse("31415926535"), -15);
+ BigDecimal parsed4 = new(BigInteger.Parse("-31415926535"), -15);
+ BigDecimal parsed5 = new(3, 0);
+ BigDecimal parsed6 = new(-3, 0);
+ BigDecimal parsed7 = new(3, -1);
+ BigDecimal parsed8 = new(-3, -1);
+
+ var expected1 = TestBigDecimalHelper.PrepareValue("0.1415926535", Format);
+ var expected2 = TestBigDecimalHelper.PrepareValue("0.1415926535", Format);
+ var expected3 = TestBigDecimalHelper.PrepareValue("0.000031415926535", Format);
+ var expected4 = TestBigDecimalHelper.PrepareValue("0.000031415926535", Format);
+ var expected5 = "0";
+ var expected6 = "0";
+ var expected7 = TestBigDecimalHelper.PrepareValue("0.3", Format);
+ var expected8 = TestBigDecimalHelper.PrepareValue("0.3", Format);
+
+ var result1 = parsed1.GetFractionalPart();
+ var result2 = parsed2.GetFractionalPart();
+ var result3 = parsed3.GetFractionalPart();
+ var result4 = parsed4.GetFractionalPart();
+ var result5 = parsed5.GetFractionalPart();
+ var result6 = parsed6.GetFractionalPart();
+ var result7 = parsed7.GetFractionalPart();
+ var result8 = parsed8.GetFractionalPart();
+
+ var actual1 = result1.ToString();
+ var actual2 = result2.ToString();
+ var actual3 = result3.ToString();
+ var actual4 = result4.ToString();
+ var actual5 = result5.ToString();
+ var actual6 = result6.ToString();
+ var actual7 = result7.ToString();
+ var actual8 = result8.ToString();
+
+ ClassicAssert.AreEqual(expected1, actual1, $"GetFractionalPart - #1: {parsed1}");
+ ClassicAssert.AreEqual(expected2, actual2, $"GetFractionalPart - #2: {parsed2}");
+ ClassicAssert.AreEqual(expected3, actual3, $"GetFractionalPart - #3: {parsed3}");
+ ClassicAssert.AreEqual(expected4, actual4, $"GetFractionalPart - #4: {parsed4}");
+ ClassicAssert.AreEqual(expected5, actual5, $"GetFractionalPart - #5: {parsed5}");
+ ClassicAssert.AreEqual(expected6, actual6, $"GetFractionalPart - #6: {parsed6}");
+ ClassicAssert.AreEqual(expected7, actual7, $"GetFractionalPart - #7: {parsed7}");
+ ClassicAssert.AreEqual(expected8, actual8, $"GetFractionalPart - #8: {parsed8}");
+ }
+
+ [Test]
+ public void TestSignifigantDigits()
+ {
+ const int expected1 = 19;
+ var number1 = new BigDecimal(12345678901234567890, -10);
+ var result1 = number1.SignificantDigits;
+ ClassicAssert.AreEqual(expected1, result1);
+
+ const int expected2 = 9;
+ var number2 = new BigDecimal(123456789, 1);
+ var result2 = number2.SignificantDigits;
+ ClassicAssert.AreEqual(expected2, result2);
+
+ const int expected3 = 19;
+ var number3 = new BigDecimal(BigInteger.Parse("-12345678901234567890"), -10);
+ var result3 = number3.SignificantDigits;
+ ClassicAssert.AreEqual(expected3, result3);
+
+ const int expected4 = 9;
+ var number4 = new BigDecimal(-123456789, 1);
+ var result4 = number4.SignificantDigits;
+ ClassicAssert.AreEqual(expected4, result4);
+ }
+
+ [Test(TestOf = typeof(BigDecimal))]
+ public void TestPlacesRightOfDecimal()
+ {
+ MethodInfo placesRightOfDecimal = typeof(BigDecimal).GetMethod("PlacesRightOfDecimal", BindingFlags.NonPublic | BindingFlags.Static);
+
+ BigDecimal parsed1 = new(BigInteger.Parse("314159265358979323846"), -20);
+ BigDecimal parsed2 = new(BigInteger.Parse("-314159265358979323846"), -20);
+ BigDecimal parsed3 = new(BigInteger.Parse("31415926535897932384600000"), -25);
+ BigDecimal parsed4 = new(BigInteger.Parse("314159265358979323846"), -25);
+ BigDecimal parsed5 = new(BigInteger.Parse("-314159265358979323846"), -25);
+ BigDecimal parsed6 = new(BigInteger.Parse("31415926535897932384600000"), -30);
+ BigDecimal parsed7 = new(3, 0);
+ BigDecimal parsed8 = new(-3, 0);
+ BigDecimal parsed9 = new(3, -1);
+ BigDecimal parsed0 = new(-3, -1);
+ BigDecimal parsed11 = new(BigInteger.Parse("10000000000000031400000"), -25);
+
+ var expected1 = "20";
+ var expected2 = "20";
+ var expected3 = "20";
+ var expected4 = "25";
+ var expected5 = "25";
+ var expected6 = "25";
+ var expected7 = "0";
+ var expected8 = "0";
+ var expected9 = "1";
+ var expected0 = "1";
+ var expected11 = "20";
+
+ var result1 = placesRightOfDecimal.Invoke(obj: null, [parsed1]);
+ var result2 = placesRightOfDecimal.Invoke(obj: null, [parsed2]);
+ var result3 = placesRightOfDecimal.Invoke(obj: null, [parsed3]);
+ var result4 = placesRightOfDecimal.Invoke(obj: null, [parsed4]);
+ var result5 = placesRightOfDecimal.Invoke(obj: null, [parsed5]);
+ var result6 = placesRightOfDecimal.Invoke(obj: null, [parsed6]);
+ var result7 = placesRightOfDecimal.Invoke(obj: null, [parsed7]);
+ var result8 = placesRightOfDecimal.Invoke(obj: null, [parsed8]);
+ var result9 = placesRightOfDecimal.Invoke(obj: null, [parsed9]);
+ var result0 = placesRightOfDecimal.Invoke(obj: null, [parsed0]);
+ var result11 = placesRightOfDecimal.Invoke(obj: null, [parsed11]);
+
+ var actual1 = result1.ToString();
+ var actual2 = result2.ToString();
+ var actual3 = result3.ToString();
+ var actual4 = result4.ToString();
+ var actual5 = result5.ToString();
+ var actual6 = result6.ToString();
+ var actual7 = result7.ToString();
+ var actual8 = result8.ToString();
+ var actual9 = result9.ToString();
+ var actual0 = result0.ToString();
+ var actual11 = result11.ToString();
+
+ ClassicAssert.AreEqual(expected1, actual1, $"PlacesRightOfDecimal - #1: {parsed1}");
+ ClassicAssert.AreEqual(expected2, actual2, $"PlacesRightOfDecimal - #2: {parsed2}");
+ ClassicAssert.AreEqual(expected3, actual3, $"PlacesRightOfDecimal - #3: {parsed3}");
+ ClassicAssert.AreEqual(expected4, actual4, $"PlacesRightOfDecimal - #4: {parsed4}");
+ ClassicAssert.AreEqual(expected5, actual5, $"PlacesRightOfDecimal - #5: {parsed5}");
+ ClassicAssert.AreEqual(expected6, actual6, $"PlacesRightOfDecimal - #6: {parsed6}");
+ ClassicAssert.AreEqual(expected7, actual7, $"PlacesRightOfDecimal - #7: {parsed7}");
+ ClassicAssert.AreEqual(expected8, actual8, $"PlacesRightOfDecimal - #8: {parsed8}");
+ ClassicAssert.AreEqual(expected9, actual9, $"PlacesRightOfDecimal - #9: {parsed9}");
+ ClassicAssert.AreEqual(expected0, actual0, $"PlacesRightOfDecimal - #10: {parsed0}");
+ ClassicAssert.AreEqual(expected11, actual11, $"PlacesRightOfDecimal - #11: {parsed11}");
+ }
+
+ [Test]
+ public void TestApproximateE()
+ {
+ BigDecimal.Precision = 1000;
+ //BigDecimal.AlwaysTruncate = true;
+
+ int precision = 1000;
+ // This value comes from a different application; precise calculator
+ string expected = "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035";
+
+ Stopwatch timer = Stopwatch.StartNew();
+
+ string actual = BigDecimal.Round(BigDecimal.ApproximateE(precision), precision).ToString();
+
+ TimeSpan timeElapsed = timer.Elapsed;
+ TestContext.WriteLine($"Time Elapsed (ms): {timeElapsed.TotalMilliseconds}");
+
+ actual = actual[..expected.Length];
+
+ ClassicAssert.AreEqual(expected, actual);
+ }
+
+ [Test]
+ public void TestApproximatePi()
+ {
+ BigDecimal.Precision = 1000;
+ //BigDecimal.AlwaysTruncate = true;
- var negative2 = BigDecimal.Subtract(BigDecimal.Zero, 3);
- Assert.AreEqual(BigInteger.MinusOne.Sign, negative2.Sign, "0 - 3");
+ int precision = 1000;
+ // This value comes from a different application; precise calculator
+ string expected = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198";
- BigDecimal negative3 = BigInteger.Subtract(0, 3);
- Assert.AreEqual(BigInteger.MinusOne.Sign, negative3.Sign, "BigInteger.Subtract(0, 3)");
+ Stopwatch timer = Stopwatch.StartNew();
- BigDecimal negative4 = 10 * -1;
- Assert.AreEqual(BigInteger.MinusOne.Sign, negative4.Sign, "10 * -1;");
+ string actual = BigDecimal.Round(BigDecimal.ApproximatePi(precision), precision).ToString();
- string largeNegativeString = "-22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853";
- BigDecimal largeNegative = BigDecimal.Parse(largeNegativeString);
- Assert.AreEqual(BigInteger.MinusOne.Sign, largeNegative.Sign, largeNegativeString);
+ TimeSpan timeElapsed = timer.Elapsed;
+ TestContext.WriteLine($"Time Elapsed (ms): {timeElapsed.TotalMilliseconds}");
- string highPrecisionNegativeString = TestBigDecimalHelper.PrepareValue("-2.2685077023948547418271375393606809233149150201282920942551781108927727789384397020382853", this.Format);
- BigDecimal highPrecisionNegative = BigDecimal.Parse(highPrecisionNegativeString);
- Assert.AreEqual(BigInteger.MinusOne.Sign, highPrecisionNegative.Sign, highPrecisionNegativeString);
+ actual = actual[..expected.Length];
- string smallNegativeString = TestBigDecimalHelper.PrepareValue("-0.000000000000000000000000000022680000000000000000000000000000150201282920942551781108927727789384397020382853", this.Format);
- BigDecimal smallNegative = BigDecimal.Parse(smallNegativeString);
- Assert.AreEqual(BigInteger.MinusOne.Sign, smallNegative.Sign, smallNegativeString);
- }
-
- [Test]
- public void TestGoldenIrrational()
- {
- var val = TestBigDecimalHelper.PrepareValue("1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144381497587012203408058879544547492461856953648644492", this.Format);
- var goldenRatio = BigDecimal.Parse(val);
-
- TestContext.Write("GoldenRatio: ");
- TestContext.WriteLine(goldenRatio.ToString());
- }
-
- [Test]
- public void TestLeastCommonDivisor()
- {
- var expected = "45319990731015";
-
- BigDecimal actual = BigIntegerHelper.LCM(new BigInteger[] {
- 3, 5, 7, 11, 13, 101, 307, 311, 313
- });
-
- // 15015, lcm(3, 5, 7, 11, 13, 101, 307, 311, 313) = 45319990731015 lcm(4973, 4292, 4978, 4968, 4297, 4287) = 2822891742340306560
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestRounding()
- {
- var up = new BigDecimal(0.50001d);
- var down = new BigDecimal(0.49m);
-
- var val = TestBigDecimalHelper.PrepareValue("1.5", this.Format);
- var oneAndAhalf = BigDecimal.Parse(val);
-
- var negEightPointFive = new BigDecimal(-8.5d);
- BigDecimal negNinePointFive = -9.5d;
-
- var threePointFourNine = new BigDecimal(3.49d);
- var threePointFiveOne = new BigDecimal(3.51m);
- var sixPointFive = new BigDecimal(6.5);
-
- var zero = BigDecimal.Round(down);
- Assert.AreEqual(BigInteger.Zero, zero);
-
- var one = BigDecimal.Round(up);
- Assert.AreEqual(BigInteger.One, one);
-
- var two = BigDecimal.Round(oneAndAhalf);
- Assert.AreEqual(one + one, two);
-
- var three = BigDecimal.Round(threePointFourNine);
- Assert.AreEqual(one + two, three);
-
- var four = BigDecimal.Round(threePointFiveOne);
- Assert.AreEqual(one + three, four);
- Assert.AreEqual(two + two, four);
-
- var six = BigDecimal.Round(sixPointFive, MidpointRounding.ToEven);
- Assert.AreEqual(two + four, six);
-
- var negEight = BigDecimal.Round(negEightPointFive, MidpointRounding.ToEven);
- Assert.AreEqual(-four + -four, negEight);
-
- var negNine = BigDecimal.Round(negEightPointFive, MidpointRounding.AwayFromZero);
- Assert.AreEqual(-four + -four - one, negNine);
-
- var negTen = BigDecimal.Round(negNinePointFive, MidpointRounding.ToEven);
- Assert.AreEqual(-four + -four - two, negTen);
- }
-
- [Test]
- public void TestRounding_NegativePrecision001()
- {
- BigDecimal a = BigDecimal.Parse("31415.92654");
-
- var result6 = BigDecimal.Round(a, 6).ToString();
- var result5 = BigDecimal.Round(a, 5).ToString();
- var result4 = BigDecimal.Round(a, 4).ToString();
- var result3 = BigDecimal.Round(a, 3).ToString();
- var result2 = BigDecimal.Round(a, 2).ToString();
- var result1 = BigDecimal.Round(a, 1).ToString();
- var result0 = BigDecimal.Round(a, 0).ToString();
- var result_1 = BigDecimal.Round(a, -1).ToString();
- var result_2 = BigDecimal.Round(a, -2).ToString();
- var result_3 = BigDecimal.Round(a, -3).ToString();
- var result_4 = BigDecimal.Round(a, -4).ToString();
- var result_5 = BigDecimal.Round(a, -5).ToString();
- var result_6 = BigDecimal.Round(a, -6).ToString();
- var result_7 = BigDecimal.Round(a, -7).ToString();
-
- TestContext.WriteLine($"{result6}");
- TestContext.WriteLine($"{result5}");
- TestContext.WriteLine($"{result4}");
- TestContext.WriteLine($"{result3}");
- TestContext.WriteLine($"{result2}");
- TestContext.WriteLine($"{result1}");
- TestContext.WriteLine($"{result0}");
- TestContext.WriteLine($"{result_1}");
- TestContext.WriteLine($"{result_2}");
- TestContext.WriteLine($"{result_3}");
- TestContext.WriteLine($"{result_4}");
- TestContext.WriteLine($"{result_5}");
- TestContext.WriteLine($"{result_6}");
- TestContext.WriteLine($"{result_7}");
-
- Assert.AreEqual("31415.92654", result6);
- Assert.AreEqual("31415.92654", result5);
- Assert.AreEqual("31415.9265", result4);
- Assert.AreEqual("31415.926", result3);
- Assert.AreEqual("31415.92", result2);
- Assert.AreEqual("31415.9", result1);
- Assert.AreEqual("31415", result0);
- Assert.AreEqual("31410", result_1);
- Assert.AreEqual("31400", result_2);
- Assert.AreEqual("31000", result_3);
- Assert.AreEqual("30000", result_4);
- Assert.AreEqual("0", result_5);
- Assert.AreEqual("0", result_6);
- Assert.AreEqual("0", result_7);
- }
-
- [Test]
- public void TestRoundingWithPrecision()
- {
- Test(7.54m, 1, RoundingStrategy.AwayFromZero, "7.5");
- Test(7.56m, 1, RoundingStrategy.AwayFromZero, "7.6");
- Test(7.55m, 1, RoundingStrategy.AwayFromZero, "7.6");
-
- Test(-7.54m, 1, RoundingStrategy.AwayFromZero, "-7.5");
- Test(-7.56m, 1, RoundingStrategy.AwayFromZero, "-7.6");
- Test(-7.55m, 1, RoundingStrategy.AwayFromZero, "-7.6");
-
- Test(7.54m, 1, RoundingStrategy.TowardZero, "7.5");
- Test(7.56m, 1, RoundingStrategy.TowardZero, "7.6");
- Test(7.55m, 1, RoundingStrategy.TowardZero, "7.5");
-
- Test(-7.54m, 1, RoundingStrategy.TowardZero, "-7.5");
- Test(-7.56m, 1, RoundingStrategy.TowardZero, "-7.6");
- Test(-7.55m, 1, RoundingStrategy.TowardZero, "-7.5");
-
- Test(-7.551m, 2, RoundingStrategy.AwayFromZero, "-7.55");
- Test(-7.559m, 2, RoundingStrategy.AwayFromZero, "-7.56");
- Test(-7.555m, 2, RoundingStrategy.AwayFromZero, "-7.56");
- }
-
- private static void Test(decimal value, int precision, RoundingStrategy roundingStrategy, string expected)
- {
- BigDecimal actual = BigDecimal.Round(value, precision, roundingStrategy);
-
- Assert.AreEqual(expected, actual.ToString(), $"Value: {value}\tRounding: {Enum.GetName(typeof(RoundingStrategy), roundingStrategy)}\tExpected: {expected}\tActual: {actual}");
- }
-
- [Test]
- public void TestGetWholeValue()
- {
- var value1 = new BigDecimal(BigInteger.Parse("22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
- BigDecimal result1 = value1.WholeValue;
- var value2 = new BigDecimal(BigInteger.Parse("-22685077023948547418271375393606809233149150201282920942551781108927727789384397020382853"), -49);
- BigDecimal result2 = value2.WholeValue;
-
- var value3 = new BigDecimal(3, -1);
- BigDecimal result3 = value3.WholeValue;
- var value4 = new BigDecimal(-3, -1);
- BigDecimal result4 = value4.WholeValue;
-
- var expected1 = "2268507702394854741827137539360680923314";
- var actual1 = result1.ToString();
-
- var expected2 = "-2268507702394854741827137539360680923314";
- var actual2 = result2.ToString();
-
- var expected3 = "0";
- var actual3 = result3.ToString();
-
- var expected4 = "0";
- var actual4 = result4.ToString();
-
- Assert.AreEqual(expected1, actual1);
- Assert.AreEqual(expected2, actual2);
- Assert.AreEqual(expected3, actual3);
- Assert.AreEqual(expected4, actual4);
- }
-
- [Test]
- public void TestGetFractionalPart()
- {
- BigDecimal parsed1 = new BigDecimal(BigInteger.Parse("31415926535"), -10);
- BigDecimal parsed2 = new BigDecimal(BigInteger.Parse("-31415926535"), -10);
- BigDecimal parsed3 = new BigDecimal(BigInteger.Parse("31415926535"), -15);
- BigDecimal parsed4 = new BigDecimal(BigInteger.Parse("-31415926535"), -15);
- BigDecimal parsed5 = new BigDecimal(3, 0);
- BigDecimal parsed6 = new BigDecimal(-3, 0);
- BigDecimal parsed7 = new BigDecimal(3, -1);
- BigDecimal parsed8 = new BigDecimal(-3, -1);
-
- var expected1 = TestBigDecimalHelper.PrepareValue("0.1415926535", this.Format);
- var expected2 = TestBigDecimalHelper.PrepareValue("0.1415926535", this.Format);
- var expected3 = TestBigDecimalHelper.PrepareValue("0.000031415926535", this.Format);
- var expected4 = TestBigDecimalHelper.PrepareValue("0.000031415926535", this.Format);
- var expected5 = "0";
- var expected6 = "0";
- var expected7 = TestBigDecimalHelper.PrepareValue("0.3", this.Format);
- var expected8 = TestBigDecimalHelper.PrepareValue("0.3", this.Format);
-
- var result1 = parsed1.GetFractionalPart();
- var result2 = parsed2.GetFractionalPart();
- var result3 = parsed3.GetFractionalPart();
- var result4 = parsed4.GetFractionalPart();
- var result5 = parsed5.GetFractionalPart();
- var result6 = parsed6.GetFractionalPart();
- var result7 = parsed7.GetFractionalPart();
- var result8 = parsed8.GetFractionalPart();
-
- var actual1 = result1.ToString();
- var actual2 = result2.ToString();
- var actual3 = result3.ToString();
- var actual4 = result4.ToString();
- var actual5 = result5.ToString();
- var actual6 = result6.ToString();
- var actual7 = result7.ToString();
- var actual8 = result8.ToString();
-
- Assert.AreEqual(expected1, actual1, $"GetFractionalPart - #1: {parsed1}");
- Assert.AreEqual(expected2, actual2, $"GetFractionalPart - #2: {parsed2}");
- Assert.AreEqual(expected3, actual3, $"GetFractionalPart - #3: {parsed3}");
- Assert.AreEqual(expected4, actual4, $"GetFractionalPart - #4: {parsed4}");
- Assert.AreEqual(expected5, actual5, $"GetFractionalPart - #5: {parsed5}");
- Assert.AreEqual(expected6, actual6, $"GetFractionalPart - #6: {parsed6}");
- Assert.AreEqual(expected7, actual7, $"GetFractionalPart - #7: {parsed7}");
- Assert.AreEqual(expected8, actual8, $"GetFractionalPart - #8: {parsed8}");
- }
-
- [Test]
- public void TestSignifigantDigits()
- {
- const Int32 expected1 = 19;
- var number1 = new BigDecimal(12345678901234567890, -10);
- var result1 = number1.SignificantDigits;
- Assert.AreEqual(expected1, result1);
-
- const Int32 expected2 = 9;
- var number2 = new BigDecimal(123456789, 1);
- var result2 = number2.SignificantDigits;
- Assert.AreEqual(expected2, result2);
-
- const Int32 expected3 = 19;
- var number3 = new BigDecimal(BigInteger.Parse("-12345678901234567890"), -10);
- var result3 = number3.SignificantDigits;
- Assert.AreEqual(expected3, result3);
-
- const Int32 expected4 = 9;
- var number4 = new BigDecimal(-123456789, 1);
- var result4 = number4.SignificantDigits;
- Assert.AreEqual(expected4, result4);
- }
-
- [Test(TestOf = typeof(BigDecimal))]
- public void TestPlacesRightOfDecimal()
- {
- MethodInfo placesRightOfDecimal = typeof(BigDecimal).GetMethod("PlacesRightOfDecimal", BindingFlags.NonPublic | BindingFlags.Static);
-
- BigDecimal parsed1 = new BigDecimal(BigInteger.Parse("314159265358979323846"), -20);
- BigDecimal parsed2 = new BigDecimal(BigInteger.Parse("-314159265358979323846"), -20);
- BigDecimal parsed3 = new BigDecimal(BigInteger.Parse("31415926535897932384600000"), -25);
- BigDecimal parsed4 = new BigDecimal(BigInteger.Parse("314159265358979323846"), -25);
- BigDecimal parsed5 = new BigDecimal(BigInteger.Parse("-314159265358979323846"), -25);
- BigDecimal parsed6 = new BigDecimal(BigInteger.Parse("31415926535897932384600000"), -30);
- BigDecimal parsed7 = new BigDecimal(3, 0);
- BigDecimal parsed8 = new BigDecimal(-3, 0);
- BigDecimal parsed9 = new BigDecimal(3, -1);
- BigDecimal parsed0 = new BigDecimal(-3, -1);
- BigDecimal parsed11 = new BigDecimal(BigInteger.Parse("10000000000000031400000"), -25);
-
- var expected1 = "20";
- var expected2 = "20";
- var expected3 = "20";
- var expected4 = "25";
- var expected5 = "25";
- var expected6 = "25";
- var expected7 = "0";
- var expected8 = "0";
- var expected9 = "1";
- var expected0 = "1";
- var expected11 = "20";
-
- var result1 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed1 });
- var result2 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed2 });
- var result3 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed3 });
- var result4 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed4 });
- var result5 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed5 });
- var result6 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed6 });
- var result7 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed7 });
- var result8 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed8 });
- var result9 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed9 });
- var result0 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed0 });
- var result11 = placesRightOfDecimal.Invoke(obj: null, new object[] { parsed11 });
-
- var actual1 = result1.ToString();
- var actual2 = result2.ToString();
- var actual3 = result3.ToString();
- var actual4 = result4.ToString();
- var actual5 = result5.ToString();
- var actual6 = result6.ToString();
- var actual7 = result7.ToString();
- var actual8 = result8.ToString();
- var actual9 = result9.ToString();
- var actual0 = result0.ToString();
- var actual11 = result11.ToString();
-
- Assert.AreEqual(expected1, actual1, $"PlacesRightOfDecimal - #1: {parsed1}");
- Assert.AreEqual(expected2, actual2, $"PlacesRightOfDecimal - #2: {parsed2}");
- Assert.AreEqual(expected3, actual3, $"PlacesRightOfDecimal - #3: {parsed3}");
- Assert.AreEqual(expected4, actual4, $"PlacesRightOfDecimal - #4: {parsed4}");
- Assert.AreEqual(expected5, actual5, $"PlacesRightOfDecimal - #5: {parsed5}");
- Assert.AreEqual(expected6, actual6, $"PlacesRightOfDecimal - #6: {parsed6}");
- Assert.AreEqual(expected7, actual7, $"PlacesRightOfDecimal - #7: {parsed7}");
- Assert.AreEqual(expected8, actual8, $"PlacesRightOfDecimal - #8: {parsed8}");
- Assert.AreEqual(expected9, actual9, $"PlacesRightOfDecimal - #9: {parsed9}");
- Assert.AreEqual(expected0, actual0, $"PlacesRightOfDecimal - #10: {parsed0}");
- Assert.AreEqual(expected11, actual11, $"PlacesRightOfDecimal - #11: {parsed11}");
- }
-
- [Test]
- public void TestApproximateE()
- {
- BigDecimal.Precision = 1000;
- //BigDecimal.AlwaysTruncate = true;
-
- int precision = 1000;
- // This value comes from a different application; precise calculator
- string expected = "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035";
-
- Stopwatch timer = Stopwatch.StartNew();
-
- string actual = BigDecimal.Round(BigDecimal.ApproximateE(precision), precision).ToString();
-
- TimeSpan timeElapsed = timer.Elapsed;
- TestContext.WriteLine($"Time Elapsed (ms): {timeElapsed.TotalMilliseconds}");
-
- actual = actual.Substring(0, expected.Length);
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestApproximatePi()
- {
- BigDecimal.Precision = 1000;
- //BigDecimal.AlwaysTruncate = true;
-
- int precision = 1000;
- // This value comes from a different application; precise calculator
- string expected = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198";
-
- Stopwatch timer = Stopwatch.StartNew();
-
- string actual = BigDecimal.Round(BigDecimal.ApproximatePi(precision), precision).ToString();
-
- TimeSpan timeElapsed = timer.Elapsed;
- TestContext.WriteLine($"Time Elapsed (ms): {timeElapsed.TotalMilliseconds}");
-
- actual = actual.Substring(0, expected.Length);
-
- Assert.AreEqual(expected, actual);
- }
+ ClassicAssert.AreEqual(expected, actual);
}
}
diff --git a/TestBigDecimal/TestBigDecimalHelper.cs b/TestBigDecimal/TestBigDecimalHelper.cs
index dbe020c..6ab0768 100644
--- a/TestBigDecimal/TestBigDecimalHelper.cs
+++ b/TestBigDecimal/TestBigDecimalHelper.cs
@@ -1,37 +1,34 @@
-using System;
-using System.Globalization;
+using System.Globalization;
using System.Text;
using ExtendedNumerics;
-namespace TestBigDecimal
-{
+namespace BigDecimalTests;
- public static class TestBigDecimalHelper
+public static class TestBigDecimalHelper
+{
+ public static string GetInternalValues(BigDecimal bigDecimal)
{
- public static String GetInternalValues(BigDecimal bigDecimal)
- {
- StringBuilder result = new StringBuilder();
+ StringBuilder result = new();
- result.AppendLine($"{bigDecimal.ToString()}");
- result.AppendLine("{");
- result.AppendLine($" Mantissa: {bigDecimal.Mantissa}");
- result.AppendLine($" Exponent: {bigDecimal.Exponent}");
- result.AppendLine($" DecimalIndex: {bigDecimal.GetDecimalIndex()}");
- result.AppendLine($" Length: {bigDecimal.Length}");
- result.AppendLine($" SignifigantDigits: {bigDecimal.SignificantDigits}");
- result.AppendLine("}");
+ result.AppendLine($"{bigDecimal.ToString()}");
+ result.AppendLine("{");
+ result.AppendLine($" Mantissa: {bigDecimal.Mantissa}");
+ result.AppendLine($" Exponent: {bigDecimal.Exponent}");
+ result.AppendLine($" DecimalIndex: {bigDecimal.GetDecimalIndex()}");
+ result.AppendLine($" Length: {bigDecimal.Length}");
+ result.AppendLine($" SignifigantDigits: {bigDecimal.SignificantDigits}");
+ result.AppendLine("}");
- return result.ToString();
- }
+ return result.ToString();
+ }
- public static string PrepareValue(string value, NumberFormatInfo numberFormatInfo)
+ public static string PrepareValue(string value, NumberFormatInfo numberFormatInfo)
+ {
+ if (numberFormatInfo.NumberDecimalSeparator != ".")
{
- if (numberFormatInfo.NumberDecimalSeparator != ".")
- {
- return value.Replace(".", numberFormatInfo.NumberDecimalSeparator);
- }
-
- return value;
+ return value.Replace(".", numberFormatInfo.NumberDecimalSeparator);
}
+
+ return value;
}
}
\ No newline at end of file
diff --git a/TestBigDecimal/TestBigDecimalOperations.cs b/TestBigDecimal/TestBigDecimalOperations.cs
index 7ba6e1f..9aa450b 100644
--- a/TestBigDecimal/TestBigDecimalOperations.cs
+++ b/TestBigDecimal/TestBigDecimalOperations.cs
@@ -5,1128 +5,1126 @@
using ExtendedNumerics;
using ExtendedNumerics.Helpers;
using NUnit.Framework;
+using NUnit.Framework.Legacy;
-namespace TestBigDecimal
+namespace BigDecimalTests;
+
+[TestFixture]
+public class TestBigDecimalOperations
{
+ private NumberFormatInfo Format => Thread.CurrentThread.CurrentCulture.NumberFormat;
+
+ [Test]
+ public void TestAddition001()
+ {
+ var number1 = BigDecimal.Parse("1234567890");
+ var expected = BigDecimal.Parse("3382051537");
+
+ var actual = number1 + 0x7FFFFFFF;
+
+ ClassicAssert.AreEqual(expected, actual);
+ }
+
+ [Test]
+ public void TestAddition002()
+ {
+ var A = new BigDecimal(new BigInteger(1234567), -1);
+ var B = new BigDecimal(new BigInteger(9876543), -9);
+
+ var actual = BigDecimal.Add(A, B);
+ var expected = TestBigDecimalHelper.PrepareValue("123456.709876543", Format);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ public void TestBigDecimalPow()
+ {
+ string expected = "268637376395543538746286686601216000000000000";
+ // 5040 ^ 12 = 268637376395543538746286686601216000000000000
+
+ var number = BigDecimal.Parse("5040");
+ var exp12 = BigDecimal.Pow(number, 12);
+ string actual = exp12.ToString();
+
+ ClassicAssert.AreEqual(expected, actual, "5040 ^ 12 = 268637376395543538746286686601216000000000000");
+ }
+
+
+ [Test]
+ public void TestIsPositive()
+ {
+ ClassicAssert.IsTrue(BigDecimal.One.IsPositive());
+ ClassicAssert.IsTrue(BigDecimal.Parse("0.001").IsPositive());
+ ClassicAssert.IsFalse(BigDecimal.Parse("0").IsPositive());
+ ClassicAssert.IsFalse(BigDecimal.Parse("-0.00000001").IsPositive());
+ ClassicAssert.IsFalse(BigDecimal.MinusOne.IsPositive());
+ }
+
+ [Test]
+ public void TestTruncate_Zero()
+ {
+ string expected = "0";
+ var actual = BigDecimal.Truncate(BigDecimal.Zero);
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ [NonParallelizable]
+ public void TestTruncate001()
+ {
+ string inputTruncated =
+ TestBigDecimalHelper.PrepareValue("0.38776413731534507341472294220970933835515664718260518542692164892369393388454765429965711304132864249950074173248631118139885711281403156400182208418498132380665348582256048635378814909035638369142648772177618951899185003568005598389270883746269077440991532220847404333505059368816860680653357748237545067181074698997348124273540082967040205337039719556204791774654186626822192852578297197622945023468587167940717672154626982847038945027431144177383552390076867181200131087852865589018597759807623800948540502708501473286123912110702619773244550322465985979980114779581215743799790210865866959716136152785422203785552850816565888483726280027736811701443283167094373785256268739306209472514414456698923382789454032363968616464876677737866600848986505927023714735496267888826964325695603484817243244809072199216323431074501911199589021095576091452848741385260278621154863084476153732935785975553768625991893151359011912157996422994871709298494233782632174324940552077998861058801035481527689611495569489001108047129667715138204276438217877458404549511313153150137527893798615996618488836664617057038126333313180040094667868763537391421370724952266262848120654117339947389714860375532634890853303061644123428823851175161042458377024247370039795707768407904842511280809838660155394320788005292339449327116941969519022963362256288633034183673183754543670394109495242494711094883817203892173617367313695468521390931623680173196403022932833410798171066411176995128779282872081932608267999112302172207922731784899893348412676098162467010668924433588685153611407005617772276741793479524433622378470125354022566116327819435029065557564148488746638319658731270457607183892425161850287824787546065413294231650473976473355046501500793677901782339691542233183741598103696583351788651177203938936918102755367072014549821942367309956671236707350861545099496206538228683951185018840006137763162355709495445928668480960580978979870379511703883251713690511544429859593313279574155504139941107166963497890833932811052504269372145803660626639428643564562691059910703703938694915154537936003382455188656514686359660013747580119285264755448830584594983111162605867224680013454700621697086948523549156403848856543212816956769085216390639154261614649538130954560421673680672884105498050605587531872704107707071402689983600332112655608194612408217782173036018661692139351433658340756975168361107372727516912020823362368253159826937134217107045868191298957690827125630453728790792408734840661702578638598543186544910552465999106381802375938701350575940262569041045146526024334627822715612658351899764042223444201035443823410277761971257862200600465373558428055133799307959576455801692979753194304758921759067399106319456847661528054181651013888120488047974670158855437555210689546049958555745098303660202886404313365902203237775035723926097742965028613593632230336269392684340085274710999024668887638930755250701806345477524832568256645103704878731032912768646402146422301881142289323523789305126831904241622042944333916620344863470012778933196413192781253025453531244850133026071231714118351386262249150472643870800725983523611903791303553632632769972142502483519860983067322477753824959399886980031912842700140970151007657989042261109130704991895244868527969247414974047405237324669264878742391500642753525622057641241164177505839173992651361990366480244195157062835803031557544691492841007028723179639729081951702197292799161437892952439082270465575308762112590993865133052593362045638622447863872110087219994330766670422412140283392118259566085972052360790645394540700438378734059789109046910356858343004387656915432928337709841252916626015752013241699464443045041876948902728601721842214670716585909801092203893128618468720651888522728597430373030188565238122801065278124235661294292641028550276301054915567825793810248724267437857461336921376742513529432313053995421425528496990787018582251366776291943999044323970133345610820834058391982655766601126736285624213085882525085728598384700565577250732861707158419417137322187913601105221450993534840307771350787020353312910993312574109077271828643659506792514058623896881407687463008239648545371730757776422468606770212802349464720937428992320859723193781752582479518699133569129895070994026410312951649922900688489934852155962598169718199217867461287501481443450777777718726084902136902441480397119384141792970385831367927360530354214563521320900336914169681532166791668676942898880184098720787172114194029069821244937592713815214434788393095503048740886117426353441330676447598548976011441527165748380891340472246800001389307364429687469295246232117792720007673578989468325170179570094184486525355114468774857635615955720968054081874458733938769018227387365842825259166694681823057556598910704367318366050815517174086712448729791746859581490981539042615521145996146036468018904747552880641671500884541141688250485043379587617571474356672696577799617797264760021142950373293666619603196041523466051054935554966409263947312788960948665394281916627352113060683470291466105925", Format);
+ string inputOverflow = "919919200639429489197056";
+
+ var expected = inputTruncated;
+
+ var longValue = BigDecimal.Parse(string.Concat(inputTruncated, inputOverflow));
+ var result = BigDecimal.Round(longValue, 5000);
+
+ var actual = result.ToString();
+
+ ClassicAssert.AreEqual(expected, actual);
+ }
+
+ [Test]
+ public void TestTruncate002()
+ {
+ string expected_floor = Math.Floor(-15.5d).ToString();
+ string expected_truncate = Math.Truncate(-15.5d).ToString();
+
+ string actual_floor = BigDecimal.Floor(new BigDecimal(-15.5)).ToString(); // -16
+ string actual_truncate = BigDecimal.Truncate(new BigDecimal(-15.5)).ToString(); // -16 (should be -15)
+
+ ClassicAssert.AreEqual(expected_floor, actual_floor, "BigDecimal.Floor(-15.5)");
+ ClassicAssert.AreEqual(expected_truncate, actual_truncate, "BigDecimal.Truncate(-15.5)");
+ }
+
+ [Test]
+ public void TestCeiling_Zero()
+ {
+ string expected = "0";
+ var actual = BigDecimal.Ceiling(BigDecimal.Zero);
+ ClassicAssert.AreEqual(expected, actual.ToString(), "Ceiling(0)");
+ }
+
+ [Test]
+ public void TestCeiling001()
+ {
+ string expected = "4";
+
+ var input = BigDecimal.Pi;
+ BigDecimal actual = BigDecimal.Ceiling(input);
+
+ string info = $"Ceiling({input.ToString()[..5]}...) = {actual} ({expected})";
+ TestContext.WriteLine(info);
+ ClassicAssert.AreEqual(expected, actual.ToString(), info);
+ }
+
+ [Test]
+ public void TestCeiling002()
+ {
+ string expected = "-3";
+ var input = -BigDecimal.Pi;
+ string actual = BigDecimal.Ceiling(input).ToString();
+
+ string info = $"Ceiling({input.ToString()[..5]}...) = {actual} ({expected})";
+ TestContext.WriteLine(info);
+ ClassicAssert.AreEqual(expected, actual, info);
+ }
+
+ [Test]
+ public void TestCeiling003()
+ {
+ var val = TestBigDecimalHelper.PrepareValue("0.14159265", Format);
+ var start = BigDecimal.Parse(val);
+ var ceiling = BigDecimal.Ceiling(start);
+
+ ClassicAssert.AreEqual(BigDecimal.One, ceiling);
+ }
+
+ [Test]
+ public void TestCeiling004()
+ {
+ var val = TestBigDecimalHelper.PrepareValue("-0.14159265", Format);
+ var start = BigDecimal.Parse(val);
+ var ceiling = BigDecimal.Ceiling(start);
+
+ ClassicAssert.AreEqual(BigDecimal.Zero, ceiling);
+ }
+
+ [Test]
+ public void TestDivide000()
+ {
+ var val1 = TestBigDecimalHelper.PrepareValue("0.63", Format);
+ var val2 = TestBigDecimalHelper.PrepareValue("0.09", Format);
+
+ var dividend = BigDecimal.Parse(val1);
+ var divisor = BigDecimal.Parse(val2);
+
+ var actual = BigDecimal.Divide(dividend, divisor);
+ string expected = "7";
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ public void TestDivide001()
+ {
+ var expected = "40094690950920881030683735292761468389214899724061";
+
+ var dividend = BigDecimal.Parse("1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139");
+ var divisor = BigDecimal.Parse("37975227936943673922808872755445627854565536638199");
+
+ var actual = BigDecimal.Divide(dividend, divisor);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ public void TestDivide002()
+ {
+ var resultDividend = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.001", Format));
+ var resultDivisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.5", Format));
+ var expectedQuotientResult = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.002", Format));
+
+ var quotientResult = BigDecimal.Divide(resultDividend, resultDivisor);
+
+ ClassicAssert.AreEqual(expectedQuotientResult, quotientResult);
+ }
+
+ [Test]
+ public void TestDivide003()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 11;
+
+ var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.90606447789", Format));
+ var actual = BigDecimal.Divide(BigDecimal.One, divisor);
+ actual = BigDecimal.Round(actual, 100);
+
+ //var expected = BigDecimal.Parse( "1.1036742134828557" );
+ string expected = TestBigDecimalHelper.PrepareValue("1.1036742134", Format);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+
+ BigDecimal.Precision = savePrecision;
+ }
+
+ [Test]
+ public void TestDivide004()
+ {
+ var twenty = new BigDecimal(20);
+ var actual = BigDecimal.Divide(BigDecimal.One, twenty);
+ string expected = TestBigDecimalHelper.PrepareValue("0.05", Format);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- [TestFixture]
- [Culture("en-US,ru-RU")]
- public class TestBigDecimalOperations
- {
- private NumberFormatInfo Format { get { return Thread.CurrentThread.CurrentCulture.NumberFormat; } }
-
- [Test]
- public void TestAddition001()
- {
- var number1 = BigDecimal.Parse("1234567890");
- var expected = BigDecimal.Parse("3382051537");
-
- var actual = number1 + 0x7FFFFFFF;
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestAddition002()
- {
- var A = new BigDecimal(new BigInteger(1234567), -1);
- var B = new BigDecimal(new BigInteger(9876543), -9);
-
- var actual = BigDecimal.Add(A, B);
- var expected = TestBigDecimalHelper.PrepareValue("123456.709876543", this.Format);
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestBigDecimalPow()
- {
- string expected = "268637376395543538746286686601216000000000000";
- // 5040 ^ 12 = 268637376395543538746286686601216000000000000
-
- var number = BigDecimal.Parse("5040");
- var exp12 = BigDecimal.Pow(number, 12);
- string actual = exp12.ToString();
-
- Assert.AreEqual(expected, actual, "5040 ^ 12 = 268637376395543538746286686601216000000000000");
- }
-
-
- [Test]
- public void TestIsPositive()
- {
- Assert.IsTrue(BigDecimal.One.IsPositive());
- Assert.IsTrue(BigDecimal.Parse("0.001").IsPositive());
- Assert.IsFalse(BigDecimal.Parse("0").IsPositive());
- Assert.IsFalse(BigDecimal.Parse("-0.00000001").IsPositive());
- Assert.IsFalse(BigDecimal.MinusOne.IsPositive());
- }
-
- [Test]
- public void TestTruncate_Zero()
- {
- string expected = "0";
- var actual = BigDecimal.Truncate(BigDecimal.Zero);
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- [NonParallelizable]
- public void TestTruncate001()
- {
- string inputTruncated =
- TestBigDecimalHelper.PrepareValue("0.38776413731534507341472294220970933835515664718260518542692164892369393388454765429965711304132864249950074173248631118139885711281403156400182208418498132380665348582256048635378814909035638369142648772177618951899185003568005598389270883746269077440991532220847404333505059368816860680653357748237545067181074698997348124273540082967040205337039719556204791774654186626822192852578297197622945023468587167940717672154626982847038945027431144177383552390076867181200131087852865589018597759807623800948540502708501473286123912110702619773244550322465985979980114779581215743799790210865866959716136152785422203785552850816565888483726280027736811701443283167094373785256268739306209472514414456698923382789454032363968616464876677737866600848986505927023714735496267888826964325695603484817243244809072199216323431074501911199589021095576091452848741385260278621154863084476153732935785975553768625991893151359011912157996422994871709298494233782632174324940552077998861058801035481527689611495569489001108047129667715138204276438217877458404549511313153150137527893798615996618488836664617057038126333313180040094667868763537391421370724952266262848120654117339947389714860375532634890853303061644123428823851175161042458377024247370039795707768407904842511280809838660155394320788005292339449327116941969519022963362256288633034183673183754543670394109495242494711094883817203892173617367313695468521390931623680173196403022932833410798171066411176995128779282872081932608267999112302172207922731784899893348412676098162467010668924433588685153611407005617772276741793479524433622378470125354022566116327819435029065557564148488746638319658731270457607183892425161850287824787546065413294231650473976473355046501500793677901782339691542233183741598103696583351788651177203938936918102755367072014549821942367309956671236707350861545099496206538228683951185018840006137763162355709495445928668480960580978979870379511703883251713690511544429859593313279574155504139941107166963497890833932811052504269372145803660626639428643564562691059910703703938694915154537936003382455188656514686359660013747580119285264755448830584594983111162605867224680013454700621697086948523549156403848856543212816956769085216390639154261614649538130954560421673680672884105498050605587531872704107707071402689983600332112655608194612408217782173036018661692139351433658340756975168361107372727516912020823362368253159826937134217107045868191298957690827125630453728790792408734840661702578638598543186544910552465999106381802375938701350575940262569041045146526024334627822715612658351899764042223444201035443823410277761971257862200600465373558428055133799307959576455801692979753194304758921759067399106319456847661528054181651013888120488047974670158855437555210689546049958555745098303660202886404313365902203237775035723926097742965028613593632230336269392684340085274710999024668887638930755250701806345477524832568256645103704878731032912768646402146422301881142289323523789305126831904241622042944333916620344863470012778933196413192781253025453531244850133026071231714118351386262249150472643870800725983523611903791303553632632769972142502483519860983067322477753824959399886980031912842700140970151007657989042261109130704991895244868527969247414974047405237324669264878742391500642753525622057641241164177505839173992651361990366480244195157062835803031557544691492841007028723179639729081951702197292799161437892952439082270465575308762112590993865133052593362045638622447863872110087219994330766670422412140283392118259566085972052360790645394540700438378734059789109046910356858343004387656915432928337709841252916626015752013241699464443045041876948902728601721842214670716585909801092203893128618468720651888522728597430373030188565238122801065278124235661294292641028550276301054915567825793810248724267437857461336921376742513529432313053995421425528496990787018582251366776291943999044323970133345610820834058391982655766601126736285624213085882525085728598384700565577250732861707158419417137322187913601105221450993534840307771350787020353312910993312574109077271828643659506792514058623896881407687463008239648545371730757776422468606770212802349464720937428992320859723193781752582479518699133569129895070994026410312951649922900688489934852155962598169718199217867461287501481443450777777718726084902136902441480397119384141792970385831367927360530354214563521320900336914169681532166791668676942898880184098720787172114194029069821244937592713815214434788393095503048740886117426353441330676447598548976011441527165748380891340472246800001389307364429687469295246232117792720007673578989468325170179570094184486525355114468774857635615955720968054081874458733938769018227387365842825259166694681823057556598910704367318366050815517174086712448729791746859581490981539042615521145996146036468018904747552880641671500884541141688250485043379587617571474356672696577799617797264760021142950373293666619603196041523466051054935554966409263947312788960948665394281916627352113060683470291466105925", this.Format);
- string inputOverflow = "919919200639429489197056";
-
- var expected = inputTruncated;
-
- var longValue = BigDecimal.Parse(String.Concat(inputTruncated, inputOverflow));
- var result = BigDecimal.Round(longValue, 5000);
-
- var actual = result.ToString();
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestTruncate002()
- {
- string expected_floor = Math.Floor(-15.5d).ToString();
- string expected_truncate = Math.Truncate(-15.5d).ToString();
-
- string actual_floor = BigDecimal.Floor(new BigDecimal(-15.5)).ToString(); // -16
- string actual_truncate = BigDecimal.Truncate(new BigDecimal(-15.5)).ToString(); // -16 (should be -15)
-
- Assert.AreEqual(expected_floor, actual_floor, "BigDecimal.Floor(-15.5)");
- Assert.AreEqual(expected_truncate, actual_truncate, "BigDecimal.Truncate(-15.5)");
- }
-
- [Test]
- public void TestCeiling_Zero()
- {
- string expected = "0";
- var actual = BigDecimal.Ceiling(BigDecimal.Zero);
- Assert.AreEqual(expected, actual.ToString(), "Ceiling(0)");
- }
-
- [Test]
- public void TestCeiling001()
- {
- string expected = "4";
-
- var input = BigDecimal.Pi;
- BigDecimal actual = BigDecimal.Ceiling(input);
-
- string info = $"Ceiling({input.ToString().Substring(0, 5)}...) = {actual} ({expected})";
- TestContext.WriteLine(info);
- Assert.AreEqual(expected, actual.ToString(), info);
- }
-
- [Test]
- public void TestCeiling002()
- {
- string expected = "-3";
- var input = -BigDecimal.Pi;
- string actual = BigDecimal.Ceiling(input).ToString();
-
- string info = $"Ceiling({input.ToString().Substring(0, 5)}...) = {actual} ({expected})";
- TestContext.WriteLine(info);
- Assert.AreEqual(expected, actual, info);
- }
-
- [Test]
- public void TestCeiling003()
- {
- var val = TestBigDecimalHelper.PrepareValue("0.14159265", this.Format);
- var start = BigDecimal.Parse(val);
- var ceiling = BigDecimal.Ceiling(start);
-
- Assert.AreEqual(BigDecimal.One, ceiling);
- }
-
- [Test]
- public void TestCeiling004()
- {
- var val = TestBigDecimalHelper.PrepareValue("-0.14159265", this.Format);
- var start = BigDecimal.Parse(val);
- var ceiling = BigDecimal.Ceiling(start);
-
- Assert.AreEqual(BigDecimal.Zero, ceiling);
- }
-
- [Test]
- public void TestDivide000()
- {
- var val1 = TestBigDecimalHelper.PrepareValue("0.63", this.Format);
- var val2 = TestBigDecimalHelper.PrepareValue("0.09", this.Format);
-
- var dividend = BigDecimal.Parse(val1);
- var divisor = BigDecimal.Parse(val2);
-
- var actual = BigDecimal.Divide(dividend, divisor);
- string expected = "7";
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide001()
- {
- var expected = "40094690950920881030683735292761468389214899724061";
-
- var dividend = BigDecimal.Parse("1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139");
- var divisor = BigDecimal.Parse("37975227936943673922808872755445627854565536638199");
+ [Test]
+ public void TestDivide005()
+ {
+ var a = new BigDecimal(5);
+ var b = new BigDecimal(8);
- var actual = BigDecimal.Divide(dividend, divisor);
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide002()
- {
- var resultDividend = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.001", this.Format));
- var resultDivisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.5", this.Format));
- var expectedQuotientResult = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.002", this.Format));
-
- var quotientResult = BigDecimal.Divide(resultDividend, resultDivisor);
-
- Assert.AreEqual(expectedQuotientResult, quotientResult);
- }
-
- [Test]
- public void TestDivide003()
- {
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 11;
-
- var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.90606447789", this.Format));
- var actual = BigDecimal.Divide(BigDecimal.One, divisor);
- actual = BigDecimal.Round(actual, 100);
-
- //var expected = BigDecimal.Parse( "1.1036742134828557" );
- string expected = TestBigDecimalHelper.PrepareValue("1.1036742134", this.Format);
-
- Assert.AreEqual(expected, actual.ToString());
-
- BigDecimal.Precision = savePrecision;
- }
-
- [Test]
- public void TestDivide004()
- {
- var twenty = new BigDecimal(20);
- var actual = BigDecimal.Divide(BigDecimal.One, twenty);
- string expected = TestBigDecimalHelper.PrepareValue("0.05", this.Format);
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide005()
- {
- var a = new BigDecimal(5);
- var b = new BigDecimal(8);
-
- var actual = BigDecimal.Divide(a, b);
- string expected = TestBigDecimalHelper.PrepareValue("0.625", this.Format);
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide006()
- {
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 12;
+ var actual = BigDecimal.Divide(a, b);
+ string expected = TestBigDecimalHelper.PrepareValue("0.625", Format);
- var a = new BigDecimal(1);
- var b = new BigDecimal(7);
-
- var actual = BigDecimal.Divide(a, b);
- string expected = TestBigDecimalHelper.PrepareValue("0.142857142857", this.Format);
-
- Assert.AreEqual(expected, actual.ToString());
-
- BigDecimal.Precision = savePrecision;
- }
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- [Test]
- public void TestDivide005A()
- {
- var val1 = TestBigDecimalHelper.PrepareValue("0.5", this.Format);
- var val2 = TestBigDecimalHelper.PrepareValue("0.01", this.Format);
-
- var value = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
- string expected = "50";
- string actual = value.ToString();
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestDivide005B()
- {
- var val1 = TestBigDecimalHelper.PrepareValue("0.5", this.Format);
- var val2 = TestBigDecimalHelper.PrepareValue("0.1", this.Format);
-
- var actual = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
- string expected = "5";
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide005C()
- {
- var val1 = TestBigDecimalHelper.PrepareValue("0.60", this.Format);
- var val2 = TestBigDecimalHelper.PrepareValue("0.01", this.Format);
-
- var actual = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
- string expected = "60";
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestDivide_50by2_001()
- {
- var actual = BigDecimal.Divide(BigDecimal.Parse("50"), BigDecimal.Parse("2"));
- string expected = "25";
-
- Assert.AreEqual(expected.ToString(), actual.ToString());
- }
-
- [Test]
- public void TestDivide_OneOver()
- {
- var numerator = BigDecimal.One;
- var val = TestBigDecimalHelper.PrepareValue("0.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068", this.Format);
- var denominator = BigDecimal.Parse(val);
+ [Test]
+ public void TestDivide006()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 12;
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 100;
-
- var actual1 = BigDecimal.One / denominator;
- var actual2 = numerator / denominator;
- var actual3 = BigDecimal.Divide(BigDecimal.One, denominator);
- var actual4 = BigDecimal.Divide(numerator, denominator);
-
- string expectedString = TestBigDecimalHelper.PrepareValue("7.062513305931045769793005152570558042734310025145531333998316873555903337580056083503977475916243946", this.Format);
- var expected = BigDecimal.Parse(expectedString);
-
- Assert.AreEqual(expectedString, actual1.ToString(), "expectedString != actual1.ToString()");
- Assert.AreEqual(expected.ToString(), actual1.ToString(), "expected.ToString() != actual1.ToString()");
- Assert.AreEqual(expected, actual1, "expected != ( BigDecimal.One / denominator )");
- Assert.AreEqual(expected, actual2, "expected != ( numerator / denominator )");
- Assert.AreEqual(expected, actual3, "expected != ( BigDecimal.Divide(BigDecimal.One, denominator) )");
- Assert.AreEqual(expected, actual4, "expected != ( BigDecimal.Divide(numerator, denominator) )");
-
- BigDecimal.Precision = savePrecision;
- }
-
- [Test]
- public void TestFloor_Zero()
- {
- string expected = "0";
- var actual0 = BigDecimal.Floor(BigDecimal.Zero);
- Assert.AreEqual(expected, actual0.ToString(), "Floor(0)");
-
- var actual1 = BigDecimal.Floor(BigDecimal.Parse("0.01"));
- Assert.AreEqual(expected, actual1.ToString(), "Floor(0.01)");
-
- var actual2 = BigDecimal.Floor(BigDecimal.Parse("-0.01"));
- Assert.AreEqual("-1", actual2.ToString(), "Floor(-0.01)");
- }
-
- [Test]
- public void TestFloor001()
- {
- string expected = "3";
- var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.14159265", this.Format));
- var floor = BigDecimal.Floor(start);
+ var a = new BigDecimal(1);
+ var b = new BigDecimal(7);
- Assert.AreEqual(expected, floor.ToString());
- }
-
- [Test]
- public void TestFloor002()
- {
- string expected = "-4";
- var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.14159265", this.Format));
- var floor = BigDecimal.Floor(start);
+ var actual = BigDecimal.Divide(a, b);
+ string expected = TestBigDecimalHelper.PrepareValue("0.142857142857", Format);
- Assert.AreEqual(expected, floor.ToString());
- }
+ ClassicAssert.AreEqual(expected, actual.ToString());
- [Test]
- public void TestFloor003()
- {
- var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.14159265", this.Format));
- var floor = BigDecimal.Floor(start);
-
- Assert.AreEqual(BigDecimal.MinusOne, floor);
- }
-
- [Test]
- public void TestFloor004()
- {
- var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.14159265", this.Format));
- var floor = BigDecimal.Floor(start);
- var actual = BigDecimal.Parse(floor.ToString());
-
- Assert.AreEqual(BigDecimal.Zero, actual);
- }
-
- [Test]
- public void TestFloor005()
- {
- float start_compare = -2629440f;
- BigDecimal start_test = BigDecimal.Parse("-2629440");
-
- BigDecimal actualFloor = BigDecimal.Floor(start_test);
-
- float expectedFloor = (float)Math.Floor(start_compare);
-
- Assert.AreEqual(expectedFloor.ToString(), actualFloor.ToString());
- }
-
- [Test]
- public void TestCeiling005()
- {
- float start_compare = -2629440f;
- BigDecimal start_test = BigDecimal.Parse("-2629440");
-
- BigDecimal actualCeil = BigDecimal.Ceiling(start_test);
-
- float expectedCeil = (float)Math.Ceiling(start_compare);
-
- Assert.AreEqual(expectedCeil.ToString(), actualCeil.ToString());
- }
+ BigDecimal.Precision = savePrecision;
+ }
- [Test]
- public void TestMod1()
- {
-
- // 31 % 19 = 12
- BigDecimal dividend = 31;
- BigDecimal divisor = 19;
- BigDecimal expected = 12;
-
- var actual = BigDecimal.Mod(dividend, divisor);
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestMod2()
- {
- // 1891 % 31 = 0
- BigDecimal dividend = 1891;
- BigDecimal divisor = 31;
- BigDecimal expected = 0;
-
- var actual = BigDecimal.Mod(dividend, divisor);
-
- Assert.AreEqual(expected, actual);
- }
-
- [Test]
- public void TestMod3()
- {
+ [Test]
+ public void TestDivide005A()
+ {
+ var val1 = TestBigDecimalHelper.PrepareValue("0.5", Format);
+ var val2 = TestBigDecimalHelper.PrepareValue("0.01", Format);
- // 6661 % 60 = 1
- BigDecimal dividend = 6661;
- BigDecimal divisor = 60;
- BigDecimal expected = 1;
-
- var actual = BigDecimal.Mod(dividend, divisor);
-
- Assert.AreEqual(expected, actual);
- }
+ var value = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
+ string expected = "50";
+ string actual = value.ToString();
- [Test]
- public void TestMod4()
- {
+ ClassicAssert.AreEqual(expected, actual);
+ }
- //NOTE This test fails if the values are Doubles instead of Decimals.
+ [Test]
+ public void TestDivide005B()
+ {
+ var val1 = TestBigDecimalHelper.PrepareValue("0.5", Format);
+ var val2 = TestBigDecimalHelper.PrepareValue("0.1", Format);
- // 31 % 3.66666 = 1.66672
- BigDecimal dividend = 31m;
- BigDecimal divisor = 3.66666m;
- BigDecimal expected = 1.66672m;
+ var actual = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
+ string expected = "5";
- var actual = BigDecimal.Mod(dividend, divisor);
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- Assert.AreEqual(expected, actual);
- }
+ [Test]
+ public void TestDivide005C()
+ {
+ var val1 = TestBigDecimalHelper.PrepareValue("0.60", Format);
+ var val2 = TestBigDecimalHelper.PrepareValue("0.01", Format);
- [Test]
- public void TestMod5()
- {
- // 240 % 2 = 0
- BigDecimal dividend = 240;
- BigDecimal divisor = 2;
- string expectedString = "0";
- BigDecimal expected = 0;
+ var actual = BigDecimal.Divide(BigDecimal.Parse(val1), BigDecimal.Parse(val2));
+ string expected = "60";
- var actual = BigDecimal.Mod(dividend, divisor);
- string actualString = actual.ToString();
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- Assert.AreEqual(expected, actual, $"{dividend} % {divisor} = {actual}");
- Assert.AreEqual(expectedString, actualString, $"{dividend} % {divisor} = {actual}");
+ [Test]
+ public void TestDivide_50by2_001()
+ {
+ var actual = BigDecimal.Divide(BigDecimal.Parse("50"), BigDecimal.Parse("2"));
+ string expected = "25";
- TestContext.WriteLine($"{dividend} % {divisor} = {actual}");
- }
+ ClassicAssert.AreEqual(expected.ToString(), actual.ToString());
+ }
- [Test]
- public void TestMod6()
- {
- BigDecimal NumberBDParse = BigDecimal.Parse("-157766400");
- TestContext.WriteLine($"{NumberBDParse}");
- BigDecimal actual = NumberBDParse % 60;
- TestContext.WriteLine(actual);
- Assert.AreEqual("0", actual.ToString(), "BigDecimal.Parse"); // ???
-
- TestContext.WriteLine("---");
-
- BigDecimal NumberBD = -157766400;
- TestContext.WriteLine($"{NumberBD}");
- actual = NumberBD % 60;
- TestContext.WriteLine(actual);
- Assert.AreEqual("0", actual.ToString(), "BigDecimal implicit cast to decimal or double"); // 60 (incorrect)
-
- TestContext.WriteLine("---");
-
- BigInteger NumberBI = -157766400;
- TestContext.WriteLine($"{NumberBI}");
- BigInteger actual2 = NumberBI % 60;
- TestContext.WriteLine(actual2);
- Assert.AreEqual("0", actual2.ToString(), "BigInteger"); // 0 (correct)
-
- TestContext.WriteLine("---");
-
- float NumberFL = -157766400;
- TestContext.WriteLine($"{NumberFL}");
- float actual3 = NumberFL % 60;
- TestContext.WriteLine(actual3);
- Assert.AreEqual("0", Math.Abs(actual3).ToString(), "float"); // 0 (correct)
- }
-
- [Test]
- public void TestMultiply()
- {
- var p = BigDecimal.Parse("-6122421090493547576937037317561418841225758554253106999");
- var actual = p * new BigDecimal(BigInteger.Parse("9996013524558575221488141657137307396757453940901242216"), -34);
- var expected = new BigDecimal(BigInteger.Parse("-61199804023616162130466158636504166524066189692091806226423722790866248079929810268920239053350152436663869784"));
-
- var matches = expected.ToString().Equals(actual.ToString().Replace(this.Format.NumberDecimalSeparator, ""), StringComparison.Ordinal);
-
- Assert.IsTrue(matches);
- }
-
- [Test]
- public void TestMultiply1()
- {
- var p = BigDecimal.Parse("6122421090493547576937037317561418841225758554253106999");
- var q = BigDecimal.Parse("5846418214406154678836553182979162384198610505601062333");
- var expected = "35794234179725868774991807832568455403003778024228226193532908190484670252364677411513516111204504060317568667";
-
- var actual = BigDecimal.Multiply(p, q);
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
- [Test]
- public void TestMultiply2()
- {
- var p = BigDecimal.Parse("6122421090493547576937037317561418841225758554253106999");
- var actual = p * p;
- var expected = "37484040009320200288159018961010536937973891182532366282540247408867702983313960194873589374267102044942786001";
-
- Assert.AreEqual(expected, actual.ToString());
- }
-
-
- [Test]
- public void TestExponentiation1()
- {
- double exp = 0.052631578947368421d;
- double phi = (1.0d + Math.Sqrt(5)) / 2.0d;
-
- BigDecimal result1 = BigDecimal.Pow(9.0d, 0.5d);
- BigDecimal result2 = BigDecimal.Pow(16.0d, 0.25d);
- string expected1 = "3";
- string expected2 = "2";
-
- BigDecimal result3 = BigDecimal.Pow(phi, 13);
- string expected3 = "521.001919378725";
-
- BigDecimal result4 = BigDecimal.Pow(1162261467, exp);
- BigDecimal result5 = BigDecimal.Pow(9349, exp);
- string expected4 = "3";
- string expected5 = "1.61803398777557";
-
- BigDecimal result6 = BigDecimal.Pow(1.618034d, 16.000000256d);
- BigDecimal result7 = BigDecimal.Pow(phi, 20.0000000128d);
- string expected6 = "2207.00006429941";
- string expected7 = "15127.0000270679";
+ [Test]
+ public void TestDivide_OneOver()
+ {
+ var numerator = BigDecimal.One;
+ var val = TestBigDecimalHelper.PrepareValue("0.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068", Format);
+ var denominator = BigDecimal.Parse(val);
- BigDecimal result8 = BigDecimal.Pow(29192926025390625d, 0.07142857142857142d);
- string expected8 = "14.999999999999998";
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 100;
- Assert.AreEqual(expected1, result1.ToString());
- Assert.AreEqual(expected2, result2.ToString());
- Assert.AreEqual(expected3, result3.ToString().Substring(0, 16));
- Assert.AreEqual(expected4, result4.ToString());
- Assert.AreEqual(expected5, result5.ToString().Substring(0, 16));
- Assert.AreEqual(expected6, result6.ToString().Substring(0, 16));
- Assert.AreEqual(expected7, result7.ToString().Substring(0, 16));
- Assert.AreEqual(expected8, result8.ToString());
- }
+ var actual1 = BigDecimal.One / denominator;
+ var actual2 = numerator / denominator;
+ var actual3 = BigDecimal.Divide(BigDecimal.One, denominator);
+ var actual4 = BigDecimal.Divide(numerator, denominator);
- [Test]
- public void TestExponentiation2()
- {
- BigDecimal result1 = BigDecimal.Pow(new BigDecimal(16), new BigInteger(16));
- BigDecimal result2 = BigDecimal.Pow(new BigDecimal(101), new BigInteger(13));
- string expected1 = "18446744073709551616";
- string expected2 = "113809328043328941786781301";
+ string expectedString = TestBigDecimalHelper.PrepareValue("7.062513305931045769793005152570558042734310025145531333998316873555903337580056083503977475916243946", Format);
+ var expected = BigDecimal.Parse(expectedString);
- BigDecimal result3 = BigDecimal.Pow(new BigDecimal(0.25), 2);
- string expected3 = "0.0625";
+ ClassicAssert.AreEqual(expectedString, actual1.ToString(), "expectedString != actual1.ToString()");
+ ClassicAssert.AreEqual(expected.ToString(), actual1.ToString(), "expected.ToString() != actual1.ToString()");
+ ClassicAssert.AreEqual(expected, actual1, "expected != ( BigDecimal.One / denominator )");
+ ClassicAssert.AreEqual(expected, actual2, "expected != ( numerator / denominator )");
+ ClassicAssert.AreEqual(expected, actual3, "expected != ( BigDecimal.Divide(BigDecimal.One, denominator) )");
+ ClassicAssert.AreEqual(expected, actual4, "expected != ( BigDecimal.Divide(numerator, denominator) )");
- Assert.AreEqual(expected1, result1.ToString());
- Assert.AreEqual(expected2, result2.ToString());
- Assert.AreEqual(expected3, result3.ToString());
- }
+ BigDecimal.Precision = savePrecision;
+ }
- [Test]
- public void TestNegate()
- {
- string expected = TestBigDecimalHelper.PrepareValue("-1.375", this.Format);
- var actual = BigDecimal.Negate(BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("1.375", this.Format)));
+ [Test]
+ public void TestFloor_Zero()
+ {
+ string expected = "0";
+ var actual0 = BigDecimal.Floor(BigDecimal.Zero);
+ ClassicAssert.AreEqual(expected, actual0.ToString(), "Floor(0)");
- Assert.AreEqual(expected, actual.ToString());
- }
+ var actual1 = BigDecimal.Floor(BigDecimal.Parse("0.01"));
+ ClassicAssert.AreEqual(expected, actual1.ToString(), "Floor(0.01)");
- [Test]
- public void TestReciprocal001()
- {
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 10;
-
- var dividend = new BigDecimal(1);
- var divisor = new BigDecimal(3);
+ var actual2 = BigDecimal.Floor(BigDecimal.Parse("-0.01"));
+ ClassicAssert.AreEqual("-1", actual2.ToString(), "Floor(-0.01)");
+ }
- var actual = BigDecimal.Divide(dividend, divisor);
+ [Test]
+ public void TestFloor001()
+ {
+ string expected = "3";
+ var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.14159265", Format));
+ var floor = BigDecimal.Floor(start);
- //var expected = BigDecimal.Parse( "0.3333333333333333" );
- string expected = TestBigDecimalHelper.PrepareValue("0.3333333333", this.Format);
+ ClassicAssert.AreEqual(expected, floor.ToString());
+ }
- Assert.AreEqual(expected, actual.ToString());
-
- BigDecimal.Precision = savePrecision;
- }
+ [Test]
+ public void TestFloor002()
+ {
+ string expected = "-4";
+ var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.14159265", Format));
+ var floor = BigDecimal.Floor(start);
- [Test]
- public void TestReciprocal002()
- {
+ ClassicAssert.AreEqual(expected, floor.ToString());
+ }
- // 1/2 = 0.5
- var expected = TestBigDecimalHelper.PrepareValue("0.5", this.Format);
-
- var dividend = new BigDecimal(1);
- var divisor = new BigDecimal(2);
-
- var actual = BigDecimal.Divide(dividend, divisor);
-
- Assert.AreEqual(expected, actual.ToString());
- }
+ [Test]
+ public void TestFloor003()
+ {
+ var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.14159265", Format));
+ var floor = BigDecimal.Floor(start);
- [Test]
- public void TestReciprocal003()
- {
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 15;
-
- //var expected = BigDecimal.Parse( "12.000000000000005" );
- string expected = TestBigDecimalHelper.PrepareValue("12.000000000000004", this.Format);
-
- var dividend = new BigDecimal(1);
- var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0833333333333333", this.Format));
-
- var actual = BigDecimal.Divide(dividend, divisor);
+ ClassicAssert.AreEqual(BigDecimal.MinusOne, floor);
+ }
- Assert.AreEqual(expected, actual.ToString());
-
- BigDecimal.Precision = savePrecision;
- }
-
- [Test]
- public void TestReciprocal004()
- {
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 14;
+ [Test]
+ public void TestFloor004()
+ {
+ var start = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.14159265", Format));
+ var floor = BigDecimal.Floor(start);
+ var actual = BigDecimal.Parse(floor.ToString());
- // 2/0.63661977236758 == 3.1415926535898
- string expected = TestBigDecimalHelper.PrepareValue("3.1415926535897", this.Format);
+ ClassicAssert.AreEqual(BigDecimal.Zero, actual);
+ }
- var dividend = new BigDecimal(2);
- var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.63661977236758", this.Format));
+ [Test]
+ public void TestFloor005()
+ {
+ float start_compare = -2629440f;
+ BigDecimal start_test = BigDecimal.Parse("-2629440");
- var actual = BigDecimal.Divide(dividend, divisor);
-
- Assert.AreEqual(expected, actual.ToString());
+ BigDecimal actualFloor = BigDecimal.Floor(start_test);
- BigDecimal.Precision = savePrecision;
- }
+ float expectedFloor = (float)Math.Floor(start_compare);
- [Test]
- public void TestSqrt()
- {
- var expected = BigInteger.Parse("8145408529");
- var expectedSquared = BigInteger.Parse("66347680104305943841");
+ ClassicAssert.AreEqual(expectedFloor.ToString(), actualFloor.ToString());
+ }
- var squared = expected * expected;
- TestContext.WriteLine($"{expected} squared is {squared}.");
- Assert.AreEqual(squared, expectedSquared);
- var actual = squared.NthRoot(2, out _);
+ [Test]
+ public void TestCeiling005()
+ {
+ float start_compare = -2629440f;
+ BigDecimal start_test = BigDecimal.Parse("-2629440");
- Assert.AreEqual(expected, actual, "sqrt(66347680104305943841) = 8145408529");
- TestContext.WriteLine($"And {squared} squared root is {actual}.");
- }
+ BigDecimal actualCeil = BigDecimal.Ceiling(start_test);
- [Test]
- public void TestSubtraction001()
- {
- var number = BigDecimal.Parse("4294967295");
- BigDecimal expected = BigDecimal.Parse("2147483648");
+ float expectedCeil = (float)Math.Ceiling(start_compare);
- var actual = number - 0x7FFFFFFF;
+ ClassicAssert.AreEqual(expectedCeil.ToString(), actualCeil.ToString());
+ }
- Assert.AreEqual(expected, actual);
- }
- /*
- [Test]
- [NonParallelizable]
- public void TestSubtractionRandom(
- [Random(-8.98846567431158E+300D, 8.98846567431158E+300D, 3)] Double b,
- [Random(-8.98846567431158E+300D, 8.98846567431158E+300D, 3)] Double d)
- {
- var strB = $"{b:R}";
- var strD = $"{d:R}";
+ [Test]
+ public void TestMod1()
+ {
- TestContext.WriteLine($"{b:R} = {strB}");
- TestContext.WriteLine($"{d:R} = {strD}");
+ // 31 % 19 = 12
+ BigDecimal dividend = 31;
+ BigDecimal divisor = 19;
+ BigDecimal expected = 12;
- var bigB = BigDecimal.Parse(strB);
- var bigD = BigDecimal.Parse(strD);
+ var actual = BigDecimal.Mod(dividend, divisor);
- TestContext.WriteLine(Environment.NewLine);
- TestContext.WriteLine($"bigB = {bigB}");
- TestContext.WriteLine($"bigD = {bigD}");
+ ClassicAssert.AreEqual(expected, actual);
+ }
- var result1 = BigDecimal.Subtract(bigB, bigD);
- var result2 = bigB - bigD;
+ [Test]
+ public void TestMod2()
+ {
+ // 1891 % 31 = 0
+ BigDecimal dividend = 1891;
+ BigDecimal divisor = 31;
+ BigDecimal expected = 0;
- Assert.AreEqual(result1, result2);
- }
- */
- [Test]
- public void TestSubtraction002()
- {
- BigDecimal high = 100.1m;
- BigDecimal low = 25.1m;
+ var actual = BigDecimal.Mod(dividend, divisor);
- string expected = "75";
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.1 - 25.1 should equal 75.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction003()
- {
- BigDecimal high = (Double)100.3;
- BigDecimal low = (Double)25.1;
-
- string expected = TestBigDecimalHelper.PrepareValue("75.2", this.Format);
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.3 - 25.1 should equal 75.2\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction004()
- {
- BigDecimal high = (Decimal)100.3;
- BigDecimal low = (Decimal)0.3;
-
- string expected = "100";
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.3 - 0.3 should equal 100.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction005()
- {
- BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.001", this.Format));
- BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.1", this.Format));
-
- string expected = TestBigDecimalHelper.PrepareValue("74.901", this.Format);
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.001 - 25.1 should equal 74.901.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction006()
- {
- BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.1", this.Format));
- BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.001", this.Format));
-
- string expected = TestBigDecimalHelper.PrepareValue("75.099", this.Format);
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.1 - 25.001 should equal 75.099.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction007()
- {
- BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.0648646786764", this.Format));
- BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.156379516", this.Format));
-
- string expected = TestBigDecimalHelper.PrepareValue("74.9084851626764", this.Format);
- BigDecimal actual = BigDecimal.Subtract(high, low);
-
- Assert.AreEqual(expected, actual.ToString(), $"100.0648646786764 - 25.156379516 should equal 74.9084851626764.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
-
- [Test]
- public void TestSubtraction008()
- {
- BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.100001", this.Format));
- BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.101", this.Format));
-
- string expected = TestBigDecimalHelper.PrepareValue("-0.000999", this.Format);
- BigDecimal actual = BigDecimal.Subtract(high, low);
+ ClassicAssert.AreEqual(expected, actual);
+ }
- Assert.AreEqual(expected, actual.ToString(), $"0.100001 - 0.101 should equal -0.000999.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
- }
+ [Test]
+ public void TestMod3()
+ {
- [Test]
- public void TestSubtraction009()
- {
- BigDecimal high = BigDecimal.Parse("240");
- BigDecimal low = BigDecimal.Parse("240");
- string expected = "0";
-
- BigDecimal result = BigDecimal.Subtract(high, low);
- string actual = result.ToString();
+ // 6661 % 60 = 1
+ BigDecimal dividend = 6661;
+ BigDecimal divisor = 60;
+ BigDecimal expected = 1;
- Assert.AreEqual(expected, actual.ToString(), $"240 - 240 should equal 0. Instead got: {TestBigDecimalHelper.GetInternalValues(result)}");
- }
+ var actual = BigDecimal.Mod(dividend, divisor);
- [Test]
- public void TestSquareRoot001()
- {
- BigDecimal value = BigDecimal.Parse("5");
- Int32 root = 2;
- Int32 precision = 30;
+ ClassicAssert.AreEqual(expected, actual);
+ }
- string expected = TestBigDecimalHelper.PrepareValue("2.236067977499789696409173668731", this.Format);
- BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+ [Test]
+ public void TestMod4()
+ {
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ //NOTE This test fails if the values are Doubles instead of Decimals.
- [Test]
- public void TestSquareRoot002()
- {
- BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0981898234602005160423505923443092051160170637298815793320356006279679013343110872318753144061611219225635804218963505102948529140625", this.Format));
- // "0.0981898234602005160423505923443092051160170637298815793320356006279679013343110872318753144061611219225635804218963505102948529140625");
- Int32 root = 2;
- Int32 precision = 50;
+ // 31 % 3.66666 = 1.66672
+ BigDecimal dividend = 31m;
+ BigDecimal divisor = 3.66666m;
+ BigDecimal expected = 1.66672m;
- string expected = TestBigDecimalHelper.PrepareValue("0.31335255457742883389571245385500659019295986107402", this.Format);
- BigDecimal result = BigDecimal.NthRoot(value, root, precision);
+ var actual = BigDecimal.Mod(dividend, divisor);
- BigDecimal actual = BigDecimal.Round(result, precision);
+ ClassicAssert.AreEqual(expected, actual);
+ }
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
-
- [Test]
- public void TestSquareRoot003()
- {
- BigDecimal value = BigDecimal.Parse("9818982346020051604235059234430920511601706372988157933203560062796790133431108723187531440616112192256358042189635051029485291406258555");
- Int32 root = 2;
- Int32 precision = 135;
+ [Test]
+ public void TestMod5()
+ {
+ // 240 % 2 = 0
+ BigDecimal dividend = 240;
+ BigDecimal divisor = 2;
+ string expectedString = "0";
+ BigDecimal expected = 0;
- string expected = TestBigDecimalHelper.PrepareValue("99090778309689603548815656125983317432034385902667809355596183348807.410596077216611169596571667988328091906450145578959539307248420211367976153463820323404307029425296409616398791728069401888988546189821", this.Format);
- BigDecimal result = BigDecimal.NthRoot(value, root, precision);
+ var actual = BigDecimal.Mod(dividend, divisor);
+ string actualString = actual.ToString();
- BigDecimal actual = BigDecimal.Round(result, precision);
-
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ ClassicAssert.AreEqual(expected, actual, $"{dividend} % {divisor} = {actual}");
+ ClassicAssert.AreEqual(expectedString, actualString, $"{dividend} % {divisor} = {actual}");
- [Test]
- public void TestSquareRoot004()
- {
- BigDecimal value = BigDecimal.Parse("9818982346020051604235059234430920511601706372988157933203560062796790133431108723187531440616112192");
- Int32 root = 2;
- Int32 precision = 135;
+ TestContext.WriteLine($"{dividend} % {divisor} = {actual}");
+ }
- string expected = TestBigDecimalHelper.PrepareValue("99090778309689603548815656125983317432034385902667.809355596183348807410596077216611169596571667988326798354988734930975117508103720966474578967977953788831616628961714711683020533839237", this.Format);
- BigDecimal result = BigDecimal.NthRoot(value, root, precision);
-
- BigDecimal actual = BigDecimal.Round(result, precision);
+ [Test]
+ public void TestMod6()
+ {
+ BigDecimal NumberBDParse = BigDecimal.Parse("-157766400");
+ TestContext.WriteLine($"{NumberBDParse}");
+ BigDecimal actual = NumberBDParse % 60;
+ TestContext.WriteLine(actual);
+ ClassicAssert.AreEqual("0", actual.ToString(), "BigDecimal.Parse"); // ???
+
+ TestContext.WriteLine("---");
+
+ BigDecimal NumberBD = -157766400;
+ TestContext.WriteLine($"{NumberBD}");
+ actual = NumberBD % 60;
+ TestContext.WriteLine(actual);
+ ClassicAssert.AreEqual("0", actual.ToString(), "BigDecimal implicit cast to decimal or double"); // 60 (incorrect)
+
+ TestContext.WriteLine("---");
+
+ BigInteger NumberBI = -157766400;
+ TestContext.WriteLine($"{NumberBI}");
+ BigInteger actual2 = NumberBI % 60;
+ TestContext.WriteLine(actual2);
+ ClassicAssert.AreEqual("0", actual2.ToString(), "BigInteger"); // 0 (correct)
+
+ TestContext.WriteLine("---");
+
+ float NumberFL = -157766400;
+ TestContext.WriteLine($"{NumberFL}");
+ float actual3 = NumberFL % 60;
+ TestContext.WriteLine(actual3);
+ ClassicAssert.AreEqual("0", Math.Abs(actual3).ToString(), "float"); // 0 (correct)
+ }
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ [Test]
+ public void TestMultiply()
+ {
+ var p = BigDecimal.Parse("-6122421090493547576937037317561418841225758554253106999");
+ var actual = p * new BigDecimal(BigInteger.Parse("9996013524558575221488141657137307396757453940901242216"), -34);
+ var expected = new BigDecimal(BigInteger.Parse("-61199804023616162130466158636504166524066189692091806226423722790866248079929810268920239053350152436663869784"));
- [Test]
- public void TestSquareRoot_25_001()
- {
- BigDecimal value = BigDecimal.Parse("25");
- Int32 root = 2;
- Int32 precision = 18;
-
- string expected = "5";
- BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+ var matches = expected.ToString().Equals(actual.ToString().Replace(Format.NumberDecimalSeparator, ""), StringComparison.Ordinal);
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ ClassicAssert.IsTrue(matches);
+ }
- [Test]
- public void TestSquareRoot_25_002()
- {
-
- BigDecimal value = new BigDecimal(-25);
- Int32 root = 2;
- Int32 precision = 18;
+ [Test]
+ public void TestMultiply1()
+ {
+ var p = BigDecimal.Parse("6122421090493547576937037317561418841225758554253106999");
+ var q = BigDecimal.Parse("5846418214406154678836553182979162384198610505601062333");
+ var expected = "35794234179725868774991807832568455403003778024228226193532908190484670252364677411513516111204504060317568667";
- BigDecimal actual;
- TestDelegate testDelegate = new TestDelegate(() => actual = BigDecimal.NthRoot(value, root, precision));
+ var actual = BigDecimal.Multiply(p, q);
- Assert.Throws(typeof(ArgumentException), testDelegate);
- }
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- [Test]
- public void TestSquareRootOfDecimal()
- {
- BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.5", this.Format));
- Int32 root = 2;
- Int32 precision = 30;
+ [Test]
+ public void TestMultiply2()
+ {
+ var p = BigDecimal.Parse("6122421090493547576937037317561418841225758554253106999");
+ var actual = p * p;
+ var expected = "37484040009320200288159018961010536937973891182532366282540247408867702983313960194873589374267102044942786001";
- string expected = TestBigDecimalHelper.PrepareValue("0.707106781186547524400844362104", this.Format);
- BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
- [Test]
- public void TestNthRoot()
- {
- BigDecimal value = BigDecimal.Parse("3");
- Int32 root = 3;
- Int32 precision = 50;
+ [Test]
+ public void TestExponentiation1()
+ {
+ double exp = 0.052631578947368421d;
+ double phi = (1.0d + Math.Sqrt(5)) / 2.0d;
+
+ BigDecimal result1 = BigDecimal.Pow(9.0d, 0.5d);
+ BigDecimal result2 = BigDecimal.Pow(16.0d, 0.25d);
+ string expected1 = "3";
+ string expected2 = "2";
+
+ BigDecimal result3 = BigDecimal.Pow(phi, 13);
+ string expected3 = "521.001919378725";
+
+ BigDecimal result4 = BigDecimal.Pow(1162261467, exp);
+ BigDecimal result5 = BigDecimal.Pow(9349, exp);
+ string expected4 = "3";
+ string expected5 = "1.61803398777557";
+
+ BigDecimal result6 = BigDecimal.Pow(1.618034d, 16.000000256d);
+ BigDecimal result7 = BigDecimal.Pow(phi, 20.0000000128d);
+ string expected6 = "2207.00006429941";
+ string expected7 = "15127.0000270679";
+
+ BigDecimal result8 = BigDecimal.Pow(29192926025390625d, 0.07142857142857142d);
+ string expected8 = "14.999999999999998";
+
+ ClassicAssert.AreEqual(expected1, result1.ToString());
+ ClassicAssert.AreEqual(expected2, result2.ToString());
+ ClassicAssert.AreEqual(expected3, result3.ToString()[..16]);
+ ClassicAssert.AreEqual(expected4, result4.ToString());
+ ClassicAssert.AreEqual(expected5, result5.ToString()[..16]);
+ ClassicAssert.AreEqual(expected6, result6.ToString()[..16]);
+ ClassicAssert.AreEqual(expected7, result7.ToString()[..16]);
+ ClassicAssert.AreEqual(expected8, result8.ToString());
+ }
- string expected = TestBigDecimalHelper.PrepareValue("1.44224957030740838232163831078010958839186925349935", this.Format);
- BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+ [Test]
+ public void TestExponentiation2()
+ {
+ BigDecimal result1 = BigDecimal.Pow(new BigDecimal(16), new BigInteger(16));
+ BigDecimal result2 = BigDecimal.Pow(new BigDecimal(101), new BigInteger(13));
+ string expected1 = "18446744073709551616";
+ string expected2 = "113809328043328941786781301";
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ BigDecimal result3 = BigDecimal.Pow(new BigDecimal(0.25), 2);
+ string expected3 = "0.0625";
- [Test]
- public void TestNthRootOfDecimal()
- {
- BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.03", this.Format));
- Int32 root = 3;
- Int32 precision = 50;
+ ClassicAssert.AreEqual(expected1, result1.ToString());
+ ClassicAssert.AreEqual(expected2, result2.ToString());
+ ClassicAssert.AreEqual(expected3, result3.ToString());
+ }
- string expected = TestBigDecimalHelper.PrepareValue("0.31072325059538588668776624275223863628549068290674", this.Format);
- BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+ [Test]
+ public void TestNegate()
+ {
+ string expected = TestBigDecimalHelper.PrepareValue("-1.375", Format);
+ var actual = BigDecimal.Negate(BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("1.375", Format)));
- Assert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
- }
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
- [Test]
- public void TestGreaterThan001()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.002", this.Format));
+ [Test]
+ public void TestReciprocal001()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 10;
- bool actual = left > right;
- bool expected = true;
+ var dividend = new BigDecimal(1);
+ var divisor = new BigDecimal(3);
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
+ var actual = BigDecimal.Divide(dividend, divisor);
- [Test]
- public void TestGreaterThan002()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000002", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000001", this.Format));
+ //var expected = BigDecimal.Parse( "0.3333333333333333" );
+ string expected = TestBigDecimalHelper.PrepareValue("0.3333333333", Format);
- bool actual = left > right;
- bool expected = true;
+ ClassicAssert.AreEqual(expected, actual.ToString());
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
+ BigDecimal.Precision = savePrecision;
+ }
+
+ [Test]
+ public void TestReciprocal002()
+ {
+
+ // 1/2 = 0.5
+ var expected = TestBigDecimalHelper.PrepareValue("0.5", Format);
+
+ var dividend = new BigDecimal(1);
+ var divisor = new BigDecimal(2);
+
+ var actual = BigDecimal.Divide(dividend, divisor);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+ }
+
+ [Test]
+ public void TestReciprocal003()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 15;
- [Test]
- public void TestGreaterThanOrEqualTo001()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", this.Format));
+ //var expected = BigDecimal.Parse( "12.000000000000005" );
+ string expected = TestBigDecimalHelper.PrepareValue("12.000000000000004", Format);
+
+ var dividend = new BigDecimal(1);
+ var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0833333333333333", Format));
+
+ var actual = BigDecimal.Divide(dividend, divisor);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+
+ BigDecimal.Precision = savePrecision;
+ }
+
+ [Test]
+ public void TestReciprocal004()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 14;
+
+ // 2/0.63661977236758 == 3.1415926535898
+ string expected = TestBigDecimalHelper.PrepareValue("3.1415926535897", Format);
+
+ var dividend = new BigDecimal(2);
+ var divisor = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.63661977236758", Format));
+
+ var actual = BigDecimal.Divide(dividend, divisor);
+
+ ClassicAssert.AreEqual(expected, actual.ToString());
+
+ BigDecimal.Precision = savePrecision;
+ }
+
+ [Test]
+ public void TestSqrt()
+ {
+ var expected = BigInteger.Parse("8145408529");
+ var expectedSquared = BigInteger.Parse("66347680104305943841");
+
+ var squared = expected * expected;
+ TestContext.WriteLine($"{expected} squared is {squared}.");
+ ClassicAssert.AreEqual(squared, expectedSquared);
+ var actual = squared.NthRoot(2, out _);
+
+ ClassicAssert.AreEqual(expected, actual, "sqrt(66347680104305943841) = 8145408529");
+ TestContext.WriteLine($"And {squared} squared root is {actual}.");
+ }
+
+ [Test]
+ public void TestSubtraction001()
+ {
+ var number = BigDecimal.Parse("4294967295");
+ BigDecimal expected = BigDecimal.Parse("2147483648");
+
+ var actual = number - 0x7FFFFFFF;
+
+ ClassicAssert.AreEqual(expected, actual);
+ }
+ /*
+ [Test]
+ [NonParallelizable]
+ public void TestSubtractionRandom(
+ [Random(-8.98846567431158E+300D, 8.98846567431158E+300D, 3)] double b,
+ [Random(-8.98846567431158E+300D, 8.98846567431158E+300D, 3)] double d)
+ {
+ var strB = $"{b:R}";
+ var strD = $"{d:R}";
+
+ TestContext.WriteLine($"{b:R} = {strB}");
+ TestContext.WriteLine($"{d:R} = {strD}");
+
+ var bigB = BigDecimal.Parse(strB);
+ var bigD = BigDecimal.Parse(strD);
+
+ TestContext.WriteLine(Environment.NewLine);
+ TestContext.WriteLine($"bigB = {bigB}");
+ TestContext.WriteLine($"bigD = {bigD}");
+
+ var result1 = BigDecimal.Subtract(bigB, bigD);
+ var result2 = bigB - bigD;
+
+ ClassicAssert.AreEqual(result1, result2);
+ }
+ */
+ [Test]
+ public void TestSubtraction002()
+ {
+ BigDecimal high = 100.1m;
+ BigDecimal low = 25.1m;
+
+ string expected = "75";
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.1 - 25.1 should equal 75.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction003()
+ {
+ BigDecimal high = (double)100.3;
+ BigDecimal low = (double)25.1;
+
+ string expected = TestBigDecimalHelper.PrepareValue("75.2", Format);
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.3 - 25.1 should equal 75.2\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction004()
+ {
+ BigDecimal high = (decimal)100.3;
+ BigDecimal low = (decimal)0.3;
+
+ string expected = "100";
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.3 - 0.3 should equal 100.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction005()
+ {
+ BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.001", Format));
+ BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.1", Format));
+
+ string expected = TestBigDecimalHelper.PrepareValue("74.901", Format);
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.001 - 25.1 should equal 74.901.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction006()
+ {
+ BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.1", Format));
+ BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.001", Format));
+
+ string expected = TestBigDecimalHelper.PrepareValue("75.099", Format);
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.1 - 25.001 should equal 75.099.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction007()
+ {
+ BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("100.0648646786764", Format));
+ BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("25.156379516", Format));
+
+ string expected = TestBigDecimalHelper.PrepareValue("74.9084851626764", Format);
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"100.0648646786764 - 25.156379516 should equal 74.9084851626764.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction008()
+ {
+ BigDecimal high = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.100001", Format));
+ BigDecimal low = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.101", Format));
+
+ string expected = TestBigDecimalHelper.PrepareValue("-0.000999", Format);
+ BigDecimal actual = BigDecimal.Subtract(high, low);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"0.100001 - 0.101 should equal -0.000999.\nHigh: {TestBigDecimalHelper.GetInternalValues(high)}\nLow: {TestBigDecimalHelper.GetInternalValues(low)}\nResult: {TestBigDecimalHelper.GetInternalValues(actual)}");
+ }
+
+ [Test]
+ public void TestSubtraction009()
+ {
+ BigDecimal high = BigDecimal.Parse("240");
+ BigDecimal low = BigDecimal.Parse("240");
+ string expected = "0";
+
+ BigDecimal result = BigDecimal.Subtract(high, low);
+ string actual = result.ToString();
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"240 - 240 should equal 0. Instead got: {TestBigDecimalHelper.GetInternalValues(result)}");
+ }
+
+ [Test]
+ public void TestSquareRoot001()
+ {
+ BigDecimal value = BigDecimal.Parse("5");
+ int root = 2;
+ int precision = 30;
+
+ string expected = TestBigDecimalHelper.PrepareValue("2.236067977499789696409173668731", Format);
+ BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestSquareRoot002()
+ {
+ BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0981898234602005160423505923443092051160170637298815793320356006279679013343110872318753144061611219225635804218963505102948529140625", Format));
+ // "0.0981898234602005160423505923443092051160170637298815793320356006279679013343110872318753144061611219225635804218963505102948529140625");
+ int root = 2;
+ int precision = 50;
+
+ string expected = TestBigDecimalHelper.PrepareValue("0.31335255457742883389571245385500659019295986107402", Format);
+ BigDecimal result = BigDecimal.NthRoot(value, root, precision);
+
+ BigDecimal actual = BigDecimal.Round(result, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestSquareRoot003()
+ {
+ BigDecimal value = BigDecimal.Parse("9818982346020051604235059234430920511601706372988157933203560062796790133431108723187531440616112192256358042189635051029485291406258555");
+ int root = 2;
+ int precision = 135;
+
+ string expected = TestBigDecimalHelper.PrepareValue("99090778309689603548815656125983317432034385902667809355596183348807.410596077216611169596571667988328091906450145578959539307248420211367976153463820323404307029425296409616398791728069401888988546189821", Format);
+ BigDecimal result = BigDecimal.NthRoot(value, root, precision);
+
+ BigDecimal actual = BigDecimal.Round(result, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestSquareRoot004()
+ {
+ BigDecimal value = BigDecimal.Parse("9818982346020051604235059234430920511601706372988157933203560062796790133431108723187531440616112192");
+ int root = 2;
+ int precision = 135;
+
+ string expected = TestBigDecimalHelper.PrepareValue("99090778309689603548815656125983317432034385902667.809355596183348807410596077216611169596571667988326798354988734930975117508103720966474578967977953788831616628961714711683020533839237", Format);
+ BigDecimal result = BigDecimal.NthRoot(value, root, precision);
+
+ BigDecimal actual = BigDecimal.Round(result, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestSquareRoot_25_001()
+ {
+ BigDecimal value = BigDecimal.Parse("25");
+ int root = 2;
+ int precision = 18;
+
+ string expected = "5";
+ BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestSquareRoot_25_002()
+ {
+
+ BigDecimal value = new(-25);
+ int root = 2;
+ int precision = 18;
+
+ BigDecimal actual;
+ TestDelegate testDelegate = new(() => actual = BigDecimal.NthRoot(value, root, precision));
+
+ Assert.Throws(testDelegate);
+ }
+
+ [Test]
+ public void TestSquareRootOfDecimal()
+ {
+ BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.5", Format));
+ int root = 2;
+ int precision = 30;
+
+ string expected = TestBigDecimalHelper.PrepareValue("0.707106781186547524400844362104", Format);
+ BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestNthRoot()
+ {
+ BigDecimal value = BigDecimal.Parse("3");
+ int root = 3;
+ int precision = 50;
+
+ string expected = TestBigDecimalHelper.PrepareValue("1.44224957030740838232163831078010958839186925349935", Format);
+ BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestNthRootOfDecimal()
+ {
+ BigDecimal value = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.03", Format));
+ int root = 3;
+ int precision = 50;
+
+ string expected = TestBigDecimalHelper.PrepareValue("0.31072325059538588668776624275223863628549068290674", Format);
+ BigDecimal actual = BigDecimal.NthRoot(value, root, precision);
+
+ ClassicAssert.AreEqual(expected, actual.ToString(), $"{root}th root of {value} did not return {expected}.");
+ }
+
+ [Test]
+ public void TestGreaterThan001()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.002", Format));
+
+ bool actual = left > right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestGreaterThan002()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000002", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000001", Format));
+
+ bool actual = left > right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestGreaterThanOrEqualTo001()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", Format));
+
+ bool actual = left >= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestGreaterThanOrEqualTo002()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.002", Format));
+
+ bool actual = left >= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestGreaterThanOrEqualTo003()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0001", Format));
+
+ bool actual = left >= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThan001()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-300000.02", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-300000.01", Format));
+
+ bool actual = left < right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThan002()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3000000.00000001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3000000.0000001", Format));
+
+ bool actual = left < right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThanOrEqualTo001()
+ {
+ BigDecimal left = BigDecimal.Parse("3");
+ BigDecimal right = BigDecimal.Parse("3");
+
+ bool actual = left <= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThanOrEqualTo002()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000002", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000001", Format));
+
+ bool actual = left <= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThanOrEqualTo003()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000001", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.00000001", Format));
+
+ bool actual = left <= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestLessThanOrEqualTo004()
+ {
+ BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000000201", Format));
+ BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("30.00000201", Format));
+
+ bool actual = left <= right;
+ bool expected = true;
+
+ ClassicAssert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
+ }
+
+ [Test]
+ public void TestDoubleCasting()
+ {
+ BigDecimal value1 = BigDecimal.Pow(10, -1);
+ BigDecimal value2 = new(99.00000001);
+ BigDecimal res = BigDecimal.Divide(value2, value1);
+
+ double delta = 0.0000001;
+ double expected = 990.0000001;
+ double actual = (double)res;
+
+ ClassicAssert.AreEqual(expected, actual, delta, $"{expected} != {actual}");
+ }
+
+ [Test]
+ public void TestSingleCasting()
+ {
+ BigDecimal value1 = BigDecimal.Pow(10, -1);
+ BigDecimal value2 = new(99.00000001);
+ BigDecimal res = BigDecimal.Divide(value2, value1);
+
+ double delta = 0.0000001;
+ float expected = 990.0000001f;
+ float actual = (float)res;
+
+ ClassicAssert.AreEqual(expected, actual, delta, $"{expected} != {actual}");
+ }
+
+ [Test]
+ public void TestDecimalCasting()
+ {
+ BigDecimal value1 = BigDecimal.Pow(10, -1);
+ BigDecimal value2 = new(99.00000001);
+ BigDecimal res = BigDecimal.Divide(value2, value1);
- bool actual = left >= right;
- bool expected = true;
+ double delta = 0.0000001;
+ double expected = 990.0000001;
+ decimal actual = (decimal)res;
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestGreaterThanOrEqualTo002()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-0.002", this.Format));
-
- bool actual = left >= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestGreaterThanOrEqualTo003()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("0.0001", this.Format));
-
- bool actual = left >= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThan001()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-300000.02", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-300000.01", this.Format));
-
- bool actual = left < right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThan002()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3000000.00000001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3000000.0000001", this.Format));
-
- bool actual = left < right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThanOrEqualTo001()
- {
- BigDecimal left = BigDecimal.Parse("3");
- BigDecimal right = BigDecimal.Parse("3");
-
- bool actual = left <= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThanOrEqualTo002()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000002", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000001", this.Format));
-
- bool actual = left <= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThanOrEqualTo003()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.0000001", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("-3.00000001", this.Format));
-
- bool actual = left <= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestLessThanOrEqualTo004()
- {
- BigDecimal left = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("3.000000201", this.Format));
- BigDecimal right = BigDecimal.Parse(TestBigDecimalHelper.PrepareValue("30.00000201", this.Format));
-
- bool actual = left <= right;
- bool expected = true;
-
- Assert.AreEqual(expected, actual, $"{left} > {right} == {expected}");
- }
-
- [Test]
- public void TestDoubleCasting()
- {
- BigDecimal value1 = BigDecimal.Pow(10, -1);
- BigDecimal value2 = new BigDecimal(99.00000001);
- BigDecimal res = BigDecimal.Divide(value2, value1);
-
- double delta = 0.0000001;
- double expected = 990.0000001;
- double actual = (double)res;
-
- Assert.AreEqual(expected, actual, delta, $"{expected} != {actual}");
- }
-
- [Test]
- public void TestSingleCasting()
- {
- BigDecimal value1 = BigDecimal.Pow(10, -1);
- BigDecimal value2 = new BigDecimal(99.00000001);
- BigDecimal res = BigDecimal.Divide(value2, value1);
-
- double delta = 0.0000001;
- Single expected = 990.0000001f;
- Single actual = (Single)res;
-
- Assert.AreEqual(expected, actual, delta, $"{expected} != {actual}");
- }
-
- [Test]
- public void TestDecimalCasting()
- {
- BigDecimal value1 = BigDecimal.Pow(10, -1);
- BigDecimal value2 = new BigDecimal(99.00000001);
- BigDecimal res = BigDecimal.Divide(value2, value1);
-
- double delta = 0.0000001;
- double expected = 990.0000001;
- decimal actual = (decimal)res;
-
- Assert.AreEqual(expected, Convert.ToDouble(actual), delta, $"{expected} != {actual}");
- }
+ ClassicAssert.AreEqual(expected, Convert.ToDouble(actual), delta, $"{expected} != {actual}");
}
}
\ No newline at end of file
diff --git a/TestBigDecimal/TestBigDecimalParsingFractions.cs b/TestBigDecimal/TestBigDecimalParsingFractions.cs
index 0f7d4a5..1b17ed5 100644
--- a/TestBigDecimal/TestBigDecimalParsingFractions.cs
+++ b/TestBigDecimal/TestBigDecimalParsingFractions.cs
@@ -1,54 +1,48 @@
-using System;
-using System.Globalization;
+using System.Globalization;
using System.Threading;
using ExtendedNumerics;
using ExtendedNumerics.Helpers;
using NUnit.Framework;
+using NUnit.Framework.Legacy;
-namespace TestBigDecimal
-{
+namespace BigDecimalTests;
- [TestFixture]
- [Culture("en-US,ru-RU")]
- public class TestBigDecimalParsingFractions
- {
+[TestFixture]
+public class TestBigDecimalParsingFractions
+{
+ private static readonly string String123450000 = "123450000" + new string('0', ushort.MaxValue);
- private static readonly String String123450000;
+ private NumberFormatInfo Format => Thread.CurrentThread.CurrentCulture.NumberFormat;
- private NumberFormatInfo Format { get { return Thread.CurrentThread.CurrentCulture.NumberFormat; } }
+ [Test]
+ public void TestFractional1()
+ {
+ int savePrecision = BigDecimal.Precision;
+ BigDecimal.Precision = 10;
- static TestBigDecimalParsingFractions() => String123450000 = "123450000" + new String('0', UInt16.MaxValue);
+ string test = TestBigDecimalHelper.PrepareValue(" 12.1 / 36.3 ", Format);
+ var val = TestBigDecimalHelper.PrepareValue("0.3333333333", Format);
+ var expected = BigDecimal.Parse(val);
- [Test]
- public void TestFractional1()
+ var parsed = test.TryParseFraction(out var result);
+ if (parsed)
{
- int savePrecision = BigDecimal.Precision;
- BigDecimal.Precision = 10;
-
- string test = TestBigDecimalHelper.PrepareValue(" 12.1 / 36.3 ", this.Format);
- var val = TestBigDecimalHelper.PrepareValue("0.3333333333", this.Format);
- var expected = BigDecimal.Parse(val);
-
- var parsed = test.TryParseFraction(out var result);
- if (parsed)
- {
- Assert.AreEqual(expected, result);
- }
- else
- {
- Assert.Fail("Only 3 3s.");
- }
-
- BigDecimal.Precision = savePrecision;
+ ClassicAssert.AreEqual(expected, result);
}
-
- [Test]
- public void TestNormalize123450000UInt16()
+ else
{
- var expected = new BigDecimal(123450000, UInt16.MaxValue);
- var bd = BigDecimal.Parse(String123450000);
-
- Assert.AreEqual(expected, bd);
+ Assert.Fail("Only 3 3s.");
}
+
+ BigDecimal.Precision = savePrecision;
+ }
+
+ [Test]
+ public void TestNormalize123450000UInt16()
+ {
+ var expected = new BigDecimal(123450000, ushort.MaxValue);
+ var bd = BigDecimal.Parse(String123450000);
+
+ ClassicAssert.AreEqual(expected, bd);
}
}
\ No newline at end of file
diff --git a/TestBigDecimal/TestBigDecimalTrigonometricFunctions.cs b/TestBigDecimal/TestBigDecimalTrigonometricFunctions.cs
index a16e0df..2831ef4 100644
--- a/TestBigDecimal/TestBigDecimalTrigonometricFunctions.cs
+++ b/TestBigDecimal/TestBigDecimalTrigonometricFunctions.cs
@@ -1,24 +1,23 @@
-namespace TestBigDecimal;
-
-using System;
+using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
-using System.Security.Policy;
using System.Text;
using System.Threading;
using ExtendedNumerics;
using ExtendedNumerics.Helpers;
using NUnit.Framework;
using NUnit.Framework.Internal;
+using NUnit.Framework.Legacy;
+
+namespace BigDecimalTests;
[TestFixture]
-[Culture("en-US,ru-RU")]
[NonParallelizable]
public class TestBigDecimalTrigonometricFunctions
{
- private static Dictionary> FunctionDomainTestDictionary = new Dictionary>
+ private static readonly Dictionary> FunctionDomainTestDictionary = new()
{
{ "Arccsc", new Func((radians) => { return (radians > -1 && radians < 1); })},
{ "Arcsec", new Func((radians) => { return (radians > -1 && radians < 1); })},
@@ -28,14 +27,23 @@ public class TestBigDecimalTrigonometricFunctions
{ "Coth", new Func((radians) => { return ( BigDecimal.Normalize(radians).IsZero()); })}
};
+ private const int Precision = 50;
+
+ [OneTimeSetUp]
+ public void SetUp()
+ {
+ BigDecimal.Precision = 50;
+ BigDecimal.AlwaysTruncate = false;
+ }
+
private static void ExecMethod(
BigDecimal input,
- Func testFunc,
- Func comparisonFunc,
- Int32 precision,
- Int32 matchDigits,
- String testDescription,
- String? callerName = null)
+ Func testFunc,
+ Func comparisonFunc,
+ int precision,
+ int matchDigits,
+ string testDescription,
+ string? callerName = null)
{
var matches = FunctionDomainTestDictionary.Where(kvp => callerName.Contains(kvp.Key));
if (matches.Any())
@@ -51,10 +59,10 @@ private static void ExecMethod(
var inputString = $"Input: {input}";
TestContext.WriteLine(inputString);
- var doubleInput = (Double)input;
+ var doubleInput = (double)input;
var doubleExpected = comparisonFunc(doubleInput);
- if (Double.IsNaN(doubleExpected) || Double.IsInfinity(doubleExpected))
+ if (double.IsNaN(doubleExpected) || double.IsInfinity(doubleExpected))
{
TestContext.WriteLine("SKIPPED: The value of this function for this input is undefined.");
@@ -93,10 +101,10 @@ private static void ExecMethod(
actualTruncLength = stringActual.Length;
}
- var truncatedExpecting = stringExpected.Substring(0, expectedTruncLength);
- var truncatedActual = stringActual.Substring(0, actualTruncLength);
+ var truncatedExpecting = stringExpected[..expectedTruncLength];
+ var truncatedActual = stringActual[..actualTruncLength];
- Double.TryParse(truncatedActual, out var doubleActual);
+ double.TryParse(truncatedActual, out var doubleActual);
var testInfo = $"{callerName}: {testDescription}{Environment.NewLine}{inputString}";
TestContext.WriteLine($"[{testInfo}]");
@@ -115,7 +123,7 @@ private static void ExecMethod(
if (displayString.Length > 65)
{
- displayString = new String(displayString.Take(65).ToArray()) + $"... ({displayString.Length - 65} more digits)";
+ displayString = new string(displayString.Take(65).ToArray()) + $"... ({displayString.Length - 65} more digits)";
}
TestContext.WriteLine($"Expecting: {doubleExpected}");
@@ -126,24 +134,22 @@ private static void ExecMethod(
if (testPassed)
{
- Assert.AreEqual(truncatedExpecting, truncatedActual, testInfo);
+ ClassicAssert.AreEqual(truncatedExpecting, truncatedActual, testInfo);
}
else
{
var delta = 0.0001;
- Assert.AreEqual(doubleExpected, doubleActual, delta, testInfo);
+ ClassicAssert.AreEqual(doubleExpected, doubleActual, delta, testInfo);
}
}
- protected static Int32 Precision = 50;
-
protected static void Test_TrigFunction(
- Func testFunc,
- Func comparisonFunc,
- Int32 matchDigits,
- Int32 precision,
- Int32 sign = 1,
- [CallerMemberName] String callerName = "")
+ Func testFunc,
+ Func comparisonFunc,
+ int matchDigits,
+ int precision,
+ int sign = 1,
+ [CallerMemberName] string callerName = "")
{
//var e = BigDecimal.Parse("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427");
@@ -187,7 +193,7 @@ protected static void Test_TrigFunction(
var sgn = sign == -1 ? "-" : "";
- var callerNameBanner = " * ".PadRight(3 + leftPad) + String.Join(" * ", Enumerable.Repeat(callerName, repeatTimes)) +
+ var callerNameBanner = " * ".PadRight(3 + leftPad) + string.Join(" * ", Enumerable.Repeat(callerName, repeatTimes)) +
" * ".PadLeft(3 + rightPad);
TestContext.WriteLine($"<{callerName}>");
@@ -259,14 +265,6 @@ protected static void Test_TrigFunction(
TestContext.WriteLine($"{callerName}>");
}
- [OneTimeSetUp]
- public void SetUp()
- {
- Precision = 50;
- BigDecimal.Precision = Precision;
- BigDecimal.AlwaysTruncate = false;
- }
-
[Test]
public static void Test_Arccos1()
{
@@ -274,7 +272,7 @@ public static void Test_Arccos1()
return;
- static Double Func(Double x)
+ static double Func(double x)
{
var input = Math.Abs(x) % 1;
var output = Math.Atan(Math.Sqrt(1.0d - Math.Pow(input, 2)) / input);
@@ -295,7 +293,7 @@ public static void Test_ArccosNeg1()
return;
- static Double Func(Double x)
+ static double Func(double x)
{
var input = Math.Abs(x) % 1;
var output = Math.Atan(Math.Sqrt(1.0d - Math.Pow(input, 2)) / input);
@@ -317,7 +315,7 @@ public static void Test_Arccot()
return;
- static Double Arccot(Double x) => (Math.PI / 2) - Math.Atan(x);
+ static double Arccot(double x) => (Math.PI / 2) - Math.Atan(x);
}
[Test]
@@ -328,7 +326,7 @@ public static void Test_Arccsc()
return;
- static Double Arccsc(Double x) => Math.Asin(1 / x);
+ static double Arccsc(double x) => Math.Asin(1 / x);
}
[Test]
@@ -339,7 +337,7 @@ public static void Test_Arcsin()
return;
- static Double Asin(Double x)
+ static double Asin(double x)
{
var input = Math.Abs(x) % 1;
@@ -377,7 +375,7 @@ public static void Test_Cot()
return;
// cot = cos / sin
- static Double Cot(Double x) => Math.Cos(x) / Math.Sin(x);
+ static double Cot(double x) => Math.Cos(x) / Math.Sin(x);
}
[Test]
@@ -389,7 +387,7 @@ public static void Test_Coth()
return;
// coth = cosh / sinh
- static Double Coth(Double x) => Math.Cosh(x) / Math.Sinh(x);
+ static double Coth(double x) => Math.Cosh(x) / Math.Sinh(x);
}
[Test]
@@ -401,7 +399,7 @@ public static void Test_Csc()
return;
// csc = 1 / sin
- static Double Csc(Double x) => 1.0d / Math.Sin(x);
+ static double Csc(double x) => 1.0d / Math.Sin(x);
}
[Test]
@@ -413,7 +411,7 @@ public static void Test_Csch()
return;
// csch = 1 / sinh
- static Double Csch(Double x) => 1.0d / Math.Sinh(x);
+ static double Csch(double x) => 1.0d / Math.Sinh(x);
}
[Test]
@@ -431,35 +429,35 @@ public static void Test_LogNatural()
BigDecimal.AlwaysTruncate = false;
BigDecimal.AlwaysNormalize = false;
- Tuple[] questionAnswerValues = new Tuple[]
- {
- new Tuple("0.000000001", "-20.72326583694641115616192309215927786840991339765895678"),
- new Tuple("0.000777", "-7.16007020759662666323925507670903264742195605720039"),
- new Tuple("0.073155", "-2.61517480115201143841773779457374933266203002783292"),
- new Tuple("0.50", "-0.69314718055994530941723212145817656807550013436025"),
- new Tuple("0.57731", "-0.54937589504899085530907326478753939476837352807873"),
- new Tuple("0.65", "-0.43078291609245425738173613457722217087133367822882"),
- new Tuple("0.66", "-0.41551544396166582316156197302289684265750543113712"),
- new Tuple("0.975311", "-0.02499888448682096802712749612065005723649133082854"),
- new Tuple("1.01575", "0.01562725588569907632425960516558987307170263378188"),
- new Tuple("1.22605", "0.20379761971667207412745062140592521363644795989009"),
- new Tuple("1.32835", "0.28393757054679798984200829263947239665011340940242"),
- new Tuple("1.33", "0.28517894223366239707839726596230485167226101362344"),
- new Tuple("1.33165", "0.28641877482725127078618464736645894088507708401364"),
- new Tuple("1.34", "0.29266961396282000105132120845317090344023006032460"),
- new Tuple("1.499", "0.40479821912046065192222057024840164526003750773195"),
- new Tuple("1.50", "0.40546510810816438197801311546434913657199042346249"),
- new Tuple("1.533371", "0.42746857974261091761922170608850145379554792901141"),
- new Tuple("1.7997", "0.58761998434502004983067406992099718856024990068669"),
- new Tuple("1.997755", "0.69202405008497071018459774938875984871561069648604"),
- new Tuple("2.57", "0.94390589890712843031581140539252703641252185172939"),
- new Tuple("3.14159265358900100002000010000001", "1.14472988584914799681491911248160367461176901448316"),
- new Tuple("31.41592653589", "3.44731497884319336251665815374030592476402235854262"),
- new Tuple("3141.592653589", "8.05248516483128473055264106310903433996622533580016"),
- new Tuple("314159.265358900100002000010000001", "12.65765535081937641690487638590342471261727645762702"),
- new Tuple("31415926535.8900100002000010000001", "24.17058081578960483699483365932524575062278390077089"),
- new Tuple("1409368056606849087457015313568.21404846132236496737", "69.42069420694206942069420694206942069420694206942069")
- };
+ Tuple[] questionAnswerValues =
+ [
+ new("0.000000001", "-20.72326583694641115616192309215927786840991339765895678"),
+ new("0.000777", "-7.16007020759662666323925507670903264742195605720039"),
+ new("0.073155", "-2.61517480115201143841773779457374933266203002783292"),
+ new("0.50", "-0.69314718055994530941723212145817656807550013436025"),
+ new("0.57731", "-0.54937589504899085530907326478753939476837352807873"),
+ new("0.65", "-0.43078291609245425738173613457722217087133367822882"),
+ new("0.66", "-0.41551544396166582316156197302289684265750543113712"),
+ new("0.975311", "-0.02499888448682096802712749612065005723649133082854"),
+ new("1.01575", "0.01562725588569907632425960516558987307170263378188"),
+ new("1.22605", "0.20379761971667207412745062140592521363644795989009"),
+ new("1.32835", "0.28393757054679798984200829263947239665011340940242"),
+ new("1.33", "0.28517894223366239707839726596230485167226101362344"),
+ new("1.33165", "0.28641877482725127078618464736645894088507708401364"),
+ new("1.34", "0.29266961396282000105132120845317090344023006032460"),
+ new("1.499", "0.40479821912046065192222057024840164526003750773195"),
+ new("1.50", "0.40546510810816438197801311546434913657199042346249"),
+ new("1.533371", "0.42746857974261091761922170608850145379554792901141"),
+ new("1.7997", "0.58761998434502004983067406992099718856024990068669"),
+ new("1.997755", "0.69202405008497071018459774938875984871561069648604"),
+ new("2.57", "0.94390589890712843031581140539252703641252185172939"),
+ new("3.14159265358900100002000010000001", "1.14472988584914799681491911248160367461176901448316"),
+ new("31.41592653589", "3.44731497884319336251665815374030592476402235854262"),
+ new("3141.592653589", "8.05248516483128473055264106310903433996622533580016"),
+ new("314159.265358900100002000010000001", "12.65765535081937641690487638590342471261727645762702"),
+ new("31415926535.8900100002000010000001", "24.17058081578960483699483365932524575062278390077089"),
+ new("1409368056606849087457015313568.21404846132236496737", "69.42069420694206942069420694206942069420694206942069")
+ ];
LogNaturalTimeElapsed = TimeSpan.Zero;
Console.WriteLine($"Beginning of test...");
@@ -474,7 +472,7 @@ public static void Test_LogNatural()
Console.WriteLine($"");
}
- private static Stopwatch LogNaturalTimer = Stopwatch.StartNew();
+ private static readonly Stopwatch LogNaturalTimer = Stopwatch.StartNew();
private static TimeSpan LogNaturalTimeElapsed = TimeSpan.Zero;
private static void Test_Single(Tuple questionAnswerValue, int precision)
@@ -486,7 +484,7 @@ private static void Test_Single(Tuple questionAnswerValue, int p
BigDecimal result1 = BigDecimal.Ln(input, precision);
LogNaturalTimeElapsed = LogNaturalTimeElapsed.Add(LogNaturalTimer.Elapsed);
- string actual = result1.ToString().Substring(0, expected.Length);
+ string actual = result1.ToString()[..expected.Length];
int matchCount = CharactersMatchCount(expected, actual);
string diffString = HightlightDiffControl(expected, actual);
@@ -500,7 +498,7 @@ private static void Test_Single(Tuple questionAnswerValue, int p
Console.WriteLine($"");
// 48 out of the 50 digits to the right of the decimal point must be correct.
- Assert.GreaterOrEqual(matchCount, 48, $"Expected/Actual:{Environment.NewLine}{expected}{Environment.NewLine}{diffString}");
+ ClassicAssert.GreaterOrEqual(matchCount, 48, $"Expected/Actual:{Environment.NewLine}{expected}{Environment.NewLine}{diffString}");
}
private static int CharactersMatchCount(string expected, string actual)
@@ -534,7 +532,7 @@ private static int CharactersMatchCount(string expected, string actual)
}
- private static Dictionary BoldedNumeralDictionary = new Dictionary()
+ private static readonly Dictionary BoldedNumeralDictionary = new()
{
{'-',"-"},
{'.',"."},
@@ -556,8 +554,8 @@ private static string HightlightDiffControl(string expected, string actual)
int maxLength = Math.Min(actual.Length, expected.Length);
bool matchSoFar = true;
- StringBuilder result = new StringBuilder();
- StringBuilder result2 = new StringBuilder();
+ StringBuilder result = new();
+ StringBuilder result2 = new();
foreach (char c in actual)
{
char? cmp = (index < maxLength) ? expected[index] : null;
@@ -586,8 +584,8 @@ public static void Test_Ln()
var result = BigDecimal.Ln(argument, Precision);
- var actual = new String(result.ToString().Take(Precision).ToArray());
- Assert.AreEqual(expected, actual, nameof(BigDecimal.Ln));
+ var actual = new string(result.ToString().Take(Precision).ToArray());
+ ClassicAssert.AreEqual(expected, actual, nameof(BigDecimal.Ln));
}
@@ -601,8 +599,8 @@ public static void Test_Ln_ChunkValue()
var result = BigDecimal.Ln(argument, Precision);
- var actual = new String(result.ToString().Take(Precision).ToArray());
- Assert.AreEqual(expected, actual, nameof(BigDecimal.Ln));
+ var actual = new string(result.ToString().Take(Precision).ToArray());
+ ClassicAssert.AreEqual(expected, actual, nameof(BigDecimal.Ln));
}
[Test]
@@ -615,8 +613,8 @@ public static void Test_Log10()
var result = BigDecimal.Log10(argument, Precision);
- var actual = new String(result.ToString().Take(Precision).ToArray());
- Assert.AreEqual(expected, actual, nameof(BigDecimal.Log10));
+ var actual = new string(result.ToString().Take(Precision).ToArray());
+ ClassicAssert.AreEqual(expected, actual, nameof(BigDecimal.Log10));
}
[Test]
@@ -630,8 +628,8 @@ public static void Test_Log2()
var result = BigDecimal.Log2(argument, Precision);
- var actual = new String(result.ToString().Take(Precision).ToArray());
- Assert.AreEqual(expected, actual, nameof(BigDecimal.Log2));
+ var actual = new string(result.ToString().Take(Precision).ToArray());
+ ClassicAssert.AreEqual(expected, actual, nameof(BigDecimal.Log2));
}
[Test]
@@ -645,8 +643,8 @@ public static void Test_LogN()
var result = BigDecimal.LogN(@base, argument, Precision);
- var actual = new String(result.ToString().Take(Precision).ToArray());
- Assert.AreEqual(expected, actual, nameof(BigDecimal.LogN));
+ var actual = new string(result.ToString().Take(Precision).ToArray());
+ ClassicAssert.AreEqual(expected, actual, nameof(BigDecimal.LogN));
}
[Test]
@@ -658,7 +656,7 @@ public static void Test_Sec()
return;
// sec = 1 / cos
- static Double Sec(Double x) => 1.0d / Math.Cos(x);
+ static double Sec(double x) => 1.0d / Math.Cos(x);
}
[Test]
@@ -670,7 +668,7 @@ public static void Test_Sech()
return;
// sech = 1 / cosh
- static Double Sech(Double x) => 1.0d / Math.Cosh(x);
+ static double Sech(double x) => 1.0d / Math.Cosh(x);
}
[Test]