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(FILE *out, ao_poly p, bool write)
51 struct ao_scheme_float *f = ao_scheme_poly_float(p);
64 fprintf(out, 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)
100 if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
101 AO_SCHEME_POLY, &val,
103 return AO_SCHEME_NIL;
104 if (ao_scheme_poly_type(val) == AO_SCHEME_FLOAT)
105 return _ao_scheme_bool_true;
106 return _ao_scheme_bool_false;
110 ao_scheme_do_finitep(struct ao_scheme_cons *cons)
115 if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
116 AO_SCHEME_POLY, &val,
118 return AO_SCHEME_NIL;
119 switch (ao_scheme_poly_type(val)) {
121 case AO_SCHEME_BIGINT:
122 return _ao_scheme_bool_true;
123 case AO_SCHEME_FLOAT:
124 f = ao_scheme_poly_float(val)->value;
125 if (!isnan(f) && !isinf(f))
126 return _ao_scheme_bool_true;
128 return _ao_scheme_bool_false;
132 ao_scheme_do_infinitep(struct ao_scheme_cons *cons)
137 if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
138 AO_SCHEME_POLY, &val,
140 return AO_SCHEME_NIL;
141 switch (ao_scheme_poly_type(val)) {
142 case AO_SCHEME_FLOAT:
143 f = ao_scheme_poly_float(val)->value;
145 return _ao_scheme_bool_true;
147 return _ao_scheme_bool_false;
151 ao_scheme_do_sqrt(struct ao_scheme_cons *cons)
155 if (!ao_scheme_parse_args(_ao_scheme_atom_sqrt, cons,
158 return AO_SCHEME_NIL;
159 return ao_scheme_float_get(sqrtf(f));