Skip to content

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

@ktye

Description

@ktye

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.arch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions