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)); }
public void SubtractBigDecimal() { BigDecimal sub1 = BigDecimal.Parse("13948"); BigDecimal sub2 = BigDecimal.Parse("2839.489"); BigDecimal result = sub1.Subtract(sub2); Assert.IsTrue(result.ToString().Equals("11108.511") && result.Scale == 3, "13948 - 2839.489 is wrong: " + result); BigDecimal result2 = sub2.Subtract(sub1); Assert.IsTrue(result2.ToString().Equals("-11108.511") && result2.Scale == 3, "2839.489 - 13948 is wrong"); Assert.IsTrue(result.Equals(result2.Negate()), "13948 - 2839.489 is not the negative of 2839.489 - 13948"); sub1 = new BigDecimal(value, 1); sub2 = BigDecimal.Parse("0"); result = sub1.Subtract(sub2); Assert.IsTrue(result.Equals(sub1), "1234590.8 - 0 is wrong"); sub1 = new BigDecimal(1.234E-03); sub2 = new BigDecimal(3.423E-10); result = sub1.Subtract(sub2); Assert.IsTrue(result.ToDouble() == 0.0012339996577, "1.234E-03 - 3.423E-10 is wrong, " + result.ToDouble()); sub1 = new BigDecimal(1234.0123); sub2 = new BigDecimal(1234.0123000); result = sub1.Subtract(sub2); Assert.IsTrue(result.ToDouble() == 0.0, "1234.0123 - 1234.0123000 is wrong, " + result.ToDouble()); }