public void AddBigDecimal() { BigDecimal add1 = BigDecimal.Parse("23.456"); BigDecimal add2 = BigDecimal.Parse("3849.235"); BigDecimal sum = add1.Add(add2); Assert.IsTrue(sum.UnscaledValue.ToString().Equals("3872691") && sum.Scale == 3, "the sum of 23.456 + 3849.235 is wrong"); Assert.IsTrue(sum.ToString().Equals("3872.691"), "the sum of 23.456 + 3849.235 is not printed correctly"); BigDecimal add3 = new BigDecimal(12.34E02D); Assert.IsTrue((add1.Add(add3)).ToString().Equals("1257.456"), "the sum of 23.456 + 12.34E02 is not printed correctly"); }
public void ToDouble() { BigDecimal bigDB = new BigDecimal(-1.234E-112); // Commenting out this part because it causes an endless loop (see HARMONY-319 and HARMONY-329) // Assert.IsTrue( // "the double representation of this BigDecimal is not correct", // bigDB.ToDouble() == -1.234E-112); bigDB = new BigDecimal(5.00E-324); Assert.IsTrue(bigDB.ToDouble() == 5.00E-324, "the double representation of bigDecimal is not correct"); bigDB = new BigDecimal(1.79E308); Assert.IsTrue(bigDB.ToDouble() == 1.79E308 && bigDB.Scale == 0, "the double representation of bigDecimal is not correct"); bigDB = new BigDecimal(-2.33E102); Assert.IsTrue(bigDB.ToDouble() == -2.33E102 && bigDB.Scale == 0, "the double representation of bigDecimal -2.33E102 is not correct"); bigDB = new BigDecimal(Double.MaxValue); bigDB = bigDB.Add(bigDB); Assert.IsTrue(bigDB.ToDouble() == Double.PositiveInfinity, "a + number out of the double range should return infinity"); bigDB = new BigDecimal(-Double.MaxValue); bigDB = bigDB.Add(bigDB); Assert.IsTrue(bigDB.ToDouble() == Double.NegativeInfinity, "a - number out of the double range should return neg infinity"); }
public BigComplex Sqrt(MathContext mc) { BigDecimal half = new BigDecimal(2); /* compute l=sqrt(re^2+im^2), then u=sqrt((l+re)/2) * and v= +- sqrt((l-re)/2 as the new real and imaginary parts. */ BigDecimal l = Abs(mc); if (l.CompareTo(BigDecimal.Zero) == 0) { return(new BigComplex(BigMath.ScalePrecision(BigDecimal.Zero, mc), BigMath.ScalePrecision(BigDecimal.Zero, mc))); } BigDecimal u = BigMath.Sqrt(l.Add(Real).Divide(half, mc), mc); BigDecimal v = BigMath.Sqrt(l.Subtract(Real).Divide(half, mc), mc); if (Imaginary.CompareTo(BigDecimal.Zero) >= 0) { return(new BigComplex(u, v)); } else { return(new BigComplex(u, v.Negate())); } }
public BigComplex Multiply(BigComplex oth, MathContext mc) { BigDecimal a = Real.Add(Imaginary).Multiply(oth.Real); BigDecimal b = oth.Real.Add(oth.Imaginary).Multiply(Imaginary); BigDecimal c = oth.Imaginary.Subtract(oth.Real).Multiply(Real); BigDecimal x = a.Subtract(b, mc); BigDecimal y = a.Add(c, mc); return(new BigComplex(x, y)); }