C Programming

Fabs() Function in C Language (Absolute Value)

The absolute value of a real number is equal to the amount with a positive sign, regardless of the sign of the original value. In this Linux Hint article, we will show you the step-by-step process on how to determine the absolute value of a floating-point variable with the fabs() function in the C language using Linux gcc.

We will practically explain how to get the absolute values of different data types using practical examples and pictures that we prepared for you. In order for you to have a solid understanding of this function, we will explain the syntax and the types of input data and output accepted by the fabs() function in a theoretical section.

We will also include the code snippets that you can copy and paste into your .c file to run this function in real time.

Syntax of the Fabs() Function in C Language

double fabs (double x)

Fabs() Function Description in the C Language

The fabs() function, like fabsf(), is a function which is derived from abs(). Both are used to determine the absolute value of a variable. The abs() function determines the absolute value of an integer and fabs() determines the absolute value of a float data type.

In the following expression, the fabs() function returns the absolute value of float X in a.

a = fabs ( x );

The data type that the fabs() function accepts in both its input and output arguments is of float type.

The fabs() function is one of a set of functions provided by the “math” library. To use this function, we must include the “math.h” header in our “.c” file as follows:

#include <math.h>

Once the “math.h” header are included, we can use abs(), fabs(), fabsf() as well as all the functions in the math libraries.

How to Find the Absolute Value of a Number with Decimal Fractions Using the Fabs() Function in the C Language

In this example, we will show you how to determine the absolute value of a variable of the Float type that contains a decimal value. The first step is to create our “main.c”. Then, we include the stdio.h and math.h headers as shown in the following:

#include <stdio.h>

#include <math.h>

Then, we create our main function which is returned empty and in which we declare our variables. To get the absolute value of “x”, we first create a variable of float type and assign it the value -3.1416. We store the result in “a”, which should be of the same type as “x”. Then, we call the fabs() function and pass “x” as input argument and “a” as output argument.

Using the printf() function, we print the result on the command console after the message, “The absolute value of x is: “. The following is the code for this example of the fabs() function:

#include <stdio.h>

#include <math.h>

void  main ()
   {
float x = -3.1416;
    float a;
    a = fabs ( x );
printf("The absolute value of x is: %f\n", a);

   }

To compile our code, we need to run the following line from the command console, specifying the path of the “main.c” file and the output which, in this case, gives our application the name, “app_fabs1”:

~$ gcc Documents/main.c -o app_fabs1

After we compile our code, we run the application as follows:

~$ ./app_fabs1

The following image shows the absolute value of “x” in the command console:

Integers and Doubles as Input Argument for the Fabs() Function in the C Language

When we use the variables of double type as input and output arguments to the fabs() function, the result is exactly the same as for the variables of float type. The following figure shows the code fragment that we used in the previous example, but with variables of double type:

In the cases where we use the variables of int type as input arguments when calling the fabs() function, it returns only the absolute value of the integer part of “x”, while the fractional part or the part after the floating point number is rounded down.

Conclusion

In this Linux Hint article, we explained how to get the absolute values from a floating-point variable using the fabs() function in Linux gcc. We gave a theoretical description of the syntax and data type which are accepted in the inputs and outputs of this function. We also showed how you can implement this function using practical examples, images, and code snippets. We explained the step-by-step process on how to compile the examples from the Linux shell and how to declare the libraries correctly in order to use the fabs() function which is part of the gcc “math” library. We hope that this article is useful for you. For more tips and information, see other articles for the C language on Linux Hint.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).