In this video tutorial lets see how we can compare a floating point variable with a constant value, and lets see the result.
Related Read:
Sizeof Operator in C Programming Language
C Program To Convert Decimal Number To Binary Number, using While Loop
Page Contents
If you assign 0.7 to a floating point variable num and then inside if condition you check if num == 0.7, it returns false. Because here variable num is of data type float and the constant value 0.7 is considered as double type data.
#include<stdio.h> int main() { float num = 0.7; if(num == 0.7) printf("Yes, true\n"); else printf("Not True\n"); return 0; }
Output:
Not True
Some of you might get surprised with the result. But there is nothing to be surprised. It’s working the way it is intended to work.
We need to keep the basics in mind. When we input decimal numbers, it gets converted into Binary number system and then the further calculation occurs.
Now getting back to our problem: value present in floating point variable num is converted to its binary equivalent. But float has 4 bytes of memory storage.
#include<stdio.h> int main() { float num = 0.7; printf("%d\n", sizeof(num)); printf("%d\n", sizeof(0.7)); return 0; }
Output:
4
8
Next the constant value 0.7 is converted into its binary equivalent. Since its data type is double, it has 8 Bytes of memory for storage. Hence it can store higher precision number.
Now the comparison between these two binary number occurs. Inside double, the binary number has more precision compared to floating point variable – due to available memory for each data type.
For Example:
#include<stdio.h> int main() { float PI = 3.14; double M_PI = 3.14159265358979323846; if(PI == M_PI) printf("YES\n"); else printf("NO\n"); return 0; }
Output:
NO
Even though both variable PI and M_PI has value of PI in it, they’re not equal.
There are 2 quick fixes for this bug:
1. Typecast constant value to float.
2. Declare double type variable instead of floating point variable.
1. Typecast constant value to float.
#include<stdio.h> int main() { float num = 0.7; if(num == 0.7f) printf("Yes, true\n"); else printf("Not True\n"); return 0; }
Output:
Yes, true
Inside if condition, we’ve converted double value 0.7 into floating point data, by appending f to it.
2. Declare double type variable instead of floating point variable.
#include<stdio.h> int main() { double num = 0.7; if(num == 0.7) printf("Yes, true\n"); else printf("Not True\n"); return 0; }
Output:
Yes, true
We had written a C program to Find Grade of Steel. One of our reader reported this comparison bug, and we fixed it using above method mentioned in this video.
Purposefully we’ve not altered the old code. We’ve preserved both old and new code, so that people can learn from our mistake. Take a look at: C Program To Find Grade of Steel
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert