本文整理汇总了Golang中math/big.Float.Cmp方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Cmp方法的具体用法?Golang Float.Cmp怎么用?Golang Float.Cmp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Cmp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}
示例4: 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)
}
}
示例5: floatLog
// floatLog computes natural log(x) using the Maclaurin series for log(1-x).
func floatLog(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
xx := newF()
xx.Quo(floatOne, x)
x = xx
}
// 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 := newF()
exp2 := x.MantExp(mantissa)
exp := newF().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 := newF().SetInt64(1)
y.Sub(y, mantissa)
// The Maclaurin series for log(1-y) == log(x) is: -y - y²/2 - y³/3 ...
yN := newF().Set(y)
term := newF()
n := newF().Set(floatOne)
z := newF()
// 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.
loop := newLoop("log", y, 40)
for {
term.Set(yN)
term.Quo(term, n)
z.Sub(z, term)
if loop.terminate(z) {
break
}
// Advance y**index (multiply by y).
yN.Mul(yN, y)
n.Add(n, floatOne)
}
if invert {
z.Neg(z)
}
z.Add(z, exp)
return z
}
示例6: compareJSONNumber
func compareJSONNumber(a, b json.Number) int {
bigA, ok := new(big.Float).SetString(string(a))
if !ok {
panic("illegal value")
}
bigB, ok := new(big.Float).SetString(string(b))
if !ok {
panic("illegal value")
}
return bigA.Cmp(bigB)
}
示例7: 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)
}
}
示例8: 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)
}
}
示例9: twoPiReduce
// twoPiReduce guarantees x < 2𝛑; x is known to be >= 0 coming in.
func twoPiReduce(x *big.Float) {
// TODO: Is there an easy better algorithm?
twoPi := newF().Set(floatTwo)
twoPi.Mul(twoPi, floatPi)
// Do something clever(er) if it's large.
if x.Cmp(newF().SetInt64(1000)) > 0 {
multiples := make([]*big.Float, 0, 100)
sixteen := newF().SetInt64(16)
multiple := newF().Set(twoPi)
for {
multiple.Mul(multiple, sixteen)
if x.Cmp(multiple) < 0 {
break
}
multiples = append(multiples, newF().Set(multiple))
}
// From the right, subtract big multiples.
for i := len(multiples) - 1; i >= 0; i-- {
multiple := multiples[i]
for x.Cmp(multiple) >= 0 {
x.Sub(x, multiple)
}
}
}
for x.Cmp(twoPi) >= 0 {
x.Sub(x, twoPi)
}
}
示例10: 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)
}
示例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: runTest
func runTest(encoderDecoder EncoderDecoder, n *big.Float) (nBytes uint64) {
y := newBigFloat(0)
buf := new(bytes.Buffer)
err := encoderDecoder.Encode(buf, n)
if err != nil {
panic(err)
}
nBytes += uint64(buf.Len())
buf = bytes.NewBuffer(buf.Bytes())
err = encoderDecoder.Decode(buf, y)
nBytes += uint64(buf.Len())
if n.Cmp(y) != 0 {
panic(fmt.Sprintf("write and read are not the same: %v, %v - %d - %d, %d", stringOfBigFloat(n), stringOfBigFloat(y), nBytes, n.Prec(), y.Prec()))
}
return
}
示例14: floatAsin
// floatAsin computes asin(x) using the formula asin(x) = atan(x/sqrt(1-x²)).
func floatAsin(c Context, x *big.Float) *big.Float {
// The asin Taylor series converges very slowly near ±1, but our
// atan implementation converges well for all values, so we use
// the formula above to compute asin. But be careful when |x|=1.
if x.Cmp(floatOne) == 0 {
z := newFloat(c).Set(floatPi)
return z.Quo(z, floatTwo)
}
if x.Cmp(floatMinusOne) == 0 {
z := newFloat(c).Set(floatPi)
z.Quo(z, floatTwo)
return z.Neg(z)
}
z := newFloat(c)
z.Mul(x, x)
z.Sub(floatOne, z)
z = floatSqrt(c, z)
z.Quo(x, z)
return floatAtan(c, z)
}
示例15: 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())
}