From d0607221faec743a726cb38e1dd12b337c33a775 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 3 Feb 2012 10:17:19 -0800 Subject: [PATCH] math/big: more accurate package comment Fix some receiver names for consistency. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5624043 --- src/pkg/math/big/nat.go | 12 +++++++++--- src/pkg/math/big/rat.go | 42 ++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/pkg/math/big/nat.go b/src/pkg/math/big/nat.go index 16f6ce9ba1..6e1c7ffde4 100644 --- a/src/pkg/math/big/nat.go +++ b/src/pkg/math/big/nat.go @@ -8,9 +8,15 @@ // - Int signed integers // - Rat rational numbers // -// All methods on Int take the result as the 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 are typically of the form: +// +// 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 diff --git a/src/pkg/math/big/rat.go b/src/pkg/math/big/rat.go index adf412485f..7bd83fc0fb 100644 --- a/src/pkg/math/big/rat.go +++ b/src/pkg/math/big/rat.go @@ -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). -func (z *Rat) String() string { +func (x *Rat) String() string { s := "/1" - if len(z.b) != 0 { - s = "/" + z.b.decimalString() + if len(x.b) != 0 { + 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, // and in the form "a" if b == 1. -func (z *Rat) RatString() string { - if z.IsInt() { - return z.a.String() +func (x *Rat) RatString() string { + if x.IsInt() { + return x.a.String() } - return z.String() + return x.String() } // FloatString returns a string representation of z in decimal form with prec // digits of precision after the decimal point and the last digit rounded. -func (z *Rat) FloatString(prec int) string { - if z.IsInt() { - s := z.a.String() +func (x *Rat) FloatString(prec int) string { + if x.IsInt() { + s := x.a.String() if prec > 0 { s += "." + strings.Repeat("0", prec) } 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 if prec > 0 { @@ -365,11 +365,11 @@ func (z *Rat) FloatString(prec int) string { } 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 r2 = r2.add(r2, r2) - if z.b.cmp(r2) <= 0 { + if x.b.cmp(r2) <= 0 { r = r.add(r, natOne) if r.cmp(p) >= 0 { q = nat(nil).add(q, natOne) @@ -378,7 +378,7 @@ func (z *Rat) FloatString(prec int) string { } s := q.decimalString() - if z.a.neg { + if x.a.neg { s = "-" + s } @@ -395,10 +395,10 @@ func (z *Rat) FloatString(prec int) string { const ratGobVersion byte = 1 // GobEncode implements the gob.GobEncoder interface. -func (z *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) - i := z.b.bytes(buf) - j := z.a.abs.bytes(buf[0:i]) +func (x *Rat) GobEncode() ([]byte, error) { + 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 := x.b.bytes(buf) + j := x.a.abs.bytes(buf[0:i]) n := i - j if int(uint32(n)) != n { // this should never happen @@ -407,7 +407,7 @@ func (z *Rat) GobEncode() ([]byte, error) { binary.BigEndian.PutUint32(buf[j-4:j], uint32(n)) j -= 1 + 4 b := ratGobVersion << 1 // make space for sign bit - if z.a.neg { + if x.a.neg { b |= 1 } buf[j] = b