使用双精度浮点数实现整数乘法直到2 ^ 53? [英] Is integer multiplication implemented using double precision floating point exact up until 2^53?

查看:479
本文介绍了使用双精度浮点数实现整数乘法直到2 ^ 53?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问,因为我计算矩阵乘法,其中矩阵的所有值是整数。

我想使用LAPACK,以便快速得到正确的代码。将两个大整数(产品小于 2 ^ 53 )存储为<​​code> double s,包含确切整数结果的 double >

解决方案

您的分析是正确的:


  • -2 53 和2 53 之间的所有整数均可精确地以double精度表示。

  • IEEE754标准要求精确执行计算,然后将其四舍五入到最接近的可表示数字。


    因此,两个等于该范围内整数的值的乘积将被精确地表示。
    $ b 参考每个计算机科学家都应该知道关于浮点运算的知识。关键部分是关于操作的IEEE标准的讨论。这包含了上面第二个要点的陈述。你已经知道了第一个要点,这是完成论证的第二点。


    I ask because I am computing matrix multiplications where all the matrix values are integers.

    I'd like to use LAPACK so that I get fast code that is correct. Will two large integers (whose product is less than 2^53), stored as doubles, when multiplied, yield a double containing the exact integer result?

    解决方案

    Your analysis is correct:

    • All integers between -253 and 253 are exactly representable in double precision.
    • The IEEE754 standard requires calculations to be performed exactly, and then rounded to the nearest representable number.

    Hence a product of two values that equals an integer in that range will therefore be represented exactly.

    Reference: What every computer scientist should know about floating-point arithmetic. The key section is the discussion of the IEEE standard as pertaining to operations. That contains the statement of the second bullet point above. You already knew the first bullet point and it's the second point that completes the argument.

    这篇关于使用双精度浮点数实现整数乘法直到2 ^ 53?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆