4

Can someone explain me how Mathematica's Associations behave with inexact numbers as keys:

<| 0.22499999999999964` -> a |> [0.22499999999999965`]

and

<| 0.22499999999999964` -> a |> [0.22499999999999961`] 

both give the value a, whereas

 <| 0.22499999999999964` -> a |> [0.22499999999999966`] gives Missing as an answer.
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42

1 Answers1

10

Lookups with inexact numbers behave like other lookups: they use hashing. We can check that the results are consistent with what Hash does.

vals = {0.22499999999999961`, 0.22499999999999964`, 
   0.22499999999999965`, 0.22499999999999966`};
Hash /@ vals

(* Out[11]= {1879126079618645156, 1879126079618645156, \
1879126079618645156, 5861724109654749116} *)

When hashing approximate values, there are going to be sharp boundaries (discontinuities, in effect). This is a tradeoff between having "very" close neighbors hash to the same value, and maintaining transitivity.

Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199