Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functions
c++
absolute
communitycreator

What is the fabs() function in C++?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The fabs() function in C++ is used to return the absolute value of an argument passed to the function.

The fabs() function is defined in the cmath header file.

Mathematically, it is represented as: fabs(num)=numfabs(num) = |num|

Syntax

fabs(double num)

Parameter value

The fabs() function takes a single parameter value which is a number of any type: double, float, or long double.

Return value

The fabs() function returns the absolute value of the number passed to it as an argument.

Example 1

Let’s use the fabs() function for double numbers:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int result1;
int result2;
// for a negative double
double x = -10.25, result;
// for a positve double
double y = 10.25;
result1 = fabs(x);
result2 = fabs(y);
cout << "fabs(" << x << ") = |" << x << "| = " << result1<<endl;
cout << "fabs(" << y << ") = |" << y << "| = " << result2;
return 0;
}
Implementing the fabs() function for double numbers

Code Explanation

  • Line 6–7: We create int variables result1 and result2.
  • Line 9–11: We create double variables x and y.
  • Line 14–15: Using the fabs() function, we take the absolute value of x and y variables and assign the outputs respectively to variables result1 and result2.
  • Line 17 & 18: we printed the variables result1 and result2.

Example 2

Let’s use the fabs() function for float numbers:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int result1;
int result2;
// for a negative float
float x = -3.5f, result;
// for a positve float
float y = 3.5f;
result1 = fabs(x);
result2 = fabs(y);
cout << "fabs(" << x << ") = |" << x << "| = " << result1<<endl;
cout << "fabs(" << y << ") = |" << y << "| = " << result2;
return 0;
}
Implementing the fabs() function for float numbers

Example 3

Let’s use the fabs() function for long double numbers:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int result1;
int result2;
// for a negative long double
long double x = -1.256, result;
// for a positve long double
long double y = 1.256;
result1 = fabs(x);
result2 = fabs(y);
cout << "fabs(" << x << ") = |" << x << "| = " << result1<<endl;
cout << "fabs(" << y << ") = |" << y << "| = " << result2;
return 0;
}
Implementing the fabs() function fo long double numbers

RELATED TAGS

functions
c++
absolute
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring