Exemplo n.º 1
0
// SetFrac64 sets q to x/y and returns q.
func (q *Rat) SetFrac64(x int64, y int64) *Rat {
	q.doinit()

	// y has to be positive for mpq_set_si
	if y < 0 {
		x *= -1
		y *= -1
	}

	C.mpq_set_si(&q.i[0], C.long(x), C.ulong(y))
	C.mpq_canonicalize(&q.i[0])
	return q
}
Exemplo n.º 2
0
Arquivo: rat.go Projeto: ncw/gmp
// SetFrac64 sets z to a/b and returns z.
func (z *Rat) SetFrac64(a, b int64) *Rat {
	z.doinit()
	if b == 0 {
		panic("division by zero")
	}
	// Detect overflow if running on 32 bits
	if a == int64(C.long(a)) && b == int64(C.long(b)) {
		if b < 0 {
			a = -a
			b = -b
		}
		C.mpq_set_si(&z.i[0], C.long(a), C.ulong(b))
		C.mpq_canonicalize(&z.i[0])
		if b < 0 {
			// This only happens when b = 1<<63
			z.Neg(z)
		}
	} else {
		// Slow path but will work on 32 bit architectures
		z.SetFrac(NewInt(a), NewInt(b))
	}
	return z
}
Exemplo n.º 3
0
// SetSint sets q to x/y and returns q.
func (q *Rat) SetSint(x int, y uint) *Rat {
	q.doinit()
	C.mpq_set_si(&q.i[0], C.long(x), C.ulong(y))
	return q
}