aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/float_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-02-10 11:49:54 -0800
committerRobert Griesemer <gri@golang.org>2015-02-11 17:02:09 +0000
commit950aa9f1bcdea247660a2393bf91506af249b539 (patch)
tree5560ec0a9190b0fe95d44ddf4115188767f97738 /src/math/big/float_test.go
parenta15818fed3032d43e57c395c3fe8b46b37a5f1ea (diff)
downloadgo-950aa9f1bcdea247660a2393bf91506af249b539.tar.xz
math/big: When result prec == 0, use at least prec == 64 for SetInt, SetRat.
This avoids surprises. Change-Id: Iaae67da2d12e29c4e797ad6313e0895f7ce80cb1 Reviewed-on: https://go-review.googlesource.com/4480 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/float_test.go')
-rw-r--r--src/math/big/float_test.go33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/math/big/float_test.go b/src/math/big/float_test.go
index e4c2e1ad99..c00aa9d97e 100644
--- a/src/math/big/float_test.go
+++ b/src/math/big/float_test.go
@@ -490,8 +490,20 @@ func TestFloatSetInt(t *testing.T) {
t.Errorf("invalid integer %s", want)
continue
}
+ n := x.BitLen()
+
var f Float
f.SetInt(&x)
+
+ // check precision
+ if n < 64 {
+ n = 64
+ }
+ if prec := f.Precision(); prec != uint(n) {
+ t.Errorf("got prec = %d; want %d", prec, n)
+ }
+
+ // check value
got := f.Format('g', 100)
if got != want {
t.Errorf("got %s (%s); want %s", got, f.Format('p', 0), want)
@@ -519,11 +531,24 @@ func TestFloatSetRat(t *testing.T) {
t.Errorf("invalid fraction %s", want)
continue
}
- f := NewFloat(0, 1000, 0) // set a high precision - TODO(gri) find a cleaner way
- f.SetRat(&x)
- got := f.Format('g', 100)
+ n := max(x.Num().BitLen(), x.Denom().BitLen())
+
+ var f1 Float
+ var f2 = NewFloat(0, 1000, 0) // set a high precision - TODO(gri) find a cleaner way
+ f1.SetRat(&x)
+ f2.SetRat(&x)
+
+ // check precision when set automatically
+ if n < 64 {
+ n = 64
+ }
+ if prec := f1.Precision(); prec != uint(n) {
+ t.Errorf("got prec = %d; want %d", prec, n)
+ }
+
+ got := f2.Format('g', 100)
if got != want {
- t.Errorf("got %s (%s); want %s", got, f.Format('p', 0), want)
+ t.Errorf("got %s (%s); want %s", got, f2.Format('p', 0), want)
}
}
}