0
Friends What is the difference.. here
int x=2; if(x==2) Condition is true float c=9.6; if(c==9.6) Condition is false Why it is false.....could you explain....âș
7 Answers
+ 11
You can review this page for more information
https://www.geeksforgeeks.org/comparison-float-value-c/
+ 7
Because floating point variables are not precision numbers. What to you is 9.6 could 9.60000000000001 or 9.600001 neither are equal. Checking for equality with floating point numbers is fraught with issues.
see: http://elliot.land/post/comparing-floating-point-numbers-in-c-c
+ 6
= is assignment operator use to assign left side value to right side variables
== is comparison operator use to compare left and right side value
+ 6
malepu rakesh oh didn't read the question correctly.
By default any floating point value is treated as double value and you are comparing float 9.6 to double 9.6 that's why results is coming false
+ 1
c and 9.6 are of different datatypes.
c is a float. 9.6000000
9.6 is a double. 9.600000000000000
because decimal numbers are set to double by default.
And like jay said, the program also wants to add 1 as the last digit of the decimal expansion (just to make programmers mad).
You can refuse the program from changing decimal numbers to doubles by default by adding f to the end of the decimal number 9.6f.
+ 1
Thank you very much friends GAWEN STEASY, jay , Seb Thes
0
Yes friend...I know that....my doubt is
if we compare integer variable and integer value..... it's getting true
But if we compare float variable and float value ......it's getting false..... why it's getting false....?