Thread: Modular Division problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    Modular Division problem

    I was working with floating point numbers and integers, and I came across a case where I needed just the part of the floating point number that comes after the decimal. So for example, if I had

    Code:
    float x = 3.5;
    what I wanted was just the ".5" part of x, not the 3 in front of it. So I decided to use modular division to get it, like this:

    Code:
    float x = 3.5;
    float partOfX = x % 1;
    So what that would do would divide 3.5 by 1, giving me a remainder of .5, and storing that into partOfX. Yet the compiler for some reason does not allow me to perform modular division on floating point numbers. I tried casting things every which way, got nothing useful. Apparently you can only use modular division with integers.

    So to fix this I just I did some casting to integer and subtraction to get the .5 that I wanted. But I thought it was weird that I couldn't perform modular division on floating point numbers. The calculator program that comes with Windows lets me but I guess Visual C++ 5.0 does not. Perhaps all C/C++ compilers don't.

    Anyways, anyone know if its all C/C++ compilers that don't allow modular division with floating point numbers? Or is my compiler just goofy?
    -Grunt (Malek)

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Nope, the modulus operator is only defined for the various integer types.

    I suppose you could overload it for floating-point types if you wanted.

  3. #3
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    you could iterate it something fierce:
    Code:
    while (num >= 1 || num <= -1)
        num -= num / fabs(num);
    i'm pretty sure that would work
    Illusion and reality become impartiality and confidence.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    double Number = 34.69823;
    int Temp = (int)Number;
    double Answer = Number - (double)Temp;
    cout << Answer << endl;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    In Java you can take the modulus of a floating point number. With out using any "tricks"
    Mr. C: Author and Instructor

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Mister C
    In Java you can take the modulus of a floating point number. With out using any "tricks"
    How does this help? This is a C/C++ forum, not Java
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    #include <cmath> and use fmod( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The reason (presumably) that the modulus operator was not implemented for floating point types in C++ is that it is a relation in number theory dealing with the divisibility of integers. Since divisibility has no useful meaning if real or rational numbers are considered, it is not defined for them.

    After all that, however, Knuth has a pretty good definition of the modulus operator for all real numbers x, and y:

    x mod y =
    x - y * [x / y] -- if y != 0
    x -- if y = 0

    Where [a] is the least integer function.

    Alright, probably more than anyone cared to hear, sorry about that.

    Cheers
    Last edited by Zach L.; 05-24-2003 at 06:11 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Math Division Problem
    By cookie in forum C Programming
    Replies: 7
    Last Post: 06-14-2007, 10:34 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with division in C!!!
    By g_p in forum C Programming
    Replies: 15
    Last Post: 04-19-2007, 02:45 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM