Is multiplication always commutative in inexact fl

2019-01-20 12:04发布

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.

2条回答
何必那么认真
2楼-- · 2019-01-20 12:34

I know nothing about the D language, but I'll happily answer the question in your title:

Is multiplication always commutative in inexact floating point arithmetic?

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.

查看更多
一夜七次
3楼-- · 2019-01-20 12:39

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.

查看更多
登录 后发表回答