C++ Floating Point Manipulation

C++Server Side ProgrammingProgramming

Numerical implementation of a decimal number is a float point number. In C++ programming language the size of a float is 32 bits. And there are some floating point manipulation functions that work on floating-point numbers. Here we have introduced some of the floating-point manipulation functions.

fmod()

The fmod() function operating on floats will return the remainder of the division of the passed arguments of the method.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float a, b, rem;
   a = 23.4;
   b = 4.1;
   rem = fmod(a,b);
   cout<<"The value of fmod( "<<a<<" , "<<b<<" ) = "<<rem;
}

Output

The value of fmod( 23.4 , 4.1 ) = 2.9

remainder()

The remainder() function does the same work as fmod function. And return the remainder of the division between the values. This method returns the minimum possible remainder in terms of numeric values. It may negative also.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float a, b, rem;
   a = 23.4;
   b = 4.1;
   rem = remainder(a,b);
   cout<<"The value of remainder( "<<a<<" , "<<b<<" ) = "<<rem;
}

Output

The value of remainder( 23.4 , 4.1 ) = -1.2

remquo()

This method returns the quotient and remainder of the two values passed also it needs a reference to a variable that will have the value of the quotient. So, this method will return the remainder the same as by the remainder function and reference of the quotient.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float a, b, rem;
   int quo;
   a = 23.4;
   b = 4.1;
   rem = remquo(a,b,&quo);
   cout<<a<<" and "<<b<<" passed to the remquo() function gives the following output\n";
   cout<<"The remainder is "<<rem<<endl;
   cout<<"The quotient is "<<quo;
}

Output

23.4 and 4.1 pass to the the remque() function gives the following
output
The reminder is -1.2
The quotient is 6

copysign()

The copysign function of C returns a variable with the sign of other variables. The returned variables have the magnitude of the first variable and sign of the second variable.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main(){
   double a, b;
   a = 9.6;
   b = -3.5;
   cout<<"copysign function with inputs "<<a<<" and "<<b<<" is "<<copysign(a,b);
}

Output

Copysign function with inputs 9.6 and -3.5 is -9.6

fmin()

The fmin function as you can see from its name returns that the minimum value of the two arguments of the function. The return type is a float.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main(){
   double a, b;
   a = 43.5;
   b = 21.2;
   cout << "The smallest of "<<a<<" and "<<b<<" is "; cout << fmin(a,b)<<endl;
}

Output

The smallest of 43.5 and 21.2 is 21.2

fmax()

The fmax function is a C programming function that returns the largest number of the two numbers in the arguments.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main(){
   double a, b;
   a = 43.5;
   b = 21.2;
   cout << "The largest of "<<a<<" and "<<b<<" is "; cout << fmax(a,b)<<endl;
}

Output

The largest of 43.5 and 21.2 is 43.5

fdim()

The fdim() function of the C programming language returns the absolute difference of the two numbers that are sent as the arguments to the function.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main(){
   double a, b;
   a = 43.5;
   b = 21.2;
   cout << "The absolute difference of "<<a<<" and "<<b<<" is";
   cout << fdim(a,b)<<endl;
}

Output

The absolute difference of 43.5 and 21.2 is 22.3

fma()

The fma() function of C, returns the multiplication of the arguments given to it. The function returns a float and accepts three floating arguments.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main(){
   double a, b, c;
   a = 3.5;
   b = 2.4;
   c = 7.2;
   cout << "The multiplication of "<<a<<" , "<<b<<" and "<<c<<" is ";
   cout << fma(a,b,c)<<endl;
}

Output

The multiplication of 3.5 , 2.4 and 7.2 is 15.6

These are all the functions that are operated over floating-point numbers. These are the functions that are defined in cmath library

raja
Published on 19-Sep-2019 09:12:43
Advertisements