Use string accessors for string->number conversion
[bpt/guile.git] / libguile / numbers.h
1 /* classes: h_files */
2
3 #ifndef SCM_NUMBERS_H
4 #define SCM_NUMBERS_H
5
6 /* Copyright (C) 1995,1996,1998,2000,2001,2002,2003,2004,2005, 2006, 2008, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 #include <gmp.h>
27
28 #include "libguile/__scm.h"
29 #include "libguile/print.h"
30
31 #if SCM_HAVE_FLOATINGPOINT_H
32 # include <floatingpoint.h>
33 #endif
34
35 #if SCM_HAVE_IEEEFP_H
36 # include <ieeefp.h>
37 #endif
38
39 #if SCM_HAVE_NAN_H
40 # if defined (SCO)
41 # define _IEEE 1
42 # endif
43 # include <nan.h>
44 # if defined (SCO)
45 # undef _IEEE
46 # endif
47 #endif /* SCM_HAVE_NAN_H */
48
49 \f
50
51 /* Immediate Numbers, also known as fixnums
52 *
53 * Inums are exact integer data that fits within an SCM word. */
54
55 /* SCM_T_SIGNED_MAX is (- (expt 2 n) 1),
56 * SCM_MOST_POSITIVE_FIXNUM should be (- (expt 2 (- n 2)) 1)
57 * which is the same as (/ (- (expt 2 n) 4) 4)
58 */
59
60 #define SCM_I_FIXNUM_BIT (SCM_LONG_BIT - 2)
61 #define SCM_MOST_POSITIVE_FIXNUM ((SCM_T_SIGNED_BITS_MAX-3)/4)
62 #define SCM_MOST_NEGATIVE_FIXNUM (-SCM_MOST_POSITIVE_FIXNUM-1)
63
64 /* SCM_SRS is signed right shift */
65 #if (-1 == (((-1) << 2) + 2) >> 2)
66 # define SCM_SRS(x, y) ((x) >> (y))
67 #else
68 # define SCM_SRS(x, y) ((x) < 0 ? ~((~(x)) >> (y)) : ((x) >> (y)))
69 #endif /* (-1 == (((-1) << 2) + 2) >> 2) */
70
71
72 #define SCM_I_INUMP(x) (2 & SCM_UNPACK (x))
73 #define SCM_I_NINUMP(x) (!SCM_I_INUMP (x))
74 #define SCM_I_MAKINUM(x) \
75 (SCM_PACK ((((scm_t_signed_bits) (x)) << 2) + scm_tc2_int))
76 #define SCM_I_INUM(x) (SCM_SRS ((scm_t_signed_bits) SCM_UNPACK (x), 2))
77
78 /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */
79 #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
80 #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
81 #define SCM_FIXABLE(n) (SCM_POSFIXABLE (n) && SCM_NEGFIXABLE (n))
82
83
84 /* A name for 0. */
85 #define SCM_INUM0 (SCM_I_MAKINUM (0))
86
87 /* SCM_MAXEXP is the maximum double precision exponent
88 * SCM_FLTMAX is less than or scm_equal the largest single precision float
89 */
90
91 #if SCM_HAVE_STDC_HEADERS
92 # ifndef GO32
93 # include <float.h>
94 # ifdef __MINGW32__
95 # define copysign _copysign
96 # define finite _finite
97 # endif /* __MINGW32__ */
98 # endif /* ndef GO32 */
99 #endif /* def STDC_HEADERS */
100
101 #ifdef DBL_MAX_10_EXP
102 # define SCM_MAXEXP DBL_MAX_10_EXP
103 #else
104 # define SCM_MAXEXP 308 /* IEEE doubles */
105 #endif /* def DBL_MAX_10_EXP */
106
107 #ifdef FLT_MAX
108 # define SCM_FLTMAX FLT_MAX
109 #else
110 # define SCM_FLTMAX 1e+23
111 #endif /* def FLT_MAX */
112
113
114 /* SCM_INTBUFLEN is the maximum number of characters neccessary for
115 * the printed or scm_string representation of an scm_t_intmax in
116 * radix 2. The buffer passed to scm_iint2str and scm_iuint2str must
117 * be of this size, for example.
118 */
119 #define SCM_INTBUFLEN (5 + SCM_CHAR_BIT*sizeof(scm_t_intmax))
120
121 \f
122
123 /* Numbers
124 */
125
126
127 /* Note that scm_tc16_real and scm_tc16_complex are given tc16-codes that only
128 * differ in one bit: This way, checking if an object is an inexact number can
129 * be done quickly (using the TYP16S macro). */
130
131 /* Number subtype 1 to 3 (note the dependency on the predicates SCM_INEXACTP
132 * and SCM_NUMP) */
133 #define scm_tc16_big (scm_tc7_number + 1 * 256L)
134 #define scm_tc16_real (scm_tc7_number + 2 * 256L)
135 #define scm_tc16_complex (scm_tc7_number + 3 * 256L)
136 #define scm_tc16_fraction (scm_tc7_number + 4 * 256L)
137
138 #define SCM_INEXACTP(x) \
139 (!SCM_IMP (x) && (0xfeff & SCM_CELL_TYPE (x)) == scm_tc16_real)
140 #define SCM_REALP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_real)
141 #define SCM_COMPLEXP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
142
143 #define SCM_REAL_VALUE(x) (((scm_t_double *) SCM2PTR (x))->real)
144 #define SCM_COMPLEX_MEM(x) ((scm_t_complex *) SCM_CELL_WORD_1 (x))
145 #define SCM_COMPLEX_REAL(x) (SCM_COMPLEX_MEM (x)->real)
146 #define SCM_COMPLEX_IMAG(x) (SCM_COMPLEX_MEM (x)->imag)
147
148 /* Each bignum is just an mpz_t stored in a double cell starting at word 1. */
149 #define SCM_I_BIG_MPZ(x) (*((mpz_t *) (SCM_CELL_OBJECT_LOC((x),1))))
150 #define SCM_BIGP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_big)
151
152 #define SCM_NUMBERP(x) (SCM_I_INUMP(x) || SCM_NUMP(x))
153 #define SCM_NUMP(x) (!SCM_IMP(x) \
154 && (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) \
155 || ((0xfbff & SCM_CELL_TYPE (x)) == scm_tc7_number)))
156 /* 0xfcff (#b1100) for 0 free, 1 big, 2 real, 3 complex, then 0xfbff (#b1011) for 4 fraction */
157
158 #define SCM_FRACTIONP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_fraction)
159 #define SCM_FRACTION_NUMERATOR(x) (SCM_CELL_OBJECT_1 (x))
160 #define SCM_FRACTION_DENOMINATOR(x) (SCM_CELL_OBJECT_2 (x))
161
162 \f
163
164 typedef struct scm_t_double
165 {
166 SCM type;
167 SCM pad;
168 double real;
169 } scm_t_double;
170
171 typedef struct scm_t_complex
172 {
173 double real;
174 double imag;
175 } scm_t_complex;
176
177 typedef scm_t_int32 scm_t_wchar;
178
179 \f
180
181 SCM_API SCM scm_exact_p (SCM x);
182 SCM_API SCM scm_odd_p (SCM n);
183 SCM_API SCM scm_even_p (SCM n);
184 SCM_API SCM scm_inf_p (SCM n);
185 SCM_API SCM scm_nan_p (SCM n);
186 SCM_API SCM scm_inf (void);
187 SCM_API SCM scm_nan (void);
188 SCM_API SCM scm_abs (SCM x);
189 SCM_API SCM scm_quotient (SCM x, SCM y);
190 SCM_API SCM scm_remainder (SCM x, SCM y);
191 SCM_API SCM scm_modulo (SCM x, SCM y);
192 SCM_API SCM scm_gcd (SCM x, SCM y);
193 SCM_API SCM scm_lcm (SCM n1, SCM n2);
194 SCM_API SCM scm_logand (SCM n1, SCM n2);
195 SCM_API SCM scm_logior (SCM n1, SCM n2);
196 SCM_API SCM scm_logxor (SCM n1, SCM n2);
197 SCM_API SCM scm_logtest (SCM n1, SCM n2);
198 SCM_API SCM scm_logbit_p (SCM n1, SCM n2);
199 SCM_API SCM scm_lognot (SCM n);
200 SCM_API SCM scm_modulo_expt (SCM n, SCM k, SCM m);
201 SCM_API SCM scm_integer_expt (SCM z1, SCM z2);
202 SCM_API SCM scm_ash (SCM n, SCM cnt);
203 SCM_API SCM scm_bit_extract (SCM n, SCM start, SCM end);
204 SCM_API SCM scm_logcount (SCM n);
205 SCM_API SCM scm_integer_length (SCM n);
206
207 SCM_API size_t scm_iint2str (scm_t_intmax num, int rad, char *p);
208 SCM_API size_t scm_iuint2str (scm_t_uintmax num, int rad, char *p);
209 SCM_API SCM scm_number_to_string (SCM x, SCM radix);
210 SCM_API int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
211 SCM_API int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
212 SCM_API int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
213 SCM_API SCM scm_c_locale_stringn_to_number (const char *mem, size_t len,
214 unsigned int radix);
215 SCM_INTERNAL SCM scm_i_string_to_number (SCM str, unsigned int radix);
216 SCM_API SCM scm_string_to_number (SCM str, SCM radix);
217 SCM_API SCM scm_bigequal (SCM x, SCM y);
218 SCM_API SCM scm_real_equalp (SCM x, SCM y);
219 SCM_API SCM scm_complex_equalp (SCM x, SCM y);
220 SCM_API SCM scm_number_p (SCM x);
221 SCM_API SCM scm_complex_p (SCM x);
222 SCM_API SCM scm_real_p (SCM x);
223 SCM_API SCM scm_rational_p (SCM z);
224 SCM_API SCM scm_integer_p (SCM x);
225 SCM_API SCM scm_inexact_p (SCM x);
226 SCM_API SCM scm_num_eq_p (SCM x, SCM y);
227 SCM_API SCM scm_less_p (SCM x, SCM y);
228 SCM_API SCM scm_gr_p (SCM x, SCM y);
229 SCM_API SCM scm_leq_p (SCM x, SCM y);
230 SCM_API SCM scm_geq_p (SCM x, SCM y);
231 SCM_API SCM scm_zero_p (SCM z);
232 SCM_API SCM scm_positive_p (SCM x);
233 SCM_API SCM scm_negative_p (SCM x);
234 SCM_API SCM scm_max (SCM x, SCM y);
235 SCM_API SCM scm_min (SCM x, SCM y);
236 SCM_API SCM scm_sum (SCM x, SCM y);
237 SCM_API SCM scm_oneplus (SCM x);
238 SCM_API SCM scm_difference (SCM x, SCM y);
239 SCM_API SCM scm_oneminus (SCM x);
240 SCM_API SCM scm_product (SCM x, SCM y);
241 SCM_API SCM scm_divide (SCM x, SCM y);
242 SCM_API SCM scm_floor (SCM x);
243 SCM_API SCM scm_ceiling (SCM x);
244 SCM_API double scm_asinh (double x);
245 SCM_API double scm_acosh (double x);
246 SCM_API double scm_atanh (double x);
247 SCM_API double scm_c_truncate (double x);
248 SCM_API double scm_c_round (double x);
249 SCM_API SCM scm_truncate_number (SCM x);
250 SCM_API SCM scm_round_number (SCM x);
251 SCM_API SCM scm_sys_expt (SCM z1, SCM z2);
252 SCM_API SCM scm_sys_atan2 (SCM z1, SCM z2);
253 SCM_API SCM scm_make_rectangular (SCM z1, SCM z2);
254 SCM_API SCM scm_make_polar (SCM z1, SCM z2);
255 SCM_API SCM scm_real_part (SCM z);
256 SCM_API SCM scm_imag_part (SCM z);
257 SCM_API SCM scm_magnitude (SCM z);
258 SCM_API SCM scm_angle (SCM z);
259 SCM_API SCM scm_exact_to_inexact (SCM z);
260 SCM_API SCM scm_inexact_to_exact (SCM z);
261 SCM_API SCM scm_trunc (SCM x);
262 SCM_API SCM scm_log (SCM z);
263 SCM_API SCM scm_log10 (SCM z);
264 SCM_API SCM scm_exp (SCM z);
265 SCM_API SCM scm_sqrt (SCM z);
266
267 /* bignum internal functions */
268 SCM_INTERNAL SCM scm_i_mkbig (void);
269 SCM_API /* FIXME: not internal */ SCM scm_i_normbig (SCM x);
270 SCM_INTERNAL int scm_i_bigcmp (SCM a, SCM b);
271 SCM_INTERNAL SCM scm_i_dbl2big (double d);
272 SCM_INTERNAL SCM scm_i_dbl2num (double d);
273 SCM_API /* FIXME: not internal */ double scm_i_big2dbl (SCM b);
274 SCM_API /* FIXME: not internal */ SCM scm_i_long2big (long n);
275 SCM_API /* FIXME: not internal */ SCM scm_i_ulong2big (unsigned long n);
276 SCM_API /* FIXME: not internal */ SCM scm_i_clonebig (SCM src_big, int same_sign_p);
277
278 /* ratio functions */
279 SCM_API SCM scm_rationalize (SCM x, SCM err);
280 SCM_API SCM scm_numerator (SCM z);
281 SCM_API SCM scm_denominator (SCM z);
282
283 /* fraction internal functions */
284 SCM_INTERNAL double scm_i_fraction2double (SCM z);
285 SCM_INTERNAL SCM scm_i_fraction_equalp (SCM x, SCM y);
286 SCM_INTERNAL int scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate);
287
288 /* general internal functions */
289 SCM_INTERNAL void scm_i_print_double (double val, SCM port);
290 SCM_INTERNAL void scm_i_print_complex (double real, double imag, SCM port);
291
292 /* conversion functions for integers */
293
294 SCM_API int scm_is_integer (SCM val);
295 SCM_API int scm_is_signed_integer (SCM val,
296 scm_t_intmax min, scm_t_intmax max);
297 SCM_API int scm_is_unsigned_integer (SCM val,
298 scm_t_uintmax min, scm_t_uintmax max);
299
300 SCM_API SCM scm_from_signed_integer (scm_t_intmax val);
301 SCM_API SCM scm_from_unsigned_integer (scm_t_uintmax val);
302
303 SCM_API scm_t_intmax scm_to_signed_integer (SCM val,
304 scm_t_intmax min,
305 scm_t_intmax max);
306 SCM_API scm_t_uintmax scm_to_unsigned_integer (SCM val,
307 scm_t_uintmax min,
308 scm_t_uintmax max);
309
310 SCM_API scm_t_int8 scm_to_int8 (SCM x);
311 SCM_API SCM scm_from_int8 (scm_t_int8 x);
312
313 SCM_API scm_t_uint8 scm_to_uint8 (SCM x);
314 SCM_API SCM scm_from_uint8 (scm_t_uint8 x);
315
316 SCM_API scm_t_int16 scm_to_int16 (SCM x);
317 SCM_API SCM scm_from_int16 (scm_t_int16 x);
318
319 SCM_API scm_t_uint16 scm_to_uint16 (SCM x);
320 SCM_API SCM scm_from_uint16 (scm_t_uint16 x);
321
322 SCM_API scm_t_int32 scm_to_int32 (SCM x);
323 SCM_API SCM scm_from_int32 (scm_t_int32 x);
324
325 SCM_API scm_t_uint32 scm_to_uint32 (SCM x);
326 SCM_API SCM scm_from_uint32 (scm_t_uint32 x);
327
328 SCM_API scm_t_wchar scm_to_wchar (SCM x);
329 SCM_API SCM scm_from_wchar (scm_t_wchar x);
330
331 #if SCM_HAVE_T_INT64
332
333 SCM_API scm_t_int64 scm_to_int64 (SCM x);
334 SCM_API SCM scm_from_int64 (scm_t_int64 x);
335
336 SCM_API scm_t_uint64 scm_to_uint64 (SCM x);
337 SCM_API SCM scm_from_uint64 (scm_t_uint64 x);
338
339 #endif
340
341 SCM_API void scm_to_mpz (SCM x, mpz_t rop);
342 SCM_API SCM scm_from_mpz (mpz_t rop);
343
344
345 /* The conversion functions for other types are aliased to the
346 appropriate ones from above. We pick the right one based on the
347 size of the type.
348
349 Not each and every possibility is covered by the code below, and
350 while it is trivial to complete the tests, it might be better to
351 just test for the 'sane' possibilities. When one of the tests
352 below fails, chances are good that some silent assumption somewhere
353 else will also fail.
354 */
355
356 #if SCM_SIZEOF_CHAR == 1
357 #define scm_to_schar scm_to_int8
358 #define scm_from_schar scm_from_int8
359 #define scm_to_uchar scm_to_uint8
360 #define scm_from_uchar scm_from_uint8
361 #if CHAR_MIN == 0
362 #define scm_to_char scm_to_uint8
363 #define scm_from_char scm_from_uint8
364 #else
365 #define scm_to_char scm_to_int8
366 #define scm_from_char scm_from_int8
367 #endif
368 #else
369 #error sizeof(char) is not 1.
370 #endif
371
372 #if SCM_SIZEOF_SHORT == 1
373 #define scm_to_short scm_to_int8
374 #define scm_from_short scm_from_int8
375 #define scm_to_ushort scm_to_uint8
376 #define scm_from_ushort scm_from_uint8
377 #else
378 #if SCM_SIZEOF_SHORT == 2
379 #define scm_to_short scm_to_int16
380 #define scm_from_short scm_from_int16
381 #define scm_to_ushort scm_to_uint16
382 #define scm_from_ushort scm_from_uint16
383 #else
384 #if SCM_SIZEOF_SHORT == 4
385 #define scm_to_short scm_to_int32
386 #define scm_from_short scm_from_int32
387 #define scm_to_ushort scm_to_uint32
388 #define scm_from_ushort scm_from_uint32
389 #else
390 #error sizeof(short) is not 1, 2, or 4.
391 #endif
392 #endif
393 #endif
394
395 #if SCM_SIZEOF_INT == 4
396 #define scm_to_int scm_to_int32
397 #define scm_from_int scm_from_int32
398 #define scm_to_uint scm_to_uint32
399 #define scm_from_uint scm_from_uint32
400 #else
401 #if SCM_SIZEOF_INT == 8
402 #define scm_to_int scm_to_int64
403 #define scm_from_int scm_from_int64
404 #define scm_to_uint scm_to_uint64
405 #define scm_from_uint scm_from_uint64
406 #else
407 #error sizeof(int) is not 4 or 8.
408 #endif
409 #endif
410
411 #if SCM_SIZEOF_LONG == 4
412 #define scm_to_long scm_to_int32
413 #define scm_from_long scm_from_int32
414 #define scm_to_ulong scm_to_uint32
415 #define scm_from_ulong scm_from_uint32
416 #else
417 #if SCM_SIZEOF_LONG == 8
418 #define scm_to_long scm_to_int64
419 #define scm_from_long scm_from_int64
420 #define scm_to_ulong scm_to_uint64
421 #define scm_from_ulong scm_from_uint64
422 #else
423 #error sizeof(long) is not 4 or 8.
424 #endif
425 #endif
426
427 #if SCM_SIZEOF_INTMAX == 4
428 #define scm_to_intmax scm_to_int32
429 #define scm_from_intmax scm_from_int32
430 #define scm_to_uintmax scm_to_uint32
431 #define scm_from_uintmax scm_from_uint32
432 #else
433 #if SCM_SIZEOF_INTMAX == 8
434 #define scm_to_intmax scm_to_int64
435 #define scm_from_intmax scm_from_int64
436 #define scm_to_uintmax scm_to_uint64
437 #define scm_from_uintmax scm_from_uint64
438 #else
439 #error sizeof(scm_t_intmax) is not 4 or 8.
440 #endif
441 #endif
442
443 #if SCM_SIZEOF_LONG_LONG == 0
444 #else
445 #if SCM_SIZEOF_LONG_LONG == 8
446 #define scm_to_long_long scm_to_int64
447 #define scm_from_long_long scm_from_int64
448 #define scm_to_ulong_long scm_to_uint64
449 #define scm_from_ulong_long scm_from_uint64
450 #else
451 #error sizeof(long long) is not 8.
452 #endif
453 #endif
454
455 #if SCM_SIZEOF_SIZE_T == 4
456 #define scm_to_ssize_t scm_to_int32
457 #define scm_from_ssize_t scm_from_int32
458 #define scm_to_size_t scm_to_uint32
459 #define scm_from_size_t scm_from_uint32
460 #else
461 #if SCM_SIZEOF_SIZE_T == 8
462 #define scm_to_ssize_t scm_to_int64
463 #define scm_from_ssize_t scm_from_int64
464 #define scm_to_size_t scm_to_uint64
465 #define scm_from_size_t scm_from_uint64
466 #else
467 #error sizeof(size_t) is not 4 or 8.
468 #endif
469 #endif
470
471 /* conversion functions for double */
472
473 SCM_API int scm_is_real (SCM val);
474 SCM_API int scm_is_rational (SCM val);
475 SCM_API double scm_to_double (SCM val);
476 SCM_API SCM scm_from_double (double val);
477
478 /* conversion functions for complex */
479
480 SCM_API int scm_is_complex (SCM val);
481 SCM_API SCM scm_c_make_rectangular (double re, double im);
482 SCM_API SCM scm_c_make_polar (double mag, double ang);
483 SCM_API double scm_c_real_part (SCM z);
484 SCM_API double scm_c_imag_part (SCM z);
485 SCM_API double scm_c_magnitude (SCM z);
486 SCM_API double scm_c_angle (SCM z);
487
488 SCM_API int scm_is_number (SCM val);
489
490 SCM_INTERNAL void scm_init_numbers (void);
491
492 #endif /* SCM_NUMBERS_H */
493
494 /*
495 Local Variables:
496 c-file-style: "gnu"
497 End:
498 */