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

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


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

both give the value a, whereas

 <| 0.22499999999999964` -> a |> [0.22499999999999966`] gives Missing as an answer.
  • 2
    $\begingroup$ Looks like this thing, but with associations. Why do you need to use inexact numbers as keys? $\endgroup$ – J. M.'s torpor Mar 31 '19 at 13:50
  • 4
    $\begingroup$ "Can someone explain me how Mathematica's Associations behave [...]?" They behave badly. Don't do that. $\endgroup$ – Henrik Schumacher Mar 31 '19 at 13:51
  • $\begingroup$ Association is the name of the function in Mathematica, so I am not sure what was productive about the edit I just rolled back. $\endgroup$ – J. M.'s torpor Apr 1 '19 at 6:22

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.