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 | ||
0f2d19dd JB |
52 | \f |
53 | ||
0f2d19dd JB |
54 | /* Immediate Numbers |
55 | * | |
56 | * Inums are exact integer data that fits within an SCM word. | |
57 | * | |
58 | * SCM_INUMP applies only to values known to be Scheme objects. | |
59 | * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known | |
3c9a524f | 60 | * to be a SCM_CONSP. If x is only known to be a non-immediate, |
0f2d19dd JB |
61 | * SCM_INUMP (SCM_CAR (x)) can give wrong answers. |
62 | */ | |
63 | ||
c014a02e | 64 | #define SCM_I_FIXNUM_BIT (SCM_LONG_BIT - 2) |
1be6b49c | 65 | #define SCM_MOST_POSITIVE_FIXNUM ((1L << (SCM_I_FIXNUM_BIT - 1)) - 1) |
5c75b29f DH |
66 | #define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM - 1) |
67 | ||
68 | ||
56100716 DH |
69 | /* SCM_SRS is signed right shift */ |
70 | #if (-1 == (((-1) << 2) + 2) >> 2) | |
92c2555f | 71 | # define SCM_SRS(x, y) ((scm_t_signed_bits)(x) >> (y)) |
0f2d19dd | 72 | #else |
56100716 DH |
73 | # define SCM_SRS(x, y) ((SCM_UNPACK (x) < 0) ? ~((~SCM_UNPACK (x)) >> (y)) : (SCM_UNPACK (x) >> (y))) |
74 | #endif /* (-1 == (((-1) << 2) + 2) >> 2) */ | |
0f2d19dd JB |
75 | |
76 | ||
56100716 DH |
77 | #define SCM_INUMP(x) (2 & SCM_UNPACK (x)) |
78 | #define SCM_NINUMP(x) (!SCM_INUMP (x)) | |
79 | #define SCM_MAKINUM(x) (SCM_PACK (((x) << 2) + 2L)) | |
92c2555f | 80 | #define SCM_INUM(x) ((scm_t_signed_bits)(SCM_SRS (SCM_UNPACK (x), 2))) |
0f2d19dd JB |
81 | |
82 | ||
894a712b DH |
83 | /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */ |
84 | #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM) | |
85 | #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM) | |
86 | #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n)) | |
87 | ||
88 | ||
56100716 | 89 | /* A name for 0. */ |
b82c6ce0 | 90 | #define SCM_INUM0 (SCM_MAKINUM (0)) |
0f2d19dd JB |
91 | |
92 | ||
88eb6852 MD |
93 | /* SCM_MAXEXP is the maximum double precision expontent |
94 | * SCM_FLTMAX is less than or scm_equal the largest single precision float | |
95 | */ | |
96 | ||
97 | #ifdef STDC_HEADERS | |
98 | #ifndef GO32 | |
99 | #include <float.h> | |
100 | #endif /* ndef GO32 */ | |
101 | #endif /* def STDC_HEADERS */ | |
102 | #ifdef DBL_MAX_10_EXP | |
103 | #define SCM_MAXEXP DBL_MAX_10_EXP | |
104 | #else | |
105 | #define SCM_MAXEXP 308 /* IEEE doubles */ | |
106 | #endif /* def DBL_MAX_10_EXP */ | |
107 | #ifdef FLT_MAX | |
108 | #define SCM_FLTMAX FLT_MAX | |
109 | #else | |
110 | #define SCM_FLTMAX 1e+23 | |
111 | #endif /* def FLT_MAX */ | |
112 | ||
0f2d19dd | 113 | |
0f2d19dd JB |
114 | /* SCM_INTBUFLEN is the maximum number of characters neccessary for the |
115 | * printed or scm_string representation of an exact immediate. | |
116 | */ | |
c014a02e | 117 | #define SCM_INTBUFLEN (5 + SCM_LONG_BIT) |
0f2d19dd JB |
118 | |
119 | \f | |
120 | ||
121 | /* Numbers | |
122 | */ | |
123 | ||
88eb6852 MD |
124 | #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real) |
125 | #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real) | |
126 | #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex) | |
3c9a524f DH |
127 | #define SCM_INEXACTP(x) (!SCM_IMP (x) && SCM_TYP16S (x) == scm_tc16_real) |
128 | #define SCM_REALP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_real) | |
129 | #define SCM_COMPLEXP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_complex) | |
88eb6852 | 130 | |
92c2555f MV |
131 | #define SCM_REAL_VALUE(x) (((scm_t_double *) SCM2PTR (x))->real) |
132 | #define SCM_COMPLEX_MEM(x) ((scm_t_complex *) SCM_CELL_WORD_1 (x)) | |
405aaef9 DH |
133 | #define SCM_COMPLEX_REAL(x) (SCM_COMPLEX_MEM (x)->real) |
134 | #define SCM_COMPLEX_IMAG(x) (SCM_COMPLEX_MEM (x)->imag) | |
0f2d19dd JB |
135 | |
136 | /* Define SCM_BIGDIG to an integer type whose size is smaller than long if | |
137 | * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG. | |
138 | * | |
139 | * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long. | |
140 | */ | |
141 | #ifdef BIGNUMS | |
142 | # ifdef _UNICOS | |
143 | # define SCM_DIGSTOOBIG | |
144 | # if (1L << 31) <= SCM_USHRT_MAX | |
145 | # define SCM_BIGDIG unsigned short | |
146 | # else | |
147 | # define SCM_BIGDIG unsigned int | |
148 | # endif /* (1L << 31) <= USHRT_MAX */ | |
149 | # define SCM_BITSPERDIG 32 | |
150 | # else | |
151 | # define SCM_BIGDIG unsigned short | |
152 | # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT) | |
153 | # endif /* def _UNICOS */ | |
154 | ||
155 | # define SCM_BIGRAD (1L << SCM_BITSPERDIG) | |
1be6b49c ML |
156 | # define SCM_DIGSPERLONG ((size_t)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG)) |
157 | # define SCM_I_BIGUP(type, x) ((type)(x) << SCM_BITSPERDIG) | |
158 | # define SCM_BIGUP(x) SCM_I_BIGUP (unsigned long, x) | |
159 | # define SCM_LONGLONGBIGUP(x) SCM_I_BIGUP (unsigned long long, x) | |
0f2d19dd JB |
160 | # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG) |
161 | # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1)) | |
162 | #endif /* def BIGNUMS */ | |
163 | ||
164 | #ifndef SCM_BIGDIG | |
165 | /* Definition is not really used but helps various function | |
166 | * prototypes to compile with conditionalization. | |
167 | */ | |
168 | # define SCM_BIGDIG unsigned short | |
0f2d19dd JB |
169 | #endif /* ndef SCM_BIGDIG */ |
170 | ||
7a710745 | 171 | #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x)) |
47457e8a | 172 | #define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob) |
7272f6d8 | 173 | #define SCM_BIGP(x) (!SCM_IMP (x) && (SCM_TYP16 (x) == scm_tc16_big)) |
88eb6852 MD |
174 | #define SCM_BIGSIGNFLAG 0x10000L |
175 | #define SCM_BIGSIZEFIELD 17 | |
47457e8a DH |
176 | #define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG) |
177 | #define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x))) | |
6a0476fd | 178 | #define SCM_SET_BIGNUM_BASE(n, b) (SCM_SET_CELL_WORD_1 ((n), (b))) |
c014a02e | 179 | #define SCM_NUMDIGS(x) ((size_t) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD)) |
88eb6852 | 180 | #define SCM_SETNUMDIGS(x, v, sign) \ |
4260a7fc | 181 | SCM_SET_CELL_WORD_0 (x, \ |
88eb6852 MD |
182 | scm_tc16_big \ |
183 | | ((sign) ? SCM_BIGSIGNFLAG : 0) \ | |
f81e080b | 184 | | (((v) + 0L) << SCM_BIGSIZEFIELD)) |
88eb6852 | 185 | |
0f2d19dd JB |
186 | \f |
187 | ||
92c2555f | 188 | typedef struct scm_t_double |
0f2d19dd JB |
189 | { |
190 | SCM type; | |
88eb6852 MD |
191 | SCM pad; |
192 | double real; | |
92c2555f | 193 | } scm_t_double; |
0f2d19dd | 194 | |
92c2555f | 195 | typedef struct scm_t_complex |
0f2d19dd | 196 | { |
88eb6852 MD |
197 | double real; |
198 | double imag; | |
92c2555f | 199 | } scm_t_complex; |
0f2d19dd | 200 | |
0f2d19dd | 201 | \f |
0f2d19dd | 202 | |
7866a09b GB |
203 | extern SCM scm_exact_p (SCM x); |
204 | extern SCM scm_odd_p (SCM n); | |
205 | extern SCM scm_even_p (SCM n); | |
206 | extern SCM scm_abs (SCM x); | |
207 | extern SCM scm_quotient (SCM x, SCM y); | |
208 | extern SCM scm_remainder (SCM x, SCM y); | |
209 | extern SCM scm_modulo (SCM x, SCM y); | |
210 | extern SCM scm_gcd (SCM x, SCM y); | |
211 | extern SCM scm_lcm (SCM n1, SCM n2); | |
212 | extern SCM scm_logand (SCM n1, SCM n2); | |
213 | extern SCM scm_logior (SCM n1, SCM n2); | |
214 | extern SCM scm_logxor (SCM n1, SCM n2); | |
215 | extern SCM scm_logtest (SCM n1, SCM n2); | |
216 | extern SCM scm_logbit_p (SCM n1, SCM n2); | |
217 | extern SCM scm_lognot (SCM n); | |
218 | extern SCM scm_integer_expt (SCM z1, SCM z2); | |
219 | extern SCM scm_ash (SCM n, SCM cnt); | |
220 | extern SCM scm_bit_extract (SCM n, SCM start, SCM end); | |
221 | extern SCM scm_logcount (SCM n); | |
222 | extern SCM scm_integer_length (SCM n); | |
1be6b49c ML |
223 | extern SCM scm_i_mkbig (size_t nlen, int sign); |
224 | extern SCM scm_i_big2inum (SCM b, size_t l); | |
225 | extern SCM scm_i_adjbig (SCM b, size_t nlen); | |
226 | extern SCM scm_i_normbig (SCM b); | |
227 | extern SCM scm_i_copybig (SCM b, int sign); | |
228 | extern SCM scm_i_short2big (short n); | |
229 | extern SCM scm_i_ushort2big (unsigned short n); | |
230 | extern SCM scm_i_int2big (int n); | |
231 | extern SCM scm_i_uint2big (unsigned int n); | |
232 | extern SCM scm_i_long2big (long n); | |
233 | extern SCM scm_i_ulong2big (unsigned long n); | |
1be6b49c ML |
234 | extern SCM scm_i_size2big (size_t n); |
235 | extern SCM scm_i_ptrdiff2big (ptrdiff_t n); | |
236 | ||
9ea8cdcb | 237 | #ifdef HAVE_LONG_LONGS |
1be6b49c ML |
238 | extern SCM scm_i_long_long2big (long long n); |
239 | extern SCM scm_i_ulong_long2big (unsigned long long n); | |
9ea8cdcb | 240 | #endif |
1be6b49c | 241 | |
7866a09b GB |
242 | extern int scm_bigcomp (SCM x, SCM y); |
243 | extern long scm_pseudolong (long x); | |
244 | extern void scm_longdigs (long x, SCM_BIGDIG digs[]); | |
1be6b49c ML |
245 | extern SCM scm_addbig (SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy, int sgny); |
246 | extern SCM scm_mulbig (SCM_BIGDIG *x, size_t nx, SCM_BIGDIG *y, size_t ny, int sgn); | |
247 | extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, size_t h, SCM_BIGDIG div); | |
248 | extern size_t scm_iint2str (long num, int rad, char *p); | |
7866a09b | 249 | extern SCM scm_number_to_string (SCM x, SCM radix); |
88eb6852 MD |
250 | extern int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate); |
251 | extern int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate); | |
7866a09b | 252 | extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate); |
3c9a524f | 253 | extern SCM scm_i_mem2number (const char *mem, size_t len, unsigned int radix); |
7866a09b | 254 | extern SCM scm_string_to_number (SCM str, SCM radix); |
88eb6852 MD |
255 | extern SCM scm_make_real (double x); |
256 | extern SCM scm_make_complex (double x, double y); | |
7866a09b | 257 | extern SCM scm_bigequal (SCM x, SCM y); |
88eb6852 MD |
258 | extern SCM scm_real_equalp (SCM x, SCM y); |
259 | extern SCM scm_complex_equalp (SCM x, SCM y); | |
7866a09b GB |
260 | extern SCM scm_number_p (SCM x); |
261 | extern SCM scm_real_p (SCM x); | |
262 | extern SCM scm_integer_p (SCM x); | |
263 | extern SCM scm_inexact_p (SCM x); | |
264 | extern SCM scm_num_eq_p (SCM x, SCM y); | |
265 | extern SCM scm_less_p (SCM x, SCM y); | |
266 | extern SCM scm_gr_p (SCM x, SCM y); | |
267 | extern SCM scm_leq_p (SCM x, SCM y); | |
268 | extern SCM scm_geq_p (SCM x, SCM y); | |
269 | extern SCM scm_zero_p (SCM z); | |
270 | extern SCM scm_positive_p (SCM x); | |
271 | extern SCM scm_negative_p (SCM x); | |
272 | extern SCM scm_max (SCM x, SCM y); | |
273 | extern SCM scm_min (SCM x, SCM y); | |
274 | extern SCM scm_sum (SCM x, SCM y); | |
275 | extern SCM scm_difference (SCM x, SCM y); | |
276 | extern SCM scm_product (SCM x, SCM y); | |
277 | extern double scm_num2dbl (SCM a, const char * why); | |
278 | extern SCM scm_divide (SCM x, SCM y); | |
279 | extern double scm_asinh (double x); | |
280 | extern double scm_acosh (double x); | |
281 | extern double scm_atanh (double x); | |
282 | extern double scm_truncate (double x); | |
283 | extern double scm_round (double x); | |
7866a09b GB |
284 | extern SCM scm_sys_expt (SCM z1, SCM z2); |
285 | extern SCM scm_sys_atan2 (SCM z1, SCM z2); | |
286 | extern SCM scm_make_rectangular (SCM z1, SCM z2); | |
287 | extern SCM scm_make_polar (SCM z1, SCM z2); | |
288 | extern SCM scm_real_part (SCM z); | |
289 | extern SCM scm_imag_part (SCM z); | |
290 | extern SCM scm_magnitude (SCM z); | |
291 | extern SCM scm_angle (SCM z); | |
3c9a524f | 292 | extern SCM scm_exact_to_inexact (SCM z); |
7866a09b GB |
293 | extern SCM scm_inexact_to_exact (SCM z); |
294 | extern SCM scm_trunc (SCM x); | |
1be6b49c | 295 | extern SCM scm_i_dbl2big (double d); |
1be6b49c ML |
296 | extern double scm_i_big2dbl (SCM b); |
297 | ||
1be6b49c ML |
298 | extern SCM scm_short2num (short n); |
299 | extern SCM scm_ushort2num (unsigned short n); | |
300 | extern SCM scm_int2num (int n); | |
301 | extern SCM scm_uint2num (unsigned int n); | |
302 | extern SCM scm_long2num (long n); | |
303 | extern SCM scm_ulong2num (unsigned long n); | |
1be6b49c ML |
304 | extern SCM scm_size2num (size_t n); |
305 | extern SCM scm_ptrdiff2num (ptrdiff_t n); | |
306 | extern short scm_num2short (SCM num, unsigned long int pos, | |
307 | const char *s_caller); | |
308 | extern unsigned short scm_num2ushort (SCM num, unsigned long int pos, | |
309 | const char *s_caller); | |
310 | extern int scm_num2int (SCM num, unsigned long int pos, | |
311 | const char *s_caller); | |
312 | extern unsigned int scm_num2uint (SCM num, unsigned long int pos, | |
313 | const char *s_caller); | |
e4b265d8 DH |
314 | extern long scm_num2long (SCM num, unsigned long int pos, |
315 | const char *s_caller); | |
1be6b49c ML |
316 | extern unsigned long scm_num2ulong (SCM num, unsigned long int pos, |
317 | const char *s_caller); | |
1be6b49c ML |
318 | extern ptrdiff_t scm_num2ptrdiff (SCM num, unsigned long int pos, |
319 | const char *s_caller); | |
320 | extern size_t scm_num2size (SCM num, unsigned long int pos, | |
321 | const char *s_caller); | |
9ea8cdcb | 322 | #ifdef HAVE_LONG_LONGS |
1be6b49c ML |
323 | extern SCM scm_long_long2num (long long sl); |
324 | extern SCM scm_ulong_long2num (unsigned long long sl); | |
325 | extern long long scm_num2long_long (SCM num, unsigned long int pos, | |
7866a09b | 326 | const char *s_caller); |
1be6b49c | 327 | extern unsigned long long scm_num2ulong_long (SCM num, unsigned long int pos, |
581ded70 | 328 | const char *s_caller); |
9ea8cdcb | 329 | #endif |
1be6b49c | 330 | |
581ded70 MD |
331 | extern SCM scm_float2num (float n); |
332 | extern SCM scm_double2num (double n); | |
333 | extern float scm_num2float (SCM num, unsigned long int pos, | |
334 | const char *s_caller); | |
335 | extern double scm_num2double (SCM num, unsigned long int pos, | |
336 | const char *s_caller); | |
337 | ||
7866a09b | 338 | extern void scm_init_numbers (void); |
0f2d19dd | 339 | |
3c9a524f | 340 | #endif /* SCM_NUMBERS_H */ |
89e00824 ML |
341 | |
342 | /* | |
343 | Local Variables: | |
344 | c-file-style: "gnu" | |
345 | End: | |
346 | */ |