fabs (data-type value); is the syntax of the fabs function. The outer data type must match the inner data type, which means when we declare double, the parameter should be double as same with integer and float data types. If you want to find the absolute value of an integer or float data-type value, you can openly change the value into a double data type. There is another function with the same functionality, abs(), but the difference is that abs() return only integer data-type values. In contrast, the fabs() function is used for integer, double, and float data-type values.
Fabs() With Integer:
The integer data-type parameter of a fabs() function is the simplest function type of the abs() function.
#include <cmath>
using namespace std;
int main() {
int a = -54;
int rs;
rs = fabs(a);
cout << "fabs(" << a << ") = " << rs;
return 0;
}
In this example, we add the libraries with the name of <iostream> that have the definition of the input-output stream also <cmatch> that contain the definition of the fabs function. Next, we add a namespace, and our main function starts. Here we take a variable with the data type of integer and assign the value of 54 with the negative sign to ensure that it must return a positive value when we put it into the fabs() function. Then we take another variable again with the data-type of integer to save the returned value from the function. After that, we call the function, assign it to the ‘rs’ variable, and print the result in the next line.
Fabs() With Float:
The function fabsf () means function having absolute value with the data-type of float thus it is a single-precision version of fabs function. The function fabsf () means function having absolute value with the data-type of float thus it is a single-precision version of fabs function.
#include <cmath>
using namespace std;
int main() {
float n = -16;
float result;
result = fabsf(n);
cout << "fabsf(" << n << ") = |" << n << "| = " << result;
return 0;
}
In the above instance, we integrate two libraries #include <iostream> and #include <cmath> that were explained above, so we move ahead. After adding using namespace std, we add the main function. Here we initialized two variables with the data type of float, and we assigned a value to one of the variables; the other is spare for the result that we get from the fabs() function. Then we call the function with the parameters of value that we assigned as above and save the result to another floating type variable and show it in the next cout statement.
Fabs() with long:
The syntax of the fabs() is fabsl(). Because this function contains the long data-type value in its parameters and we also pass this value to a value that has data-type long.
#include <cmath>
using namespace std;
int main() {
long a = -30;
long b;
b = fabsl(a);
cout << "fabsl(" << a << ") = " << b;
return 0;
}
Just Include header files and standard input and start the main body of the code where you take two variables with the data type of long and assign a value to one variable. Another variable stored the value when the function passed to it as the parameter we put as an argument. After printing the value, we terminate the code.
Fabs() Return Values:
The fab () function will return us a +0 value if we pass the argument with the value of either negative or positive value of 0. The fab() function will return us a +infinity value if we pass the argument with the value of either negative or positive value of infinity. The fab() function will return us a NaN value if we pass the argument with the value of either negative or positive value of NaN. Just look at this sample.
#include <cmath>
int main()
{
std::cout << "fabs(+5.0) = " << std::fabs(+5.0) << '\n'
<< "fabs(-6.0) = " < std::fabs(-6.0) << '\n';
std::cout << "fabs(-0.0) = " << std::fabs(-0.0) << '\n'
<< "fabs(-Inf) = " << std::fabs(-INFINITY) << '\n'
<< "fabs(-NaN) = " << std::fabs(-NAN) << '\n';
}
In the beginning, we write two library files which are #include<iostream> and #include<cmath>, having all the necessary functions and definitions that we need in this code. Then we call the main function here; we use standard std for the input-output standard like cout, cin, and endl statements, etc. Our system needs to know about all the statements because we did not mention the std above. With the std standard, we write two statements to display our values, and we use one cout statement to display two values. In these statements, we call out function fabs, and in the parameters of these functions, we pass the values that we want to explain. As a result, our negative values become positive, and negative infinity values become positive inf and NaN with the negative sign converted to the positive NaN.
Conclusion:
In this article, we define what absolute value really is and discuss fabs() function and how we can use it in our code with the help of some examples in C++. Then we see the multiple types of returning absolute values also experienced that we must set the same and true data-type for all the functions. We see that the values we pass in the parameters of the function and the return value that function gives us back are much different from each other.