Skip to content

Commit 73749d4

Browse files
committed
Correctly handle negative floats
Fixes: #114
1 parent 27907c8 commit 73749d4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

big.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,12 @@ func (x *Big) Float64() (f float64, ok bool) {
514514
case xc == 0:
515515
ok = true
516516
case x.IsInt():
517-
if xc, ok = x.Uint64(); !ok {
518-
v, _ := x.Int64()
519-
xc = uint64(v)
517+
if xc, ok := x.Int64(); ok {
518+
f = float64(xc)
519+
} else if xc, ok := x.Uint64(); ok {
520+
f = float64(xc)
520521
}
521-
fallthrough
522+
ok = xc < maxMantissa || (xc&(xc-1)) == 0
522523
case x.exp == 0:
523524
f = float64(xc)
524525
ok = xc < maxMantissa || (xc&(xc-1)) == 0

issues_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ func TestIssue105(t *testing.T) {
8383
var b Big
8484
b.SetString("6190.000000000000")
8585
if _, ok := b.Float64(); !ok {
86-
t.Fatalf("6190 should fit in a float just fine")
86+
t.Fatal("6190 should fit in a float just fine")
87+
}
88+
}
89+
90+
func TestIssue114(t *testing.T) {
91+
val := New(-1, 0)
92+
f, ok := val.Float64()
93+
if !ok {
94+
t.Fatal("expected true, got false")
95+
}
96+
if f != -1 {
97+
t.Fatalf("expected -1, got %f", f)
8798
}
8899
}

0 commit comments

Comments
 (0)