如何将两个浮点数相加导致溢出

How to add two float numbers resulting in overflow

我有两个二进制小数要相加:

1.100110011001100110011001100110011001100110011001101 x 2-4

11.001100110011001100110011001100110011001100110011010 x 2-4

如果我简单地添加它们,似乎会导致溢出(54位):

      1.100110011001100110011001100110011001100110011001101
   + 11.001100110011001100110011001100110011001100110011010
      -----------------------------------------------------
    100.110011001100110011001100110011001100110011001100111

如果我仍然需要将其存储为双精度 52 位尾数,我该如何处理?

加法后的下一步是调整指数,使前导有效位紧接在二进制小数点之前。在这种情况下,您需要将指数加 2。

新的有效数字是 1.00110011001100110011001100110011001100110011001100111

现在四舍五入到53位,去掉最后的11位,根据四舍五入方式调整。如果舍入到最近,则需要向上舍入。