numbers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* classes: h_files */
  2. #ifndef NUMBERSH
  3. #define NUMBERSH
  4. /* Copyright (C) 1995, 1996, 1998, 2000, 2002 Free Software Foundation, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307 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. #include "libguile/__scm.h"
  45. #include "libguile/print.h"
  46. /* Immediate Numbers
  47. *
  48. * Inums are exact integer data that fits within an SCM word.
  49. *
  50. * SCM_INUMP applies only to values known to be Scheme objects.
  51. * In particular, SCM_INUMP (SCM_CAR (x)) is valid only if x is known
  52. * to be a SCM_CONSP. If x is only known to be a SCM_NIMP,
  53. * SCM_INUMP (SCM_CAR (x)) can give wrong answers.
  54. */
  55. /* SCM_SRS is signed right shift */
  56. #if (-1 == (((-1) << 2) + 2) >> 2)
  57. # define SCM_SRS(x, y) ((x) >> (y))
  58. #else
  59. # define SCM_SRS(x, y) ((SCM_UNPACK (x) < 0) ? ~((~SCM_UNPACK (x)) >> (y)) : (SCM_UNPACK (x) >> (y)))
  60. #endif /* (-1 == (((-1) << 2) + 2) >> 2) */
  61. #define SCM_INUMP(x) (2 & SCM_UNPACK (x))
  62. #define SCM_NINUMP(x) (!SCM_INUMP (x))
  63. #define SCM_MAKINUM(x) (SCM_PACK (((x) << 2) + 2L))
  64. #define SCM_INUM(x) (SCM_SRS (SCM_UNPACK (x), 2))
  65. /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */
  66. #define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
  67. #define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
  68. #define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
  69. /* A name for 0. */
  70. #define SCM_INUM0 (SCM_MAKINUM (0))
  71. /* SCM_MAXEXP is the maximum double precision expontent
  72. * SCM_FLTMAX is less than or scm_equal the largest single precision float
  73. */
  74. #ifdef STDC_HEADERS
  75. #ifndef GO32
  76. #include <float.h>
  77. #endif /* ndef GO32 */
  78. #endif /* def STDC_HEADERS */
  79. #ifdef DBL_MAX_10_EXP
  80. #define SCM_MAXEXP DBL_MAX_10_EXP
  81. #else
  82. #define SCM_MAXEXP 308 /* IEEE doubles */
  83. #endif /* def DBL_MAX_10_EXP */
  84. #ifdef FLT_MAX
  85. #define SCM_FLTMAX FLT_MAX
  86. #else
  87. #define SCM_FLTMAX 1e+23
  88. #endif /* def FLT_MAX */
  89. /* SCM_INTBUFLEN is the maximum number of characters neccessary for the
  90. * printed or scm_string representation of an exact immediate.
  91. */
  92. #ifndef SCM_CHAR_BIT
  93. # define SCM_CHAR_BIT 8
  94. #endif /* ndef SCM_CHAR_BIT */
  95. #ifndef SCM_LONG_BIT
  96. # define SCM_LONG_BIT (SCM_CHAR_BIT*sizeof(long)/sizeof(char))
  97. #endif /* ndef SCM_LONG_BIT */
  98. #define SCM_INTBUFLEN (5+SCM_LONG_BIT)
  99. /* Numbers
  100. */
  101. #define SCM_SLOPPY_INEXACTP(x) (SCM_TYP16S (x) == scm_tc16_real)
  102. #define SCM_SLOPPY_REALP(x) (SCM_TYP16 (x) == scm_tc16_real)
  103. #define SCM_SLOPPY_COMPLEXP(x) (SCM_TYP16 (x) == scm_tc16_complex)
  104. #define SCM_INEXACTP(x) (SCM_NIMP (x) && SCM_TYP16S (x) == scm_tc16_real)
  105. #define SCM_REALP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_real)
  106. #define SCM_COMPLEXP(x) (SCM_NIMP (x) && SCM_TYP16 (x) == scm_tc16_complex)
  107. #define SCM_REAL_VALUE(x) (((scm_double_t *) SCM2PTR (x))->real)
  108. #define SCM_COMPLEX_REAL(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->real)
  109. #define SCM_COMPLEX_IMAG(x) (((scm_complex_t *) SCM_CELL_WORD_1 (x))->imag)
  110. /* Define SCM_BIGDIG to an integer type whose size is smaller than long if
  111. * you want bignums. SCM_BIGRAD is one greater than the biggest SCM_BIGDIG.
  112. *
  113. * Define SCM_DIGSTOOBIG if the digits equivalent to a long won't fit in a long.
  114. */
  115. #ifdef BIGNUMS
  116. # ifdef _UNICOS
  117. # define SCM_DIGSTOOBIG
  118. # if (1L << 31) <= SCM_USHRT_MAX
  119. # define SCM_BIGDIG unsigned short
  120. # else
  121. # define SCM_BIGDIG unsigned int
  122. # endif /* (1L << 31) <= USHRT_MAX */
  123. # define SCM_BITSPERDIG 32
  124. # else
  125. # define SCM_BIGDIG unsigned short
  126. # define SCM_BITSPERDIG (sizeof(SCM_BIGDIG)*SCM_CHAR_BIT)
  127. # endif /* def _UNICOS */
  128. # define SCM_BIGRAD (1L << SCM_BITSPERDIG)
  129. # define SCM_DIGSPERLONG ((scm_sizet)((sizeof(long)*SCM_CHAR_BIT+SCM_BITSPERDIG-1)/SCM_BITSPERDIG))
  130. # define SCM_BIGUP(x) ((unsigned long)(x) << SCM_BITSPERDIG)
  131. # define SCM_LONGLONGBIGUP(x) ((ulong_long)(x) << SCM_BITSPERDIG)
  132. # define SCM_BIGDN(x) ((x) >> SCM_BITSPERDIG)
  133. # define SCM_BIGLO(x) ((x) & (SCM_BIGRAD-1))
  134. #endif /* def BIGNUMS */
  135. #ifndef SCM_BIGDIG
  136. /* Definition is not really used but helps various function
  137. * prototypes to compile with conditionalization.
  138. */
  139. # define SCM_BIGDIG unsigned short
  140. #endif /* ndef SCM_BIGDIG */
  141. #define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
  142. #define SCM_NUMP(x) (!SCM_IMP(x) && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_smob)
  143. #define SCM_BIGP(x) (!SCM_IMP (x) && (SCM_TYP16 (x) == scm_tc16_big))
  144. #define SCM_BIGSIGNFLAG 0x10000L
  145. #define SCM_BIGSIZEFIELD 17
  146. #define SCM_BIGSIGN(x) (SCM_CELL_WORD_0 (x) & SCM_BIGSIGNFLAG)
  147. #define SCM_BDIGITS(x) ((SCM_BIGDIG *) (SCM_CELL_WORD_1 (x)))
  148. #define SCM_NUMDIGS(x) ((scm_sizet) (SCM_CELL_WORD_0 (x) >> SCM_BIGSIZEFIELD))
  149. #define SCM_SETNUMDIGS(x, v, sign) \
  150. SCM_SET_CELL_WORD_0 (x, \
  151. scm_tc16_big \
  152. | ((sign) ? SCM_BIGSIGNFLAG : 0) \
  153. | (((v) + 0L) << SCM_BIGSIZEFIELD))
  154. typedef struct scm_double_t
  155. {
  156. SCM type;
  157. SCM pad;
  158. double real;
  159. } scm_double_t;
  160. typedef struct scm_complex_t
  161. {
  162. double real;
  163. double imag;
  164. } scm_complex_t;
  165. extern SCM scm_exact_p (SCM x);
  166. extern SCM scm_odd_p (SCM n);
  167. extern SCM scm_even_p (SCM n);
  168. extern SCM scm_abs (SCM x);
  169. extern SCM scm_quotient (SCM x, SCM y);
  170. extern SCM scm_remainder (SCM x, SCM y);
  171. extern SCM scm_modulo (SCM x, SCM y);
  172. extern SCM scm_gcd (SCM x, SCM y);
  173. extern SCM scm_lcm (SCM n1, SCM n2);
  174. extern SCM scm_logand (SCM n1, SCM n2);
  175. extern SCM scm_logior (SCM n1, SCM n2);
  176. extern SCM scm_logxor (SCM n1, SCM n2);
  177. extern SCM scm_logtest (SCM n1, SCM n2);
  178. extern SCM scm_logbit_p (SCM n1, SCM n2);
  179. extern SCM scm_lognot (SCM n);
  180. extern SCM scm_integer_expt (SCM z1, SCM z2);
  181. extern SCM scm_ash (SCM n, SCM cnt);
  182. extern SCM scm_bit_extract (SCM n, SCM start, SCM end);
  183. extern SCM scm_logcount (SCM n);
  184. extern SCM scm_integer_length (SCM n);
  185. extern SCM scm_mkbig (scm_sizet nlen, int sign);
  186. extern SCM scm_big2inum (SCM b, scm_sizet l);
  187. extern SCM scm_adjbig (SCM b, scm_sizet nlen);
  188. extern SCM scm_normbig (SCM b);
  189. extern SCM scm_copybig (SCM b, int sign);
  190. extern SCM scm_long2big (long n);
  191. #ifdef HAVE_LONG_LONGS
  192. extern SCM scm_long_long2big (long_long n);
  193. #endif
  194. extern SCM scm_2ulong2big (unsigned long * np);
  195. extern SCM scm_ulong2big (unsigned long n);
  196. extern int scm_bigcomp (SCM x, SCM y);
  197. extern long scm_pseudolong (long x);
  198. extern void scm_longdigs (long x, SCM_BIGDIG digs[]);
  199. extern SCM scm_addbig (SCM_BIGDIG *x, scm_sizet nx, int xsgn, SCM bigy, int sgny);
  200. extern SCM scm_mulbig (SCM_BIGDIG *x, scm_sizet nx, SCM_BIGDIG *y, scm_sizet ny, int sgn);
  201. extern unsigned int scm_divbigdig (SCM_BIGDIG *ds, scm_sizet h, SCM_BIGDIG div);
  202. extern scm_sizet scm_iint2str (long num, int rad, char *p);
  203. extern SCM scm_number_to_string (SCM x, SCM radix);
  204. extern int scm_print_real (SCM sexp, SCM port, scm_print_state *pstate);
  205. extern int scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate);
  206. extern int scm_bigprint (SCM exp, SCM port, scm_print_state *pstate);
  207. extern SCM scm_istr2int (char *str, long len, long radix);
  208. extern SCM scm_istr2flo (char *str, long len, long radix);
  209. extern SCM scm_istring2number (char *str, long len, long radix);
  210. extern SCM scm_string_to_number (SCM str, SCM radix);
  211. extern SCM scm_make_real (double x);
  212. extern SCM scm_make_complex (double x, double y);
  213. extern SCM scm_bigequal (SCM x, SCM y);
  214. extern SCM scm_real_equalp (SCM x, SCM y);
  215. extern SCM scm_complex_equalp (SCM x, SCM y);
  216. extern SCM scm_number_p (SCM x);
  217. extern SCM scm_real_p (SCM x);
  218. extern SCM scm_integer_p (SCM x);
  219. extern SCM scm_inexact_p (SCM x);
  220. extern SCM scm_num_eq_p (SCM x, SCM y);
  221. extern SCM scm_less_p (SCM x, SCM y);
  222. extern SCM scm_gr_p (SCM x, SCM y);
  223. extern SCM scm_leq_p (SCM x, SCM y);
  224. extern SCM scm_geq_p (SCM x, SCM y);
  225. extern SCM scm_zero_p (SCM z);
  226. extern SCM scm_positive_p (SCM x);
  227. extern SCM scm_negative_p (SCM x);
  228. extern SCM scm_max (SCM x, SCM y);
  229. extern SCM scm_min (SCM x, SCM y);
  230. extern SCM scm_sum (SCM x, SCM y);
  231. extern SCM scm_difference (SCM x, SCM y);
  232. extern SCM scm_product (SCM x, SCM y);
  233. extern double scm_num2dbl (SCM a, const char * why);
  234. extern SCM scm_divide (SCM x, SCM y);
  235. extern double scm_asinh (double x);
  236. extern double scm_acosh (double x);
  237. extern double scm_atanh (double x);
  238. extern double scm_truncate (double x);
  239. extern double scm_round (double x);
  240. extern double scm_exact_to_inexact (double z);
  241. extern SCM scm_sys_expt (SCM z1, SCM z2);
  242. extern SCM scm_sys_atan2 (SCM z1, SCM z2);
  243. extern SCM scm_make_rectangular (SCM z1, SCM z2);
  244. extern SCM scm_make_polar (SCM z1, SCM z2);
  245. extern SCM scm_real_part (SCM z);
  246. extern SCM scm_imag_part (SCM z);
  247. extern SCM scm_magnitude (SCM z);
  248. extern SCM scm_angle (SCM z);
  249. extern SCM scm_inexact_to_exact (SCM z);
  250. extern SCM scm_trunc (SCM x);
  251. extern SCM scm_dbl2big (double d);
  252. extern double scm_big2dbl (SCM b);
  253. extern SCM scm_long2num (long sl);
  254. extern SCM scm_ulong2num (unsigned long sl);
  255. extern long scm_num2long (SCM num, char *pos, const char *s_caller);
  256. #ifdef HAVE_LONG_LONGS
  257. extern SCM scm_long_long2num (long_long sl);
  258. extern long_long scm_num2long_long (SCM num, char *pos,
  259. const char *s_caller);
  260. #endif
  261. extern unsigned long scm_num2ulong (SCM num, char *pos,
  262. const char *s_caller);
  263. extern void scm_init_numbers (void);
  264. #if (SCM_DEBUG_DEPRECATED == 0)
  265. typedef struct scm_dblproc
  266. {
  267. char *scm_string;
  268. double (*cproc) ();
  269. } scm_dblproc;
  270. #define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
  271. #define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  272. #define SCM_INEXP(x) SCM_INEXACTP(x)
  273. #define SCM_CPLXP(x) SCM_COMPLEXP(x)
  274. #define SCM_REAL(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
  275. #define SCM_IMAG(x) (SCM_SLOPPY_REALP (x) ? 0.0 : SCM_COMPLEX_IMAG (x))
  276. #define SCM_REALPART(x) (SCM_SLOPPY_REALP (x) ? SCM_REAL_VALUE (x) : SCM_COMPLEX_REAL (x))
  277. #define scm_makdbl scm_make_complex
  278. #define SCM_SINGP(x) 0
  279. #define SCM_NUM2DBL(x) scm_num2dbl(x, "SCM_NUM2DBL")
  280. #ifndef SCM_BIGDIG
  281. # define SCM_NO_BIGDIG
  282. #endif
  283. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  284. #endif /* NUMBERSH */
  285. /*
  286. Local Variables:
  287. c-file-style: "gnu"
  288. End:
  289. */