*** empty log message ***
[bpt/guile.git] / libguile / numbers.h
1 /* classes: h_files */
2
3 #ifndef NUMBERSH
4 #define NUMBERSH
5 /* Copyright (C) 1995, 1996, 1998, 2000 Free Software Foundation, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this software; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
21 *
22 * As a special exception, the Free Software Foundation gives permission
23 * for additional uses of the text contained in its release of GUILE.
24 *
25 * The exception is that, if you link the GUILE library with other files
26 * to produce an executable, this does not by itself cause the
27 * resulting executable to be covered by the GNU General Public License.
28 * Your use of that executable is in no way restricted on account of
29 * linking the GUILE library code into it.
30 *
31 * This exception does not however invalidate any other reasons why
32 * the executable file might be covered by the GNU General Public License.
33 *
34 * This exception applies only to the code released by the
35 * Free Software Foundation under the name GUILE. If you copy
36 * code from other Free Software Foundation releases into a copy of
37 * GUILE, as the General Public License permits, the exception does
38 * not apply to the code that you add in this way. To avoid misleading
39 * anyone as to the status of such modified files, you must delete
40 * this exception notice from them.
41 *
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
44 * If you do not wish that, delete this exception notice. */
45
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 \f
49
50 #include "libguile/__scm.h"
51 #include "libguile/print.h"
52
53 \f
54
55
56 /* Immediate Numbers
57 *
58 * Inums are exact integer data that fits within an SCM word.
59 *
60 * SCM_INUMP applies only to values known to be Scheme objects.
61 * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known
62 * to be a SCM_CONSP. If x is only known to be a SCM_NIMP,
63 * SCM_INUMP (SCM_CAR (x)) can give wrong answers.
64 */
65
66 /* SCM_SRS is signed right shift */
67 #if (-1 == (((-1) << 2) + 2) >> 2)
68 # define SCM_SRS(x, y) ((x) >> (y))
69 #else
70 # define SCM_SRS(x, y) ((SCM_UNPACK (x) < 0) ? ~((~SCM_UNPACK (x)) >> (y)) : (SCM_UNPACK (x) >> (y)))
71 #endif /* (-1 == (((-1) << 2) + 2) >> 2) */
72
73
74 #define SCM_INUMP(x) (2 & SCM_UNPACK (x))
75 #define SCM_NINUMP(x) (!SCM_INUMP (x))
76 #define SCM_MAKINUM(x) (SCM_PACK (((x) << 2) + 2L))
77 #define SCM_INUM(x) (SCM_SRS (SCM_UNPACK (x), 2))
78
79
80 /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */
81 #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
82 #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
83 #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
84
85
86 /* A name for 0. */
87 #define SCM_INUM0 (SCM_MAKINUM (0))
88
89
90 /* SCM_MAXEXP is the maximum double precision expontent
91 * SCM_FLTMAX is less than or scm_equal the largest single precision float
92 */
93
94 #ifdef STDC_HEADERS
95 #ifndef GO32
96 #include <float.h>
97 #endif /* ndef GO32 */
98 #endif /* def STDC_HEADERS */
99 #ifdef DBL_MAX_10_EXP
100 #define SCM_MAXEXP DBL_MAX_10_EXP
101 #else
102 #define SCM_MAXEXP 308 /* IEEE doubles */
103 #endif /* def DBL_MAX_10_EXP */
104 #ifdef FLT_MAX
105 #define SCM_FLTMAX FLT_MAX
106 #else
107 #define SCM_FLTMAX 1e+23
108 #endif /* def FLT_MAX */
109
110
111 /* SCM_INTBUFLEN is the maximum number of characters neccessary for the
112 * printed or scm_string representation of an exact immediate.
113 */
114
115 #ifndef SCM_CHAR_BIT
116 # define SCM_CHAR_BIT 8
117 #endif /* ndef SCM_CHAR_BIT */
118 #ifndef SCM_LONG_BIT
119 # define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
120 #endif /* ndef SCM_LONG_BIT */
121 #define SCM_INTBUFLEN (5+SCM_LONG_BIT)
122
123
124 \f
125
126 /* Numbers
127 */
128
129 #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real)
130 #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real)
131 #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex)
132 #define SCM_INEXACTP(x) (SCM_NIMP (x) && SCM_TYP16S (x) == scm_tc16_real)
133 #define SCM_REALP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_real)
134 #define SCM_COMPLEXP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
135
136 #define SCM_REAL_VALUE(x) (((scm_double_t *) SCM2PTR (x))->real)
137 #define SCM_COMPLEX_REAL(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->real)
138 #define SCM_COMPLEX_IMAG(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->imag)
139
140 /* Define SCM_BIGDIG to an integer type whose size is smaller than long if
141 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
142 *
143 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
144 */
145 #ifdef BIGNUMS
146 # ifdef _UNICOS
147 # define SCM_DIGSTOOBIG
148 # if (1L << 31) <= SCM_USHRT_MAX
149 # define SCM_BIGDIG unsigned short
150 # else
151 # define SCM_BIGDIG unsigned int
152 # endif /* (1L << 31) <= USHRT_MAX */
153 # define SCM_BITSPERDIG 32
154 # else
155 # define SCM_BIGDIG unsigned short
156 # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
157 # endif /* def _UNICOS */
158
159 # define SCM_BIGRAD (1L << SCM_BITSPERDIG)
160 # define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
161 # define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
162 # define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
163 # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
164 # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
165 #endif /* def BIGNUMS */
166
167 #ifndef SCM_BIGDIG
168 /* Definition is not really used but helps various function
169 * prototypes to compile with conditionalization.
170 */
171 # define SCM_BIGDIG unsigned short
172 #endif /* ndef SCM_BIGDIG */
173
174 #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
175 #define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob)
176 #define SCM_BIGP(x) (!SCM_IMP (x) && (SCM_TYP16 (x) == scm_tc16_big))
177 #define SCM_BIGSIGNFLAG 0x10000L
178 #define SCM_BIGSIZEFIELD 17
179 #define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG)
180 #define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x)))
181 #define SCM_NUMDIGS(x) ((scm_sizet) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD))
182 #define SCM_SETNUMDIGS(x, v, sign) \
183 SCM_SET_CELL_WORD_0 (x, \
184 scm_tc16_big \
185 | ((sign) ? SCM_BIGSIGNFLAG : 0) \
186 | (((v) + 0L) << SCM_BIGSIZEFIELD))
187
188 \f
189
190 typedef struct scm_double_t
191 {
192 SCM type;
193 SCM pad;
194 double real;
195 } scm_double_t;
196
197 typedef struct scm_complex_t
198 {
199 double real;
200 double imag;
201 } scm_complex_t;
202
203 \f
204
205 extern SCM scm_exact_p (SCM x);
206 extern SCM scm_odd_p (SCM n);
207 extern SCM scm_even_p (SCM n);
208 extern SCM scm_abs (SCM x);
209 extern SCM scm_quotient (SCM x, SCM y);
210 extern SCM scm_remainder (SCM x, SCM y);
211 extern SCM scm_modulo (SCM x, SCM y);
212 extern SCM scm_gcd (SCM x, SCM y);
213 extern SCM scm_lcm (SCM n1, SCM n2);
214 extern SCM scm_logand (SCM n1, SCM n2);
215 extern SCM scm_logior (SCM n1, SCM n2);
216 extern SCM scm_logxor (SCM n1, SCM n2);
217 extern SCM scm_logtest (SCM n1, SCM n2);
218 extern SCM scm_logbit_p (SCM n1, SCM n2);
219 extern SCM scm_lognot (SCM n);
220 extern SCM scm_integer_expt (SCM z1, SCM z2);
221 extern SCM scm_ash (SCM n, SCM cnt);
222 extern SCM scm_bit_extract (SCM n, SCM start, SCM end);
223 extern SCM scm_logcount (SCM n);
224 extern SCM scm_integer_length (SCM n);
225 extern SCM scm_mkbig (scm_sizet nlen, int sign);
226 extern SCM scm_big2inum (SCM b, scm_sizet l);
227 extern SCM scm_adjbig (SCM b, scm_sizet nlen);
228 extern SCM scm_normbig (SCM b);
229 extern SCM scm_copybig (SCM b, int sign);
230 extern SCM scm_long2big (long n);
231 #ifdef HAVE_LONG_LONGS
232 extern SCM scm_long_long2big (long_long n);
233 #endif
234 extern SCM scm_2ulong2big (unsigned long * np);
235 extern SCM scm_ulong2big (unsigned long n);
236 extern int scm_bigcomp (SCM x, SCM y);
237 extern long scm_pseudolong (long x);
238 extern void scm_longdigs (long x, SCM_BIGDIG digs[]);
239 extern SCM scm_addbig (SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny);
240 extern SCM scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn);
241 extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div);
242 extern scm_sizet scm_iint2str (long num, int rad, char *p);
243 extern SCM scm_number_to_string (SCM x, SCM radix);
244 extern int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
245 extern int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
246 extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
247 extern SCM scm_istr2int (char *str, long len, long radix);
248 extern SCM scm_istr2flo (char *str, long len, long radix);
249 extern SCM scm_istring2number (char *str, long len, long radix);
250 extern SCM scm_string_to_number (SCM str, SCM radix);
251 extern SCM scm_make_real (double x);
252 extern SCM scm_make_complex (double x, double y);
253 extern SCM scm_bigequal (SCM x, SCM y);
254 extern SCM scm_real_equalp (SCM x, SCM y);
255 extern SCM scm_complex_equalp (SCM x, SCM y);
256 extern SCM scm_number_p (SCM x);
257 extern SCM scm_real_p (SCM x);
258 extern SCM scm_integer_p (SCM x);
259 extern SCM scm_inexact_p (SCM x);
260 extern SCM scm_num_eq_p (SCM x, SCM y);
261 extern SCM scm_less_p (SCM x, SCM y);
262 extern SCM scm_gr_p (SCM x, SCM y);
263 extern SCM scm_leq_p (SCM x, SCM y);
264 extern SCM scm_geq_p (SCM x, SCM y);
265 extern SCM scm_zero_p (SCM z);
266 extern SCM scm_positive_p (SCM x);
267 extern SCM scm_negative_p (SCM x);
268 extern SCM scm_max (SCM x, SCM y);
269 extern SCM scm_min (SCM x, SCM y);
270 extern SCM scm_sum (SCM x, SCM y);
271 extern SCM scm_difference (SCM x, SCM y);
272 extern SCM scm_product (SCM x, SCM y);
273 extern double scm_num2dbl (SCM a, const char * why);
274 extern SCM scm_divide (SCM x, SCM y);
275 extern double scm_asinh (double x);
276 extern double scm_acosh (double x);
277 extern double scm_atanh (double x);
278 extern double scm_truncate (double x);
279 extern double scm_round (double x);
280 extern double scm_exact_to_inexact (double z);
281 extern SCM scm_sys_expt (SCM z1, SCM z2);
282 extern SCM scm_sys_atan2 (SCM z1, SCM z2);
283 extern SCM scm_make_rectangular (SCM z1, SCM z2);
284 extern SCM scm_make_polar (SCM z1, SCM z2);
285 extern SCM scm_real_part (SCM z);
286 extern SCM scm_imag_part (SCM z);
287 extern SCM scm_magnitude (SCM z);
288 extern SCM scm_angle (SCM z);
289 extern SCM scm_inexact_to_exact (SCM z);
290 extern SCM scm_trunc (SCM x);
291 extern SCM scm_dbl2big (double d);
292 extern double scm_big2dbl (SCM b);
293 extern SCM scm_long2num (long sl);
294 extern SCM scm_ulong2num (unsigned long sl);
295 extern long scm_num2long (SCM num, char *pos, const char *s_caller);
296 #ifdef HAVE_LONG_LONGS
297 extern SCM scm_long_long2num (long_long sl);
298 extern long_long scm_num2long_long (SCM num, char *pos,
299 const char *s_caller);
300 #endif
301 extern unsigned long scm_num2ulong (SCM num, char *pos,
302 const char *s_caller);
303 extern void scm_init_numbers (void);
304
305 \f
306
307 #if (SCM_DEBUG_DEPRECATED == 0)
308
309 typedef struct scm_dblproc
310 {
311 char *scm_string;
312 double (*cproc) ();
313 } scm_dblproc;
314
315 #define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
316 #define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
317 #define SCM_INEXP(x) SCM_INEXACTP(x)
318 #define SCM_CPLXP(x) SCM_COMPLEXP(x)
319 #define SCM_REAL(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
320 #define SCM_IMAG(x) (SCM_SLOPPY_REALP (x) ? 0.0 : SCM_COMPLEX_IMAG (x))
321 #define SCM_REALPART(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
322 #define scm_makdbl scm_make_complex
323 #define SCM_SINGP(x) 0
324 #define SCM_NUM2DBL(x) scm_num2dbl(x, "SCM_NUM2DBL")
325
326 #ifndef SCM_BIGDIG
327 # define SCM_NO_BIGDIG
328 #endif
329
330 #endif /* SCM_DEBUG_DEPRECATED == 0 */
331
332 #endif /* NUMBERSH */
333
334 /*
335 Local Variables:
336 c-file-style: "gnu"
337 End:
338 */