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.
Answer from assylias on Stack ExchangeEpsilon 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.
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.
I'm presuming you mean epsilon in the sense of the error in the value. I.e this.
If so then in Java it's referred to as ULP (unit in last place). You can find it by using the java.lang.Math package and the Math.ulp() method. See javadocs here.
The value isn't stored as a static member because it will be different depending on the double you are concerned with.
EDIT: By the OP's definition of epsilon now in the question, the ULP of a double of value 1.0 is 2.220446049250313E-16 expressed as a double. (I.e. the return value of Math.ulp(1.0).)
By the edit of the question, explaining what is meant by EPSILON, the question is now clear, but it might be good to point out the following:
I believe that the original question was triggered by the fact that in C there is a constant DBL_EPSILON, defined in the standard header file float.h, which captures what the question refers to. The same standard header file contains definitions of constants DBL_MIN and DBL_MAX, which clearly correspond to Double.MIN_VALUE and Double.MAX_VALUE, respectively, in Java. Therefore it would be natural to assume that Java, by analogy, should also contain a definition of something like Double.EPSILON with the same meaning as DBL_EPSILON in C. Strangely, however, it does not. Even more strangely, C# does contain a definition double.EPSILON, but it has a different meaning, namely the one that is covered in C by the constant DBL_MIN and in Java by Double.MIN_VALUE. Certainly a situation that can lead to some confusion, as it makes the term EPSILON ambiguous.
equality - What's wrong with using == to compare floats in Java? - Stack Overflow
How do you compare two double values
Small lesson of the day: never check two floats for equality!
Need help understanding machine epsilon
You do NOT use double to represent money. Not ever. Use java.math.BigDecimal instead.
Then you can specify how exactly to do rounding (which is sometimes dictated by law in financial applications!) and don't have to do stupid hacks like this epsilon thing.
Seriously, using floating point types to represent money is extremely unprofessional.
Yes. Java doubles will hold their precision better than your given epsilon of 0.00001.
Any rounding error that occurs due to the storage of floating point values will occur smaller than 0.00001. I regularly use 1E-6 or 0.000001 for a double epsilon in Java with no trouble.
On a related note, I like the format of epsilon = 1E-5; because I feel it is more readable (1E-5 in Java = 1 x 10^-5). 1E-6 is easy to distinguish from 1E-5 when reading code whereas 0.00001 and 0.000001 look so similar when glancing at code I think they are the same value.
the correct way to test floats for 'equality' is:
if(Math.abs(sectionID - currentSectionID) < epsilon)
where epsilon is a very small number like 0.00000001, depending on the desired precision.
Floating point values can be off by a little bit, so they may not report as exactly equal. For example, setting a float to "6.1" and then printing it out again, you may get a reported value of something like "6.099999904632568359375". This is fundamental to the way floats work; therefore, you don't want to compare them using equality, but rather comparison within a range, that is, if the diff of the float to the number you want to compare it to is less than a certain absolute value.
This article on the Register gives a good overview of why this is the case; useful and interesting reading.