I took a quiz in my CS class today and got a question about the modulo operator wrong because I didn't know about the availability of % in C, I've been using fmod()
. Why do both exist? Is one better/faster or do they just deal with different data types?

- 113
- 2
- 7
-
3The operator % may be applied only for integers in C. – Vlad from Moscow Jan 25 '17 at 17:24
-
5duplicate of http://stackoverflow.com/questions/11243632/versus-fmod-for-calculating-modulus – MK. Jan 25 '17 at 17:24
-
1Possible duplicate of [% versus FMOD for calculating modulus](http://stackoverflow.com/questions/11243632/versus-fmod-for-calculating-modulus) – Dellowar Jan 25 '17 at 17:37
4 Answers
modulo division
using %
operator in C only works for integer operands and returns an integer remainder of the division.
The function fmod
accepts double
as arguments meaning that it accepts non-integer values and returns the remainder of the division.
Additional note on fmod: how is the remainder calculated in case of double
operand? Thanks @chux for showing the documentation on how fmod calculates the remainder of a floating point division.
The floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where n is x/y with its fractional part truncated.
The returned value has the same sign as x and is less or equal to y in magnitude.
On the other hand, when the modulo division binary operator (%) was first designed, it was determined by the language designers that it would only support operands of 'integer' types because technically speaking, the notion of 'remainder' in mathematics only applies to integer divisions.

- 9,534
- 3
- 19
- 43
-
@chux, thanks for pointing it out. I have edited my answer to fix that additional note. – VHS Jan 25 '17 at 19:04
-
1New explanation also has holes. Suggest "The `fmod` functions return the value `x` − n`y`, for some integer n such that, if `y` is nonzero, the result has the same sign as `x` and magnitude less than the magnitude of `y`...." C11 §7.12.10.1 3 – chux - Reinstate Monica Jan 25 '17 at 19:12
-
Thanks @chux for showing the documentation on how fmod calculates the remainder of a floating point division. I have updated my answer yet again to revise the "additional note". – VHS Jan 25 '17 at 19:23
-
Hmmm, interesting. Your link says "_less or equal_ to y in magnitude." and the C spec says "_less_ than the magnitude of y." Seems like the _equal_ part is questionable, as a zero could be returned rather than a value equal to `y`. – chux - Reinstate Monica Jan 25 '17 at 19:38
-
@chux, I would agree to your comment above. In Mathematics, a remainder is always less than the divisor. It cannot be equal to a divisor. – VHS Jan 25 '17 at 19:55
It's because %
is an integer operator, and fmod
stands for floatmod
and is used for floating point numbers.

- 15,586
- 12
- 56
- 91
Why do both exist?
Because they may have computed different results, even with the same values. These differences may occur with negative values. In essence fmod()
and %
were different mathematical functions.
fmod(x,y)
, since C89, had the result "the result has the same sign as x and magnitude less than the magnitude of y".
i%j
was not so singularly defined. See Remainder calculation for the modulo operation. This allow code to use existing variant processors effectively. The div()
function was created to address this variability. Ref
By C99 they compute the same for the same values. Future C could allow 123.4 % 56.7

- 1
- 1

- 143,097
- 13
- 135
- 256
% is just integer modulo
fmod is float modulo and can be used as described in MSDN. https://msdn.microsoft.com/en-us/library/20dckbeh.aspx

- 1,006
- 7
- 25
-
1`a%b` may differ from _integer modulo_ expectations when `a` or `b` are negative. [What's the difference between “mod” and “remainder”?](https://stackoverflow.com/q/13683563/2410359). Like-wise for `double fmod(double)`. – chux - Reinstate Monica Jan 28 '20 at 16:52