tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.2k stars 892 forks source link

Zero and Negative Zero are not treated as equal in maps #2356

Closed dgryski closed 2 years ago

dgryski commented 2 years ago
~/go/src/github.com/dgryski/bug $ go run bug.go
-0.000000e+000 2
-0.000000e+000 2
true true
~/go/src/github.com/dgryski/bug $ ~/go/src/github.com/tinygo-org/tinygo/build/tinygo run bug.go
+0.000000e+000 1
-0.000000e+000 1
+0.000000e+000 1
-0.000000e+000 1
true true
~/go/src/github.com/dgryski/bug $ cat bug.go
package main

import "math"

type fstruct struct {
    x float64
}

func main() {
    var zero float64
    var negzero float64 = math.Copysign(zero, -1)

    m := make(map[fstruct]int)

    var f fstruct
    var negf fstruct
    negf.x = negzero

    m[f]++
    m[negf]++

    for k, v := range m {
        println(k.x, v)
    }

    m2 := make(map[float64]int)
    m2[zero]++
    m2[negzero]++

    for k, v := range m2 {
        println(k, v)
    }

    println(zero == negzero, f == negf)
}
dgryski commented 2 years ago

I'm working on a PR for this.

aykevl commented 2 years ago

Thanks! Looks like the bug is in hashmapInterfaceHash.

dgryski commented 2 years ago

Sadly my fix for this solve the one test case for golang/geo/s2, but that package fails later with a Bus Error.