Пример #1
0
// SetBit sets z to x, with x's i'th bit set to b (0 or 1).
// That is, if bit is 1 SetBit sets z = x | (1 << i);
// if bit is 0 it sets z = x &^ (1 << i). If bit is not 0 or 1,
// SetBit will panic.
func (z *Int) SetBit(x *Int, i int, b uint) *Int {
	z.doinit()

	if i < 0 {
		panic("negative bit index")
	}

	z.Set(x)
	switch b {
	case 0:
		C.mpz_clrbit(z.ptr, C.mp_bitcnt_t(i))
	case 1:
		C.mpz_setbit(z.ptr, C.mp_bitcnt_t(i))
	default:
		panic("set bit is not 0 or 1")
	}
	return z
}
Пример #2
0
// Int promises that the zero value is a 0, but in gmp
// the zero value is a crash.  To bridge the gap, the
// init bool says whether this is a valid gmp value.
// doinit initializes f.i if it needs it.  This is not inherent
// to FFI, just a mismatch between Go's convention of
// making zero values useful and gmp's decision not to.
func (f *Float) doinit() {
	if f.init {
		return
	}
	if f.prec != 0 {
		C.mpf_init2(&f.i[0], C.mp_bitcnt_t(f.prec))
	} else {
		C.mpf_init(&f.i[0])
	}
	f.init = true
}
Пример #3
0
// Return non-zero if the first n bits of x and y are equal,
// zero otherwise.  I.e., test if x and y are approximately equal.
func EqFloat(x, y *Float, n uint) int {
	x.doinit()
	y.doinit()
	return int(C.mpf_eq(&x.i[0], &y.i[0], C.mp_bitcnt_t(n)))
}
Пример #4
0
// Div2Exp sets z = x / 2^s and returns z.
func (f *Float) Div2Exp(x *Float, s uint) *Float {
	x.doinit()
	f.doinit()
	C.mpf_div_2exp(&f.i[0], &x.i[0], C.mp_bitcnt_t(s))
	return f
}
Пример #5
0
func SetDefaultPrec(prec uint) {
	C.mpf_set_default_prec(C.mp_bitcnt_t(prec))
}
Пример #6
0
func (f *Float) SetPrecRaw(prec uint) {
	f.doinit()
	C.mpf_set_prec_raw(&f.i[0], C.mp_bitcnt_t(prec))
}
Пример #7
0
func (f *Float) SetPrec(prec uint) {
	f.doinit()
	C.mpf_set_prec(&f.i[0], C.mp_bitcnt_t(prec))
	f.prec = prec
}
Пример #8
0
// Rsh sets z = x >> s and returns z.
func (z *Int) Rsh(x *Int, s uint) *Int {
	x.doinit()
	z.doinit()
	C.mpz_div_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s))
	return z
}
Пример #9
0
// Div2Exp sets z = x / 2^s and returns z.
func (q *Rat) Div2Exp(x *Rat, s uint) *Rat {
	x.doinit()
	q.doinit()
	C.mpq_div_2exp(&q.i[0], &x.i[0], C.mp_bitcnt_t(s))
	return q
}
Пример #10
0
// Bit returns the value of the i'th bit of x. That is, it
// returns (x>>i)&1. The bit index i must be >= 0.
func (x *Int) Bit(i int) uint {
	x.doinit()
	return uint(C.mpz_tstbit(x.ptr, C.mp_bitcnt_t(i)))
}
Пример #11
0
// Mul2Exp sets z = x * 2^s and returns z.
func (f *Float) Mul2Exp(x *Float, s uint) *Float {
	C.mpf_mul_2exp(&f.i[0], &x.i[0], C.mp_bitcnt_t(s))
	return f
}
Пример #12
0
func (f *Float) SetPrec(prec int) {
	C.mpf_set_prec(&f.i[0], C.mp_bitcnt_t(prec))
}