My goal is to check if there is any remainder left when dividing 2 floats, and if there is, give that remainder back to the user.
Given the following code, I had expected that fmod(2, 0.2)
would be 0, however, I get back 0.2
. I read that this has to do with floating point problems. But is there any way this can be done properly?
int main() {
float a = 2.0;
float b = 0.2;
float rem = fmod(a, b);
if (rem > 0) {
std::cout << "There is a remainder: " << rem << std::endl;
} else {
std::cout << "No remainder: " << rem << std::endl;
}
}
Output:
There is a remainder: 0.2