# Simple mod function won't work

This is a discussion on Simple mod function won't work within the C Programming forums, part of the General Programming Boards category; I'm studying K&amp;R. I'm on exercise 4.3. Part of the exercise requires me to calculate the remainder of two double ...

1. ## Simple mod function won't work

I'm studying K&R. I'm on exercise 4.3. Part of the exercise requires me to calculate the remainder of two double types.

The following code isn't working for me, though:

Code:
double get_mod(double num, double den)
{
double quotient;
quotient = num/den;
return (num - quotient * den); //this math should produce the remainder
}
I just copied some math that I seen online somewhere to make this function. If I try to calculate 10.0 % 3.0, it returns 0.0. I don't understand why.

The K&R answer book suggests that I use the function fmod(), which returns the mod of two double types. I would like to figure it out myself, though.

Could someone point me in the right direction? Thanks.

2. Substitute
quotient = num/den;

in
return (num - quotient * den); //this math should produce the remainder

and you get
return (num - num/den * den); //this math should produce the remainder

otherwise known as num - num
or put another way, 0

3. I just copied some math that I seen online somewhere to make this function.
Sort of off-topic, but why? Why not think about how the remainder of a division is calculated and do it yourself?

In any event, quotient is going to be the actual quotient, including the decimal part. You can lop off the decimal part in the remainder calculation by casting it to an int.

4. Originally Posted by billybob2020
I just copied some math that I seen online somewhere to make this function.
That is not how programming works... that is not how programmers work ... and it most certainly is not how you learn to be a programmer.

The art and the craft of programming is all about analysing and solving problems. The major purpose of the exercises in the many different C textbooks is to help you develop the analytical skills needed to solve real world problems and apply your solution in C syntax.

Trust me, you aren't doing yourself any favours messing about with scoop and poop coding... and especially not while still mid way through the textbook!

If I was your teacher I'd make you do that chapter over again!

5. What is the quotient you are getting? Does it have a faction part? If it does then it is NOT what you want.
You need to look up the floor() function or right it yourself to write your own fmod function.

Tim S.

6. Go through an example...
7.0 / 2.0 equals 3.5 (not 3)
7.0 - 3.5 * 2.0 = 0
The trick is that wou want the answer from that first line to give 3.0 and not 3.5 because then...
7.0 - 3.0 * 2.0 = 1.0
So, do you know what kind of function throws away the fractional part of the result?

7. ## Experimenting

I tried to experiment by doing the following, which truncates the quotient before continuing with the operation:

Code:
int main()
{
double num = 10.3;
double den = 3.3;
double mod;
int trun;
trun = num/den;
mod = num - trun*den;
}
The mod becomes 0.40000000000000213. Is this math even correct?

For some reason, my variables are showing up in my Visual Studio Autos window like this:

num 10.300000000000001
den 3.2999999999999998

I'm not sure why.
------------------------------------------------------------------------------

Using the same variable values, but instead using the fmod function for the calculation gives me a slightly different result, which is the following code block:

Code:
#include <math.h>
int main()
{
double num = 10.3;
double den = 3.3;
double mod;
mod = fmod(num,den);
}
The mod result here is 0.40000000000000124, which is slightly different.

I understand the basics of dividing decimals, but I dont know how to get the remainder on paper, so I don't understand how the mod becomes .4 to begin with.

If someone wants to explain to me how to find the remainder of an operation where decimals are divided, I won't mind.

I really want to understand how the fmod function works, though.

8. Originally Posted by billybob2020
The mod becomes 0.40000000000000213. Is this math even correct?
Work it out on paper.
Originally Posted by billybob2020
For some reason, my variables are showing up in my Visual Studio Autos window like this:
...
Using the same variable values, but instead using the fmod function for the calculation gives me a slightly different result
If you want accuracy, don't use floating point numbers.

Quzah.