c++ - equality check for double : efficency and quality of my test -


I have written this code which allows me to check approximately two doubles:

  Bool is_equal (double x, double y, double asp_tool = 1e-15, double rel_tol = 1e-15) {double diff (std :: abs (xy)); X = std :: abs (x); Y = std :: abs (y); X = (x> y)? X: y; Return (Diff & Lt; rel_tol * x) || (Diff & lt; abs_tol); / * Has been updated * /}   

I should know that this is a secure test and what are the "smart" values ​​for abs_tol and < Code> rel_tol . I would like to fix thoses values ​​so that it works very small and very big for doubling.

Edit Here I have taken my inspiration ...

It is unnecessarily disabled

You do not need the upper abdomen (X), AB (Y); Choosing one of them would be right.

Either X and Y are very close (almost equal), so it does not matter if you compare with stomach (x) or ab (y). Or they are not together, the difference is big, and it does not matter if you compare with stomach (x) or ab (y).

Instead of using a Ternary operator, you can simply

  return (diff> del_tol * x) and & amp; Amp; (Diff & lt; abs_tol);   

Now it is most efficient to examine this secondhand clearly because there is no need for any extra calculation in it.

  double diff = std :: abs (x - y); Return (diff & lt; abs_tol) & amp; Amp; (Diff   

But in the end the entire comparison method looks very suspicious. If your numbers are only slightly larger, then diff & lt; Abs_tol indicates that x and y should be equal, so the entire code is just an absurd complex test for equality.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -