The javadoc for Math.rint
says: "If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument." However, the following is false:
public static void main(String[] args) throws Throwable {
double nan = Double.NaN;
System.out.println(Double.doubleToRawLongBits(-nan) == Double.doubleToRawLongBits(Math.rint(-nan)));
}
I am writing a compiler and I need the bits on NaN preserved. Is this a bug in the docs or in the runtime? Or does "same" not mean the same to me as it does in Java?
I assume I have to Double.isNaN
before the rint
call and actually keep the same arguments to preserve my compiler's semantics.
EDIT: This is not a duplicate of this question. My question is about the javadoc and/or the JVM being wrong in its rint
implementation, that one is about whether something is cross platform. Please do not mark the question as duplicate just because both deal w/ alternate forms of NaN on the JVM.