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.

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