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.

4093 lines
136 KiB

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