469,946 Members | 1,366 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

comparison of float variable with const value less than 1.

Here is a C pgm, which gives comparison of float variable with its own value, wrong result.
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {float a=0.7;
  3. if(a<0.7)
  4. printf("it is less");
  5. else
  6. printf("it is large");
  7. }
  8.  
Result is
It is less

if value assigned is 1.0,

result is
it is large
Oct 25 '07 #1
1 1670
bartonc
6,596 Expert 4TB
Here is a C pgm, which gives comparison of float variable with its own value, wrong result.

main()
{float a=0.7;
if(a<0.7)
printf("it is less");
else
printf("it is large");
}

Result is
It is less

if value assigned is 1.0,

result is
it is large
0.7 will typically be represented in the computer as
0.69999999999999996, so comparing to the constant might not work quite right.
Oct 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dave | last post: by
8 posts views Thread by Jonathan Fielder | last post: by
5 posts views Thread by Danilo Kempf | last post: by
8 posts views Thread by vijay | last post: by
3 posts views Thread by bofh1234 | last post: by
8 posts views Thread by Gernot Frisch | last post: by
4 posts views Thread by john | last post: by
4 posts views Thread by Stef Mientki | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.