本文整理汇总了Golang中math/big.Float.SetInt方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.SetInt方法的具体用法?Golang Float.SetInt怎么用?Golang Float.SetInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.SetInt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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 {
//.........这里部分代码省略.........