Epsilon Machine

I considered 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 transform in floatin a loop while:

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 don’t quite understand why this is .... my guess is that in the second case, Java is trying to deploy machEpsfrom floatto double. However, I'm not sure if this is an exact statement, or there is another reason why I get two different answers.

+4
source share
3 answers

1.0and 2.0are doubles.

double float float doubles.

float, f .

+6

java.lang.Math.ulp?

ulp . Ulp - , .

+2

You can easily find the smallest float that is strictly greater than 1.0 using Math.nextUp (1.0f).

0
source

Source: https://habr.com/ru/post/1525647/


All Articles