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++
// C++ code to demonstrate working of fmod()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of remainder()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of remquo()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of copysign()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of nextafter()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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);
}
OutputThe 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++
// C++ code to demonstrate working of fmin()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of fmax()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of fdim()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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;
}
OutputThe 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++
// C++ code to demonstrate working of fma()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int 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);
}
OutputThe 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).
Similar Reads
ios manipulators showpoint() function in C++
The showpoint() method of stream manipulators in C++ is used to set the showpoint format flag for the specified str stream. This flag always displays the floating point values along with their decimal values. Syntax: ios_base& showpoint (ios_base& str) Parameters: This method accepts str as
1 min read
ios manipulators noshowpoint() function in C++
The noshowpoint() method of stream manipulators in C++ is used to clear the showpoint format flag for the specified str stream. This flag displays the floating values in decimal only when they have a decimal values.Syntax: ios_base& noshowpoint (ios_base& str) Parameters: This method accepts
1 min read
Can we use % operator on floating point numbers?
Predict the output of following program: Can % be used with floating point numbers in C++? CPP #include <iostream> int main() { float f = 9.9f, m = 3.3f; float c = f % m; // LINE 5 std::cout << c; return 0; } The above program fails in compilation and compiler report the following error
1 min read
ios manipulators right() function in C++
The right() method of stream manipulators in C++ is used to set the adjustfield format flag for the specified str stream. This flag sets the adjustfield to right. It means that the number in the output will be padded to the field width by inserting fill characters at the start. Syntax: ios_base&
1 min read
ios manipulators scientific() function in C++
The scientific() method of stream manipulators in C++ is used to set the floatfield format flag for the specified str stream. This flag sets the floatfield to scientific. It means that, the floating point values will be written in scientific notations. Syntax: ios_base& scientific (ios_base&
2 min read
fma() function in C++
The fma() function takes three arguments a, b and c, and returns a*b+c without losing precision. The fma() function is defined in the cmath header file. If any argument passed to fma() is long double, the return type is long double. If not, the return type is double. Syntax: double fma(double a, dou
2 min read
feupdateenv() function in C++
The feupdateenv() function in C++ first saves currently raised floating-point exceptions. It restores the floating-point environment from the given fenv_t object and then raises the exceptions which were saved previously.Syntax: int feupdateenv( fenv_t* envp ) Parameters: It accepts a single mandato
2 min read
Floating Point Operations & Associativity in C, C++ and Java
Do Floating point operations follow property of associativity? In other words, do we always get the same results for expressions "(A + B) + C" and "A + (B + C)" One may expect that floating numbers to follow the rule of associativity in programming languages as they are associative mathematically. H
4 min read
ilogb() function in C++ STL
The ilogb(x) function in C++ STL returns the integral part of the logarithm of |x|, by using the FLT_RADIX as base for the logarithm. In general, the value of FLT_RADIX is 2, so ilogb() is equivalent to ilog2()(for positive values only). Syntax: ilogb(x) Parameter: The function accepts a single mand
2 min read
clock() function in C
The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
2 min read