Maximum value for Float in Java?

前端 未结 5 620
一整个雨季
一整个雨季 2021-02-05 02:54

The following question indicates that the minimum value of a Double is -Double.MAX_VALUE. Is this also true for Float (i.e., -Float.MAX_VALUE)?

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 03:35

    Yes - it's the same bit pattern as Float.MAX_VALUE except with the sign bit flipped... and that's another way to get at the value:

    public class Test {
        public static void main(String[] args) {
            // Float.MAX_VALUE is intBitsToFloat(0x7f7fffff)
            // so we set the most significant bit - the sign bit
            float f = Float.intBitsToFloat((int) 0xff7fffff);
            System.out.println(f == -Float.MAX_VALUE); // true
        }
    }
    

提交回复
热议问题