# Floating-point modulus

• 10-01-2002
Sang-drax
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)
• 10-01-2002
ygfperson
i think there's a c function that does it... fmod, maybe?
• 10-01-2002
moi
fmod() is available in c and c++
• 10-01-2002
skipper
Code:

```#include [itex] #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