I was looking at a simple way to approximate Machine Epsilon in Java:
float machEps = 1.0f;
do
machEps /= 2.0f;
while ((float) (1.0 + (machEps / 2.0)) != 1.0);
System.out.println( machEps);
This returns:
1.1920929E-7
However, when I remove the conversion to float
in the while
loop:
float machEps = 1.0f;
do
machEps /= 2.0f;
while ( (1.0 + (machEps / 2.0)) != 1.0);
System.out.println( machEps);
I get:
2.220446E-16
I'm not quite sure why this is....my guess is that in the second case Java attempts to expand machEps
from a float
to a double
. However, I'm not sure if that is an accurate statement or if there is another reason why I get two different answers.
Represents the round-off error for a floating-point number with a given precision. Use the machine epsilon constant to compare whether two floating-point numbers are equivalent.
The machine epsilon (in double precision) is eps =2.220446049250313e−016. It is obtained when the number of terms is n =53.
Machine Epsilon is a machine-dependent floating point value that provides an upper bound on relative error due to rounding in floating point arithmetic. Mathematically, for each floating point type, it is equivalent to the difference between 1.0 and the smallest representable value that is greater than 1.0.
The epsilon of the machine (short: eps) is the minimum distance that a floating point arithmetic program like Matlab can recognize between two numbers x and y.
1.0
and 2.0
are double
s.
Arithmetic between double
s and float
s will implicitly convert the float
s to doubles
.
You need to force the entire expression to use float
s by adding the f
suffix to all of your literals.
Are you looking for java.lang.Math.ulp
?
Returns the size of an ulp of the argument. An ulp of a double value is the positive distance between this floating-point value and the double value next larger in magnitude.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With