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.

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