Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incorrect conversion from float64 to uint8. #733

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

Closed
dmitshur opened this issue Dec 26, 2017 · 0 comments · Fixed by #1305
Closed

Incorrect conversion from float64 to uint8. #733

dmitshur opened this issue Dec 26, 2017 · 0 comments · Fixed by #1305

Comments

@dmitshur
Copy link
Member

Say you start with a float64 value like 300.0. If you convert it to int, it becomes 300. If you convert that int to uint8, it overflows and you get effectively 300 % 256 = 44.

In Go, when you convert float64 directly to uint8, the same process happens implicitly and the expected output is uint8(44). GopherJS doesn't implement this behavior correctly and returns an incorrect uint8 value 300.

Probably affects most of float(32,64) -> (u,)int(8,16,32,64) conversions, not just float64->uint8.

https://play.golang.org/p/AmWeBRW2G0x
https://gopherjs.github.io/playground/#/2-nWgC-jIJ

nevkontakte added a commit to nevkontakte/gopherjs that referenced this issue May 6, 2024
Before this change, GopherJS compiler emitted `(f >> 0)` expression to
convert a float64 `f` to any non-64-bit unsigned integer type. This is
incorrect, because `>>` is a signed bitshift operator in JS, so the
returned value remained signed. Moreover, it did not take into account
to bit size of the target type.

By removing the switch cause, we make the compiler fall through to the
default clause where `fc.fixNumber()` actually does the right thing,
taking the target into account.

Fixes gopherjs#733.
nevkontakte added a commit to nevkontakte/gopherjs that referenced this issue May 6, 2024
Before this change, GopherJS compiler emitted `(f >> 0)` expression to
convert a float64 `f` to any non-64-bit unsigned integer type. This is
incorrect, because `>>` is a signed bitshift operator in JS, so the
returned value remained signed. Moreover, it did not take into account
to bit size of the target type.

By removing the switch cause, we make the compiler fall through to the
default clause where `fc.fixNumber()` actually does the right thing,
taking the target into account.

Fixes gopherjs#733.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant