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.

3686 lines
125 KiB

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