本文整理汇总了Golang中math/big.Float.Quo方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Quo方法的具体用法?Golang Float.Quo怎么用?Golang Float.Quo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Quo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: renderFloat
func renderFloat(img *image.RGBA) {
var yminF, ymaxMinF, heightF big.Float
yminF.SetInt64(ymin)
ymaxMinF.SetInt64(ymax - ymin)
heightF.SetInt64(height)
var xminF, xmaxMinF, widthF big.Float
xminF.SetInt64(xmin)
xmaxMinF.SetInt64(xmax - xmin)
widthF.SetInt64(width)
var y, x big.Float
for py := int64(0); py < height; py++ {
// y := float64(py)/height*(ymax-ymin) + ymin
y.SetInt64(py)
y.Quo(&y, &heightF)
y.Mul(&y, &ymaxMinF)
y.Add(&y, &yminF)
for px := int64(0); px < width; px++ {
// x := float64(px)/width*(xmax-xmin) + xmin
x.SetInt64(px)
x.Quo(&x, &widthF)
x.Mul(&x, &xmaxMinF)
x.Add(&x, &xminF)
c := mandelbrotFloat(&x, &y)
if c == nil {
c = color.Black
}
img.Set(int(px), int(py), c)
}
}
}
示例2: Sqrt
// Sqrt returns the square root n.
func Sqrt(n *big.Float) *big.Float {
prec := n.Prec()
x := new(big.Float).SetPrec(prec).SetInt64(1)
z := new(big.Float).SetPrec(prec).SetInt64(1)
half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
t := new(big.Float).SetPrec(prec)
for {
z.Copy(x)
t.Mul(x, x)
t.Sub(t, n)
t.Quo(t, x)
t.Mul(t, half)
x.Sub(x, t)
if x.Cmp(z) == 0 {
break
}
}
return x
}
示例3: Sqrt
// 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)
}
}
示例4: Div_S
// Divide
func (a Scalar) Div_S(b S) S {
var x, y big.Float
x = big.Float(a)
y = big.Float(b.(Scalar))
z := x.Quo(&x, &y)
return (Scalar)(*z)
}
示例5: splitRangeString
func splitRangeString(start, end string, splits int) []string {
results := []string{start}
if start == end {
return results
}
if end < start {
tmp := start
start = end
end = tmp
}
// find longest common prefix between strings
minLen := len(start)
if len(end) < minLen {
minLen = len(end)
}
prefix := ""
for i := 0; i < minLen; i++ {
if start[i] == end[i] {
prefix = start[0 : i+1]
} else {
break
}
}
// remove prefix from strings to split
start = start[len(prefix):]
end = end[len(prefix):]
ordStart := stringToOrd(start)
ordEnd := stringToOrd(end)
tmp := new(big.Int)
tmp.Sub(ordEnd, ordStart)
stride := new(big.Float)
stride.SetInt(tmp)
stride.Quo(stride, big.NewFloat(float64(splits)))
for i := 1; i <= splits; i++ {
tmp := new(big.Float)
tmp.Mul(stride, big.NewFloat(float64(i)))
tmp.Add(tmp, new(big.Float).SetInt(ordStart))
result, _ := tmp.Int(new(big.Int))
value := prefix + ordToString(result, 0)
if value != results[len(results)-1] {
results = append(results, value)
}
}
return results
}
示例6: floatLog
// floatLog computes natural log(x) using the Maclaurin series for log(1-x).
func floatLog(c Context, x *big.Float) *big.Float {
if x.Sign() <= 0 {
Errorf("log of non-positive value")
}
// The series wants x < 1, and log 1/x == -log x, so exploit that.
invert := false
if x.Cmp(floatOne) > 0 {
invert = true
x.Quo(floatOne, x)
}
// x = mantissa * 2**exp, and 0.5 <= mantissa < 1.
// So log(x) is log(mantissa)+exp*log(2), and 1-x will be
// between 0 and 0.5, so the series for 1-x will converge well.
// (The series converges slowly in general.)
mantissa := newFloat(c)
exp2 := x.MantExp(mantissa)
exp := newFloat(c).SetInt64(int64(exp2))
exp.Mul(exp, floatLog2)
if invert {
exp.Neg(exp)
}
// y = 1-x (whereupon x = 1-y and we use that in the series).
y := newFloat(c).SetInt64(1)
y.Sub(y, mantissa)
// The Maclaurin series for log(1-y) == log(x) is: -y - y²/2 - y³/3 ...
yN := newFloat(c).Set(y)
term := newFloat(c)
n := newFloat(c).Set(floatOne)
z := newFloat(c)
// This is the slowest-converging series, so we add a factor of ten to the cutoff.
// Only necessary when FloatPrec is at or beyond constPrecisionInBits.
for loop := newLoop(c.Config(), "log", x, 40); ; {
term.Quo(yN, n.SetUint64(loop.i+1))
z.Sub(z, term)
if loop.done(z) {
break
}
// Advance y**index (multiply by y).
yN.Mul(yN, y)
}
if invert {
z.Neg(z)
}
z.Add(z, exp)
return z
}
示例7: sqrt
// sqrt for big.Float
func sqrt(given *big.Float) *big.Float {
const prec = 200
steps := int(math.Log2(prec))
given.SetPrec(prec)
half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
x := new(big.Float).SetPrec(prec).SetInt64(1)
t := new(big.Float)
for i := 0; i <= steps; i++ {
t.Quo(given, x)
t.Add(x, t)
t.Mul(half, t)
}
return x
}
示例8: sqrtDirect
// 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())
}
示例9: hypot
// 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)
}
示例10: sqrtFloat
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
}
示例11: Example_sqrt2
// This example shows how to use big.Float to compute the square root of 2 with
// a precision of 200 bits, and how to print the result as a decimal number.
func Example_sqrt2() {
// We'll do computations with 200 bits of precision in the mantissa.
const prec = 200
// Compute the square root of 2 using Newton's Method. We start with
// an initial estimate for sqrt(2), and then iterate:
// x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) )
// Since Newton's Method doubles the number of correct digits at each
// iteration, we need at least log_2(prec) steps.
steps := int(math.Log2(prec))
// Initialize values we need for the computation.
two := new(big.Float).SetPrec(prec).SetInt64(2)
half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
// Use 1 as the initial estimate.
x := new(big.Float).SetPrec(prec).SetInt64(1)
// 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.
t := new(big.Float)
// Iterate.
for i := 0; i <= steps; i++ {
t.Quo(two, x) // t = 2.0 / x_n
t.Add(x, t) // t = x_n + (2.0 / x_n)
x.Mul(half, t) // x_{n+1} = 0.5 * t
}
// We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter
fmt.Printf("sqrt(2) = %.50f\n", x)
// Print the error between 2 and x*x.
t.Mul(x, x) // t = x*x
fmt.Printf("error = %e\n", t.Sub(two, t))
// Output:
// sqrt(2) = 1.41421356237309504880168872420969807856967187537695
// error = 0.000000e+00
}
示例12: Pow
// return: x^y
func Pow(x *big.Float, n int64) *big.Float {
res := new(big.Float).Copy(x)
if n < 0 {
res = res.Quo(big.NewFloat(1), res)
n = -n
} else if n == 0 {
return big.NewFloat(1)
}
y := big.NewFloat(1)
for i := n; i > 1; {
if i%2 == 0 {
i /= 2
} else {
y = y.Mul(res, y)
i = (i - 1) / 2
}
res = res.Mul(res, res)
}
return res.Mul(res, y)
}
示例13: Pow
// 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())
}
示例14: bbp
// Evaluates a BBP term
//
// sum(k=0->inf)(1/base**k * (1/a*k + b))
func bbp(prec uint, base, a, b int64, result *big.Float) {
var term, power, aFp, bFp, _1, k, _base, oldresult big.Float
power.SetPrec(prec).SetInt64(1)
result.SetPrec(prec).SetInt64(0)
aFp.SetPrec(prec).SetInt64(a)
bFp.SetPrec(prec).SetInt64(b)
_1.SetPrec(prec).SetInt64(1)
k.SetPrec(prec).SetInt64(0)
_base.SetPrec(prec).SetInt64(base)
for {
oldresult.Set(result)
term.Mul(&aFp, &k)
term.Add(&term, &bFp)
term.Quo(&_1, &term)
term.Mul(&term, &power)
result.Add(result, &term)
if oldresult.Cmp(result) == 0 {
break
}
power.Quo(&power, &_base)
k.Add(&k, &_1)
}
}
示例15: acot
// Returns acot(x) in result
func acot(prec uint, x int64, result *big.Float) {
var term, power, _x, _kp, x2, oldresult big.Float
_x.SetPrec(prec).SetInt64(x)
power.SetPrec(prec).SetInt64(1)
power.Quo(&power, &_x) // 1/x
x2.Mul(&_x, &_x)
result.SetPrec(prec).SetInt64(0)
positive := true
for k := int64(1); ; k += 2 {
oldresult.Set(result)
kp := k
if !positive {
kp = -k
}
positive = !positive
_kp.SetPrec(prec).SetInt64(kp)
term.Quo(&power, &_kp)
result.Add(result, &term)
if oldresult.Cmp(result) == 0 {
break
}
power.Quo(&power, &x2)
}
}