Check if a double is evenly divisible by another double in C?

前端 未结 7 2087
故里飘歌
故里飘歌 2020-12-10 02:41

How can I check if a double x is evenly divisible by another double y in C? With integers I would just use modulo, but what would be the correct/best way to do it with doubl

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 03:23

    How can I check if a double x is evenly dividable by another double y in C? With integers I would just use modulo, but what would be the correct/best way to do it with doubles?

    You would include and link to the math library:

    #include

    Then you would call the floating point modulus function fmod:

    if (fmod(5.0, 2.5) == 0.0)
      // evenly divisible
    else
      // not evenly divisible
    

    You may want to compare the result of fmod with a small value instead of 0.0 depending on your needs.

提交回复
热议问题