diff options
| author | Robert Griesemer <gri@golang.org> | 2015-03-04 17:00:41 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-03-12 18:41:45 +0000 |
| commit | 363617c7d3afdc9df3caf49a240679d6b6c7cc4b (patch) | |
| tree | 80524a7551907793d0cf27e671a43a021fab2733 /src/math/big/float_test.go | |
| parent | 0ff7c3ea458dc9162333efa5ffd1ff9dccfd4fe8 (diff) | |
| download | go-363617c7d3afdc9df3caf49a240679d6b6c7cc4b.tar.xz | |
math/big: added (internal) Float.form field for easier case distinctions
This is a fairly significant _internal_ representation change. Instead
of encoding 0, finite, infinite, and NaN values with special mantissa
and exponent values, a new (1 byte) 'form' field is used (without making
the Float struct bigger). The form field permits simpler and faster
case distinctions. As a side benefit, for zero and non-finite floats,
fewer fields need to be set. Also, the exponent range is not the full
int32 range (in the old format, infExp and nanExp were used to represent
Inf and NaN values and tests for those values sometimes didn't test
for the empty mantissa, so the range was reduced by 2 values).
The correspondence between the old and new fields is as follows.
Old representation:
x neg mant exp
---------------------------------------------------------------
+/-0 sign empty 0
0 < |x| < +Inf sign mantissa exponent
+/-Inf sign empty infExp
NaN false empty nanExp
New representation (- stands for ignored fields):
x neg mant exp form
---------------------------------------------------------------
+/-0 sign - - zero
0 < |x| < +Inf sign mantissa exponent finite
+/-Inf sign - - inf
NaN - - - nan
Client should not be affected by this change.
Change-Id: I7e355894d602ceb23f9ec01da755fe6e0386b101
Reviewed-on: https://go-review.googlesource.com/6870
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/float_test.go')
| -rw-r--r-- | src/math/big/float_test.go | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/math/big/float_test.go b/src/math/big/float_test.go index 281e099bd6..edd0056ff3 100644 --- a/src/math/big/float_test.go +++ b/src/math/big/float_test.go @@ -90,15 +90,20 @@ func TestFloatZeroValue(t *testing.T) { func makeFloat(s string) *Float { var x Float - if s == "Inf" || s == "+Inf" { + + switch s { + case "0": + return &x + case "-0": + return x.Neg(&x) + case "Inf", "+Inf": return x.SetInf(+1) - } - if s == "-Inf" { + case "-Inf": return x.SetInf(-1) - } - if s == "NaN" || s == "-NaN" { + case "NaN", "-NaN": return x.SetNaN() } + x.SetPrec(1000) if _, ok := x.SetString(s); !ok { panic(fmt.Sprintf("%q is not a valid float", s)) @@ -146,13 +151,6 @@ func TestFloatSetPrec(t *testing.T) { if got, acc := x.String(), x.Acc(); got != test.want || acc != test.acc { t.Errorf("%s.SetPrec(%d) = %s (%s); want %s (%s)", test.x, test.prec, got, acc, test.want, test.acc) } - // look inside x and check correct value for x.exp - if len(x.mant) == 0 { - // ±0, ±Inf, or NaN - if x.exp != 0 && x.exp > MinExp { - t.Errorf("%s.SetPrec(%d): incorrect exponent %d", test.x, test.prec, x.exp) - } - } } } @@ -209,7 +207,7 @@ func feq(x, y *Float) bool { if x.IsNaN() || y.IsNaN() { return x.IsNaN() && y.IsNaN() } - return x.Cmp(y) == 0 && x.neg == y.neg + return x.Cmp(y) == 0 && x.IsNeg() == y.IsNeg() } func TestFloatMantExp(t *testing.T) { @@ -261,11 +259,11 @@ func TestFloatSetMantExp(t *testing.T) { {"Inf", 1234, "+Inf"}, {"+Inf", -1234, "+Inf"}, {"-Inf", -1234, "-Inf"}, - {"0", -MaxExp - 1, "0"}, - {"0.5", -MaxExp - 1, "+0"}, // exponent underflow - {"-0.5", -MaxExp - 1, "-0"}, // exponent underflow - {"1", MaxExp, "+Inf"}, // exponent overflow - {"2", MaxExp - 1, "+Inf"}, // exponent overflow + {"0", MinExp, "0"}, + {"0.25", MinExp, "+0"}, // exponent underflow + {"-0.25", MinExp, "-0"}, // exponent underflow + {"1", MaxExp, "+Inf"}, // exponent overflow + {"2", MaxExp - 1, "+Inf"}, // exponent overflow {"0.75", 1, "1.5"}, {"0.5", 11, "1024"}, {"-0.5", -2, "-0.125"}, |
