Skip to content

Commit

Permalink
fix: test smallest float divided by zero
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
  • Loading branch information
ajnavarro committed Oct 2, 2023
1 parent 28455d8 commit 104d9a2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,11 @@ func ConvertUntypedBigdecTo(dst *TypedValue, bv BigdecValue, t Type) {
case Float32Kind:
dst.T = t
dst.V = nil
f64, _ := bd.Float64()
f64, err := bd.Float64()
if err != nil {
panic(fmt.Errorf("cannot convert untyped bigdec to float64: %w", err))

Check warning on line 1245 in gnovm/pkg/gnolang/values_conversions.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_conversions.go#L1243-L1245

Added lines #L1243 - L1245 were not covered by tests
}

bf := big.NewFloat(f64)
f32, acc := bf.Float32()
if f32 == 0 && (acc == big.Below || acc == big.Above) {
Expand All @@ -1253,7 +1257,10 @@ func ConvertUntypedBigdecTo(dst *TypedValue, bv BigdecValue, t Type) {
case Float64Kind:
dst.T = t
dst.V = nil
f64, _ := bd.Float64()
f64, err := bd.Float64()
if err != nil {
panic(fmt.Errorf("cannot convert untyped bigdec to float64: %w", err))

Check warning on line 1262 in gnovm/pkg/gnolang/values_conversions.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_conversions.go#L1262

Added line #L1262 was not covered by tests
}
if f64 == 0 && !bd.IsZero() {
panic("cannot convert untyped bigdec to float64 -- too close to zero")
} else if math.IsInf(f64, 0) {
Expand Down
25 changes: 25 additions & 0 deletions gnovm/pkg/gnolang/values_conversions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gnolang

import (
"math"
"testing"

"github.com/cockroachdb/apd"
"github.com/stretchr/testify/require"
)

func TestConvertUntypedBigdecToFloat(t *testing.T) {
dst := &TypedValue{}

dec, err := apd.New(-math.MaxInt64, -4).SetFloat64(math.SmallestNonzeroFloat64 / 2)
require.NoError(t, err)
bd := BigdecValue{
V: dec,
}

typ := Float64Type

ConvertUntypedBigdecTo(dst, bd, typ)

require.Equal(t, float64(0), dst.GetFloat64())
}
5 changes: 5 additions & 0 deletions gnovm/tests/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func TestMachineTestMemPackage(t *testing.T) {
path: "testdata/TestMemPackage/success",
shouldSucceed: true,
},
{
name: "TestSmallestFloat",
path: "testdata/TestMemPackage/success",
shouldSucceed: true,
},
{
name: "TestFail",
path: "testdata/TestMemPackage/fail",
Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/testdata/TestMemPackage/success/float_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package test

import "testing"

func TestSmallestFloat(t *testing.T) {
SmallestNonzeroFloat64 := 0x1p-1022 * 0x1p-52 // 4.9406564584124654417656879286822137236505980e-324
DividedByTwo := SmallestNonzeroFloat64 / 2

println(DividedByTwo)
}

0 comments on commit 104d9a2

Please sign in to comment.