#ifndef SCM_NUMBERS_H
#define SCM_NUMBERS_H
-/* Copyright (C) 1995,1996,1998,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1998,2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
\f
+#include <gmp.h>
+
#include "libguile/__scm.h"
#include "libguile/print.h"
#endif /* def FLT_MAX */
-/* SCM_INTBUFLEN is the maximum number of characters neccessary for the
- * printed or scm_string representation of an exact immediate.
+/* SCM_INTBUFLEN is the maximum number of characters neccessary for
+ * the printed or scm_string representation of an scm_t_intmax in
+ * radix 2. The buffer passed to scm_iint2str and scm_iuint2str must
+ * be of this size, for example.
*/
-#define SCM_INTBUFLEN (5 + SCM_LONG_BIT)
+#define SCM_INTBUFLEN (5 + SCM_CHAR_BIT*sizeof(scm_t_intmax))
\f
SCM_API SCM scm_logcount (SCM n);
SCM_API SCM scm_integer_length (SCM n);
-SCM_API size_t scm_iint2str (long num, int rad, char *p);
+SCM_API size_t scm_iint2str (scm_t_intmax num, int rad, char *p);
+SCM_API size_t scm_iuint2str (scm_t_uintmax num, int rad, char *p);
SCM_API SCM scm_number_to_string (SCM x, SCM radix);
SCM_API int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
SCM_API int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
SCM_API int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
SCM_API SCM scm_i_mem2number (const char *mem, size_t len, unsigned int radix);
SCM_API SCM scm_string_to_number (SCM str, SCM radix);
-SCM_API SCM scm_make_real (double x);
-SCM_API SCM scm_make_complex (double x, double y);
SCM_API SCM scm_bigequal (SCM x, SCM y);
SCM_API SCM scm_real_equalp (SCM x, SCM y);
SCM_API SCM scm_complex_equalp (SCM x, SCM y);
SCM_API SCM scm_number_p (SCM x);
+SCM_API SCM scm_complex_p (SCM x);
SCM_API SCM scm_real_p (SCM x);
+SCM_API SCM scm_rational_p (SCM z);
SCM_API SCM scm_integer_p (SCM x);
SCM_API SCM scm_inexact_p (SCM x);
SCM_API SCM scm_num_eq_p (SCM x, SCM y);
SCM_API SCM scm_sum (SCM x, SCM y);
SCM_API SCM scm_difference (SCM x, SCM y);
SCM_API SCM scm_product (SCM x, SCM y);
-SCM_API double scm_num2dbl (SCM a, const char * why);
SCM_API SCM scm_divide (SCM x, SCM y);
SCM_API SCM scm_floor (SCM x);
SCM_API SCM scm_ceiling (SCM x);
SCM_API double scm_asinh (double x);
SCM_API double scm_acosh (double x);
SCM_API double scm_atanh (double x);
-SCM_API double scm_truncate (double x);
-SCM_API double scm_round (double x);
+SCM_API double scm_c_truncate (double x);
+SCM_API double scm_c_round (double x);
SCM_API SCM scm_truncate_number (SCM x);
SCM_API SCM scm_round_number (SCM x);
SCM_API SCM scm_sys_expt (SCM z1, SCM z2);
SCM_API SCM scm_inexact_to_exact (SCM z);
SCM_API SCM scm_trunc (SCM x);
-SCM_API SCM scm_short2num (short n);
-SCM_API SCM scm_ushort2num (unsigned short n);
-SCM_API SCM scm_int2num (int n);
-SCM_API SCM scm_uint2num (unsigned int n);
-SCM_API SCM scm_long2num (long n);
-SCM_API SCM scm_ulong2num (unsigned long n);
-SCM_API SCM scm_size2num (size_t n);
-SCM_API SCM scm_ptrdiff2num (scm_t_ptrdiff n);
-SCM_API short scm_num2short (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API unsigned short scm_num2ushort (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API int scm_num2int (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API unsigned int scm_num2uint (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API long scm_num2long (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API unsigned long scm_num2ulong (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API scm_t_ptrdiff scm_num2ptrdiff (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API size_t scm_num2size (SCM num, unsigned long int pos,
- const char *s_caller);
-#if SCM_SIZEOF_LONG_LONG != 0
-SCM_API SCM scm_long_long2num (long long sl);
-SCM_API SCM scm_ulong_long2num (unsigned long long sl);
-SCM_API long long scm_num2long_long (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API unsigned long long scm_num2ulong_long (SCM num, unsigned long int pos,
- const char *s_caller);
-#endif
-
-SCM_API SCM scm_float2num (float n);
-SCM_API SCM scm_double2num (double n);
-SCM_API float scm_num2float (SCM num, unsigned long int pos,
- const char *s_caller);
-SCM_API double scm_num2double (SCM num, unsigned long int pos,
- const char *s_caller);
-
-
/* bignum internal functions */
SCM_API SCM scm_i_mkbig (void);
SCM_API SCM scm_i_normbig (SCM x);
SCM_API SCM scm_i_dbl2big (double d);
SCM_API SCM scm_i_dbl2num (double d);
SCM_API double scm_i_big2dbl (SCM b);
-SCM_API SCM scm_i_short2big (short n);
-SCM_API SCM scm_i_ushort2big (unsigned short n);
-SCM_API SCM scm_i_int2big (int n);
-SCM_API SCM scm_i_uint2big (unsigned int n);
SCM_API SCM scm_i_long2big (long n);
SCM_API SCM scm_i_ulong2big (unsigned long n);
-SCM_API SCM scm_i_size2big (size_t n);
-SCM_API SCM scm_i_ptrdiff2big (scm_t_ptrdiff n);
-
-#if SCM_SIZEOF_LONG_LONG != 0
-SCM_API SCM scm_i_long_long2big (long long n);
-SCM_API SCM scm_i_ulong_long2big (unsigned long long n);
-#endif
-
+SCM_API SCM scm_i_clonebig (SCM src_big, int same_sign_p);
/* ratio functions */
-SCM_API SCM scm_make_ratio (SCM num, SCM den);
SCM_API SCM scm_rationalize (SCM x, SCM err);
SCM_API SCM scm_numerator (SCM z);
SCM_API SCM scm_denominator (SCM z);
-SCM_API SCM scm_rational_p (SCM z);
/* fraction internal functions */
SCM_API double scm_i_fraction2double (SCM z);
SCM_API SCM scm_i_fraction_equalp (SCM x, SCM y);
SCM_API int scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate);
+/* general internal functions */
+SCM_API void scm_i_print_double (double val, SCM port);
+SCM_API void scm_i_print_complex (double real, double imag, SCM port);
-#ifdef GUILE_DEBUG
-SCM_API SCM scm_sys_check_number_conversions (void);
-#endif
-
-/* conversion functions */
+/* conversion functions for integers */
SCM_API int scm_is_integer (SCM val);
SCM_API int scm_is_signed_integer (SCM val,
scm_t_uintmax min,
scm_t_uintmax max);
-#define scm_to_schar(x) \
- ((signed char)scm_to_signed_integer ((x), SCHAR_MIN, SCHAR_MAX))
-#define scm_to_uchar(x) \
- ((unsigned char)scm_to_unsigned_integer ((x), 0, UCHAR_MAX))
-#if CHAR_MIN == 0
-#define scm_to_char scm_to_uchar
-#else
-#define scm_to_char scm_to_schar
-#endif
+SCM_API scm_t_int8 scm_to_int8 (SCM x);
+SCM_API SCM scm_from_int8 (scm_t_int8 x);
-#define scm_to_short(x) \
- ((short)scm_to_signed_integer ((x), SHRT_MIN, SHRT_MAX))
-#define scm_to_ushort(x) \
- ((unsigned short)scm_to_unsigned_integer ((x), 0, SHRT_MAX))
-
-#define scm_to_int(x) \
- ((int)scm_to_signed_integer ((x), INT_MIN, INT_MAX))
-#define scm_to_uint(x) \
- ((unsigned int)scm_to_unsigned_integer ((x), 0, UINT_MAX))
-
-#define scm_to_long(x) \
- ((long)scm_to_signed_integer ((x), LONG_MIN, LONG_MAX))
-#define scm_to_ulong(x) \
- ((unsigned long)scm_to_unsigned_integer ((x), 0, ULONG_MAX))
-
-#define scm_to_ssize_t(x) \
- ((ssize_t)scm_to_signed_integer ((x), -SSIZE_MAX-1, SSIZE_MAX))
-#define scm_to_size_t(x) \
- ((unsigned long)scm_to_unsigned_integer ((x), 0, SIZE_MAX))
-
-#if SCM_SIZEOF_LONG_LONG != 0
-#define scm_to_long_long(x) \
- ((long long)scm_to_signed_integer ((x), SCM_I_LLONG_MIN, SCM_I_LLONG_MAX))
-#define scm_to_ulong_long(x) \
- ((unsigned long long)scm_to_unsigned_integer ((x), 0, SCM_I_ULLONG_MAX))
-#endif
+SCM_API scm_t_uint8 scm_to_uint8 (SCM x);
+SCM_API SCM scm_from_uint8 (scm_t_uint8 x);
+
+SCM_API scm_t_int16 scm_to_int16 (SCM x);
+SCM_API SCM scm_from_int16 (scm_t_int16 x);
-#define scm_to_int8(x) \
- ((scm_t_int8)scm_to_signed_integer ((x), SCM_T_INT8_MIN, SCM_T_INT8_MAX))
-#define scm_to_uint8(x) \
- ((scm_t_uint8)scm_to_unsigned_integer ((x), 0, SCM_T_UINT8_MAX))
+SCM_API scm_t_uint16 scm_to_uint16 (SCM x);
+SCM_API SCM scm_from_uint16 (scm_t_uint16 x);
-#define scm_to_int16(x) \
- ((scm_t_int16)scm_to_signed_integer ((x), SCM_T_INT16_MIN, SCM_T_INT16_MAX))
-#define scm_to_uint16(x) \
- ((scm_t_uint16)scm_to_unsigned_integer ((x), 0, SCM_T_UINT16_MAX))
+SCM_API scm_t_int32 scm_to_int32 (SCM x);
+SCM_API SCM scm_from_int32 (scm_t_int32 x);
-#define scm_to_int32(x) \
- ((scm_t_int32)scm_to_signed_integer ((x), SCM_T_INT32_MIN, SCM_T_INT32_MAX))
-#define scm_to_uint32(x) \
- ((scm_t_uint32)scm_to_unsigned_integer ((x), 0, SCM_T_UINT32_MAX))
+SCM_API scm_t_uint32 scm_to_uint32 (SCM x);
+SCM_API SCM scm_from_uint32 (scm_t_uint32 x);
#if SCM_HAVE_T_INT64
-#define scm_to_int64(x) \
- ((scm_t_int64)scm_to_signed_integer ((x), SCM_T_INT64_MIN, SCM_T_INT64_MAX))
-#define scm_to_uint64(x) \
- ((scm_t_uint64)scm_to_unsigned_integer ((x), 0, SCM_T_UINT64_MAX))
-#endif
-#define scm_to_intmax(x) \
- ((scm_t_intmax)scm_to_signed_integer ((x),SCM_T_INTMAX_MIN,SCM_T_INTMAX_MAX))
-#define scm_to_uintmax(x) \
- ((scm_t_uintmax)scm_to_unsigned_integer ((x), 0, SCM_T_UINTMAX_MAX))
+SCM_API scm_t_int64 scm_to_int64 (SCM x);
+SCM_API SCM scm_from_int64 (scm_t_int64 x);
+
+SCM_API scm_t_uint64 scm_to_uint64 (SCM x);
+SCM_API SCM scm_from_uint64 (scm_t_uint64 x);
-#define scm_from_schar(x) scm_from_signed_integer ((signed char)(x))
-#define scm_from_uchar(x) scm_from_unsigned_integer ((unsigned char)(x))
-#if CHAR_MIN == 0
-#define scm_from_char scm_from_uchar
-#else
-#define scm_from_char scm_from_schar
#endif
-#define scm_from_short(x) scm_from_signed_integer ((short)(x))
-#define scm_from_ushort(x) scm_from_unsigned_integer ((unsigned short)(x))
+SCM_API void scm_to_mpz (SCM x, mpz_t rop);
+SCM_API SCM scm_from_mpz (mpz_t rop);
-#define scm_from_int(x) scm_from_signed_integer ((int)(x))
-#define scm_from_uint(x) scm_from_unsigned_integer ((unsigned int)(x))
-#define scm_from_long(x) scm_from_signed_integer ((long)(x))
-#define scm_from_ulong(x) scm_from_unsigned_integer ((unsigned long)(x))
+/* The conversion functions for other types are aliased to the
+ appropriate ones from above. We pick the right one based on the
+ size of the type.
-#define scm_from_ssize_t(x) scm_from_signed_integer ((ssize_t)(x))
-#define scm_from_size_t(x) scm_from_unsigned_integer ((size_t)(x))
+ Not each and every possibility is covered by the code below, and
+ while it is trivial to complete the tests, it might be better to
+ just test for the 'sane' possibilities. When one of the tests
+ below fails, chances are good that some silent assumption somewhere
+ else will also fail.
+*/
-#if SCM_SIZEOF_LONG_LONG != 0
-#define scm_from_long_long(x) scm_from_signed_integer ((long long)(x))
-#define scm_from_ulong_long(x) scm_from_unsigned_integer ((unsigned long long)(x))
+#if SCM_SIZEOF_CHAR == 1
+#define scm_to_schar scm_to_int8
+#define scm_from_schar scm_from_int8
+#define scm_to_uchar scm_to_uint8
+#define scm_from_uchar scm_from_uint8
+#if CHAR_MIN == 0
+#define scm_to_char scm_to_uint8
+#define scm_from_char scm_from_uint8
+#else
+#define scm_to_char scm_to_int8
+#define scm_from_char scm_from_int8
+#endif
+#else
+#error sizeof(char) is not 1.
#endif
-#define scm_from_int8(x) scm_from_signed_integer ((scm_t_int8)(x))
-#define scm_from_uint8(x) scm_from_unsigned_integer ((scm_t_uint8)(x))
+#if SCM_SIZEOF_SHORT == 1
+#define scm_to_short scm_to_int8
+#define scm_from_short scm_from_int8
+#define scm_to_ushort scm_to_uint8
+#define scm_from_ushort scm_from_uint8
+#else
+#if SCM_SIZEOF_SHORT == 2
+#define scm_to_short scm_to_int16
+#define scm_from_short scm_from_int16
+#define scm_to_ushort scm_to_uint16
+#define scm_from_ushort scm_from_uint16
+#else
+#if SCM_SIZEOF_SHORT == 4
+#define scm_to_short scm_to_int32
+#define scm_from_short scm_from_int32
+#define scm_to_ushort scm_to_uint32
+#define scm_from_ushort scm_from_uint32
+#else
+#error sizeof(short) is not 1, 2, or 4.
+#endif
+#endif
+#endif
-#define scm_from_int16(x) scm_from_signed_integer ((scm_t_int16)(x))
-#define scm_from_uint16(x) scm_from_unsigned_integer ((scm_t_uint16)(x))
+#if SCM_SIZEOF_INT == 4
+#define scm_to_int scm_to_int32
+#define scm_from_int scm_from_int32
+#define scm_to_uint scm_to_uint32
+#define scm_from_uint scm_from_uint32
+#else
+#if SCM_SIZEOF_INT == 8
+#define scm_to_int scm_to_int64
+#define scm_from_int scm_from_int64
+#define scm_to_uint scm_to_uint64
+#define scm_from_uint scm_from_uint64
+#else
+#error sizeof(int) is not 4 or 8.
+#endif
+#endif
-#define scm_from_int32(x) scm_from_signed_integer ((scm_t_int32)(x))
-#define scm_from_uint32(x) scm_from_unsigned_integer ((scm_t_uint32)(x))
+#if SCM_SIZEOF_LONG == 4
+#define scm_to_long scm_to_int32
+#define scm_from_long scm_from_int32
+#define scm_to_ulong scm_to_uint32
+#define scm_from_ulong scm_from_uint32
+#else
+#if SCM_SIZEOF_LONG == 8
+#define scm_to_long scm_to_int64
+#define scm_from_long scm_from_int64
+#define scm_to_ulong scm_to_uint64
+#define scm_from_ulong scm_from_uint64
+#else
+#error sizeof(long) is not 4 or 8.
+#endif
+#endif
-#if SCM_HAVE_T_INT64
-#define scm_from_int64(x) scm_from_signed_integer ((scm_t_int64)(x))
-#define scm_from_uint64(x) scm_from_unsigned_integer ((scm_t_uint64)(x))
+#if SCM_SIZEOF_INTMAX == 4
+#define scm_to_intmax scm_to_int32
+#define scm_from_intmax scm_from_int32
+#define scm_to_uintmax scm_to_uint32
+#define scm_from_uintmax scm_from_uint32
+#else
+#if SCM_SIZEOF_INTMAX == 8
+#define scm_to_intmax scm_to_int64
+#define scm_from_intmax scm_from_int64
+#define scm_to_uintmax scm_to_uint64
+#define scm_from_uintmax scm_from_uint64
+#else
+#error sizeof(scm_t_intmax) is not 4 or 8.
+#endif
+#endif
+
+#if SCM_SIZEOF_LONG_LONG == 0
+#else
+#if SCM_SIZEOF_LONG_LONG == 8
+#define scm_to_long_long scm_to_int64
+#define scm_from_long_long scm_from_int64
+#define scm_to_ulong_long scm_to_uint64
+#define scm_from_ulong_long scm_from_uint64
+#else
+#error sizeof(long long) is not 8.
+#endif
+#endif
+
+#if SCM_SIZEOF_SIZE_T == 4
+#define scm_to_ssize_t scm_to_int32
+#define scm_from_ssize_t scm_from_int32
+#define scm_to_size_t scm_to_uint32
+#define scm_from_size_t scm_from_uint32
+#else
+#if SCM_SIZEOF_SIZE_T == 8
+#define scm_to_ssize_t scm_to_int64
+#define scm_from_ssize_t scm_from_int64
+#define scm_to_size_t scm_to_uint64
+#define scm_from_size_t scm_from_uint64
+#else
+#error sizeof(size_t) is not 4 or 8.
+#endif
#endif
-#define scm_from_intmax(x) scm_from_signed_integer ((scm_t_intmax)(x))
-#define scm_from_uintmax(x) scm_from_unsigned_integer ((scm_t_uintmax)(x))
+/* conversion functions for double */
SCM_API int scm_is_real (SCM val);
+SCM_API int scm_is_rational (SCM val);
SCM_API double scm_to_double (SCM val);
SCM_API SCM scm_from_double (double val);
+/* conversion functions for complex */
+
+SCM_API int scm_is_complex (SCM val);
+SCM_API SCM scm_c_make_rectangular (double re, double im);
+SCM_API SCM scm_c_make_polar (double mag, double ang);
+SCM_API double scm_c_real_part (SCM z);
+SCM_API double scm_c_imag_part (SCM z);
+SCM_API double scm_c_magnitude (SCM z);
+SCM_API double scm_c_angle (SCM z);
+
+SCM_API int scm_is_number (SCM val);
+
SCM_API void scm_init_numbers (void);
#endif /* SCM_NUMBERS_H */