summaryrefslogtreecommitdiff
path: root/caux.c
diff options
context:
space:
mode:
authorVladimir Azarov <avm@intermediate-node.net>2025-05-31 19:30:21 +0200
committerVladimir Azarov <avm@intermediate-node.net>2025-05-31 19:30:38 +0200
commit546a5861526192a908f2aa2bfc3cfe4f3f3baf43 (patch)
treeacfe627e088bdba54a42e786d3b6b7053ec56fca /caux.c
parent868e6313e3824d68b3121c5c95c7f29bc088c0e9 (diff)
Proper constant parsing
Diffstat (limited to 'caux.c')
-rw-r--r--caux.c35
1 files changed, 35 insertions, 0 deletions
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 <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)