fabs() in C++ Last Updated : 31 Jan, 2023 Comments Improve Suggest changes Like Article Like Report The fabs() function returns the absolute value of the argument. Mathematically |a|. If a is value given in the argument. Syntax: double fabs(double a); float fabs(float a); int fabs(int a); Parameter: The fabs() function takes a single argument, a whose absolute value has to be returned. Return: The fabs() function returns the absolute value of the argument. Error: It is mandatory to give both the arguments otherwise it will give error no matching function for call to 'fabs()' like this. # CODE 1 CPP // CPP code to illustrate // fabs() function #include <cmath> #include <iostream> using namespace std; int main() { int a = -10, answer; answer = fabs(a); cout << "fabs of " << a << " is " << answer << endl; return 0; } OUTPUT : fabs of -10 is 10 Time Complexity: O(1) Space Complexity: O(1) # CODE 2 CPP // CPP code to illustrate // fabs() function #include <cmath> #include <iostream> using namespace std; int main() { long int a = -35; double answer; answer = fabs(a); cout << "fabs of " << a << " is " << answer << endl; return 0; } OUTPUT : fabs of -35 is 35 Time Complexity: O(1) Space Complexity: O(1) Here is an example of a double. #Code 3 C++ #include <cmath> #include <iostream> using namespace std; int main() { double x = -3.14; cout << "The absolute value of " << x << " is " << fabs(x) << endl; return 0; } OutputThe absolute value of -3.14 is 3.14 Time Complexity: O(1) Space Complexity: O(1) #Code 4 C++ #include <cmath> #include <iostream> #include <string> using namespace std; int main() { double x = -5.67; cout << "The absolute value of " << x << " is " << fabs(x) << endl; string s = to_string(fabs(x)); cout<<"The absolute value of "<<x<<" is string format: "<<s<<endl; return 0; } OutputThe absolute value of -5.67 is 5.67 The absolute value of -5.67 is string format: 5.670000 Time Complexity: O(1) Space Complexity: O(1) Create Quiz Comment P pawan_asipu Follow 0 Improve P pawan_asipu Follow 0 Improve Article Tags : Misc Programming Language C++ CPP-Library cpp-math +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like