Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

Example of non associative floating point addition

I'm writing an article about floating numbers. I put inside some simple test to try in Javascript console.

I'm looking for an example of non commutative addition ie: (x+y)+z!=(z+x)+y

If you have some values for x y z that works thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no such example, because floating point addition, as defined by IEEE-754, is guaranteed to be commutative. The relevant verbiage:

...Each of the operations shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then coerced this intermediate result to fit in the destination's format.

That is, the addition itself is performed with infinite precision; all errors in the result come from the need to discard some of that precision. Since infinite-precision (that is, "regular") addition is commutative, that means the operation as a whole is commutative.

What you may be thinking of is associativity. Floating point addition is not associative, because the precision loss following adding the first two numbers will not generally be the same as that from adding the last two numbers. The most common example of this is known as "catastrophic cancellation": (1 + 1e100) + -1e100 = 0, and 1 + (1e100 + -1e100) = 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...