0

When using indexed object, I found the behavior of value 0.7 as the index quite strange, then did the following test.

enter image description here

The first output cell was executed directly as the second input cell.

Have not found other values with the same issue.


Edit: an example where non-int indexing is used in my intuitive but trouble-causing way.

 SimuBody[p1_, p2_] := Table[N@Sin[p1 x], {x, 1, p2, 1}]
 Table[p2 = 15; f[p1, p2] = SimuBody[p1, p2], {p1, 0.1, 0.7, 0.1}];
 Table[p2 = 12; 
 f[p1, p2] = SimuBody[p1, p2], {p1, {2.2, 0.001, -\[Pi], 3.3}}];
 Table[p2 = 5; f[p1, p2] = SimuBody[p1, p2], {p1, {7, 3.4}}];
 g[a_Symbol] := 
 DownValues[a] /. (HoldPattern[_[_[x__]]] :> z_) :> {{x}, z}
 ftab = g[f];
 ListPointPlot3D[
 Transpose@{ftab[[All, 1, 1]], ftab[[All, 1, 2]], ftab[[All, 2, 1]]}, 
 PlotRange -> All, Filling -> Bottom]
 f[0.7, 15]

I started using this non-int indexing method after reading this, where the index does not even have to be a number. So I assumed using non-integer should be ok.

Chromatic
  • 794
  • 4
  • 13
  • 1
    Moral lesson: do not use inexact numbers for indexing. (I'm sure this is a dupe, but I can't find the earlier thread.) – J. M.'s missing motivation Nov 16 '15 at 14:23
  • @J.M. closely related: 65298 – Kuba Nov 16 '15 at 14:25
  • 1
    Regardless of the subtleties of floating point arithmetic that cause this, I think this is a classic case of GIGO. What were you hoping to achieve by putting a non-integer number in Indexed? – Szabolcs Nov 16 '15 at 14:55
  • @J.M. But I need a dynamical container for elements at a particular time. Is there any way to do so without inexact indexing? – Chromatic Nov 16 '15 at 15:31
  • I would suggest editing your question to explain your application further, and maybe we can reopen this. – J. M.'s missing motivation Nov 16 '15 at 15:39
  • @Szabolcs For instance, my simulation has a non-int parameter k and returns list1. I use an indexed object f[k] to map the resulting 'list1'. This mapping is intuitive to me, e.g. I can use ListPlot to plot certain element in f[k] against all the ks I have used in simulation. The thing is, I do not know in advance how many k I will need before getting the simulation reuslt, so it is awkward to use a fixed-sized list to store the data of k and list1. I will be glad if you can offer reference to a elegant method. – Chromatic Nov 16 '15 at 15:48
  • @J.M. Ok. Let me work out a concrete example. – Chromatic Nov 16 '15 at 15:51
  • 1
    When you said "indexed object", I thought you meant Indexed. Now I see what you mean. I think using the term "indexing" here may be confusing to some. I agree with J.M., please edit the post and explain what you said in your comment, then also show how the problem with comparing floating point numbers comes up. The simplest solution is to avoid this sort of comparison, but there may be other ways too (e.g. Nearest for binary search, etc.) My answer would depend on how exactly you end up comparing two floating point numbers which look the same but aren't really the same. – Szabolcs Nov 16 '15 at 16:17
  • 2
    I just saw your update. In that example, it looks like you are comparing 0.7 that you typed by hand to 0.7 that was generated by Table. Is this the only situation where the problem comes up? I would suggest trying to always work with the exact parameters (p1, p2 values) that were generated by/for your simulation. Avoid using values you type by hand. When you want to find a value that you know exists, you can use Nearest to extract it from an existing list of p1-values. To make it easier to handle these, I suggest using Association instead of DownValues. – Szabolcs Nov 16 '15 at 16:23
  • I.e. for a one-parameter case, you'd have a results association which has elements of the form p1 -> res. You know based on something that it should have the key 0.7. But do not extract it as results[0.7]. Instead use something like results[ Nearest[Keys[results], 0.7] ]. Performance shouldn't matter since you typed this by hand anyway. – Szabolcs Nov 16 '15 at 16:34
  • Try indexing with strings; e.g., f["0.7"] = 1 – m_goldberg Nov 16 '15 at 16:49
  • @Szabolcs Hmm. Association looks great! – Chromatic Nov 16 '15 at 18:31

0 Answers0