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.

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