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.

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