-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_util.c
More file actions
80 lines (64 loc) · 1.5 KB
/
float_util.c
File metadata and controls
80 lines (64 loc) · 1.5 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "es.h"
#include "prim.h"
#include "float_util.h"
double
termtof(Term *term, char *funname, int arg)
{
char *str = nil;
char *endptr = nil;
double res;
errno = 0;
str = getstr(term);
res = strtod(str, &endptr);
if(res == 0 && str == endptr)
fail(funname, "invalid input: $%d = %s", arg, str);
if(res == 0 && errno == ERANGE)
fail(funname, "conversion overflow: $%d = %s", arg, str);
return res;
}
double
listtof(List *list, char *funname, int arg)
{
if(list == nil)
fail(funname, "missing argument $%d", arg);
return termtof(list->term, funname, arg);
}
List *
floattolist(double num, char *funname)
{
char temp[256];
int printlen;
List *result = nil; Root r_result;
gcref(&r_result, (void **)&result);
printlen = snprintf(&temp[0], sizeof(temp), "%.8g", num);
if(printlen >= (int)sizeof(temp))
fail(funname, "result conversion failed (temp string too short)");
result = mklist(mkstr(str("%s", temp)), nil);
gcrderef(&r_result);
return result;
}
int64_t
termtoint(Term *term, char *funname, int arg)
{
int64_t res;
errno = 0;
res = strtoll(getstr(term), NULL, 10);
if(res == 0) {
switch(errno) {
case EINVAL:
fail(funname, str("invalid input: $%d = '%s'", arg, getstr(term)));
break;
case ERANGE:
fail(funname, str("conversion overflow: $%d = '%s'", arg, getstr(term)));
break;
}
}
return res;
}
int64_t
listtoint(List *list, char *funname, int arg)
{
if(list == nil)
fail(funname, "missing argument $%d", arg);
return termtoint(list->term, funname, arg);
}