Thread: Floating-point modulus

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Floating-point modulus

    Is there a C++ function that allows calculation of floating point modulus?

    I don't think there is, so I ask: Is there a faster way to calculate modulus than my example here?


    Code:
    double modulus(double left, double right)
    {
        double quota = left / right;
        double frac,tmp;
        frac = std::modf(quota,&tmp);
        //frac contains what's beyond the '.'
        frac *= right;
        return frac;
    }
    (edit: spelling)
    Last edited by Sang-drax; 10-01-2002 at 05:55 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i think there's a c function that does it... fmod, maybe?

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    fmod() is available in c and c++
    hello, internet!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Code:
    #include <math>
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    void pause(){system("PAUSE");}
    
    int main(void)
    {
       double x = 5.0, y = 2.0;
       double result;
       result = fmod(x, y);
       std::cout << std::showpoint; // display decimal point and trailing zeroes
       std::cout << "The remainder of ( "<< x << " / " << y << " ) = "
                 << result << "\n\n";
       pause();
       return 0;
    }
    A little borrowing here from Borland, and Prelude, with my own conversion from C to C++. (Borland users will know what I'm talking about. )

    The reference to Prelude? The "system" call - which no one can agree on except that it's not portable.

    Looking past the "expense" of a system call, the "definition" is all that needs to be modified in order for the code to work on a different platform. Trivial here, but add numerous such calls and you can see what her point was.

    (Actually, her point was writing code that's portable from the standpoint of quality programming regardless of the number of platforms that it may run on - even if it's only one. Students? )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM