Skip to content

Fix float64 to uint{8,16,32} conversion. #1305

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

Merged
merged 1 commit into from
May 6, 2024
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
2 changes: 0 additions & 2 deletions compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,6 @@ func (fc *funcContext) translateConversion(expr ast.Expr, desiredType types.Type
return fc.fixNumber(fc.formatParenExpr("%1l + ((%1h >> 31) * 4294967296)", expr), t)
}
return fc.fixNumber(fc.formatExpr("%s.$low", fc.translateExpr(expr)), t)
case isFloat(basicExprType):
return fc.formatParenExpr("%e >> 0", expr)
case types.Identical(exprType, types.Typ[types.UnsafePointer]):
return fc.translateExpr(expr)
default:
Expand Down
25 changes: 25 additions & 0 deletions tests/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"runtime"
"testing"
"testing/quick"

"github.com/gopherjs/gopherjs/js"
)

// naiveMul64 performs 64-bit multiplication without using the multiplication
Expand Down Expand Up @@ -93,3 +95,26 @@ func BenchmarkMul64(b *testing.B) {
}
})
}

func TestIssue733(t *testing.T) {
if runtime.GOOS != "js" {
t.Skip("test uses GopherJS-specific features")
}

t.Run("sign", func(t *testing.T) {
f := float64(-1)
i := uint32(f)
underlying := js.InternalObject(i).Float() // Get the raw JS number behind i.
if want := float64(4294967295); underlying != want {
t.Errorf("Got: uint32(float64(%v)) = %v. Want: %v.", f, underlying, want)
}
})
t.Run("truncation", func(t *testing.T) {
f := float64(300)
i := uint8(f)
underlying := js.InternalObject(i).Float() // Get the raw JS number behind i.
if want := float64(44); underlying != want {
t.Errorf("Got: uint32(float64(%v)) = %v. Want: %v.", f, underlying, want)
}
})
}
Loading