本文整理汇总了Golang中math/big.Rat.Cmp方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.Cmp方法的具体用法?Golang Rat.Cmp怎么用?Golang Rat.Cmp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.Cmp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: mandelbrotRat
func mandelbrotRat(a, b *big.Rat) color.Color {
var x, y, nx, ny, x2, y2, f2, f4, r2, tmp big.Rat
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
}
示例2: quo
func quo(x, y *complexRat) *complexRat {
z := newComplexRat()
denominator := new(big.Rat)
t := new(big.Rat)
t.Mul(y.r, y.r)
denominator.Mul(y.i, y.i)
denominator.Add(denominator, t)
if denominator.Cmp(zero) == 0 {
return newComplexRat()
}
ac := new(big.Rat)
bd := new(big.Rat)
ac.Mul(x.r, y.r)
bd.Mul(x.i, y.i)
bc := new(big.Rat)
ad := new(big.Rat)
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
}
示例3: ratProb
func ratProb(mode int) func(*big.Rat) *big.Rat {
return func(x *big.Rat) *big.Rat {
lo := big.NewInt(0)
hi := new(big.Int).Set(big2p63)
n := 0
for lo.Cmp(hi) != 0 {
m := new(big.Int).Add(lo, hi)
m = m.Rsh(m, 1)
if n++; n > 100 {
fmt.Printf("??? %v %v %v\n", lo, hi, m)
break
}
v := new(big.Rat).SetFrac(m, big2p63)
f, _ := v.Float64()
v.SetFloat64(f)
if v.Cmp(x) < 0 {
lo.Add(m, bigOne)
} else {
hi.Set(m)
}
}
switch mode {
default: // case 0
return new(big.Rat).SetFrac(lo, big2p63)
case 1:
if lo.Cmp(big.NewInt(cutoff1)) <= 0 {
lo.Add(lo, big.NewInt(1<<63-cutoff1))
}
return new(big.Rat).SetFrac(lo, big2p63)
case 2:
return new(big.Rat).SetFrac(lo, big.NewInt(cutoff1))
}
}
}
示例4: BigratToBool
func BigratToBool(bigrat *big.Rat) bool {
cmp := bigrat.Cmp(big.NewRat(0, 1))
if cmp == 0 {
return false
} else {
return true
}
}
示例5: clampTargetAdjustment
// clampTargetAdjustment returns a clamped version of the base adjustment
// value. The clamp keeps the maximum adjustment to ~7x every 2000 blocks. This
// ensures that raising and lowering the difficulty requires a minimum amount
// of total work, which prevents certain classes of difficulty adjusting
// attacks.
func clampTargetAdjustment(base *big.Rat) *big.Rat {
if base.Cmp(types.MaxAdjustmentUp) > 0 {
return types.MaxAdjustmentUp
} else if base.Cmp(types.MaxAdjustmentDown) < 0 {
return types.MaxAdjustmentDown
}
return base
}
示例6: floatString
// floatString returns the string representation for a
// numeric value v in normalized floating-point format.
func floatString(v exact.Value) string {
if exact.Sign(v) == 0 {
return "0.0"
}
// x != 0
// convert |v| into a big.Rat x
x := new(big.Rat).SetFrac(absInt(exact.Num(v)), absInt(exact.Denom(v)))
// normalize x and determine exponent e
// (This is not very efficient, but also not speed-critical.)
var e int
for x.Cmp(ten) >= 0 {
x.Quo(x, ten)
e++
}
for x.Cmp(one) < 0 {
x.Mul(x, ten)
e--
}
// TODO(gri) Values such as 1/2 are easier to read in form 0.5
// rather than 5.0e-1. Similarly, 1.0e1 is easier to read as
// 10.0. Fine-tune best exponent range for readability.
s := x.FloatString(100) // good-enough precision
// trim trailing 0's
i := len(s)
for i > 0 && s[i-1] == '0' {
i--
}
s = s[:i]
// add a 0 if the number ends in decimal point
if len(s) > 0 && s[len(s)-1] == '.' {
s += "0"
}
// add exponent and sign
if e != 0 {
s += fmt.Sprintf("e%+d", e)
}
if exact.Sign(v) < 0 {
s = "-" + s
}
// TODO(gri) If v is a "small" fraction (i.e., numerator and denominator
// are just a small number of decimal digits), add the exact fraction as
// a comment. For instance: 3.3333...e-1 /* = 1/3 */
return s
}
示例7: Intersect
// Returns hash if they intersect, otherwise nil
func Intersect(P1, Q1, P2, Q2 Vec) ([20]byte, bool) {
// Vectors with same length and direction as line segments
v1 := Q1.Sub(P1)
v2 := Q2.Sub(P2)
// Parallel vectors never intersect
det := v2.X*v1.Y - v1.X*v2.Y
if det == 0 {
return sha1nil, false
}
// Intersection is calculated using linear algebra
var k1, k2 big.Rat
Pdx, Pdy := P2.X-P1.X, P2.Y-P1.Y
invDet := big.NewRat(1, det)
k1.Mul(invDet, big.NewRat(-v2.Y*Pdx+v2.X*Pdy, 1))
k2.Mul(invDet, big.NewRat(-v1.Y*Pdx+v1.X*Pdy, 1))
// Check if intersection is inside both segments
zero := big.NewRat(0, 1)
one := big.NewRat(1, 1)
k1valid := k1.Cmp(zero) == 1 && k1.Cmp(one) == -1
k2valid := k2.Cmp(zero) == 1 && k2.Cmp(one) == -1
// Return hash of intersection coordinate if it was
if k1valid && k2valid {
var Ix, Iy big.Rat
Ix.Mul(big.NewRat(v1.X, 1), &k1).Add(&Ix, big.NewRat(P1.X, 1))
Iy.Mul(big.NewRat(v1.Y, 1), &k1).Add(&Iy, big.NewRat(P1.Y, 1))
return sha1.Sum([]byte(fmt.Sprintf("%v,%v", Ix.String(), Iy.String()))), true
} else {
return sha1nil, false
}
}
示例8: NewRational
func NewRational(r *big.Rat) Rational {
if !r.IsInt() || r.Cmp(min) < 0 || r.Cmp(max) > 0 {
return Rational{r}
}
n := r.Num().Int64()
i := n + 256
p := rat[i]
if p.v == nil {
p = Rational{r}
rat[i] = p
}
return p
}
示例9: sqrtFloat
func sqrtFloat(x *big.Rat) *big.Rat {
t1 := new(big.Rat)
t2 := new(big.Rat)
t1.Set(x)
// Iterate.
// x{n} = (x{n-1}+x{0}/x{n-1}) / 2
for i := 0; i <= 4; i++ {
if t1.Cmp(zero) == 0 {
return t1
}
t2.Quo(x, t1)
t2.Add(t2, t1)
t1.Mul(half, t2)
}
return t1
}
示例10: NewRational
func NewRational(r *big.Rat) *Rational {
if !r.IsInt() || r.Cmp(min) < 0 || r.Cmp(max) > 0 {
return (*Rational)(r)
}
n := r.Num().Int64()
i := n + 256
ratl.RLock()
p := rat[i]
ratl.RUnlock()
if p == nil {
p = (*Rational)(r)
ratl.Lock()
rat[i] = p
ratl.Unlock()
}
return p
}
示例11: ratExponent
// ratExponent returns the power of ten that x would display in scientific notation.
func ratExponent(x *big.Rat) int {
if x.Sign() < 0 {
x.Neg(x)
}
e := 0
invert := false
if x.Num().Cmp(x.Denom()) < 0 {
invert = true
x.Inv(x)
e++
}
for x.Cmp(bigRatBillion) >= 0 {
e += 9
x.Quo(x, bigRatBillion)
}
for x.Cmp(bigRatTen) > 0 {
e++
x.Quo(x, bigRatTen)
}
if invert {
return -e
}
return e
}
示例12: EqualBigRat
// EqualBigRat returns true if both *big.Rats are equal
func EqualBigRat(a, b *big.Rat) bool { return a.Cmp(b) == 0 }
示例13: MinBigRat
// MinBigRat returns the smaller of the two *big.Rats
func MinBigRat(a, b *big.Rat) *big.Rat {
if a.Cmp(b) < 0 {
return a
}
return b
}
示例14: binaryFloatOp
func binaryFloatOp(x *big.Rat, op token.Token, y *big.Rat) interface{} {
var z big.Rat
switch op {
case token.ADD:
return z.Add(x, y)
case token.SUB:
return z.Sub(x, y)
case token.MUL:
return z.Mul(x, y)
case token.QUO:
return z.Quo(x, y)
case token.EQL:
return x.Cmp(y) == 0
case token.NEQ:
return x.Cmp(y) != 0
case token.LSS:
return x.Cmp(y) < 0
case token.LEQ:
return x.Cmp(y) <= 0
case token.GTR:
return x.Cmp(y) > 0
case token.GEQ:
return x.Cmp(y) >= 0
}
panic("unreachable")
}
示例15: MinBigRat
// Returns the smaller of x or y.
func MinBigRat(x, y *big.Rat) *big.Rat {
if x.Cmp(y) < 0 {
return x
}
return y
}