The value of floatToRawIntBits(0.0f/0.0f) is different on x86_64 and aarch64?
Aleksey Shipilev
shade at redhat.com
Mon Jul 15 06:46:06 UTC 2019
On 7/15/19 6:05 AM, Tianhua huang wrote:
> The value of floatToRawIntBits(0.0f/0.0f) is different on x86_64 and
> aarch64? Does it depends on the platform? I think the behaviour should be
> same on different platforms, right?
Why should it be? 0.0f/0.0f is NaN. There are multiple allowed representations of NaN in IEEE-754.
And there is a difference about "raw" conversions:
"If the argument is NaN, the result is the integer representing the actual NaN value. Unlike the
floatToIntBits method, floatToRawIntBits does not collapse all the bit patterns encoding a NaN to a
single "canonical" NaN value."
(https://docs.oracle.com/javase/8/docs/api/java/lang/Float.html#floatToRawIntBits-float-)
$ cat Test.scala
import java.lang.Float.floatToRawIntBits;
import java.lang.Float.floatToIntBits;
import java.lang.Float.intBitsToFloat;
import java.lang.Float.isNaN;
object Test {
def main(args : Array[String]) {
var f = 0.0f/0.0f;
System.out.println(f);
System.out.println(isNaN(f));
System.out.println(floatToRawIntBits(f));
System.out.println(intBitsToFloat(floatToRawIntBits(f)));
System.out.println(floatToIntBits(f));
System.out.println(intBitsToFloat(floatToIntBits(f)));
}
}
$ scalac Test.scala
$ java Test
NaN
true
-4194304
NaN
2143289344
NaN
So, these are all NaNs, and floatToIntBits returns what you expect, a "canonical" NaN representation.
Trivia: -4194304 is 32-bit binary 1|11111111|10000000000000000000000 (sign|exponent|fraction) which
is in line with IEEE-754 definition of NaN: sign either 0 or 1, biased exponent is all 1 bits,
fraction is anything except 0 bits.
--
Thanks,
-Aleksey
More information about the jdk-dev
mailing list