1

I want to have the absolute value of a long double.

According to <cmath> or <math.h>, the following is available:

     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));, I get the following warning (LLVM 7.1):

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?

Edit:

std::abs eventually did the job. However, std::fabs didn't. As was pointed out in the comments, this may be due to a non-conforming implementation.

Stingery
  • 432
  • 4
  • 16
  • Did you verify in the public headers if the function is implemented, or did you only check the docs? – JVApen Aug 09 '16 at 18:26
  • Only the docs, assuming this is standard. Apparently, I was wrong (?) – Stingery Aug 09 '16 at 18:28
  • 1
    Maybe try `std::fabs` or `std::abs`? – Fred Larson Aug 09 '16 at 18:29
  • The standard is whatever should be implemented, though that doesn't mean this is in practice. Where possible I'll try to look at the headers as they match the implementation. – JVApen Aug 09 '16 at 18:30
  • 2
    as far as I can tell only `` is required to have `long double fabs (long double x);`. If including `` and using `std::fabs` does not work then you have a non conforming implementation. – NathanOliver Aug 09 '16 at 18:34
  • 1
    This is pretty cool, actually. Usually people post about the surprising aftermath of the opposite resulting from a `using namespace std;` lurking in the code. – user4581301 Aug 09 '16 at 18:36

1 Answers1

4

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188