13

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?

9
  • 1
    Alternative terms: "precision", "resolution". I like these precisely ;) because they don't sound overly technical.
    – stakx
    Aug 7, 2013 at 22:10
  • 1
    Off-topic: The Floating-Point Guide recommends against using this type of near-equality comparison.
    – stakx
    Aug 7, 2013 at 22:13
  • 1
    @stakx - the terms you suggest are incorrect and have different meanings from what the OP is asking about. The question is detailed, yes, but it is answerable based upon external reference and it does have relevance to programming when dealing with floating point values. It's constructive and on-topic.
    – user53019
    Aug 8, 2013 at 13:24
  • 1
    @GlenH7: I never said that the question was not a good one, or not answerable. In fact, I was the one who upvoted it. And since you're claiming that the (admittedly less precise) terms which I suggested are incorrect, I'd be interested in learning why that is so.
    – stakx
    Aug 8, 2013 at 15:36
  • @stakx - apologies for implying you had voted to close. I was reacting more to the four close votes on the question at the moment.
    – user53019
    Aug 8, 2013 at 16:21

5 Answers 5

20

Epsilon in maths and engineering

In maths and engineering in general:

  • Delta is generally used to refer to a difference, which can be of any scale.
  • Epsilon is generally used to refer to a negligible quantity.

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 than 1.0f. That latter number is 1.00000011920928955078125f for floats in Java and can be calculated with:

float f = Float.intBitsToFloat(Float.floatToIntBits(1f) + 1);

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:

9223372036854775808f == 9223372036854775808f + 1000000000f; //this is true!

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 and f, but their difference is still well below the machine epsilon:

float small = Float.MIN_VALUE; // small = 1.4E-45
float f = Float.intBitsToFloat(Float.floatToIntBits(small) + 100000000); // f = 2.3122343E-35
boolean b = (f - small < 0.00000011920928955078125f); //true!

The article linked in GlenH7's answer investigates float comparison further and proposes several solutions to overcome these issues.

5
  • 2
    -1: In scientific computational software, Epsilon refers to either Machine epsilon or Relative epsilon (see same article). Typically, this is not the same quantity used in accepting approximate equality, because rounding errors are multiples of machine epsilons or relative epsilons, and typically a few order of magnitudes bigger than that.
    – rwong
    Aug 8, 2013 at 19:30
  • 2
    @rwong That is one specialisation of the term epsilon, and there are many other. In engineering in general, epsilon does refer to a small quantity or an error and Machine epsilon is compatible with that idea.
    – assylias
    Aug 8, 2013 at 22:47
  • @assylias, using a name which has a standard definition, in a context where the standard definition makes sense, but for something which isn't corresponding to the standard definition is a receipt for problems. Aug 11, 2013 at 14:18
  • 1
    @AProgrammer I disagree that the general definition of epsilon is not applicable to computing.
    – assylias
    Aug 11, 2013 at 17:43
  • 2
    @assylias: thanks for the clarification. I have removed my -1.
    – rwong
    Aug 11, 2013 at 20:09
16

In mathematics, delta is used to represent some difference from a value, epsilon is used to represent an arbitrary error value. In this case, epsilon would be the conventional name.

0
8

To directly answer your question, you want to use the term epsilon. More accurately, it's machine epsilon but common usage drops "machine" and just uses epsilon.

Looking in my local copy of float.h I see:

#define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */  
#define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */  
#define LDBL_EPSILON    DBL_EPSILON             /* smallest such that 1.0+LDBL_EPSILON != 1.0 */

And the associated comments makes it clear that epsilon is the term you're referring to.

But we can also rely upon some other, external references to verify that epsilon is the correct term. See here, here, here, and finally this combination of SO query tags. I wasn't able to find a direct reference to the IEEE 754 standard to quote.


You didn't ask, but I found this reference that is very relevant to the example you provided to clarify your question.

Have a look at this blog article by Bruce Dawson of Valve on comparing floating point values for some insight as to why you don't want to use the comparison that you suggested.

There's quite a bit of information packed into that article, but this is the most relevant snipppet from there:

If comparing floats for equality is a bad idea then how about checking whether their difference is within some error bounds or epsilon value, like this:

bool isEqual = fabs(f1 – f2) <= epsilon;

With this calculation we can express the concept of two floats being close enough that we want to consider them to be equal. But what value should we use for epsilon?
Given our experimentation above we might be tempted to use the error in our sum, which was about 1.19e-7f. In fact, there’s even a define in float.h with that exact value, and it’s called FLT_EPSILON.
Clearly that’s it. The header file gods have spoken and FLT_EPSILON is the one true epsilon!
Except that that is rubbish. For numbers between 1.0 and 2.0 FLT_EPSILON represents the difference between adjacent floats. For numbers smaller than 1.0 an epsilon of FLT_EPSILON quickly becomes too large, and with small enough numbers FLT_EPSILON may be bigger than the numbers you are comparing!

Dawson goes over quite a few other considerations about the intricacies involved when comparing floats and dealing with very small values like this, so I would encourage your reading the rest of his post.

2
  • You may want to clarify the first part of your answer: Bruce's article already explains why one should not use a constant epsilon (such as the ones defined in a header file) for tolerance comparison. Also, in many cases, an error of a few millions of ULPs is not something to worry about, because in most applications, we care more about the significant digits more than the errors in the least-significant digits, because double-precision already gives many more digits than we care about.
    – rwong
    Aug 10, 2013 at 23:08
  • @rwong - as I read it, the question was to identify the correct term to use for a constant's name. So that's why I provided the float.h reference along with a few others to machine epsilon. The article from Dawson is something I found while searching for the IEEE 754 reference and I thought was relevant to the OP's simplest formula for comparison. Many use that approach as a first attempt, and I included Dawson's article because it really goes into the nuances of just how tricky the comparison is. So I tried to directly answer the question and then point out why not to use it that way.
    – user53019
    Aug 11, 2013 at 13:40
5

This is an error function; absolute error is usually called ε (epsilon) or Δx for some quantity x:

ε = |expected − actual|

Δx = |x0x |

Relative error is sometimes called η (eta):

η = |1 − actual/expected|

For programming purposes, absoluteError and relativeError (or some abbreviations thereof) are more descriptive. If you want to assert that the error is less than a certain value, that value would simply be called a threshold or tolerance.

See:

0
4

I would call it "tolerance".

Maybe that is not the mathematically correct term, but the mere fact that you ask the question implies to me that neither "delta" nor "epsilon" would be a good variable name to use.

In my experience, it is better to use identifier names that makes sense to those who will actually read the code. What good is a perfectly correct name if it means that the reader needs to look it up on Wikipedia to understand what it means?

4
  • +1. I do always hope people ask their coworkers about these naming questions as well as posting here.
    – MarkJ
    Aug 8, 2013 at 17:21
  • 7
    -1, Better to learn conventions than avoid them.
    – djechlin
    Aug 8, 2013 at 18:20
  • +1 because this is the exact same reason I posted this question. Aug 8, 2013 at 19:27
  • I see this used and there doesn't seem to be a single convention, for example, in R's testthat library.
    – ggorlen
    Jan 15, 2022 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.