Why does changing the sum order returns a different result?

后端 未结 6 1376
别那么骄傲
别那么骄傲 2020-11-30 16:18

Why does changing the sum order returns a different result?

23.53 + 5.88 + 17.64 = 47.05

23.53 + 17.64 + 5.8

6条回答
  •  無奈伤痛
    2020-11-30 16:53

    Floating point numbers are represented using the IEEE 754 format, which provides a specific size of bits for the mantissa (significand). Unfortunately this gives you a specific number of 'fractional building blocks' to play with, and certain fractional values cannot be represented precisely.

    What is happening in your case is that in the second case, the addition is probably running into some precision issue because of the order the additions are evaluated. I haven't calculated the values, but it could be for example that 23.53 + 17.64 cannot be precisely represented, while 23.53 + 5.88 can.

    Unfortunately it is a known problem that you just have to deal with.

提交回复
热议问题