c++ - Two General CS Questions -


this question has answer here:

when comparing 2 "real" numbers equality, why should not use == operator, , should use instead?

what difference between coersion , casting? general assumption casting when force value of type so:

int n = 9; return double(n)/5; 

to answer first question directly: “[why] should not use == operator”? answer because earlier operations have produced rounding errors, , is, in general, impossible compute correct result of function applied incorrect data. if have computed values x , y model exact mathematical values x , y, x , y have been affected rounding errors, there no function of x , y tells whether x equals y.

this means impossible compute whether x equals y in these circumstances. not == operator problem. (== in fact 1 of few floating-point operations computed no error @ all; returns correct result given inputs.) problem there no function gives correct answer incorrect input. (and not problem ==. if have x rounding errors, function computed contain errors: sqrt(1-x*x) have errors, , acos(x) have errors. worse, might signal exceptions because 1-x*x might incorrectly negative or x might incorrectly greater one.)

then question becomes “what instead?”

“fudging” whether comparison reports true or false introduces new errors program. question is, errors acceptable in application? if comparison reports 2 numbers equal when unequal exact mathematics, acceptable or unacceptable? if comparison reports 2 numbers unequal when equal, acceptable or unacceptable?

the answers these questions differ program program. generally, there multiple things consider:

  • how error may present in values being compared?
  • for cases in exact values differ acceptable comparison report true?
  • for cases in exact values equal acceptable comparison report false?

the answers above question depend on each application, there no general answer use instead of ==. applications may able use relative tolerance in comparison, may able use absolute tolerance, may need else. , applications might not find comparison acceptable given errors in values. in such cases, need redesign calculations reduce errors, or find other solutions.

so:

  • beware of advice compare tolerance, either relative or absolute. whether using tolerance acceptable depends on application; there no general rule. how tolerance acceptable depends on application; there no general rule.
  • whether there acceptable solution depends on errors in computed values. errors may large allow solution. beware of general recommendations magnitude of tolerance. floating-point errors can vary 0 infinity , dependent on circumstances, there no general tolerance works.
  • be aware all functions computed data errors result in errors, not ==.

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -