From 73749d4874d5d3efba704f6046eae0517f998f4e Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Mon, 31 Dec 2018 15:05:00 -0800 Subject: [PATCH] Correctly handle negative floats Fixes: #114 --- big.go | 9 +++++---- issues_test.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/big.go b/big.go index 0bf7fa4..b0f7340 100644 --- a/big.go +++ b/big.go @@ -514,11 +514,12 @@ func (x *Big) Float64() (f float64, ok bool) { case xc == 0: ok = true case x.IsInt(): - if xc, ok = x.Uint64(); !ok { - v, _ := x.Int64() - xc = uint64(v) + if xc, ok := x.Int64(); ok { + f = float64(xc) + } else if xc, ok := x.Uint64(); ok { + f = float64(xc) } - fallthrough + ok = xc < maxMantissa || (xc&(xc-1)) == 0 case x.exp == 0: f = float64(xc) ok = xc < maxMantissa || (xc&(xc-1)) == 0 diff --git a/issues_test.go b/issues_test.go index c345d30..7353ef9 100644 --- a/issues_test.go +++ b/issues_test.go @@ -83,6 +83,17 @@ func TestIssue105(t *testing.T) { var b Big b.SetString("6190.000000000000") if _, ok := b.Float64(); !ok { - t.Fatalf("6190 should fit in a float just fine") + t.Fatal("6190 should fit in a float just fine") + } +} + +func TestIssue114(t *testing.T) { + val := New(-1, 0) + f, ok := val.Float64() + if !ok { + t.Fatal("expected true, got false") + } + if f != -1 { + t.Fatalf("expected -1, got %f", f) } }