Okay, I didn't upload the whole function here. Also, the only reason that it doesn't overrun in the real DG code is that the last value in AoA[] is 180*RAD, which is greater than all possible values of AoA.
dbeachy, judging by the behavior that I got, it definitely behaved like an array overrun. (If it looks like a duck and quacks like a duck, then it's a duck...)
Nevertheless, changing from nabsc-1 to nabsc-2 fixed the crashes, so that's all that really matters. (I added code for a similar mechanism with drag, and the drag coefficient got set to approx. -4E15, thus leading to NaN-spacing)
Let me upload that code...
---------- Post added at 09:39 PM ---------- Previous post was at 09:24 PM ----------
Okay, don't have access to my codebase right now, but I did write and test this code online at
http://www.compileonline.com/compile_cpp11_online.php
I think it illustrates the problem. If the code were working right, the radian value of 60 degrees would have been printed to the output. However, the result is zero.
Dbeachy, I can't find anything wrong with the theory of your argument about the algorithm, but the compiler seems to disagree with you.
Code:
#include <iostream>
using namespace std;
int main() {
double RAD = 0.0174532925;
int i;
double aoa = 90*RAD;
const int nabsc = 8;
static const double AOA[nabsc] = {-180*RAD,-60*RAD,-30*RAD, -2*RAD, 15*RAD,20*RAD,25*RAD,60*RAD};
for (i = 0; i < nabsc-1 && AOA[i+1] < aoa; i++);
cout << AOA[i+1];
}
---------- Post added at 09:42 PM ---------- Previous post was at 09:39 PM ----------
Wait, I got it:
Code:
for (i = 0; i < nabsc-1 && AOA[i+1] < aoa; i++);
"i" is incremented at the end of every cycle through the loop, regardless of how the condition evaluates. If the code to change "i" were inside the loop, it would have worked right, but now it always increments one step too far.
---------- Post added at 09:44 PM ---------- Previous post was at 09:42 PM ----------
It's equivalent to saying:
Code:
int i=0;
while (i<4) {
i++;
}
cout << i
This code would return four, not three. I guess this means that there is a better workaround after all.