在不精确的浮点运算中,乘法总是可交换的吗?
我试图理解 D 语言运行时中的一些代码。似乎以下两件事有单独的函数:
array1[] += scalar * array2[];
array1[] += array2[] * scalar;
为什么不能用一个函数来完成这些事情?我认为即使在不精确的浮点算术中,乘法也是可交换的。
I'm trying to understand some code in the D language runtime. It seems like there are separate functions for the following two things:
array1[] += scalar * array2[];
array1[] += array2[] * scalar;
Why can't these be done with one function? I thought multiplication was commutative even in inexact floating-point arithmetic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对D语言一无所知,但我很乐意回答你标题中的问题:
直到 NaN 值的“有效负载”为止,是的。 IEEE-754 浮点乘法是可交换的(加法也是如此)。如果您不知道 NaN 的有效负载是什么,请不要担心。
I know nothing about the D language, but I'll happily answer the question in your title:
Up to the "payload" of NaN values, yes. IEEE-754 floating-point multiplication is commutative (and so is addition). If you don't know what the payload of a NaN is, don't worry about it.
我想区别只是在于函数原型 - 一个是
(double, double[])
,另一个是(double[], double)
。但无论哪种方式,结果都应该是相同的。I guess the difference is just in the function prototypes - one is
(double, double[])
and the other is(double[], double)
. But the result should be the same either way.