Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* classes: h_files */ |
2 | ||
3c9a524f DH |
3 | #ifndef SCM_NUMBERS_H |
4 | #define SCM_NUMBERS_H | |
dee01b01 | 5 | |
e4b265d8 | 6 | /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc. |
dee01b01 | 7 | * |
0f2d19dd JB |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
dee01b01 | 12 | * |
0f2d19dd JB |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
dee01b01 | 17 | * |
0f2d19dd JB |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
20 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
21 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
22 | * |
23 | * As a special exception, the Free Software Foundation gives permission | |
24 | * for additional uses of the text contained in its release of GUILE. | |
25 | * | |
26 | * The exception is that, if you link the GUILE library with other files | |
27 | * to produce an executable, this does not by itself cause the | |
28 | * resulting executable to be covered by the GNU General Public License. | |
29 | * Your use of that executable is in no way restricted on account of | |
30 | * linking the GUILE library code into it. | |
31 | * | |
32 | * This exception does not however invalidate any other reasons why | |
33 | * the executable file might be covered by the GNU General Public License. | |
34 | * | |
35 | * This exception applies only to the code released by the | |
36 | * Free Software Foundation under the name GUILE. If you copy | |
37 | * code from other Free Software Foundation releases into a copy of | |
38 | * GUILE, as the General Public License permits, the exception does | |
39 | * not apply to the code that you add in this way. To avoid misleading | |
40 | * anyone as to the status of such modified files, you must delete | |
41 | * this exception notice from them. | |
42 | * | |
43 | * If you write modifications of your own for GUILE, it is your choice | |
44 | * whether to permit this exception to apply to your modifications. | |
82892bed | 45 | * If you do not wish that, delete this exception notice. */ |
d3a6bc94 | 46 | |
0f2d19dd JB |
47 | \f |
48 | ||
b4309c3c | 49 | #include "libguile/__scm.h" |
34f74fe1 MD |
50 | #include "libguile/print.h" |
51 | ||
232898d9 MV |
52 | #if defined (HAVE_FLOATINGPOINT_H) |
53 | #include <floatingpoint.h> | |
54 | #endif | |
55 | ||
56 | #if defined (HAVE_IEEEFP_H) | |
57 | #include <ieeefp.h> | |
58 | #endif | |
59 | ||
60 | #if defined (HAVE_NAN_H) | |
61 | #if defined (SCO) | |
62 | #define _IEEE 1 | |
63 | #endif | |
64 | #include <nan.h> | |
65 | #if defined (SCO) | |
66 | #undef _IEEE | |
67 | #endif | |
68 | #endif | |
69 | ||
0f2d19dd JB |
70 | \f |
71 | ||
14282d0f | 72 | /* Immediate Numbers, also known as fixnums |
0f2d19dd | 73 | * |
14282d0f | 74 | * Inums are exact integer data that fits within an SCM word. */ |
0f2d19dd | 75 | |
c0060c95 MV |
76 | /* SCM_T_SIGNED_MAX is (- (expt 2 n) 1), |
77 | * SCM_MOST_POSITIVE_FIXNUM should be (- (expt 2 (- n 2)) 1) | |
78 | * which is the same as (/ (- (expt 2 n) 4) 4) | |
79 | */ | |
80 | ||
004c0902 | 81 | #define SCM_I_FIXNUM_BIT (SCM_LONG_BIT - 2) |
c0060c95 | 82 | #define SCM_MOST_POSITIVE_FIXNUM ((SCM_T_SIGNED_BITS_MAX-3)/4) |
004c0902 | 83 | #define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM-1) |
5c75b29f | 84 | |
56100716 DH |
85 | /* SCM_SRS is signed right shift */ |
86 | #if (-1 == (((-1) << 2) + 2) >> 2) | |
14282d0f | 87 | # define SCM_SRS(x, y) ((x) >> (y)) |
0f2d19dd | 88 | #else |
14282d0f | 89 | # define SCM_SRS(x, y) ((x) < 0 ? ~((~(x)) >> (y)) : ((x) >> (y))) |
56100716 | 90 | #endif /* (-1 == (((-1) << 2) + 2) >> 2) */ |
0f2d19dd JB |
91 | |
92 | ||
56100716 DH |
93 | #define SCM_INUMP(x) (2 & SCM_UNPACK (x)) |
94 | #define SCM_NINUMP(x) (!SCM_INUMP (x)) | |
14282d0f DH |
95 | #define SCM_MAKINUM(x) (SCM_PACK ((((scm_t_signed_bits) (x)) << 2) + 2)) |
96 | #define SCM_INUM(x) (SCM_SRS ((scm_t_signed_bits) SCM_UNPACK (x), 2)) | |
0f2d19dd JB |
97 | |
98 | ||
894a712b DH |
99 | /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */ |
100 | #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM) | |
101 | #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM) | |
14282d0f | 102 | #define SCM_FIXABLE(n) (SCM_POSFIXABLE (n) && SCM_NEGFIXABLE (n)) |
894a712b DH |
103 | |
104 | ||
56100716 | 105 | /* A name for 0. */ |
b82c6ce0 | 106 | #define SCM_INUM0 (SCM_MAKINUM (0)) |
0f2d19dd JB |
107 | |
108 | ||
c0060c95 | 109 | /* SCM_MAXEXP is the maximum double precision exponent |
88eb6852 MD |
110 | * SCM_FLTMAX is less than or scm_equal the largest single precision float |
111 | */ | |
112 | ||
113 | #ifdef STDC_HEADERS | |
114 | #ifndef GO32 | |
115 | #include <float.h> | |
116 | #endif /* ndef GO32 */ | |
117 | #endif /* def STDC_HEADERS */ | |
118 | #ifdef DBL_MAX_10_EXP | |
119 | #define SCM_MAXEXP DBL_MAX_10_EXP | |
120 | #else | |
121 | #define SCM_MAXEXP 308 /* IEEE doubles */ | |
122 | #endif /* def DBL_MAX_10_EXP */ | |
123 | #ifdef FLT_MAX | |
124 | #define SCM_FLTMAX FLT_MAX | |
125 | #else | |
126 | #define SCM_FLTMAX 1e+23 | |
127 | #endif /* def FLT_MAX */ | |
128 | ||
0f2d19dd | 129 | |
0f2d19dd JB |
130 | /* SCM_INTBUFLEN is the maximum number of characters neccessary for the |
131 | * printed or scm_string representation of an exact immediate. | |
132 | */ | |
c014a02e | 133 | #define SCM_INTBUFLEN (5 + SCM_LONG_BIT) |
0f2d19dd JB |
134 | |
135 | \f | |
136 | ||
137 | /* Numbers | |
138 | */ | |
139 | ||
88eb6852 MD |
140 | #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real) |
141 | #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real) | |
142 | #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex) | |
3c9a524f DH |
143 | #define SCM_INEXACTP(x) (!SCM_IMP (x) && SCM_TYP16S (x) == scm_tc16_real) |
144 | #define SCM_REALP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_real) | |
145 | #define SCM_COMPLEXP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_complex) | |
88eb6852 | 146 | |
92c2555f MV |
147 | #define SCM_REAL_VALUE(x) (((scm_t_double *) SCM2PTR (x))->real) |
148 | #define SCM_COMPLEX_MEM(x) ((scm_t_complex *) SCM_CELL_WORD_1 (x)) | |
405aaef9 DH |
149 | #define SCM_COMPLEX_REAL(x) (SCM_COMPLEX_MEM (x)->real) |
150 | #define SCM_COMPLEX_IMAG(x) (SCM_COMPLEX_MEM (x)->imag) | |
0f2d19dd JB |
151 | |
152 | /* Define SCM_BIGDIG to an integer type whose size is smaller than long if | |
153 | * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG. | |
154 | * | |
155 | * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long. | |
156 | */ | |
157 | #ifdef BIGNUMS | |
158 | # ifdef _UNICOS | |
159 | # define SCM_DIGSTOOBIG | |
160 | # if (1L << 31) <= SCM_USHRT_MAX | |
161 | # define SCM_BIGDIG unsigned short | |
162 | # else | |
163 | # define SCM_BIGDIG unsigned int | |
164 | # endif /* (1L << 31) <= USHRT_MAX */ | |
165 | # define SCM_BITSPERDIG 32 | |
166 | # else | |
167 | # define SCM_BIGDIG unsigned short | |
168 | # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT) | |
169 | # endif /* def _UNICOS */ | |
170 | ||
171 | # define SCM_BIGRAD (1L << SCM_BITSPERDIG) | |
1be6b49c ML |
172 | # define SCM_DIGSPERLONG ((size_t)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG)) |
173 | # define SCM_I_BIGUP(type, x) ((type)(x) << SCM_BITSPERDIG) | |
174 | # define SCM_BIGUP(x) SCM_I_BIGUP (unsigned long, x) | |
175 | # define SCM_LONGLONGBIGUP(x) SCM_I_BIGUP (unsigned long long, x) | |
0f2d19dd JB |
176 | # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG) |
177 | # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1)) | |
178 | #endif /* def BIGNUMS */ | |
179 | ||
180 | #ifndef SCM_BIGDIG | |
181 | /* Definition is not really used but helps various function | |
182 | * prototypes to compile with conditionalization. | |
183 | */ | |
184 | # define SCM_BIGDIG unsigned short | |
0f2d19dd JB |
185 | #endif /* ndef SCM_BIGDIG */ |
186 | ||
7a710745 | 187 | #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x)) |
47457e8a | 188 | #define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob) |
7272f6d8 | 189 | #define SCM_BIGP(x) (!SCM_IMP (x) && (SCM_TYP16 (x) == scm_tc16_big)) |
88eb6852 MD |
190 | #define SCM_BIGSIGNFLAG 0x10000L |
191 | #define SCM_BIGSIZEFIELD 17 | |
47457e8a DH |
192 | #define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG) |
193 | #define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x))) | |
6a0476fd | 194 | #define SCM_SET_BIGNUM_BASE(n, b) (SCM_SET_CELL_WORD_1 ((n), (b))) |
c014a02e | 195 | #define SCM_NUMDIGS(x) ((size_t) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD)) |
760eb0ce MV |
196 | #define SCM_MAKE_BIGNUM_TAG(v, sign) \ |
197 | (scm_tc16_big \ | |
198 | | ((sign) ? SCM_BIGSIGNFLAG : 0) \ | |
199 | | (((v) + 0L) << SCM_BIGSIZEFIELD)) | |
88eb6852 | 200 | #define SCM_SETNUMDIGS(x, v, sign) \ |
760eb0ce | 201 | SCM_SET_CELL_WORD_0 (x, SCM_MAKE_BIGNUM_TAG (v, sign)) |
88eb6852 | 202 | |
0f2d19dd JB |
203 | \f |
204 | ||
92c2555f | 205 | typedef struct scm_t_double |
0f2d19dd JB |
206 | { |
207 | SCM type; | |
88eb6852 MD |
208 | SCM pad; |
209 | double real; | |
92c2555f | 210 | } scm_t_double; |
0f2d19dd | 211 | |
92c2555f | 212 | typedef struct scm_t_complex |
0f2d19dd | 213 | { |
88eb6852 MD |
214 | double real; |
215 | double imag; | |
92c2555f | 216 | } scm_t_complex; |
0f2d19dd | 217 | |
0f2d19dd | 218 | \f |
0f2d19dd | 219 | |
33b001fd MV |
220 | SCM_API SCM scm_exact_p (SCM x); |
221 | SCM_API SCM scm_odd_p (SCM n); | |
222 | SCM_API SCM scm_even_p (SCM n); | |
232898d9 MV |
223 | SCM_API SCM scm_inf_p (SCM n); |
224 | SCM_API SCM scm_nan_p (SCM n); | |
225 | SCM_API SCM scm_inf (void); | |
226 | SCM_API SCM scm_nan (void); | |
33b001fd MV |
227 | SCM_API SCM scm_abs (SCM x); |
228 | SCM_API SCM scm_quotient (SCM x, SCM y); | |
229 | SCM_API SCM scm_remainder (SCM x, SCM y); | |
230 | SCM_API SCM scm_modulo (SCM x, SCM y); | |
231 | SCM_API SCM scm_gcd (SCM x, SCM y); | |
232 | SCM_API SCM scm_lcm (SCM n1, SCM n2); | |
233 | SCM_API SCM scm_logand (SCM n1, SCM n2); | |
234 | SCM_API SCM scm_logior (SCM n1, SCM n2); | |
235 | SCM_API SCM scm_logxor (SCM n1, SCM n2); | |
236 | SCM_API SCM scm_logtest (SCM n1, SCM n2); | |
237 | SCM_API SCM scm_logbit_p (SCM n1, SCM n2); | |
238 | SCM_API SCM scm_lognot (SCM n); | |
239 | SCM_API SCM scm_integer_expt (SCM z1, SCM z2); | |
240 | SCM_API SCM scm_ash (SCM n, SCM cnt); | |
241 | SCM_API SCM scm_bit_extract (SCM n, SCM start, SCM end); | |
242 | SCM_API SCM scm_logcount (SCM n); | |
243 | SCM_API SCM scm_integer_length (SCM n); | |
244 | SCM_API SCM scm_i_mkbig (size_t nlen, int sign); | |
245 | SCM_API SCM scm_i_big2inum (SCM b, size_t l); | |
246 | SCM_API SCM scm_i_adjbig (SCM b, size_t nlen); | |
247 | SCM_API SCM scm_i_normbig (SCM b); | |
248 | SCM_API SCM scm_i_copybig (SCM b, int sign); | |
249 | SCM_API SCM scm_i_short2big (short n); | |
250 | SCM_API SCM scm_i_ushort2big (unsigned short n); | |
251 | SCM_API SCM scm_i_int2big (int n); | |
252 | SCM_API SCM scm_i_uint2big (unsigned int n); | |
253 | SCM_API SCM scm_i_long2big (long n); | |
254 | SCM_API SCM scm_i_ulong2big (unsigned long n); | |
255 | SCM_API SCM scm_i_size2big (size_t n); | |
256 | SCM_API SCM scm_i_ptrdiff2big (ptrdiff_t n); | |
1be6b49c | 257 | |
9ea8cdcb | 258 | #ifdef HAVE_LONG_LONGS |
33b001fd MV |
259 | SCM_API SCM scm_i_long_long2big (long long n); |
260 | SCM_API SCM scm_i_ulong_long2big (unsigned long long n); | |
9ea8cdcb | 261 | #endif |
1be6b49c | 262 | |
33b001fd MV |
263 | SCM_API int scm_bigcomp (SCM x, SCM y); |
264 | SCM_API long scm_pseudolong (long x); | |
265 | SCM_API void scm_longdigs (long x, SCM_BIGDIG digs[]); | |
266 | SCM_API SCM scm_addbig (SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy, int sgny); | |
267 | SCM_API SCM scm_mulbig (SCM_BIGDIG *x, size_t nx, SCM_BIGDIG *y, size_t ny, int sgn); | |
268 | SCM_API unsigned int scm_divbigdig (SCM_BIGDIG *ds, size_t h, SCM_BIGDIG div); | |
269 | SCM_API size_t scm_iint2str (long num, int rad, char *p); | |
270 | SCM_API SCM scm_number_to_string (SCM x, SCM radix); | |
271 | SCM_API int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate); | |
272 | SCM_API int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate); | |
273 | SCM_API int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate); | |
274 | SCM_API SCM scm_i_mem2number (const char *mem, size_t len, unsigned int radix); | |
275 | SCM_API SCM scm_string_to_number (SCM str, SCM radix); | |
276 | SCM_API SCM scm_make_real (double x); | |
277 | SCM_API SCM scm_make_complex (double x, double y); | |
278 | SCM_API SCM scm_bigequal (SCM x, SCM y); | |
279 | SCM_API SCM scm_real_equalp (SCM x, SCM y); | |
280 | SCM_API SCM scm_complex_equalp (SCM x, SCM y); | |
281 | SCM_API SCM scm_number_p (SCM x); | |
282 | SCM_API SCM scm_real_p (SCM x); | |
283 | SCM_API SCM scm_integer_p (SCM x); | |
284 | SCM_API SCM scm_inexact_p (SCM x); | |
285 | SCM_API SCM scm_num_eq_p (SCM x, SCM y); | |
286 | SCM_API SCM scm_less_p (SCM x, SCM y); | |
287 | SCM_API SCM scm_gr_p (SCM x, SCM y); | |
288 | SCM_API SCM scm_leq_p (SCM x, SCM y); | |
289 | SCM_API SCM scm_geq_p (SCM x, SCM y); | |
290 | SCM_API SCM scm_zero_p (SCM z); | |
291 | SCM_API SCM scm_positive_p (SCM x); | |
292 | SCM_API SCM scm_negative_p (SCM x); | |
293 | SCM_API SCM scm_max (SCM x, SCM y); | |
294 | SCM_API SCM scm_min (SCM x, SCM y); | |
295 | SCM_API SCM scm_sum (SCM x, SCM y); | |
296 | SCM_API SCM scm_difference (SCM x, SCM y); | |
297 | SCM_API SCM scm_product (SCM x, SCM y); | |
298 | SCM_API double scm_num2dbl (SCM a, const char * why); | |
299 | SCM_API SCM scm_divide (SCM x, SCM y); | |
300 | SCM_API double scm_asinh (double x); | |
301 | SCM_API double scm_acosh (double x); | |
302 | SCM_API double scm_atanh (double x); | |
303 | SCM_API double scm_truncate (double x); | |
304 | SCM_API double scm_round (double x); | |
305 | SCM_API SCM scm_sys_expt (SCM z1, SCM z2); | |
306 | SCM_API SCM scm_sys_atan2 (SCM z1, SCM z2); | |
307 | SCM_API SCM scm_make_rectangular (SCM z1, SCM z2); | |
308 | SCM_API SCM scm_make_polar (SCM z1, SCM z2); | |
309 | SCM_API SCM scm_real_part (SCM z); | |
310 | SCM_API SCM scm_imag_part (SCM z); | |
311 | SCM_API SCM scm_magnitude (SCM z); | |
312 | SCM_API SCM scm_angle (SCM z); | |
313 | SCM_API SCM scm_exact_to_inexact (SCM z); | |
314 | SCM_API SCM scm_inexact_to_exact (SCM z); | |
315 | SCM_API SCM scm_trunc (SCM x); | |
316 | SCM_API SCM scm_i_dbl2big (double d); | |
317 | SCM_API double scm_i_big2dbl (SCM b); | |
318 | ||
319 | SCM_API SCM scm_short2num (short n); | |
320 | SCM_API SCM scm_ushort2num (unsigned short n); | |
321 | SCM_API SCM scm_int2num (int n); | |
322 | SCM_API SCM scm_uint2num (unsigned int n); | |
323 | SCM_API SCM scm_long2num (long n); | |
324 | SCM_API SCM scm_ulong2num (unsigned long n); | |
325 | SCM_API SCM scm_size2num (size_t n); | |
326 | SCM_API SCM scm_ptrdiff2num (ptrdiff_t n); | |
327 | SCM_API short scm_num2short (SCM num, unsigned long int pos, | |
328 | const char *s_caller); | |
329 | SCM_API unsigned short scm_num2ushort (SCM num, unsigned long int pos, | |
330 | const char *s_caller); | |
331 | SCM_API int scm_num2int (SCM num, unsigned long int pos, | |
332 | const char *s_caller); | |
333 | SCM_API unsigned int scm_num2uint (SCM num, unsigned long int pos, | |
334 | const char *s_caller); | |
335 | SCM_API long scm_num2long (SCM num, unsigned long int pos, | |
336 | const char *s_caller); | |
337 | SCM_API unsigned long scm_num2ulong (SCM num, unsigned long int pos, | |
338 | const char *s_caller); | |
339 | SCM_API ptrdiff_t scm_num2ptrdiff (SCM num, unsigned long int pos, | |
340 | const char *s_caller); | |
341 | SCM_API size_t scm_num2size (SCM num, unsigned long int pos, | |
342 | const char *s_caller); | |
9ea8cdcb | 343 | #ifdef HAVE_LONG_LONGS |
33b001fd MV |
344 | SCM_API SCM scm_long_long2num (long long sl); |
345 | SCM_API SCM scm_ulong_long2num (unsigned long long sl); | |
346 | SCM_API long long scm_num2long_long (SCM num, unsigned long int pos, | |
347 | const char *s_caller); | |
348 | SCM_API unsigned long long scm_num2ulong_long (SCM num, unsigned long int pos, | |
349 | const char *s_caller); | |
9ea8cdcb | 350 | #endif |
1be6b49c | 351 | |
33b001fd MV |
352 | SCM_API SCM scm_float2num (float n); |
353 | SCM_API SCM scm_double2num (double n); | |
354 | SCM_API float scm_num2float (SCM num, unsigned long int pos, | |
355 | const char *s_caller); | |
356 | SCM_API double scm_num2double (SCM num, unsigned long int pos, | |
357 | const char *s_caller); | |
581ded70 | 358 | |
b4e15479 SJ |
359 | #ifdef GUILE_DEBUG |
360 | SCM_API SCM scm_sys_check_number_conversions (void); | |
361 | #endif | |
362 | ||
33b001fd | 363 | SCM_API void scm_init_numbers (void); |
0f2d19dd | 364 | |
3c9a524f | 365 | #endif /* SCM_NUMBERS_H */ |
89e00824 ML |
366 | |
367 | /* | |
368 | Local Variables: | |
369 | c-file-style: "gnu" | |
370 | End: | |
371 | */ |