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 integeri
such that, ify
is non-zero, the result has the same sign asx
and 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
x
ory
isNaN
, aNaN
shall be returnedIf
y
is zero, a domain error shall occur, and either aNaN
(if supported), or an implementation-defined value shall be returned.If
x
is infinite, a domain error shall occur, and either aNaN
(if supported), or an implementation-defined value shall be returned.If
x
is±0
andy
is not zero,±0
shall be returned.If
x
is not infinite and y is±Inf
,x
shall 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
, wheren
is the quotient ofnumerator
divided 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