* Replace SCM_UNPACK_CAR appropriately.
[bpt/guile.git] / libguile / numbers.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef NUMBERSH
4#define NUMBERSH
7a58e59f 5/* Copyright (C) 1995, 1996, 1998, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 44 * If you do not wish that, delete this exception notice. */
d3a6bc94
GB
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 */
0f2d19dd
JB
48\f
49
b4309c3c 50#include "libguile/__scm.h"
34f74fe1
MD
51#include "libguile/print.h"
52
0f2d19dd
JB
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
56100716
DH
66/* SCM_SRS is signed right shift */
67#if (-1 == (((-1) << 2) + 2) >> 2)
68# define SCM_SRS(x, y) ((x) >> (y))
0f2d19dd 69#else
56100716
DH
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) */
0f2d19dd
JB
72
73
56100716
DH
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))
0f2d19dd
JB
78
79
56100716 80/* A name for 0. */
b82c6ce0 81#define SCM_INUM0 (SCM_MAKINUM (0))
0f2d19dd
JB
82
83
88eb6852
MD
84/* SCM_MAXEXP is the maximum double precision expontent
85 * SCM_FLTMAX is less than or scm_equal the largest single precision float
86 */
87
88#ifdef STDC_HEADERS
89#ifndef GO32
90#include <float.h>
91#endif /* ndef GO32 */
92#endif /* def STDC_HEADERS */
93#ifdef DBL_MAX_10_EXP
94#define SCM_MAXEXP DBL_MAX_10_EXP
95#else
96#define SCM_MAXEXP 308 /* IEEE doubles */
97#endif /* def DBL_MAX_10_EXP */
98#ifdef FLT_MAX
99#define SCM_FLTMAX FLT_MAX
100#else
101#define SCM_FLTMAX 1e+23
102#endif /* def FLT_MAX */
103
0f2d19dd 104
0f2d19dd
JB
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
0f2d19dd
JB
117
118\f
119
120/* Numbers
121 */
122
88eb6852
MD
123#define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real)
124#define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real)
125#define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex)
126#define SCM_INEXACTP(x) (SCM_NIMP (x) && SCM_TYP16S (x) == scm_tc16_real)
127#define SCM_REALP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_real)
128#define SCM_COMPLEXP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
129
88eb6852 130#define SCM_REAL_VALUE(x) (((scm_double_t *) SCM2PTR (x))->real)
4260a7fc
DH
131#define SCM_COMPLEX_REAL(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->real)
132#define SCM_COMPLEX_IMAG(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->imag)
0f2d19dd
JB
133
134/* Define SCM_BIGDIG to an integer type whose size is smaller than long if
135 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
136 *
137 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
138 */
139#ifdef BIGNUMS
140# ifdef _UNICOS
141# define SCM_DIGSTOOBIG
142# if (1L << 31) <= SCM_USHRT_MAX
143# define SCM_BIGDIG unsigned short
144# else
145# define SCM_BIGDIG unsigned int
146# endif /* (1L << 31) <= USHRT_MAX */
147# define SCM_BITSPERDIG 32
148# else
149# define SCM_BIGDIG unsigned short
150# define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
151# endif /* def _UNICOS */
152
153# define SCM_BIGRAD (1L << SCM_BITSPERDIG)
154# define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
0f2d19dd 155# define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
08b5b88c 156# define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
0f2d19dd
JB
157# define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
158# define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
159#endif /* def BIGNUMS */
160
161#ifndef SCM_BIGDIG
162/* Definition is not really used but helps various function
163 * prototypes to compile with conditionalization.
164 */
165# define SCM_BIGDIG unsigned short
0f2d19dd
JB
166#endif /* ndef SCM_BIGDIG */
167
7a710745 168#define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
47457e8a 169#define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob)
88eb6852
MD
170#define SCM_BIGP(x) SCM_SMOB_PREDICATE (scm_tc16_big, x)
171#define SCM_BIGSIGNFLAG 0x10000L
172#define SCM_BIGSIZEFIELD 17
47457e8a
DH
173#define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG)
174#define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x)))
175#define SCM_NUMDIGS(x) ((scm_sizet) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD))
88eb6852 176#define SCM_SETNUMDIGS(x, v, sign) \
4260a7fc 177 SCM_SET_CELL_WORD_0 (x, \
88eb6852
MD
178 scm_tc16_big \
179 | ((sign) ? SCM_BIGSIGNFLAG : 0) \
f81e080b 180 | (((v) + 0L) << SCM_BIGSIZEFIELD))
88eb6852 181
0f2d19dd
JB
182\f
183
88eb6852 184typedef struct scm_double_t
0f2d19dd
JB
185{
186 SCM type;
88eb6852
MD
187 SCM pad;
188 double real;
189} scm_double_t;
0f2d19dd 190
88eb6852 191typedef struct scm_complex_t
0f2d19dd 192{
88eb6852
MD
193 double real;
194 double imag;
195} scm_complex_t;
0f2d19dd 196
0f2d19dd 197\f
0f2d19dd 198
7866a09b
GB
199extern SCM scm_exact_p (SCM x);
200extern SCM scm_odd_p (SCM n);
201extern SCM scm_even_p (SCM n);
202extern SCM scm_abs (SCM x);
203extern SCM scm_quotient (SCM x, SCM y);
204extern SCM scm_remainder (SCM x, SCM y);
205extern SCM scm_modulo (SCM x, SCM y);
206extern SCM scm_gcd (SCM x, SCM y);
207extern SCM scm_lcm (SCM n1, SCM n2);
208extern SCM scm_logand (SCM n1, SCM n2);
209extern SCM scm_logior (SCM n1, SCM n2);
210extern SCM scm_logxor (SCM n1, SCM n2);
211extern SCM scm_logtest (SCM n1, SCM n2);
212extern SCM scm_logbit_p (SCM n1, SCM n2);
213extern SCM scm_lognot (SCM n);
214extern SCM scm_integer_expt (SCM z1, SCM z2);
215extern SCM scm_ash (SCM n, SCM cnt);
216extern SCM scm_bit_extract (SCM n, SCM start, SCM end);
217extern SCM scm_logcount (SCM n);
218extern SCM scm_integer_length (SCM n);
219extern SCM scm_mkbig (scm_sizet nlen, int sign);
220extern SCM scm_big2inum (SCM b, scm_sizet l);
221extern SCM scm_adjbig (SCM b, scm_sizet nlen);
222extern SCM scm_normbig (SCM b);
223extern SCM scm_copybig (SCM b, int sign);
224extern SCM scm_long2big (long n);
225extern SCM scm_long_long2big (long_long n);
226extern SCM scm_2ulong2big (unsigned long * np);
227extern SCM scm_ulong2big (unsigned long n);
228extern int scm_bigcomp (SCM x, SCM y);
229extern long scm_pseudolong (long x);
230extern void scm_longdigs (long x, SCM_BIGDIG digs[]);
231extern SCM scm_addbig (SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny);
232extern SCM scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn);
233extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div);
7866a09b
GB
234extern scm_sizet scm_iint2str (long num, int rad, char *p);
235extern SCM scm_number_to_string (SCM x, SCM radix);
88eb6852
MD
236extern int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
237extern int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
7866a09b
GB
238extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
239extern SCM scm_istr2int (char *str, long len, long radix);
240extern SCM scm_istr2flo (char *str, long len, long radix);
241extern SCM scm_istring2number (char *str, long len, long radix);
242extern SCM scm_string_to_number (SCM str, SCM radix);
88eb6852
MD
243extern SCM scm_make_real (double x);
244extern SCM scm_make_complex (double x, double y);
7866a09b 245extern SCM scm_bigequal (SCM x, SCM y);
88eb6852
MD
246extern SCM scm_real_equalp (SCM x, SCM y);
247extern SCM scm_complex_equalp (SCM x, SCM y);
7866a09b
GB
248extern SCM scm_number_p (SCM x);
249extern SCM scm_real_p (SCM x);
250extern SCM scm_integer_p (SCM x);
251extern SCM scm_inexact_p (SCM x);
252extern SCM scm_num_eq_p (SCM x, SCM y);
253extern SCM scm_less_p (SCM x, SCM y);
254extern SCM scm_gr_p (SCM x, SCM y);
255extern SCM scm_leq_p (SCM x, SCM y);
256extern SCM scm_geq_p (SCM x, SCM y);
257extern SCM scm_zero_p (SCM z);
258extern SCM scm_positive_p (SCM x);
259extern SCM scm_negative_p (SCM x);
260extern SCM scm_max (SCM x, SCM y);
261extern SCM scm_min (SCM x, SCM y);
262extern SCM scm_sum (SCM x, SCM y);
263extern SCM scm_difference (SCM x, SCM y);
264extern SCM scm_product (SCM x, SCM y);
265extern double scm_num2dbl (SCM a, const char * why);
266extern SCM scm_divide (SCM x, SCM y);
267extern double scm_asinh (double x);
268extern double scm_acosh (double x);
269extern double scm_atanh (double x);
270extern double scm_truncate (double x);
271extern double scm_round (double x);
272extern double scm_exact_to_inexact (double z);
273extern SCM scm_sys_expt (SCM z1, SCM z2);
274extern SCM scm_sys_atan2 (SCM z1, SCM z2);
275extern SCM scm_make_rectangular (SCM z1, SCM z2);
276extern SCM scm_make_polar (SCM z1, SCM z2);
277extern SCM scm_real_part (SCM z);
278extern SCM scm_imag_part (SCM z);
279extern SCM scm_magnitude (SCM z);
280extern SCM scm_angle (SCM z);
281extern SCM scm_inexact_to_exact (SCM z);
282extern SCM scm_trunc (SCM x);
283extern SCM scm_dbl2big (double d);
284extern double scm_big2dbl (SCM b);
285extern SCM scm_long2num (long sl);
286extern SCM scm_long_long2num (long_long sl);
287extern SCM scm_ulong2num (unsigned long sl);
288extern long scm_num2long (SCM num, char *pos, const char *s_caller);
289extern long_long scm_num2long_long (SCM num, char *pos,
290 const char *s_caller);
291extern unsigned long scm_num2ulong (SCM num, char *pos,
292 const char *s_caller);
293extern void scm_init_numbers (void);
0f2d19dd 294
3a9809df
DH
295\f
296
297#if (SCM_DEBUG_DEPRECATED == 0)
298
299typedef struct scm_dblproc
300{
301 char *scm_string;
302 double (*cproc) ();
303} scm_dblproc;
304
305#define SCM_NEWREAL(z, x) do { z = scm_make_real (x); } while (0)
306#define SCM_NEWCOMPLEX(z, x, y) do { z = scm_make_complex (x, y); } while (0)
307#define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
308#define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
309#define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
310#define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
311#define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
312#define SCM_INEXP(x) SCM_INEXACTP(x)
313#define SCM_CPLXP(x) SCM_COMPLEXP(x)
314#define SCM_REAL(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
315#define SCM_IMAG(x) (SCM_SLOPPY_REALP (x) ? 0.0 : SCM_COMPLEX_IMAG (x))
316#define SCM_REALPART(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
317#define scm_makdbl scm_make_complex
318#define SCM_SINGP(x) 0
319#define SCM_NUM2DBL(x) scm_num2dbl(x, "SCM_NUM2DBL")
320
321#ifndef SCM_BIGDIG
322# define SCM_NO_BIGDIG
323#endif
324
325#endif /* SCM_DEBUG_DEPRECATED == 0 */
326
0f2d19dd 327#endif /* NUMBERSH */
89e00824
ML
328
329/*
330 Local Variables:
331 c-file-style: "gnu"
332 End:
333*/