Floating Point Arithmetic - Modulo Operator on Double Type

后端 未结 4 2098
野性不改
野性不改 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:21

    The error you see is small; it only looks large at first glance. Your result (after rounding for display) was 0.09999999999999995 == (0.1 - 5e-17) when you expected 0 from a % 0.1 operation. But remember that this is almost 0.1, and 0.1 % 0.1 == 0.

    So your actual error here is -5e-17. I would call this small.

    Depending on what you need the number for, it might be better to write:

    double result = 1.0 % 0.1; result = result >= 0.1/2 ? result - 0.1 : result;

提交回复
热议问题