2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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.
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.
15 #include "ao_scheme.h"
18 #ifdef AO_SCHEME_FEATURE_FLOAT
20 static void float_mark(void *addr)
25 static int float_size(void *addr)
29 return sizeof (struct ao_scheme_float);
32 static void float_move(void *addr)
37 const struct ao_scheme_type ao_scheme_float_type = {
45 #define FLOAT_FORMAT "%g"
49 ao_scheme_float_write(ao_poly p, bool write)
51 struct ao_scheme_float *f = ao_scheme_poly_float(p);
64 printf (FLOAT_FORMAT, v);
68 ao_scheme_poly_number(ao_poly p)
70 switch (ao_scheme_poly_base_type(p)) {
72 return ao_scheme_poly_int(p);
73 case AO_SCHEME_BIGINT:
74 return ao_scheme_poly_bigint(p)->value;
76 switch (ao_scheme_other_type(ao_scheme_poly_other(p))) {
78 return ao_scheme_poly_float(p)->value;
85 ao_scheme_float_get(float value)
87 struct ao_scheme_float *f;
89 f = ao_scheme_alloc(sizeof (struct ao_scheme_float));
90 f->type = AO_SCHEME_FLOAT;
92 return ao_scheme_float_poly(f);
96 ao_scheme_do_inexactp(struct ao_scheme_cons *cons)
98 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
100 if (ao_scheme_poly_type(ao_scheme_arg(cons, 0)) == AO_SCHEME_FLOAT)
101 return _ao_scheme_bool_true;
102 return _ao_scheme_bool_false;
106 ao_scheme_do_finitep(struct ao_scheme_cons *cons)
111 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
112 return AO_SCHEME_NIL;
113 value = ao_scheme_arg(cons, 0);
114 switch (ao_scheme_poly_type(value)) {
116 case AO_SCHEME_BIGINT:
117 return _ao_scheme_bool_true;
118 case AO_SCHEME_FLOAT:
119 f = ao_scheme_poly_float(value)->value;
120 if (!isnan(f) && !isinf(f))
121 return _ao_scheme_bool_true;
123 return _ao_scheme_bool_false;
127 ao_scheme_do_infinitep(struct ao_scheme_cons *cons)
132 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
133 return AO_SCHEME_NIL;
134 value = ao_scheme_arg(cons, 0);
135 switch (ao_scheme_poly_type(value)) {
136 case AO_SCHEME_FLOAT:
137 f = ao_scheme_poly_float(value)->value;
139 return _ao_scheme_bool_true;
141 return _ao_scheme_bool_false;
145 ao_scheme_do_sqrt(struct ao_scheme_cons *cons)
149 if (!ao_scheme_check_argc(_ao_scheme_atom_sqrt, cons, 1, 1))
150 return AO_SCHEME_NIL;
151 value = ao_scheme_arg(cons, 0);
152 if (!ao_scheme_number_typep(ao_scheme_poly_type(value)))
153 return ao_scheme_error(AO_SCHEME_INVALID, "%s: non-numeric", ao_scheme_poly_atom(_ao_scheme_atom_sqrt)->name);
154 return ao_scheme_float_get(sqrtf(ao_scheme_poly_number(value)));