shopspring / decimal

Arbitrary-precision fixed-point decimal numbers in Go
Other
6.34k stars 622 forks source link

Map[decimal.Decimal][]int bug #293

Closed 1337b0t closed 2 years ago

1337b0t commented 2 years ago

I was playing around with the package and ran into a bug when trying to access a decimal key out of a map.

See example: https://go.dev/play/p/0ja7R0O2JaK

Let's say I make an arbitrary map with an int slice, make(map[decimal.Decimal][]int)

I can easily load the map and fetch the results.

When I make a key assignment the map does not find results, see testVal

Example:

testVal:= decimal.NewFromFloat(1500)
valFromMap := decimal.Decimal{}

        // assigning 1500 from the map
    for k := range gridMap {

        if k.Equals(decimal.NewFromFloat(1500)) {
            // assign the decimal value 1500 from the map
            valFromMap = k
        }

    }

// make the same 1500 value from above
    testVal := decimal.NewFromFloat(1500)

// results differ
    fmt.Println("Test Val: gridMap[testVal] ", gridMap[testVal], "\nVal From Map: gridMap[valFromMap]", gridMap[valFromMap])

Output

Test Val: decimal.Decimal Val From Map: decimal.Decimal

Test Val: 1500 Val From Map: 1500

Test Val: gridMap[testVal] [] Val From Map: gridMap[valFromMap] [0 0] --> this is what I am looking for

felipeneuwald commented 2 years ago

Not sure if I get your point. Where is the bug? https://go.dev/play/p/wchLneQLFvx

1337b0t commented 2 years ago

This looks like a me problem -- thank you very much for taking the time to respond.