Java中Float的最大值?

发布于 2024-11-29 21:01:00 字数 208 浏览 2 评论 0原文

以下问题表示 Double 的最小值为-Double.MAX_VALUE。对于 Float(即 -Float.MAX_VALUE)来说也是如此吗?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光①夏 2024-12-06 21:01:01

是的,-Float.MAX_VALUE 是最大量值的负数。 float 的表示方式与 double 相同,只是存储空间减半(以及随之而来的精度损失)。由于 IEEE 754 中的符号由单个表示位,翻转该位不会改变其余位可达到的总体幅度。

Yes, -Float.MAX_VALUE is the negative number with largest magnitude. floats are represented the same way as doubles, just with half the storage space (and the accompanying loss of precision.) Since signs in IEEE 754 are represented by a single bit, flipping that bit doesn't change the overall magnitude attainable by the remaining bits.

那片花海 2024-12-06 21:01:01

是的 - 它与 Float.MAX_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
    }
}

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
    }
}
忱杏 2024-12-06 21:01:01

编辑:我原来的答案似乎非常不正确。感谢@aioobe 指出了这一点。

相反,使用java代码的魔力来回答标题问题:

System.out.printf( "Float.MAX_VALUE: %,f\n", Float.MAX_VALUE );

Float.MAX_VALUE: 340,282,346,638,528,860,000,000,000,000,000,000,000.000000

System.out.printf("in scientific notation: %.18g\n", Float.MAX_VALUE );

in science notation: 3.40282346638528860e+38

System.out.printf(
            "in hexadecimal floating-point number with a significand and "
            + "an exponent: %a", Float.MAX_VALUE );

in hexadecimal带有尾数和指数的浮点数:0x1.fffffep127

EDIT: My original answer appears to be badly incorrect. Thank you @aioobe for pointing this out.

Instead, using the magic of java code to answer the title question:

System.out.printf( "Float.MAX_VALUE: %,f\n", Float.MAX_VALUE );

Float.MAX_VALUE: 340,282,346,638,528,860,000,000,000,000,000,000,000.000000

System.out.printf("in scientific notation: %.18g\n", Float.MAX_VALUE );

in scientific notation: 3.40282346638528860e+38

System.out.printf(
            "in hexadecimal floating-point number with a significand and "
            + "an exponent: %a", Float.MAX_VALUE );

in hexadecimal floating-point number with a significand and an exponent: 0x1.fffffep127

空城缀染半城烟沙 2024-12-06 21:01:01

是的,Float 也是如此。

有关详细信息,请查看此处的手册 http://download。 oracle.com/javase/7/docs/api/java/lang/Float.html

Yes, it's also true for Float.

For more information check the manual here http://download.oracle.com/javase/7/docs/api/java/lang/Float.html

只是一片海 2024-12-06 21:01:01

是的,由于与您链接的问题的答案中所述的原因完全相同,浮点数和双精度数使用 IEEE754 表示,由于它们的存储方式,该表示是“对称的”。

Yes it is, and for exactly the same reason as stated in the answer for the question you linked, Floats and Doubles use IEEE754 representation which is "symmetrical" due to the way they are stored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文