In python how to compare whether the two types of data equal to the float

The computer inside the binary number is saved, some numbers do not accurately stored inside the computer, so he saved a digital closest.

Computer representation float (float or double type) has a limited accuracy, the precision for floating point exceeds the limit, the computer will truncate the fractional portion thereof other than accuracy.

Therefore, to compare two float for equality, not just rely on == to judge, but when the difference is less than their two we can tolerate a small value, they can be considered equal.
Such as:
     return ABS (F1 - F2) <= allowed_error
after python 3.5, PEP485 proposal gives a solution.
     math.close (a, b, rel_tol = 1e-5)
 

Guess you like

Origin blog.csdn.net/qq_35462323/article/details/90638603