Floating Point Arithmetic - Modulo Operator on Double Type

后端 未结 4 2104
野性不改
野性不改 2020-12-10 05:35

So I\'m trying to figure out why the modulo operator is returning such a large unusual value.

If I have the code:

double result = 1.0d % 0.1d;

4条回答
  •  醉话见心
    2020-12-10 06:22

    It's not precisely an "error" in the calculation but the fact that you never really had 0.1 to start with.

    The problem is that 1.0 can be represented exactly in binary floating point but 0.1 cannot, because it can't be constructed exactly from negative powers of two. (It's 1/16 + 1/32 + ...)

    So you aren't really getting 1.0 % 0.1, the machine is left to compute 1.0 % 0.1 +- 0.00... and then it reports honestly what it got as a result...

    In order to have a large remainder, I suppose the second operand of % must have been slightly over 0.1, preventing the final division, and resulting in almost the entire 0.1 being the result of the operation.

提交回复
热议问题