0
1)#include<stdio.h>
int main()
{
float x=0.5;

if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}

output->lesser

2)#include<stdio.h>
int main()
{
float x=0.1;

if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");

return 0;
}

output->greater Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?

EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?

cst
  • 43
  • 6
  • http://stackoverflow.com/questions/25082569/float-point-numbers-are-not-equal-despite-being-the-same/25082613#25082613 and also look @ http://www.geeksforgeeks.org/comparison-float-value-c/ – vinay hunachyal Aug 18 '14 at 15:18
  • 4
    `x` is a float ~ `0.10000000149011611938` while `0.1` is a double ~ `0.10000000000000000555`. `0.5` is exactly, so `x>0.5` evaluates to false – mch Aug 18 '14 at 15:22
  • I think you're comparing float with double. Change to if(x>(float)0.1) and if(x>(float)0.5) and the result will be always lesser. – eightShirt Jan 28 '16 at 00:22

1 Answers1

3

I almost sure it's because you are comparing float and double.

There's an answer to why it is greater answered here : 0.1 float greater than double

Community
  • 1
  • 1
MrSykkox
  • 528
  • 4
  • 14