Associativity math: (a + b) + c != a + (b + c)

前端 未结 6 1098
北荒
北荒 2020-12-15 02:40

Recently I was going through an old blog post by Eric Lippert in which, while writing about associativity he mentions that in C#, (a + b) + c is not equivalent

6条回答
  •  余生分开走
    2020-12-15 03:15

    Short answer is (a + b) + c == a + (b + c) mathematically, but not necessarily computationally.

    Remembering that computers really work in binary, even simple decimals can result in roundoff errors when converted to internal format.

    Depending on the language, even addition can incur roundoff errors, and in the above example, the roundoff error in a+b may differ from that in b+c.

    One surprising offender is JavaScript: 0.1 + 0.2 != 0.3. The roundoff error is a long way down the decimal, but real and problematic.

    It’s a general principal that you reduce roundoff error by adding the small parts first. This way they can accumulate before being overwhelmed by the larger number.

提交回复
热议问题