Why the output is not as expected?
#include <stdio.h>
void main(){
float a,b,c;
b=0.7;
if( b<0.7 )
printf(" It should NOT be here");
else
printf("It Should be here");
}
Why the output is not as expected?
#include <stdio.h>
void main(){
float a,b,c;
b=0.7;
if( b<0.7 )
printf(" It should NOT be here");
else
printf("It Should be here");
}
0.7 is double value. Try 0.7f in code. It should work.
Floating point numbers
0.7
is such value. On my platform is
0.699999988
.
Reason: we write numbers as decimal, but internal are realised as binary. Many longer materials exist
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers.2C_conversion_and_roundingSo behaviour You are surprised
Please Try the below Code, it works!!!:
Code
#include <stdio.h>
int main(void)
{
float a,b,c,temp;
temp=0.7;
b=0.7;
if( b<temp )
printf(" It should NOT be here");
else
printf("It Should be here");
}