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

4071 lines
135 KiB

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