Why am I getting a different result from std::fmod and std::remainder

后端 未结 2 1577
春和景丽
春和景丽 2020-12-19 17:29

In the below example app I calculate the floating point remainder from dividing 953 by 0.1, using std::fmod

What I was expectin

2条回答
  •  有刺的猬
    2020-12-19 18:08

    Welcome to floating point math. Here's what happens: One tenth cannot be represented exactly in binary, just as one third cannot be represented exactly in decimal. As a result, the division produces a result slightly below 9530. The floor operation produces the integer 9529 instead of 9530. And then this leaves 0.1 left over.

提交回复
热议问题