Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
4  
It's rounding error; boundary0[0.7000000000000001] is defined, but boundary0[0.7] is not. –  J. M. Nov 5 '12 at 3:57

1 Answer 1

up vote 7 down vote accepted

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 *)
share|improve this answer
    
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 –  J. M. Nov 5 '12 at 4:04
    
In my machine both .6and .7show this behavior. However boundary0[.6] is "defined" while boundary0[.7] isn't –  belisarius Nov 5 '12 at 4:07
    
@rm-rf.. thank goodness I'm not the one losing it. So is there a way to get around this? –  Amatya Nov 5 '12 at 5:46
1  
@Amatya: easy, use rationals instead of reals (i.e., define boundary0[1/10], ... boundary0[9/10] instead). –  J. M. Nov 5 '12 at 8:49
    
@J.M. oh sweet... thank you! –  Amatya Nov 5 '12 at 9:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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