big: usable zero Rat values without need for explicit initialization

- no explicit API change, but new(big.Rat) now creates a big.Rat value
  of 0 that is immediately usable, in sync. w/ the conventions elsewhere
- various cleanups along the way

R=r
CC=golang-dev
https://golang.org/cl/5301056
This commit is contained in:
Robert Griesemer 2011-10-21 14:11:36 -07:00
parent d481d7c854
commit 696ced50fe
5 changed files with 147 additions and 63 deletions

View File

@ -176,7 +176,7 @@ func (z *Int) Quo(x, y *Int) *Int {
// If y == 0, a division-by-zero run-time panic occurs. // If y == 0, a division-by-zero run-time panic occurs.
// Rem implements truncated modulus (like Go); see QuoRem for more details. // Rem implements truncated modulus (like Go); see QuoRem for more details.
func (z *Int) Rem(x, y *Int) *Int { func (z *Int) Rem(x, y *Int) *Int {
_, z.abs = nat(nil).div(z.abs, x.abs, y.abs) _, z.abs = nat{}.div(z.abs, x.abs, y.abs)
z.neg = len(z.abs) > 0 && x.neg // 0 has no sign z.neg = len(z.abs) > 0 && x.neg // 0 has no sign
return z return z
} }

View File

@ -35,7 +35,7 @@ import (
// During arithmetic operations, denormalized values may occur but are // During arithmetic operations, denormalized values may occur but are
// always normalized before returning the final result. The normalized // always normalized before returning the final result. The normalized
// representation of 0 is the empty or nil slice (length = 0). // representation of 0 is the empty or nil slice (length = 0).
//
type nat []Word type nat []Word
var ( var (
@ -447,10 +447,10 @@ func (z nat) mulRange(a, b uint64) nat {
case a == b: case a == b:
return z.setUint64(a) return z.setUint64(a)
case a+1 == b: case a+1 == b:
return z.mul(nat(nil).setUint64(a), nat(nil).setUint64(b)) return z.mul(nat{}.setUint64(a), nat{}.setUint64(b))
} }
m := (a + b) / 2 m := (a + b) / 2
return z.mul(nat(nil).mulRange(a, m), nat(nil).mulRange(m+1, b)) return z.mul(nat{}.mulRange(a, m), nat{}.mulRange(m+1, b))
} }
// q = (x-r)/y, with 0 <= r < y // q = (x-r)/y, with 0 <= r < y
@ -589,7 +589,6 @@ func (x nat) bitLen() int {
// MaxBase is the largest number base accepted for string conversions. // MaxBase is the largest number base accepted for string conversions.
const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1 const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1
func hexValue(ch int) Word { func hexValue(ch int) Word {
d := MaxBase + 1 // illegal base d := MaxBase + 1 // illegal base
switch { switch {
@ -786,7 +785,7 @@ func (x nat) string(charset string) string {
} }
// preserve x, create local copy for use in repeated divisions // preserve x, create local copy for use in repeated divisions
q := nat(nil).set(x) q := nat{}.set(x)
var r Word var r Word
// convert // convert
@ -1192,11 +1191,11 @@ func (n nat) probablyPrime(reps int) bool {
return false return false
} }
nm1 := nat(nil).sub(n, natOne) nm1 := nat{}.sub(n, natOne)
// 1<<k * q = nm1; // 1<<k * q = nm1;
q, k := nm1.powersOfTwoDecompose() q, k := nm1.powersOfTwoDecompose()
nm3 := nat(nil).sub(nm1, natTwo) nm3 := nat{}.sub(nm1, natTwo)
rand := rand.New(rand.NewSource(int64(n[0]))) rand := rand.New(rand.NewSource(int64(n[0])))
var x, y, quotient nat var x, y, quotient nat

View File

@ -67,7 +67,7 @@ var prodNN = []argNN{
func TestSet(t *testing.T) { func TestSet(t *testing.T) {
for _, a := range sumNN { for _, a := range sumNN {
z := nat(nil).set(a.z) z := nat{}.set(a.z)
if z.cmp(a.z) != 0 { if z.cmp(a.z) != 0 {
t.Errorf("got z = %v; want %v", z, a.z) t.Errorf("got z = %v; want %v", z, a.z)
} }
@ -129,7 +129,7 @@ var mulRangesN = []struct {
func TestMulRangeN(t *testing.T) { func TestMulRangeN(t *testing.T) {
for i, r := range mulRangesN { for i, r := range mulRangesN {
prod := nat(nil).mulRange(r.a, r.b).decimalString() prod := nat{}.mulRange(r.a, r.b).decimalString()
if prod != r.prod { if prod != r.prod {
t.Errorf("#%d: got %s; want %s", i, prod, r.prod) t.Errorf("#%d: got %s; want %s", i, prod, r.prod)
} }
@ -175,7 +175,7 @@ func toString(x nat, charset string) string {
s := make([]byte, i) s := make([]byte, i)
// don't destroy x // don't destroy x
q := nat(nil).set(x) q := nat{}.set(x)
// convert // convert
for len(q) > 0 { for len(q) > 0 {
@ -212,7 +212,7 @@ func TestString(t *testing.T) {
t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s) t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s)
} }
x, b, err := nat(nil).scan(strings.NewReader(a.s), len(a.c)) x, b, err := nat{}.scan(strings.NewReader(a.s), len(a.c))
if x.cmp(a.x) != 0 { if x.cmp(a.x) != 0 {
t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x) t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
} }
@ -271,7 +271,7 @@ var natScanTests = []struct {
func TestScanBase(t *testing.T) { func TestScanBase(t *testing.T) {
for _, a := range natScanTests { for _, a := range natScanTests {
r := strings.NewReader(a.s) r := strings.NewReader(a.s)
x, b, err := nat(nil).scan(r, a.base) x, b, err := nat{}.scan(r, a.base)
if err == nil && !a.ok { if err == nil && !a.ok {
t.Errorf("scan%+v\n\texpected error", a) t.Errorf("scan%+v\n\texpected error", a)
} }
@ -651,17 +651,17 @@ var expNNTests = []struct {
func TestExpNN(t *testing.T) { func TestExpNN(t *testing.T) {
for i, test := range expNNTests { for i, test := range expNNTests {
x, _, _ := nat(nil).scan(strings.NewReader(test.x), 0) x, _, _ := nat{}.scan(strings.NewReader(test.x), 0)
y, _, _ := nat(nil).scan(strings.NewReader(test.y), 0) y, _, _ := nat{}.scan(strings.NewReader(test.y), 0)
out, _, _ := nat(nil).scan(strings.NewReader(test.out), 0) out, _, _ := nat{}.scan(strings.NewReader(test.out), 0)
var m nat var m nat
if len(test.m) > 0 { if len(test.m) > 0 {
m, _, _ = nat(nil).scan(strings.NewReader(test.m), 0) m, _, _ = nat{}.scan(strings.NewReader(test.m), 0)
} }
z := nat(nil).expNN(x, y, m) z := nat{}.expNN(x, y, m)
if z.cmp(out) != 0 { if z.cmp(out) != 0 {
t.Errorf("#%d got %v want %v", i, z, out) t.Errorf("#%d got %v want %v", i, z, out)
} }

View File

@ -14,10 +14,10 @@ import (
) )
// A Rat represents a quotient a/b of arbitrary precision. // A Rat represents a quotient a/b of arbitrary precision.
// The zero value for a Rat, 0/0, is not a legal Rat. // The zero value for a Rat represents the value 0.
type Rat struct { type Rat struct {
a Int a Int
b nat b nat // len(b) == 0 acts like b == 1
} }
// NewRat creates a new Rat with numerator a and denominator b. // NewRat creates a new Rat with numerator a and denominator b.
@ -29,8 +29,11 @@ func NewRat(a, b int64) *Rat {
func (z *Rat) SetFrac(a, b *Int) *Rat { func (z *Rat) SetFrac(a, b *Int) *Rat {
z.a.neg = a.neg != b.neg z.a.neg = a.neg != b.neg
babs := b.abs babs := b.abs
if len(babs) == 0 {
panic("division by zero")
}
if &z.a == b || alias(z.a.abs, babs) { if &z.a == b || alias(z.a.abs, babs) {
babs = nat(nil).set(babs) // make a copy babs = nat{}.set(babs) // make a copy
} }
z.a.abs = z.a.abs.set(a.abs) z.a.abs = z.a.abs.set(a.abs)
z.b = z.b.set(babs) z.b = z.b.set(babs)
@ -40,6 +43,9 @@ func (z *Rat) SetFrac(a, b *Int) *Rat {
// SetFrac64 sets z to a/b and returns z. // SetFrac64 sets z to a/b and returns z.
func (z *Rat) SetFrac64(a, b int64) *Rat { func (z *Rat) SetFrac64(a, b int64) *Rat {
z.a.SetInt64(a) z.a.SetInt64(a)
if b == 0 {
panic("division by zero")
}
if b < 0 { if b < 0 {
b = -b b = -b
z.a.neg = !z.a.neg z.a.neg = !z.a.neg
@ -51,14 +57,14 @@ func (z *Rat) SetFrac64(a, b int64) *Rat {
// SetInt sets z to x (by making a copy of x) and returns z. // SetInt sets z to x (by making a copy of x) and returns z.
func (z *Rat) SetInt(x *Int) *Rat { func (z *Rat) SetInt(x *Int) *Rat {
z.a.Set(x) z.a.Set(x)
z.b = z.b.setWord(1) z.b = z.b.make(0)
return z return z
} }
// SetInt64 sets z to x and returns z. // SetInt64 sets z to x and returns z.
func (z *Rat) SetInt64(x int64) *Rat { func (z *Rat) SetInt64(x int64) *Rat {
z.a.SetInt64(x) z.a.SetInt64(x)
z.b = z.b.setWord(1) z.b = z.b.make(0)
return z return z
} }
@ -91,7 +97,15 @@ func (z *Rat) Inv(x *Rat) *Rat {
panic("division by zero") panic("division by zero")
} }
z.Set(x) z.Set(x)
z.a.abs, z.b = z.b, z.a.abs // sign doesn't change a := z.b
if len(a) == 0 {
a = a.setWord(1) // materialize numerator
}
b := z.a.abs
if b.cmp(natOne) == 0 {
b = b.make(0) // normalize denominator
}
z.a.abs, z.b = a, b // sign doesn't change
return z return z
} }
@ -107,21 +121,24 @@ func (x *Rat) Sign() int {
// IsInt returns true if the denominator of x is 1. // IsInt returns true if the denominator of x is 1.
func (x *Rat) IsInt() bool { func (x *Rat) IsInt() bool {
return len(x.b) == 1 && x.b[0] == 1 return len(x.b) == 0 || x.b.cmp(natOne) == 0
} }
// Num returns the numerator of z; it may be <= 0. // Num returns the numerator of x; it may be <= 0.
// The result is a reference to z's numerator; it // The result is a reference to x's numerator; it
// may change if a new value is assigned to z. // may change if a new value is assigned to x.
func (z *Rat) Num() *Int { func (x *Rat) Num() *Int {
return &z.a return &x.a
} }
// Denom returns the denominator of z; it is always > 0. // Denom returns the denominator of x; it is always > 0.
// The result is a reference to z's denominator; it // The result is a reference to x's denominator; it
// may change if a new value is assigned to z. // may change if a new value is assigned to x.
func (z *Rat) Denom() *Int { func (x *Rat) Denom() *Int {
return &Int{false, z.b} if len(x.b) == 0 {
return &Int{abs: nat{1}}
}
return &Int{abs: x.b}
} }
func gcd(x, y nat) nat { func gcd(x, y nat) nat {
@ -139,24 +156,47 @@ func gcd(x, y nat) nat {
} }
func (z *Rat) norm() *Rat { func (z *Rat) norm() *Rat {
f := gcd(z.a.abs, z.b) switch {
if len(z.a.abs) == 0 { case len(z.a.abs) == 0:
// z == 0 // z == 0 - normalize sign and denominator
z.a.neg = false // normalize sign z.a.neg = false
z.b = z.b.setWord(1) z.b = z.b.make(0)
return z case len(z.b) == 0:
} // z is normalized int - nothing to do
if f.cmp(natOne) != 0 { case z.b.cmp(natOne) == 0:
// z is int - normalize denominator
z.b = z.b.make(0)
default:
if f := gcd(z.a.abs, z.b); f.cmp(natOne) != 0 {
z.a.abs, _ = z.a.abs.div(nil, z.a.abs, f) z.a.abs, _ = z.a.abs.div(nil, z.a.abs, f)
z.b, _ = z.b.div(nil, z.b, f) z.b, _ = z.b.div(nil, z.b, f)
} }
}
return z return z
} }
func mulNat(x *Int, y nat) *Int { // mulDenom sets z to the denominator product x*y (by taking into
// account that 0 values for x or y must be interpreted as 1) and
// returns z.
func mulDenom(z, x, y nat) nat {
switch {
case len(x) == 0:
return z.set(y)
case len(y) == 0:
return z.set(x)
}
return z.mul(x, y)
}
// scaleDenom computes x*f.
// If f == 0 (zero value of denominator), the result is (a copy of) x.
func scaleDenom(x *Int, f nat) *Int {
var z Int var z Int
z.abs = z.abs.mul(x.abs, y) if len(f) == 0 {
z.neg = len(z.abs) > 0 && x.neg return z.Set(x)
}
z.abs = z.abs.mul(x.abs, f)
z.neg = x.neg
return &z return &z
} }
@ -167,31 +207,31 @@ func mulNat(x *Int, y nat) *Int {
// +1 if x > y // +1 if x > y
// //
func (x *Rat) Cmp(y *Rat) int { func (x *Rat) Cmp(y *Rat) int {
return mulNat(&x.a, y.b).Cmp(mulNat(&y.a, x.b)) return scaleDenom(&x.a, y.b).Cmp(scaleDenom(&y.a, x.b))
} }
// Add sets z to the sum x+y and returns z. // Add sets z to the sum x+y and returns z.
func (z *Rat) Add(x, y *Rat) *Rat { func (z *Rat) Add(x, y *Rat) *Rat {
a1 := mulNat(&x.a, y.b) a1 := scaleDenom(&x.a, y.b)
a2 := mulNat(&y.a, x.b) a2 := scaleDenom(&y.a, x.b)
z.a.Add(a1, a2) z.a.Add(a1, a2)
z.b = z.b.mul(x.b, y.b) z.b = mulDenom(z.b, x.b, y.b)
return z.norm() return z.norm()
} }
// Sub sets z to the difference x-y and returns z. // Sub sets z to the difference x-y and returns z.
func (z *Rat) Sub(x, y *Rat) *Rat { func (z *Rat) Sub(x, y *Rat) *Rat {
a1 := mulNat(&x.a, y.b) a1 := scaleDenom(&x.a, y.b)
a2 := mulNat(&y.a, x.b) a2 := scaleDenom(&y.a, x.b)
z.a.Sub(a1, a2) z.a.Sub(a1, a2)
z.b = z.b.mul(x.b, y.b) z.b = mulDenom(z.b, x.b, y.b)
return z.norm() return z.norm()
} }
// Mul sets z to the product x*y and returns z. // Mul sets z to the product x*y and returns z.
func (z *Rat) Mul(x, y *Rat) *Rat { func (z *Rat) Mul(x, y *Rat) *Rat {
z.a.Mul(&x.a, &y.a) z.a.Mul(&x.a, &y.a)
z.b = z.b.mul(x.b, y.b) z.b = mulDenom(z.b, x.b, y.b)
return z.norm() return z.norm()
} }
@ -201,8 +241,8 @@ func (z *Rat) Quo(x, y *Rat) *Rat {
if len(y.a.abs) == 0 { if len(y.a.abs) == 0 {
panic("division by zero") panic("division by zero")
} }
a := mulNat(&x.a, y.b) a := scaleDenom(&x.a, y.b)
b := mulNat(&y.a, x.b) b := scaleDenom(&y.a, x.b)
z.a.abs = a.abs z.a.abs = a.abs
z.b = b.abs z.b = b.abs
z.a.neg = a.neg != b.neg z.a.neg = a.neg != b.neg
@ -281,7 +321,7 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
z.norm() z.norm()
} else { } else {
z.a.abs = z.a.abs.mul(z.a.abs, powTen) z.a.abs = z.a.abs.mul(z.a.abs, powTen)
z.b = z.b.setWord(1) z.b = z.b.make(0)
} }
return z, true return z, true
@ -289,7 +329,11 @@ 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 (z *Rat) String() string {
return z.a.String() + "/" + z.b.decimalString() s := "/1"
if len(z.b) != 0 {
s = "/" + z.b.decimalString()
}
return z.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,
@ -311,6 +355,7 @@ func (z *Rat) FloatString(prec int) string {
} }
return s return s
} }
// z.b != 0
q, r := nat{}.div(nat{}, z.a.abs, z.b) q, r := nat{}.div(nat{}, z.a.abs, z.b)

View File

@ -11,6 +11,46 @@ import (
"testing" "testing"
) )
func TestZeroRat(t *testing.T) {
var x, y, z Rat
y.SetFrac64(0, 42)
if x.Cmp(&y) != 0 {
t.Errorf("x and y should be both equal and zero")
}
if s := x.String(); s != "0/1" {
t.Errorf("got x = %s, want 0/1", s)
}
if s := x.RatString(); s != "0" {
t.Errorf("got x = %s, want 0", s)
}
z.Add(&x, &y)
if s := z.RatString(); s != "0" {
t.Errorf("got x+y = %s, want 0", s)
}
z.Sub(&x, &y)
if s := z.RatString(); s != "0" {
t.Errorf("got x-y = %s, want 0", s)
}
z.Mul(&x, &y)
if s := z.RatString(); s != "0" {
t.Errorf("got x*y = %s, want 0", s)
}
// check for division by zero
defer func() {
if s := recover(); s == nil || s.(string) != "division by zero" {
panic(s)
}
}()
z.Quo(&x, &y)
}
var setStringTests = []struct { var setStringTests = []struct {
in, out string in, out string
ok bool ok bool
@ -174,7 +214,7 @@ func TestIsInt(t *testing.T) {
} }
func TestRatAbs(t *testing.T) { func TestRatAbs(t *testing.T) {
zero := NewRat(0, 1) zero := new(Rat)
for _, a := range setStringTests { for _, a := range setStringTests {
x, ok := new(Rat).SetString(a.in) x, ok := new(Rat).SetString(a.in)
if !ok { if !ok {
@ -192,7 +232,7 @@ func TestRatAbs(t *testing.T) {
} }
func TestRatNeg(t *testing.T) { func TestRatNeg(t *testing.T) {
zero := NewRat(0, 1) zero := new(Rat)
for _, a := range setStringTests { for _, a := range setStringTests {
x, ok := new(Rat).SetString(a.in) x, ok := new(Rat).SetString(a.in)
if !ok { if !ok {
@ -207,7 +247,7 @@ func TestRatNeg(t *testing.T) {
} }
func TestRatInv(t *testing.T) { func TestRatInv(t *testing.T) {
zero := NewRat(0, 1) zero := new(Rat)
for _, a := range setStringTests { for _, a := range setStringTests {
x, ok := new(Rat).SetString(a.in) x, ok := new(Rat).SetString(a.in)
if !ok { if !ok {