altos/scheme: Support scheme subsetting via feature settings
[fw/altos] / src / scheme / ao_scheme_float.c
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_scheme.h"
16 #include <math.h>
17
18 #ifdef AO_SCHEME_FEATURE_FLOAT
19
20 static void float_mark(void *addr)
21 {
22         (void) addr;
23 }
24
25 static int float_size(void *addr)
26 {
27         if (!addr)
28                 return 0;
29         return sizeof (struct ao_scheme_float);
30 }
31
32 static void float_move(void *addr)
33 {
34         (void) addr;
35 }
36
37 const struct ao_scheme_type ao_scheme_float_type = {
38         .mark = float_mark,
39         .size = float_size,
40         .move = float_move,
41         .name = "float",
42 };
43
44 #ifndef FLOAT_FORMAT
45 #define FLOAT_FORMAT "%g"
46 #endif
47
48 void
49 ao_scheme_float_write(ao_poly p)
50 {
51         struct ao_scheme_float *f = ao_scheme_poly_float(p);
52         float   v = f->value;
53
54         if (isnanf(v))
55                 printf("+nan.0");
56         else if (isinff(v)) {
57                 if (v < 0)
58                         printf("-");
59                 else
60                         printf("+");
61                 printf("inf.0");
62         } else
63                 printf (FLOAT_FORMAT, v);
64 }
65
66 float
67 ao_scheme_poly_number(ao_poly p)
68 {
69         switch (ao_scheme_poly_base_type(p)) {
70         case AO_SCHEME_INT:
71                 return ao_scheme_poly_int(p);
72         case AO_SCHEME_OTHER:
73                 switch (ao_scheme_other_type(ao_scheme_poly_other(p))) {
74                 case AO_SCHEME_BIGINT:
75                         return ao_scheme_bigint_int(ao_scheme_poly_bigint(p)->value);
76                 case AO_SCHEME_FLOAT:
77                         return ao_scheme_poly_float(p)->value;
78                 }
79         }
80         return NAN;
81 }
82
83 ao_poly
84 ao_scheme_float_get(float value)
85 {
86         struct ao_scheme_float  *f;
87
88         f = ao_scheme_alloc(sizeof (struct ao_scheme_float));
89         f->type = AO_SCHEME_FLOAT;
90         f->value = value;
91         return ao_scheme_float_poly(f);
92 }
93
94 ao_poly
95 ao_scheme_do_inexactp(struct ao_scheme_cons *cons)
96 {
97         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
98                 return AO_SCHEME_NIL;
99         if (ao_scheme_poly_type(ao_scheme_arg(cons, 0)) == AO_SCHEME_FLOAT)
100                 return _ao_scheme_bool_true;
101         return _ao_scheme_bool_false;
102 }
103
104 ao_poly
105 ao_scheme_do_finitep(struct ao_scheme_cons *cons)
106 {
107         ao_poly value;
108         float   f;
109
110         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
111                 return AO_SCHEME_NIL;
112         value = ao_scheme_arg(cons, 0);
113         switch (ao_scheme_poly_type(value)) {
114         case AO_SCHEME_INT:
115         case AO_SCHEME_BIGINT:
116                 return _ao_scheme_bool_true;
117         case AO_SCHEME_FLOAT:
118                 f = ao_scheme_poly_float(value)->value;
119                 if (!isnan(f) && !isinf(f))
120                         return _ao_scheme_bool_true;
121         }
122         return _ao_scheme_bool_false;
123 }
124
125 ao_poly
126 ao_scheme_do_infinitep(struct ao_scheme_cons *cons)
127 {
128         ao_poly value;
129         float   f;
130
131         if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
132                 return AO_SCHEME_NIL;
133         value = ao_scheme_arg(cons, 0);
134         switch (ao_scheme_poly_type(value)) {
135         case AO_SCHEME_FLOAT:
136                 f = ao_scheme_poly_float(value)->value;
137                 if (isinf(f))
138                         return _ao_scheme_bool_true;
139         }
140         return _ao_scheme_bool_false;
141 }
142
143 ao_poly
144 ao_scheme_do_sqrt(struct ao_scheme_cons *cons)
145 {
146         ao_poly value;
147
148         if (!ao_scheme_check_argc(_ao_scheme_atom_sqrt, cons, 1, 1))
149                 return AO_SCHEME_NIL;
150         value = ao_scheme_arg(cons, 0);
151         if (!ao_scheme_number_typep(ao_scheme_poly_type(value)))
152                 return ao_scheme_error(AO_SCHEME_INVALID, "%s: non-numeric", ao_scheme_poly_atom(_ao_scheme_atom_sqrt)->name);
153         return ao_scheme_float_get(sqrtf(ao_scheme_poly_number(value)));
154 }
155 #endif