Friends What is the difference.. here | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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....☺

24th May 2019, 9:02 AM
malepu rakesh
malepu rakesh - avatar
7 Answers
+ 11
You can review this page for more information https://www.geeksforgeeks.org/comparison-float-value-c/
24th May 2019, 10:37 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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
24th May 2019, 9:48 AM
jay
jay - avatar
+ 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
24th May 2019, 9:05 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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
24th May 2019, 9:23 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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.
24th May 2019, 10:11 AM
Seb TheS
Seb TheS - avatar
+ 1
Thank you very much friends GAWEN STEASY, jay , Seb Thes
24th May 2019, 10:32 AM
malepu rakesh
malepu rakesh - avatar
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....?
24th May 2019, 9:09 AM
malepu rakesh
malepu rakesh - avatar