c++ fmod returns 0.2 for fmod(0.6,0.2)

后端 未结 3 635
执念已碎
执念已碎 2021-01-23 06:00

when I use fmod(0.6,0.2) in c++ it returns 0.2

I know this is caused by floating point accuracy but it seems I have to get remainder of two double this moment

th

3条回答
  •  长发绾君心
    2021-01-23 06:26

    I think your best bet here would be to use the remainder function instead of fmod. For the given example, it will return a very small number rather than 0.2. You can use this fact to assume a remainder of 0 by rounding to a certain level of precision.

提交回复
热议问题