Click here to Skip to main content
15,887,930 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Objective-C
#include<stdio.h>

int main(){

float x = 9.81 ; 
if( x == 9.81)
{printf("the value of x is 9.81\n");}
else {printf("No its not 9.81 \n");}
}


In this we expect "if" statement to execute but instead "else" statement get executed.
Why is it so??
Posted
Updated 14-Jan-16 8:16am
v2

it is common issue that float arent exactly what they seem. ;-)

The reason is there internal representation in the memory. Make an output of it.

The solution is to calculate the difference an accept a small one. Remember that it maybe signed.

C++
float x = 9.81f; 
float d = x - 9.81f;

printf("the diff is %f\n", d);

if( fabs(d) < 0.005/*accepted tolerance*/ )
{printf("the value of x is 9.81\n");}
else {printf("No its not 9.81 \n");}
}
 
Share this answer
 
v3
Comments
Dishank Bansal 14-Jan-16 13:38pm    
Still its Executing the "else" statement...
You try yourself once @karstenk
CPallini 14-Jan-16 13:56pm    
Because you should call fabs (you have to include math.h) instead of abs. See the updated solution.
Dishank Bansal 14-Jan-16 15:20pm    
Thank You...
Dishank Bansal 14-Jan-16 15:24pm    
But why is f added after 9.81??
Sergey Alexandrovich Kryukov 14-Jan-16 15:45pm    
This is the syntax for the literal of the type float.
—SA
KarstenK already gave you the solution. I suggest you to read: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"[^].
 
Share this answer
 
Comments
Maciej Los 14-Jan-16 14:17pm    
5ed!
Sergey Alexandrovich Kryukov 14-Jan-16 15:40pm    
My 5. As this article contains a lot of information, I added some more concrete explanation in Solution 3, please see.
—SA
nv3 14-Jan-16 17:13pm    
Yes, sir! 5.
Just my 5 cents in addition to other answers:

It's not a good idea to use the operator == with floating-point operands. That's right, I don't know the situations where it can be useful at all. Even with floating-point values representing numbers with zero fractional part, you should not use it; instead you can round a value and typecast to one of the integer types, and even that is rarely needed. Floating-point values represent approximate values. If is impossible to represent "all" real number is such a finite-state machine as computer, even a single real number, generally, contains infinite volume of information. A typical comparison, in case when it makes sense, may look like:
C++
#include <cmath>
double margin = //... some value which is "small enough"
double myValue = //...
double testValue = //...
// let's say, you mean to compare:
// if (myValue == testValue) ... // bad idea
// instead, do this:
if (std::abs(myValue - testValue) < margin) // ...


[EDIT]

After I submitted this post, I found that this code sample is essentially the same as in Solution 1. Crediting that, I decided not to remove mine, maybe it may provide some extra clarification.

Sometimes you need a different criterion, "relative closeness". For example
C++
double margin = 0.001; // say, 0.1%
if (std::abs(myValue - testValue) * 2 <
    margin * (myValue + testValue)) // ...

It is useful if the compared values are supposed to be "big" values with "small" difference between them, so the criterion is to check if the relative difference is "small enough".

[END EDIT]

See also: std::abs(float), std::fabs - cppreference.com[^].

The principles of floating-point calculation have been analyzed and explained to the practical software developers quite deeply in the classical Donald Knuth's book The Art of Computer Programming.

—SA
 
Share this answer
 
v4
Comments
CPallini 14-Jan-16 15:44pm    
My 5.
Sergey Alexandrovich Kryukov 14-Jan-16 15:46pm    
Thank you, Carlo. (Probably, you forgot the vote itself.)
—SA
CPallini 14-Jan-16 15:59pm    
Sorry, fixed now.
Sergey Alexandrovich Kryukov 14-Jan-16 17:23pm    
Thank you, and I fixed my answer some more...
—SA
Wendelius 16-Jan-16 4:22am    
Nice answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900