diff options
author | Vladimir Azarov <avm@intermediate-node.net> | 2025-05-31 19:30:21 +0200 |
---|---|---|
committer | Vladimir Azarov <avm@intermediate-node.net> | 2025-05-31 19:30:38 +0200 |
commit | 546a5861526192a908f2aa2bfc3cfe4f3f3baf43 (patch) | |
tree | acfe627e088bdba54a42e786d3b6b7053ec56fca /caux.c | |
parent | 868e6313e3824d68b3121c5c95c7f29bc088c0e9 (diff) |
Proper constant parsing
Diffstat (limited to 'caux.c')
-rw-r--r-- | caux.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> + +#include <math.h> + +#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) |