Thread: Simple mod function won't work

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by billybob2020 View Post
    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. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Unhappy 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.
    Last edited by cb0ardpr0gr^mm3r; 06-18-2011 at 06:59 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by billybob2020 View Post
    The mod becomes 0.40000000000000213. Is this math even correct?
    Work it out on paper.
    Quote Originally Posted by billybob2020 View Post
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is my work correct?...simple function
    By enjoyincubus in forum C Programming
    Replies: 12
    Last Post: 11-11-2006, 09:32 AM
  2. Simple C++ Won't Work...
    By jothesmo in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2005, 07:00 PM
  3. Replies: 1
    Last Post: 04-02-2003, 07:50 PM
  4. My function seems too simple to work..
    By face_master in forum C++ Programming
    Replies: 10
    Last Post: 05-27-2002, 02:58 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM