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.

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