You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4093 lines
136 KiB

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