本文整理汇总了Golang中math/big.Float.Abs方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Abs方法的具体用法?Golang Float.Abs怎么用?Golang Float.Abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Abs方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BigCommaf
// BigCommaf produces a string form of the given big.Float in base 10
// with commas after every three orders of magnitude.
func BigCommaf(v *big.Float) string {
buf := &bytes.Buffer{}
if v.Sign() < 0 {
buf.Write([]byte{'-'})
v.Abs(v)
}
comma := []byte{','}
parts := strings.Split(v.Text('f', -1), ".")
pos := 0
if len(parts[0])%3 != 0 {
pos += len(parts[0]) % 3
buf.WriteString(parts[0][:pos])
buf.Write(comma)
}
for ; pos < len(parts[0]); pos += 3 {
buf.WriteString(parts[0][pos : pos+3])
buf.Write(comma)
}
buf.Truncate(buf.Len() - 1)
if len(parts) > 1 {
buf.Write([]byte{'.'})
buf.WriteString(parts[1])
}
return buf.String()
}
示例2: TestPow
func TestPow(t *testing.T) {
x := big.NewFloat(0.12381245613960218386)
n := 3
res := Pow(x, n)
exp := big.NewFloat(0.00189798605)
diff := new(big.Float).Sub(res, exp)
diff = diff.Abs(diff)
if diff.Cmp(big.NewFloat(0.00000001)) >= 0 {
log.Fatal("Pow failed:", exp, res)
}
}
示例3: TestRoot
func TestRoot(t *testing.T) {
x := big.NewFloat(0.12381245613960218386)
n := 16
res := Root(x, n)
exp := big.NewFloat(0.8776023372475015)
diff := new(big.Float).Sub(res, exp)
diff = diff.Abs(diff)
if diff.Cmp(big.NewFloat(0.00000001)) >= 0 {
log.Fatal("Exp failed:", exp, res)
}
}
示例4: 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)
}
示例5: 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 {
//.........这里部分代码省略.........
示例6: arithAbs
func arithAbs(a *big.Float) (*big.Float, error) {
return a.Abs(a), nil
}