Labels

Monday, June 8, 2020

Test two float number if equal

math.isclose(a,b,rel_tol=1e-9,abs_tol=0)
rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b.
abs_tol is the minimum absolute tolerance
the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
Return True if the values a and b are close to each other and False otherwise.

  
  import math
  import sys
  print(math.isclose(0.3,0.3+1e-7,rel_tol=sys.float_info.epsilon,abs_tol=0))

bool CompareDoubles2 (double A, double B) 
{
   diff = A - B;
   return (diff < EPSILON) && (-diff < EPSILON);
}
Reference: https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison

No comments:

Post a Comment