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.

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