When I do this:
NSLog(@"%i",1.5 - 1.00 == 0.5);
I get 1, but when I do this:
NSLog(@"%i",1.33 - 1.00 == 0.33);
I get 0.
Is there any way to get around this?
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.
The problem is with the precision of floating-point calculations: you should generally avoid comparing floats and double-precision numbers for equality using
==operator, preferring a check for the difference to be smaller than a tiny epsilon (e.g.1E-9).The first example works because the numbers are composed of powers of 2:
1 = 2^0, and0.5 = 2^-1. The numbers from the second example cannot be decomposed as powers of 2 exactly, so the equality check does not work.