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.

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