1
$\begingroup$

This simple table command holds no mysteries:

n = 10;
tmin = 0;
tmax = 10;
dt = (tmax - tmin)/n;
list = Table[t, {t, tmin, tmax, dt}]
nList = Length[list]

The output is, as we might expect,

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
11

This other equally simple piece of code, on the other hand, is misbehaving on some values of n:

Do[
  n = i;
  tmin = AbsoluteTime[{2018, 6, 11, 16, 10, 20.026}];
  tmax = AbsoluteTime[{2019, 9, 29, 4, 44, 47.9803}];
  dt = (tmax - tmin)/n;
  list = Table[t, {t, tmin, tmax, dt}];
  nList = Length[list];
  If[nList != i + 1,
   Print["i = ", i, "   nList = ", nList]
   ],
  {i, 1, 50}
  ];

i = 3   nList = 3
i = 7   nList = 7
i = 9   nList = 9
i = 10   nList = 10
i = 11   nList = 11
i = 12   nList = 12
i = 15   nList = 15
i = 17   nList = 17
i = 18   nList = 18
i = 19   nList = 19
i = 20   nList = 20
i = 21   nList = 21
i = 26   nList = 26
i = 30   nList = 30
i = 31   nList = 31
i = 33   nList = 33
i = 36   nList = 36
i = 37   nList = 37
i = 39   nList = 39
i = 41   nList = 41
i = 42   nList = 42
i = 43   nList = 43
i = 44   nList = 44
i = 45   nList = 45
i = 48   nList = 48

The problem happens on 25/50 cases, not sure if it's a trend, i.e. half of all the cases.

I need this to sample a function at different frequencies. Since my data uses DateList format for the abscissa, I am using AbsoluteTime to map to the reals. I am using MMA 10.0.2 on a Mac.

Could anyone let me know if there is a bug with AbsoluteTime in my version of Mathematica, or if I am doing something stupid, or if there is an easy fix?

Many thanks

EDIT: In plain English the problem is as follows. I need to subdivide a (time)line into n adjoining intervals. I am doing this by defining a list that represents the n intervals. Such a list has n + 1 end-points, or perhaps "coordinates" might be a better term. For example: 1 interval has 2 ends, 2 adjoining intervals can be defined as a list with 3 "coordinates", and so on. The first example above shows what happens when you subdivide a segment of length 10 into equal sub-intervals of length = 1: you get 11 coordinates.

Now, when I tried the same thing with real numbers representing dates obtained by AbsoluteTime, sometimes it works and sometimes it doesn't. In the example above there are 25 cases where nList = n instead of n + 1, which I printed out explicitly. The solution I seem to have found just after posting my question was simply to put N[ ] around the formula for calculating dt. So my question has morphed into "Is this the correct way to handle this problem and why is it even needed?".

$\endgroup$
  • $\begingroup$ Adding this modification dt = N[(tmax - tmin)/n]; seems to be enough to fix the problem, but I don't fully get why. Isn't a division of a Real by an Integer = Real? $\endgroup$ – pdini Oct 26 '19 at 7:27
  • $\begingroup$ Can you describe what the problem is in plain English? $\endgroup$ – Szabolcs Oct 26 '19 at 7:58
  • $\begingroup$ Try list = Table[t, {t, Subdivide[tmin, tmax, n]}]; $\endgroup$ – Coolwater Oct 26 '19 at 9:52

Browse other questions tagged or ask your own question.