diff options
| author | Robert Griesemer <gri@golang.org> | 2015-02-05 17:21:48 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2015-02-06 17:21:01 +0000 |
| commit | 15594df6b4e913d1ed9d7b38fa71868be28e9b63 (patch) | |
| tree | ba08ef6bf4aa3719f91bebed379790f0634a15a7 /src/math/big/float_test.go | |
| parent | 9b6ccb13233f2977c74c73ae836212c55d342d28 (diff) | |
| download | go-15594df6b4e913d1ed9d7b38fa71868be28e9b63.tar.xz | |
math/big: handling of +/-Inf and zero precision, enable zero values
- clarified representation of +/-Inf
- only 0 and Inf values can have 0 precision
- a zero precision value used as result value takes the max precision
of the arguments (to be fine-tuned for setters)
- the zero precision approach makes Float zero values possible
(they represent +0)
- more tests
Missing: Filling in the blanks. More tests.
Change-Id: Ibb4f97e12e1f356c3085ce80f3464e97b82ac130
Reviewed-on: https://go-review.googlesource.com/4000
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 | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/math/big/float_test.go b/src/math/big/float_test.go index e37d2ed365..979b739b08 100644 --- a/src/math/big/float_test.go +++ b/src/math/big/float_test.go @@ -6,11 +6,70 @@ package big import ( "fmt" + "math" "sort" "strconv" "testing" ) +func TestFloatZeroValue(t *testing.T) { + // zero (uninitialized) value is a ready-to-use 0.0 + var x Float + if s := x.Format('f', 1); s != "0.0" { + t.Errorf("zero value = %s; want 0.0", s) + } + + // zero value has precision 0 + if prec := x.Precision(); prec != 0 { + t.Errorf("prec = %d; want 0", prec) + } + + // zero value can be used in any and all positions of binary operations + make := func(x int) *Float { + if x == 0 { + return new(Float) // 0 translates into the zero value + } + return NewFloat(float64(x), 10, 0) + } + for _, test := range []struct { + z, x, y, want int + opname rune + op func(z, x, y *Float) *Float + }{ + {0, 0, 0, 0, '+', (*Float).Add}, + {0, 1, 2, 3, '+', (*Float).Add}, + {1, 2, 0, 2, '+', (*Float).Add}, + {2, 0, 1, 1, '+', (*Float).Add}, + + {0, 0, 0, 0, '-', (*Float).Sub}, + {0, 1, 2, -1, '-', (*Float).Sub}, + {1, 2, 0, 2, '-', (*Float).Sub}, + {2, 0, 1, -1, '-', (*Float).Sub}, + + {0, 0, 0, 0, '*', (*Float).Mul}, + {0, 1, 2, 2, '*', (*Float).Mul}, + {1, 2, 0, 0, '*', (*Float).Mul}, + {2, 0, 1, 0, '*', (*Float).Mul}, + + {0, 0, 0, 0, '/', (*Float).Quo}, + {0, 2, 1, 2, '/', (*Float).Quo}, + {1, 2, 0, 0, '/', (*Float).Quo}, + {2, 0, 1, 0, '/', (*Float).Quo}, + } { + z := make(test.z) + test.op(z, make(test.x), make(test.y)) + if got := int(z.Int64()); got != test.want { + t.Errorf("%d %c %d = %d; want %d", test.x, test.opname, test.y, got, test.want) + } + } + + // TODO(gri) test how precision is set for zero value results +} + +func TestFloatInf(t *testing.T) { + // TODO(gri) implement this +} + func fromBinary(s string) int64 { x, err := strconv.ParseInt(s, 2, 64) if err != nil { @@ -244,6 +303,9 @@ func TestFloatSetFloat64(t *testing.T) { 3.14159265e10, 2.718281828e-123, 1.0 / 3, + math.Inf(-1), + math.Inf(0), + -math.Inf(1), } { for i := range [2]int{} { if i&1 != 0 { |
