I dug into old code, and saw a function like this:
inline double mod(double x, double y)
{
    return x-y*floor(x/y);
}
is fmod its full equivalent, or have I missed something?
No, the above routine it is not the same as fmod(). Specifically it is different for situations where one argument is negative.
Your routine does a floor(), which rounds down to the next integer. With fmod() the rounding is like trunc(), i.e. towards zero.
Here's an extract from the Open Group standard (here):
These functions shall return the value
x- i* y, for some integerisuch that, ifyis non-zero, the result has the same sign asxand magnitude less than the magnitude ofy.If the correct value would cause underflow, and is not representable, a range error may occur, and either 0.0 (if supported), or an implementation-defined value shall be returned.
If
xoryisNaN, aNaNshall be returnedIf
yis zero, a domain error shall occur, and either aNaN(if supported), or an implementation-defined value shall be returned.If
xis infinite, a domain error shall occur, and either aNaN(if supported), or an implementation-defined value shall be returned.If
xis±0andyis not zero,±0shall be returned.If
xis not infinite and y is±Inf,xshall be returned.If the correct value would cause underflow, and is representable, a range error may occur and the correct value shall be returned.
That's difficult to understand, but the word 'magnitude' in the first paragraph illustrates the rounding is towards zero.
Here's an extract from the much more helpful documentation for the GCC library:
These functions compute the remainder from the division of numerator by denominator. Specifically, the return value is
numerator-n*denominator, wherenis the quotient ofnumeratordivided bydenominator, rounded towards zero to an integer. Thus,fmod(6.5, 2.3) returns 1.9, which is 6.5 minus 4.6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With