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.

3803 lines
130 KiB

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