a=0.7 resolves to a<0.7?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dheeraj agrawal

    a=0.7 resolves to a<0.7?

    I declared a=0.7,but when I checked with if else it goes to statement a<0.7,why?
    Last edited by MMcCarthy; Oct 24 '10, 05:45 AM. Reason: question moved from title
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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:

    Code:
    if ( (a - .000001) < 0.7)
    etc...

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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:
      Code:
      if (fabs(variable - desiredValue) < threshold)
      ...
      Refer to C FAQ 14.5 for the best way to do it.

      Comment

      Working...