To Compare Float, Double, or Long Data Types

A way to make doubles, floats, or longs ‘equal’.

Float a=1.56148; Float b=1.56139;
  Float threshold = 0.00009 or 0.0001 //difference of Float a and Float b
  if (Math.abs(a-b) <= threshold){//Or < threshold
     //logic here
  }


The 0.001 is like the maximum edge of actual difference of those two floats;
I think it is better to make the threshold as close to the actual difference as possible.

This kind of way only applies when you really need to make your code skip into ‘//logic here’, and some big decimal values that may need to be equal to avoid further too detailed calculations…

This is not recommended, but sometime useful.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.