1
$\begingroup$

I'm confused by the following trivial code:

{Range[0, 1, .1][[8]]} === {.7}

gives True

MemberQ[{.7}, .7]

gives True, but

MemberQ[{Range[0, 1, .1][[8]]}, .7]

gives False. Huh? What am I missing?

$\endgroup$

marked as duplicate by m_goldberg, MarcoB, Alexey Popkov, Kuba Aug 7 '17 at 8:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ {Range[0, 1, .1][[8]], .7} // InputForm ought to be enlightening. Moral lesson is that you don't do such pattern matches on inexact numbers. $\endgroup$ – J. M. will be back soon Aug 6 '17 at 12:53
  • $\begingroup$ @J.M. thanks, I thought it might be a floating point thing, but then I was surprised by the first equality, which I still don't understand $\endgroup$ – bjorne Aug 6 '17 at 12:55
  • 1
    $\begingroup$ That would be due to Internal`$SameQTolerance (or Internal`$EqualTolerance if you had used ==). See e.g. this. $\endgroup$ – J. M. will be back soon Aug 6 '17 at 12:59
  • $\begingroup$ @J.M. Great, thanks! $\endgroup$ – bjorne Aug 6 '17 at 13:00
  • 1
    $\begingroup$ Recommended reading: (5149), (5580), (21711), (66386), (86815). Please take a look at these and tell me if you feel that your question is adequately addressed in these. If so we can mark it as such. $\endgroup$ – Mr.Wizard Aug 6 '17 at 17:07