Open In App

C++ Floating Point Manipulation

Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


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++




// 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;
}


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++




// 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;
}


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++




// 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;
}


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++




// 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);
}


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++




// 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;
}


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++




// 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;
}


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++




// 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;
}


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-addx*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);
}


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).



Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads