C++ Math fabs()

Last Updated : 22 May 2026

Math fabs() Function

In C++, the fabs() function is defined in the <cmath> or <math.h> header file and is used to calculate the absolute value of a given number. It removes the negative sign from a number and always returns a non-negative result.

The fabs() function supports float, double, and integer values.

Suppose a number is 'x':

Syntax

It has the following syntax.

double fabs(double x); int fabs(int x);

Parameters

x: The value whose absolute value is to be determined.

Return value

It returns the absolute value of x.

Examples of the List remainder() Function

Here, we are going to discuss several examples to demonstrate the List remainder() Function Function.

Example 1: Calculate the Absolute Value of a Positive Number Using fabs()

This example demonstrates how the fabs() function behaves when the input value is positive.

Output:

Value of x is :11.2
Absolute value of x is : 11.2   

Explanation:

In this example, fabs() function determines the absolute value of x=11.2.

Example 2: Calculate the Absolute Value of a Negative Number Using fabs()

This example demonstrates how the fabs() function converts a negative value into a positive value.

Output:

Value of x is :-9.4
Absolute value of x is : 9.4

Explanation:

In this example, fabs() function computes the absolute value of x when the value of x is equal to -9.4.

Example 3: Use fabs() with a Zero Value

This example demonstrates how the fabs() function behaves when the input value is zero.

Output:

Value of x is : 0
Absolute value of x is : 0