Why Modulus Operator is not used with float type values in C & C++ [SOLUTION]


After a long time, I'm writing this post, actually one of mine friend asked a question to me that, why c language compiler gives an error while compiling a program having use of modulus operator with float (real) type values. So
from there I thought about writing this post.

Firstly I should tell you the use of modulus operator (%) in programming languages. Each programming language has same usage of modulus operator and in C++ this operator is also called as remainder operator.

Modulus Operator (%) is a binary operator that’s why used with two operands. This operator works similar like division operator but division operator gives quotient and in Modulus operator the output is remainder after dividing two operands on which modulus operator is applied.

Example: -

After seeing example you have found that modulus operator gives remainder of operands after division of them. But this example shows use of modulus operator with integer type values.

When we use float or real type values with this modulus operator compiler will gives error like this.

In this image you will find that compiler says this modulus operator (%) is invalid for this type values i.e. float type values.
The question arises here why float type values are not allowed for modulus operator in c & c++.

For this you should remind the basics of mathematics and programming. When we use integer type values and divide them we will get or return quotient of division by (/) division operator and the remainder value is discarded by operator because the work of division operator is to give quotient from division operation.

Similarly, when we use modulus operator (%) we know the output should be the remainder and if we use integer value modulus operator will do division operation and the remainder value will gives as output (as like in division operation this remainder value is discarded by division operator), but in the case of floating type values for operands in modulus operator when division operation is applied by modulus operator the remainder doesn’t comes out because for floating type values division operation floating (decimal) values comes out (such as: 1.4, 2.344, etc.) and remainder value will always be void for floating type values with modulus operator.

And, that’s why at the time of C99 version another function is given by makers for modulus operation with floating type values i.e. fmod() which is used in C & C++ when float or real type values are used for modulus operations.

This fmod() function used hybrid approach to solve this issue i.e. internally rounding off the real type operands to integer type quotients and finally gives remainder of floating type values in C & C++.

If you found any mistakes or have suggestions and new info please let me know in comments below.

You Might Also Like


  1. Sir if we divide 3.0 and 7, then it will never completely gives remainder 0 ......So explain me about these type of cases..

    1. If we divide 3.0 by 7 then you'll see it's not dividing so it'll always give you the term as answer(remainder).

  2. Sir if we divide 3.0 and 7, then it will never completely gives remainder 0 ......So explain me about these type of cases..

  3. Sir can you give a example by the use of fmod()


If you have any questions or suggestions, you are free to ask, i will appreciate that and, i will try my best...

Check Out