Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double.isFinite implementation detail - why DoubleConsts.MAX_VALUE rather than Double.MAX_VALUE?

Tags:

java

java-8

I looked up the implementation of Double.isFinite() which exists since java 8 (because I needed the functionality in java 7):

public static boolean isFinite(double d) {
    return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}

where DoubleConsts.MAX_VALUE is double sun.misc.DoubleConsts.MAX_VALUE with the value 1.7976931348623157E308. This seems to be equivalent to Double.MAX_VALUE, which is defined as:

public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308

Why does this implementation use the constant from the sun.misc-package instead of Double.MAX_VALUE?

(Float.isFinite uses the same pattern)

like image 643
Hulk Avatar asked Sep 22 '17 10:09

Hulk


1 Answers

The reason seems to be mostly a historic one:

The funtionality provided by these methods was previously located in sun.misc.FpUtils. This class does not directly depend on Double, it only imports

import sun.misc.FloatConsts;
import sun.misc.DoubleConsts;

so the implementation made more sense there - it seems that it was exactly the same:

public static boolean isFinite(double d) {
    return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}

See also The ticket that led to moving this to Double

like image 69
Hulk Avatar answered Nov 15 '22 14:11

Hulk