Hi,
You can format the output
std::cout
to show as many decimal places as you want, also the default is not to show trailing zero's.
How does start, end and step work ? |
It is pretty easy:
1 2 3 4 5 6 7 8 9 10 11
|
const double StartX = -10.0;
const double EndX = 10.0;
const Step = 0.1;
double x = 1000.0;
NumberOfXIterations = static_cast<int>( (EndX -StartX) / Step ); // is 200
for (int a = 0; a < NumberOfXIterations ; ++a) {
x = StartX + (a * Step) ;
}
|
No, I haven't tried a debugger. The three for loops together generate around 4 millions values so it would be a bit complicated to check them out I think :-) |
You don't have to test with 4 million values, and you can set conditional breakpoints.
With the FP values, the inaccuracy of them causes problems on line 45 and 48. When you increment by what you think is 0.1, it is actually something like 0.1 + 5e-16. So once you increment x until you think it is zero, it is actually 5e-16. Now with the logic on line 45:
if(x > 0 && x < posXMin) posXMin = x;
x > 0
is true, and
x < 10
is true, so now
posXMin
is set to 5e-16. With the next iteration, x is now 0.1+ 5e-16, so the
x < 5e-16
part is now false, and
posXMin
remains at 5e-16 for the rest of the program.
To fix this you need to incorporate a precision value into your comparisons:
1 2 3 4 5 6 7 8 9 10 11
|
const double precison = 0.01; // set this to whatever you think is reasonable
// greater than zero
bool IsGreaterThanZero(double x, const double precision) {
if( (x > precision) ) {
return true;
}
else {
return false;
}
}
|
This is more concisely written with the ternary operator:
1 2 3 4 5 6
|
const double precison = 0.01; // set this to whatever you think is reasonable
// greater than zero
bool IsGreaterThanZero(const double x, const double precision) {
return (x > precision) ? true : false;
}
|
For equality with zero:
1 2 3 4
|
// equality with zero
bool IsEqualZero(double x, const double precision) {
return (std::abs(x) < precision) ? true : false;
}
|
For comparison with a number:
1 2 3 4
|
// comparison with a number
bool IsEqualNumber(const double x, const double number, const double precision) {
return (std::abs(x -number) < precision) ? true : false;
}
|
I haven't compiled any of this code, hopefully haven't pulled any clangers!
Hope this makes sense, and helps you out.