Testing Float64 (#82)

* Additional (and some breaking) tests for NewFromFloatWithExponent

* Addressing tests for NewFromFloatWithExponent

* Naming cosmetic correction

* removing unused code

* Improving FromFloatWithExponent

* Tests for exact float representation added

* Exact float representation in FromFloat

* Adding breaking test for NewFromFloat

* Fast path in FromFloat is unreliable, fixing it

* Addressing special meaning of zero exponent in float64

* NewFromFloatWithExponent: subnormals support

* NewFromFloatWithExponent: just a few additional test cases

* NewFromFloat: documentation update

* NewFromFloatWithExponent: optimization and some documentation

* NewFromFloatWithExponent: optimizations

* NewFromFloatWithExponent: optimizations

* Subnormal test added

* Float64: adding tests
This commit is contained in:
Igor Mikushkin 2018-03-02 01:31:53 +04:00 committed by Victor Quinn
parent 78e9b82f68
commit ca6009d707

View file

@ -18,31 +18,33 @@ type testEnt struct {
float float64 float float64
short string short string
exact string exact string
inexact string
} }
var testTable = []*testEnt{ var testTable = []*testEnt{
{3.141592653589793, "3.141592653589793", ""}, {3.141592653589793, "3.141592653589793", "", "3.14159265358979300000000000000000000000000000000000004"},
{3, "3", ""}, {3, "3", "", "3.0000000000000000000000002"},
{1234567890123456, "1234567890123456", ""}, {1234567890123456, "1234567890123456", "", "1234567890123456.00000000000000002"},
{1234567890123456000, "1234567890123456000", ""}, {1234567890123456000, "1234567890123456000", "", "1234567890123456000.0000000000000008"},
{1234.567890123456, "1234.567890123456", ""}, {1234.567890123456, "1234.567890123456", "", "1234.5678901234560000000000000009"},
{.1234567890123456, "0.1234567890123456", ""}, {.1234567890123456, "0.1234567890123456", "", "0.12345678901234560000000000006"},
{0, "0", ""}, {0, "0", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"},
{.1111111111111110, "0.111111111111111", ""}, {.1111111111111110, "0.111111111111111", "", "0.111111111111111000000000000000009"},
{.1111111111111111, "0.1111111111111111", ""}, {.1111111111111111, "0.1111111111111111", "", "0.111111111111111100000000000000000000023423545644534234"},
{.1111111111111119, "0.1111111111111119", ""}, {.1111111111111119, "0.1111111111111119", "", "0.111111111111111900000000000000000000000000000000000134123984192834"},
{.000000000000000001, "0.000000000000000001", ""}, {.000000000000000001, "0.000000000000000001", "", "0.00000000000000000100000000000000000000000000000000012341234"},
{.000000000000000002, "0.000000000000000002", ""}, {.000000000000000002, "0.000000000000000002", "", "0.0000000000000000020000000000000000000012341234123"},
{.000000000000000003, "0.000000000000000003", ""}, {.000000000000000003, "0.000000000000000003", "", "0.00000000000000000299999999999999999999999900000000000123412341234"},
{.000000000000000005, "0.000000000000000005", ""}, {.000000000000000005, "0.000000000000000005", "", "0.00000000000000000500000000000000000023412341234"},
{.000000000000000008, "0.000000000000000008", ""}, {.000000000000000008, "0.000000000000000008", "", "0.0000000000000000080000000000000000001241234432"},
{.1000000000000001, "0.1000000000000001", ""}, {.1000000000000001, "0.1000000000000001", "", "0.10000000000000010000000000000012341234"},
{.1000000000000002, "0.1000000000000002", ""}, {.1000000000000002, "0.1000000000000002", "", "0.10000000000000020000000000001234123412"},
{.1000000000000003, "0.1000000000000003", ""}, {.1000000000000003, "0.1000000000000003", "", "0.1000000000000003000000000000001234123412"},
{.1000000000000005, "0.1000000000000005", ""}, {.1000000000000005, "0.1000000000000005", "", "0.1000000000000005000000000000000006441234"},
{.1000000000000008, "0.1000000000000008", ""}, {.1000000000000008, "0.1000000000000008", "", "0.100000000000000800000000000000000009999999999999999999999999999"},
{1e25, "10000000000000000000000000", ""}, {1e25, "10000000000000000000000000", "", "10000000000000000000000000.00000000000000000098798978"},
{math.MaxInt64, strconv.FormatInt(math.MaxInt64, 10), ""}, {math.MaxInt64, strconv.FormatInt(math.MaxInt64, 10), "", strconv.FormatInt(math.MaxInt64, 10)},
{1.29067116156722e-309, "0", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001290671161567218558822290567835270536800098852722416870074139002112543896676308448335063375297788379444685193974290737962187240854947838776604607190387984577130572928111657710645015086812756013489109884753559084166516937690932698276436869274093950997935137476803610007959500457935217950764794724766740819156974617155861568214427828145972181876775307023388139991104942469299524961281641158436752347582767153796914843896176260096039358494077706152272661453132497761307744086665088096215425146090058519888494342944692629602847826300550628670375451325582843627504604013541465361435761965354140678551369499812124085312128659002910905639984075064968459581691226705666561364681985266583563078466180095375402399087817404368974165082030458595596655868575908243656158447265625000000000000000000000000000000000000004440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
} }
var testTableScientificNotation = map[string]string{ var testTableScientificNotation = map[string]string{
@ -62,7 +64,7 @@ var testTableScientificNotation = map[string]string{
func init() { func init() {
for _, s := range testTable { for _, s := range testTable {
s.exact = strconv.FormatFloat(s.float, 'f', 300, 64) s.exact = strconv.FormatFloat(s.float, 'f', 1500, 64)
if strings.ContainsRune(s.exact, '.') { if strings.ContainsRune(s.exact, '.') {
s.exact = strings.TrimRight(s.exact, "0") s.exact = strings.TrimRight(s.exact, "0")
s.exact = strings.TrimRight(s.exact, ".") s.exact = strings.TrimRight(s.exact, ".")
@ -72,8 +74,8 @@ func init() {
// add negatives // add negatives
withNeg := testTable[:] withNeg := testTable[:]
for _, s := range testTable { for _, s := range testTable {
if s.float > 0 { if s.float > 0 && s.short != "0" && s.exact != "0" {
withNeg = append(withNeg, &testEnt{-s.float, "-" + s.short, "-" + s.exact}) withNeg = append(withNeg, &testEnt{-s.float, "-" + s.short, "-" + s.exact, "-" + s.inexact})
} }
} }
testTable = withNeg testTable = withNeg
@ -147,6 +149,25 @@ func TestNewFromString(t *testing.T) {
} }
} }
func TestFloat64(t *testing.T) {
for _, x := range testTable {
s := x.exact
d, err := NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if f, exact := d.Float64(); !exact || f != x.float {
t.Errorf("cannot represent exactly %s", s)
}
s = x.inexact
d, err = NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if f, exact := d.Float64(); exact || f != x.float {
t.Errorf("%s should be represented inexactly", s)
}
}
}
func TestNewFromStringErrs(t *testing.T) { func TestNewFromStringErrs(t *testing.T) {
tests := []string{ tests := []string{
"", "",