6 /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc.
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)
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.
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
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
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.
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
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.
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.
45 * If you do not wish that, delete this exception notice. */
49 #include "libguile/__scm.h"
50 #include "libguile/print.h"
54 /* Immediate Numbers, also known as fixnums
56 * Inums are exact integer data that fits within an SCM word. */
58 #define SCM_I_FIXNUM_BIT (SCM_LONG_BIT - 2)
59 #define SCM_MOST_POSITIVE_FIXNUM (SCM_T_SIGNED_BITS_MAX/8-1)
60 #define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM-1)
62 /* SCM_SRS is signed right shift */
63 #if (-1 == (((-1) << 2) + 2) >> 2)
64 # define SCM_SRS(x, y) ((x) >> (y))
66 # define SCM_SRS(x, y) ((x) < 0 ? ~((~(x)) >> (y)) : ((x) >> (y)))
67 #endif /* (-1 == (((-1) << 2) + 2) >> 2) */
70 #define SCM_INUMP(x) (2 & SCM_UNPACK (x))
71 #define SCM_NINUMP(x) (!SCM_INUMP (x))
72 #define SCM_MAKINUM(x) (SCM_PACK ((((scm_t_signed_bits) (x)) << 2) + 2))
73 #define SCM_INUM(x) (SCM_SRS ((scm_t_signed_bits) SCM_UNPACK (x), 2))
76 /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */
77 #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
78 #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
79 #define SCM_FIXABLE(n) (SCM_POSFIXABLE (n) && SCM_NEGFIXABLE (n))
83 #define SCM_INUM0 (SCM_MAKINUM (0))
86 /* SCM_MAXEXP is the maximum double precision expontent
87 * SCM_FLTMAX is less than or scm_equal the largest single precision float
93 #endif /* ndef GO32 */
94 #endif /* def STDC_HEADERS */
96 #define SCM_MAXEXP DBL_MAX_10_EXP
98 #define SCM_MAXEXP 308 /* IEEE doubles */
99 #endif /* def DBL_MAX_10_EXP */
101 #define SCM_FLTMAX FLT_MAX
103 #define SCM_FLTMAX 1e+23
104 #endif /* def FLT_MAX */
107 /* SCM_INTBUFLEN is the maximum number of characters neccessary for the
108 * printed or scm_string representation of an exact immediate.
110 #define SCM_INTBUFLEN (5 + SCM_LONG_BIT)
117 #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real)
118 #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real)
119 #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex)
120 #define SCM_INEXACTP(x) (!SCM_IMP (x) && SCM_TYP16S (x) == scm_tc16_real)
121 #define SCM_REALP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_real)
122 #define SCM_COMPLEXP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
124 #define SCM_REAL_VALUE(x) (((scm_t_double *) SCM2PTR (x))->real)
125 #define SCM_COMPLEX_MEM(x) ((scm_t_complex *) SCM_CELL_WORD_1 (x))
126 #define SCM_COMPLEX_REAL(x) (SCM_COMPLEX_MEM (x)->real)
127 #define SCM_COMPLEX_IMAG(x) (SCM_COMPLEX_MEM (x)->imag)
129 /* Define SCM_BIGDIG to an integer type whose size is smaller than long if
130 * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
132 * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
136 # define SCM_DIGSTOOBIG
137 # if (1L << 31) <= SCM_USHRT_MAX
138 # define SCM_BIGDIG unsigned short
140 # define SCM_BIGDIG unsigned int
141 # endif /* (1L << 31) <= USHRT_MAX */
142 # define SCM_BITSPERDIG 32
144 # define SCM_BIGDIG unsigned short
145 # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
146 # endif /* def _UNICOS */
148 # define SCM_BIGRAD (1L << SCM_BITSPERDIG)
149 # define SCM_DIGSPERLONG ((size_t)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
150 # define SCM_I_BIGUP(type, x) ((type)(x) << SCM_BITSPERDIG)
151 # define SCM_BIGUP(x) SCM_I_BIGUP (unsigned long, x)
152 # define SCM_LONGLONGBIGUP(x) SCM_I_BIGUP (unsigned long long, x)
153 # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
154 # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
155 #endif /* def BIGNUMS */
158 /* Definition is not really used but helps various function
159 * prototypes to compile with conditionalization.
161 # define SCM_BIGDIG unsigned short
162 #endif /* ndef SCM_BIGDIG */
164 #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
165 #define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob)
166 #define SCM_BIGP(x) (!SCM_IMP (x) && (SCM_TYP16 (x) == scm_tc16_big))
167 #define SCM_BIGSIGNFLAG 0x10000L
168 #define SCM_BIGSIZEFIELD 17
169 #define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG)
170 #define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x)))
171 #define SCM_SET_BIGNUM_BASE(n, b) (SCM_SET_CELL_WORD_1 ((n), (b)))
172 #define SCM_NUMDIGS(x) ((size_t) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD))
173 #define SCM_SETNUMDIGS(x, v, sign) \
174 SCM_SET_CELL_WORD_0 (x, \
176 | ((sign) ? SCM_BIGSIGNFLAG : 0) \
177 | (((v) + 0L) << SCM_BIGSIZEFIELD))
181 typedef struct scm_t_double
188 typedef struct scm_t_complex
196 extern SCM
scm_exact_p (SCM x
);
197 extern SCM
scm_odd_p (SCM n
);
198 extern SCM
scm_even_p (SCM n
);
199 extern SCM
scm_abs (SCM x
);
200 extern SCM
scm_quotient (SCM x
, SCM y
);
201 extern SCM
scm_remainder (SCM x
, SCM y
);
202 extern SCM
scm_modulo (SCM x
, SCM y
);
203 extern SCM
scm_gcd (SCM x
, SCM y
);
204 extern SCM
scm_lcm (SCM n1
, SCM n2
);
205 extern SCM
scm_logand (SCM n1
, SCM n2
);
206 extern SCM
scm_logior (SCM n1
, SCM n2
);
207 extern SCM
scm_logxor (SCM n1
, SCM n2
);
208 extern SCM
scm_logtest (SCM n1
, SCM n2
);
209 extern SCM
scm_logbit_p (SCM n1
, SCM n2
);
210 extern SCM
scm_lognot (SCM n
);
211 extern SCM
scm_integer_expt (SCM z1
, SCM z2
);
212 extern SCM
scm_ash (SCM n
, SCM cnt
);
213 extern SCM
scm_bit_extract (SCM n
, SCM start
, SCM end
);
214 extern SCM
scm_logcount (SCM n
);
215 extern SCM
scm_integer_length (SCM n
);
216 extern SCM
scm_i_mkbig (size_t nlen
, int sign
);
217 extern SCM
scm_i_big2inum (SCM b
, size_t l
);
218 extern SCM
scm_i_adjbig (SCM b
, size_t nlen
);
219 extern SCM
scm_i_normbig (SCM b
);
220 extern SCM
scm_i_copybig (SCM b
, int sign
);
221 extern SCM
scm_i_short2big (short n
);
222 extern SCM
scm_i_ushort2big (unsigned short n
);
223 extern SCM
scm_i_int2big (int n
);
224 extern SCM
scm_i_uint2big (unsigned int n
);
225 extern SCM
scm_i_long2big (long n
);
226 extern SCM
scm_i_ulong2big (unsigned long n
);
227 extern SCM
scm_i_size2big (size_t n
);
228 extern SCM
scm_i_ptrdiff2big (ptrdiff_t n
);
230 #ifdef HAVE_LONG_LONGS
231 extern SCM
scm_i_long_long2big (long long n
);
232 extern SCM
scm_i_ulong_long2big (unsigned long long n
);
235 extern int scm_bigcomp (SCM x
, SCM y
);
236 extern long scm_pseudolong (long x
);
237 extern void scm_longdigs (long x
, SCM_BIGDIG digs
[]);
238 extern SCM
scm_addbig (SCM_BIGDIG
*x
, size_t nx
, int xsgn
, SCM bigy
, int sgny
);
239 extern SCM
scm_mulbig (SCM_BIGDIG
*x
, size_t nx
, SCM_BIGDIG
*y
, size_t ny
, int sgn
);
240 extern unsigned int scm_divbigdig (SCM_BIGDIG
*ds
, size_t h
, SCM_BIGDIG div
);
241 extern size_t scm_iint2str (long num
, int rad
, char *p
);
242 extern SCM
scm_number_to_string (SCM x
, SCM radix
);
243 extern int scm_print_real (SCM sexp
, SCM port
, scm_print_state
*pstate
);
244 extern int scm_print_complex (SCM sexp
, SCM port
, scm_print_state
*pstate
);
245 extern int scm_bigprint (SCM exp
, SCM port
, scm_print_state
*pstate
);
246 extern SCM
scm_i_mem2number (const char *mem
, size_t len
, unsigned int radix
);
247 extern SCM
scm_string_to_number (SCM str
, SCM radix
);
248 extern SCM
scm_make_real (double x
);
249 extern SCM
scm_make_complex (double x
, double y
);
250 extern SCM
scm_bigequal (SCM x
, SCM y
);
251 extern SCM
scm_real_equalp (SCM x
, SCM y
);
252 extern SCM
scm_complex_equalp (SCM x
, SCM y
);
253 extern SCM
scm_number_p (SCM x
);
254 extern SCM
scm_real_p (SCM x
);
255 extern SCM
scm_integer_p (SCM x
);
256 extern SCM
scm_inexact_p (SCM x
);
257 extern SCM
scm_num_eq_p (SCM x
, SCM y
);
258 extern SCM
scm_less_p (SCM x
, SCM y
);
259 extern SCM
scm_gr_p (SCM x
, SCM y
);
260 extern SCM
scm_leq_p (SCM x
, SCM y
);
261 extern SCM
scm_geq_p (SCM x
, SCM y
);
262 extern SCM
scm_zero_p (SCM z
);
263 extern SCM
scm_positive_p (SCM x
);
264 extern SCM
scm_negative_p (SCM x
);
265 extern SCM
scm_max (SCM x
, SCM y
);
266 extern SCM
scm_min (SCM x
, SCM y
);
267 extern SCM
scm_sum (SCM x
, SCM y
);
268 extern SCM
scm_difference (SCM x
, SCM y
);
269 extern SCM
scm_product (SCM x
, SCM y
);
270 extern double scm_num2dbl (SCM a
, const char * why
);
271 extern SCM
scm_divide (SCM x
, SCM y
);
272 extern double scm_asinh (double x
);
273 extern double scm_acosh (double x
);
274 extern double scm_atanh (double x
);
275 extern double scm_truncate (double x
);
276 extern double scm_round (double x
);
277 extern SCM
scm_sys_expt (SCM z1
, SCM z2
);
278 extern SCM
scm_sys_atan2 (SCM z1
, SCM z2
);
279 extern SCM
scm_make_rectangular (SCM z1
, SCM z2
);
280 extern SCM
scm_make_polar (SCM z1
, SCM z2
);
281 extern SCM
scm_real_part (SCM z
);
282 extern SCM
scm_imag_part (SCM z
);
283 extern SCM
scm_magnitude (SCM z
);
284 extern SCM
scm_angle (SCM z
);
285 extern SCM
scm_exact_to_inexact (SCM z
);
286 extern SCM
scm_inexact_to_exact (SCM z
);
287 extern SCM
scm_trunc (SCM x
);
288 extern SCM
scm_i_dbl2big (double d
);
289 extern double scm_i_big2dbl (SCM b
);
291 extern SCM
scm_short2num (short n
);
292 extern SCM
scm_ushort2num (unsigned short n
);
293 extern SCM
scm_int2num (int n
);
294 extern SCM
scm_uint2num (unsigned int n
);
295 extern SCM
scm_long2num (long n
);
296 extern SCM
scm_ulong2num (unsigned long n
);
297 extern SCM
scm_size2num (size_t n
);
298 extern SCM
scm_ptrdiff2num (ptrdiff_t n
);
299 extern short scm_num2short (SCM num
, unsigned long int pos
,
300 const char *s_caller
);
301 extern unsigned short scm_num2ushort (SCM num
, unsigned long int pos
,
302 const char *s_caller
);
303 extern int scm_num2int (SCM num
, unsigned long int pos
,
304 const char *s_caller
);
305 extern unsigned int scm_num2uint (SCM num
, unsigned long int pos
,
306 const char *s_caller
);
307 extern long scm_num2long (SCM num
, unsigned long int pos
,
308 const char *s_caller
);
309 extern unsigned long scm_num2ulong (SCM num
, unsigned long int pos
,
310 const char *s_caller
);
311 extern ptrdiff_t scm_num2ptrdiff (SCM num
, unsigned long int pos
,
312 const char *s_caller
);
313 extern size_t scm_num2size (SCM num
, unsigned long int pos
,
314 const char *s_caller
);
315 #ifdef HAVE_LONG_LONGS
316 extern SCM
scm_long_long2num (long long sl
);
317 extern SCM
scm_ulong_long2num (unsigned long long sl
);
318 extern long long scm_num2long_long (SCM num
, unsigned long int pos
,
319 const char *s_caller
);
320 extern unsigned long long scm_num2ulong_long (SCM num
, unsigned long int pos
,
321 const char *s_caller
);
324 extern SCM
scm_float2num (float n
);
325 extern SCM
scm_double2num (double n
);
326 extern float scm_num2float (SCM num
, unsigned long int pos
,
327 const char *s_caller
);
328 extern double scm_num2double (SCM num
, unsigned long int pos
,
329 const char *s_caller
);
331 extern void scm_init_numbers (void);
333 #endif /* SCM_NUMBERS_H */