savonet / ocaml-posix

Bindings to the various POSIX APIs
https://www.liquidsoap.info/ocaml-posix/
MIT License
27 stars 2 forks source link

Fix detection of posix arithmetic (int or float) types #9

Closed MisterDA closed 1 year ago

MisterDA commented 1 year ago

This warning is generated by clang 14.

warning: floating-point comparison is always false; constant cannot be represented exactly in type float [-Wliteral-range]

Use float instead of double constants.

#include <stdio.h>

typedef float x;
typedef double y;
typedef int z;

#define IS_FLOAT_FLOAT ((float)((x)1.23) == 1.23) // warning, 0, incorrect!
#define IS_FLOAT_FLOAT_F ((float)((x)1.23f) == 1.23f) // 1
#define IS_DOUBLE_FLOAT ((float)((y)1.23) == 1.23) // warning, 0
#define IS_DOUBLE_FLOAT_F ((float)((y)1.23f) == 1.23f) // 1
#define IS_INT_FLOAT ((float)((z)1.23) == 1.23) // warning, 0
#define IS_INT_FLOAT_F ((float)((z)1.23f) == 1.23f) // 0

int main(void) {
printf("IS_FLOAT_FLOAT: %d\nIS_FLOAT_FLOAT_F: %d\nIS_DOUBLE_FLOAT: %d\n"
       "IS_DOUBLE_FLOAT_F: %d\nIS_INT_FLOAT: %d\nIS_INT_FLOAT_F: %d\n",
       IS_FLOAT_FLOAT, IS_FLOAT_FLOAT_F, IS_DOUBLE_FLOAT, IS_DOUBLE_FLOAT_F,
       IS_INT_FLOAT, IS_INT_FLOAT_F);
}
toots commented 1 year ago

Thanks!