Estou criando um valor float usando bits inteiros brutos, onde a mantissa tem todos os bits definidos como 1. No entanto, converter para double inverte o bit final para 0. Por que isso acontece? Pelo que tenho visto online, a conversão de float para double supostamente preserva a mantissa e o expoente, já que double pode acomodar todos os floats.
class Main {
public static void main(String[] args) {
float f = Float.intBitsToFloat((1 << 23) - 1);
// The following line gives 23 consecutive 1s representing a mantissa with all bits set to 1.
System.out.println(Integer.toBinaryString(Float.floatToIntBits(f)));
double d = f;
// The following line gives 11100000001111111111111111111111000000000000000000000000000000.
// The mantissa has 22 consecutive 1s not 23 like in the original float
System.out.println(Long.toBinaryString(Double.doubleToLongBits(d)));
}
}
Estou usando o JDK 17 e adicionar strictfp à função mantém esse comportamento. Recebo um aviso informando que strictfp não é mais necessário.