LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file implements multi-precision rational numbers. | 5 // This file implements multi-precision rational numbers. |
6 | 6 |
7 package big | 7 package big |
8 | 8 |
9 import ( | 9 import ( |
10 "encoding/binary" | 10 "encoding/binary" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // may change if a new value is assigned to x. | 129 // may change if a new value is assigned to x. |
130 func (x *Rat) Num() *Int { | 130 func (x *Rat) Num() *Int { |
131 return &x.a | 131 return &x.a |
132 } | 132 } |
133 | 133 |
134 // Denom returns the denominator of x; it is always > 0. | 134 // Denom returns the denominator of x; it is always > 0. |
135 // The result is a reference to x's denominator; it | 135 // The result is a reference to x's denominator; it |
136 // may change if a new value is assigned to x. | 136 // may change if a new value is assigned to x. |
137 func (x *Rat) Denom() *Int { | 137 func (x *Rat) Denom() *Int { |
138 if len(x.b) == 0 { | 138 if len(x.b) == 0 { |
139 » » return &Int{abs: nat{1}} | 139 » » return &Int{abs: {1}} |
140 } | 140 } |
141 return &Int{abs: x.b} | 141 return &Int{abs: x.b} |
142 } | 142 } |
143 | 143 |
144 func gcd(x, y nat) nat { | 144 func gcd(x, y nat) nat { |
145 // Euclidean algorithm. | 145 // Euclidean algorithm. |
146 var a, b nat | 146 var a, b nat |
147 a = a.set(x) | 147 a = a.set(x) |
148 b = b.set(y) | 148 b = b.set(y) |
149 for len(b) != 0 { | 149 for len(b) != 0 { |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 if b>>1 != ratGobVersion { | 423 if b>>1 != ratGobVersion { |
424 return errors.New(fmt.Sprintf("Rat.GobDecode: encoding version %
d not supported", b>>1)) | 424 return errors.New(fmt.Sprintf("Rat.GobDecode: encoding version %
d not supported", b>>1)) |
425 } | 425 } |
426 const j = 1 + 4 | 426 const j = 1 + 4 |
427 i := j + binary.BigEndian.Uint32(buf[j-4:j]) | 427 i := j + binary.BigEndian.Uint32(buf[j-4:j]) |
428 z.a.neg = b&1 != 0 | 428 z.a.neg = b&1 != 0 |
429 z.a.abs = z.a.abs.setBytes(buf[j:i]) | 429 z.a.abs = z.a.abs.setBytes(buf[j:i]) |
430 z.b = z.b.setBytes(buf[i:]) | 430 z.b = z.b.setBytes(buf[i:]) |
431 return nil | 431 return nil |
432 } | 432 } |
LEFT | RIGHT |