maintainer changed: was lord, now jimb; first import
[bpt/guile.git] / libguile / numbers.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef NUMBERSH
4#define NUMBERSH
5/* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * As a special exception, the Free Software Foundation gives permission
22 * for additional uses of the text contained in its release of GUILE.
23 *
24 * The exception is that, if you link the GUILE library with other files
25 * to produce an executable, this does not by itself cause the
26 * resulting executable to be covered by the GNU General Public License.
27 * Your use of that executable is in no way restricted on account of
28 * linking the GUILE library code into it.
29 *
30 * This exception does not however invalidate any other reasons why
31 * the executable file might be covered by the GNU General Public License.
32 *
33 * This exception applies only to the code released by the
34 * Free Software Foundation under the name GUILE. If you copy
35 * code from other Free Software Foundation releases into a copy of
36 * GUILE, as the General Public License permits, the exception does
37 * not apply to the code that you add in this way. To avoid misleading
38 * anyone as to the status of such modified files, you must delete
39 * this exception notice from them.
40 *
41 * If you write modifications of your own for GUILE, it is your choice
42 * whether to permit this exception to apply to your modifications.
43 * If you do not wish that, delete this exception notice.
44 */
45\f
46
47#include "__scm.h"
48
49\f
50
51
52/* Immediate Numbers
53 *
54 * Inums are exact integer data that fits within an SCM word.
55 *
56 * SCM_INUMP applies only to values known to be Scheme objects.
57 * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known
58 * to be a SCM_CONSP. If x is only known to be a SCM_NIMP,
59 * SCM_INUMP (SCM_CAR (x)) can give wrong answers.
60 */
61
62#define SCM_INUMP(x) (2 & (int)(x))
63#define SCM_NINUMP(x) (!SCM_INUMP(x))
64
65#ifdef __TURBOC__
66/* shifts of more than one are done by a library call, single shifts are
67 * performed in registers
68 */
69# define SCM_MAKINUM(x) ((((x)<<1)<<1)+2L)
70#else
71# define SCM_MAKINUM(x) (((x)<<2)+2L)
72#endif /* def __TURBOC__ */
73
74
75/* SCM_SRS is signed right shift */
76/* Turbo C++ v1.0 has a bug with right shifts of signed longs!
77 * It is believed to be fixed in Turbo C++ v1.01
78 */
79#if (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295)
80# define SCM_SRS(x, y) ((x)>>y)
81# ifdef __TURBOC__
82# define SCM_INUM(x) (((x)>>1)>>1)
83# else
84# define SCM_INUM(x) SCM_SRS(x, 2)
85# endif /* def __TURBOC__ */
86#else
87# define SCM_SRS(x, y) (((x)<0) ? ~((~(x))>>y) : (x)>>y)
88# define SCM_INUM(x) SCM_SRS(x, 2)
89#endif /* (-1==(((-1)<<2)+2)>>2) && (__TURBOC__ != 0x295) */
90
91
92/* A name for 0.
93 */
94#define SCM_INUM0 ((SCM) 2)
95
96
97
98/* SCM_FIXABLE is non-0 if its long argument can be encoded in an SCM_INUM.
99 */
100#define SCM_POSSCM_FIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
101#define SCM_NEGSCM_FIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
102#define SCM_UNEGSCM_FIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
103#define SCM_FIXABLE(n) (SCM_POSSCM_FIXABLE(n) && SCM_NEGSCM_FIXABLE(n))
104
105/* SCM_INTBUFLEN is the maximum number of characters neccessary for the
106 * printed or scm_string representation of an exact immediate.
107 */
108
109#ifndef SCM_CHAR_BIT
110# define SCM_CHAR_BIT 8
111#endif /* ndef SCM_CHAR_BIT */
112#ifndef SCM_LONG_BIT
113# define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
114#endif /* ndef SCM_LONG_BIT */
115#define SCM_INTBUFLEN (5+SCM_LONG_BIT)
116
117/* SCM_FLOBUFLEN is the maximum number of characters neccessary for the
118 * printed or scm_string representation of an inexact number.
119 */
120
121#define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
122
123
124\f
125
126/* Numbers
127 */
128
129#define SCM_INEXP(x) (SCM_TYP16(x)==scm_tc16_flo)
130#define SCM_CPLXP(x) (SCM_CAR(x)==scm_tc_dblc)
131#define SCM_REAL(x) (*(((scm_dbl *) (SCM2PTR(x)))->real))
132#define SCM_IMAG(x) (*((double *)(SCM_CHARS(x)+sizeof(double))))
133/* ((&SCM_REAL(x))[1]) */
134
135
136#ifdef SCM_SINGLES
137#define SCM_REALP(x) ((~SCM_REAL_PART & SCM_CAR(x))==scm_tc_flo)
138#define SCM_SINGP(x) (SCM_CAR(x)==scm_tc_flo)
139#define SCM_FLO(x) (((scm_flo *)(SCM2PTR(x)))->num)
140#define SCM_REALPART(x) (SCM_SINGP(x)?0.0+SCM_FLO(x):SCM_REAL(x))
141#else /* SCM_SINGLES */
142#define SCM_REALP(x) (SCM_CAR(x)==scm_tc_dblr)
143#define SCM_REALPART SCM_REAL
144#endif /* SCM_SINGLES */
145
146
147/* Define SCM_BIGDIG to an integer type whose size is smaller than long if
148 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
149 *
150 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
151 */
152#ifdef BIGNUMS
153# ifdef _UNICOS
154# define SCM_DIGSTOOBIG
155# if (1L << 31) <= SCM_USHRT_MAX
156# define SCM_BIGDIG unsigned short
157# else
158# define SCM_BIGDIG unsigned int
159# endif /* (1L << 31) <= USHRT_MAX */
160# define SCM_BITSPERDIG 32
161# else
162# define SCM_BIGDIG unsigned short
163# define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
164# endif /* def _UNICOS */
165
166# define SCM_BIGRAD (1L << SCM_BITSPERDIG)
167# define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
168# define SCM_DIGSPERLONGLONG ((scm_sizet)((sizeof(long long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
169# define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
170# define SCM_LONGLONGSCM_BIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
171# define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
172# define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
173#endif /* def BIGNUMS */
174
175#ifndef SCM_BIGDIG
176/* Definition is not really used but helps various function
177 * prototypes to compile with conditionalization.
178 */
179# define SCM_BIGDIG unsigned short
180# define NO_SCM_BIGDIG
181# ifndef SCM_FLOATS
182# define SCM_INUMS_ONLY
183# endif /* ndef SCM_FLOATS */
184#endif /* ndef SCM_BIGDIG */
185
186#ifdef SCM_FLOATS
187#define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x)))
188#else
189#ifdef SCM_BIGDIG
190#define SCM_NUMBERP(x) (SCM_INUMP(x) || (SCM_NIMP(x) && SCM_NUMP(x)))
191#else
192#define SCM_NUMBERP SCM_INUMP
193#endif
194#endif
195#define SCM_NUMP(x) ((0xfcff & (int)SCM_CAR(x))==scm_tc7_smob)
196#define SCM_BIGP(x) (SCM_TYP16S(x)==scm_tc16_bigpos)
197#define SCM_BIGSIGN(x) (0x0100 & (int)SCM_CAR(x))
198#define SCM_BDIGITS(x) ((SCM_BIGDIG *)(SCM_CDR(x)))
199#define SCM_NUMDIGS(x) ((scm_sizet)(SCM_CAR(x)>>16))
200#define SCM_SETNUMDIGS(x, v, t) SCM_CAR(x) = (((v)+0L)<<16)+(t)
201\f
202
203#ifdef SCM_FLOATS
204typedef struct scm_dblproc
205{
206 char *scm_string;
207 double (*cproc) ();
208} scm_dblproc;
209
210#ifdef SCM_SINGLES
211typedef struct scm_flo
212{
213 SCM type;
214 float num;
215} scm_flo;
216#endif
217
218typedef struct scm_dbl
219{
220 SCM type;
221 double *real;
222} scm_dbl;
223#endif
224
225
226
227\f
228#ifdef __STDC__
229extern SCM scm_exact_p(SCM x);
230extern SCM scm_odd_p(SCM n);
231extern SCM scm_even_p(SCM n);
232extern SCM scm_abs(SCM x);
233extern SCM scm_quotient(SCM x, SCM y);
234extern SCM scm_remainder(SCM x, SCM y);
235extern SCM scm_modulo(SCM x, SCM y);
236extern SCM scm_gcd(SCM x, SCM y);
237extern SCM scm_lcm(SCM n1, SCM n2);
238extern SCM scm_logand(SCM n1, SCM n2);
239extern SCM scm_logior(SCM n1, SCM n2);
240extern SCM scm_logxor(SCM n1, SCM n2);
241extern SCM scm_logtest(SCM n1, SCM n2);
242extern SCM scm_logbit_p(SCM n1, SCM n2);
243extern SCM scm_logand(SCM n1, SCM n2);
244extern SCM scm_logior(SCM n1, SCM n2);
245extern SCM scm_logxor(SCM n1, SCM n2);
246extern SCM scm_logtest(SCM n1, SCM n2);
247extern SCM scm_logbit_p(SCM n1, SCM n2);
248extern SCM scm_lognot(SCM n);
249extern SCM scm_integer_expt(SCM z1, SCM z2);
250extern SCM scm_ash(SCM n, SCM cnt);
251extern SCM scm_bit_extract(SCM n, SCM start, SCM end);
252extern SCM scm_logcount (SCM n);
253extern SCM scm_integer_length(SCM n);
254extern SCM scm_mkbig(scm_sizet nlen, int sign);
255extern SCM scm_big2inum(SCM b, scm_sizet l);
256extern SCM scm_adjbig(SCM b, scm_sizet nlen);
257extern SCM scm_normbig(SCM b);
258extern SCM scm_copybig(SCM b, int sign);
259extern SCM scm_long2big(long n);
260extern SCM scm_long_long2big(long_long n);
261extern SCM scm_2ulong2big(unsigned long * np);
262extern SCM scm_ulong2big(unsigned long n);
263extern int scm_bigcomp(SCM x, SCM y);
264extern long scm_pseudolong(long x);
265extern void scm_longdigs(long x, SCM_BIGDIG digs[]);
266extern SCM scm_addbig(SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny);
267extern SCM scm_mulbig(SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn);
268extern unsigned int scm_divbigdig(SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div);
269extern SCM scm_divbigint(SCM x, long z, int sgn, int mode);
270extern SCM scm_divbigbig(SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn, int modes);
271extern scm_sizet scm_iint2str(long num, int rad, char *p);
272extern SCM scm_number_to_string(SCM x, SCM radix);
273extern int scm_floprint(SCM sexp, SCM port, int writing);
274extern int scm_bigprint(SCM exp, SCM port, int writing);
275extern SCM scm_istr2int(char *str, long len, long radix);
276extern SCM scm_istr2int(char *str, long len, long radix);
277extern SCM scm_istr2flo(char *str, long len, long radix);
278extern SCM scm_istring2number(char *str, long len, long radix);
279extern SCM scm_string_to_number(SCM str, SCM radix);
280extern SCM scm_makdbl (double x, double y);
281extern SCM scm_bigequal(SCM x, SCM y);
282extern SCM scm_floequal(SCM x, SCM y);
283extern SCM scm_number_p(SCM x);
284extern SCM scm_real_p(SCM x);
285extern SCM scm_int_p(SCM x);
286extern SCM scm_inexact_p(SCM x);
287extern SCM scm_num_eq_p (SCM x, SCM y);
288extern SCM scm_less_p(SCM x, SCM y);
289extern SCM scm_gr_p(SCM x, SCM y);
290extern SCM scm_leq_p(SCM x, SCM y);
291extern SCM scm_geq_p(SCM x, SCM y);
292extern SCM scm_zero_p(SCM z);
293extern SCM scm_positive_p(SCM x);
294extern SCM scm_negative_p(SCM x);
295extern SCM scm_max(SCM x, SCM y);
296extern SCM scm_min(SCM x, SCM y);
297extern SCM scm_sum(SCM x, SCM y);
298extern SCM scm_difference(SCM x, SCM y);
299extern SCM scm_product(SCM x, SCM y);
300extern double scm_num2dbl (SCM a, char * why);
301extern SCM scm_fuck (SCM a);
302extern SCM scm_divide(SCM x, SCM y);
303extern double scm_asinh(double x);
304extern double scm_acosh(double x);
305extern double scm_atanh(double x);
306extern double scm_truncate(double x);
307extern double scm_round(double x);
308extern double scm_exact_to_inexact(double z);
309extern SCM scm_sys_expt(SCM z1, SCM z2);
310extern SCM scm_sys_atan2(SCM z1, SCM z2);
311extern SCM scm_make_rectangular(SCM z1, SCM z2);
312extern SCM scm_make_polar(SCM z1, SCM z2);
313extern SCM scm_real_part(SCM z);
314extern SCM scm_imag_part(SCM z);
315extern SCM scm_magnitude(SCM z);
316extern SCM scm_angle(SCM z);
317extern SCM scm_inexact_to_exact(SCM z);
318extern SCM scm_trunc(SCM x);
319extern SCM scm_dbl2big(double d);
320extern double scm_big2dbl(SCM b);
321extern SCM scm_long2num(long sl);
322extern SCM scm_long_long2num(long_long sl);
323extern SCM scm_ulong2num(unsigned long sl);
324extern long scm_num2long(SCM num, char *pos, char *s_caller);
325extern long num2long(SCM num, char *pos, char *s_caller);
326extern long_long scm_num2long_long(SCM num, char *pos, char *s_caller);
327extern unsigned long scm_num2ulong(SCM num, char *pos, char *s_caller);
328extern void scm_init_numbers (void);
329
330#else /* STDC */
331extern SCM scm_exact_p();
332extern SCM scm_odd_p();
333extern SCM scm_even_p();
334extern SCM scm_abs();
335extern SCM scm_quotient();
336extern SCM scm_remainder();
337extern SCM scm_modulo();
338extern SCM scm_gcd();
339extern SCM scm_lcm();
340extern SCM scm_logand();
341extern SCM scm_logior();
342extern SCM scm_logxor();
343extern SCM scm_logtest();
344extern SCM scm_logbit_p();
345extern SCM scm_logand();
346extern SCM scm_logior();
347extern SCM scm_logxor();
348extern SCM scm_logtest();
349extern SCM scm_logbit_p();
350extern SCM scm_lognot();
351extern SCM scm_integer_expt();
352extern SCM scm_ash();
353extern SCM scm_bit_extract();
354extern SCM scm_logcount ();
355extern SCM scm_integer_length();
356extern SCM scm_mkbig();
357extern SCM scm_big2inum();
358extern SCM scm_adjbig();
359extern SCM scm_normbig();
360extern SCM scm_copybig();
361extern SCM scm_long2big();
362extern SCM scm_long_long2big();
363extern SCM scm_2ulong2big();
364extern SCM scm_ulong2big();
365extern int scm_bigcomp();
366extern long scm_pseudolong();
367extern void scm_longdigs();
368extern SCM scm_addbig();
369extern SCM scm_mulbig();
370extern unsigned int scm_divbigdig();
371extern SCM scm_divbigint();
372extern SCM scm_divbigbig();
373extern scm_sizet scm_iint2str();
374extern SCM scm_number_to_string();
375extern int scm_floprint();
376extern int scm_bigprint();
377extern SCM scm_istr2int();
378extern SCM scm_istr2int();
379extern SCM scm_istr2flo();
380extern SCM scm_istring2number();
381extern SCM scm_string_to_number();
382extern SCM scm_makdbl ();
383extern SCM scm_bigequal();
384extern SCM scm_floequal();
385extern SCM scm_number_p();
386extern SCM scm_real_p();
387extern SCM scm_int_p();
388extern SCM scm_inexact_p();
389extern SCM scm_num_eq_p ();
390extern SCM scm_less_p();
391extern SCM scm_gr_p();
392extern SCM scm_leq_p();
393extern SCM scm_geq_p();
394extern SCM scm_zero_p();
395extern SCM scm_positive_p();
396extern SCM scm_negative_p();
397extern SCM scm_max();
398extern SCM scm_min();
399extern SCM scm_sum();
400extern SCM scm_difference();
401extern SCM scm_product();
402extern double scm_num2dbl ();
403extern SCM scm_fuck ();
404extern SCM scm_divide();
405extern double scm_asinh();
406extern double scm_acosh();
407extern double scm_atanh();
408extern double scm_truncate();
409extern double scm_round();
410extern double scm_exact_to_inexact();
411extern SCM scm_sys_expt();
412extern SCM scm_sys_atan2();
413extern SCM scm_make_rectangular();
414extern SCM scm_make_polar();
415extern SCM scm_real_part();
416extern SCM scm_imag_part();
417extern SCM scm_magnitude();
418extern SCM scm_angle();
419extern SCM scm_inexact_to_exact();
420extern SCM scm_trunc();
421extern SCM scm_dbl2big();
422extern double scm_big2dbl();
423extern SCM scm_long2num();
424extern SCM scm_long_long2num();
425extern SCM scm_ulong2num();
426extern long scm_num2long();
427extern long num2long();
428extern long_long scm_num2long_long();
429extern unsigned long scm_num2ulong();
430extern void scm_init_numbers ();
431
432#endif /* STDC */
433
434
435
436#endif /* NUMBERSH */