You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3366 lines
116 KiB

25 years ago
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename cln.info
  4. @settitle CLN, a Class Library for Numbers
  5. @c @setchapternewpage off
  6. @c For `info' only.
  7. @paragraphindent 0
  8. @c For TeX only.
  9. @iftex
  10. @c I hate putting "@noindent" in front of every paragraph.
  11. @parindent=0pt
  12. @end iftex
  13. @c %**end of header
  14. @c My own index.
  15. @defindex my
  16. @c Don't need the other types of indices.
  17. @synindex cp my
  18. @synindex fn my
  19. @synindex vr my
  20. @synindex ky my
  21. @synindex pg my
  22. @synindex tp my
  23. @c For `info' only.
  24. @ifinfo
  25. This file documents @sc{cln}, a Class Library for Numbers.
  26. Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
  27. Richard Kreckel, @code{<kreckel@@ginac.de>}.
  28. Copyright (C) Bruno Haible 1995, 1996, 1997, 1998, 1999, 2000.
  29. Permission is granted to make and distribute verbatim copies of
  30. this manual provided the copyright notice and this permission notice
  31. are preserved on all copies.
  32. @ignore
  33. Permission is granted to process this file through TeX and print the
  34. results, provided the printed document carries copying permission
  35. notice identical to this one except for the removal of this paragraph
  36. (this paragraph not being relevant to the printed manual).
  37. @end ignore
  38. Permission is granted to copy and distribute modified versions of this
  39. manual under the conditions for verbatim copying, provided that the entire
  40. resulting derived work is distributed under the terms of a permission
  41. notice identical to this one.
  42. Permission is granted to copy and distribute translations of this manual
  43. into another language, under the above conditions for modified versions,
  44. except that this permission notice may be stated in a translation approved
  45. by the author.
  46. @end ifinfo
  47. @c For TeX only.
  48. @c prevent ugly black rectangles on overfull hbox lines:
  49. @finalout
  50. @titlepage
  51. @title CLN, a Class Library for Numbers
  52. @author by Bruno Haible
  53. @page
  54. @vskip 0pt plus 1filll
  55. Copyright @copyright{} Bruno Haible 1995, 1996, 1997, 1998, 1999, 2000.
  56. @sp 2
  57. Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
  58. Richard Kreckel, @code{<kreckel@@ginac.de>}.
  59. Permission is granted to make and distribute verbatim copies of
  60. this manual provided the copyright notice and this permission notice
  61. are preserved on all copies.
  62. Permission is granted to copy and distribute modified versions of this
  63. manual under the conditions for verbatim copying, provided that the entire
  64. resulting derived work is distributed under the terms of a permission
  65. notice identical to this one.
  66. Permission is granted to copy and distribute translations of this manual
  67. into another language, under the above conditions for modified versions,
  68. except that this permission notice may be stated in a translation approved
  69. by the author.
  70. @end titlepage
  71. @page
  72. @node Top, Introduction, (dir), (dir)
  73. @c @menu
  74. @c * Introduction:: Introduction
  75. @c @end menu
  76. @node Introduction, Top, Top, Top
  77. @comment node-name, next, previous, up
  78. @chapter Introduction
  79. @noindent
  80. CLN is a library for computations with all kinds of numbers.
  81. It has a rich set of number classes:
  82. @itemize @bullet
  83. @item
  84. Integers (with unlimited precision),
  85. @item
  86. Rational numbers,
  87. @item
  88. Floating-point numbers:
  89. @itemize @minus
  90. @item
  91. Short float,
  92. @item
  93. Single float,
  94. @item
  95. Double float,
  96. @item
  97. Long float (with unlimited precision),
  98. @end itemize
  99. @item
  100. Complex numbers,
  101. @item
  102. Modular integers (integers modulo a fixed integer),
  103. @item
  104. Univariate polynomials.
  105. @end itemize
  106. @noindent
  107. The subtypes of the complex numbers among these are exactly the
  108. types of numbers known to the Common Lisp language. Therefore
  109. @code{CLN} can be used for Common Lisp implementations, giving
  110. @samp{CLN} another meaning: it becomes an abbreviation of
  111. ``Common Lisp Numbers''.
  112. @noindent
  113. The CLN package implements
  114. @itemize @bullet
  115. @item
  116. Elementary functions (@code{+}, @code{-}, @code{*}, @code{/}, @code{sqrt},
  117. comparisons, @dots{}),
  118. @item
  119. Logical functions (logical @code{and}, @code{or}, @code{not}, @dots{}),
  120. @item
  121. Transcendental functions (exponential, logarithmic, trigonometric, hyperbolic
  122. functions and their inverse functions).
  123. @end itemize
  124. @noindent
  125. CLN is a C++ library. Using C++ as an implementation language provides
  126. @itemize @bullet
  127. @item
  128. efficiency: it compiles to machine code,
  129. @item
  130. type safety: the C++ compiler knows about the number types and complains
  131. if, for example, you try to assign a float to an integer variable.
  132. @item
  133. algebraic syntax: You can use the @code{+}, @code{-}, @code{*}, @code{=},
  134. @code{==}, @dots{} operators as in C or C++.
  135. @end itemize
  136. @noindent
  137. CLN is memory efficient:
  138. @itemize @bullet
  139. @item
  140. Small integers and short floats are immediate, not heap allocated.
  141. @item
  142. Heap-allocated memory is reclaimed through an automatic, non-interruptive
  143. garbage collection.
  144. @end itemize
  145. @noindent
  146. CLN is speed efficient:
  147. @itemize @bullet
  148. @item
  149. The kernel of CLN has been written in assembly language for some CPUs
  150. (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
  151. @item
  152. On all CPUs, CLN uses the superefficient low-level routines from GNU
  153. GMP version 2.
  154. @item
  155. It uses Karatsuba multiplication, which is significantly faster
  156. for large numbers than the standard multiplication algorithm.
  157. @item
  158. For very large numbers (more than 12000 decimal digits), it uses
  159. @iftex
  160. Sch{@"o}nhage-Strassen
  161. @end iftex
  162. @ifinfo
  163. Sch�nhage-Strassen
  164. @end ifinfo
  165. multiplication, which is an asymptotically
  166. optimal multiplication algorithm, for multiplication, division and
  167. radix conversion.
  168. @end itemize
  169. @noindent
  170. CLN aims at being easily integrated into larger software packages:
  171. @itemize @bullet
  172. @item
  173. The garbage collection imposes no burden on the main application.
  174. @item
  175. The library provides hooks for memory allocation and exceptions.
  176. @end itemize
  177. @chapter Installation
  178. This section describes how to install the CLN package on your system.
  179. @section Prerequisites
  180. @subsection C++ compiler
  181. To build CLN, you need a C++ compiler.
  182. Actually, you need GNU @code{g++ 2.7.0} or newer.
  183. On HPPA, you need GNU @code{g++ 2.8.0} or newer.
  184. I recommend GNU @code{egcs 1.1} or newer.
  185. The following C++ features are used:
  186. classes, member functions,
  187. overloading of functions and operators,
  188. constructors and destructors, inline, const,
  189. multiple inheritance, templates.
  190. The following C++ features are not used:
  191. @code{new}, @code{delete}, virtual inheritance,
  192. exceptions.
  193. CLN relies on semi-automatic ordering of initializations
  194. of static and global variables, a feature which I could
  195. implement for GNU g++ only.
  196. @ignore
  197. @comment cl_modules.h requires g++
  198. Therefore nearly any C++ compiler will do.
  199. The following C++ compilers are known to compile CLN:
  200. @itemize @minus
  201. @item
  202. GNU @code{g++ 2.7.0}, @code{g++ 2.7.2}
  203. @item
  204. SGI @code{CC 4}
  205. @end itemize
  206. The following C++ compilers are known to be unusable for CLN:
  207. @itemize @minus
  208. @item
  209. On SunOS 4, @code{CC 2.1}, because it doesn't grok @code{//} comments
  210. in lines containing @code{#if} or @code{#elif} preprocessor commands.
  211. @item
  212. On AIX 3.2.5, @code{xlC}, because it doesn't grok the template syntax
  213. in @code{cl_SV.h} and @code{cl_GV.h}, because it forces most class types
  214. to have default constructors, and because it probably miscompiles the
  215. integer multiplication routines.
  216. @item
  217. On AIX 4.1.4.0, @code{xlC}, because when optimizing, it sometimes converts
  218. @code{short}s to @code{int}s by zero-extend.
  219. @item
  220. GNU @code{g++ 2.5.8}
  221. @item
  222. On HPPA, GNU @code{g++ 2.7.x}, because the semi-automatic ordering of
  223. initializations will not work.
  224. @end itemize
  225. @end ignore
  226. @subsection Make utility
  227. To build CLN, you also need to have GNU @code{make} installed.
  228. @subsection Sed utility
  229. To build CLN on HP-UX, you also need to have GNU @code{sed} installed.
  230. This is because the libtool script, which creates the CLN library, relies
  231. on @code{sed}, and the vendor's @code{sed} utility on these systems is too
  232. limited.
  233. @section Building the library
  234. As with any autoconfiguring GNU software, installation is as easy as this:
  235. @example
  236. $ ./configure
  237. $ make
  238. $ make check
  239. @end example
  240. If on your system, @samp{make} is not GNU @code{make}, you have to use
  241. @samp{gmake} instead of @samp{make} above.
  242. The @code{configure} command checks out some features of your system and
  243. C++ compiler and builds the @code{Makefile}s. The @code{make} command
  244. builds the library. This step may take 4 hours on an average workstation.
  245. The @code{make check} runs some test to check that no important subroutine
  246. has been miscompiled.
  247. The @code{configure} command accepts options. To get a summary of them, try
  248. @example
  249. $ ./configure --help
  250. @end example
  251. Some of the options are explained in detail in the @samp{INSTALL.generic} file.
  252. You can specify the C compiler, the C++ compiler and their options through
  253. the following environment variables when running @code{configure}:
  254. @table @code
  255. @item CC
  256. Specifies the C compiler.
  257. @item CFLAGS
  258. Flags to be given to the C compiler when compiling programs (not when linking).
  259. @item CXX
  260. Specifies the C++ compiler.
  261. @item CXXFLAGS
  262. Flags to be given to the C++ compiler when compiling programs (not when linking).
  263. @end table
  264. Examples:
  265. @example
  266. $ CC="gcc" CFLAGS="-O" CXX="g++" CXXFLAGS="-O" ./configure
  267. $ CC="gcc -V 2.7.2" CFLAGS="-O -g" \
  268. CXX="g++ -V 2.7.2" CXXFLAGS="-O -g" ./configure
  269. $ CC="gcc -V 2.8.1" CFLAGS="-O -fno-exceptions" \
  270. CXX="g++ -V 2.8.1" CXXFLAGS="-O -fno-exceptions" ./configure
  271. $ CC="gcc -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" \
  272. CXX="g++ -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" ./configure
  273. @end example
  274. @ignore
  275. @comment cl_modules.h requires g++
  276. You should not mix GNU and non-GNU compilers. So, if @code{CXX} is a non-GNU
  277. compiler, @code{CC} should be set to a non-GNU compiler as well. Examples:
  278. @example
  279. $ CC="cc" CFLAGS="-O" CXX="CC" CXXFLAGS="-O" ./configure
  280. $ CC="gcc -V 2.7.0" CFLAGS="-g" CXX="g++ -V 2.7.0" CXXFLAGS="-g" ./configure
  281. @end example
  282. On SGI Irix 5, if you wish not to use @code{g++}:
  283. @example
  284. $ CC="cc" CFLAGS="-O" CXX="CC" CXXFLAGS="-O -Olimit 16000" ./configure
  285. @end example
  286. On SGI Irix 6, if you wish not to use @code{g++}:
  287. @example
  288. $ CC="cc -32" CFLAGS="-O" CXX="CC -32" CXXFLAGS="-O -Olimit 34000" \
  289. ./configure --without-gmp
  290. $ CC="cc -n32" CFLAGS="-O" CXX="CC -n32" CXXFLAGS="-O \
  291. -OPT:const_copy_limit=32400 -OPT:global_limit=32400 -OPT:fprop_limit=4000" \
  292. ./configure --without-gmp
  293. @end example
  294. @end ignore
  295. Note that for these environment variables to take effect, you have to set
  296. them (assuming a Bourne-compatible shell) on the same line as the
  297. @code{configure} command. If you made the settings in earlier shell
  298. commands, you have to @code{export} the environment variables before
  299. calling @code{configure}. In a @code{csh} shell, you have to use the
  300. @samp{setenv} command for setting each of the environment variables.
  301. On Linux, @code{g++} needs 15 MB to compile the tests. So you should better
  302. have 17 MB swap space and 1 MB room in $TMPDIR.
  303. If you use @code{g++} version 2.7.x, don't add @samp{-O2} to the CXXFLAGS,
  304. because @samp{g++ -O} generates better code for CLN than @samp{g++ -O2}.
  305. If you use @code{g++} version 2.8.x or egcs-2.91.x (a.k.a. egcs-1.1) or
  306. gcc-2.95.x, I recommend adding @samp{-fno-exceptions} to the CXXFLAGS.
  307. This will likely generate better code.
  308. If you use @code{g++} version egcs-2.91.x (egcs-1.1) or gcc-2.95.x on Sparc,
  309. add either @samp{-O} or @samp{-O2 -fno-schedule-insns} to the CXXFLAGS.
  310. With full @samp{-O2}, @code{g++} miscompiles the division routines. Also, for
  311. --enable-shared to work, you need egcs-1.1.2 or newer.
  312. On MIPS (SGI Irix 6), pass option @code{--without-gmp} to configure. gmp does
  313. not work when compiled in @samp{n32} binary format on Irix.
  314. By default, only a static library is built. You can build CLN as a shared
  315. library too, by calling @code{configure} with the option @samp{--enable-shared}.
  316. To get it built as a shared library only, call @code{configure} with the options
  317. @samp{--enable-shared --disable-static}.
  318. If you use @code{g++} version egcs-2.91.x (egcs-1.1) on Sparc, you cannot
  319. use @samp{--enable-shared} because @code{g++} would miscompile parts of the
  320. library.
  321. @section Installing the library
  322. As with any autoconfiguring GNU software, installation is as easy as this:
  323. @example
  324. $ make install
  325. @end example
  326. The @samp{make install} command installs the library and the include files
  327. into public places (@file{/usr/local/lib/} and @file{/usr/local/include/},
  328. if you haven't specified a @code{--prefix} option to @code{configure}).
  329. This step may require superuser privileges.
  330. If you have already built the library and wish to install it, but didn't
  331. specify @code{--prefix=@dots{}} at configure time, just re-run
  332. @code{configure}, giving it the same options as the first time, plus
  333. the @code{--prefix=@dots{}} option.
  334. @section Cleaning up
  335. You can remove system-dependent files generated by @code{make} through
  336. @example
  337. $ make clean
  338. @end example
  339. You can remove all files generated by @code{make}, thus reverting to a
  340. virgin distribution of CLN, through
  341. @example
  342. $ make distclean
  343. @end example
  344. @chapter Ordinary number types
  345. CLN implements the following class hierarchy:
  346. @example
  347. Number
  348. cl_number
  349. <cl_number.h>
  350. |
  351. |
  352. Real or complex number
  353. cl_N
  354. <cl_complex.h>
  355. |
  356. |
  357. Real number
  358. cl_R
  359. <cl_real.h>
  360. |
  361. +-------------------+-------------------+
  362. | |
  363. Rational number Floating-point number
  364. cl_RA cl_F
  365. <cl_rational.h> <cl_float.h>
  366. | |
  367. | +-------------+-------------+-------------+
  368. Integer | | | |
  369. cl_I Short-Float Single-Float Double-Float Long-Float
  370. <cl_integer.h> cl_SF cl_FF cl_DF cl_LF
  371. <cl_sfloat.h> <cl_ffloat.h> <cl_dfloat.h> <cl_lfloat.h>
  372. @end example
  373. The base class @code{cl_number} is an abstract base class.
  374. It is not useful to declare a variable of this type except if you want
  375. to completely disable compile-time type checking and use run-time type
  376. checking instead.
  377. The class @code{cl_N} comprises real and complex numbers. There is
  378. no special class for complex numbers since complex numbers with imaginary
  379. part @code{0} are automatically converted to real numbers.
  380. The class @code{cl_R} comprises real numbers of different kinds. It is an
  381. abstract class.
  382. The class @code{cl_RA} comprises exact real numbers: rational numbers, including
  383. integers. There is no special class for non-integral rational numbers
  384. since rational numbers with denominator @code{1} are automatically converted
  385. to integers.
  386. The class @code{cl_F} implements floating-point approximations to real numbers.
  387. It is an abstract class.
  388. @section Exact numbers
  389. Some numbers are represented as exact numbers: there is no loss of information
  390. when such a number is converted from its mathematical value to its internal
  391. representation. On exact numbers, the elementary operations (@code{+},
  392. @code{-}, @code{*}, @code{/}, comparisons, @dots{}) compute the completely
  393. correct result.
  394. In CLN, the exact numbers are:
  395. @itemize @bullet
  396. @item
  397. rational numbers (including integers),
  398. @item
  399. complex numbers whose real and imaginary parts are both rational numbers.
  400. @end itemize
  401. Rational numbers are always normalized to the form
  402. @code{@var{numerator}/@var{denominator}} where the numerator and denominator
  403. are coprime integers and the denominator is positive. If the resulting
  404. denominator is @code{1}, the rational number is converted to an integer.
  405. Small integers (typically in the range @code{-2^30}@dots{}@code{2^30-1},
  406. for 32-bit machines) are especially efficient, because they consume no heap
  407. allocation. Otherwise the distinction between these immediate integers
  408. (called ``fixnums'') and heap allocated integers (called ``bignums'')
  409. is completely transparent.
  410. @section Floating-point numbers
  411. Not all real numbers can be represented exactly. (There is an easy mathematical
  412. proof for this: Only a countable set of numbers can be stored exactly in
  413. a computer, even if one assumes that it has unlimited storage. But there
  414. are uncountably many real numbers.) So some approximation is needed.
  415. CLN implements ordinary floating-point numbers, with mantissa and exponent.
  416. The elementary operations (@code{+}, @code{-}, @code{*}, @code{/}, @dots{})
  417. only return approximate results. For example, the value of the expression
  418. @code{(cl_F) 0.3 + (cl_F) 0.4} prints as @samp{0.70000005}, not as
  419. @samp{0.7}. Rounding errors like this one are inevitable when computing
  420. with floating-point numbers.
  421. Nevertheless, CLN rounds the floating-point results of the operations @code{+},
  422. @code{-}, @code{*}, @code{/}, @code{sqrt} according to the ``round-to-even''
  423. rule: It first computes the exact mathematical result and then returns the
  424. floating-point number which is nearest to this. If two floating-point numbers
  425. are equally distant from the ideal result, the one with a @code{0} in its least
  426. significant mantissa bit is chosen.
  427. Similarly, testing floating point numbers for equality @samp{x == y}
  428. is gambling with random errors. Better check for @samp{abs(x - y) < epsilon}
  429. for some well-chosen @code{epsilon}.
  430. Floating point numbers come in four flavors:
  431. @itemize @bullet
  432. @item
  433. Short floats, type @code{cl_SF}.
  434. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  435. and 17 mantissa bits (including the ``hidden'' bit).
  436. They don't consume heap allocation.
  437. @item
  438. Single floats, type @code{cl_FF}.
  439. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  440. and 24 mantissa bits (including the ``hidden'' bit).
  441. In CLN, they are represented as IEEE single-precision floating point numbers.
  442. This corresponds closely to the C/C++ type @samp{float}.
  443. @item
  444. Double floats, type @code{cl_DF}.
  445. They have 1 sign bit, 11 exponent bits (including the exponent's sign),
  446. and 53 mantissa bits (including the ``hidden'' bit).
  447. In CLN, they are represented as IEEE double-precision floating point numbers.
  448. This corresponds closely to the C/C++ type @samp{double}.
  449. @item
  450. Long floats, type @code{cl_LF}.
  451. They have 1 sign bit, 32 exponent bits (including the exponent's sign),
  452. and n mantissa bits (including the ``hidden'' bit), where n >= 64.
  453. The precision of a long float is unlimited, but once created, a long float
  454. has a fixed precision. (No ``lazy recomputation''.)
  455. @end itemize
  456. Of course, computations with long floats are more expensive than those
  457. with smaller floating-point formats.
  458. CLN does not implement features like NaNs, denormalized numbers and
  459. gradual underflow. If the exponent range of some floating-point type
  460. is too limited for your application, choose another floating-point type
  461. with larger exponent range.
  462. As a user of CLN, you can forget about the differences between the
  463. four floating-point types and just declare all your floating-point
  464. variables as being of type @code{cl_F}. This has the advantage that
  465. when you change the precision of some computation (say, from @code{cl_DF}
  466. to @code{cl_LF}), you don't have to change the code, only the precision
  467. of the initial values. Also, many transcendental functions have been
  468. declared as returning a @code{cl_F} when the argument is a @code{cl_F},
  469. but such declarations are missing for the types @code{cl_SF}, @code{cl_FF},
  470. @code{cl_DF}, @code{cl_LF}. (Such declarations would be wrong if
  471. the floating point contagion rule happened to change in the future.)
  472. @section Complex numbers
  473. Complex numbers, as implemented by the class @code{cl_N}, have a real
  474. part and an imaginary part, both real numbers. A complex number whose
  475. imaginary part is the exact number @code{0} is automatically converted
  476. to a real number.
  477. Complex numbers can arise from real numbers alone, for example
  478. through application of @code{sqrt} or transcendental functions.
  479. @section Conversions
  480. Conversions from any class to any its superclasses (``base classes'' in
  481. C++ terminology) is done automatically.
  482. Conversions from the C built-in types @samp{long} and @samp{unsigned long}
  483. are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
  484. @code{cl_N} and @code{cl_number}.
  485. Conversions from the C built-in types @samp{int} and @samp{unsigned int}
  486. are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
  487. @code{cl_N} and @code{cl_number}. However, these conversions emphasize
  488. efficiency. Their range is therefore limited:
  489. @itemize @minus
  490. @item
  491. The conversion from @samp{int} works only if the argument is < 2^29 and > -2^29.
  492. @item
  493. The conversion from @samp{unsigned int} works only if the argument is < 2^29.
  494. @end itemize
  495. In a declaration like @samp{cl_I x = 10;} the C++ compiler is able to
  496. do the conversion of @code{10} from @samp{int} to @samp{cl_I} at compile time
  497. already. On the other hand, code like @samp{cl_I x = 1000000000;} is
  498. in error.
  499. So, if you want to be sure that an @samp{int} whose magnitude is not guaranteed
  500. to be < 2^29 is correctly converted to a @samp{cl_I}, first convert it to a
  501. @samp{long}. Similarly, if a large @samp{unsigned int} is to be converted to a
  502. @samp{cl_I}, first convert it to an @samp{unsigned long}.
  503. Conversions from the C built-in type @samp{float} are provided for the classes
  504. @code{cl_FF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
  505. Conversions from the C built-in type @samp{double} are provided for the classes
  506. @code{cl_DF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
  507. Conversions from @samp{const char *} are provided for the classes
  508. @code{cl_I}, @code{cl_RA},
  509. @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F},
  510. @code{cl_R}, @code{cl_N}.
  511. The easiest way to specify a value which is outside of the range of the
  512. C++ built-in types is therefore to specify it as a string, like this:
  513. @example
  514. cl_I order_of_rubiks_cube_group = "43252003274489856000";
  515. @end example
  516. Note that this conversion is done at runtime, not at compile-time.
  517. Conversions from @code{cl_I} to the C built-in types @samp{int},
  518. @samp{unsigned int}, @samp{long}, @samp{unsigned long} are provided through
  519. the functions
  520. @table @code
  521. @item int cl_I_to_int (const cl_I& x)
  522. @itemx unsigned int cl_I_to_uint (const cl_I& x)
  523. @itemx long cl_I_to_long (const cl_I& x)
  524. @itemx unsigned long cl_I_to_ulong (const cl_I& x)
  525. Returns @code{x} as element of the C type @var{ctype}. If @code{x} is not
  526. representable in the range of @var{ctype}, a runtime error occurs.
  527. @end table
  528. Conversions from the classes @code{cl_I}, @code{cl_RA},
  529. @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F} and
  530. @code{cl_R}
  531. to the C built-in types @samp{float} and @samp{double} are provided through
  532. the functions
  533. @table @code
  534. @item float cl_float_approx (const @var{type}& x)
  535. @itemx double cl_double_approx (const @var{type}& x)
  536. Returns an approximation of @code{x} of C type @var{ctype}.
  537. If @code{abs(x)} is too close to 0 (underflow), 0 is returned.
  538. If @code{abs(x)} is too large (overflow), an IEEE infinity is returned.
  539. @end table
  540. Conversions from any class to any of its subclasses (``derived classes'' in
  541. C++ terminology) are not provided. Instead, you can assert and check
  542. that a value belongs to a certain subclass, and return it as element of that
  543. class, using the @samp{As} and @samp{The} macros.
  544. @code{As(@var{type})(@var{value})} checks that @var{value} belongs to
  545. @var{type} and returns it as such.
  546. @code{The(@var{type})(@var{value})} assumes that @var{value} belongs to
  547. @var{type} and returns it as such. It is your responsibility to ensure
  548. that this assumption is valid.
  549. Example:
  550. @example
  551. @group
  552. cl_I x = @dots{};
  553. if (!(x >= 0)) abort();
  554. cl_I ten_x = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer.
  555. // In general, it would be a rational number.
  556. @end group
  557. @end example
  558. @chapter Functions on numbers
  559. Each of the number classes declares its mathematical operations in the
  560. corresponding include file. For example, if your code operates with
  561. objects of type @code{cl_I}, it should @code{#include <cl_integer.h>}.
  562. @section Constructing numbers
  563. Here is how to create number objects ``from nothing''.
  564. @subsection Constructing integers
  565. @code{cl_I} objects are most easily constructed from C integers and from
  566. strings. See @ref{Conversions}.
  567. @subsection Constructing rational numbers
  568. @code{cl_RA} objects can be constructed from strings. The syntax
  569. for rational numbers is described in @ref{Internal and printed representation}.
  570. Another standard way to produce a rational number is through application
  571. of @samp{operator /} or @samp{recip} on integers.
  572. @subsection Constructing floating-point numbers
  573. @code{cl_F} objects with low precision are most easily constructed from
  574. C @samp{float} and @samp{double}. See @ref{Conversions}.
  575. To construct a @code{cl_F} with high precision, you can use the conversion
  576. from @samp{const char *}, but you have to specify the desired precision
  577. within the string. (See @ref{Internal and printed representation}.)
  578. Example:
  579. @example
  580. cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
  581. @end example
  582. will set @samp{e} to the given value, with a precision of 40 decimal digits.
  583. The programmatic way to construct a @code{cl_F} with high precision is
  584. through the @code{cl_float} conversion function, see
  585. @ref{Conversion to floating-point numbers}. For example, to compute
  586. @code{e} to 40 decimal places, first construct 1.0 to 40 decimal places
  587. and then apply the exponential function:
  588. @example
  589. cl_float_format_t precision = cl_float_format(40);
  590. cl_F e = exp(cl_float(1,precision));
  591. @end example
  592. @subsection Constructing complex numbers
  593. Non-real @code{cl_N} objects are normally constructed through the function
  594. @example
  595. cl_N complex (const cl_R& realpart, const cl_R& imagpart)
  596. @end example
  597. See @ref{Elementary complex functions}.
  598. @section Elementary functions
  599. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  600. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  601. defines the following operations:
  602. @table @code
  603. @item @var{type} operator + (const @var{type}&, const @var{type}&)
  604. Addition.
  605. @item @var{type} operator - (const @var{type}&, const @var{type}&)
  606. Subtraction.
  607. @item @var{type} operator - (const @var{type}&)
  608. Returns the negative of the argument.
  609. @item @var{type} plus1 (const @var{type}& x)
  610. Returns @code{x + 1}.
  611. @item @var{type} minus1 (const @var{type}& x)
  612. Returns @code{x - 1}.
  613. @item @var{type} operator * (const @var{type}&, const @var{type}&)
  614. Multiplication.
  615. @item @var{type} square (const @var{type}& x)
  616. Returns @code{x * x}.
  617. @end table
  618. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
  619. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  620. defines the following operations:
  621. @table @code
  622. @item @var{type} operator / (const @var{type}&, const @var{type}&)
  623. Division.
  624. @item @var{type} recip (const @var{type}&)
  625. Returns the reciprocal of the argument.
  626. @end table
  627. The class @code{cl_I} doesn't define a @samp{/} operation because
  628. in the C/C++ language this operator, applied to integral types,
  629. denotes the @samp{floor} or @samp{truncate} operation (which one of these,
  630. is implementation dependent). (@xref{Rounding functions})
  631. Instead, @code{cl_I} defines an ``exact quotient'' function:
  632. @table @code
  633. @item cl_I exquo (const cl_I& x, const cl_I& y)
  634. Checks that @code{y} divides @code{x}, and returns the quotient @code{x}/@code{y}.
  635. @end table
  636. The following exponentiation functions are defined:
  637. @table @code
  638. @item cl_I expt_pos (const cl_I& x, const cl_I& y)
  639. @itemx cl_RA expt_pos (const cl_RA& x, const cl_I& y)
  640. @code{y} must be > 0. Returns @code{x^y}.
  641. @item cl_RA expt (const cl_RA& x, const cl_I& y)
  642. @itemx cl_R expt (const cl_R& x, const cl_I& y)
  643. @itemx cl_N expt (const cl_N& x, const cl_I& y)
  644. Returns @code{x^y}.
  645. @end table
  646. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  647. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  648. defines the following operation:
  649. @table @code
  650. @item @var{type} abs (const @var{type}& x)
  651. Returns the absolute value of @code{x}.
  652. This is @code{x} if @code{x >= 0}, and @code{-x} if @code{x <= 0}.
  653. @end table
  654. The class @code{cl_N} implements this as follows:
  655. @table @code
  656. @item cl_R abs (const cl_N x)
  657. Returns the absolute value of @code{x}.
  658. @end table
  659. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  660. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  661. defines the following operation:
  662. @table @code
  663. @item @var{type} signum (const @var{type}& x)
  664. Returns the sign of @code{x}, in the same number format as @code{x}.
  665. This is defined as @code{x / abs(x)} if @code{x} is non-zero, and
  666. @code{x} if @code{x} is zero. If @code{x} is real, the value is either
  667. 0 or 1 or -1.
  668. @end table
  669. @section Elementary rational functions
  670. Each of the classes @code{cl_RA}, @code{cl_I} defines the following operations:
  671. @table @code
  672. @item cl_I numerator (const @var{type}& x)
  673. Returns the numerator of @code{x}.
  674. @item cl_I denominator (const @var{type}& x)
  675. Returns the denominator of @code{x}.
  676. @end table
  677. The numerator and denominator of a rational number are normalized in such
  678. a way that they have no factor in common and the denominator is positive.
  679. @section Elementary complex functions
  680. The class @code{cl_N} defines the following operation:
  681. @table @code
  682. @item cl_N complex (const cl_R& a, const cl_R& b)
  683. Returns the complex number @code{a+bi}, that is, the complex number with
  684. real part @code{a} and imaginary part @code{b}.
  685. @end table
  686. Each of the classes @code{cl_N}, @code{cl_R} defines the following operations:
  687. @table @code
  688. @item cl_R realpart (const @var{type}& x)
  689. Returns the real part of @code{x}.
  690. @item cl_R imagpart (const @var{type}& x)
  691. Returns the imaginary part of @code{x}.
  692. @item @var{type} conjugate (const @var{type}& x)
  693. Returns the complex conjugate of @code{x}.
  694. @end table
  695. We have the relations
  696. @itemize @asis
  697. @item
  698. @code{x = complex(realpart(x), imagpart(x))}
  699. @item
  700. @code{conjugate(x) = complex(realpart(x), -imagpart(x))}
  701. @end itemize
  702. @section Comparisons
  703. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  704. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  705. defines the following operations:
  706. @table @code
  707. @item bool operator == (const @var{type}&, const @var{type}&)
  708. @itemx bool operator != (const @var{type}&, const @var{type}&)
  709. Comparison, as in C and C++.
  710. @item uint32 cl_equal_hashcode (const @var{type}&)
  711. Returns a 32-bit hash code that is the same for any two numbers which are
  712. the same according to @code{==}. This hash code depends on the number's value,
  713. not its type or precision.
  714. @item cl_boolean zerop (const @var{type}& x)
  715. Compare against zero: @code{x == 0}
  716. @end table
  717. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  718. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  719. defines the following operations:
  720. @table @code
  721. @item cl_signean cl_compare (const @var{type}& x, const @var{type}& y)
  722. Compares @code{x} and @code{y}. Returns +1 if @code{x}>@code{y},
  723. -1 if @code{x}<@code{y}, 0 if @code{x}=@code{y}.
  724. @item bool operator <= (const @var{type}&, const @var{type}&)
  725. @itemx bool operator < (const @var{type}&, const @var{type}&)
  726. @itemx bool operator >= (const @var{type}&, const @var{type}&)
  727. @itemx bool operator > (const @var{type}&, const @var{type}&)
  728. Comparison, as in C and C++.
  729. @item cl_boolean minusp (const @var{type}& x)
  730. Compare against zero: @code{x < 0}
  731. @item cl_boolean plusp (const @var{type}& x)
  732. Compare against zero: @code{x > 0}
  733. @item @var{type} max (const @var{type}& x, const @var{type}& y)
  734. Return the maximum of @code{x} and @code{y}.
  735. @item @var{type} min (const @var{type}& x, const @var{type}& y)
  736. Return the minimum of @code{x} and @code{y}.
  737. @end table
  738. When a floating point number and a rational number are compared, the float
  739. is first converted to a rational number using the function @code{rational}.
  740. Since a floating point number actually represents an interval of real numbers,
  741. the result might be surprising.
  742. For example, @code{(cl_F)(cl_R)"1/3" == (cl_R)"1/3"} returns false because
  743. there is no floating point number whose value is exactly @code{1/3}.
  744. @section Rounding functions
  745. When a real number is to be converted to an integer, there is no ``best''
  746. rounding. The desired rounding function depends on the application.
  747. The Common Lisp and ISO Lisp standards offer four rounding functions:
  748. @table @code
  749. @item floor(x)
  750. This is the largest integer <=@code{x}.
  751. @item ceiling(x)
  752. This is the smallest integer >=@code{x}.
  753. @item truncate(x)
  754. Among the integers between 0 and @code{x} (inclusive) the one nearest to @code{x}.
  755. @item round(x)
  756. The integer nearest to @code{x}. If @code{x} is exactly halfway between two
  757. integers, choose the even one.
  758. @end table
  759. These functions have different advantages:
  760. @code{floor} and @code{ceiling} are translation invariant:
  761. @code{floor(x+n) = floor(x) + n} and @code{ceiling(x+n) = ceiling(x) + n}
  762. for every @code{x} and every integer @code{n}.
  763. On the other hand, @code{truncate} and @code{round} are symmetric:
  764. @code{truncate(-x) = -truncate(x)} and @code{round(-x) = -round(x)},
  765. and furthermore @code{round} is unbiased: on the ``average'', it rounds
  766. down exactly as often as it rounds up.
  767. The functions are related like this:
  768. @itemize @asis
  769. @item
  770. @code{ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1}
  771. for rational numbers @code{m/n} (@code{m}, @code{n} integers, @code{n}>0), and
  772. @item
  773. @code{truncate(x) = sign(x) * floor(abs(x))}
  774. @end itemize
  775. Each of the classes @code{cl_R}, @code{cl_RA},
  776. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  777. defines the following operations:
  778. @table @code
  779. @item cl_I floor1 (const @var{type}& x)
  780. Returns @code{floor(x)}.
  781. @item cl_I ceiling1 (const @var{type}& x)
  782. Returns @code{ceiling(x)}.
  783. @item cl_I truncate1 (const @var{type}& x)
  784. Returns @code{truncate(x)}.
  785. @item cl_I round1 (const @var{type}& x)
  786. Returns @code{round(x)}.
  787. @end table
  788. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  789. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  790. defines the following operations:
  791. @table @code
  792. @item cl_I floor1 (const @var{type}& x, const @var{type}& y)
  793. Returns @code{floor(x/y)}.
  794. @item cl_I ceiling1 (const @var{type}& x, const @var{type}& y)
  795. Returns @code{ceiling(x/y)}.
  796. @item cl_I truncate1 (const @var{type}& x, const @var{type}& y)
  797. Returns @code{truncate(x/y)}.
  798. @item cl_I round1 (const @var{type}& x, const @var{type}& y)
  799. Returns @code{round(x/y)}.
  800. @end table
  801. These functions are called @samp{floor1}, @dots{} here instead of
  802. @samp{floor}, @dots{}, because on some systems, system dependent include
  803. files define @samp{floor} and @samp{ceiling} as macros.
  804. In many cases, one needs both the quotient and the remainder of a division.
  805. It is more efficient to compute both at the same time than to perform
  806. two divisions, one for quotient and the next one for the remainder.
  807. The following functions therefore return a structure containing both
  808. the quotient and the remainder. The suffix @samp{2} indicates the number
  809. of ``return values''. The remainder is defined as follows:
  810. @itemize @bullet
  811. @item
  812. for the computation of @code{quotient = floor(x)},
  813. @code{remainder = x - quotient},
  814. @item
  815. for the computation of @code{quotient = floor(x,y)},
  816. @code{remainder = x - quotient*y},
  817. @end itemize
  818. and similarly for the other three operations.
  819. Each of the classes @code{cl_R}, @code{cl_RA},
  820. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  821. defines the following operations:
  822. @table @code
  823. @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
  824. @itemx @var{type}_div_t floor2 (const @var{type}& x)
  825. @itemx @var{type}_div_t ceiling2 (const @var{type}& x)
  826. @itemx @var{type}_div_t truncate2 (const @var{type}& x)
  827. @itemx @var{type}_div_t round2 (const @var{type}& x)
  828. @end table
  829. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
  830. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  831. defines the following operations:
  832. @table @code
  833. @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
  834. @itemx @var{type}_div_t floor2 (const @var{type}& x, const @var{type}& y)
  835. @itemx @var{type}_div_t ceiling2 (const @var{type}& x, const @var{type}& y)
  836. @itemx @var{type}_div_t truncate2 (const @var{type}& x, const @var{type}& y)
  837. @itemx @var{type}_div_t round2 (const @var{type}& x, const @var{type}& y)
  838. @end table
  839. Sometimes, one wants the quotient as a floating-point number (of the
  840. same format as the argument, if the argument is a float) instead of as
  841. an integer. The prefix @samp{f} indicates this.
  842. Each of the classes
  843. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  844. defines the following operations:
  845. @table @code
  846. @item @var{type} ffloor (const @var{type}& x)
  847. @itemx @var{type} fceiling (const @var{type}& x)
  848. @itemx @var{type} ftruncate (const @var{type}& x)
  849. @itemx @var{type} fround (const @var{type}& x)
  850. @end table
  851. and similarly for class @code{cl_R}, but with return type @code{cl_F}.
  852. The class @code{cl_R} defines the following operations:
  853. @table @code
  854. @item cl_F ffloor (const @var{type}& x, const @var{type}& y)
  855. @itemx cl_F fceiling (const @var{type}& x, const @var{type}& y)
  856. @itemx cl_F ftruncate (const @var{type}& x, const @var{type}& y)
  857. @itemx cl_F fround (const @var{type}& x, const @var{type}& y)
  858. @end table
  859. These functions also exist in versions which return both the quotient
  860. and the remainder. The suffix @samp{2} indicates this.
  861. Each of the classes
  862. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  863. defines the following operations:
  864. @table @code
  865. @item struct @var{type}_fdiv_t @{ @var{type} quotient; @var{type} remainder; @};
  866. @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x)
  867. @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x)
  868. @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x)
  869. @itemx @var{type}_fdiv_t fround2 (const @var{type}& x)
  870. @end table
  871. and similarly for class @code{cl_R}, but with quotient type @code{cl_F}.
  872. The class @code{cl_R} defines the following operations:
  873. @table @code
  874. @item struct @var{type}_fdiv_t @{ cl_F quotient; cl_R remainder; @};
  875. @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x, const @var{type}& y)
  876. @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x, const @var{type}& y)
  877. @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x, const @var{type}& y)
  878. @itemx @var{type}_fdiv_t fround2 (const @var{type}& x, const @var{type}& y)
  879. @end table
  880. Other applications need only the remainder of a division.
  881. The remainder of @samp{floor} and @samp{ffloor} is called @samp{mod}
  882. (abbreviation of ``modulo''). The remainder @samp{truncate} and
  883. @samp{ftruncate} is called @samp{rem} (abbreviation of ``remainder'').
  884. @itemize @bullet
  885. @item
  886. @code{mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y}
  887. @item
  888. @code{rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y}
  889. @end itemize
  890. If @code{x} and @code{y} are both >= 0, @code{mod(x,y) = rem(x,y) >= 0}.
  891. In general, @code{mod(x,y)} has the sign of @code{y} or is zero,
  892. and @code{rem(x,y)} has the sign of @code{x} or is zero.
  893. The classes @code{cl_R}, @code{cl_I} define the following operations:
  894. @table @code
  895. @item @var{type} mod (const @var{type}& x, const @var{type}& y)
  896. @itemx @var{type} rem (const @var{type}& x, const @var{type}& y)
  897. @end table
  898. @section Roots
  899. Each of the classes @code{cl_R},
  900. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  901. defines the following operation:
  902. @table @code
  903. @item @var{type} sqrt (const @var{type}& x)
  904. @code{x} must be >= 0. This function returns the square root of @code{x},
  905. normalized to be >= 0. If @code{x} is the square of a rational number,
  906. @code{sqrt(x)} will be a rational number, else it will return a
  907. floating-point approximation.
  908. @end table
  909. The classes @code{cl_RA}, @code{cl_I} define the following operation:
  910. @table @code
  911. @item cl_boolean sqrtp (const @var{type}& x, @var{type}* root)
  912. This tests whether @code{x} is a perfect square. If so, it returns true
  913. and the exact square root in @code{*root}, else it returns false.
  914. @end table
  915. Furthermore, for integers, similarly:
  916. @table @code
  917. @item cl_boolean isqrt (const @var{type}& x, @var{type}* root)
  918. @code{x} should be >= 0. This function sets @code{*root} to
  919. @code{floor(sqrt(x))} and returns the same value as @code{sqrtp}:
  920. the boolean value @code{(expt(*root,2) == x)}.
  921. @end table
  922. For @code{n}th roots, the classes @code{cl_RA}, @code{cl_I}
  923. define the following operation:
  924. @table @code
  925. @item cl_boolean rootp (const @var{type}& x, const cl_I& n, @var{type}* root)
  926. @code{x} must be >= 0. @code{n} must be > 0.
  927. This tests whether @code{x} is an @code{n}th power of a rational number.
  928. If so, it returns true and the exact root in @code{*root}, else it returns
  929. false.
  930. @end table
  931. The only square root function which accepts negative numbers is the one
  932. for class @code{cl_N}:
  933. @table @code
  934. @item cl_N sqrt (const cl_N& z)
  935. Returns the square root of @code{z}, as defined by the formula
  936. @code{sqrt(z) = exp(log(z)/2)}. Conversion to a floating-point type
  937. or to a complex number are done if necessary. The range of the result is the
  938. right half plane @code{realpart(sqrt(z)) >= 0}
  939. including the positive imaginary axis and 0, but excluding
  940. the negative imaginary axis.
  941. The result is an exact number only if @code{z} is an exact number.
  942. @end table
  943. @section Transcendental functions
  944. The transcendental functions return an exact result if the argument
  945. is exact and the result is exact as well. Otherwise they must return
  946. inexact numbers even if the argument is exact.
  947. For example, @code{cos(0) = 1} returns the rational number @code{1}.
  948. @subsection Exponential and logarithmic functions
  949. @table @code
  950. @item cl_R exp (const cl_R& x)
  951. @itemx cl_N exp (const cl_N& x)
  952. Returns the exponential function of @code{x}. This is @code{e^x} where
  953. @code{e} is the base of the natural logarithms. The range of the result
  954. is the entire complex plane excluding 0.
  955. @item cl_R ln (const cl_R& x)
  956. @code{x} must be > 0. Returns the (natural) logarithm of x.
  957. @item cl_N log (const cl_N& x)
  958. Returns the (natural) logarithm of x. If @code{x} is real and positive,
  959. this is @code{ln(x)}. In general, @code{log(x) = log(abs(x)) + i*phase(x)}.
  960. The range of the result is the strip in the complex plane
  961. @code{-pi < imagpart(log(x)) <= pi}.
  962. @item cl_R phase (const cl_N& x)
  963. Returns the angle part of @code{x} in its polar representation as a
  964. complex number. That is, @code{phase(x) = atan(realpart(x),imagpart(x))}.
  965. This is also the imaginary part of @code{log(x)}.
  966. The range of the result is the interval @code{-pi < phase(x) <= pi}.
  967. The result will be an exact number only if @code{zerop(x)} or
  968. if @code{x} is real and positive.
  969. @item cl_R log (const cl_R& a, const cl_R& b)
  970. @code{a} and @code{b} must be > 0. Returns the logarithm of @code{a} with
  971. respect to base @code{b}. @code{log(a,b) = ln(a)/ln(b)}.
  972. The result can be exact only if @code{a = 1} or if @code{a} and @code{b}
  973. are both rational.
  974. @item cl_N log (const cl_N& a, const cl_N& b)
  975. Returns the logarithm of @code{a} with respect to base @code{b}.
  976. @code{log(a,b) = log(a)/log(b)}.
  977. @item cl_N expt (const cl_N& x, const cl_N& y)
  978. Exponentiation: Returns @code{x^y = exp(y*log(x))}.
  979. @end table
  980. The constant e = exp(1) = 2.71828@dots{} is returned by the following functions:
  981. @table @code
  982. @item cl_F cl_exp1 (cl_float_format_t f)
  983. Returns e as a float of format @code{f}.
  984. @item cl_F cl_exp1 (const cl_F& y)
  985. Returns e in the float format of @code{y}.
  986. @item cl_F cl_exp1 (void)
  987. Returns e as a float of format @code{cl_default_float_format}.
  988. @end table
  989. @subsection Trigonometric functions
  990. @table @code
  991. @item cl_R sin (const cl_R& x)
  992. Returns @code{sin(x)}. The range of the result is the interval
  993. @code{-1 <= sin(x) <= 1}.
  994. @item cl_N sin (const cl_N& z)
  995. Returns @code{sin(z)}. The range of the result is the entire complex plane.
  996. @item cl_R cos (const cl_R& x)
  997. Returns @code{cos(x)}. The range of the result is the interval
  998. @code{-1 <= cos(x) <= 1}.
  999. @item cl_N cos (const cl_N& x)
  1000. Returns @code{cos(z)}. The range of the result is the entire complex plane.
  1001. @item struct cl_cos_sin_t @{ cl_R cos; cl_R sin; @};
  1002. @itemx cl_cos_sin_t cl_cos_sin (const cl_R& x)
  1003. Returns both @code{sin(x)} and @code{cos(x)}. This is more efficient than
  1004. computing them separately. The relation @code{cos^2 + sin^2 = 1} will
  1005. hold only approximately.
  1006. @item cl_R tan (const cl_R& x)
  1007. @itemx cl_N tan (const cl_N& x)
  1008. Returns @code{tan(x) = sin(x)/cos(x)}.
  1009. @item cl_N cis (const cl_R& x)
  1010. @itemx cl_N cis (const cl_N& x)
  1011. Returns @code{exp(i*x)}. The name @samp{cis} means ``cos + i sin'', because
  1012. @code{e^(i*x) = cos(x) + i*sin(x)}.
  1013. @item cl_N asin (const cl_N& z)
  1014. Returns @code{arcsin(z)}. This is defined as
  1015. @code{arcsin(z) = log(iz+sqrt(1-z^2))/i} and satisfies
  1016. @code{arcsin(-z) = -arcsin(z)}.
  1017. The range of the result is the strip in the complex domain
  1018. @code{-pi/2 <= realpart(arcsin(z)) <= pi/2}, excluding the numbers
  1019. with @code{realpart = -pi/2} and @code{imagpart < 0} and the numbers
  1020. with @code{realpart = pi/2} and @code{imagpart > 0}.
  1021. @ignore
  1022. Proof: This follows from arcsin(z) = arsinh(iz)/i and the corresponding
  1023. results for arsinh.
  1024. @end ignore
  1025. @item cl_N acos (const cl_N& z)
  1026. Returns @code{arccos(z)}. This is defined as
  1027. @code{arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i}
  1028. @ignore
  1029. Kahan's formula:
  1030. @code{arccos(z) = 2*log(sqrt((1+z)/2)+i*sqrt((1-z)/2))/i}
  1031. @end ignore
  1032. and satisfies @code{arccos(-z) = pi - arccos(z)}.
  1033. The range of the result is the strip in the complex domain
  1034. @code{0 <= realpart(arcsin(z)) <= pi}, excluding the numbers
  1035. with @code{realpart = 0} and @code{imagpart < 0} and the numbers
  1036. with @code{realpart = pi} and @code{imagpart > 0}.
  1037. @ignore
  1038. Proof: This follows from the results about arcsin.
  1039. @end ignore
  1040. @item cl_R atan (const cl_R& x, const cl_R& y)
  1041. Returns the angle of the polar representation of the complex number
  1042. @code{x+iy}. This is @code{atan(y/x)} if @code{x>0}. The range of
  1043. the result is the interval @code{-pi < atan(x,y) <= pi}. The result will
  1044. be an exact number only if @code{x > 0} and @code{y} is the exact @code{0}.
  1045. WARNING: In Common Lisp, this function is called as @code{(atan y x)},
  1046. with reversed order of arguments.
  1047. @item cl_R atan (const cl_R& x)
  1048. Returns @code{arctan(x)}. This is the same as @code{atan(1,x)}. The range
  1049. of the result is the interval @code{-pi/2 < atan(x) < pi/2}. The result
  1050. will be an exact number only if @code{x} is the exact @code{0}.
  1051. @item cl_N atan (const cl_N& z)
  1052. Returns @code{arctan(z)}. This is defined as
  1053. @code{arctan(z) = (log(1+iz)-log(1-iz)) / 2i} and satisfies
  1054. @code{arctan(-z) = -arctan(z)}. The range of the result is
  1055. the strip in the complex domain
  1056. @code{-pi/2 <= realpart(arctan(z)) <= pi/2}, excluding the numbers
  1057. with @code{realpart = -pi/2} and @code{imagpart >= 0} and the numbers
  1058. with @code{realpart = pi/2} and @code{imagpart <= 0}.
  1059. @ignore
  1060. Proof: arctan(z) = artanh(iz)/i, we know the range of the artanh function.
  1061. @end ignore
  1062. @end table
  1063. The constant pi = 3.14@dots{} is returned by the following functions:
  1064. @table @code
  1065. @item cl_F cl_pi (cl_float_format_t f)
  1066. Returns pi as a float of format @code{f}.
  1067. @item cl_F cl_pi (const cl_F& y)
  1068. Returns pi in the float format of @code{y}.
  1069. @item cl_F cl_pi (void)
  1070. Returns pi as a float of format @code{cl_default_float_format}.
  1071. @end table
  1072. @subsection Hyperbolic functions
  1073. @table @code
  1074. @item cl_R sinh (const cl_R& x)
  1075. Returns @code{sinh(x)}.
  1076. @item cl_N sinh (const cl_N& z)
  1077. Returns @code{sinh(z)}. The range of the result is the entire complex plane.
  1078. @item cl_R cosh (const cl_R& x)
  1079. Returns @code{cosh(x)}. The range of the result is the interval
  1080. @code{cosh(x) >= 1}.
  1081. @item cl_N cosh (const cl_N& z)
  1082. Returns @code{cosh(z)}. The range of the result is the entire complex plane.
  1083. @item struct cl_cosh_sinh_t @{ cl_R cosh; cl_R sinh; @};
  1084. @itemx cl_cosh_sinh_t cl_cosh_sinh (const cl_R& x)
  1085. Returns both @code{sinh(x)} and @code{cosh(x)}. This is more efficient than
  1086. computing them separately. The relation @code{cosh^2 - sinh^2 = 1} will
  1087. hold only approximately.
  1088. @item cl_R tanh (const cl_R& x)
  1089. @itemx cl_N tanh (const cl_N& x)
  1090. Returns @code{tanh(x) = sinh(x)/cosh(x)}.
  1091. @item cl_N asinh (const cl_N& z)
  1092. Returns @code{arsinh(z)}. This is defined as
  1093. @code{arsinh(z) = log(z+sqrt(1+z^2))} and satisfies
  1094. @code{arsinh(-z) = -arsinh(z)}.
  1095. @ignore
  1096. Proof: Knowing the range of log, we know -pi < imagpart(arsinh(z)) <= pi.
  1097. Actually, z+sqrt(1+z^2) can never be real and <0, so
  1098. -pi < imagpart(arsinh(z)) < pi.
  1099. We have (z+sqrt(1+z^2))*(-z+sqrt(1+(-z)^2)) = (1+z^2)-z^2 = 1, hence the
  1100. logs of both factors sum up to 0 mod 2*pi*i, hence to 0.
  1101. @end ignore
  1102. The range of the result is the strip in the complex domain
  1103. @code{-pi/2 <= imagpart(arsinh(z)) <= pi/2}, excluding the numbers
  1104. with @code{imagpart = -pi/2} and @code{realpart > 0} and the numbers
  1105. with @code{imagpart = pi/2} and @code{realpart < 0}.
  1106. @ignore
  1107. Proof: Write z = x+iy. Because of arsinh(-z) = -arsinh(z), we may assume
  1108. that z is in Range(sqrt), that is, x>=0 and, if x=0, then y>=0.
  1109. If x > 0, then Re(z+sqrt(1+z^2)) = x + Re(sqrt(1+z^2)) >= x > 0,
  1110. so -pi/2 < imagpart(log(z+sqrt(1+z^2))) < pi/2.
  1111. If x = 0 and y >= 0, arsinh(z) = log(i*y+sqrt(1-y^2)).
  1112. If y <= 1, the realpart is 0 and the imagpart is >= 0 and <= pi/2.
  1113. If y >= 1, the imagpart is pi/2 and the realpart is
  1114. log(y+sqrt(y^2-1)) >= log(y) >= 0.
  1115. @end ignore
  1116. @ignore
  1117. Moreover, if z is in Range(sqrt),
  1118. log(sqrt(1+z^2)+z) = 2 artanh(z/(1+sqrt(1+z^2)))
  1119. (for a proof, see file src/cl_C_asinh.cc).
  1120. @end ignore
  1121. @item cl_N acosh (const cl_N& z)
  1122. Returns @code{arcosh(z)}. This is defined as
  1123. @code{arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))}.
  1124. The range of the result is the half-strip in the complex domain
  1125. @code{-pi < imagpart(arcosh(z)) <= pi, realpart(arcosh(z)) >= 0},
  1126. excluding the numbers with @code{realpart = 0} and @code{-pi < imagpart < 0}.
  1127. @ignore
  1128. Proof: sqrt((z+1)/2) and sqrt((z-1)/2)) lie in Range(sqrt), hence does
  1129. their sum, hence its log has an imagpart <= pi/2 and > -pi/2.
  1130. If z is in Range(sqrt), we have
  1131. sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1)
  1132. ==> (sqrt((z+1)/2)+sqrt((z-1)/2))^2 = (z+1)/2 + sqrt(z^2-1) + (z-1)/2
  1133. = z + sqrt(z^2-1)
  1134. ==> arcosh(z) = log(z+sqrt(z^2-1)) mod 2*pi*i
  1135. and since the imagpart of both expressions is > -pi, <= pi
  1136. ==> arcosh(z) = log(z+sqrt(z^2-1))
  1137. To prove that the realpart of this is >= 0, write z = x+iy with x>=0,
  1138. z^2-1 = u+iv with u = x^2-y^2-1, v = 2xy,
  1139. sqrt(z^2-1) = p+iq with p = sqrt((sqrt(u^2+v^2)+u)/2) >= 0,
  1140. q = sqrt((sqrt(u^2+v^2)-u)/2) * sign(v),
  1141. then |z+sqrt(z^2-1)|^2 = |x+iy + p+iq|^2
  1142. = (x+p)^2 + (y+q)^2
  1143. = x^2 + 2xp + p^2 + y^2 + 2yq + q^2
  1144. >= x^2 + p^2 + y^2 + q^2 (since x>=0, p>=0, yq>=0)
  1145. = x^2 + y^2 + sqrt(u^2+v^2)
  1146. >= x^2 + y^2 + |u|
  1147. >= x^2 + y^2 - u
  1148. = 1 + 2*y^2
  1149. >= 1
  1150. hence realpart(log(z+sqrt(z^2-1))) = log(|z+sqrt(z^2-1)|) >= 0.
  1151. Equality holds only if y = 0 and u <= 0, i.e. 0 <= x < 1.
  1152. In this case arcosh(z) = log(x+i*sqrt(1-x^2)) has imagpart >=0.
  1153. Otherwise, -z is in Range(sqrt).
  1154. If y != 0, sqrt((z+1)/2) = i^sign(y) * sqrt((-z-1)/2),
  1155. sqrt((z-1)/2) = i^sign(y) * sqrt((-z+1)/2),
  1156. hence arcosh(z) = sign(y)*pi/2*i + arcosh(-z),
  1157. and this has realpart > 0.
  1158. If y = 0 and -1<=x<=0, we still have sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1),
  1159. ==> arcosh(z) = log(z+sqrt(z^2-1)) = log(x+i*sqrt(1-x^2))
  1160. has realpart = 0 and imagpart > 0.
  1161. If y = 0 and x<=-1, however, sqrt(z+1)*sqrt(z-1) = - sqrt(z^2-1),
  1162. ==> arcosh(z) = log(z-sqrt(z^2-1)) = pi*i + arcosh(-z).
  1163. This has realpart >= 0 and imagpart = pi.
  1164. @end ignore
  1165. @item cl_N atanh (const cl_N& z)
  1166. Returns @code{artanh(z)}. This is defined as
  1167. @code{artanh(z) = (log(1+z)-log(1-z)) / 2} and satisfies
  1168. @code{artanh(-z) = -artanh(z)}. The range of the result is
  1169. the strip in the complex domain
  1170. @code{-pi/2 <= imagpart(artanh(z)) <= pi/2}, excluding the numbers
  1171. with @code{imagpart = -pi/2} and @code{realpart <= 0} and the numbers
  1172. with @code{imagpart = pi/2} and @code{realpart >= 0}.
  1173. @ignore
  1174. Proof: Write z = x+iy. Examine
  1175. imagpart(artanh(z)) = (atan(1+x,y) - atan(1-x,-y))/2.
  1176. Case 1: y = 0.
  1177. x > 1 ==> imagpart = -pi/2, realpart = 1/2 log((x+1)/(x-1)) > 0,
  1178. x < -1 ==> imagpart = pi/2, realpart = 1/2 log((-x-1)/(-x+1)) < 0,
  1179. |x| < 1 ==> imagpart = 0
  1180. Case 2: y > 0.
  1181. imagpart(artanh(z))
  1182. = (atan(1+x,y) - atan(1-x,-y))/2
  1183. = ((pi/2 - atan((1+x)/y)) - (-pi/2 - atan((1-x)/-y)))/2
  1184. = (pi - atan((1+x)/y) - atan((1-x)/y))/2
  1185. > (pi - pi/2 - pi/2 )/2 = 0
  1186. and (1+x)/y > (1-x)/y
  1187. ==> atan((1+x)/y) > atan((-1+x)/y) = - atan((1-x)/y)
  1188. ==> imagpart < pi/2.
  1189. Hence 0 < imagpart < pi/2.
  1190. Case 3: y < 0.
  1191. By artanh(z) = -artanh(-z) and case 2, -pi/2 < imagpart < 0.
  1192. @end ignore
  1193. @end table
  1194. @subsection Euler gamma
  1195. Euler's constant C = 0.577@dots{} is returned by the following functions:
  1196. @table @code
  1197. @item cl_F cl_eulerconst (cl_float_format_t f)
  1198. Returns Euler's constant as a float of format @code{f}.
  1199. @item cl_F cl_eulerconst (const cl_F& y)
  1200. Returns Euler's constant in the float format of @code{y}.
  1201. @item cl_F cl_eulerconst (void)
  1202. Returns Euler's constant as a float of format @code{cl_default_float_format}.
  1203. @end table
  1204. Catalan's constant G = 0.915@dots{} is returned by the following functions:
  1205. @table @code
  1206. @item cl_F cl_catalanconst (cl_float_format_t f)
  1207. Returns Catalan's constant as a float of format @code{f}.
  1208. @item cl_F cl_catalanconst (const cl_F& y)
  1209. Returns Catalan's constant in the float format of @code{y}.
  1210. @item cl_F cl_catalanconst (void)
  1211. Returns Catalan's constant as a float of format @code{cl_default_float_format}.
  1212. @end table
  1213. @subsection Riemann zeta
  1214. Riemann's zeta function at an integral point @code{s>1} is returned by the
  1215. following functions:
  1216. @table @code
  1217. @item cl_F cl_zeta (int s, cl_float_format_t f)
  1218. Returns Riemann's zeta function at @code{s} as a float of format @code{f}.
  1219. @item cl_F cl_zeta (int s, const cl_F& y)
  1220. Returns Riemann's zeta function at @code{s} in the float format of @code{y}.
  1221. @item cl_F cl_zeta (int s)
  1222. Returns Riemann's zeta function at @code{s} as a float of format
  1223. @code{cl_default_float_format}.
  1224. @end table
  1225. @section Functions on integers
  1226. @subsection Logical functions
  1227. Integers, when viewed as in two's complement notation, can be thought as
  1228. infinite bit strings where the bits' values eventually are constant.
  1229. For example,
  1230. @example
  1231. 17 = ......00010001
  1232. -6 = ......11111010
  1233. @end example
  1234. The logical operations view integers as such bit strings and operate
  1235. on each of the bit positions in parallel.
  1236. @table @code
  1237. @item cl_I lognot (const cl_I& x)
  1238. @itemx cl_I operator ~ (const cl_I& x)
  1239. Logical not, like @code{~x} in C. This is the same as @code{-1-x}.
  1240. @item cl_I logand (const cl_I& x, const cl_I& y)
  1241. @itemx cl_I operator & (const cl_I& x, const cl_I& y)
  1242. Logical and, like @code{x & y} in C.
  1243. @item cl_I logior (const cl_I& x, const cl_I& y)
  1244. @itemx cl_I operator | (const cl_I& x, const cl_I& y)
  1245. Logical (inclusive) or, like @code{x | y} in C.
  1246. @item cl_I logxor (const cl_I& x, const cl_I& y)
  1247. @itemx cl_I operator ^ (const cl_I& x, const cl_I& y)
  1248. Exclusive or, like @code{x ^ y} in C.
  1249. @item cl_I logeqv (const cl_I& x, const cl_I& y)
  1250. Bitwise equivalence, like @code{~(x ^ y)} in C.
  1251. @item cl_I lognand (const cl_I& x, const cl_I& y)
  1252. Bitwise not and, like @code{~(x & y)} in C.
  1253. @item cl_I lognor (const cl_I& x, const cl_I& y)
  1254. Bitwise not or, like @code{~(x | y)} in C.
  1255. @item cl_I logandc1 (const cl_I& x, const cl_I& y)
  1256. Logical and, complementing the first argument, like @code{~x & y} in C.
  1257. @item cl_I logandc2 (const cl_I& x, const cl_I& y)
  1258. Logical and, complementing the second argument, like @code{x & ~y} in C.
  1259. @item cl_I logorc1 (const cl_I& x, const cl_I& y)
  1260. Logical or, complementing the first argument, like @code{~x | y} in C.
  1261. @item cl_I logorc2 (const cl_I& x, const cl_I& y)
  1262. Logical or, complementing the second argument, like @code{x | ~y} in C.
  1263. @end table
  1264. These operations are all available though the function
  1265. @table @code
  1266. @item cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)
  1267. @end table
  1268. where @code{op} must have one of the 16 values (each one stands for a function
  1269. which combines two bits into one bit): @code{boole_clr}, @code{boole_set},
  1270. @code{boole_1}, @code{boole_2}, @code{boole_c1}, @code{boole_c2},
  1271. @code{boole_and}, @code{boole_ior}, @code{boole_xor}, @code{boole_eqv},
  1272. @code{boole_nand}, @code{boole_nor}, @code{boole_andc1}, @code{boole_andc2},
  1273. @code{boole_orc1}, @code{boole_orc2}.
  1274. Other functions that view integers as bit strings:
  1275. @table @code
  1276. @item cl_boolean logtest (const cl_I& x, const cl_I& y)
  1277. Returns true if some bit is set in both @code{x} and @code{y}, i.e. if
  1278. @code{logand(x,y) != 0}.
  1279. @item cl_boolean logbitp (const cl_I& n, const cl_I& x)
  1280. Returns true if the @code{n}th bit (from the right) of @code{x} is set.
  1281. Bit 0 is the least significant bit.
  1282. @item uintL logcount (const cl_I& x)
  1283. Returns the number of one bits in @code{x}, if @code{x} >= 0, or
  1284. the number of zero bits in @code{x}, if @code{x} < 0.
  1285. @end table
  1286. The following functions operate on intervals of bits in integers.
  1287. The type
  1288. @example
  1289. struct cl_byte @{ uintL size; uintL position; @};
  1290. @end example
  1291. represents the bit interval containing the bits
  1292. @code{position}@dots{}@code{position+size-1} of an integer.
  1293. The constructor @code{cl_byte(size,position)} constructs a @code{cl_byte}.
  1294. @table @code
  1295. @item cl_I ldb (const cl_I& n, const cl_byte& b)
  1296. extracts the bits of @code{n} described by the bit interval @code{b}
  1297. and returns them as a nonnegative integer with @code{b.size} bits.
  1298. @item cl_boolean ldb_test (const cl_I& n, const cl_byte& b)
  1299. Returns true if some bit described by the bit interval @code{b} is set in
  1300. @code{n}.
  1301. @item cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
  1302. Returns @code{n}, with the bits described by the bit interval @code{b}
  1303. replaced by @code{newbyte}. Only the lowest @code{b.size} bits of
  1304. @code{newbyte} are relevant.
  1305. @end table
  1306. The functions @code{ldb} and @code{dpb} implicitly shift. The following
  1307. functions are their counterparts without shifting:
  1308. @table @code
  1309. @item cl_I mask_field (const cl_I& n, const cl_byte& b)
  1310. returns an integer with the bits described by the bit interval @code{b}
  1311. copied from the corresponding bits in @code{n}, the other bits zero.
  1312. @item cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
  1313. returns an integer where the bits described by the bit interval @code{b}
  1314. come from @code{newbyte} and the other bits come from @code{n}.
  1315. @end table
  1316. The following relations hold:
  1317. @itemize @asis
  1318. @item
  1319. @code{ldb (n, b) = mask_field(n, b) >> b.position},
  1320. @item
  1321. @code{dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)},
  1322. @item
  1323. @code{deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)}.
  1324. @end itemize
  1325. The following operations on integers as bit strings are efficient shortcuts
  1326. for common arithmetic operations:
  1327. @table @code
  1328. @item cl_boolean oddp (const cl_I& x)
  1329. Returns true if the least significant bit of @code{x} is 1. Equivalent to
  1330. @code{mod(x,2) != 0}.
  1331. @item cl_boolean evenp (const cl_I& x)
  1332. Returns true if the least significant bit of @code{x} is 0. Equivalent to
  1333. @code{mod(x,2) == 0}.
  1334. @item cl_I operator << (const cl_I& x, const cl_I& n)
  1335. Shifts @code{x} by @code{n} bits to the left. @code{n} should be >=0.
  1336. Equivalent to @code{x * expt(2,n)}.
  1337. @item cl_I operator >> (const cl_I& x, const cl_I& n)
  1338. Shifts @code{x} by @code{n} bits to the right. @code{n} should be >=0.
  1339. Bits shifted out to the right are thrown away.
  1340. Equivalent to @code{floor(x / expt(2,n))}.
  1341. @item cl_I ash (const cl_I& x, const cl_I& y)
  1342. Shifts @code{x} by @code{y} bits to the left (if @code{y}>=0) or
  1343. by @code{-y} bits to the right (if @code{y}<=0). In other words, this
  1344. returns @code{floor(x * expt(2,y))}.
  1345. @item uintL integer_length (const cl_I& x)
  1346. Returns the number of bits (excluding the sign bit) needed to represent @code{x}
  1347. in two's complement notation. This is the smallest n >= 0 such that
  1348. -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
  1349. 2^(n-1) <= x < 2^n.
  1350. @item uintL ord2 (const cl_I& x)
  1351. @code{x} must be non-zero. This function returns the number of 0 bits at the
  1352. right of @code{x} in two's complement notation. This is the largest n >= 0
  1353. such that 2^n divides @code{x}.
  1354. @item uintL power2p (const cl_I& x)
  1355. @code{x} must be > 0. This function checks whether @code{x} is a power of 2.
  1356. If @code{x} = 2^(n-1), it returns n. Else it returns 0.
  1357. (See also the function @code{logp}.)
  1358. @end table
  1359. @subsection Number theoretic functions
  1360. @table @code
  1361. @item uint32 gcd (uint32 a, uint32 b)
  1362. @itemx cl_I gcd (const cl_I& a, const cl_I& b)
  1363. This function returns the greatest common divisor of @code{a} and @code{b},
  1364. normalized to be >= 0.
  1365. @item cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)
  1366. This function (``extended gcd'') returns the greatest common divisor @code{g} of
  1367. @code{a} and @code{b} and at the same time the representation of @code{g}
  1368. as an integral linear combination of @code{a} and @code{b}:
  1369. @code{u} and @code{v} with @code{u*a+v*b = g}, @code{g} >= 0.
  1370. @code{u} and @code{v} will be normalized to be of smallest possible absolute
  1371. value, in the following sense: If @code{a} and @code{b} are non-zero, and
  1372. @code{abs(a) != abs(b)}, @code{u} and @code{v} will satisfy the inequalities
  1373. @code{abs(u) <= abs(b)/(2*g)}, @code{abs(v) <= abs(a)/(2*g)}.
  1374. @item cl_I lcm (const cl_I& a, const cl_I& b)
  1375. This function returns the least common multiple of @code{a} and @code{b},
  1376. normalized to be >= 0.
  1377. @item cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l)
  1378. @itemx cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l)
  1379. @code{a} must be > 0. @code{b} must be >0 and != 1. If log(a,b) is
  1380. rational number, this function returns true and sets *l = log(a,b), else
  1381. it returns false.
  1382. @end table
  1383. @subsection Combinatorial functions
  1384. @table @code
  1385. @item cl_I factorial (uintL n)
  1386. @code{n} must be a small integer >= 0. This function returns the factorial
  1387. @code{n}! = @code{1*2*@dots{}*n}.
  1388. @item cl_I doublefactorial (uintL n)
  1389. @code{n} must be a small integer >= 0. This function returns the
  1390. doublefactorial @code{n}!! = @code{1*3*@dots{}*n} or
  1391. @code{n}!! = @code{2*4*@dots{}*n}, respectively.
  1392. @item cl_I binomial (uintL n, uintL k)
  1393. @code{n} and @code{k} must be small integers >= 0. This function returns the
  1394. binomial coefficient
  1395. @tex
  1396. ${n \choose k} = {n! \over n! (n-k)!}$
  1397. @end tex
  1398. @ifinfo
  1399. (@code{n} choose @code{k}) = @code{n}! / @code{k}! @code{(n-k)}!
  1400. @end ifinfo
  1401. for 0 <= k <= n, 0 else.
  1402. @end table
  1403. @section Functions on floating-point numbers
  1404. Recall that a floating-point number consists of a sign @code{s}, an
  1405. exponent @code{e} and a mantissa @code{m}. The value of the number is
  1406. @code{(-1)^s * 2^e * m}.
  1407. Each of the classes
  1408. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1409. defines the following operations.
  1410. @table @code
  1411. @item @var{type} scale_float (const @var{type}& x, sintL delta)
  1412. @itemx @var{type} scale_float (const @var{type}& x, const cl_I& delta)
  1413. Returns @code{x*2^delta}. This is more efficient than an explicit multiplication
  1414. because it copies @code{x} and modifies the exponent.
  1415. @end table
  1416. The following functions provide an abstract interface to the underlying
  1417. representation of floating-point numbers.
  1418. @table @code
  1419. @item sintL float_exponent (const @var{type}& x)
  1420. Returns the exponent @code{e} of @code{x}.
  1421. For @code{x = 0.0}, this is 0. For @code{x} non-zero, this is the unique
  1422. integer with @code{2^(e-1) <= abs(x) < 2^e}.
  1423. @item sintL float_radix (const @var{type}& x)
  1424. Returns the base of the floating-point representation. This is always @code{2}.
  1425. @item @var{type} float_sign (const @var{type}& x)
  1426. Returns the sign @code{s} of @code{x} as a float. The value is 1 for
  1427. @code{x} >= 0, -1 for @code{x} < 0.
  1428. @item uintL float_digits (const @var{type}& x)
  1429. Returns the number of mantissa bits in the floating-point representation
  1430. of @code{x}, including the hidden bit. The value only depends on the type
  1431. of @code{x}, not on its value.
  1432. @item uintL float_precision (const @var{type}& x)
  1433. Returns the number of significant mantissa bits in the floating-point
  1434. representation of @code{x}. Since denormalized numbers are not supported,
  1435. this is the same as @code{float_digits(x)} if @code{x} is non-zero, and
  1436. 0 if @code{x} = 0.
  1437. @end table
  1438. The complete internal representation of a float is encoded in the type
  1439. @code{cl_decoded_float} (or @code{cl_decoded_sfloat}, @code{cl_decoded_ffloat},
  1440. @code{cl_decoded_dfloat}, @code{cl_decoded_lfloat}, respectively), defined by
  1441. @example
  1442. struct cl_decoded_@var{type}float @{
  1443. @var{type} mantissa; cl_I exponent; @var{type} sign;
  1444. @};
  1445. @end example
  1446. and returned by the function
  1447. @table @code
  1448. @item cl_decoded_@var{type}float decode_float (const @var{type}& x)
  1449. For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
  1450. @code{x = (-1)^s * 2^e * m} and @code{0.5 <= m < 1.0}. For @code{x} = 0,
  1451. it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
  1452. @code{e} is the same as returned by the function @code{float_exponent}.
  1453. @end table
  1454. A complete decoding in terms of integers is provided as type
  1455. @example
  1456. struct cl_idecoded_float @{
  1457. cl_I mantissa; cl_I exponent; cl_I sign;
  1458. @};
  1459. @end example
  1460. by the following function:
  1461. @table @code
  1462. @item cl_idecoded_float integer_decode_float (const @var{type}& x)
  1463. For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
  1464. @code{x = (-1)^s * 2^e * m} and @code{m} an integer with @code{float_digits(x)}
  1465. bits. For @code{x} = 0, it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
  1466. WARNING: The exponent @code{e} is not the same as the one returned by
  1467. the functions @code{decode_float} and @code{float_exponent}.
  1468. @end table
  1469. Some other function, implemented only for class @code{cl_F}:
  1470. @table @code
  1471. @item cl_F float_sign (const cl_F& x, const cl_F& y)
  1472. This returns a floating point number whose precision and absolute value
  1473. is that of @code{y} and whose sign is that of @code{x}. If @code{x} is
  1474. zero, it is treated as positive. Same for @code{y}.
  1475. @end table
  1476. @section Conversion functions
  1477. @subsection Conversion to floating-point numbers
  1478. The type @code{cl_float_format_t} describes a floating-point format.
  1479. @table @code
  1480. @item cl_float_format_t cl_float_format (uintL n)
  1481. Returns the smallest float format which guarantees at least @code{n}
  1482. decimal digits in the mantissa (after the decimal point).
  1483. @item cl_float_format_t cl_float_format (const cl_F& x)
  1484. Returns the floating point format of @code{x}.
  1485. @item cl_float_format_t cl_default_float_format
  1486. Global variable: the default float format used when converting rational numbers
  1487. to floats.
  1488. @end table
  1489. To convert a real number to a float, each of the types
  1490. @code{cl_R}, @code{cl_F}, @code{cl_I}, @code{cl_RA},
  1491. @code{int}, @code{unsigned int}, @code{float}, @code{double}
  1492. defines the following operations:
  1493. @table @code
  1494. @item cl_F cl_float (const @var{type}&x, cl_float_format_t f)
  1495. Returns @code{x} as a float of format @code{f}.
  1496. @item cl_F cl_float (const @var{type}&x, const cl_F& y)
  1497. Returns @code{x} in the float format of @code{y}.
  1498. @item cl_F cl_float (const @var{type}&x)
  1499. Returns @code{x} as a float of format @code{cl_default_float_format} if
  1500. it is an exact number, or @code{x} itself if it is already a float.
  1501. @end table
  1502. Of course, converting a number to a float can lose precision.
  1503. Every floating-point format has some characteristic numbers:
  1504. @table @code
  1505. @item cl_F most_positive_float (cl_float_format_t f)
  1506. Returns the largest (most positive) floating point number in float format @code{f}.
  1507. @item cl_F most_negative_float (cl_float_format_t f)
  1508. Returns the smallest (most negative) floating point number in float format @code{f}.
  1509. @item cl_F least_positive_float (cl_float_format_t f)
  1510. Returns the least positive floating point number (i.e. > 0 but closest to 0)
  1511. in float format @code{f}.
  1512. @item cl_F least_negative_float (cl_float_format_t f)
  1513. Returns the least negative floating point number (i.e. < 0 but closest to 0)
  1514. in float format @code{f}.
  1515. @item cl_F float_epsilon (cl_float_format_t f)
  1516. Returns the smallest floating point number e > 0 such that @code{1+e != 1}.
  1517. @item cl_F float_negative_epsilon (cl_float_format_t f)
  1518. Returns the smallest floating point number e > 0 such that @code{1-e != 1}.
  1519. @end table
  1520. @subsection Conversion to rational numbers
  1521. Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_F}
  1522. defines the following operation:
  1523. @table @code
  1524. @item cl_RA rational (const @var{type}& x)
  1525. Returns the value of @code{x} as an exact number. If @code{x} is already
  1526. an exact number, this is @code{x}. If @code{x} is a floating-point number,
  1527. the value is a rational number whose denominator is a power of 2.
  1528. @end table
  1529. In order to convert back, say, @code{(cl_F)(cl_R)"1/3"} to @code{1/3}, there is
  1530. the function
  1531. @table @code
  1532. @item cl_RA rationalize (const cl_R& x)
  1533. If @code{x} is a floating-point number, it actually represents an interval
  1534. of real numbers, and this function returns the rational number with
  1535. smallest denominator (and smallest numerator, in magnitude)
  1536. which lies in this interval.
  1537. If @code{x} is already an exact number, this function returns @code{x}.
  1538. @end table
  1539. If @code{x} is any float, one has
  1540. @itemize @asis
  1541. @item
  1542. @code{cl_float(rational(x),x) = x}
  1543. @item
  1544. @code{cl_float(rationalize(x),x) = x}
  1545. @end itemize
  1546. @section Random number generators
  1547. A random generator is a machine which produces (pseudo-)random numbers.
  1548. The include file @code{<cl_random.h>} defines a class @code{cl_random_state}
  1549. which contains the state of a random generator. If you make a copy
  1550. of the random number generator, the original one and the copy will produce
  1551. the same sequence of random numbers.
  1552. The following functions return (pseudo-)random numbers in different formats.
  1553. Calling one of these modifies the state of the random number generator in
  1554. a complicated but deterministic way.
  1555. The global variable
  1556. @example
  1557. cl_random_state cl_default_random_state
  1558. @end example
  1559. contains a default random number generator. It is used when the functions
  1560. below are called without @code{cl_random_state} argument.
  1561. @table @code
  1562. @item uint32 random32 (cl_random_state& randomstate)
  1563. @itemx uint32 random32 ()
  1564. Returns a random unsigned 32-bit number. All bits are equally random.
  1565. @item cl_I random_I (cl_random_state& randomstate, const cl_I& n)
  1566. @itemx cl_I random_I (const cl_I& n)
  1567. @code{n} must be an integer > 0. This function returns a random integer @code{x}
  1568. in the range @code{0 <= x < n}.
  1569. @item cl_F random_F (cl_random_state& randomstate, const cl_F& n)
  1570. @itemx cl_F random_F (const cl_F& n)
  1571. @code{n} must be a float > 0. This function returns a random floating-point
  1572. number of the same format as @code{n} in the range @code{0 <= x < n}.
  1573. @item cl_R random_R (cl_random_state& randomstate, const cl_R& n)
  1574. @itemx cl_R random_R (const cl_R& n)
  1575. Behaves like @code{random_I} if @code{n} is an integer and like @code{random_F}
  1576. if @code{n} is a float.
  1577. @end table
  1578. @section Obfuscating operators
  1579. The modifying C/C++ operators @code{+=}, @code{-=}, @code{*=}, @code{/=},
  1580. @code{&=}, @code{|=}, @code{^=}, @code{<<=}, @code{>>=}
  1581. are not available by default because their
  1582. use tends to make programs unreadable. It is trivial to get away without
  1583. them. However, if you feel that you absolutely need these operators
  1584. to get happy, then add
  1585. @example
  1586. #define WANT_OBFUSCATING_OPERATORS
  1587. @end example
  1588. to the beginning of your source files, before the inclusion of any CLN
  1589. include files. This flag will enable the following operators:
  1590. For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
  1591. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
  1592. @table @code
  1593. @item @var{type}& operator += (@var{type}&, const @var{type}&)
  1594. @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
  1595. @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
  1596. @itemx @var{type}& operator /= (@var{type}&, const @var{type}&)
  1597. @end table
  1598. For the class @code{cl_I}:
  1599. @table @code
  1600. @item @var{type}& operator += (@var{type}&, const @var{type}&)
  1601. @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
  1602. @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
  1603. @itemx @var{type}& operator &= (@var{type}&, const @var{type}&)
  1604. @itemx @var{type}& operator |= (@var{type}&, const @var{type}&)
  1605. @itemx @var{type}& operator ^= (@var{type}&, const @var{type}&)
  1606. @itemx @var{type}& operator <<= (@var{type}&, const @var{type}&)
  1607. @itemx @var{type}& operator >>= (@var{type}&, const @var{type}&)
  1608. @end table
  1609. For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  1610. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
  1611. @table @code
  1612. @item @var{type}& operator ++ (@var{type}& x)
  1613. The prefix operator @code{++x}.
  1614. @item void operator ++ (@var{type}& x, int)
  1615. The postfix operator @code{x++}.
  1616. @item @var{type}& operator -- (@var{type}& x)
  1617. The prefix operator @code{--x}.
  1618. @item void operator -- (@var{type}& x, int)
  1619. The postfix operator @code{x--}.
  1620. @end table
  1621. Note that by using these obfuscating operators, you wouldn't gain efficiency:
  1622. In CLN @samp{x += y;} is exactly the same as @samp{x = x+y;}, not more
  1623. efficient.
  1624. @chapter Input/Output
  1625. @section Internal and printed representation
  1626. All computations deal with the internal representations of the numbers.
  1627. Every number has an external representation as a sequence of ASCII characters.
  1628. Several external representations may denote the same number, for example,
  1629. "20.0" and "20.000".
  1630. Converting an internal to an external representation is called ``printing'',
  1631. converting an external to an internal representation is called ``reading''.
  1632. In CLN, is it always true that conversion of an internal to an external
  1633. representation and then back to an internal representation will yield the
  1634. same internal representation. Symbolically: @code{read(print(x)) == x}.
  1635. This is called ``print-read consistency''.
  1636. Different types of numbers have different external representations (case
  1637. is insignificant):
  1638. @table @asis
  1639. @item Integers
  1640. External representation: @var{sign}@{@var{digit}@}+. The reader also accepts the
  1641. Common Lisp syntaxes @var{sign}@{@var{digit}@}+@code{.} with a trailing dot
  1642. for decimal integers
  1643. and the @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes.
  1644. @item Rational numbers
  1645. External representation: @var{sign}@{@var{digit}@}+@code{/}@{@var{digit}@}+.
  1646. The @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes are allowed
  1647. here as well.
  1648. @item Floating-point numbers
  1649. External representation: @var{sign}@{@var{digit}@}*@var{exponent} or
  1650. @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}*@var{exponent} or
  1651. @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}+. A precision specifier
  1652. of the form _@var{prec} may be appended. There must be at least
  1653. one digit in the non-exponent part. The exponent has the syntax
  1654. @var{expmarker} @var{expsign} @{@var{digit}@}+.
  1655. The exponent marker is
  1656. @itemize @asis
  1657. @item
  1658. @samp{s} for short-floats,
  1659. @item
  1660. @samp{f} for single-floats,
  1661. @item
  1662. @samp{d} for double-floats,
  1663. @item
  1664. @samp{L} for long-floats,
  1665. @end itemize
  1666. or @samp{e}, which denotes a default float format. The precision specifying
  1667. suffix has the syntax _@var{prec} where @var{prec} denotes the number of
  1668. valid mantissa digits (in decimal, excluding leading zeroes), cf. also
  1669. function @samp{cl_float_format}.
  1670. @item Complex numbers
  1671. External representation:
  1672. @itemize @asis
  1673. @item
  1674. In algebraic notation: @code{@var{realpart}+@var{imagpart}i}. Of course,
  1675. if @var{imagpart} is negative, its printed representation begins with
  1676. a @samp{-}, and the @samp{+} between @var{realpart} and @var{imagpart}
  1677. may be omitted. Note that this notation cannot be used when the @var{imagpart}
  1678. is rational and the rational number's base is >18, because the @samp{i}
  1679. is then read as a digit.
  1680. @item
  1681. In Common Lisp notation: @code{#C(@var{realpart} @var{imagpart})}.
  1682. @end itemize
  1683. @end table
  1684. @section Input functions
  1685. Including @code{<cl_io.h>} defines a type @code{cl_istream}, which is
  1686. the type of the first argument to all input functions. Unless you build
  1687. and use CLN with the macro CL_IO_STDIO being defined, @code{cl_istream}
  1688. is the same as @code{istream&}.
  1689. The variable
  1690. @itemize @asis
  1691. @item
  1692. @code{cl_istream cl_stdin}
  1693. @end itemize
  1694. contains the standard input stream.
  1695. These are the simple input functions:
  1696. @table @code
  1697. @item int freadchar (cl_istream stream)
  1698. Reads a character from @code{stream}. Returns @code{cl_EOF} (not a @samp{char}!)
  1699. if the end of stream was encountered or an error occurred.
  1700. @item int funreadchar (cl_istream stream, int c)
  1701. Puts back @code{c} onto @code{stream}. @code{c} must be the result of the
  1702. last @code{freadchar} operation on @code{stream}.
  1703. @end table
  1704. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  1705. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1706. defines, in @code{<cl_@var{type}_io.h>}, the following input function:
  1707. @table @code
  1708. @item cl_istream operator>> (cl_istream stream, @var{type}& result)
  1709. Reads a number from @code{stream} and stores it in the @code{result}.
  1710. @end table
  1711. The most flexible input functions, defined in @code{<cl_@var{type}_io.h>},
  1712. are the following:
  1713. @table @code
  1714. @item cl_N read_complex (cl_istream stream, const cl_read_flags& flags)
  1715. @itemx cl_R read_real (cl_istream stream, const cl_read_flags& flags)
  1716. @itemx cl_F read_float (cl_istream stream, const cl_read_flags& flags)
  1717. @itemx cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)
  1718. @itemx cl_I read_integer (cl_istream stream, const cl_read_flags& flags)
  1719. Reads a number from @code{stream}. The @code{flags} are parameters which
  1720. affect the input syntax. Whitespace before the number is silently skipped.
  1721. @item cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  1722. @itemx cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  1723. @itemx cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  1724. @itemx cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  1725. @itemx cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
  1726. Reads a number from a string in memory. The @code{flags} are parameters which
  1727. affect the input syntax. The string starts at @code{string} and ends at
  1728. @code{string_limit} (exclusive limit). @code{string_limit} may also be
  1729. @code{NULL}, denoting the entire string, i.e. equivalent to
  1730. @code{string_limit = string + strlen(string)}. If @code{end_of_parse} is
  1731. @code{NULL}, the string in memory must contain exactly one number and nothing
  1732. more, else a fatal error will be signalled. If @code{end_of_parse}
  1733. is not @code{NULL}, @code{*end_of_parse} will be assigned a pointer past
  1734. the last parsed character (i.e. @code{string_limit} if nothing came after
  1735. the number). Whitespace is not allowed.
  1736. @end table
  1737. The structure @code{cl_read_flags} contains the following fields:
  1738. @table @code
  1739. @item cl_read_syntax_t syntax
  1740. The possible results of the read operation. Possible values are
  1741. @code{syntax_number}, @code{syntax_real}, @code{syntax_rational},
  1742. @code{syntax_integer}, @code{syntax_float}, @code{syntax_sfloat},
  1743. @code{syntax_ffloat}, @code{syntax_dfloat}, @code{syntax_lfloat}.
  1744. @item cl_read_lsyntax_t lsyntax
  1745. Specifies the language-dependent syntax variant for the read operation.
  1746. Possible values are
  1747. @table @code
  1748. @item lsyntax_standard
  1749. accept standard algebraic notation only, no complex numbers,
  1750. @item lsyntax_algebraic
  1751. accept the algebraic notation @code{@var{x}+@var{y}i} for complex numbers,
  1752. @item lsyntax_commonlisp
  1753. accept the @code{#b}, @code{#o}, @code{#x} syntaxes for binary, octal,
  1754. hexadecimal numbers,
  1755. @code{#@var{base}R} for rational numbers in a given base,
  1756. @code{#c(@var{realpart} @var{imagpart})} for complex numbers,
  1757. @item lsyntax_all
  1758. accept all of these extensions.
  1759. @end table
  1760. @item unsigned int rational_base
  1761. The base in which rational numbers are read.
  1762. @item cl_float_format_t float_flags.default_float_format
  1763. The float format used when reading floats with exponent marker @samp{e}.
  1764. @item cl_float_format_t float_flags.default_lfloat_format
  1765. The float format used when reading floats with exponent marker @samp{l}.
  1766. @item cl_boolean float_flags.mantissa_dependent_float_format
  1767. When this flag is true, floats specified with more digits than corresponding
  1768. to the exponent marker they contain, but without @var{_nnn} suffix, will get a
  1769. precision corresponding to their number of significant digits.
  1770. @end table
  1771. @section Output functions
  1772. Including @code{<cl_io.h>} defines a type @code{cl_ostream}, which is
  1773. the type of the first argument to all output functions. Unless you build
  1774. and use CLN with the macro CL_IO_STDIO being defined, @code{cl_ostream}
  1775. is the same as @code{ostream&}.
  1776. The variable
  1777. @itemize @asis
  1778. @item
  1779. @code{cl_ostream cl_stdout}
  1780. @end itemize
  1781. contains the standard output stream.
  1782. The variable
  1783. @itemize @asis
  1784. @item
  1785. @code{cl_ostream cl_stderr}
  1786. @end itemize
  1787. contains the standard error output stream.
  1788. These are the simple output functions:
  1789. @table @code
  1790. @item void fprintchar (cl_ostream stream, char c)
  1791. Prints the character @code{x} literally on the @code{stream}.
  1792. @item void fprint (cl_ostream stream, const char * string)
  1793. Prints the @code{string} literally on the @code{stream}.
  1794. @item void fprintdecimal (cl_ostream stream, int x)
  1795. @itemx void fprintdecimal (cl_ostream stream, const cl_I& x)
  1796. Prints the integer @code{x} in decimal on the @code{stream}.
  1797. @item void fprintbinary (cl_ostream stream, const cl_I& x)
  1798. Prints the integer @code{x} in binary (base 2, without prefix)
  1799. on the @code{stream}.
  1800. @item void fprintoctal (cl_ostream stream, const cl_I& x)
  1801. Prints the integer @code{x} in octal (base 8, without prefix)
  1802. on the @code{stream}.
  1803. @item void fprinthexadecimal (cl_ostream stream, const cl_I& x)
  1804. Prints the integer @code{x} in hexadecimal (base 16, without prefix)
  1805. on the @code{stream}.
  1806. @end table
  1807. Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
  1808. @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
  1809. defines, in @code{<cl_@var{type}_io.h>}, the following output functions:
  1810. @table @code
  1811. @item void fprint (cl_ostream stream, const @var{type}& x)
  1812. @itemx cl_ostream operator<< (cl_ostream stream, const @var{type}& x)
  1813. Prints the number @code{x} on the @code{stream}. The output may depend
  1814. on the global printer settings in the variable @code{cl_default_print_flags}.
  1815. The @code{ostream} flags and settings (flags, width and locale) are
  1816. ignored.
  1817. @end table
  1818. The most flexible output function, defined in @code{<cl_@var{type}_io.h>},
  1819. are the following:
  1820. @example
  1821. void print_complex (cl_ostream stream, const cl_print_flags& flags,
  1822. const cl_N& z);
  1823. void print_real (cl_ostream stream, const cl_print_flags& flags,
  1824. const cl_R& z);
  1825. void print_float (cl_ostream stream, const cl_print_flags& flags,
  1826. const cl_F& z);
  1827. void print_rational (cl_ostream stream, const cl_print_flags& flags,
  1828. const cl_RA& z);
  1829. void print_integer (cl_ostream stream, const cl_print_flags& flags,
  1830. const cl_I& z);
  1831. @end example
  1832. Prints the number @code{x} on the @code{stream}. The @code{flags} are
  1833. parameters which affect the output.
  1834. The structure type @code{cl_print_flags} contains the following fields:
  1835. @table @code
  1836. @item unsigned int rational_base
  1837. The base in which rational numbers are printed. Default is @code{10}.
  1838. @item cl_boolean rational_readably
  1839. If this flag is true, rational numbers are printed with radix specifiers in
  1840. Common Lisp syntax (@code{#@var{n}R} or @code{#b} or @code{#o} or @code{#x}
  1841. prefixes, trailing dot). Default is false.
  1842. @item cl_boolean float_readably
  1843. If this flag is true, type specific exponent markers have precedence over 'E'.
  1844. Default is false.
  1845. @item cl_float_format_t default_float_format
  1846. Floating point numbers of this format will be printed using the 'E' exponent
  1847. marker. Default is @code{cl_float_format_ffloat}.
  1848. @item cl_boolean complex_readably
  1849. If this flag is true, complex numbers will be printed using the Common Lisp
  1850. syntax @code{#C(@var{realpart} @var{imagpart})}. Default is false.
  1851. @item cl_string univpoly_varname
  1852. Univariate polynomials with no explicit indeterminate name will be printed
  1853. using this variable name. Default is @code{"x"}.
  1854. @end table
  1855. The global variable @code{cl_default_print_flags} contains the default values,
  1856. used by the function @code{fprint},
  1857. @chapter Rings
  1858. CLN has a class of abstract rings.
  1859. @example
  1860. Ring
  1861. cl_ring
  1862. <cl_ring.h>
  1863. @end example
  1864. Rings can be compared for equality:
  1865. @table @code
  1866. @item bool operator== (const cl_ring&, const cl_ring&)
  1867. @itemx bool operator!= (const cl_ring&, const cl_ring&)
  1868. These compare two rings for equality.
  1869. @end table
  1870. Given a ring @code{R}, the following members can be used.
  1871. @table @code
  1872. @item void R->fprint (cl_ostream stream, const cl_ring_element& x)
  1873. @itemx cl_boolean R->equal (const cl_ring_element& x, const cl_ring_element& y)
  1874. @itemx cl_ring_element R->zero ()
  1875. @itemx cl_boolean R->zerop (const cl_ring_element& x)
  1876. @itemx cl_ring_element R->plus (const cl_ring_element& x, const cl_ring_element& y)
  1877. @itemx cl_ring_element R->minus (const cl_ring_element& x, const cl_ring_element& y)
  1878. @itemx cl_ring_element R->uminus (const cl_ring_element& x)
  1879. @itemx cl_ring_element R->one ()
  1880. @itemx cl_ring_element R->canonhom (const cl_I& x)
  1881. @itemx cl_ring_element R->mul (const cl_ring_element& x, const cl_ring_element& y)
  1882. @itemx cl_ring_element R->square (const cl_ring_element& x)
  1883. @itemx cl_ring_element R->expt_pos (const cl_ring_element& x, const cl_I& y)
  1884. @end table
  1885. The following rings are built-in.
  1886. @table @code
  1887. @item cl_null_ring cl_0_ring
  1888. The null ring, containing only zero.
  1889. @item cl_complex_ring cl_C_ring
  1890. The ring of complex numbers. This corresponds to the type @code{cl_N}.
  1891. @item cl_real_ring cl_R_ring
  1892. The ring of real numbers. This corresponds to the type @code{cl_R}.
  1893. @item cl_rational_ring cl_RA_ring
  1894. The ring of rational numbers. This corresponds to the type @code{cl_RA}.
  1895. @item cl_integer_ring cl_I_ring
  1896. The ring of integers. This corresponds to the type @code{cl_I}.
  1897. @end table
  1898. Type tests can be performed for any of @code{cl_C_ring}, @code{cl_R_ring},
  1899. @code{cl_RA_ring}, @code{cl_I_ring}:
  1900. @table @code
  1901. @item cl_boolean instanceof (const cl_number& x, const cl_number_ring& R)
  1902. Tests whether the given number is an element of the number ring R.
  1903. @end table
  1904. @chapter Modular integers
  1905. @section Modular integer rings
  1906. CLN implements modular integers, i.e. integers modulo a fixed integer N.
  1907. The modulus is explicitly part of every modular integer. CLN doesn't
  1908. allow you to (accidentally) mix elements of different modular rings,
  1909. e.g. @code{(3 mod 4) + (2 mod 5)} will result in a runtime error.
  1910. (Ideally one would imagine a generic data type @code{cl_MI(N)}, but C++
  1911. doesn't have generic types. So one has to live with runtime checks.)
  1912. The class of modular integer rings is
  1913. @example
  1914. Ring
  1915. cl_ring
  1916. <cl_ring.h>
  1917. |
  1918. |
  1919. Modular integer ring
  1920. cl_modint_ring
  1921. <cl_modinteger.h>
  1922. @end example
  1923. and the class of all modular integers (elements of modular integer rings) is
  1924. @example
  1925. Modular integer
  1926. cl_MI
  1927. <cl_modinteger.h>
  1928. @end example
  1929. Modular integer rings are constructed using the function
  1930. @table @code
  1931. @item cl_modint_ring cl_find_modint_ring (const cl_I& N)
  1932. This function returns the modular ring @samp{Z/NZ}. It takes care
  1933. of finding out about special cases of @code{N}, like powers of two
  1934. and odd numbers for which Montgomery multiplication will be a win,
  1935. and precomputes any necessary auxiliary data for computing modulo @code{N}.
  1936. There is a cache table of rings, indexed by @code{N} (or, more precisely,
  1937. by @code{abs(N)}). This ensures that the precomputation costs are reduced
  1938. to a minimum.
  1939. @end table
  1940. Modular integer rings can be compared for equality:
  1941. @table @code
  1942. @item bool operator== (const cl_modint_ring&, const cl_modint_ring&)
  1943. @itemx bool operator!= (const cl_modint_ring&, const cl_modint_ring&)
  1944. These compare two modular integer rings for equality. Two different calls
  1945. to @code{cl_find_modint_ring} with the same argument necessarily return the
  1946. same ring because it is memoized in the cache table.
  1947. @end table
  1948. @section Functions on modular integers
  1949. Given a modular integer ring @code{R}, the following members can be used.
  1950. @table @code
  1951. @item cl_I R->modulus
  1952. This is the ring's modulus, normalized to be nonnegative: @code{abs(N)}.
  1953. @item cl_MI R->zero()
  1954. This returns @code{0 mod N}.
  1955. @item cl_MI R->one()
  1956. This returns @code{1 mod N}.
  1957. @item cl_MI R->canonhom (const cl_I& x)
  1958. This returns @code{x mod N}.
  1959. @item cl_I R->retract (const cl_MI& x)
  1960. This is a partial inverse function to @code{R->canonhom}. It returns the
  1961. standard representative (@code{>=0}, @code{<N}) of @code{x}.
  1962. @item cl_MI R->random(cl_random_state& randomstate)
  1963. @itemx cl_MI R->random()
  1964. This returns a random integer modulo @code{N}.
  1965. @end table
  1966. The following operations are defined on modular integers.
  1967. @table @code
  1968. @item cl_modint_ring x.ring ()
  1969. Returns the ring to which the modular integer @code{x} belongs.
  1970. @item cl_MI operator+ (const cl_MI&, const cl_MI&)
  1971. Returns the sum of two modular integers. One of the arguments may also be
  1972. a plain integer.
  1973. @item cl_MI operator- (const cl_MI&, const cl_MI&)
  1974. Returns the difference of two modular integers. One of the arguments may also be
  1975. a plain integer.
  1976. @item cl_MI operator- (const cl_MI&)
  1977. Returns the negative of a modular integer.
  1978. @item cl_MI operator* (const cl_MI&, const cl_MI&)
  1979. Returns the product of two modular integers. One of the arguments may also be
  1980. a plain integer.
  1981. @item cl_MI square (const cl_MI&)
  1982. Returns the square of a modular integer.
  1983. @item cl_MI recip (const cl_MI& x)
  1984. Returns the reciprocal @code{x^-1} of a modular integer @code{x}. @code{x}
  1985. must be coprime to the modulus, otherwise an error message is issued.
  1986. @item cl_MI div (const cl_MI& x, const cl_MI& y)
  1987. Returns the quotient @code{x*y^-1} of two modular integers @code{x}, @code{y}.
  1988. @code{y} must be coprime to the modulus, otherwise an error message is issued.
  1989. @item cl_MI expt_pos (const cl_MI& x, const cl_I& y)
  1990. @code{y} must be > 0. Returns @code{x^y}.
  1991. @item cl_MI expt (const cl_MI& x, const cl_I& y)
  1992. Returns @code{x^y}. If @code{y} is negative, @code{x} must be coprime to the
  1993. modulus, else an error message is issued.
  1994. @item cl_MI operator<< (const cl_MI& x, const cl_I& y)
  1995. Returns @code{x*2^y}.
  1996. @item cl_MI operator>> (const cl_MI& x, const cl_I& y)
  1997. Returns @code{x*2^-y}. When @code{y} is positive, the modulus must be odd,
  1998. or an error message is issued.
  1999. @item bool operator== (const cl_MI&, const cl_MI&)
  2000. @itemx bool operator!= (const cl_MI&, const cl_MI&)
  2001. Compares two modular integers, belonging to the same modular integer ring,
  2002. for equality.
  2003. @item cl_boolean zerop (const cl_MI& x)
  2004. Returns true if @code{x} is @code{0 mod N}.
  2005. @end table
  2006. The following output functions are defined (see also the chapter on
  2007. input/output).
  2008. @table @code
  2009. @item void fprint (cl_ostream stream, const cl_MI& x)
  2010. @itemx cl_ostream operator<< (cl_ostream stream, const cl_MI& x)
  2011. Prints the modular integer @code{x} on the @code{stream}. The output may depend
  2012. on the global printer settings in the variable @code{cl_default_print_flags}.
  2013. @end table
  2014. @chapter Symbolic data types
  2015. CLN implements two symbolic (non-numeric) data types: strings and symbols.
  2016. @section Strings
  2017. The class
  2018. @example
  2019. String
  2020. cl_string
  2021. <cl_string.h>
  2022. @end example
  2023. implements immutable strings.
  2024. Strings are constructed through the following constructors:
  2025. @table @code
  2026. @item cl_string (const char * s)
  2027. Returns an immutable copy of the (zero-terminated) C string @code{s}.
  2028. @item cl_string (const char * ptr, unsigned long len)
  2029. Returns an immutable copy of the @code{len} characters at
  2030. @code{ptr[0]}, @dots{}, @code{ptr[len-1]}. NUL characters are allowed.
  2031. @end table
  2032. The following functions are available on strings:
  2033. @table @code
  2034. @item operator =
  2035. Assignment from @code{cl_string} and @code{const char *}.
  2036. @item s.length()
  2037. @itemx strlen(s)
  2038. Returns the length of the string @code{s}.
  2039. @item s[i]
  2040. Returns the @code{i}th character of the string @code{s}.
  2041. @code{i} must be in the range @code{0 <= i < s.length()}.
  2042. @item bool equal (const cl_string& s1, const cl_string& s2)
  2043. Compares two strings for equality. One of the arguments may also be a
  2044. plain @code{const char *}.
  2045. @end table
  2046. @section Symbols
  2047. Symbols are uniquified strings: all symbols with the same name are shared.
  2048. This means that comparison of two symbols is fast (effectively just a pointer
  2049. comparison), whereas comparison of two strings must in the worst case walk
  2050. both strings until their end.
  2051. Symbols are used, for example, as tags for properties, as names of variables
  2052. in polynomial rings, etc.
  2053. Symbols are constructed through the following constructor:
  2054. @table @code
  2055. @item cl_symbol (const cl_string& s)
  2056. Looks up or creates a new symbol with a given name.
  2057. @end table
  2058. The following operations are available on symbols:
  2059. @table @code
  2060. @item cl_string (const cl_symbol& sym)
  2061. Conversion to @code{cl_string}: Returns the string which names the symbol
  2062. @code{sym}.
  2063. @item bool equal (const cl_symbol& sym1, const cl_symbol& sym2)
  2064. Compares two symbols for equality. This is very fast.
  2065. @end table
  2066. @chapter Univariate polynomials
  2067. @section Univariate polynomial rings
  2068. CLN implements univariate polynomials (polynomials in one variable) over an
  2069. arbitrary ring. The indeterminate variable may be either unnamed (and will be
  2070. printed according to @code{cl_default_print_flags.univpoly_varname}, which
  2071. defaults to @samp{x}) or carry a given name. The base ring and the
  2072. indeterminate are explicitly part of every polynomial. CLN doesn't allow you to
  2073. (accidentally) mix elements of different polynomial rings, e.g.
  2074. @code{(a^2+1) * (b^3-1)} will result in a runtime error. (Ideally this should
  2075. return a multivariate polynomial, but they are not yet implemented in CLN.)
  2076. The classes of univariate polynomial rings are
  2077. @example
  2078. Ring
  2079. cl_ring
  2080. <cl_ring.h>
  2081. |
  2082. |
  2083. Univariate polynomial ring
  2084. cl_univpoly_ring
  2085. <cl_univpoly.h>
  2086. |
  2087. +----------------+-------------------+
  2088. | | |
  2089. Complex polynomial ring | Modular integer polynomial ring
  2090. cl_univpoly_complex_ring | cl_univpoly_modint_ring
  2091. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  2092. |
  2093. +----------------+
  2094. | |
  2095. Real polynomial ring |
  2096. cl_univpoly_real_ring |
  2097. <cl_univpoly_real.h> |
  2098. |
  2099. +----------------+
  2100. | |
  2101. Rational polynomial ring |
  2102. cl_univpoly_rational_ring |
  2103. <cl_univpoly_rational.h> |
  2104. |
  2105. +----------------+
  2106. |
  2107. Integer polynomial ring
  2108. cl_univpoly_integer_ring
  2109. <cl_univpoly_integer.h>
  2110. @end example
  2111. and the corresponding classes of univariate polynomials are
  2112. @example
  2113. Univariate polynomial
  2114. cl_UP
  2115. <cl_univpoly.h>
  2116. |
  2117. +----------------+-------------------+
  2118. | | |
  2119. Complex polynomial | Modular integer polynomial
  2120. cl_UP_N | cl_UP_MI
  2121. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  2122. |
  2123. +----------------+
  2124. | |
  2125. Real polynomial |
  2126. cl_UP_R |
  2127. <cl_univpoly_real.h> |
  2128. |
  2129. +----------------+
  2130. | |
  2131. Rational polynomial |
  2132. cl_UP_RA |
  2133. <cl_univpoly_rational.h> |
  2134. |
  2135. +----------------+
  2136. |
  2137. Integer polynomial
  2138. cl_UP_I
  2139. <cl_univpoly_integer.h>
  2140. @end example
  2141. Univariate polynomial rings are constructed using the functions
  2142. @table @code
  2143. @item cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R)
  2144. @itemx cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R, const cl_symbol& varname)
  2145. This function returns the polynomial ring @samp{R[X]}, unnamed or named.
  2146. @code{R} may be an arbitrary ring. This function takes care of finding out
  2147. about special cases of @code{R}, such as the rings of complex numbers,
  2148. real numbers, rational numbers, integers, or modular integer rings.
  2149. There is a cache table of rings, indexed by @code{R} and @code{varname}.
  2150. This ensures that two calls of this function with the same arguments will
  2151. return the same polynomial ring.
  2152. @item cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R)
  2153. @itemx cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R, const cl_symbol& varname)
  2154. @itemx cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R)
  2155. @itemx cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R, const cl_symbol& varname)
  2156. @itemx cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R)
  2157. @itemx cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R, const cl_symbol& varname)
  2158. @itemx cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R)
  2159. @itemx cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R, const cl_symbol& varname)
  2160. @itemx cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R)
  2161. @itemx cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R, const cl_symbol& varname)
  2162. These functions are equivalent to the general @code{cl_find_univpoly_ring},
  2163. only the return type is more specific, according to the base ring's type.
  2164. @end table
  2165. @section Functions on univariate polynomials
  2166. Given a univariate polynomial ring @code{R}, the following members can be used.
  2167. @table @code
  2168. @item cl_ring R->basering()
  2169. This returns the base ring, as passed to @samp{cl_find_univpoly_ring}.
  2170. @item cl_UP R->zero()
  2171. This returns @code{0 in R}, a polynomial of degree -1.
  2172. @item cl_UP R->one()
  2173. This returns @code{1 in R}, a polynomial of degree <= 0.
  2174. @item cl_UP R->canonhom (const cl_I& x)
  2175. This returns @code{x in R}, a polynomial of degree <= 0.
  2176. @item cl_UP R->monomial (const cl_ring_element& x, uintL e)
  2177. This returns a sparse polynomial: @code{x * X^e}, where @code{X} is the
  2178. indeterminate.
  2179. @item cl_UP R->create (sintL degree)
  2180. Creates a new polynomial with a given degree. The zero polynomial has degree
  2181. @code{-1}. After creating the polynomial, you should put in the coefficients,
  2182. using the @code{set_coeff} member function, and then call the @code{finalize}
  2183. member function.
  2184. @end table
  2185. The following are the only destructive operations on univariate polynomials.
  2186. @table @code
  2187. @item void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)
  2188. This changes the coefficient of @code{X^index} in @code{x} to be @code{y}.
  2189. After changing a polynomial and before applying any "normal" operation on it,
  2190. you should call its @code{finalize} member function.
  2191. @item void finalize (cl_UP& x)
  2192. This function marks the endpoint of destructive modifications of a polynomial.
  2193. It normalizes the internal representation so that subsequent computations have
  2194. less overhead. Doing normal computations on unnormalized polynomials may
  2195. produce wrong results or crash the program.
  2196. @end table
  2197. The following operations are defined on univariate polynomials.
  2198. @table @code
  2199. @item cl_univpoly_ring x.ring ()
  2200. Returns the ring to which the univariate polynomial @code{x} belongs.
  2201. @item cl_UP operator+ (const cl_UP&, const cl_UP&)
  2202. Returns the sum of two univariate polynomials.
  2203. @item cl_UP operator- (const cl_UP&, const cl_UP&)
  2204. Returns the difference of two univariate polynomials.
  2205. @item cl_UP operator- (const cl_UP&)
  2206. Returns the negative of a univariate polynomial.
  2207. @item cl_UP operator* (const cl_UP&, const cl_UP&)
  2208. Returns the product of two univariate polynomials. One of the arguments may
  2209. also be a plain integer or an element of the base ring.
  2210. @item cl_UP square (const cl_UP&)
  2211. Returns the square of a univariate polynomial.
  2212. @item cl_UP expt_pos (const cl_UP& x, const cl_I& y)
  2213. @code{y} must be > 0. Returns @code{x^y}.
  2214. @item bool operator== (const cl_UP&, const cl_UP&)
  2215. @itemx bool operator!= (const cl_UP&, const cl_UP&)
  2216. Compares two univariate polynomials, belonging to the same univariate
  2217. polynomial ring, for equality.
  2218. @item cl_boolean zerop (const cl_UP& x)
  2219. Returns true if @code{x} is @code{0 in R}.
  2220. @item sintL degree (const cl_UP& x)
  2221. Returns the degree of the polynomial. The zero polynomial has degree @code{-1}.
  2222. @item cl_ring_element coeff (const cl_UP& x, uintL index)
  2223. Returns the coefficient of @code{X^index} in the polynomial @code{x}.
  2224. @item cl_ring_element x (const cl_ring_element& y)
  2225. Evaluation: If @code{x} is a polynomial and @code{y} belongs to the base ring,
  2226. then @samp{x(y)} returns the value of the substitution of @code{y} into
  2227. @code{x}.
  2228. @item cl_UP deriv (const cl_UP& x)
  2229. Returns the derivative of the polynomial @code{x} with respect to the
  2230. indeterminate @code{X}.
  2231. @end table
  2232. The following output functions are defined (see also the chapter on
  2233. input/output).
  2234. @table @code
  2235. @item void fprint (cl_ostream stream, const cl_UP& x)
  2236. @itemx cl_ostream operator<< (cl_ostream stream, const cl_UP& x)
  2237. Prints the univariate polynomial @code{x} on the @code{stream}. The output may
  2238. depend on the global printer settings in the variable
  2239. @code{cl_default_print_flags}.
  2240. @end table
  2241. @section Special polynomials
  2242. The following functions return special polynomials.
  2243. @table @code
  2244. @item cl_UP_I cl_tschebychev (sintL n)
  2245. Returns the n-th Tchebychev polynomial (n >= 0).
  2246. @item cl_UP_I cl_hermite (sintL n)
  2247. Returns the n-th Hermite polynomial (n >= 0).
  2248. @item cl_UP_RA cl_legendre (sintL n)
  2249. Returns the n-th Legendre polynomial (n >= 0).
  2250. @item cl_UP_I cl_laguerre (sintL n)
  2251. Returns the n-th Laguerre polynomial (n >= 0).
  2252. @end table
  2253. Information how to derive the differential equation satisfied by each
  2254. of these polynomials from their definition can be found in the
  2255. @code{doc/polynomial/} directory.
  2256. @chapter Internals
  2257. @section Why C++ ?
  2258. Using C++ as an implementation language provides
  2259. @itemize @bullet
  2260. @item
  2261. Efficiency: It compiles to machine code.
  2262. @item
  2263. Portability: It runs on all platforms supporting a C++ compiler. Because
  2264. of the availability of GNU C++, this includes all currently used 32-bit and
  2265. 64-bit platforms, independently of the quality of the vendor's C++ compiler.
  2266. @item
  2267. Type safety: The C++ compilers knows about the number types and complains if,
  2268. for example, you try to assign a float to an integer variable. However,
  2269. a drawback is that C++ doesn't know about generic types, hence a restriction
  2270. like that @code{operation+ (const cl_MI&, const cl_MI&)} requires that both
  2271. arguments belong to the same modular ring cannot be expressed as a compile-time
  2272. information.
  2273. @item
  2274. Algebraic syntax: The elementary operations @code{+}, @code{-}, @code{*},
  2275. @code{=}, @code{==}, ... can be used in infix notation, which is more
  2276. convenient than Lisp notation @samp{(+ x y)} or C notation @samp{add(x,y,&z)}.
  2277. @end itemize
  2278. With these language features, there is no need for two separate languages,
  2279. one for the implementation of the library and one in which the library's users
  2280. can program. This means that a prototype implementation of an algorithm
  2281. can be integrated into the library immediately after it has been tested and
  2282. debugged. No need to rewrite it in a low-level language after having prototyped
  2283. in a high-level language.
  2284. @section Memory efficiency
  2285. In order to save memory allocations, CLN implements:
  2286. @itemize @bullet
  2287. @item
  2288. Object sharing: An operation like @code{x+0} returns @code{x} without copying
  2289. it.
  2290. @item
  2291. Garbage collection: A reference counting mechanism makes sure that any
  2292. number object's storage is freed immediately when the last reference to the
  2293. object is gone.
  2294. @item
  2295. Small integers are represented as immediate values instead of pointers
  2296. to heap allocated storage. This means that integers @code{> -2^29},
  2297. @code{< 2^29} don't consume heap memory, unless they were explicitly allocated
  2298. on the heap.
  2299. @end itemize
  2300. @section Speed efficiency
  2301. Speed efficiency is obtained by the combination of the following tricks
  2302. and algorithms:
  2303. @itemize @bullet
  2304. @item
  2305. Small integers, being represented as immediate values, don't require
  2306. memory access, just a couple of instructions for each elementary operation.
  2307. @item
  2308. The kernel of CLN has been written in assembly language for some CPUs
  2309. (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
  2310. @item
  2311. On all CPUs, CLN uses the superefficient low-level routines from GNU
  2312. GMP version 2.
  2313. @item
  2314. For large numbers, CLN uses, instead of the standard @code{O(N^2)}
  2315. algorithm, the Karatsuba multiplication, which is an
  2316. @iftex
  2317. @tex
  2318. $O(N^{1.6})$
  2319. @end tex
  2320. @end iftex
  2321. @ifinfo
  2322. @code{O(N^1.6)}
  2323. @end ifinfo
  2324. algorithm.
  2325. @item
  2326. For very large numbers (more than 12000 decimal digits), CLN uses
  2327. @iftex
  2328. Sch{@"o}nhage-Strassen
  2329. @end iftex
  2330. @ifinfo
  2331. Sch�nhage-Strassen
  2332. @end ifinfo
  2333. multiplication, which is an asymptotically
  2334. optimal multiplication algorithm.
  2335. @item
  2336. These fast multiplication algorithms also give improvements in the speed
  2337. of division and radix conversion.
  2338. @end itemize
  2339. @section Garbage collection
  2340. All the number classes are reference count classes: They only contain a pointer
  2341. to an object in the heap. Upon construction, assignment and destruction of
  2342. number objects, only the objects' reference count are manipulated.
  2343. Memory occupied by number objects are automatically reclaimed as soon as
  2344. their reference count drops to zero.
  2345. For number rings, another strategy is implemented: There is a cache of,
  2346. for example, the modular integer rings. A modular integer ring is destroyed
  2347. only if its reference count dropped to zero and the cache is about to be
  2348. resized. The effect of this strategy is that recently used rings remain
  2349. cached, whereas undue memory consumption through cached rings is avoided.
  2350. @chapter Using the library
  2351. For the following discussion, we will assume that you have installed
  2352. the CLN source in @code{$CLN_DIR} and built it in @code{$CLN_TARGETDIR}.
  2353. For example, for me it's @code{CLN_DIR="$HOME/cln"} and
  2354. @code{CLN_TARGETDIR="$HOME/cln/linuxelf"}. You might define these as
  2355. environment variables, or directly substitute the appropriate values.
  2356. @section Compiler options
  2357. Until you have installed CLN in a public place, the following options are
  2358. needed:
  2359. When you compile CLN application code, add the flags
  2360. @example
  2361. -I$CLN_DIR/include -I$CLN_TARGETDIR/include
  2362. @end example
  2363. to the C++ compiler's command line (@code{make} variable CFLAGS or CXXFLAGS).
  2364. When you link CLN application code to form an executable, add the flags
  2365. @example
  2366. $CLN_TARGETDIR/src/libcln.a
  2367. @end example
  2368. to the C/C++ compiler's command line (@code{make} variable LIBS).
  2369. If you did a @code{make install}, the include files are installed in a
  2370. public directory (normally @code{/usr/local/include}), hence you don't
  2371. need special flags for compiling. The library has been installed to a
  2372. public directory as well (normally @code{/usr/local/lib}), hence when
  2373. linking a CLN application it is sufficient to give the flag @code{-lcln}.
  2374. @section Include files
  2375. Here is a summary of the include files and their contents.
  2376. @table @code
  2377. @item <cl_object.h>
  2378. General definitions, reference counting, garbage collection.
  2379. @item <cl_number.h>
  2380. The class cl_number.
  2381. @item <cl_complex.h>
  2382. Functions for class cl_N, the complex numbers.
  2383. @item <cl_real.h>
  2384. Functions for class cl_R, the real numbers.
  2385. @item <cl_float.h>
  2386. Functions for class cl_F, the floats.
  2387. @item <cl_sfloat.h>
  2388. Functions for class cl_SF, the short-floats.
  2389. @item <cl_ffloat.h>
  2390. Functions for class cl_FF, the single-floats.
  2391. @item <cl_dfloat.h>
  2392. Functions for class cl_DF, the double-floats.
  2393. @item <cl_lfloat.h>
  2394. Functions for class cl_LF, the long-floats.
  2395. @item <cl_rational.h>
  2396. Functions for class cl_RA, the rational numbers.
  2397. @item <cl_integer.h>
  2398. Functions for class cl_I, the integers.
  2399. @item <cl_io.h>
  2400. Input/Output.
  2401. @item <cl_complex_io.h>
  2402. Input/Output for class cl_N, the complex numbers.
  2403. @item <cl_real_io.h>
  2404. Input/Output for class cl_R, the real numbers.
  2405. @item <cl_float_io.h>
  2406. Input/Output for class cl_F, the floats.
  2407. @item <cl_sfloat_io.h>
  2408. Input/Output for class cl_SF, the short-floats.
  2409. @item <cl_ffloat_io.h>
  2410. Input/Output for class cl_FF, the single-floats.
  2411. @item <cl_dfloat_io.h>
  2412. Input/Output for class cl_DF, the double-floats.
  2413. @item <cl_lfloat_io.h>
  2414. Input/Output for class cl_LF, the long-floats.
  2415. @item <cl_rational_io.h>
  2416. Input/Output for class cl_RA, the rational numbers.
  2417. @item <cl_integer_io.h>
  2418. Input/Output for class cl_I, the integers.
  2419. @item <cl_input.h>
  2420. Flags for customizing input operations.
  2421. @item <cl_output.h>
  2422. Flags for customizing output operations.
  2423. @item <cl_malloc.h>
  2424. @code{cl_malloc_hook}, @code{cl_free_hook}.
  2425. @item <cl_abort.h>
  2426. @code{cl_abort}.
  2427. @item <cl_condition.h>
  2428. Conditions/exceptions.
  2429. @item <cl_string.h>
  2430. Strings.
  2431. @item <cl_symbol.h>
  2432. Symbols.
  2433. @item <cl_proplist.h>
  2434. Property lists.
  2435. @item <cl_ring.h>
  2436. General rings.
  2437. @item <cl_null_ring.h>
  2438. The null ring.
  2439. @item <cl_complex_ring.h>
  2440. The ring of complex numbers.
  2441. @item <cl_real_ring.h>
  2442. The ring of real numbers.
  2443. @item <cl_rational_ring.h>
  2444. The ring of rational numbers.
  2445. @item <cl_integer_ring.h>
  2446. The ring of integers.
  2447. @item <cl_numtheory.h>
  2448. Number threory functions.
  2449. @item <cl_modinteger.h>
  2450. Modular integers.
  2451. @item <cl_V.h>
  2452. Vectors.
  2453. @item <cl_GV.h>
  2454. General vectors.
  2455. @item <cl_GV_number.h>
  2456. General vectors over cl_number.
  2457. @item <cl_GV_complex.h>
  2458. General vectors over cl_N.
  2459. @item <cl_GV_real.h>
  2460. General vectors over cl_R.
  2461. @item <cl_GV_rational.h>
  2462. General vectors over cl_RA.
  2463. @item <cl_GV_integer.h>
  2464. General vectors over cl_I.
  2465. @item <cl_GV_modinteger.h>
  2466. General vectors of modular integers.
  2467. @item <cl_SV.h>
  2468. Simple vectors.
  2469. @item <cl_SV_number.h>
  2470. Simple vectors over cl_number.
  2471. @item <cl_SV_complex.h>
  2472. Simple vectors over cl_N.
  2473. @item <cl_SV_real.h>
  2474. Simple vectors over cl_R.
  2475. @item <cl_SV_rational.h>
  2476. Simple vectors over cl_RA.
  2477. @item <cl_SV_integer.h>
  2478. Simple vectors over cl_I.
  2479. @item <cl_SV_ringelt.h>
  2480. Simple vectors of general ring elements.
  2481. @item <cl_univpoly.h>
  2482. Univariate polynomials.
  2483. @item <cl_univpoly_integer.h>
  2484. Univariate polynomials over the integers.
  2485. @item <cl_univpoly_rational.h>
  2486. Univariate polynomials over the rational numbers.
  2487. @item <cl_univpoly_real.h>
  2488. Univariate polynomials over the real numbers.
  2489. @item <cl_univpoly_complex.h>
  2490. Univariate polynomials over the complex numbers.
  2491. @item <cl_univpoly_modint.h>
  2492. Univariate polynomials over modular integer rings.
  2493. @item <cl_timing.h>
  2494. Timing facilities.
  2495. @item <cln.h>
  2496. Includes all of the above.
  2497. @end table
  2498. @section An Example
  2499. A function which computes the nth Fibonacci number can be written as follows.
  2500. @example
  2501. #include <cl_integer.h>
  2502. #include <cl_real.h>
  2503. // Returns F_n, computed as the nearest integer to
  2504. // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
  2505. const cl_I fibonacci (int n)
  2506. @{
  2507. // Need a precision of ((1+sqrt(5))/2)^-n.
  2508. cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
  2509. cl_R sqrt5 = sqrt(cl_float(5,prec));
  2510. cl_R phi = (1+sqrt5)/2;
  2511. return round1( expt(phi,n)/sqrt5 );
  2512. @}
  2513. @end example
  2514. Let's explain what is going on in detail.
  2515. The include file @code{<cl_integer.h>} is necessary because the type
  2516. @code{cl_I} is used in the function, and the include file @code{<cl_real.h>}
  2517. is needed for the type @code{cl_R} and the floating point number functions.
  2518. The order of the include files does not matter.
  2519. Then comes the function declaration. The argument is an @code{int}, the
  2520. result an integer. The return type is defined as @samp{const cl_I}, not
  2521. simply @samp{cl_I}, because that allows the compiler to detect typos like
  2522. @samp{fibonacci(n) = 100}. It would be possible to declare the return
  2523. type as @code{const cl_R} (real number) or even @code{const cl_N} (complex
  2524. number). We use the most specialized possible return type because functions
  2525. which call @samp{fibonacci} will be able to profit from the compiler's type
  2526. analysis: Adding two integers is slightly more efficient than adding the
  2527. same objects declared as complex numbers, because it needs less type
  2528. dispatch. Also, when linking to CLN as a non-shared library, this minimizes
  2529. the size of the resulting executable program.
  2530. The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
  2531. integer. In order to get a correct result, the absolute error should be less
  2532. than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
  2533. To this end, the first line computes a floating point precision for sqrt(5)
  2534. and phi.
  2535. Then sqrt(5) is computed by first converting the integer 5 to a floating point
  2536. number and than taking the square root. The converse, first taking the square
  2537. root of 5, and then converting to the desired precision, would not work in
  2538. CLN: The square root would be computed to a default precision (normally
  2539. single-float precision), and the following conversion could not help about
  2540. the lacking accuracy. This is because CLN is not a symbolic computer algebra
  2541. system and does not represent sqrt(5) in a non-numeric way.
  2542. The type @code{cl_R} for sqrt5 and, in the following line, phi is the only
  2543. possible choice. You cannot write @code{cl_F} because the C++ compiler can
  2544. only infer that @code{cl_float(5,prec)} is a real number. You cannot write
  2545. @code{cl_N} because a @samp{round1} does not exist for general complex
  2546. numbers.
  2547. When the function returns, all the local variables in the function are
  2548. automatically reclaimed (garbage collected). Only the result survives and
  2549. gets passed to the caller.
  2550. @section Debugging support
  2551. When debugging a CLN application with GNU @code{gdb}, two facilities are
  2552. available from the library:
  2553. @itemize @bullet
  2554. @item The library does type checks, range checks, consistency checks at
  2555. many places. When one of these fails, the function @code{cl_abort()} is
  2556. called. Its default implementation is to perform an @code{exit(1)}, so
  2557. you won't have a core dump. But for debugging, it is best to set a
  2558. breakpoint at this function:
  2559. @example
  2560. (gdb) break cl_abort
  2561. @end example
  2562. When this breakpoint is hit, look at the stack's backtrace:
  2563. @example
  2564. (gdb) where
  2565. @end example
  2566. @item The debugger's normal @code{print} command doesn't know about
  2567. CLN's types and therefore prints mostly useless hexadecimal addresses.
  2568. CLN offers a function @code{cl_print}, callable from the debugger,
  2569. for printing number objects. In order to get this function, you have
  2570. to define the macro @samp{CL_DEBUG} and then include all the header files
  2571. for which you want @code{cl_print} debugging support. For example:
  2572. @example
  2573. #define CL_DEBUG
  2574. #include <cl_string.h>
  2575. @end example
  2576. Now, if you have in your program a variable @code{cl_string s}, and
  2577. inspect it under @code{gdb}, the output may look like this:
  2578. @example
  2579. (gdb) print s
  2580. $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
  2581. word = 134568800@}@}, @}
  2582. (gdb) call cl_print(s)
  2583. (cl_string) ""
  2584. $8 = 134568800
  2585. @end example
  2586. Note that the output of @code{cl_print} goes to the program's error output,
  2587. not to gdb's standard output.
  2588. Note, however, that the above facility does not work with all CLN types,
  2589. only with number objects and similar. Therefore CLN offers a member function
  2590. @code{debug_print()} on all CLN types. The same macro @samp{CL_DEBUG}
  2591. is needed for this member function to be implemented. Under @code{gdb},
  2592. you call it like this:
  2593. @example
  2594. (gdb) print s
  2595. $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
  2596. word = 134568800@}@}, @}
  2597. (gdb) call s.debug_print()
  2598. (cl_string) ""
  2599. (gdb) define cprint
  2600. >call ($1).debug_print()
  2601. >end
  2602. (gdb) cprint s
  2603. (cl_string) ""
  2604. @end example
  2605. Unfortunately, this feature does not seem to work under all circumstances.
  2606. @end itemize
  2607. @chapter Customizing
  2608. @section Error handling
  2609. When a fatal error occurs, an error message is output to the standard error
  2610. output stream, and the function @code{cl_abort} is called. The default
  2611. version of this function (provided in the library) terminates the application.
  2612. To catch such a fatal error, you need to define the function @code{cl_abort}
  2613. yourself, with the prototype
  2614. @example
  2615. #include <cl_abort.h>
  2616. void cl_abort (void);
  2617. @end example
  2618. This function must not return control to its caller.
  2619. @section Floating-point underflow
  2620. Floating point underflow denotes the situation when a floating-point number
  2621. is to be created which is so close to @code{0} that its exponent is too
  2622. low to be represented internally. By default, this causes a fatal error.
  2623. If you set the global variable
  2624. @example
  2625. cl_boolean cl_inhibit_floating_point_underflow
  2626. @end example
  2627. to @code{cl_true}, the error will be inhibited, and a floating-point zero
  2628. will be generated instead.
  2629. The default value of @code{cl_inhibit_floating_point_underflow} is
  2630. @code{cl_false}.
  2631. @section Customizing I/O
  2632. The output of the function @code{fprint} may be customized by changing the
  2633. value of the global variable @code{cl_default_print_flags}.
  2634. @section Customizing the memory allocator
  2635. Every memory allocation of CLN is done through the function pointer
  2636. @code{cl_malloc_hook}. Freeing of this memory is done through the function
  2637. pointer @code{cl_free_hook}. The default versions of these functions,
  2638. provided in the library, call @code{malloc} and @code{free} and check
  2639. the @code{malloc} result against @code{NULL}.
  2640. If you want to provide another memory allocator, you need to define
  2641. the variables @code{cl_malloc_hook} and @code{cl_free_hook} yourself,
  2642. like this:
  2643. @example
  2644. #include <cl_malloc.h>
  2645. void* (*cl_malloc_hook) (size_t size) = @dots{};
  2646. void (*cl_free_hook) (void* ptr) = @dots{};
  2647. @end example
  2648. The @code{cl_malloc_hook} function must not return a @code{NULL} pointer.
  2649. It is not possible to change the memory allocator at runtime, because
  2650. it is already called at program startup by the constructors of some
  2651. global variables.
  2652. @c Indices
  2653. @unnumbered Index
  2654. @printindex my
  2655. @c Table of contents
  2656. @contents
  2657. @bye