Java: Math.random () Maximum value (double value less than 1)

I was a little curious about this. Math.random () gives a value in the range [0.0, 1.0]. So, what could be the greatest value he can give? In other words, what is the closest double value to 1.0, which is less than 1.0?

+5
source share
3 answers

Java uses the IEEE-754 64-bit representation , so the closest number is less than one in theory 3FEFFFFFFFFFFFFFin hexadecimal, which is 0 for the sign, 1 for the exponent and 1.9999999999999997 for the 52-bit value. This is approximately equal 0.9999999999999998.

References: IEEE-754 Calculator .

+6

, , Math.nextAfter(1.0, -1.0).

. Math.nextAfter(a, 1.0) , a (.. a), Math.nextAfter(a, -1.0) , a (.. a).

. : 1.0-Double.MIN_NORMAL. . 1.0-Double.MIN_NORMAL 1.0.

+4

Double.MIN_NORMAL. , 1,0 1.0-Double.MIN_NORMAL.

Double.MIN_NORMALis 2-1022 so the answer is still extremely close to 1.0. You will need to print the value 1.0-Double.MIN_NORMALup to 308 decimal places before you can see anything other than 9.

0
source

All Articles