In C++, std::abs is overloaded for both signed integer and floating point types. std::fabs only deals with floating point types (pre C++11). Note that the std:: is important; the C function ::abs that is commonly available for legacy reasons will only handle int!

The problem with

Copyfloat f2= fabs(-9);

is not that there is no conversion from int (the type of -9) to double, but that the compiler does not know which conversion to pick (int -> float, double, long double) since there is a std::fabs for each of those three. Your workaround explicitly tells the compiler to use the int -> double conversion, so the ambiguity goes away.

C++11 solves this by adding double fabs( Integral arg ); which will return the abs of any integer type converted to double. Apparently, this overload is also available in C++98 mode with libstdc++ and libc++.

In general, just use std::abs, it will do the right thing. (Interesting pitfall pointed out by @Shafik Yaghmour. Unsigned integer types do funny things in C++.)

Answer from Baum mit Augen on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-abs-method-examples
Java Math abs() method with Examples - GeeksforGeeks
July 11, 2025 - The java.lang.Math.abs() returns the absolute value of a given argument. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
Top answer
1 of 3
49

In C++, std::abs is overloaded for both signed integer and floating point types. std::fabs only deals with floating point types (pre C++11). Note that the std:: is important; the C function ::abs that is commonly available for legacy reasons will only handle int!

The problem with

Copyfloat f2= fabs(-9);

is not that there is no conversion from int (the type of -9) to double, but that the compiler does not know which conversion to pick (int -> float, double, long double) since there is a std::fabs for each of those three. Your workaround explicitly tells the compiler to use the int -> double conversion, so the ambiguity goes away.

C++11 solves this by adding double fabs( Integral arg ); which will return the abs of any integer type converted to double. Apparently, this overload is also available in C++98 mode with libstdc++ and libc++.

In general, just use std::abs, it will do the right thing. (Interesting pitfall pointed out by @Shafik Yaghmour. Unsigned integer types do funny things in C++.)

2 of 3
19

With C++ 11, using abs() alone is very dangerous:

Copy#include <iostream>
#include <cmath>

int main() {
    std::cout << abs(-2.5) << std::endl;
    return 0;
}

This program outputs 2 as a result. (See it live)

Always use std::abs():

Copy#include <iostream>
#include <cmath>

int main() {
    std::cout << std::abs(-2.5) << std::endl;
    return 0;
}

This program outputs 2.5.

You can avoid the unexpected result with using namespace std; but I would adwise against it, because it is considered bad practice in general, and because you have to search for the using directive to know if abs() means the int overload or the double overload.

Discussions

Difference between abs() and fabs(), and can i replace with each other in the program?? And abs() fn is kinda not working.....
fabs is one of a family of type-specific names from C. The C++ standard libraries provide these names, fabsf for float, fabs for double and fabsl for long double, for compatibility with C, so that barring other issues C code that uses these names can be compiled as C++. However, the C++ library provides corresponding overloads of the single name abs, which one should use in C++. There is an issue with abs, namely that the overloads for integral argument types reside in (or for the name placed in the std namespace, in ). So, for using abs one should better include both and . And since in C++17 and later the header provides some functions that are not provided by , if one writes a wrapper to include standard library numerical stuff headers then it should best include also . More on reddit.com
🌐 r/cpp_questions
9
1
November 26, 2019
Fabs and abs
Try simply this: [code]#if 1 /* 0 or 1 / #include // for “abs” #include // for “fabs” #else / 0 or 1 / #include // for “std::abs” and “std::fabs” #endif / 0 or 1 */ · You should have got it by now that there’s no “default c++ behaviour” what concerns ... More on root-forum.cern.ch
🌐 root-forum.cern.ch
0
0
July 10, 2012
c++ - Is there any difference between std::abs and std::fabs when applied to floating point values? - Stack Overflow
Because I don't like wasting my ... std. That is, I use ::fabs when I want a double to be returned, ::fabsf when I want a float out, and ::fabsl when I want a long double out. Similarly for ::abs when I want an int, ::absl when I want a long, and ::absll when I want a long long. ... Sign up to request clarification or add additional ... More on stackoverflow.com
🌐 stackoverflow.com
Using abs () method in java. My compiler doesn't know the method - Stack Overflow
I have a simple question, but i can't find a solution for it. I want to use abs() method, but it doesn't work. I'm always getting the error Cannot find symbol: method abs(int) I have already trie... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/cpp_questions › difference between abs() and fabs(), and can i replace with each other in the program?? and abs() fn is kinda not working.....
r/cpp_questions on Reddit: Difference between abs() and fabs(), and can i replace with each other in the program?? And abs() fn is kinda not working.....
November 26, 2019 - fabs is one of a family of type-specific names from C. The C++ standard libraries provide these names, fabsf for float, fabs for double and fabsl for long double, for compatibility with C, so that barring other issues C code that uses these ...
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs.html
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
March 14, 2025 - 1-4) Computes the absolute value of the floating-point value num. The library provides overloads of std::abs and std::fabs for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)
🌐
Tutorialspoint
tutorialspoint.com › home › java › java number abs method
Java Number abs Method
September 1, 2008 - Discover how to utilize the abs() method in Java for obtaining absolute values of numbers with detailed examples.
🌐
Quora
quora.com › What-is-the-difference-between-abs-and-fabs-function
What is the difference between abs() and fabs() function? - Quora
The only difference between both of them is, abs() is used to calculate the absolute value for integer type numbers whereas fabs() are used for floating type numbers. abs() function is use un...
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java math › java math abs() method
Java Math abs() method
January 9, 2025 - The abs() method of Java is overloaded for various data types. The allowed types are as under. ... public class DriverClass { public static void main(String args[]) { int number = +5; // Print the original number System.out.println("Original Number = " + number); // Printing the absolute value // Calling the Math.abs() method System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number)); number = -5; // Print the original number System.out.println("Original Number = " + number); // Printing the absolute value // Calling the Math.abs() method System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number)); } }
🌐
CERN
root-forum.cern.ch › t › fabs-and-abs › 14556
Fabs and abs - ROOT - ROOT Forum
July 10, 2012 - Try simply this: [code]#if 1 /* 0 or 1 / #include <stdlib.h> // for “abs” #include <math.h> // for “fabs” #else / 0 or 1 / #include // for “std::abs” and “std::fabs” #endif / 0 or 1 */ · You should have got it by now that there’s no “default c++ behaviour” what concerns ...
🌐
SACO Evaluator
saco-evaluator.org.za › docs › cppreference › en › cpp › numeric › math › fabs.html
std::abs(float), std::fabs - cppreference.com
If successful, returns the absolute value of arg (|arg|). The value returned is exact and does not depend on any rounding modes. This function is not subject to any of the error conditions specified in math_errhandling · If the implementation supports IEEE floating-point arithmetic (IEC 60559), ... Between C++11 and C++14, the standard erroneously required std::abs to have overloads for integer types returning double.
🌐
Programmers
programmers.io › home › blog › absolute value abs () in java
Absolute Value Abs () In Java - Programmers.io
June 26, 2025 - The Math.abs () method is used in Java development to obtain the absolute value of a number. It belongs to the java.lang.Math class, which is a part of the core Java API. The method is designed to handle various numerical data types and provides ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › implementing and manipulating abs in java
Java Math abs() Method with Examples
March 6, 2025 - Explanation: Abs in Java converts negative numbers to positive while keeping positive numbers unchanged. It supports multiple data types, ensuring flexibility in handling different numeric values.
🌐
Scaler
scaler.com › home › topics › abs() in java
abs() in Java - Scaler Topics
May 5, 2024 - abs() in Java is the inbuilt method of the Math that is present in java.lang package. Math.abs() returns an absolute value of the number, i.e., a non-negative representation of the number.
🌐
Turing
turing.com › kb › java-absolute-value
Java Math Absolute Value Abs() Method
So, the abs() function helps you count the number of marbles in the jar, regardless of whether they have a plus sign or a minus sign. It only cares about the number of marbles, not the signs on them. To find a number's absolute value, Java's Math class has the abs() function.
🌐
Coin-or
coin-or.org › CppAD › Doc › abs.htm
AD Absolute Value Functions: abs, fabs
abs and · fabs are not defined for the base types · std::complex<float> or · std::complex<double> because the complex · abs function is not complex differentiable (see complex types faq ). Derivative CppAD defines the derivative of the ·
Top answer
1 of 2
15

In standard-conforming code that has included cmath and only calls std::abs on floats, doubles, and long doubles, there is no difference. However, it is instructive to look at the types returned by std::abs on various types when you call it with various sets of header files included.

On my system, std::abs(-42) is a double if I've included cmath but not cstdlib, an int if I've included cstdlib, and it produces a compilation error if I've included neither. Conversely, std::abs(-42.0) produces a compilation error (ambiguous overload) if I've included cstdlib but I haven't included cmath or a different compilation error (never heard of std::abs) if I've included neither.

On my platform, std::abs('x') gives me a double if I've included cmath or an int if I've included cstdlib but not cmath. Similar for a short int. Signedness does not appear to matter.

On my platform, the complex header apparently causes both the integral and the floating-point overloads of std::abs to be declared. I'm not certain this is mandated; perhaps you can find an otherwise reasonable platform on which std::abs(1e222) returns infinity with the wrong set of standard headers included.


The usual consequence of "you forgot a header in your program" is a compilation failure complaining of an undefined symbol, not a silent change in behaviour. With std::abs, however, the result can be std::abs(42) returning a double if you forgot cstdlib or std::abs('x') returning an int if you didn't. (Or perhaps you expected std::abs to give you an integral type when you pass it a short? Then, assuming my compiler got its promotion and overload resolution right, you had better make sure you don't include cmath.)

I have also spent too much time in the past trying to work out why code like double precise_sine = std::sin(myfloat) gives imprecise results. Because I don't like wasting my time on these sorts of surprises, I tend to avoid the overloaded variants of standard C functions in namespace std. That is, I use ::fabs when I want a double to be returned, ::fabsf when I want a float out, and ::fabsl when I want a long double out. Similarly for ::abs when I want an int, ::absl when I want a long, and ::absll when I want a long long.

2 of 2
0

Is there any difference at all between std::abs and std::fabs when applied to floating point values?

No there is not. Nor is there a difference for integral types.

It is idiomatic to use std::abs() because it is closest to the commonly used mathematical nomenclature.

🌐
W3Schools
w3schools.com › java › ref_math_abs.asp
Java Math abs() Method
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Javaer101
javaer101.com › en › article › 14119241.html
What's the difference between abs and fabs? - Javaer101
October 23, 2020 - std::fabs only deals with floating point types (pre C++11). Note that the std:: is important, the C function ::abs that is commonly available for legacy reasons will only handle int!