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
diff --git a/src/pkg/big/nat.go b/src/pkg/big/nat.go
index 33d6bb1..c0769d8 100644
--- a/src/pkg/big/nat.go
+++ b/src/pkg/big/nat.go
@@ -35,7 +35,7 @@
// During arithmetic operations, denormalized values may occur but are
// always normalized before returning the final result. The normalized
// representation of 0 is the empty or nil slice (length = 0).
-
+//
type nat []Word
var (
@@ -447,10 +447,10 @@
case a == b:
return z.setUint64(a)
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
- 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
@@ -589,7 +589,6 @@
// MaxBase is the largest number base accepted for string conversions.
const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1
-
func hexValue(ch int) Word {
d := MaxBase + 1 // illegal base
switch {
@@ -786,7 +785,7 @@
}
// preserve x, create local copy for use in repeated divisions
- q := nat(nil).set(x)
+ q := nat{}.set(x)
var r Word
// convert
@@ -1192,11 +1191,11 @@
return false
}
- nm1 := nat(nil).sub(n, natOne)
+ nm1 := nat{}.sub(n, natOne)
// 1<<k * q = nm1;
q, k := nm1.powersOfTwoDecompose()
- nm3 := nat(nil).sub(nm1, natTwo)
+ nm3 := nat{}.sub(nm1, natTwo)
rand := rand.New(rand.NewSource(int64(n[0])))
var x, y, quotient nat