summaryrefslogtreecommitdiff
path: root/caux.c
blob: 1602c557f6f3a1fad7538e65ceaa0621a5958e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)