0

I was doing something fairly ordinary and noticed something I can't, for the life of me, explain. For context, I wanted to take make a function that takes an array of data like the one below, then an x range (the 1st variable) you give it, and it figures out what the min and max in that range is, so you can plot it more "intelligently" (since Automatic) seems to not be working).

First I created a table of pairs:

blorp = Table[{x, x^3}, {x, 0, 10, .1}]

{{0., 0.}, {0.1, 0.001}, {0.2, 0.008}, {0.3, 0.027}, {0.4, 
  0.064}, {0.5, 0.125}, {0.6, 0.216}, {0.7, 0.343}, {0.8, 
  0.512}, {0.9, 0.729}, {1., 1.}, {1.1, 1.331}, {1.2, 1.728}, {1.3, 
  2.197}, {1.4, 2.744}, {1.5, 3.375}, {1.6, 4.096}, {1.7, 
  4.913}, {1.8, 5.832}, {1.9, 6.859}, {2., 8.}, {2.1, 9.261}, {2.2, 
  10.648}, {2.3, 12.167}, {2.4, 13.824}, {2.5, 15.625}, {2.6, 
  17.576}, {2.7, 19.683}, {2.8, 21.952}, {2.9, 24.389}, {3., 
  27.}, {3.1, 29.791}, {3.2, 32.768}, {3.3, 35.937}, {3.4, 
  39.304}, {3.5, 42.875}, {3.6, 46.656}, {3.7, 50.653}, {3.8, 
  54.872}, {3.9, 59.319}, {4., 64.}, {4.1, 68.921}, {4.2, 
  74.088}, {4.3, 79.507}, {4.4, 85.184}, {4.5, 91.125}, {4.6, 
  97.336}, {4.7, 103.823}, {4.8, 110.592}, {4.9, 117.649}, {5., 
  125.}, {5.1, 132.651}, {5.2, 140.608}, {5.3, 148.877}, {5.4, 
  157.464}, {5.5, 166.375}, {5.6, 175.616}, {5.7, 185.193}, {5.8, 
  195.112}, {5.9, 205.379}, {6., 216.}, {6.1, 226.981}, {6.2, 
  238.328}, {6.3, 250.047}, {6.4, 262.144}, {6.5, 274.625}, {6.6, 
  287.496}, {6.7, 300.763}, {6.8, 314.432}, {6.9, 328.509}, {7., 
  343.}, {7.1, 357.911}, {7.2, 373.248}, {7.3, 389.017}, {7.4, 
  405.224}, {7.5, 421.875}, {7.6, 438.976}, {7.7, 456.533}, {7.8, 
  474.552}, {7.9, 493.039}, {8., 512.}, {8.1, 531.441}, {8.2, 
  551.368}, {8.3, 571.787}, {8.4, 592.704}, {8.5, 614.125}, {8.6, 
  636.056}, {8.7, 658.503}, {8.8, 681.472}, {8.9, 704.969}, {9., 
  729.}, {9.1, 753.571}, {9.2, 778.688}, {9.3, 804.357}, {9.4, 
  830.584}, {9.5, 857.375}, {9.6, 884.736}, {9.7, 912.673}, {9.8, 
  941.192}, {9.9, 970.299}, {10., 1000.}}

Then I took the transpose of this, so I can search just the first variable of the pairs:

tblorp = First@Transpose@blorp

{0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1., 1.1, 1.2, 1.3, \
1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2., 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, \
2.8, 2.9, 3., 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4., 4.1, \
4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5., 5.1, 5.2, 5.3, 5.4, 5.5, \
5.6, 5.7, 5.8, 5.9, 6., 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, \
7., 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8., 8.1, 8.2, 8.3, \
8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9., 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, \
9.8, 9.9, 10.}

Then I tested with Position[] on some random value:

Position[tblorp, 3.2]

{{33}}

Good...

Position[tblorp, 7.6]

{}

??? Maybe I unknowingly went out of bounds or something..? Let me try nearby values:

Position[tblorp, 7.5]
Position[tblorp, 7.7]

{{76}}
{{78}}

What the... Maybe somehow that one value is empty?

tblorp[[77]]

7.6

Uhh... at this point I tried a few other values and found that a few others do this:

Position[tblorp, 3.6]
Position[tblorp, 4.6]
Position[tblorp, 5.6]
Position[tblorp, 6.6]
Position[tblorp, 8.6]

{{37}} 
{}
{}
{}
{{87}}

Okay, I'm flummoxed. To see if there was some obvious pattern in the values that do this, I did:

Table[{i, Position[tblorp, i]}, {i, 0, 10, 0.1}]

{{0., {{1}}}, {0.1, {{2}}}, {0.2, {{3}}}, {0.3, {{4}}}, {0.4, {{5}}}, \
{0.5, {{6}}}, {0.6, {{7}}}, {0.7, {{8}}}, {0.8, {{9}}}, {0.9, \
{{10}}}, {1., {{11}}}, {1.1, {{12}}}, {1.2, {{13}}}, {1.3, {{14}}}, \
{1.4, {{15}}}, {1.5, {{16}}}, {1.6, {{17}}}, {1.7, {{18}}}, {1.8, \
{{19}}}, {1.9, {{20}}}, {2., {{21}}}, {2.1, {{22}}}, {2.2, {{23}}}, \
{2.3, {{24}}}, {2.4, {{25}}}, {2.5, {{26}}}, {2.6, {{27}}}, {2.7, \
{{28}}}, {2.8, {{29}}}, {2.9, {{30}}}, {3., {{31}}}, {3.1, {{32}}}, \
{3.2, {{33}}}, {3.3, {{34}}}, {3.4, {{35}}}, {3.5, {{36}}}, {3.6, \
{{37}}}, {3.7, {{38}}}, {3.8, {{39}}}, {3.9, {{40}}}, {4., {{41}}}, \
{4.1, {{42}}}, {4.2, {{43}}}, {4.3, {{44}}}, {4.4, {{45}}}, {4.5, \
{{46}}}, {4.6, {{47}}}, {4.7, {{48}}}, {4.8, {{49}}}, {4.9, {{50}}}, \
{5., {{51}}}, {5.1, {{52}}}, {5.2, {{53}}}, {5.3, {{54}}}, {5.4, \
{{55}}}, {5.5, {{56}}}, {5.6, {{57}}}, {5.7, {{58}}}, {5.8, {{59}}}, \
{5.9, {{60}}}, {6., {{61}}}, {6.1, {{62}}}, {6.2, {{63}}}, {6.3, \
{{64}}}, {6.4, {{65}}}, {6.5, {{66}}}, {6.6, {{67}}}, {6.7, {{68}}}, \
{6.8, {{69}}}, {6.9, {{70}}}, {7., {{71}}}, {7.1, {{72}}}, {7.2, \
{{73}}}, {7.3, {{74}}}, {7.4, {{75}}}, {7.5, {{76}}}, {7.6, {{77}}}, \
{7.7, {{78}}}, {7.8, {{79}}}, {7.9, {{80}}}, {8., {{81}}}, {8.1, \
{{82}}}, {8.2, {{83}}}, {8.3, {{84}}}, {8.4, {{85}}}, {8.5, {{86}}}, \
{8.6, {{87}}}, {8.7, {{88}}}, {8.8, {{89}}}, {8.9, {{90}}}, {9., \
{{91}}}, {9.1, {{92}}}, {9.2, {{93}}}, {9.3, {{94}}}, {9.4, {{95}}}, \
{9.5, {{96}}}, {9.6, {{97}}}, {9.7, {{98}}}, {9.8, {{99}}}, {9.9, \
{{100}}}, {10., {{101}}}}

aaaaaand they all work here for some reason.

I'm sure there's something really simple that I'm stupidly missing here, but can anyone illuminate it for me?

edit: @Kuba figured it out: it's a floating point problem, as illuminated by InputForm:

tblorp = First@Transpose@blorp// InputForm

{0., 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1., 1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 
 1.6, 1.7000000000000002, 1.8, 1.9000000000000001, 2., 2.1, 2.2, 2.3000000000000003, 2.4000000000000004, 2.5, 2.6, 2.7, 2.8000000000000003, 
 2.9000000000000004, 3., 3.1, 3.2, 3.3000000000000003, 3.4000000000000004, 3.5, 3.6, 3.7, 3.8000000000000003, 3.9000000000000004, 4., 4.1000000000000005, 
 4.2, 4.3, 4.4, 4.5, 4.6000000000000005, 4.7, 4.800000000000001, 4.9, 5., 5.1000000000000005, 5.2, 5.300000000000001, 5.4, 5.5, 5.6000000000000005, 5.7, 
 5.800000000000001, 5.9, 6., 6.1000000000000005, 6.2, 6.300000000000001, 6.4, 6.5, 6.6000000000000005, 6.7, 6.800000000000001, 6.9, 7., 
 7.1000000000000005, 7.2, 7.300000000000001, 7.4, 7.5, 7.6000000000000005, 7.7, 7.800000000000001, 7.9, 8., 8.1, 8.200000000000001, 8.3, 8.4, 8.5, 8.6, 
 8.700000000000001, 8.8, 8.9, 9., 9.1, 9.200000000000001, 9.3, 9.4, 9.5, 9.600000000000001, 9.700000000000001, 9.8, 9.9, 10.}

An obvious quick fix is:

p1 = First@First@Position[tblorp, First@Nearest[tblorp, 3.2]]
p2 = First@First@Position[tblorp, First@Nearest[tblorp, 7.6]]

33
77
Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199
YungHummmma
  • 3,042
  • 15
  • 30

1 Answers1

2

Set Accuracy before processing blorp, this makes you trouble free for matching numbers.

SetAccuracy [#, Accuracy @ # // Min // IntegerPart] &  /@ blorp

or simply

 SetAccuracy [#, 12] &  /@ blorp (*on my computer *)

You may also consider

 SetAccuracy[#,5]& /@ tblorp

or even only 2 instead of 5, because of .1 increments. I think you got the idea to work with Accuracy.

NO, NO, NO, not a bug, but about the beauty of Mathematica... hi,hi,hi

penguin77
  • 1,645
  • 9
  • 8