本文整理汇总了Golang中math/big.Float.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Set方法的具体用法?Golang Float.Set怎么用?Golang Float.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Set方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: SetFloat
func (a *Mpint) SetFloat(b *Mpflt) int {
// avoid converting huge floating-point numbers to integers
// (2*Mpprec is large enough to permit all tests to pass)
if b.Val.MantExp(nil) > 2*Mpprec {
return -1
}
if _, acc := b.Val.Int(&a.Val); acc == big.Exact {
return 0
}
const delta = 16 // a reasonably small number of bits > 0
var t big.Float
t.SetPrec(Mpprec - delta)
// try rounding down a little
t.SetMode(big.ToZero)
t.Set(&b.Val)
if _, acc := t.Int(&a.Val); acc == big.Exact {
return 0
}
// try rounding up a little
t.SetMode(big.AwayFromZero)
t.Set(&b.Val)
if _, acc := t.Int(&a.Val); acc == big.Exact {
return 0
}
return -1
}
示例3: mandelbrotFloat
func mandelbrotFloat(a, b *big.Float) color.Color {
var x, y, nx, ny, x2, y2, f2, f4, r2, tmp big.Float
f2.SetInt64(2)
f4.SetInt64(4)
x.SetInt64(0)
y.SetInt64(0)
defer func() { recover() }()
for n := uint8(0); n < iterations; n++ {
// Not update x2 and y2
// because they are already updated in the previous loop
nx.Sub(&x2, &y2)
nx.Add(&nx, a)
tmp.Mul(&x, &y)
ny.Mul(&f2, &tmp)
ny.Add(&ny, b)
x.Set(&nx)
y.Set(&ny)
x2.Mul(&x, &x)
y2.Mul(&y, &y)
r2.Add(&x2, &y2)
if r2.Cmp(&f4) > 0 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}
示例4: ToInt
// ToInt converts x to an Int value if x is representable as an Int.
// Otherwise it returns an Unknown.
func ToInt(x Value) Value {
switch x := x.(type) {
case int64Val, intVal:
return x
case ratVal:
if x.val.IsInt() {
return makeInt(x.val.Num())
}
case floatVal:
// avoid creation of huge integers
// (Existing tests require permitting exponents of at least 1024;
// allow any value that would also be permissible as a fraction.)
if smallRat(x.val) {
i := newInt()
if _, acc := x.val.Int(i); acc == big.Exact {
return makeInt(i)
}
// If we can get an integer by rounding up or down,
// assume x is not an integer because of rounding
// errors in prior computations.
const delta = 4 // a small number of bits > 0
var t big.Float
t.SetPrec(prec - delta)
// try rounding down a little
t.SetMode(big.ToZero)
t.Set(x.val)
if _, acc := t.Int(i); acc == big.Exact {
return makeInt(i)
}
// try rounding up a little
t.SetMode(big.AwayFromZero)
t.Set(x.val)
if _, acc := t.Int(i); acc == big.Exact {
return makeInt(i)
}
}
case complexVal:
if re := ToFloat(x); re.Kind() == Float {
return ToInt(re)
}
}
return unknownVal{}
}
示例5: 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)
}
}
示例6: 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)
}
}
示例7: Log
// 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())
}
示例8: Run
// Given a vector of real numbers x = [x_0, x_1, ..., x_n], this
// uses the PSLQ algorithm to find a list of integers
// [c_0, c_1, ..., c_n] such that
//
// |c_1 * x_1 + c_2 * x_2 + ... + c_n * x_n| < tolerance
//
// and such that max |c_k| < maxcoeff. If no such vector exists, Pslq
// returns one of the errors in this package depending on whether it
// has run out of iterations, precision or explored up to the
// maxcoeff. The tolerance defaults to 3/4 of the precision.
//
// This is a fairly direct translation of the pseudocode given by
// David Bailey, "The PSLQ Integer Relation Algorithm":
// http://www.cecm.sfu.ca/organics/papers/bailey/paper/html/node3.html
//
// If a result is returned, the first non-zero element will be positive
func (e *Pslq) Run(x []big.Float) ([]big.Int, error) {
n := len(x)
if n <= 1 {
return nil, ErrorBadArguments
}
// At too low precision, the algorithm becomes meaningless
if e.prec < 64 {
return nil, ErrorPrecisionTooLow
}
if e.verbose && int(e.prec)/max(2, int(n)) < 5 {
log.Printf("Warning: precision for PSLQ may be too low")
}
if e.verbose {
log.Printf("PSLQ using prec %d and tol %g", e.prec, e.tol)
}
if e.tol.Sign() == 0 {
return nil, ErrorToleranceRoundsToZero
}
// Temporary variables
tmp0 := new(big.Float).SetPrec(e.prec)
tmp1 := new(big.Float).SetPrec(e.prec)
bigTmp := new(big.Int)
// Convert to use 1-based indexing to allow us to be
// consistent with Bailey's indexing.
xNew := make([]big.Float, len(x)+1)
minx := new(big.Float).SetPrec(e.prec)
minxFirst := true
for i, xk := range x {
p := &xNew[i+1]
p.Set(&xk)
tmp0.Abs(p)
if minxFirst || tmp0.Cmp(minx) < 0 {
minxFirst = false
minx.Set(tmp0)
}
}
x = xNew
if debug {
printVector("x", x)
}
// Sanity check on magnitudes
if minx.Sign() == 0 {
return nil, ErrorZeroArguments
}
tmp1.SetInt64(128)
tmp0.Quo(&e.tol, tmp1)
if minx.Cmp(tmp0) < 0 { // minx < tol/128
return nil, ErrorArgumentTooSmall
}
tmp0.SetInt64(4)
tmp1.SetInt64(3)
tmp0.Quo(tmp0, tmp1)
var γ big.Float
e.Sqrt(tmp0, &γ) // sqrt(4<<prec)/3)
if debug {
fmt.Printf("γ = %f\n", &γ)
}
A := newBigIntMatrix(n+1, n+1)
B := newBigIntMatrix(n+1, n+1)
H := newMatrix(n+1, n+1)
// Initialization Step 1
//
// Set the n×n matrices A and B to the identity.
for i := 1; i <= n; i++ {
for j := 1; j <= n; j++ {
if i == j {
A[i][j].SetInt64(1)
B[i][j].SetInt64(1)
} else {
A[i][j].SetInt64(0)
B[i][j].SetInt64(0)
}
H[i][j].SetInt64(0)
}
}
if debug {
//.........这里部分代码省略.........