mirror of
https://github.com/golang/go
synced 2024-11-19 21:24:40 -07:00
math/big: more accurate package comment
Fix some receiver names for consistency. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5624043
This commit is contained in:
parent
f6f5ce87cd
commit
d0607221fa
@ -8,9 +8,15 @@
|
|||||||
// - Int signed integers
|
// - Int signed integers
|
||||||
// - Rat rational numbers
|
// - Rat rational numbers
|
||||||
//
|
//
|
||||||
// All methods on Int take the result as the receiver; if it is one
|
// Methods are typically of the form:
|
||||||
// of the operands it may be overwritten (and its memory reused).
|
//
|
||||||
// To enable chaining of operations, the result is also returned.
|
// func (z *Int) Op(x, y *Int) *Int (similar for *Rat)
|
||||||
|
//
|
||||||
|
// and implement operations z = x Op y with the result as receiver; if it
|
||||||
|
// is one of the operands it may be overwritten (and its memory reused).
|
||||||
|
// To enable chaining of operations, the result is also returned. Methods
|
||||||
|
// returning a result other than *Int or *Rat take one of the operands as
|
||||||
|
// the receiver.
|
||||||
//
|
//
|
||||||
package big
|
package big
|
||||||
|
|
||||||
|
@ -328,36 +328,36 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string representation of z in the form "a/b" (even if b == 1).
|
// String returns a string representation of z in the form "a/b" (even if b == 1).
|
||||||
func (z *Rat) String() string {
|
func (x *Rat) String() string {
|
||||||
s := "/1"
|
s := "/1"
|
||||||
if len(z.b) != 0 {
|
if len(x.b) != 0 {
|
||||||
s = "/" + z.b.decimalString()
|
s = "/" + x.b.decimalString()
|
||||||
}
|
}
|
||||||
return z.a.String() + s
|
return x.a.String() + s
|
||||||
}
|
}
|
||||||
|
|
||||||
// RatString returns a string representation of z in the form "a/b" if b != 1,
|
// RatString returns a string representation of z in the form "a/b" if b != 1,
|
||||||
// and in the form "a" if b == 1.
|
// and in the form "a" if b == 1.
|
||||||
func (z *Rat) RatString() string {
|
func (x *Rat) RatString() string {
|
||||||
if z.IsInt() {
|
if x.IsInt() {
|
||||||
return z.a.String()
|
return x.a.String()
|
||||||
}
|
}
|
||||||
return z.String()
|
return x.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FloatString returns a string representation of z in decimal form with prec
|
// FloatString returns a string representation of z in decimal form with prec
|
||||||
// digits of precision after the decimal point and the last digit rounded.
|
// digits of precision after the decimal point and the last digit rounded.
|
||||||
func (z *Rat) FloatString(prec int) string {
|
func (x *Rat) FloatString(prec int) string {
|
||||||
if z.IsInt() {
|
if x.IsInt() {
|
||||||
s := z.a.String()
|
s := x.a.String()
|
||||||
if prec > 0 {
|
if prec > 0 {
|
||||||
s += "." + strings.Repeat("0", prec)
|
s += "." + strings.Repeat("0", prec)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
// z.b != 0
|
// x.b != 0
|
||||||
|
|
||||||
q, r := nat(nil).div(nat(nil), z.a.abs, z.b)
|
q, r := nat(nil).div(nat(nil), x.a.abs, x.b)
|
||||||
|
|
||||||
p := natOne
|
p := natOne
|
||||||
if prec > 0 {
|
if prec > 0 {
|
||||||
@ -365,11 +365,11 @@ func (z *Rat) FloatString(prec int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r = r.mul(r, p)
|
r = r.mul(r, p)
|
||||||
r, r2 := r.div(nat(nil), r, z.b)
|
r, r2 := r.div(nat(nil), r, x.b)
|
||||||
|
|
||||||
// see if we need to round up
|
// see if we need to round up
|
||||||
r2 = r2.add(r2, r2)
|
r2 = r2.add(r2, r2)
|
||||||
if z.b.cmp(r2) <= 0 {
|
if x.b.cmp(r2) <= 0 {
|
||||||
r = r.add(r, natOne)
|
r = r.add(r, natOne)
|
||||||
if r.cmp(p) >= 0 {
|
if r.cmp(p) >= 0 {
|
||||||
q = nat(nil).add(q, natOne)
|
q = nat(nil).add(q, natOne)
|
||||||
@ -378,7 +378,7 @@ func (z *Rat) FloatString(prec int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s := q.decimalString()
|
s := q.decimalString()
|
||||||
if z.a.neg {
|
if x.a.neg {
|
||||||
s = "-" + s
|
s = "-" + s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,10 +395,10 @@ func (z *Rat) FloatString(prec int) string {
|
|||||||
const ratGobVersion byte = 1
|
const ratGobVersion byte = 1
|
||||||
|
|
||||||
// GobEncode implements the gob.GobEncoder interface.
|
// GobEncode implements the gob.GobEncoder interface.
|
||||||
func (z *Rat) GobEncode() ([]byte, error) {
|
func (x *Rat) GobEncode() ([]byte, error) {
|
||||||
buf := make([]byte, 1+4+(len(z.a.abs)+len(z.b))*_S) // extra bytes for version and sign bit (1), and numerator length (4)
|
buf := make([]byte, 1+4+(len(x.a.abs)+len(x.b))*_S) // extra bytes for version and sign bit (1), and numerator length (4)
|
||||||
i := z.b.bytes(buf)
|
i := x.b.bytes(buf)
|
||||||
j := z.a.abs.bytes(buf[0:i])
|
j := x.a.abs.bytes(buf[0:i])
|
||||||
n := i - j
|
n := i - j
|
||||||
if int(uint32(n)) != n {
|
if int(uint32(n)) != n {
|
||||||
// this should never happen
|
// this should never happen
|
||||||
@ -407,7 +407,7 @@ func (z *Rat) GobEncode() ([]byte, error) {
|
|||||||
binary.BigEndian.PutUint32(buf[j-4:j], uint32(n))
|
binary.BigEndian.PutUint32(buf[j-4:j], uint32(n))
|
||||||
j -= 1 + 4
|
j -= 1 + 4
|
||||||
b := ratGobVersion << 1 // make space for sign bit
|
b := ratGobVersion << 1 // make space for sign bit
|
||||||
if z.a.neg {
|
if x.a.neg {
|
||||||
b |= 1
|
b |= 1
|
||||||
}
|
}
|
||||||
buf[j] = b
|
buf[j] = b
|
||||||
|
Loading…
Reference in New Issue
Block a user