Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java double epsilon

Tags:

I'm currently in the need of an epsilon of type double (preferred are constants in java's libraries instead of own implementations/definitions)

As far as I can see Double has MIN_VALUE and MAX_VALUE as static members.

Why there is no EPSILON?

What would a epsilon<double> be?

Are there any differences to a std::numeric_limits< double >::epsilon()?

Epsilon: The difference between 1 and the smallest value greater than 1 that is representable for the data type.

like image 364
nonsensation Avatar asked Aug 07 '14 11:08

nonsensation


People also ask

What does Epsilon do in Java?

Epsilon - a very small number to define an interval For the purposes of comparison, we would be satisfied to say that two floating point numbers were the same if they were within a certain value, eg, 0.00001. So we could write codes something like the following to see if two doubles were effectively "equal".

What is double Epsilon?

Epsilon is a field in C# that represents the smallest positive double value that is greater than zero.

Can you use == to compare doubles in Java?

Using the == Operator As a result, we can't have an exact representation of most double values in our computers. They must be rounded to be saved. In that case, comparing both values with the == operator would produce a wrong result.


2 Answers

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).)

like image 191
Smalltown2k Avatar answered Oct 23 '22 11:10

Smalltown2k


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.

like image 30
Christian Borgelt Avatar answered Oct 23 '22 10:10

Christian Borgelt