Search code examples
cc99xcode7-beta5

fabs vs. fabsl, should I ever have to type fabsl in my source?


I'm rewriting some Mac code that embeds a freeware library originally written in C. The compiler is complaining that since I'm using long double, I should use fabsl rather than fabs. So I went and changed them.

However, reading a few pages on the topic it seems that there should be no difference, that ever since C99, there is a type generic macro that inserts the correct call based on type.

So perhaps I am using the wrong dialect?

Does anyone know what the Compiler Default is in xcode7, and whether it has the generic macro?


Solution

  • The generic macro is defined in <tgmath.h>, so you need to #include it, as shown in the following snippet:

    #include <tgmath.h>
    #include <stdio.h>
    
    int main() {
      long double ld = 3.14;
      double d = 3.14;
      float f = 3.14f;
      printf("%Lf %lf, %f\n",fabs(ld), fabs(d), fabs(f));
      return 0;
    }
    

    It compiles flawlessly with

    gcc -Wall -Wextra a.c -oa -std=c99