Skip to content

Commit

Permalink
Correctly handle negative floats
Browse files Browse the repository at this point in the history
Fixes: #114
  • Loading branch information
ericlagergren committed Dec 31, 2018
1 parent 27907c8 commit 73749d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 5 additions & 4 deletions big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 73749d4

Please sign in to comment.