/whythough.c Secret
Created
October 9, 2021 13:20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <stdlib.h> | |
double handle_doubles(double input) { | |
return fabs(input); | |
} | |
float handle_floats(float input) { | |
return fabsf(input); | |
} | |
long handle_long(long input) { | |
return labs(input); | |
} | |
#define abs(x)\ | |
__builtin_choose_expr(\ | |
__builtin_types_compatible_p(typeof(x), double),\ | |
handle_doubles(x),\ | |
__builtin_choose_expr(\ | |
__builtin_types_compatible_p(typeof(x), float),\ | |
handle_floats(x),\ | |
handle_long((long)x))) | |
int main() { | |
float a = 0.f; | |
double b = -5.; | |
int c = 0; | |
printf("%f\n", abs(a)); | |
printf("%f\n", abs(b)); | |
printf("%ld\n", abs(c)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment