为什么我们得到的输出为“无穷大”超出浮动范围后

发布于 2025-01-11 03:11:59 字数 469 浏览 0 评论 0原文

我试图弄清楚,当浮点值超出范围(即大于 3.4E38)时,它的输出将为“无穷大”。我知道,当浮点数保持 max_value 时,指数位中的所有位都将保持“1”,即 11111111,一旦超过范围,它就会变成 100000000,但指数位只能保持 8 位,因此“1”将被截断,并且仅“ 00000000'将被存储在等于0的指数块中。那么,输出屏幕如何打印‘无限’?

'''

public class MyClass
{
    public static void main(String args[]) 
    {
        float f=(float)3.4E+76;
        System.out.println(f);
    }
}

'''

上述代码的输出

I am trying to figure out, when value of float exceed's the range i.e. greater than 3.4E38 it gives the output as 'infinity'. I understand that when float holds max_value all the bits in exponent bit will be holding '1' i.e. 11111111 and as soon as it exceeds the range it becomes 100000000 but Exponent bit can only hold 8 bit so '1' will be trucated and only '00000000' will be stored in the exponent block which is equal to 0. So, how output screen prints 'infinity'?

'''

public class MyClass
{
    public static void main(String args[]) 
    {
        float f=(float)3.4E+76;
        System.out.println(f);
    }
}

'''

output of the above code

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

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

发布评论

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

评论(1

乜一 2025-01-18 03:11:59

无穷大(包括正无穷大和负无穷大)是浮点算术中的特殊值 如浮点标准 IEEE 754 中所定义。某些操作会产生无穷大的值,包括溢出,其格式正确为一个字符串。

正如维基百科文章的基本原理部分中所述:

诸如无穷大和 NaN 之类的特殊值确保浮点算术在代数上是完整的:每个浮点运算都会产生明确定义的结果,并且默认情况下不会抛出机器中断或陷阱。此外,在特殊情况下返回的特殊值的选择旨在在许多情况下给出正确的答案。例如,在 IEEE 754 算术中,连分数如 R(z) := 7 − 3/[z − 2 − 1/(z − 7 + 10/[z − 2 − 2/(z − 3)]) ] 将给出所有输入的正确答案,因为通过给出 +无穷大可以正确处理除以零的电位(例如 z = 3),因此可以安全地忽略此类异常。正如 Kahan 所指出的,在默认的 IEEE 754 浮点策略下,不会发生继浮点到 16 位整数转换溢出而导致阿丽亚娜 5 号火箭丢失的未处理陷阱。

Infinity (including positive and negative infinity) is a special value in floating-point arithmetic as defined in the floating-point standard IEEE 754. Certain operations produce the value infinity, including overflow, and it is appropriately formatted as a string.

As described in the rationale section of the Wikipedia article:

The special values such as infinity and NaN ensure that the floating-point arithmetic is algebraically complete: every floating-point operation produces a well-defined result and will not—by default—throw a machine interrupt or trap. Moreover, the choices of special values returned in exceptional cases were designed to give the correct answer in many cases. For instance, under IEEE 754 arithmetic, continued fractions such as R(z) := 7 − 3/[z − 2 − 1/(z − 7 + 10/[z − 2 − 2/(z − 3)])] will give the correct answer on all inputs, as the potential divide by zero, e.g. for z = 3, is correctly handled by giving +infinity, and so such exceptions can be safely ignored. As noted by Kahan, the unhandled trap consecutive to a floating-point to 16-bit integer conversion overflow that caused the loss of an Ariane 5 rocket would not have happened under the default IEEE 754 floating-point policy.

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