strange situation, when performing the following lines of Code:
const float a = 47.848711;
const float b = 47.862952;
float result = b - a;
I get a (NSLog %.10f) result = 0.0142440796.
I expected to get 0.0142410000.
What’s going on?
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.
What if I ask you the following:
In this case, the answer is obvious, right? 1.3 isn’t an integer, so the actual value that gets stored in
ais 1, and the value that gets stored inbisn’t 2.7, but rather 2. When I subtract 1 from 2 I get exactly 1, which is the observed answer. If you’re with me so far, keep reading.The exact same thing is happening in your example. 47.848711 isn’t a single-precision float, so the closest floating-point value is stored in
ainstead, which is exactly:Similarly, the value stored in
bis the closest floating-point value to47.862952, which is exactly:When you subtract these numbers to get
result, you get:When you round that value to 10 digits to print it out, you get: