6
$\begingroup$

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?

$\endgroup$
1
  • 4
    $\begingroup$ It's rounding error; boundary0[0.7000000000000001] is defined, but boundary0[0.7] is not. $\endgroup$ Commented Nov 5, 2012 at 3:57

1 Answer 1

7
$\begingroup$

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 *)
$\endgroup$
6
  • $\begingroup$ Your computer fares a bit better than mine; here's how the sequence looks on my end: 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9 $\endgroup$ Commented Nov 5, 2012 at 4:04
  • $\begingroup$ In my machine both .6and .7show this behavior. However boundary0[.6] is "defined" while boundary0[.7] isn't $\endgroup$ Commented Nov 5, 2012 at 4:07
  • $\begingroup$ @rm-rf.. thank goodness I'm not the one losing it. So is there a way to get around this? $\endgroup$
    – Amatya
    Commented Nov 5, 2012 at 5:46
  • 1
    $\begingroup$ @Amatya: easy, use rationals instead of reals (i.e., define boundary0[1/10], ... boundary0[9/10] instead). $\endgroup$ Commented Nov 5, 2012 at 8:49
  • $\begingroup$ @J.M. oh sweet... thank you! $\endgroup$
    – Amatya
    Commented Nov 5, 2012 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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