LEFT | RIGHT |
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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 158 |
159 f = math.Ldexp(float64(mantissa), exp-53) | 159 f = math.Ldexp(float64(mantissa), exp-53) |
160 if math.IsInf(f, 0) { | 160 if math.IsInf(f, 0) { |
161 exact = false | 161 exact = false |
162 } | 162 } |
163 return | 163 return |
164 } | 164 } |
165 | 165 |
166 // Float64 returns the nearest float64 value to z. | 166 // Float64 returns the nearest float64 value to z. |
167 // If z is exactly representable as a float64, Float64 returns exact=true. | 167 // If z is exactly representable as a float64, Float64 returns exact=true. |
| 168 // If z is negative, so too is f, even if f==0. |
168 func (z *Rat) Float64() (f float64, exact bool) { | 169 func (z *Rat) Float64() (f float64, exact bool) { |
169 b := z.b.abs | 170 b := z.b.abs |
170 if len(b) == 0 { | 171 if len(b) == 0 { |
171 b = b.set(natOne) // materialize denominator | 172 b = b.set(natOne) // materialize denominator |
172 } | 173 } |
173 f, exact = quotToFloat(z.a.abs, b) | 174 f, exact = quotToFloat(z.a.abs, b) |
174 if z.a.neg { | 175 if z.a.neg { |
175 f = -f | 176 f = -f |
176 } | 177 } |
177 return | 178 return |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 if b>>1 != ratGobVersion { | 572 if b>>1 != ratGobVersion { |
572 return errors.New(fmt.Sprintf("Rat.GobDecode: encoding version %
d not supported", b>>1)) | 573 return errors.New(fmt.Sprintf("Rat.GobDecode: encoding version %
d not supported", b>>1)) |
573 } | 574 } |
574 const j = 1 + 4 | 575 const j = 1 + 4 |
575 i := j + binary.BigEndian.Uint32(buf[j-4:j]) | 576 i := j + binary.BigEndian.Uint32(buf[j-4:j]) |
576 z.a.neg = b&1 != 0 | 577 z.a.neg = b&1 != 0 |
577 z.a.abs = z.a.abs.setBytes(buf[j:i]) | 578 z.a.abs = z.a.abs.setBytes(buf[j:i]) |
578 z.b.abs = z.b.abs.setBytes(buf[i:]) | 579 z.b.abs = z.b.abs.setBytes(buf[i:]) |
579 return nil | 580 return nil |
580 } | 581 } |
LEFT | RIGHT |