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.

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