From 546a5861526192a908f2aa2bfc3cfe4f3f3baf43 Mon Sep 17 00:00:00 2001 From: Vladimir Azarov Date: Sat, 31 May 2025 19:30:21 +0200 Subject: Proper constant parsing --- caux.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 caux.c (limited to 'caux.c') diff --git a/caux.c b/caux.c new file mode 100644 index 0000000..1602c55 --- /dev/null +++ b/caux.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +#include + +#include "export.h" + +#define PARSE_FP(fp_type, func, huge_val) \ + fp_type parse_ ## fp_type (Pointer repr, int *status) {\ + char *f = (char*)repr;\ + char *tmp;\ + fp_type result; \ + int saved_errno = errno;\ +\ + errno = 0;\ + result = func(f, &tmp);\ +\ + if (errno == ERANGE) {\ + if (result == huge_val)\ + *status = 1;\ + else\ + *status = -1;\ + }\ + errno = saved_errno;\ +\ + if (f + strlen(f) != tmp)\ + *status = 2;\ + return result;\ + } + +PARSE_FP(float, strtof, HUGE_VALF) +PARSE_FP(double, strtod, HUGE_VAL) -- cgit v1.2.3