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
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.