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.

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