本文整理汇总了Golang中math/big.Rat.SetFrac方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.SetFrac方法的具体用法?Golang Rat.SetFrac怎么用?Golang Rat.SetFrac使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.SetFrac方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: extr
func (t *lft) extr(x *big.Int) *big.Rat {
var n, d big.Int
var r big.Rat
return r.SetFrac(
n.Add(n.Mul(&t.q, x), &t.r),
d.Add(d.Mul(&t.s, x), &t.t))
}
示例2: Rat
// Rat returns the rational number representation of z.
// If x is non-nil, Rat stores the result in x instead of
// allocating a new Rat.
func (z *Decimal) Rat(x *big.Rat) *big.Rat {
// TODO(eric):
// We use big.Ints here when technically we could use our
// int64 with big.Rat's SetInt64 methoz.
// I'm not sure if it'll be an optimization or not.
var num, denom big.Int
if z.compact != overflown {
c := big.NewInt(z.compact)
if z.scale >= 0 {
num.Set(c)
denom.Set(mulBigPow10(oneInt, z.scale))
} else {
num.Set(mulBigPow10(c, -z.scale))
denom.SetInt64(1)
}
} else {
if z.scale >= 0 {
num.Set(&z.mantissa)
denom.Set(mulBigPow10(oneInt, z.scale))
} else {
num.Set(mulBigPow10(&z.mantissa, -z.scale))
denom.SetInt64(1)
}
}
if x != nil {
return x.SetFrac(&num, &denom)
}
return new(big.Rat).SetFrac(&num, &denom)
}
示例3: PutFrac
func (me *StatisticalAccumulator) PutFrac(a, b *big.Int) {
xx := new(big.Rat)
xx.SetFrac(a, b)
me.PutRat(xx)
}
示例4: BirthdayProblem
// Returns an approximate Birthday probability calculation
// based on the number of blocks given and the hash size.
//
// It uses the simplified calculation: p = k(k-1) / (2N)
//
// From http://preshing.com/20110504/hash-collision-probabilities/
func BirthdayProblem(blocks int) string {
k := big.NewInt(int64(blocks))
km1 := big.NewInt(int64(blocks - 1))
ksq := k.Mul(k, km1)
n := big.NewInt(0)
n = n.Exp(big.NewInt(2), big.NewInt(int64(HashSize)*8), nil)
twoN := n.Add(n, n)
var t, t2 big.Rat
var res *big.Rat
//
res = t.SetFrac(ksq, twoN)
f64, _ := res.Float64()
inv := t2.Inv(res).FloatString(0)
invs := fmt.Sprintf(" ~ 1/%s ~ %v", inv, f64)
return "Collision probability is" + invs
}