26

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.

4
  • 1
    i'm vorting it down - ask clear questions! Commented Aug 7, 2014 at 11:15
  • 14
    In fairness, it's clear if you know what an epsilon is (en.wikipedia.org/wiki/Machine_epsilon). Commented Aug 7, 2014 at 11:18
  • Edited the Q. Sorry, I personally don't know other useages of an epsilon other than small error values (and therefore didn't think of this..) Commented Aug 7, 2014 at 12:01
  • 3
    hm, yes, maybe i was a bit emotionaly yesterday, i hope i didn't hurt anybody... thanks for clearing your question! Commented Aug 8, 2014 at 5:13

4 Answers 4

25

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

2
  • 2
    Seven minutes ago, the OP edited their question to define “epsilon” as “The difference between 1 and the smallest value greater than 1 that is representable for the data type”. That value is constant. Commented Aug 7, 2014 at 12:10
  • @Pascal Indeed. I have added the edit to my answer accordingly. Commented Aug 7, 2014 at 12:22
4

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.

2

Without using Math package:

Double.longBitsToDouble(971l << 52)        

That's 2^-52 (971 = 1023(double exponent bias) - 52, shift by 52 is because mantissa is stored on the first 52 bits).

It's a little quicker than Math.ulp(1.0);

Also, if you need this to compare double values, there's a really helpful article: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

0

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

looking up at IEEE 754 you'll find the precision of epsion...

http://en.wikipedia.org/wiki/IEEE_floating_point

binary64:

  • Base(b)=2
  • precision(p)=53
  • machineEpsion(e) (b^-(p-1))/2=2^-53=1.11e-16
  • machineEpsilon(e) b^-(p-1)=2^-52=2.22e-16
3
  • Please add your answer to your answer - I haven't found the word "epsilon" (or "epsion"...) in your Wikipedia link, nor do I think that you understand the question.
    – stuXnet
    Commented Aug 7, 2014 at 11:41
  • i should vote down this answer as well, because it's merely some google research - not even one part thinking in it.... Commented Aug 7, 2014 at 11:53
  • Are you talking about your own answer?
    – stuXnet
    Commented Aug 7, 2014 at 12:39

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.