// smallRat reports whether x would lead to "reasonably"-sized fraction // if converted to a *big.Rat. func smallRat(x *big.Float) bool { if !x.IsInf() { e := x.MantExp(nil) return -maxExp < e && e < maxExp } return false }
func quo(x, y *complexFloat) *complexFloat { z := newComplexFloat() denominator := new(big.Float).SetPrec(prec) c2 := new(big.Float).SetPrec(prec) d2 := new(big.Float).SetPrec(prec) c2.Mul(y.r, y.r) d2.Mul(y.i, y.i) denominator.Add(c2, d2) if denominator.Cmp(zero) == 0 || denominator.IsInf() { return newComplexFloat() } ac := new(big.Float).SetPrec(prec) bd := new(big.Float).SetPrec(prec) ac.Mul(x.r, y.r) bd.Mul(x.i, y.i) bc := new(big.Float).SetPrec(prec) ad := new(big.Float).SetPrec(prec) bc.Mul(x.i, y.r) ad.Mul(x.r, y.i) z.r.Add(ac, bd) z.r.Quo(z.r, denominator) z.i.Add(bc, ad.Neg(ad)) z.i.Quo(z.i, denominator) return z }
// Sqrt returns a big.Float representation of the square root of // z. Precision is the same as the one of the argument. The function // panics if z is negative, returns ±0 when z = ±0, and +Inf when z = // +Inf. func Sqrt(z *big.Float) *big.Float { // panic on negative z if z.Sign() == -1 { panic("Sqrt: argument is negative") } // √±0 = ±0 if z.Sign() == 0 { return big.NewFloat(float64(z.Sign())) } // √+Inf = +Inf if z.IsInf() { return big.NewFloat(math.Inf(+1)) } // Compute √(a·2**b) as // √(a)·2**b/2 if b is even // √(2a)·2**b/2 if b > 0 is odd // √(0.5a)·2**b/2 if b < 0 is odd // // The difference in the odd exponent case is due to the fact that // exp/2 is rounded in different directions when exp is negative. mant := new(big.Float) exp := z.MantExp(mant) switch exp % 2 { case 1: mant.Mul(big.NewFloat(2), mant) case -1: mant.Mul(big.NewFloat(0.5), mant) } // Solving x² - z = 0 directly requires a Quo call, but it's // faster for small precisions. // // Solving 1/x² - z = 0 avoids the Quo call and is much faster for // high precisions. // // Use sqrtDirect for prec <= 128 and sqrtInverse for prec > 128. var x *big.Float if z.Prec() <= 128 { x = sqrtDirect(mant) } else { x = sqrtInverse(mant) } // re-attach the exponent and return return x.SetMantExp(x, exp/2) }
// hypot for big.Float func hypot(p, q *big.Float) *big.Float { // special cases switch { case p.IsInf() || q.IsInf(): return big.NewFloat(math.Inf(1)) } p = p.Abs(p) q = q.Abs(q) if p.Cmp(p) < 0 { p, q = q, p } if p.Cmp(big.NewFloat(0)) == 0 { return big.NewFloat(0) } q = q.Quo(q, p) return sqrt(q.Mul(q, q).Add(q, big.NewFloat(1))).Mul(q, p) }
func sqrtFloat(x *big.Float) *big.Float { t1 := new(big.Float).SetPrec(prec) t2 := new(big.Float).SetPrec(prec) t1.Copy(x) // Iterate. // x{n} = (x{n-1}+x{0}/x{n-1}) / 2 for i := 0; i <= steps; i++ { if t1.Cmp(zero) == 0 || t1.IsInf() { return t1 } t2.Quo(x, t1) t2.Add(t2, t1) t1.Mul(half, t2) } return t1 }
// 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 }
// Pow returns a big.Float representation of z**w. Precision is the same as the one // of the first argument. The function panics when z is negative. func Pow(z *big.Float, w *big.Float) *big.Float { if z.Sign() < 0 { panic("Pow: negative base") } // Pow(z, 0) = 1.0 if w.Sign() == 0 { return big.NewFloat(1).SetPrec(z.Prec()) } // Pow(z, 1) = z // Pow(+Inf, n) = +Inf if w.Cmp(big.NewFloat(1)) == 0 || z.IsInf() { return new(big.Float).Copy(z) } // Pow(z, -w) = 1 / Pow(z, w) if w.Sign() < 0 { x := new(big.Float) zExt := new(big.Float).Copy(z).SetPrec(z.Prec() + 64) wNeg := new(big.Float).Neg(w) return x.Quo(big.NewFloat(1), Pow(zExt, wNeg)).SetPrec(z.Prec()) } // w integer fast path if w.IsInt() { wi, _ := w.Int64() return powInt(z, int(wi)) } // compute w**z as exp(z log(w)) x := new(big.Float).SetPrec(z.Prec() + 64) logZ := Log(new(big.Float).Copy(z).SetPrec(z.Prec() + 64)) x.Mul(w, logZ) x = Exp(x) return x.SetPrec(z.Prec()) }
// Log returns a big.Float representation of the natural logarithm of // z. Precision is the same as the one of the argument. The function // panics if z is negative, returns -Inf when z = 0, and +Inf when z = // +Inf func Log(z *big.Float) *big.Float { // panic on negative z if z.Sign() == -1 { panic("Log: argument is negative") } // Log(0) = -Inf if z.Sign() == 0 { return big.NewFloat(math.Inf(-1)).SetPrec(z.Prec()) } prec := z.Prec() + 64 // guard digits one := big.NewFloat(1).SetPrec(prec) two := big.NewFloat(2).SetPrec(prec) four := big.NewFloat(4).SetPrec(prec) // Log(1) = 0 if z.Cmp(one) == 0 { return big.NewFloat(0).SetPrec(z.Prec()) } // Log(+Inf) = +Inf if z.IsInf() { return big.NewFloat(math.Inf(+1)).SetPrec(z.Prec()) } x := new(big.Float).SetPrec(prec) // if 0 < z < 1 we compute log(z) as -log(1/z) var neg bool if z.Cmp(one) < 0 { x.Quo(one, z) neg = true } else { x.Set(z) } // We scale up x until x >= 2**(prec/2), and then we'll be allowed // to use the AGM formula for Log(x). // // Double x until the condition is met, and keep track of the // number of doubling we did (needed to scale back later). lim := new(big.Float) lim.SetMantExp(two, int(prec/2)) k := 0 for x.Cmp(lim) < 0 { x.Mul(x, x) k++ } // Compute the natural log of x using the fact that // log(x) = π / (2 * AGM(1, 4/x)) // if // x >= 2**(prec/2), // where prec is the desired precision (in bits) pi := pi(prec) agm := agm(one, x.Quo(four, x)) // agm = AGM(1, 4/x) x.Quo(pi, x.Mul(two, agm)) // reuse x, we don't need it if neg { x.Neg(x) } // scale the result back multiplying by 2**-k // reuse lim to reduce allocations. x.Mul(x, lim.SetMantExp(one, -k)) return x.SetPrec(z.Prec()) }