本文整理汇总了Golang中math/big.Float.Add方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Add方法的具体用法?Golang Float.Add怎么用?Golang Float.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
示例2: Add_S
// Add
func (a Scalar) Add_S(b S) S {
var x, y big.Float
x = big.Float(a)
y = big.Float(b.(Scalar))
z := x.Add(&x, &y)
return (Scalar)(*z)
}
示例3: sincos
// sincos iterates a sin or cos Taylor series.
func sincos(name string, c Context, index int, x *big.Float, z *big.Float, exp uint64, factorial *big.Float) *big.Float {
term := newFloat(c).Set(floatOne)
for j := 0; j < index; j++ {
term.Mul(term, x)
}
xN := newFloat(c).Set(term)
x2 := newFloat(c).Mul(x, x)
n := newFloat(c)
for loop := newLoop(c.Config(), name, x, 4); ; {
// Invariant: factorial holds -1ⁿ*exponent!.
factorial.Neg(factorial)
term.Quo(term, factorial)
z.Add(z, term)
if loop.done(z) {
break
}
// Advance x**index (multiply by x²).
term.Mul(xN, x2)
xN.Set(term)
// Advance factorial.
factorial.Mul(factorial, n.SetUint64(exp+1))
factorial.Mul(factorial, n.SetUint64(exp+2))
exp += 2
}
return z
}
示例4: quo
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
}
示例5: 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)
}
}
}
示例6: opsum
func opsum(a, b *big.Float) *big.Float {
if a == nil {
return b
} else if b == nil {
return a
}
return a.Add(a, b)
}
示例7: NearestInt
// NearestInt set res to the nearest integer to x
func (e *Pslq) NearestInt(x *big.Float, res *big.Int) {
prec := x.Prec()
var tmp big.Float
tmp.SetPrec(prec)
if x.Sign() >= 0 {
tmp.Add(x, &e.half)
} else {
tmp.Sub(x, &e.half)
}
tmp.Int(res)
}
示例8: abs
func abs(x *complexFloat) *big.Float {
r := new(big.Float).SetPrec(prec)
i := new(big.Float).SetPrec(prec)
r.Copy(x.r)
i.Copy(x.i)
r.Mul(r, x.r) // r^2
i.Mul(i, x.i) // i^2
r.Add(r, i) // r^2 + i^2
return sqrtFloat(r) // sqrt(r^2 + i^2)
}
示例9: 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
}
示例10: ExampleFloat_Add
func ExampleFloat_Add() {
// Operating on numbers of different precision.
var x, y, z big.Float
x.SetInt64(1000) // x is automatically set to 64bit precision
y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
z.SetPrec(32)
z.Add(&x, &y)
fmt.Printf("x = %s (%s, prec = %d, acc = %s)\n", &x, x.Format('p', 0), x.Prec(), x.Acc())
fmt.Printf("y = %s (%s, prec = %d, acc = %s)\n", &y, y.Format('p', 0), y.Prec(), y.Acc())
fmt.Printf("z = %s (%s, prec = %d, acc = %s)\n", &z, z.Format('p', 0), z.Prec(), z.Acc())
// Output:
// x = 1000 (0x.fap10, prec = 64, acc = Exact)
// y = 2.718281828 (0x.adf85458248cd8p2, prec = 53, acc = Exact)
// z = 1002.718282 (0x.faadf854p10, prec = 32, acc = Below)
}
示例11: Root
// Implements the nth root algorithm from
// https://en.wikipedia.org/wiki/Nth_root_algorithm
// return: nth root of x within some epsilon
func Root(x *big.Float, n int64) *big.Float {
guess := new(big.Float).Quo(x, big.NewFloat(float64(n)))
diff := big.NewFloat(1)
ep := big.NewFloat(0.00000001)
abs := new(big.Float).Abs(diff)
for abs.Cmp(ep) >= 0 {
//fmt.Println(guess, abs)
prev := Pow(guess, n-1)
diff = new(big.Float).Quo(x, prev)
diff = diff.Sub(diff, guess)
diff = diff.Quo(diff, big.NewFloat(float64(n)))
guess = guess.Add(guess, diff)
abs = new(big.Float).Abs(diff)
}
return guess
}
示例12: 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
}
示例13: 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
}
示例14: main
func main() {
i1, i2, i3 := 12, 45, 68
intSum := i1 + i2 + i3
fmt.Println("Integer Sum:", intSum)
f1, f2, f3 := 23.5, 65.1, 76.3
floatSum := f1 + f2 + f3
fmt.Println("Float Sum:", floatSum)
var b1, b2, b3, bigSum big.Float
b1.SetFloat64(23.5)
b2.SetFloat64(65.1)
b3.SetFloat64(76.3)
bigSum.Add(&b1, &b2).Add(&bigSum, &b3)
fmt.Printf("BigSum = %.10g\n", &bigSum)
circleRadius := 15.5
circumference := 2 * circleRadius * math.Pi
fmt.Printf("Circumference: %.2f\n", circumference)
}
示例15: sincos
// sincos iterates a sin or cos Taylor series.
func sincos(name string, index int, x, z, exponent, factorial *big.Float) *big.Float {
plus := false
term := newF().Set(floatOne)
for j := 0; j < index; j++ {
term.Mul(term, x)
}
xN := newF().Set(term)
x2 := newF().Mul(x, x)
loop := newLoop(name, x, 4)
for {
// Invariant: factorial holds exponent!.
term.Quo(term, factorial)
if plus {
z.Add(z, term)
} else {
z.Sub(z, term)
}
plus = !plus
if loop.terminate(z) {
break
}
// Advance x**index (multiply by x²).
term.Mul(xN, x2)
xN.Set(term)
// Advance exponent and factorial.
exponent.Add(exponent, floatOne)
factorial.Mul(factorial, exponent)
exponent.Add(exponent, floatOne)
factorial.Mul(factorial, exponent)
}
return z
}