Why Does Modulus Division (%) Only Work With Integers

Why does modulus division (%) only work with integers?

Because the normal mathematical notion of "remainder" is only applicable to integer division. i.e. division that is required to generate integer quotient.

In order to extend the concept of "remainder" to real numbers you have to introduce a new kind of "hybrid" operation that would generate integer quotient for real operands. Core C language does not support such operation, but it is provided as a standard library fmod function, as well as remainder function in C99. (Note that these functions are not the same and have some peculiarities. In particular, they do not follow the rounding rules of integer division.)

How Does Modulus Divison Work

The result of a modulo division is the remainder of an integer division of the given numbers.

That means:

27 / 16 = 1, remainder 11
=> 27 mod 16 = 11

Other examples:

30 / 3 = 10, remainder 0
=> 30 mod 3 = 0

35 / 3 = 11, remainder 2
=> 35 mod 3 = 2

does modulus function is only applicable on integer data types?

You could declare your variables as int64_t or long long ; then they would compute the modulus in their range (e.g. 64 bits for int64_t). And it would work correctly only if all intermediate values fit in their range.

However, you probably want or need bignums. I suggest you to learn and use GMPlib for that.

BTW, don't use pow since it computes in floating point. Try i = n * n; instead of i = pow(n,2);

P.S. this is not for a beginner in C programming, using gmplib requires some fluency with C programming (and programming in general)

Understanding The Modulus Operator %

(This explanation is only for positive numbers since it depends on the language otherwise)

Definition

The Modulus is the remainder of the euclidean division of one number by another. % is called the modulo operation.

For instance, 9 divided by 4 equals 2 but it remains 1. Here, 9 / 4 = 2 and 9 % 4 = 1.

Euclidean Division

In your example: 5 divided by 7 gives 0 but it remains 5 (5 % 7 == 5).

Calculation

The modulo operation can be calculated using this equation:

a % b = a - floor(a / b) * b
  • floor(a / b) represents the number of times you can divide a by b
  • floor(a / b) * b is the amount that was successfully shared entirely
  • The total (a) minus what was shared equals the remainder of the division

Applied to the last example, this gives:

5 % 7 = 5 - floor(5 / 7) * 7 = 5

Modular Arithmetic

That said, your intuition was that it could be -2 and not 5. Actually, in modular arithmetic, -2 = 5 (mod 7) because it exists k in Z such that 7k - 2 = 5.

You may not have learned modular arithmetic, but you have probably used angles and know that -90° is the same as 270° because it is modulo 360. It's similar, it wraps! So take a circle, and say that its perimeter is 7. Then you read where is 5. And if you try with 10, it should be at 3 because 10 % 7 is 3.

can someone explain why the modulo function in this code isn't working?

While it is correct that fmod for floating point numbers, I am not sure why you want to consider coins to be a float in your code. The number of coins will always need to be an integer as you cannot have something like half a coin.

int main(void){
float dollars;
int cents;
int coins;

do{
printf("O hai! How much change is owed?");
dollars = get_float();
} while(dollars < 0);

cents = roundf(dollars * 100);

coins = cents / 25;
cents = cents % 25;

if (cents < 25){
coins += (cents / 10);
cents = cents % 10;
}

if (cents < 10){
coins += (cents / 5);
cents = cents % 5;
}

if (cents < 5){
coins += (cents / 1);
cents = cents % 1;
}

printf("%d\n", coins);
}

You can calculate the number of whole coins of each type by checking the denomination and decrementing the remainder while incrementing total coins accordingly.

Why do both % and fmod() exist in C

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.



Related Topics



Leave a reply



Submit