I'm comparing floats in Java right now and the simplest formula is:
Math.abs(a - b) < THRESHOLD
When naming your variable for the threshold of difference, should you name it delta or epsilon? Specifically, which of the two is the correct term for the smallest value that a floating-point number can represent?
Is the term programming language specific, or is it universal across languages?
Best Answer
Epsilon in maths and engineering
In maths and engineering in general:
and epsilon seems more appropriate in your case.
Epsilon in computer science
In computer science in particular, the term epsilon also refers to machine espilon which measures the difference between
1.0f
and the smallest float which is strictly larger than1.0f
. That latter number is1.00000011920928955078125f
for floats in Java and can be calculated with:The definition of machine epsilon is consistent with the general use of epsilon described above.
Comparing floats
Note however that before comparing floats for "proximity", you need to have an idea of their scale. Two very large and supposedly very different float can be equal:
And inversely, there might be many possible float values (and several orders of magnitude) between two small floats which differ by the machine epsilon "only". In the example below, there are 10,000,000 available float values between
small
andf
, but their difference is still well below the machine epsilon:The article linked in GlenH7's answer investigates float comparison further and proposes several solutions to overcome these issues.