Compare floats for equality

  1. Why don’t you just do the == comparison directly instead of writing a method to do the comparison?

    Because the storage of floating-point numbers in the calculation is binary, there will be a loss of precision.

  2. Such as:

    Floating point Numbers16777216The binary of is0-10010111-00000000000000000000000

    Floating point Numbers16777217The binary of is0-10010111-00000000000000000000000

    So they’re equal to each other.
  3. Specific reasons can refer to the article…

2. The tools

Const MIN = 0.000001 // isEqual // float func isEqual (x, y float64) bool {return Math.Abs(x-y) < MIN}