-
Notifications
You must be signed in to change notification settings - Fork 952
DIfferent behavior in Go 1.19/TinyGo when casting float with negative value to uint #3390
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
Comments
Interesting. In this case, I think the behavior is actually correct in both cases. Namely, the spec says the following:
As the number -58657.84 cannot be represented as a uint32 (a negative value can never fit in an unsigned integer), the implementation is free to pick a number. Usually this is whatever the underlying architecture uses as a default value for out-of-range values but I believe we have some custom code for this in TinyGo because the native LLVM instruction results in undefined behavior on out-of-range values. CC @niaow |
So converting to a signed int type first would be "safer" in this case rather than going directly from |
Depends on what you want. If the value could be negative, converting to a Here is a slightly more simplified version of the reproducer: package main
func main() {
var vf1 float64 = -5
var vf2 float64 = 5
println(uint32(vf1), uint32(vf2))
} |
UPDATE: Sorry all. Please ignore my post here and its associated GitHub sent email. I shouldn't be seeking 3rd-party validation over this petty matter. I will concentrate more on the standard libraries assimilations development (actual work) rather messing in the forum. Regards, |
Closing since this is undefined behavior. Thanks everyone! |
The following program produces different output in Go 1.19 vs. TinyGo:
Running this using Go 1.19 results in:
Running it using TinyGo results in:
The text was updated successfully, but these errors were encountered: