Possible Duplicate:
Addition of Double values inconsistent
int x = 0;
float n = 0;
while ( n != 1 ) {
n += 0.1;
++x;
}
I wonder Why this loop is infinite?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
0.1cannot be represented exactly in floating-point:displays:
So
nwill never be exactly equal to1.As @sirlak says in comments below, it’s almost never correct to test floating-point variables for equality like this.