Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow constant values of infininitesimal non-zero floating points #1185

Merged
merged 5 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,23 +1240,26 @@
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) {
panic("cannot convert untyped bigdec to float32 -- too close to zero")
} else if math.IsInf(float64(f32), 0) {
f32, _ := bf.Float32()
if math.IsInf(float64(f32), 0) {

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_conversions.go#L1249-L1250

Added lines #L1249 - L1250 were not covered by tests
panic("cannot convert untyped bigdec to float32 -- too close to +-Inf")
}
dst.SetFloat32(f32)
return
case Float64Kind:
dst.T = t
dst.V = nil
f64, _ := bd.Float64()
jaekwon marked this conversation as resolved.
Show resolved Hide resolved
if f64 == 0 && !bd.IsZero() {
panic("cannot convert untyped bigdec to float64 -- too close to zero")
} else if math.IsInf(f64, 0) {
f64, err := bd.Float64()
if err != nil {
panic(fmt.Errorf("cannot convert untyped bigdec to float64: %w", err))

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_conversions.go#L1260

Added line #L1260 was not covered by tests
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}
if math.IsInf(f64, 0) {
panic("cannot convert untyped bigdec to float64 -- too close to +-Inf")
}
dst.SetFloat64(f64)
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) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
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())
}
13 changes: 13 additions & 0 deletions gnovm/tests/files/float6.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

const (
SmallestNonzeroFloat64 = 0x1p-1022 * 0x1p-52 // 4.9406564584124654417656879286822137236505980e-324
DividedByTwo = SmallestNonzeroFloat64 / 2
)

func main() {
println(DividedByTwo)
}

// Output:
// 0
15 changes: 15 additions & 0 deletions gnovm/tests/files/float7.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

const (
SmallestNonzeroFloat32 = 0x1p-126 * 0x1p-23 // 1.401298464324817070923729583289916131280e-45
DividedByTwo = SmallestNonzeroFloat32 / 2
)

func main() {
var i float32
i = DividedByTwo
println(i)
}

// Output:
// 0
Loading