@@ -82,6 +82,7 @@ func (q *Rat) SetFrac64(x int64, y int64) *Rat {
82
82
83
83
// SetInt64 sets z to x and returns z.
84
84
func (z * Rat ) SetInt64 (x int64 ) * Rat {
85
+ z .doinit ()
85
86
z .SetFrac64 (x , 1 )
86
87
return z
87
88
}
@@ -90,6 +91,7 @@ func (z *Rat) SetInt64(x int64) *Rat {
90
91
func (q * Rat ) SetUint (x , y uint ) * Rat {
91
92
q .doinit ()
92
93
C .mpq_set_ui (& q .i [0 ], C .ulong (x ), C .ulong (y ))
94
+ C .mpq_canonicalize (& q .i [0 ])
93
95
return q
94
96
}
95
97
@@ -123,6 +125,7 @@ func (q *Rat) SetStringBase(s string, base int) (*Rat, bool) {
123
125
// optionally followed by an exponent. If the operation failed, the value of
124
126
// z is undefined but the returned value is nil.
125
127
func (q * Rat ) SetString (s string ) (* Rat , bool ) {
128
+ q .doinit ()
126
129
return q .SetStringBase (s , 10 )
127
130
}
128
131
@@ -150,13 +153,15 @@ func (q *Rat) StringBase(base int) (string, error) {
150
153
// RatString returns a string representation of z in the form "a/b" if b != 1,
151
154
// and in the form "a" if b == 1.
152
155
func (q * Rat ) RatString () string {
156
+ q .doinit ()
153
157
s , _ := q .StringBase (10 )
154
158
return s
155
159
}
156
160
157
161
// String returns a string representation of z in the form "a/b"
158
162
// (even if b == 1).
159
163
func (q * Rat ) String () string {
164
+ q .doinit ()
160
165
s := q .RatString ()
161
166
if len (s ) < 3 { // s not in the form a/b
162
167
s = s + "/1"
0 commit comments