In programming languages, the abbreviation of fabs is the floating Absolute, which means the floating-point numeric values. In C , fabs() is a function that returns in their argument an absolute value. The header library that defines and is used for this function is . The absolute value is defined as a real number that is not negative; whatever your sign, the result will always be a positive and no-zero value.

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

#include

using namespace std;

int main() {


  int a = 54;


  int rs;


  rs = fabs(a);


  cout << “fabs(“ << a << “) = “ << rs;


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-1.png" data-lazy- height="203" src="data:image/svg xml,” width=”634″>

In this example, we add the libraries with the name of that have the definition of the input-output stream also 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-2.png" data-lazy- height="98" src="data:image/svg xml,” width=”637″>

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

#include

using namespace std;

int main() {


  float n = 16;


  float result;


  result = fabsf(n);


  cout << “fabsf(“ << n << “) = |” << n << “| = “ << result;


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-3.png" data-lazy- height="201" src="data:image/svg xml,” width=”635″>

In the above instance, we integrate two libraries #include and #include 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-4.png" data-lazy- height="96" src="data:image/svg xml,” width=”637″>

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

#include

using namespace std;

int main() {


  long a = 30;


  long b;


  b = fabsl(a);


  cout << “fabsl(“ << a << “) = “ << b;


  return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-5.png" data-lazy- height="195" src="data:image/svg xml,” width=”631″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-6.png" data-lazy- height="113" src="data:image/svg xml,” width=”630″>

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

#include

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;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-7.png" data-lazy- height="199" src="data:image/svg xml,” width=”634″>

In the beginning, we write two library files which are #include and #include, 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Fabs-C-8.png" data-lazy- height="162" src="data:image/svg xml,” width=”633″>

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/omar-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.