hey im working on a program that calculates the trajectory of an object fired at a certain velocity with an angle X. the Program runs perfectly except for the fact that I can't get my variable to react with the if statement. in the main() i have a do while statement and at the end of the do...while (before it repeats) I am doing a time += .01 where time is a double variable type. I have an if statement at the beginning of main() where i check to see if (time == 1) then output something. The if statement is never entered for some reason, and I can't figure it out. Please help if you have any solutions. I have put some of the code below that pertains. All of the variables used have been declared.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
double time = 0;
do
{
//If the Time = a nondecimal second then it displays the time, xCoordinate, and yCoordinate
if (time == 1)
{
cout << time << " ";
cout << fixed << setprecision(2) << xCoordinate << " " << setprecision(2) << yCoordinate << endl;
}
//adds .01 of a second onto the time
time += .01;
while(yCoordinate >= 0);
where time is a double variable type. [snip] i check to see if (time == 1) [snip] The if statement is never entered for some reason
This is because floating point variables (doubles) are approximations. == will virtually never work. Time may not ever be actually 1, it might be 0.99999999999999 or 1.000000000001.
Avoid using == and != with floating points. Instead, try to use >= or similar.