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

math: inconsistent conversion from NaN to int on js/wasm #35034

Open
ktye opened this issue Oct 21, 2019 · 2 comments
Open

math: inconsistent conversion from NaN to int on js/wasm #35034

ktye opened this issue Oct 21, 2019 · 2 comments

Comments

@ktye
Copy link

@ktye ktye commented Oct 21, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13.3 windows/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

windows/amd64
js/wasm

go env Output
$ go env

What did you do?

The following program converts a NaN to int32.
The conversion is different on js/wasm, if math.NaN() is used or the bit pattern, even if both seem to be identical.

package main

import (
        "fmt"
        "math"
)

var unan = uint64(0x7FF8000000000001)

func main() {
        n := math.NaN()
        i := int32(n)
        fmt.Printf("%f %d %d\n", n, i, math.Float64bits(n))

        n = math.Float64frombits(unan)
        i = int32(n)
        fmt.Printf("%f %d %d\n", n, i, math.Float64bits(n))
}

To run the wasm program, it is compiled with:
GOOS=js GOARCH=wasm go build -o main.wasm main.go
and loaded in a browser with the method described in github.com/golang/go/wiki/WebAssembly

What did you expect to see?

On playground and windows/amd64 the result is as expected:
NaN -2147483648 9221120237041090561
NaN -2147483648 9221120237041090561

I expect to see the same result in the js console when running the wasm program.

What did you see instead?

on js/wasm the result is:
NaN -2147483648 9221120237041090561
NaN 0 9221120237041090561

Both chrome and firefox give the same result.

@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Oct 21, 2019

I thought there's a chance this could be related to 64-bit vs 32-bit, so I tried on darwin/386 in addition to darwin/amd64, but they both got the same result as windows/amd64. So this seems specific to Wasm.

I can reproduce with node too:

$ GOOS=js GOARCH=wasm go run -exec=$(go env GOROOT)/misc/wasm/go_js_wasm_exec .
NaN -2147483648 9221120237041090561
NaN 0 9221120237041090561

/cc @neelance @cherrymui

@dmitshur dmitshur changed the title js/wasm: inconsistent conversion from NaN to int math: inconsistent conversion from NaN to int on js/wasm Oct 21, 2019
@cherrymui

This comment has been minimized.

Copy link
Contributor

@cherrymui cherrymui commented Oct 21, 2019

The difference is converting a Nan to an int32. The spec says (https://golang.org/ref/spec#Conversions)

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

So, technically, this is fine.

The two Wasm results being different is because the compiler happen to constant-fold one of them, and not the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.