Stingery - 2 months ago 5x

C++ Question

I want to have the absolute value of a

`long double`

According to

`<cmath>`

`<math.h>`

`double fabs (double x);`

float fabs (float x);

long double fabs (long double x);

However, when doing

`long double ld = fabs(static_cast<long double>(0));`

`Absolute value function 'fabs' given an argument of type 'long double' but has parameter of type 'double' which may cause truncation of value`

How come?

What other ways are there to get the absolute value of a

`long double`

Answer

According to cppreference http://en.cppreference.com/w/c/numeric/math/fabs and http://en.cppreference.com/w/cpp/numeric/math/fabs the C version of `fabs`

in the global namespace only accepts a `double`

argument and you would need to use `fabsl`

. However `std::fabs`

or `std::abs`

should have the proper `long double`

overloads for you.

Source (Stackoverflow)

Comments