1

Has this nagging question where bytes32 that are near to each other returns true on comparison.

truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c323821311a == 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c323821311c
true

truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c323821311a == 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c323821311d
true

truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322d == 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c
true

truffle(development)> 0x4b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322d == 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c
false

Am I missing something?
It is affecting my mapping. Using values that are near to each other causes the same result to be returned. 
Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82

1 Answers1

3

It is because Javascript uses float to store your numbers:

truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322d
4.1247432523778224e+76

truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c
4.1247432523778224e+76

You want to use BigNumber class and its equals() function:

truffle(development)> var a = new web3.BigNumber("0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c")
undefined

truffle(development)> var a_again = new web3.BigNumber("0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c")
undefined

truffle(development)> var b = new web3.BigNumber("0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322d")
undefined

truffle(development)> a.equals(b)
false

truffle(development)> a.equals(a_again)
**true**

truffle(development)> a == b
false

truffle(development)> a == a_again
false
abb
  • 466
  • 2
  • 6