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 BigDecimal ToBigDecimal(MathContext mc) { /* numerator and denominator individually rephrased */ var n = new BigDecimal(Numerator); var d = new BigDecimal(Denominator); /* the problem with n.divide(d,mc) is that the apparent precision might be * smaller than what is set by mc if the value has a precise truncated representation. * 1/4 will appear as 0.25, independent of mc */ return(BigMath.ScalePrecision(n.Divide(d, mc), mc)); }