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.

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