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.

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