Commit 1b37ba93 authored by Robert Griesemer's avatar Robert Griesemer

math/big: minor cleanups

- comment fixes
- s/z/x/ in (*rat).Float64 to match convention for functions
  returning a non-*Rat
- minor test output tweaking

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/8327044
parent 4f1ef563
...@@ -53,7 +53,7 @@ func (z *Int) SetInt64(x int64) *Int { ...@@ -53,7 +53,7 @@ func (z *Int) SetInt64(x int64) *Int {
// SetUint64 sets z to x and returns z. // SetUint64 sets z to x and returns z.
func (z *Int) SetUint64(x uint64) *Int { func (z *Int) SetUint64(x uint64) *Int {
z.abs = z.abs.setUint64(uint64(x)) z.abs = z.abs.setUint64(x)
z.neg = false z.neg = false
return z return z
} }
...@@ -513,13 +513,7 @@ func (z *Int) Scan(s fmt.ScanState, ch rune) error { ...@@ -513,13 +513,7 @@ func (z *Int) Scan(s fmt.ScanState, ch rune) error {
// Int64 returns the int64 representation of x. // Int64 returns the int64 representation of x.
// If x cannot be represented in an int64, the result is undefined. // If x cannot be represented in an int64, the result is undefined.
func (x *Int) Int64() int64 { func (x *Int) Int64() int64 {
if len(x.abs) == 0 { v := int64(x.Uint64())
return 0
}
v := int64(x.abs[0])
if _W == 32 && len(x.abs) > 1 {
v |= int64(x.abs[1]) << 32
}
if x.neg { if x.neg {
v = -v v = -v
} }
...@@ -527,7 +521,7 @@ func (x *Int) Int64() int64 { ...@@ -527,7 +521,7 @@ func (x *Int) Int64() int64 {
} }
// Uint64 returns the uint64 representation of x. // Uint64 returns the uint64 representation of x.
// If x cannot be represented in an uint64, the result is undefined. // If x cannot be represented in a uint64, the result is undefined.
func (x *Int) Uint64() uint64 { func (x *Int) Uint64() uint64 {
if len(x.abs) == 0 { if len(x.abs) == 0 {
return 0 return 0
......
...@@ -163,16 +163,16 @@ func quotToFloat(a, b nat) (f float64, exact bool) { ...@@ -163,16 +163,16 @@ func quotToFloat(a, b nat) (f float64, exact bool) {
return return
} }
// Float64 returns the nearest float64 value to z. // Float64 returns the nearest float64 value for x and a bool indicating
// If z is exactly representable as a float64, Float64 returns exact=true. // whether f represents x exactly. The sign of f always matches the sign
// If z is negative, so too is f, even if f==0. // of x, even if f == 0.
func (z *Rat) Float64() (f float64, exact bool) { func (x *Rat) Float64() (f float64, exact bool) {
b := z.b.abs b := x.b.abs
if len(b) == 0 { if len(b) == 0 {
b = b.set(natOne) // materialize denominator b = b.set(natOne) // materialize denominator
} }
f, exact = quotToFloat(z.a.abs, b) f, exact = quotToFloat(x.a.abs, b)
if z.a.neg { if x.a.neg {
f = -f f = -f
} }
return return
......
...@@ -503,9 +503,7 @@ func TestIssue3521(t *testing.T) { ...@@ -503,9 +503,7 @@ func TestIssue3521(t *testing.T) {
// Test inputs to Rat.SetString. The prefix "long:" causes the test // Test inputs to Rat.SetString. The prefix "long:" causes the test
// to be skipped in --test.short mode. (The threshold is about 500us.) // to be skipped in --test.short mode. (The threshold is about 500us.)
var float64inputs = []string{ var float64inputs = []string{
//
// Constants plundered from strconv/testfp.txt. // Constants plundered from strconv/testfp.txt.
//
// Table 1: Stress Inputs for Conversion to 53-bit Binary, < 1/2 ULP // Table 1: Stress Inputs for Conversion to 53-bit Binary, < 1/2 ULP
"5e+125", "5e+125",
...@@ -583,9 +581,7 @@ var float64inputs = []string{ ...@@ -583,9 +581,7 @@ var float64inputs = []string{
"75224575729e-45", "75224575729e-45",
"459926601011e+15", "459926601011e+15",
//
// Constants plundered from strconv/atof_test.go. // Constants plundered from strconv/atof_test.go.
//
"0", "0",
"1", "1",
...@@ -734,7 +730,7 @@ func TestFloat64SpecialCases(t *testing.T) { ...@@ -734,7 +730,7 @@ func TestFloat64SpecialCases(t *testing.T) {
case f == 0 && r.Num().BitLen() == 0: case f == 0 && r.Num().BitLen() == 0:
// Ok: Rat(0) is equivalent to both +/- float64(0). // Ok: Rat(0) is equivalent to both +/- float64(0).
default: default:
t.Errorf("strconv.ParseFloat(%q) = %g (%b), want %g (%b); delta=%g", input, e, e, f, f, f-e) t.Errorf("strconv.ParseFloat(%q) = %g (%b), want %g (%b); delta = %g", input, e, e, f, f, f-e)
} }
} }
...@@ -795,7 +791,7 @@ func TestFloat64Distribution(t *testing.T) { ...@@ -795,7 +791,7 @@ func TestFloat64Distribution(t *testing.T) {
if !checkIsBestApprox(t, f, r) { if !checkIsBestApprox(t, f, r) {
// Append context information. // Append context information.
t.Errorf("(input was mantissa %#x, exp %d; f=%g (%b); f~%g; r=%v)", t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)",
b, exp, f, f, math.Ldexp(float64(b), exp), r) b, exp, f, f, math.Ldexp(float64(b), exp), r)
} }
...@@ -830,7 +826,7 @@ func checkNonLossyRoundtrip(t *testing.T, f float64) { ...@@ -830,7 +826,7 @@ func checkNonLossyRoundtrip(t *testing.T, f float64) {
} }
f2, exact := r.Float64() f2, exact := r.Float64()
if f != f2 || !exact { if f != f2 || !exact {
t.Errorf("Rat.SetFloat64(%g).Float64() = %g (%b), %v, want %g (%b), %v; delta=%b", t.Errorf("Rat.SetFloat64(%g).Float64() = %g (%b), %v, want %g (%b), %v; delta = %b",
f, f2, f2, exact, f, f, true, f2-f) f, f2, f2, exact, f, f, true, f2-f)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment