C++ Floating Point Manipulation (fmod(), remainder(), remquo() … in cmath)

Like integers, C++11 introduced some basic inbuilt functions for handling simple mathematical computations of floating point numbers necessary for day to day programming as well as competitive programming. This article discusses some of the functions introduced.

1. fmod() : This function is used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments. The quotient computed is truncated.

2. remainder() : This function is also used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments.The quotient computed is rounded.

3. remquo() : This function returns the remainder and also stored remainder in variable reference passed as argument. This function takes 3 arguments, numerator, denominator and reference of variable where quotient has to be stored.

 // C++ code to demonstrate working of // fmod(), remainder() and remquo() #include #include using namespace std;    int main() {           double a, b, c, d, f;    int g;          // initializing values    a = 9.6;    b = 3.5;          // using fmod() to compute the remainder    // computes 2 as quotient (truncated)    // returns 2.6 as remainder    d = fmod(a,b);          // using remainder() to compute the remainder    // computes 3 as quotient (rounded)    // returns -0.9 as remainder    c = remainder(a,b);          // using remquo() to return quotient and remainder    // quotient stored in g    f = remquo(a,b,&g);          cout << "The remainder computed using fmod() is : " <

Output:

The remainder computed using fmod() is : 2.6
The remainder computed using remainder() is : -0.9
The remainder part of 9.6/3.5 is : -0.9
The quotient part of 9.6/3.5 is : 3

4. copysign() : This function returns a number with the magnitude to 1st argument and sign of 2nd argument.

5. nextafter() : This function computes the next representable value of 1st argument in the direction of 2nd argument.

 // C++ code to demonstrate working of // nextafter() and copysign() #include #include using namespace std;    int main()  {        double a, b, c;          // initializing values    a = 9.6;    b = -3.5;    c = 0.0;          // using copysign()    cout << "The value returned after copysigning is : ";    cout << copysign(a,b);          cout << endl;          // using nextafter() to compute next approximated value     cout << "The next value approximated is : ";    cout << nextafter(c,b); }

Output:

The value returned after copysigning is : -9.6
The next value approximated is : -4.94066e-324

6. fmin() : Returns the smallest of two arguments.

7. fmax() : Returns the largest of two arguments.

8. fdim() : Returns the positive difference of the numbers passed as arguments.

9. fma() : This function takes 3 arguments and returns multiply-addx*y+z” value after computing.

 // C++ code to demonstrate working of // fmin(), fmax(), fdim(), fma() #include #include using namespace std;    int main()  {        double a, b, c;          // initializing values    a = 2.5;    b = 2.0;    c = 2.5;          // using fmax() to compute maximum of two numbers    cout << "The largest of 2 numbers is : ";    cout << fmax(a,b);          cout << endl;          // using fmin() to compute smallest of two numbers    cout << "The smallest of 2 numbers is : ";    cout << fmin(a,b);          cout << endl;          // using fdim() to compute positive difference of two numbers    cout << "The positive difference of 2 numbers is : ";    cout << fdim(a,b);          cout <<  endl;          // using fma() to compute multiply-add    cout << "The multiply-add of 3 numbers is : ";    cout << fma(a,b,c);    }

Output:

The largest of 2 numbers is : 2.5
The smallest of 2 numbers is : 2
The positive difference of 2 numbers is : 0.5
The multiply-add of 3 numbers is : 7.5

This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.