Exemplo n.º 1
0
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))
}
Exemplo n.º 2
0
// 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)
}
func (me *StatisticalAccumulator) PutFrac(a, b *big.Int) {

	xx := new(big.Rat)
	xx.SetFrac(a, b)

	me.PutRat(xx)
}
Exemplo n.º 4
0
// 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
}