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

Closed
ktye opened this issue Oct 21, 2019 · 3 comments
Closed

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

ktye opened this issue Oct 21, 2019 · 3 comments
Labels
arch-wasm WebAssembly issues FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@ktye
Copy link

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 dmitshur added arch-wasm WebAssembly issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 21, 2019
@dmitshur
Copy link
Contributor

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
Copy link
Member

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.

@neelance
Copy link
Member

neelance commented Jul 7, 2020

Right, NaN can not be represented as an int32. Closing.

@neelance neelance closed this as completed Jul 7, 2020
@golang golang locked and limited conversation to collaborators Jul 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants