diff options
Diffstat (limited to 'libgo/go/math')
-rw-r--r-- | libgo/go/math/asin.go | 4 | ||||
-rw-r--r-- | libgo/go/math/atan.go | 2 | ||||
-rw-r--r-- | libgo/go/math/big/int.go | 18 | ||||
-rw-r--r-- | libgo/go/math/big/int_test.go | 28 | ||||
-rw-r--r-- | libgo/go/math/big/nat_test.go | 2 | ||||
-rw-r--r-- | libgo/go/math/big/rat.go | 12 | ||||
-rw-r--r-- | libgo/go/math/big/rat_test.go | 26 | ||||
-rw-r--r-- | libgo/go/math/bits.go | 10 | ||||
-rw-r--r-- | libgo/go/math/rand/rand.go | 42 | ||||
-rw-r--r-- | libgo/go/math/sin.go | 4 | ||||
-rw-r--r-- | libgo/go/math/tan.go | 2 |
11 files changed, 115 insertions, 35 deletions
diff --git a/libgo/go/math/asin.go b/libgo/go/math/asin.go index 0d4fa9ebb50..46a5fe9d808 100644 --- a/libgo/go/math/asin.go +++ b/libgo/go/math/asin.go @@ -11,7 +11,7 @@ package math after appropriate range reduction. */ -// Asin returns the arcsine of x. +// Asin returns the arcsine, in radians, of x. // // Special cases are: // Asin(±0) = ±0 @@ -50,7 +50,7 @@ func asin(x float64) float64 { return temp } -// Acos returns the arccosine of x. +// Acos returns the arccosine, in radians, of x. // // Special case is: // Acos(x) = NaN if x < -1 or x > 1 diff --git a/libgo/go/math/atan.go b/libgo/go/math/atan.go index 986ba068661..d942bce0968 100644 --- a/libgo/go/math/atan.go +++ b/libgo/go/math/atan.go @@ -87,7 +87,7 @@ func satan(x float64) float64 { return Pi/4 + xatan((x-1)/(x+1)) + 0.5*Morebits } -// Atan returns the arctangent of x. +// Atan returns the arctangent, in radians, of x. // // Special cases are: // Atan(±0) = ±0 diff --git a/libgo/go/math/big/int.go b/libgo/go/math/big/int.go index d1b5602d668..7bbb152d79c 100644 --- a/libgo/go/math/big/int.go +++ b/libgo/go/math/big/int.go @@ -563,13 +563,13 @@ func (z *Int) SetBytes(buf []byte) *Int { return z } -// Bytes returns the absolute value of z as a big-endian byte slice. +// Bytes returns the absolute value of x as a big-endian byte slice. func (x *Int) Bytes() []byte { buf := make([]byte, len(x.abs)*_S) return buf[x.abs.bytes(buf):] } -// BitLen returns the length of the absolute value of z in bits. +// BitLen returns the length of the absolute value of x in bits. // The bit length of 0 is 0. func (x *Int) BitLen() int { return x.abs.bitLen() @@ -703,14 +703,15 @@ func (z *Int) binaryGCD(a, b *Int) *Int { // reduce t t.Rsh(t, t.abs.trailingZeroBits()) if t.neg { - v.Neg(t) + v, t = t, v + v.neg = len(v.abs) > 0 && !v.neg // 0 has no sign } else { - u.Set(t) + u, t = t, u } t.Sub(u, v) } - return u.Lsh(u, k) + return z.Lsh(u, k) } // ProbablyPrime performs n Miller-Rabin tests to check whether x is prime. @@ -951,6 +952,9 @@ const intGobVersion byte = 1 // GobEncode implements the gob.GobEncoder interface. func (x *Int) GobEncode() ([]byte, error) { + if x == nil { + return nil, nil + } buf := make([]byte, 1+len(x.abs)*_S) // extra byte for version and sign bit i := x.abs.bytes(buf) - 1 // i >= 0 b := intGobVersion << 1 // make space for sign bit @@ -964,7 +968,9 @@ func (x *Int) GobEncode() ([]byte, error) { // GobDecode implements the gob.GobDecoder interface. func (z *Int) GobDecode(buf []byte) error { if len(buf) == 0 { - return errors.New("Int.GobDecode: no data") + // Other side sent a nil or default value. + *z = Int{} + return nil } b := buf[0] if b>>1 != intGobVersion { diff --git a/libgo/go/math/big/int_test.go b/libgo/go/math/big/int_test.go index 6c981e7752e..87b975d5c4b 100644 --- a/libgo/go/math/big/int_test.go +++ b/libgo/go/math/big/int_test.go @@ -89,7 +89,7 @@ func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) { var z Int f(&z, a.x, a.y) if !isNormalized(&z) { - t.Errorf("%s%v is not normalized", z, msg) + t.Errorf("%s%v is not normalized", msg, z) } if (&z).Cmp(a.z) != 0 { t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z) @@ -1484,6 +1484,32 @@ func TestIntGobEncoding(t *testing.T) { } } +// Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero. +// TODO: top-level nils. +func TestGobEncodingNilIntInSlice(t *testing.T) { + buf := new(bytes.Buffer) + enc := gob.NewEncoder(buf) + dec := gob.NewDecoder(buf) + + var in = make([]*Int, 1) + err := enc.Encode(&in) + if err != nil { + t.Errorf("gob encode failed: %q", err) + } + var out []*Int + err = dec.Decode(&out) + if err != nil { + t.Fatalf("gob decode failed: %q", err) + } + if len(out) != 1 { + t.Fatalf("wrong len; want 1 got %d", len(out)) + } + var zero Int + if out[0].Cmp(&zero) != 0 { + t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out) + } +} + func TestIntJSONEncoding(t *testing.T) { for _, test := range encodingTests { var tx Int diff --git a/libgo/go/math/big/nat_test.go b/libgo/go/math/big/nat_test.go index 2dd7bf63968..1d4dfe80d3d 100644 --- a/libgo/go/math/big/nat_test.go +++ b/libgo/go/math/big/nat_test.go @@ -685,7 +685,7 @@ func runModWTests(t *testing.T, tests []modWTest) { r := in.abs.modW(d.abs[0]) if r != out.abs[0] { - t.Errorf("#%d failed: got %s want %s", i, r, out) + t.Errorf("#%d failed: got %d want %s", i, r, out) } } } diff --git a/libgo/go/math/big/rat.go b/libgo/go/math/big/rat.go index 75d044fe21d..7faee61a465 100644 --- a/libgo/go/math/big/rat.go +++ b/libgo/go/math/big/rat.go @@ -164,8 +164,9 @@ func quotToFloat(a, b nat) (f float64, exact bool) { } // Float64 returns the nearest float64 value for x and a bool indicating -// whether f represents x exactly. The sign of f always matches the sign -// of x, even if f == 0. +// whether f represents x exactly. If the magnitude of x is too large to +// be represented by a float64, f is an infinity and exact is false. +// The sign of f always matches the sign of x, even if f == 0. func (x *Rat) Float64() (f float64, exact bool) { b := x.b.abs if len(b) == 0 { @@ -545,6 +546,9 @@ const ratGobVersion byte = 1 // GobEncode implements the gob.GobEncoder interface. func (x *Rat) GobEncode() ([]byte, error) { + if x == nil { + return nil, nil + } buf := make([]byte, 1+4+(len(x.a.abs)+len(x.b.abs))*_S) // extra bytes for version and sign bit (1), and numerator length (4) i := x.b.abs.bytes(buf) j := x.a.abs.bytes(buf[0:i]) @@ -566,7 +570,9 @@ func (x *Rat) GobEncode() ([]byte, error) { // GobDecode implements the gob.GobDecoder interface. func (z *Rat) GobDecode(buf []byte) error { if len(buf) == 0 { - return errors.New("Rat.GobDecode: no data") + // Other side sent a nil or default value. + *z = Rat{} + return nil } b := buf[0] if b>>1 != ratGobVersion { diff --git a/libgo/go/math/big/rat_test.go b/libgo/go/math/big/rat_test.go index 1c2c642379c..0d432637ba1 100644 --- a/libgo/go/math/big/rat_test.go +++ b/libgo/go/math/big/rat_test.go @@ -407,6 +407,32 @@ func TestRatGobEncoding(t *testing.T) { } } +// Sending a nil Rat pointer (inside a slice) on a round trip through gob should yield a zero. +// TODO: top-level nils. +func TestGobEncodingNilRatInSlice(t *testing.T) { + buf := new(bytes.Buffer) + enc := gob.NewEncoder(buf) + dec := gob.NewDecoder(buf) + + var in = make([]*Rat, 1) + err := enc.Encode(&in) + if err != nil { + t.Errorf("gob encode failed: %q", err) + } + var out []*Rat + err = dec.Decode(&out) + if err != nil { + t.Fatalf("gob decode failed: %q", err) + } + if len(out) != 1 { + t.Fatalf("wrong len; want 1 got %d", len(out)) + } + var zero Rat + if out[0].Cmp(&zero) != 0 { + t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out) + } +} + func TestIssue2379(t *testing.T) { // 1) no aliasing q := NewRat(3, 2) diff --git a/libgo/go/math/bits.go b/libgo/go/math/bits.go index 0df0b1cc9f1..d85ee9cb137 100644 --- a/libgo/go/math/bits.go +++ b/libgo/go/math/bits.go @@ -27,7 +27,7 @@ func Inf(sign int) float64 { // NaN returns an IEEE 754 ``not-a-number'' value. func NaN() float64 { return Float64frombits(uvnan) } -// IsNaN returns whether f is an IEEE 754 ``not-a-number'' value. +// IsNaN reports whether f is an IEEE 754 ``not-a-number'' value. func IsNaN(f float64) (is bool) { // IEEE 754 says that only NaNs satisfy f != f. // To avoid the floating-point hardware, could use: @@ -36,10 +36,10 @@ func IsNaN(f float64) (is bool) { return f != f } -// IsInf returns whether f is an infinity, according to sign. -// If sign > 0, IsInf returns whether f is positive infinity. -// If sign < 0, IsInf returns whether f is negative infinity. -// If sign == 0, IsInf returns whether f is either infinity. +// IsInf reports whether f is an infinity, according to sign. +// If sign > 0, IsInf reports whether f is positive infinity. +// If sign < 0, IsInf reports whether f is negative infinity. +// If sign == 0, IsInf reports whether f is either infinity. func IsInf(f float64, sign int) bool { // Test for infinity by comparing against maximum float. // To avoid the floating-point hardware, could use: diff --git a/libgo/go/math/rand/rand.go b/libgo/go/math/rand/rand.go index 94f84a85fbe..2157cdb4658 100644 --- a/libgo/go/math/rand/rand.go +++ b/libgo/go/math/rand/rand.go @@ -3,6 +3,12 @@ // license that can be found in the LICENSE file. // Package rand implements pseudo-random number generators. +// +// Random numbers are generated by a Source. Top-level functions, such as +// Float64 and Int, use a default shared Source that produces a deterministic +// sequence of values each time a program is run. Use the Seed function to +// initialize the default Source if different behavior is required for each run. +// The default Source is safe for concurrent use by multiple goroutines. package rand import "sync" @@ -113,47 +119,57 @@ func (r *Rand) Perm(n int) []int { var globalRand = New(&lockedSource{src: NewSource(1)}) -// Seed uses the provided seed value to initialize the generator to a +// Seed uses the provided seed value to initialize the default Source to a // deterministic state. If Seed is not called, the generator behaves as // if seeded by Seed(1). func Seed(seed int64) { globalRand.Seed(seed) } -// Int63 returns a non-negative pseudo-random 63-bit integer as an int64. +// Int63 returns a non-negative pseudo-random 63-bit integer as an int64 +// from the default Source. func Int63() int64 { return globalRand.Int63() } -// Uint32 returns a pseudo-random 32-bit value as a uint32. +// Uint32 returns a pseudo-random 32-bit value as a uint32 +// from the default Source. func Uint32() uint32 { return globalRand.Uint32() } -// Int31 returns a non-negative pseudo-random 31-bit integer as an int32. +// Int31 returns a non-negative pseudo-random 31-bit integer as an int32 +// from the default Source. func Int31() int32 { return globalRand.Int31() } -// Int returns a non-negative pseudo-random int. +// Int returns a non-negative pseudo-random int from the default Source. func Int() int { return globalRand.Int() } -// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n). +// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n) +// from the default Source. // It panics if n <= 0. func Int63n(n int64) int64 { return globalRand.Int63n(n) } -// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n). +// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n) +// from the default Source. // It panics if n <= 0. func Int31n(n int32) int32 { return globalRand.Int31n(n) } -// Intn returns, as an int, a non-negative pseudo-random number in [0,n). +// Intn returns, as an int, a non-negative pseudo-random number in [0,n) +// from the default Source. // It panics if n <= 0. func Intn(n int) int { return globalRand.Intn(n) } -// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0). +// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0) +// from the default Source. func Float64() float64 { return globalRand.Float64() } -// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0). +// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0) +// from the default Source. func Float32() float32 { return globalRand.Float32() } -// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n). +// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) +// from the default Source. func Perm(n int) []int { return globalRand.Perm(n) } // NormFloat64 returns a normally distributed float64 in the range // [-math.MaxFloat64, +math.MaxFloat64] with -// standard normal distribution (mean = 0, stddev = 1). +// standard normal distribution (mean = 0, stddev = 1) +// from the default Source. // To produce a different normal distribution, callers can // adjust the output using: // @@ -163,7 +179,7 @@ func NormFloat64() float64 { return globalRand.NormFloat64() } // ExpFloat64 returns an exponentially distributed float64 in the range // (0, +math.MaxFloat64] with an exponential distribution whose rate parameter -// (lambda) is 1 and whose mean is 1/lambda (1). +// (lambda) is 1 and whose mean is 1/lambda (1) from the default Source. // To produce a distribution with a different rate parameter, // callers can adjust the output using: // diff --git a/libgo/go/math/sin.go b/libgo/go/math/sin.go index 13fe408ba06..1c5491f6dc4 100644 --- a/libgo/go/math/sin.go +++ b/libgo/go/math/sin.go @@ -109,7 +109,7 @@ var _cos = [...]float64{ 4.16666666666665929218E-2, // 0x3fa555555555554b } -// Cos returns the cosine of x. +// Cos returns the cosine of the radian argument x. // // Special cases are: // Cos(±Inf) = NaN @@ -171,7 +171,7 @@ func cos(x float64) float64 { return y } -// Sin returns the sine of x. +// Sin returns the sine of the radian argument x. // // Special cases are: // Sin(±0) = ±0 diff --git a/libgo/go/math/tan.go b/libgo/go/math/tan.go index 640f404fbe6..e544b276b5d 100644 --- a/libgo/go/math/tan.go +++ b/libgo/go/math/tan.go @@ -73,7 +73,7 @@ var _tanQ = [...]float64{ -5.38695755929454629881E7, //0xc189afe03cbe5a31 } -// Tan returns the tangent of x. +// Tan returns the tangent of the radian argument x. // // Special cases are: // Tan(±0) = ±0 |