C++ Floating Point Manipulation
Last Updated :
24 May, 2023
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()
The fmod() function is used to return the remainder(modulus) of 2 floating-point numbers mentioned in its arguments. The quotient computed is truncated.
Syntax
double fmod( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b, c;
a = 9.6;
b = 3.5;
c = fmod (a, b);
cout << "The remainder computed using fmod() is : "
<< c;
cout << endl;
}
|
Output
The remainder computed using fmod() is : 2.6
Time Complexity: O(1)
Space Complexity: O(1)
2. remainder()
The remainder() function is also used to return the remainder(modulus) of 2 floating-point numbers mentioned in its arguments. The quotient computed is rounded.
Syntax
double remainder( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b, c;
a = 9.6;
b = 3.5;
c = remainder(a, b);
cout << "The remainder computed using remainder() is : "
<< c;
cout << endl;
}
|
Output
The remainder computed using remainder() is : -0.9
Time Complexity: O(1)
Space Complexity: O(1)
3. remquo()
The remquo() function returns the remainder and also stores the remainder in variable reference passed as an argument. This function takes 3 arguments, numerator, denominator, and reference of the variable where the quotient has to be stored.
Syntax
double remquo( double x, double y, int *quo );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b, f;
int g;
a = 9.6;
b = 3.5;
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;
}
|
Output
The remainder part of 9.6/3.5 is : -0.9
The quotient part of 9.6/3.5 is : 3
Time Complexity: O(1)
Space Complexity: O(1)
4. copysign()
The copysign() function returns a number with the magnitude of the 1st argument and sign of the 2nd argument.
Syntax
double copysign( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b;
a = 9.6;
b = -3.5;
cout << "The value returned after copysigning is : " ;
cout << copysign(a, b);
cout << endl;
}
|
Output
The value returned after copysigning is : -9.6
Time Complexity: O(1)
Space Complexity: O(1)
5. nextafter()
The nextafter() function computes the next representable value of the 1st argument in the direction of the 2nd argument.
Syntax
double nextafter( double from, double to );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b;
a = -3.5;
b = 0.0;
cout << "The next value approximated is : " ;
cout << nextafter(a, b);
}
|
Output
The next value approximated is : -3.5
Time Complexity: O(1)
Space Complexity: O(1)
6. fmin()
The fmin() function returns the smallest of two arguments.
Syntax
double fmin( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b;
a = 2.5;
b = 2.0;
cout << "The smallest of 2 numbers is : " ;
cout << fmin(a, b);
cout << endl;
}
|
Output
The smallest of 2 numbers is : 2
Time Complexity: O(1)
Space Complexity: O(1)
7. fmax()
The fmax() function returns the largest of two arguments.
Syntax
double fmax( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b;
a = 2.5;
b = 2.0;
cout << "The largest of 2 numbers is : " ;
cout << fmax(a, b);
cout << endl;
}
|
Output
The largest of 2 numbers is : 2.5
Time Complexity: O(1)
Space Complexity: O(1)
8. fdim()
The fdim() function returns the positive difference of the numbers passed as arguments.
Syntax
double fdim( double x, double y );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b;
a = 2.5;
b = 2.0;
cout << "The positive difference of 2 numbers is : " ;
cout << fdim(a, b);
cout << endl;
}
|
Output
The positive difference of 2 numbers is : 0.5
Time Complexity: O(1)
Space Complexity: O(1)
9. fma()
The fma() function takes 3 arguments and returns the multiply-add “x*y+z” value after computing.
Syntax
double fma( double x, double y, double z );
Example
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a, b, c;
a = 2.5;
b = 2.0;
c = 2.5;
cout << "The multiply-add of 3 numbers is : " ;
cout << fma(a, b, c);
}
|
Output
The multiply-add of 3 numbers is : 7.5
Time Complexity: O(1)
Space Complexity: O(1)
This article is contributed by Manjeet Singh(S. Nandini).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...