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.

4069 lines
135 KiB

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