* scm_divbigbig and scm_divbigint are static now
[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
52 #include "libguile/print.h"
53
54 \f
55
56
57 /* Immediate Numbers
58 *
59 * Inums are exact integer data that fits within an SCM word.
60 *
61 * SCM_INUMP applies only to values known to be Scheme objects.
62 * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known
63 * to be a SCM_CONSP. If x is only known to be a SCM_NIMP,
64 * SCM_INUMP (SCM_CAR (x)) can give wrong answers.
65 */
66
67 #define SCM_INUMP(x) (2 & SCM_UNPACK (x))
68 #define SCM_NINUMP(x) (!SCM_INUMP (x))
69
70 #ifdef __TURBOC__
71 /* shifts of more than one are done by a library call, single shifts are
72 * performed in registers
73 */
74 # define SCM_MAKINUM(x) (SCM_PACK ((((x) << 1) << 1) + 2L))
75 #else
76 # define SCM_MAKINUM(x) (SCM_PACK (((x) << 2) + 2L))
77 #endif /* def __TURBOC__ */
78
79
80 /* SCM_SRS is signed right shift */
81 /* SCM_INUM makes a C int from an SCM immediate number. */
82 /* Turbo C++ v1.0 has a bug with right shifts of signed longs!
83 * It is believed to be fixed in Turbo C++ v1.01
84 */
85 #if (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295)
86 # define SCM_SRS(x, y) ((x) >> y)
87 # ifdef __TURBOC__
88 # define SCM_INUM(x) ((SCM_UNPACK (x) >>1) >>1)
89 # else
90 # define SCM_INUM(x) SCM_SRS (SCM_UNPACK (x), 2)
91 # endif /* def __TURBOC__ */
92 #else
93 # define SCM_SRS(x, y)\
94 ((SCM_UNPACK (x) < 0) ? ~( (~SCM_UNPACK (x)) >> y) : (SCM_UNPACK (x) >> y))
95 # define SCM_INUM(x) SCM_SRS (SCM_UNPACK (x), 2)
96 #endif /* (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) */
97
98
99 /* A name for 0.
100 */
101 #define SCM_INUM0 (SCM_PACK (2))
102
103
104 /* SCM_MAXEXP is the maximum double precision expontent
105 * SCM_FLTMAX is less than or scm_equal the largest single precision float
106 */
107
108 #ifdef STDC_HEADERS
109 #ifndef GO32
110 #include <float.h>
111 #endif /* ndef GO32 */
112 #endif /* def STDC_HEADERS */
113 #ifdef DBL_MAX_10_EXP
114 #define SCM_MAXEXP DBL_MAX_10_EXP
115 #else
116 #define SCM_MAXEXP 308 /* IEEE doubles */
117 #endif /* def DBL_MAX_10_EXP */
118 #ifdef FLT_MAX
119 #define SCM_FLTMAX FLT_MAX
120 #else
121 #define SCM_FLTMAX 1e+23
122 #endif /* def FLT_MAX */
123
124
125 /* SCM_FIXABLE is non-0 if its long argument can be encoded in an SCM_INUM.
126 */
127 #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
128 #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
129 #define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
130 #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
131
132 /* SCM_INTBUFLEN is the maximum number of characters neccessary for the
133 * printed or scm_string representation of an exact immediate.
134 */
135
136 #ifndef SCM_CHAR_BIT
137 # define SCM_CHAR_BIT 8
138 #endif /* ndef SCM_CHAR_BIT */
139 #ifndef SCM_LONG_BIT
140 # define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
141 #endif /* ndef SCM_LONG_BIT */
142 #define SCM_INTBUFLEN (5+SCM_LONG_BIT)
143
144 /* SCM_FLOBUFLEN is the maximum number of characters neccessary for the
145 * printed or scm_string representation of an inexact number.
146 */
147
148 #define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
149
150
151 \f
152
153 /* Numbers
154 */
155
156 #define SCM_NEWREAL(z, x) \
157 do { \
158 SCM_NEWCELL2 (z); \
159 SCM_SET_CELL_TYPE (z, scm_tc16_real); \
160 SCM_REAL_VALUE (z) = (x); \
161 } while (0) \
162
163 #define SCM_NEWCOMPLEX(z, x, y) \
164 do { \
165 double __SCM_complex_tmp = (y); \
166 if (__SCM_complex_tmp == 0.0) \
167 SCM_NEWREAL (z, x); \
168 else \
169 { \
170 SCM_NEWSMOB (z, scm_tc16_complex, \
171 scm_must_malloc (2L * sizeof (double), "complex")); \
172 SCM_COMPLEX_REAL (z) = (x); \
173 SCM_COMPLEX_IMAG (z) = __SCM_complex_tmp; \
174 } \
175 } while (0) \
176
177 #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real)
178 #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real)
179 #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex)
180 #define SCM_INEXACTP(x) (SCM_NIMP (x) && SCM_TYP16S (x) == scm_tc16_real)
181 #define SCM_REALP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_real)
182 #define SCM_COMPLEXP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
183
184 #define SCM_INEXP(x) SCM_INEXACTP(x) /* Deprecated */
185 #define SCM_CPLXP(x) SCM_COMPLEXP(x) /* Deprecated */
186
187 #define SCM_REAL_VALUE(x) (((scm_double_t *) SCM2PTR (x))->real)
188 #define SCM_COMPLEX_REAL(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->real)
189 #define SCM_COMPLEX_IMAG(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->imag)
190 #define SCM_REAL(x) \
191 (SCM_SLOPPY_REALP (x) \
192 ? SCM_REAL_VALUE (x) \
193 : SCM_COMPLEX_REAL (x)) \
194
195 #define SCM_IMAG(x) \
196 (SCM_SLOPPY_REALP (x) \
197 ? 0.0 \
198 : SCM_COMPLEX_IMAG (x)) \
199
200 #define SCM_REALPART(x) \
201 (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
202
203 #define scm_makdbl scm_make_complex /* Deprecated */
204 #define SCM_SINGP(x) SCM_BOOL_F /* Deprecated */
205
206 /* Define SCM_BIGDIG to an integer type whose size is smaller than long if
207 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
208 *
209 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
210 */
211 #ifdef BIGNUMS
212 # ifdef _UNICOS
213 # define SCM_DIGSTOOBIG
214 # if (1L << 31) <= SCM_USHRT_MAX
215 # define SCM_BIGDIG unsigned short
216 # else
217 # define SCM_BIGDIG unsigned int
218 # endif /* (1L << 31) <= USHRT_MAX */
219 # define SCM_BITSPERDIG 32
220 # else
221 # define SCM_BIGDIG unsigned short
222 # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
223 # endif /* def _UNICOS */
224
225 # define SCM_BIGRAD (1L << SCM_BITSPERDIG)
226 # define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
227 # define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
228 # define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
229 # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
230 # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
231
232 #endif /* def BIGNUMS */
233
234 #ifndef SCM_BIGDIG
235 /* Definition is not really used but helps various function
236 * prototypes to compile with conditionalization.
237 */
238 # define SCM_BIGDIG unsigned short
239 # define SCM_NO_BIGDIG
240 #endif /* ndef SCM_BIGDIG */
241
242 #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
243 #ifdef SCM_BIGDIG
244 #define SCM_NUM2DBL(x) (SCM_INUMP (x) \
245 ? (double) SCM_INUM (x) \
246 : (SCM_REALP (x) \
247 ? SCM_REALPART (x) \
248 : scm_big2dbl (x)))
249 #else
250 #define SCM_NUM2DBL(x) (SCM_INUMP (x) \
251 ? (double) SCM_INUM (x) \
252 : SCM_REALPART (x))
253 #endif
254 #define SCM_NUMP(x) \
255 (SCM_NIMP(x) && (0xfcff & SCM_UNPACK (SCM_CAR(x))) == scm_tc7_smob)
256 #define SCM_BIGP(x) SCM_SMOB_PREDICATE (scm_tc16_big, x)
257 #define SCM_BIGSIGNFLAG 0x10000L
258 #define SCM_BIGSIZEFIELD 17
259 #define SCM_BIGSIGN(x) (SCM_UNPACK_CAR (x) & SCM_BIGSIGNFLAG)
260 #define SCM_BDIGITS(x) ((SCM_BIGDIG *) SCM_UNPACK (SCM_CDR (x)))
261 #define SCM_NUMDIGS(x) ((scm_sizet) (SCM_UNPACK_CAR (x) >> SCM_BIGSIZEFIELD))
262 #define SCM_SETNUMDIGS(x, v, sign) \
263 SCM_SET_CELL_WORD_0 (x, \
264 scm_tc16_big \
265 | ((sign) ? SCM_BIGSIGNFLAG : 0) \
266 | (((v) + 0L) << SCM_BIGSIZEFIELD))
267
268 \f
269
270 typedef struct scm_dblproc
271 {
272 char *scm_string;
273 double (*cproc) ();
274 } scm_dblproc;
275
276 typedef struct scm_double_t
277 {
278 SCM type;
279 SCM pad;
280 double real;
281 } scm_double_t;
282
283 typedef struct scm_complex_t
284 {
285 double real;
286 double imag;
287 } scm_complex_t;
288
289
290 \f
291
292 extern SCM scm_exact_p (SCM x);
293 extern SCM scm_odd_p (SCM n);
294 extern SCM scm_even_p (SCM n);
295 extern SCM scm_abs (SCM x);
296 extern SCM scm_quotient (SCM x, SCM y);
297 extern SCM scm_remainder (SCM x, SCM y);
298 extern SCM scm_modulo (SCM x, SCM y);
299 extern SCM scm_gcd (SCM x, SCM y);
300 extern SCM scm_lcm (SCM n1, SCM n2);
301 extern SCM scm_logand (SCM n1, SCM n2);
302 extern SCM scm_logior (SCM n1, SCM n2);
303 extern SCM scm_logxor (SCM n1, SCM n2);
304 extern SCM scm_logtest (SCM n1, SCM n2);
305 extern SCM scm_logbit_p (SCM n1, SCM n2);
306 extern SCM scm_lognot (SCM n);
307 extern SCM scm_integer_expt (SCM z1, SCM z2);
308 extern SCM scm_ash (SCM n, SCM cnt);
309 extern SCM scm_bit_extract (SCM n, SCM start, SCM end);
310 extern SCM scm_logcount (SCM n);
311 extern SCM scm_integer_length (SCM n);
312 extern SCM scm_mkbig (scm_sizet nlen, int sign);
313 extern SCM scm_big2inum (SCM b, scm_sizet l);
314 extern SCM scm_adjbig (SCM b, scm_sizet nlen);
315 extern SCM scm_normbig (SCM b);
316 extern SCM scm_copybig (SCM b, int sign);
317 extern SCM scm_long2big (long n);
318 extern SCM scm_long_long2big (long_long n);
319 extern SCM scm_2ulong2big (unsigned long * np);
320 extern SCM scm_ulong2big (unsigned long n);
321 extern int scm_bigcomp (SCM x, SCM y);
322 extern long scm_pseudolong (long x);
323 extern void scm_longdigs (long x, SCM_BIGDIG digs[]);
324 extern SCM scm_addbig (SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny);
325 extern SCM scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn);
326 extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div);
327 extern scm_sizet scm_iint2str (long num, int rad, char *p);
328 extern SCM scm_number_to_string (SCM x, SCM radix);
329 extern int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
330 extern int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
331 extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
332 extern SCM scm_istr2int (char *str, long len, long radix);
333 extern SCM scm_istr2flo (char *str, long len, long radix);
334 extern SCM scm_istring2number (char *str, long len, long radix);
335 extern SCM scm_string_to_number (SCM str, SCM radix);
336 extern SCM scm_make_real (double x);
337 extern SCM scm_make_complex (double x, double y);
338 extern SCM scm_makdbl (double x, double y); /* Deprecated */
339 extern SCM scm_bigequal (SCM x, SCM y);
340 extern SCM scm_real_equalp (SCM x, SCM y);
341 extern SCM scm_complex_equalp (SCM x, SCM y);
342 extern SCM scm_number_p (SCM x);
343 extern SCM scm_real_p (SCM x);
344 extern SCM scm_integer_p (SCM x);
345 extern SCM scm_inexact_p (SCM x);
346 extern SCM scm_num_eq_p (SCM x, SCM y);
347 extern SCM scm_less_p (SCM x, SCM y);
348 extern SCM scm_gr_p (SCM x, SCM y);
349 extern SCM scm_leq_p (SCM x, SCM y);
350 extern SCM scm_geq_p (SCM x, SCM y);
351 extern SCM scm_zero_p (SCM z);
352 extern SCM scm_positive_p (SCM x);
353 extern SCM scm_negative_p (SCM x);
354 extern SCM scm_max (SCM x, SCM y);
355 extern SCM scm_min (SCM x, SCM y);
356 extern SCM scm_sum (SCM x, SCM y);
357 extern SCM scm_difference (SCM x, SCM y);
358 extern SCM scm_product (SCM x, SCM y);
359 extern double scm_num2dbl (SCM a, const char * why);
360 extern SCM scm_divide (SCM x, SCM y);
361 extern double scm_asinh (double x);
362 extern double scm_acosh (double x);
363 extern double scm_atanh (double x);
364 extern double scm_truncate (double x);
365 extern double scm_round (double x);
366 extern double scm_exact_to_inexact (double z);
367 extern SCM scm_sys_expt (SCM z1, SCM z2);
368 extern SCM scm_sys_atan2 (SCM z1, SCM z2);
369 extern SCM scm_make_rectangular (SCM z1, SCM z2);
370 extern SCM scm_make_polar (SCM z1, SCM z2);
371 extern SCM scm_real_part (SCM z);
372 extern SCM scm_imag_part (SCM z);
373 extern SCM scm_magnitude (SCM z);
374 extern SCM scm_angle (SCM z);
375 extern SCM scm_inexact_to_exact (SCM z);
376 extern SCM scm_trunc (SCM x);
377 extern SCM scm_dbl2big (double d);
378 extern double scm_big2dbl (SCM b);
379 extern SCM scm_long2num (long sl);
380 extern SCM scm_long_long2num (long_long sl);
381 extern SCM scm_ulong2num (unsigned long sl);
382 extern long scm_num2long (SCM num, char *pos, const char *s_caller);
383 extern long_long scm_num2long_long (SCM num, char *pos,
384 const char *s_caller);
385 extern unsigned long scm_num2ulong (SCM num, char *pos,
386 const char *s_caller);
387 extern void scm_init_numbers (void);
388
389 #endif /* NUMBERSH */
390
391 /*
392 Local Variables:
393 c-file-style: "gnu"
394 End:
395 */