开发者

c++ to java conversion, a few questions

I am converting a some C++ to java and have a small bit that I am unsure about first question is what is tested for in the line

if (ampconst[i][0] || ampconst[i][1])

this in an example to the data in the array.

 static short ampconst[NUT_SERIES][2] = {
{0,0},
{0,0},
{46,-24}
};

and my second question is that the ampsecul array is far shorter than NUT_SERIES so I am getting array out of bounds exceptions, the array terminates like so

static long ampsecul[][5] = {
{0  ,-171996 ,-1742 ,92025 ,89},
{1  ,2062    ,2     ,-895  ,5},
{8  ,-13187  ,-16   ,5736  ,-31},
{9  ,1426    ,-34   ,54    ,-1},
{10 ,-517    ,12    ,224   ,-6},
{11 ,217     ,-5    ,-95   ,3},
{12 ,129     ,1     ,-70   ,0},
{15 ,17      ,-1    ,0     ,0},
{17 ,-16     ,1     ,7     ,0},
{30 ,-2274   ,-2    ,977   ,-5},
{31 ,712     ,1     ,-7    ,0},
{32 ,-386    ,-4    ,200   ,0},
{33 ,-301    ,0     ,129   ,-1},
{37 ,63      ,1     ,-33   ,0},
{38 ,-58     ,-1    ,32    ,0},
/* termination */  { -1, }
};

so how could this be handled in java and what would the values be at these lines when the array is out of bounds or how would C++ handle this.

ampsin = ampsecul[isecul][1] + ampsecul[isecul][2] * T10;
ampcos = ampsecul[isecul][3] + ampsecul[isecul][4] * T10;

thanks in advance for any advice. This is the whole for loop too see the开发者_如何学运维 code in context.

for (i = isecul = 0; i < NUT_SERIES ; ++i) {
    double arg = 0., ampsin, ampcos;
    short j;

    if (ampconst[i][0] || ampconst[i][1]) {
    /* take non-secular terms from simple array */
    ampsin = ampconst[i][0];
    ampcos = ampconst[i][1];
    } else {
    /* secular terms from different array */
    ampsin = ampsecul[isecul][1] + ampsecul[isecul][2] * T10;
    ampcos = ampsecul[isecul][3] + ampsecul[isecul][4] * T10;
    ++isecul;
    }

    for (j = 0; j < 5; ++j)
    arg += delcache[j][NUT_MAXMUL + multarg[i][j]];

    if (fabs(ampsin) >= prec)
    lastdpsi += ampsin * sin(arg);

    if (fabs(ampcos) >= prec)
    lastdeps += ampcos * cos(arg);
}


  if (ampconst[i][0] || ampconst[i][1])

tests whether the first/second column in ampconst[i] contain non-zero (it is an early-out optimization: if both the constants are 0 then the calculation can be skipped)

Edit I just found (google!) that this is a nutation calculation that has been adopted in quite a few places, but seems to be originally from a libastro.

     hg clone https://bitbucket.org/brandon/pyephem

As far as the isecul index is concerned: apparently isecul should never grow to >= 15 (note that i is the loop variable, not isecul, isecul is incremented conditionally).

However, seeing the 'terminator' (-1) value, I'd really expect a check some like

if (ampsecul[isecul][0] == -1)
   isecul = 0; // ? just guessing :)

or

if (ampsecul[isecul][0] == -1)
   break;

Also, I get the impression that the first column of ampsecul is a range-based division, so somehow, there would be a binarysearch for the matching slot into ampsecul, not direct indexing (i.e. isecul=4 would select index 2 (2..8) not 4)

Are you sure you are getting the source code correctly? I looks very much like there are some custom indexers (operators[](...)) that you misssed out on? This would probably be about the same class/function that contains the terminator check like shown above. Edit from the linked source I get the impression that the code is very much intended as is, and hence isecul should simply not be growing >= 15


That first if statement is testing the array entries for zero/non-zero. In C/C++ a boolean is simply an int that is used in a special way such that zero is false and non-zero is true.

As for your second array question I haven't grocked it yet. But understand that C/C++ does no array bounds checking (other than what may accidentally occur if you touch an undefined storage page), so unless there's an egregious error in the C++ code there must be something that limits references to the valid bounds of the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜