C++ Floating Point Manipulation - GeeksforGeeks

10 Jan, 2022
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. These functions belong to the <cmath> header file. 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.
Syntax:
fmod(a,b);  // a, b are 2 floating point numbers
Example:
C++
// C++ code to demonstrate working of fmod()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){      double a, b, c;      // Initializing values    a = 9.6;    b = 3.5;      // using fmod() to compute the remainder    // computes 2 as quotient (truncated)    // returns 2.6 as remainder    c = fmod(a, b);      cout << "The remainder computed using fmod() is : "         << c;    cout << endl;}
The remainder computed using fmod() is : 2.6
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.
Syntax:
 remainder(a,b);  // a, b are 2 floating point numbers
Example:
C++
// C++ code to demonstrate working of remainder()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){      double a, b, c;      // Initializing values    a = 9.6;    b = 3.5;      // using remainder() to compute the remainder    // computes 3 as quotient (rounded)    // returns -0.9 as remainder    c = remainder(a, b);      cout << "The remainder computed using remainder() is : "         << c;    cout << endl;}
The remainder computed using remainder() is : -0.9
3. remquo(): This function returns the remainder and also stores the remainder in variable reference passed as argument. This function takes 3 arguments, numerator, denominator, and reference of the variable where the quotient has to be stored.
Syntax:
remquo(a,b,&quo); // a, b and c are three arguments
Example:
CPP
// C++ code to demonstrate working of remquo()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){      double a, b, f;    int g;      // Initializing values    a = 9.6;    b = 3.5;      // using remquo() to return quotient and remainder    // quotient stored in g    f = remquo(a, b, &g);      cout << "The remainder part of " << a << "/" << b         << " is : " << f;    cout << endl;    cout << "The quotient part of " << a << "/" << b         << " is : " << g;    cout << endl;}
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.
Syntax:
copysign(a, b);  // a and b are arguments
Example:
C++
// C++ code to demonstrate working of copysign()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b;      // Initializing values    a = 9.6;    b = -3.5;      // using copysign()    cout << "The value returned after copysigning is : ";    cout << copysign(a, b);      cout << endl;}
The value returned after copysigning is : -9.6
5. nextafter(): This function computes the next representable value of the 1st argument in the direction of the 2nd argument.
Syntax:
nextafter(a, b);  // a and b are arguments
Example:
CPP
// C++ code to demonstrate working of nextafter()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b;      // Initializing values      a = -3.5;    b = 0.0;      // using nextafter() to compute next approximated value    cout << "The next value approximated is : ";    cout << nextafter(a, b);}
The next value approximated is : -3.5
6. fmin(): Returns the smallest of two arguments.
Syntax:
 fmin(a, b);  // a and b are arguments
Example:
C++
// C++ code to demonstrate working of fmin()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b;      // initializing values    a = 2.5;    b = 2.0;      // using fmin() to compute smallest of two numbers    cout << "The smallest of 2 numbers is : ";    cout << fmin(a, b);      cout << endl;}
The smallest of 2 numbers is : 2
7. fmax(): Returns the largest of two arguments.
Syntax:
 fmax(a, b);  // a and b are arguments
Example:
C++
// C++ code to demonstrate working of fmax()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b;      // initializing values    a = 2.5;    b = 2.0;      // using fmax() to compute maximum of two numbers    cout << "The largest of 2 numbers is : ";    cout << fmax(a, b);      cout << endl;}
The largest of 2 numbers is : 2.5
8. fdim(): Returns the positive difference of the numbers passed as arguments.
Syntax:
fdim(a, b);  // a and b are arguments
Example:
C++
// C++ code to demonstrate working of fdim()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b;      // initializing values    a = 2.5;    b = 2.0;      // using fdim() to compute positive difference of two    // numbers    cout << "The positive difference of 2 numbers is : ";    cout << fdim(a, b);      cout << endl;}
The positive difference of 2 numbers is : 0.5
9. fma(): This function takes 3 arguments and returns the multiply-add “x*y+z” value after computing.
Syntax:
fma(a, b, c);  // a, b and c are arguments
Example:
CPP
// C++ code to demonstrate working of fma()#include <cmath>#include <iostream>using namespace std;  // Driver Codeint main(){    double a, b, c;      // initializing values    a = 2.5;    b = 2.0;    c = 2.5;      // using fma() to compute multiply-add    cout << "The multiply-add of 3 numbers is : ";    cout << fma(a, b, c);}
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 write.geeksforgeeks.org or mail your article to review-team@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.
anshikajain26
CPP-Library
cpp-math
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Bitwise Operators in C/C++
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples
Operator Overloading in C++
Socket Programming in C/C++
vector erase() and clear() in C++