# C++ Floating Point Manipulation

• Difficulty Level : Basic
• Last Updated : 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 ``#include ``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`

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 ``#include ``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`

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 ``#include ``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```

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 ``#include ``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`

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 ``#include ``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`

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 ``#include ``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`

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 ``#include ``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`

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 ``#include ``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`

9. fma(): This function takes 3 arguments and returns the multiply-addx*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 ``#include ``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`