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)
51 struct ao_scheme_float *f = ao_scheme_poly_float(p);
63 printf (FLOAT_FORMAT, v);
67 ao_scheme_poly_number(ao_poly p)
69 switch (ao_scheme_poly_base_type(p)) {
71 return ao_scheme_poly_int(p);
72 case AO_SCHEME_BIGINT:
73 return ao_scheme_poly_bigint(p)->value;
75 switch (ao_scheme_other_type(ao_scheme_poly_other(p))) {
77 return ao_scheme_poly_float(p)->value;
84 ao_scheme_float_get(float value)
86 struct ao_scheme_float *f;
88 f = ao_scheme_alloc(sizeof (struct ao_scheme_float));
89 f->type = AO_SCHEME_FLOAT;
91 return ao_scheme_float_poly(f);
95 ao_scheme_do_inexactp(struct ao_scheme_cons *cons)
97 if (!ao_scheme_check_argc(_ao_scheme_atom_led, cons, 1, 1))
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;
105 ao_scheme_do_finitep(struct ao_scheme_cons *cons)
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)) {
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;
122 return _ao_scheme_bool_false;
126 ao_scheme_do_infinitep(struct ao_scheme_cons *cons)
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;
138 return _ao_scheme_bool_true;
140 return _ao_scheme_bool_false;
144 ao_scheme_do_sqrt(struct ao_scheme_cons *cons)
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)));