I wrote a program that, among other things, utilizes a simple while loop with a '<=' operator. It malfunctioned in that it ignored the 'or equal to' part of the operator and quit the loop early. Here is an example of the part of the program that is malfunctioning, along with the output.
Code:
#include <iostream>
using namespace std;
main(){
double d = 0.1;
double e = 0.02;
double f = 0.2;
while( d <= f ){
cout << "d: " << d << endl;
d += e;
}
cout << endl;
double a = 0.3;
double b = 0.05;
double c = 0.7;
while( a <= c ){
cout << "a: " << a << endl;
a += b;
}
}
This program should first loop from 0.1 to 0.2 in increments of 0.02, printing every number from 0.1 to 0.2, inclusive. It then should loop from 0.3 to 0.7 in increments of 0.05, printing every number from 0.3 to 0.7, inclusive. Here is what it prints:
d: 0.1
d: 0.12
d: 0.14
d: 0.16
d: 0.18
d: 0.2
a: 0.3
a: 0.35
a: 0.4
a: 0.45
a: 0.5
a: 0.55
a: 0.6
a: 0.65
Notice that it does not print 0.7. This is my problem. The same program, executing the same loop, is behaving differently when using different values in the loop. Am I missing something blatantly obvious here? This isn't a complex piece of code here, but maybe one of you out there knows what's happening when it claims that 0.7 <= 0.7 returns false. I'd appreciate any comments.