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.

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