// Compute the square root of n using Newton's Method. We start with // an initial estimate for sqrt(n), and then iterate // x_{i+1} = 1/2 * ( x_i + (n / x_i) ) // Result is returned in x func (e *Pslq) Sqrt(n, x *big.Float) { if n == x { panic("need distinct input and output") } if n.Sign() == 0 { x.Set(n) return } else if n.Sign() < 0 { panic("Sqrt of negative number") } prec := n.Prec() // Use the floating point square root as initial estimate nFloat64, _ := n.Float64() x.SetPrec(prec).SetFloat64(math.Sqrt(nFloat64)) // We use t as a temporary variable. There's no need to set its precision // since big.Float values with unset (== 0) precision automatically assume // the largest precision of the arguments when used as the result (receiver) // of a big.Float operation. var t big.Float // Iterate. for { t.Quo(n, x) // t = n / x_i t.Add(x, &t) // t = x_i + (n / x_i) t.Mul(&e.half, &t) // x_{i+1} = 0.5 * t if x.Cmp(&t) == 0 { // Exit loop if no change to result break } x.Set(&t) } }
func fconv(fvp *Mpflt, flag FmtFlag) string { if flag&FmtSharp == 0 { return fvp.Val.Text('b', 0) } // use decimal format for error messages // determine sign f := &fvp.Val var sign string if f.Sign() < 0 { sign = "-" f = new(big.Float).Abs(f) } else if flag&FmtSign != 0 { sign = "+" } // Don't try to convert infinities (will not terminate). if f.IsInf() { return sign + "Inf" } // Use exact fmt formatting if in float64 range (common case): // proceed if f doesn't underflow to 0 or overflow to inf. if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) { return fmt.Sprintf("%s%.6g", sign, x) } // Out of float64 range. Do approximate manual to decimal // conversion to avoid precise but possibly slow Float // formatting. // f = mant * 2**exp var mant big.Float exp := f.MantExp(&mant) // 0.5 <= mant < 1.0 // approximate float64 mantissa m and decimal exponent d // f ~ m * 10**d m, _ := mant.Float64() // 0.5 <= m < 1.0 d := float64(exp) * (math.Ln2 / math.Ln10) // log_10(2) // adjust m for truncated (integer) decimal exponent e e := int64(d) m *= math.Pow(10, d-float64(e)) // ensure 1 <= m < 10 switch { case m < 1-0.5e-6: // The %.6g format below rounds m to 5 digits after the // decimal point. Make sure that m*10 < 10 even after // rounding up: m*10 + 0.5e-5 < 10 => m < 1 - 0.5e6. m *= 10 e-- case m >= 10: m /= 10 e++ } return fmt.Sprintf("%s%.6ge%+d", sign, m, e) }
// compute √z using newton to solve // t² - z = 0 for t func sqrtDirect(z *big.Float) *big.Float { // f(t)/f'(t) = 0.5(t² - z)/t half := big.NewFloat(0.5) f := func(t *big.Float) *big.Float { x := new(big.Float).Mul(t, t) // x = t² x.Sub(x, z) // x = t² - z x.Mul(half, x) // x = 0.5(t² - z) return x.Quo(x, t) // return x = 0.5(t² - z)/t } // initial guess zf, _ := z.Float64() guess := big.NewFloat(math.Sqrt(zf)) return newton(f, guess, z.Prec()) }
// String returns returns a decimal approximation of the Float value. func (x floatVal) String() string { f := x.val // Don't try to convert infinities (will not terminate). if f.IsInf() { return f.String() } // Use exact fmt formatting if in float64 range (common case): // proceed if f doesn't underflow to 0 or overflow to inf. if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) { return fmt.Sprintf("%.6g", x) } // Out of float64 range. Do approximate manual to decimal // conversion to avoid precise but possibly slow Float // formatting. // f = mant * 2**exp var mant big.Float exp := f.MantExp(&mant) // 0.5 <= |mant| < 1.0 // approximate float64 mantissa m and decimal exponent d // f ~ m * 10**d m, _ := mant.Float64() // 0.5 <= |m| < 1.0 d := float64(exp) * (math.Ln2 / math.Ln10) // log_10(2) // adjust m for truncated (integer) decimal exponent e e := int64(d) m *= math.Pow(10, d-float64(e)) // ensure 1 <= |m| < 10 switch am := math.Abs(m); { case am < 1-0.5e-6: // The %.6g format below rounds m to 5 digits after the // decimal point. Make sure that m*10 < 10 even after // rounding up: m*10 + 0.5e-5 < 10 => m < 1 - 0.5e6. m *= 10 e-- case am >= 10: m /= 10 e++ } return fmt.Sprintf("%.6ge%+d", m, e) }
// Exp returns a big.Float representation of exp(z). Precision is // the same as the one of the argument. The function returns +Inf // when z = +Inf, and 0 when z = -Inf. func Exp(z *big.Float) *big.Float { // exp(0) == 1 if z.Sign() == 0 { return big.NewFloat(1).SetPrec(z.Prec()) } // Exp(+Inf) = +Inf if z.IsInf() && z.Sign() > 0 { return big.NewFloat(math.Inf(+1)).SetPrec(z.Prec()) } // Exp(-Inf) = 0 if z.IsInf() && z.Sign() < 0 { return big.NewFloat(0).SetPrec(z.Prec()) } guess := new(big.Float) // try to get initial estimate using IEEE-754 math zf, _ := z.Float64() if zfs := math.Exp(zf); zfs == math.Inf(+1) || zfs == 0 { // too big or too small for IEEE-754 math, // perform argument reduction using // e^{2z} = (e^z)² halfZ := new(big.Float).Mul(z, big.NewFloat(0.5)) halfExp := Exp(halfZ.SetPrec(z.Prec() + 64)) return new(big.Float).Mul(halfExp, halfExp).SetPrec(z.Prec()) } else { // we got a nice IEEE-754 estimate guess.SetFloat64(zfs) } // f(t)/f'(t) = t*(log(t) - z) f := func(t *big.Float) *big.Float { x := new(big.Float) x.Sub(Log(t), z) return x.Mul(x, t) } x := newton(f, guess, z.Prec()) return x }
// compute √z using newton to solve // 1/t² - z = 0 for x and then inverting. func sqrtInverse(z *big.Float) *big.Float { // f(t)/f'(t) = -0.5t(1 - zt²) nhalf := big.NewFloat(-0.5) one := big.NewFloat(1) f := func(t *big.Float) *big.Float { u := new(big.Float) u.Mul(t, t) // u = t² u.Mul(u, z) // u = zt² u.Sub(one, u) // u = 1 - zt² u.Mul(u, nhalf) // u = -0.5(1 - zt²) return new(big.Float).Mul(t, u) // x = -0.5t(1 - zt²) } // initial guess zf, _ := z.Float64() guess := big.NewFloat(1 / math.Sqrt(zf)) // There's another operation after newton, // so we need to force it to return at least // a few guard digits. Use 32. x := newton(f, guess, z.Prec()+32) return x.Mul(z, x).SetPrec(z.Prec()) }