By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

a=0.7 resolves to a<0.7?

P: n/a
I declared a=0.7,but when I checked with if else it goes to statement a<0.7,why?
Oct 23 '10 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You don't post your if/else statement.

I suspect you are running into rounding.

You should compare your floating point numbers +/- using an allowable precision:

Expand|Select|Wrap|Line Numbers
  1. if ( (a - .000001) < 0.7)
  2. etc...
Oct 24 '10 #2

Expert 100+
P: 2,398
Read what every computer scientist should know about floating-point arithmetic.

By the way, a better way to compare floating-point numbers for equality within a precision threshold is more like this:
Expand|Select|Wrap|Line Numbers
  1. if (fabs(variable - desiredValue) < threshold)
  2. ...
Refer to C FAQ 14.5 for the best way to do it.
Oct 24 '10 #3

Post your reply

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