示例#1
0
文件: rat.go 项目: jamesadney/gmp
// 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
}
示例#2
0
文件: rat.go 项目: 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
}
示例#3
0
文件: gogmp.go 项目: i-neda/gogmp
// 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
}