Search code examples
c++integerstdroundingfmodf

fmod telling me fractional part of 1 is 1


I'm trying to check if a double variable p is approximately equal to an integer. At some point in my code I have

double ip;
cout << setprecision(15) << abs(p) << " " << modf(abs(p), &ip) << endl;

And for a given run I get the printout

1 1

This seems to say that the fractional part of 1 is 1, am I missing something here or could there be some roundoff problem etc?

Note: I'm not including the whole code since the origin of p is complicated and I'm just asking if this is a familiar issue


Solution

  • could there be some roundoff problem etc?

    There certainly could. If the value is very slightly less than 1, then both its value and its fractional part could be rounded to 1 when displayed.

    the origin of p is complicated

    Then it's very likely not to be an exact round number.