6

I'm losing my mind. Please tell me my laptop is doing weird things and not me. Or do I need to get committed someplace?

data = {{872.2528741960809`, 2.7221624644873685`, 
0.02`}, {870.8866903801253`, 0.25288222647045855`, 
0.0201`}, {869.5228595650024`, 0.2668660821332401`, 0.0202`}}

Table[boundary0[H1R] = DeleteCases[data, a_ /; a[[1]] <= H1R || a[[2]] >= H1R], {H1R, 0.1,
0.9, 0.1}];

This should collect sets where the first and the second entry of each row are on either side of the index of the set.

When I input

boundary0[0.6]

I get

 {{870.887, 0.252882, 0.0201}, {869.523, 0.266866, 0.0202}}

but when I input

boundary0[0.7]

I get no output.., just my command is repeated

boundary0[0.7]

When I enter

boundary0[0.8]

I get

 {{870.887, 0.252882, 0.0201}, {869.523, 0.266866, 0.0202}}

So back to normal.

Now when I do

Table[boundary0[H1R] = DeleteCases[data, a_ /; a[[1]] <= H1R || a[[2]] >= H1R], {H1R,0.7, 0.9, 0.1}];

and then I do

 boundary0[0.7]

voila.. I get

{{870.887, 0.252882, 0.0201}, {869.523, 0.266866, 0.0202}}

Is my laptop the son of anti-christ? Is it gas-lighting me? Do I need to drive a stake through my kernel?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Amatya
  • 6,888
  • 3
  • 26
  • 35

1 Answers1

7

It's a floating point issue. The article "What every computer scientist should know about floating point arithmetic" by David Goldberg, is a very good introduction to the topic.

In your case, look at the following:

Table[NumberForm[i, 16], {i, 0.1, 0.9, 0.1}] // TableForm
(*  0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7000000000000001
    0.8
    0.9 *)

The DownValue is defined for 0.7000000000000001, not 0.7. On the other hand, look at this:

Table[NumberForm[i, 16], {i, 0.7, 0.9, 0.1}] // TableForm
(*  0.7
    0.7999999999999999
    0.9 *)
rm -rf
  • 88,781
  • 21
  • 293
  • 472