Floating-point modulus

This is a discussion on Floating-point modulus within the C++ Programming forums, part of the General Programming Boards category; Is there a C++ function that allows calculation of floating point modulus? I don't think there is, so I ask: ...

  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 06: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,493
    i think there's a c function that does it... fmod, maybe?

  3. #3
    moi
    moi is offline
    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, 09:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 11:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 11:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21