diff options
| author | Robert Griesemer <gri@golang.org> | 2015-09-24 09:48:39 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-09-24 22:23:55 +0000 |
| commit | 3f7c3e01db49067645053e4bd66533a8cde1d308 (patch) | |
| tree | 93e0ae4c7ae963edda3682e6e4d73e563d90f1aa /src/math/big/floatconv_test.go | |
| parent | 44ab8bab1ce14eba5486b288a4930c832ea5342e (diff) | |
| download | go-3f7c3e01db49067645053e4bd66533a8cde1d308.tar.xz | |
math/big: fix test for denormalized inputs and enable more test cases
Also: removed unnecessary BUG comment (was fixed).
Change-Id: I8f11fbcb4e30a19ec5a25df742b3e25e2ee7f846
Reviewed-on: https://go-review.googlesource.com/14923
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatconv_test.go')
| -rw-r--r-- | src/math/big/floatconv_test.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/math/big/floatconv_test.go b/src/math/big/floatconv_test.go index a29f8a1369..cea8f82ca9 100644 --- a/src/math/big/floatconv_test.go +++ b/src/math/big/floatconv_test.go @@ -254,11 +254,10 @@ func TestFloat64Text(t *testing.T) { {above1e23, 'f', -1, "100000000000000010000000"}, {above1e23, 'g', -1, "1.0000000000000001e+23"}, - // TODO(gri) track down why these don't work yet - // {5e-304/1e20, 'g', -1, "5e-324"}, - // {-5e-304/1e20, 'g', -1, "-5e-324"}, - // {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, // avoid constant arithmetic - // {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic + {5e-304 / 1e20, 'g', -1, "5e-324"}, + {-5e-304 / 1e20, 'g', -1, "-5e-324"}, + {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, // avoid constant arithmetic + {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic {32, 'g', -1, "32"}, {32, 'g', 0, "3e+01"}, @@ -292,10 +291,16 @@ func TestFloat64Text(t *testing.T) { {383260575764816448, 'f', 0, "383260575764816448"}, {383260575764816448, 'g', -1, "3.8326057576481645e+17"}, } { - f := new(Float).SetFloat64(test.x) + // The test cases are from the strconv package which tests float64 values. + // When formatting values with prec = -1 (shortest representation), + // the actually available mantissa precision matters. + // For denormalized values, that precision is < 53 (SetFloat64 default). + // Compute and set the actual precision explicitly. + f := new(Float).SetPrec(actualPrec(test.x)).SetFloat64(test.x) got := f.Text(test.format, test.prec) if got != test.want { t.Errorf("%v: got %s; want %s", test, got, test.want) + continue } if test.format == 'b' && test.x == 0 { @@ -313,6 +318,15 @@ func TestFloat64Text(t *testing.T) { } } +// actualPrec returns the number of actually used mantissa bits. +func actualPrec(x float64) uint { + if bits := math.Float64bits(x); x != 0 && bits&(0x7ff<<52) == 0 { + // x is denormalized + return 64 - nlz64(bits&(1<<52-1)) + } + return 53 +} + func TestFloatText(t *testing.T) { for _, test := range []struct { x string |
