Skip to content

Commit 12ad828

Browse files
committedMar 24, 2013
always doinit and canonicalize
1 parent 7758d66 commit 12ad828

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed
 

‎rat.go

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (q *Rat) SetFrac64(x int64, y int64) *Rat {
8282

8383
// SetInt64 sets z to x and returns z.
8484
func (z *Rat) SetInt64(x int64) *Rat {
85+
z.doinit()
8586
z.SetFrac64(x, 1)
8687
return z
8788
}
@@ -90,6 +91,7 @@ func (z *Rat) SetInt64(x int64) *Rat {
9091
func (q *Rat) SetUint(x, y uint) *Rat {
9192
q.doinit()
9293
C.mpq_set_ui(&q.i[0], C.ulong(x), C.ulong(y))
94+
C.mpq_canonicalize(&q.i[0])
9395
return q
9496
}
9597

@@ -123,6 +125,7 @@ func (q *Rat) SetStringBase(s string, base int) (*Rat, bool) {
123125
// optionally followed by an exponent. If the operation failed, the value of
124126
// z is undefined but the returned value is nil.
125127
func (q *Rat) SetString(s string) (*Rat, bool) {
128+
q.doinit()
126129
return q.SetStringBase(s, 10)
127130
}
128131

@@ -150,13 +153,15 @@ func (q *Rat) StringBase(base int) (string, error) {
150153
// RatString returns a string representation of z in the form "a/b" if b != 1,
151154
// and in the form "a" if b == 1.
152155
func (q *Rat) RatString() string {
156+
q.doinit()
153157
s, _ := q.StringBase(10)
154158
return s
155159
}
156160

157161
// String returns a string representation of z in the form "a/b"
158162
// (even if b == 1).
159163
func (q *Rat) String() string {
164+
q.doinit()
160165
s := q.RatString()
161166
if len(s) < 3 { // s not in the form a/b
162167
s = s + "/1"

0 commit comments

Comments
 (0)
Please sign in to comment.