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.

3355 lines
115 KiB

25 years ago
  1. This is Info file cln.info, produced by Makeinfo version 1.68 from the
  2. input file cln.texi.
  3. This file documents CLN, a Class Library for Numbers.
  4. Published by Bruno Haible, `<haible@clisp.cons.org>' and Richard
  5. Kreckel, `<kreckel@ginac.de>'.
  6. Copyright (C) Bruno Haible 1995, 1996, 1997, 1998, 1999, 2000.
  7. Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10. Permission is granted to copy and distribute modified versions of this
  11. manual under the conditions for verbatim copying, provided that the
  12. entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14. Permission is granted to copy and distribute translations of this manual
  15. into another language, under the above conditions for modified versions,
  16. except that this permission notice may be stated in a translation
  17. approved by the author.
  18. 
  19. File: cln.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir)
  20. * Menu:
  21. * Introduction::
  22. * Installation::
  23. * Ordinary number types::
  24. * Functions on numbers::
  25. * Input/Output::
  26. * Rings::
  27. * Modular integers::
  28. * Symbolic data types::
  29. * Univariate polynomials::
  30. * Internals::
  31. * Using the library::
  32. * Customizing::
  33. * Index::
  34. -- The Detailed Node Listing --
  35. Installation
  36. * Prerequisites::
  37. * Building the library::
  38. * Installing the library::
  39. * Cleaning up::
  40. Prerequisites
  41. * C++ compiler::
  42. * Make utility::
  43. * Sed utility::
  44. Ordinary number types
  45. * Exact numbers::
  46. * Floating-point numbers::
  47. * Complex numbers::
  48. * Conversions::
  49. Functions on numbers
  50. * Constructing numbers::
  51. * Elementary functions::
  52. * Elementary rational functions::
  53. * Elementary complex functions::
  54. * Comparisons::
  55. * Rounding functions::
  56. * Roots::
  57. * Transcendental functions::
  58. * Functions on integers::
  59. * Functions on floating-point numbers::
  60. * Conversion functions::
  61. * Random number generators::
  62. * Obfuscating operators::
  63. Constructing numbers
  64. * Constructing integers::
  65. * Constructing rational numbers::
  66. * Constructing floating-point numbers::
  67. * Constructing complex numbers::
  68. Transcendental functions
  69. * Exponential and logarithmic functions::
  70. * Trigonometric functions::
  71. * Hyperbolic functions::
  72. * Euler gamma::
  73. * Riemann zeta::
  74. Functions on integers
  75. * Logical functions::
  76. * Number theoretic functions::
  77. * Combinatorial functions::
  78. Conversion functions
  79. * Conversion to floating-point numbers::
  80. * Conversion to rational numbers::
  81. Input/Output
  82. * Internal and printed representation::
  83. * Input functions::
  84. * Output functions::
  85. Modular integers
  86. * Modular integer rings::
  87. * Functions on modular integers::
  88. Symbolic data types
  89. * Strings::
  90. * Symbols::
  91. Univariate polynomials
  92. * Univariate polynomial rings::
  93. * Functions on univariate polynomials::
  94. * Special polynomials::
  95. Internals
  96. * Why C++ ?::
  97. * Memory efficiency::
  98. * Speed efficiency::
  99. * Garbage collection::
  100. Using the library
  101. * Compiler options::
  102. * Include files::
  103. * An Example::
  104. * Debugging support::
  105. Customizing
  106. * Error handling::
  107. * Floating-point underflow::
  108. * Customizing I/O::
  109. * Customizing the memory allocator::
  110. 
  111. File: cln.info, Node: Introduction, Next: Installation, Prev: Top, Up: Top
  112. Introduction
  113. ************
  114. CLN is a library for computations with all kinds of numbers. It has a
  115. rich set of number classes:
  116. * Integers (with unlimited precision),
  117. * Rational numbers,
  118. * Floating-point numbers:
  119. - Short float,
  120. - Single float,
  121. - Double float,
  122. - Long float (with unlimited precision),
  123. * Complex numbers,
  124. * Modular integers (integers modulo a fixed integer),
  125. * Univariate polynomials.
  126. The subtypes of the complex numbers among these are exactly the types
  127. of numbers known to the Common Lisp language. Therefore `CLN' can be
  128. used for Common Lisp implementations, giving `CLN' another meaning: it
  129. becomes an abbreviation of "Common Lisp Numbers".
  130. The CLN package implements
  131. * Elementary functions (`+', `-', `*', `/', `sqrt', comparisons,
  132. ...),
  133. * Logical functions (logical `and', `or', `not', ...),
  134. * Transcendental functions (exponential, logarithmic, trigonometric,
  135. hyperbolic functions and their inverse functions).
  136. CLN is a C++ library. Using C++ as an implementation language provides
  137. * efficiency: it compiles to machine code,
  138. * type safety: the C++ compiler knows about the number types and
  139. complains if, for example, you try to assign a float to an integer
  140. variable.
  141. * algebraic syntax: You can use the `+', `-', `*', `=', `==', ...
  142. operators as in C or C++.
  143. CLN is memory efficient:
  144. * Small integers and short floats are immediate, not heap allocated.
  145. * Heap-allocated memory is reclaimed through an automatic,
  146. non-interruptive garbage collection.
  147. CLN is speed efficient:
  148. * The kernel of CLN has been written in assembly language for some
  149. CPUs (`i386', `m68k', `sparc', `mips', `arm').
  150. * On all CPUs, CLN uses the superefficient low-level routines from
  151. GNU GMP version 2.
  152. * It uses Karatsuba multiplication, which is significantly faster
  153. for large numbers than the standard multiplication algorithm.
  154. * For very large numbers (more than 12000 decimal digits), it uses
  155. Sch�nhage-Strassen multiplication, which is an asymptotically
  156. optimal multiplication algorithm, for multiplication, division and
  157. radix conversion.
  158. CLN aims at being easily integrated into larger software packages:
  159. * The garbage collection imposes no burden on the main application.
  160. * The library provides hooks for memory allocation and exceptions.
  161. 
  162. File: cln.info, Node: Installation, Next: Ordinary number types, Prev: Introduction, Up: Top
  163. Installation
  164. ************
  165. This section describes how to install the CLN package on your system.
  166. * Menu:
  167. * Prerequisites::
  168. * Building the library::
  169. * Installing the library::
  170. * Cleaning up::
  171. 
  172. File: cln.info, Node: Prerequisites, Next: Building the library, Prev: Installation, Up: Installation
  173. Prerequisites
  174. =============
  175. * Menu:
  176. * C++ compiler::
  177. * Make utility::
  178. * Sed utility::
  179. 
  180. File: cln.info, Node: C++ compiler, Next: Make utility, Prev: Prerequisites, Up: Prerequisites
  181. C++ compiler
  182. ------------
  183. To build CLN, you need a C++ compiler. Actually, you need GNU `g++
  184. 2.7.0' or newer. On HPPA, you need GNU `g++ 2.8.0' or newer. I
  185. recommend GNU `egcs 1.1' or newer.
  186. The following C++ features are used: classes, member functions,
  187. overloading of functions and operators, constructors and destructors,
  188. inline, const, multiple inheritance, templates.
  189. The following C++ features are not used: `new', `delete', virtual
  190. inheritance, exceptions.
  191. CLN relies on semi-automatic ordering of initializations of static and
  192. global variables, a feature which I could implement for GNU g++ only.
  193. 
  194. File: cln.info, Node: Make utility, Next: Sed utility, Prev: C++ compiler, Up: Prerequisites
  195. Make utility
  196. ------------
  197. To build CLN, you also need to have GNU `make' installed.
  198. 
  199. File: cln.info, Node: Sed utility, Prev: Make utility, Up: Prerequisites
  200. Sed utility
  201. -----------
  202. To build CLN on HP-UX, you also need to have GNU `sed' installed. This
  203. is because the libtool script, which creates the CLN library, relies on
  204. `sed', and the vendor's `sed' utility on these systems is too limited.
  205. 
  206. File: cln.info, Node: Building the library, Next: Installing the library, Prev: Prerequisites, Up: Installation
  207. Building the library
  208. ====================
  209. As with any autoconfiguring GNU software, installation is as easy as
  210. this:
  211. $ ./configure
  212. $ make
  213. $ make check
  214. If on your system, `make' is not GNU `make', you have to use `gmake'
  215. instead of `make' above.
  216. The `configure' command checks out some features of your system and C++
  217. compiler and builds the `Makefile's. The `make' command builds the
  218. library. This step may take 4 hours on an average workstation. The
  219. `make check' runs some test to check that no important subroutine has
  220. been miscompiled.
  221. The `configure' command accepts options. To get a summary of them, try
  222. $ ./configure --help
  223. Some of the options are explained in detail in the `INSTALL.generic'
  224. file.
  225. You can specify the C compiler, the C++ compiler and their options
  226. through the following environment variables when running `configure':
  227. `CC'
  228. Specifies the C compiler.
  229. `CFLAGS'
  230. Flags to be given to the C compiler when compiling programs (not
  231. when linking).
  232. `CXX'
  233. Specifies the C++ compiler.
  234. `CXXFLAGS'
  235. Flags to be given to the C++ compiler when compiling programs (not
  236. when linking).
  237. Examples:
  238. $ CC="gcc" CFLAGS="-O" CXX="g++" CXXFLAGS="-O" ./configure
  239. $ CC="gcc -V 2.7.2" CFLAGS="-O -g" \
  240. CXX="g++ -V 2.7.2" CXXFLAGS="-O -g" ./configure
  241. $ CC="gcc -V 2.8.1" CFLAGS="-O -fno-exceptions" \
  242. CXX="g++ -V 2.8.1" CXXFLAGS="-O -fno-exceptions" ./configure
  243. $ CC="gcc -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" \
  244. CXX="g++ -V egcs-2.91.60" CFLAGS="-O2 -fno-exceptions" ./configure
  245. Note that for these environment variables to take effect, you have to
  246. set them (assuming a Bourne-compatible shell) on the same line as the
  247. `configure' command. If you made the settings in earlier shell
  248. commands, you have to `export' the environment variables before calling
  249. `configure'. In a `csh' shell, you have to use the `setenv' command for
  250. setting each of the environment variables.
  251. On Linux, `g++' needs 15 MB to compile the tests. So you should better
  252. have 17 MB swap space and 1 MB room in $TMPDIR.
  253. If you use `g++' version 2.7.x, don't add `-O2' to the CXXFLAGS,
  254. because `g++ -O' generates better code for CLN than `g++ -O2'.
  255. If you use `g++' version 2.8.x or egcs-2.91.x (a.k.a. egcs-1.1) or
  256. gcc-2.95.x, I recommend adding `-fno-exceptions' to the CXXFLAGS. This
  257. will likely generate better code.
  258. If you use `g++' version egcs-2.91.x (egcs-1.1) or gcc-2.95.x on Sparc,
  259. add either `-O' or `-O2 -fno-schedule-insns' to the CXXFLAGS. With
  260. full `-O2', `g++' miscompiles the division routines. Also, for
  261. -enable-shared to work, you need egcs-1.1.2 or newer.
  262. On MIPS (SGI Irix 6), pass option `--without-gmp' to configure. gmp does
  263. not work when compiled in `n32' binary format on Irix.
  264. By default, only a static library is built. You can build CLN as a
  265. shared library too, by calling `configure' with the option
  266. `--enable-shared'. To get it built as a shared library only, call
  267. `configure' with the options `--enable-shared --disable-static'.
  268. If you use `g++' version egcs-2.91.x (egcs-1.1) on Sparc, you cannot
  269. use `--enable-shared' because `g++' would miscompile parts of the
  270. library.
  271. 
  272. File: cln.info, Node: Installing the library, Next: Cleaning up, Prev: Building the library, Up: Installation
  273. Installing the library
  274. ======================
  275. As with any autoconfiguring GNU software, installation is as easy as
  276. this:
  277. $ make install
  278. The `make install' command installs the library and the include files
  279. into public places (`/usr/local/lib/' and `/usr/local/include/', if you
  280. haven't specified a `--prefix' option to `configure'). This step may
  281. require superuser privileges.
  282. If you have already built the library and wish to install it, but didn't
  283. specify `--prefix=...' at configure time, just re-run `configure',
  284. giving it the same options as the first time, plus the `--prefix=...'
  285. option.
  286. 
  287. File: cln.info, Node: Cleaning up, Prev: Installing the library, Up: Installation
  288. Cleaning up
  289. ===========
  290. You can remove system-dependent files generated by `make' through
  291. $ make clean
  292. You can remove all files generated by `make', thus reverting to a
  293. virgin distribution of CLN, through
  294. $ make distclean
  295. 
  296. File: cln.info, Node: Ordinary number types, Next: Functions on numbers, Prev: Installation, Up: Top
  297. Ordinary number types
  298. *********************
  299. CLN implements the following class hierarchy:
  300. Number
  301. cl_number
  302. <cl_number.h>
  303. |
  304. |
  305. Real or complex number
  306. cl_N
  307. <cl_complex.h>
  308. |
  309. |
  310. Real number
  311. cl_R
  312. <cl_real.h>
  313. |
  314. +-------------------+-------------------+
  315. | |
  316. Rational number Floating-point number
  317. cl_RA cl_F
  318. <cl_rational.h> <cl_float.h>
  319. | |
  320. | +-------------+-------------+-------------+
  321. Integer | | | |
  322. cl_I Short-Float Single-Float Double-Float Long-Float
  323. <cl_integer.h> cl_SF cl_FF cl_DF cl_LF
  324. <cl_sfloat.h> <cl_ffloat.h> <cl_dfloat.h> <cl_lfloat.h>
  325. The base class `cl_number' is an abstract base class. It is not useful
  326. to declare a variable of this type except if you want to completely
  327. disable compile-time type checking and use run-time type checking
  328. instead.
  329. The class `cl_N' comprises real and complex numbers. There is no
  330. special class for complex numbers since complex numbers with imaginary
  331. part `0' are automatically converted to real numbers.
  332. The class `cl_R' comprises real numbers of different kinds. It is an
  333. abstract class.
  334. The class `cl_RA' comprises exact real numbers: rational numbers,
  335. including integers. There is no special class for non-integral rational
  336. numbers since rational numbers with denominator `1' are automatically
  337. converted to integers.
  338. The class `cl_F' implements floating-point approximations to real
  339. numbers. It is an abstract class.
  340. * Menu:
  341. * Exact numbers::
  342. * Floating-point numbers::
  343. * Complex numbers::
  344. * Conversions::
  345. 
  346. File: cln.info, Node: Exact numbers, Next: Floating-point numbers, Prev: Ordinary number types, Up: Ordinary number types
  347. Exact numbers
  348. =============
  349. Some numbers are represented as exact numbers: there is no loss of
  350. information when such a number is converted from its mathematical value
  351. to its internal representation. On exact numbers, the elementary
  352. operations (`+', `-', `*', `/', comparisons, ...) compute the completely
  353. correct result.
  354. In CLN, the exact numbers are:
  355. * rational numbers (including integers),
  356. * complex numbers whose real and imaginary parts are both rational
  357. numbers.
  358. Rational numbers are always normalized to the form
  359. `NUMERATOR/DENOMINATOR' where the numerator and denominator are coprime
  360. integers and the denominator is positive. If the resulting denominator
  361. is `1', the rational number is converted to an integer.
  362. Small integers (typically in the range `-2^30'...`2^30-1', for 32-bit
  363. machines) are especially efficient, because they consume no heap
  364. allocation. Otherwise the distinction between these immediate integers
  365. (called "fixnums") and heap allocated integers (called "bignums") is
  366. completely transparent.
  367. 
  368. File: cln.info, Node: Floating-point numbers, Next: Complex numbers, Prev: Exact numbers, Up: Ordinary number types
  369. Floating-point numbers
  370. ======================
  371. Not all real numbers can be represented exactly. (There is an easy
  372. mathematical proof for this: Only a countable set of numbers can be
  373. stored exactly in a computer, even if one assumes that it has unlimited
  374. storage. But there are uncountably many real numbers.) So some
  375. approximation is needed. CLN implements ordinary floating-point
  376. numbers, with mantissa and exponent.
  377. The elementary operations (`+', `-', `*', `/', ...) only return
  378. approximate results. For example, the value of the expression `(cl_F)
  379. 0.3 + (cl_F) 0.4' prints as `0.70000005', not as `0.7'. Rounding errors
  380. like this one are inevitable when computing with floating-point numbers.
  381. Nevertheless, CLN rounds the floating-point results of the operations
  382. `+', `-', `*', `/', `sqrt' according to the "round-to-even" rule: It
  383. first computes the exact mathematical result and then returns the
  384. floating-point number which is nearest to this. If two floating-point
  385. numbers are equally distant from the ideal result, the one with a `0'
  386. in its least significant mantissa bit is chosen.
  387. Similarly, testing floating point numbers for equality `x == y' is
  388. gambling with random errors. Better check for `abs(x - y) < epsilon'
  389. for some well-chosen `epsilon'.
  390. Floating point numbers come in four flavors:
  391. * Short floats, type `cl_SF'. They have 1 sign bit, 8 exponent bits
  392. (including the exponent's sign), and 17 mantissa bits (including
  393. the "hidden" bit). They don't consume heap allocation.
  394. * Single floats, type `cl_FF'. They have 1 sign bit, 8 exponent
  395. bits (including the exponent's sign), and 24 mantissa bits
  396. (including the "hidden" bit). In CLN, they are represented as
  397. IEEE single-precision floating point numbers. This corresponds
  398. closely to the C/C++ type `float'.
  399. * Double floats, type `cl_DF'. They have 1 sign bit, 11 exponent
  400. bits (including the exponent's sign), and 53 mantissa bits
  401. (including the "hidden" bit). In CLN, they are represented as
  402. IEEE double-precision floating point numbers. This corresponds
  403. closely to the C/C++ type `double'.
  404. * Long floats, type `cl_LF'. They have 1 sign bit, 32 exponent bits
  405. (including the exponent's sign), and n mantissa bits (including
  406. the "hidden" bit), where n >= 64. The precision of a long float
  407. is unlimited, but once created, a long float has a fixed
  408. precision. (No "lazy recomputation".)
  409. Of course, computations with long floats are more expensive than those
  410. with smaller floating-point formats.
  411. CLN does not implement features like NaNs, denormalized numbers and
  412. gradual underflow. If the exponent range of some floating-point type is
  413. too limited for your application, choose another floating-point type
  414. with larger exponent range.
  415. As a user of CLN, you can forget about the differences between the four
  416. floating-point types and just declare all your floating-point variables
  417. as being of type `cl_F'. This has the advantage that when you change
  418. the precision of some computation (say, from `cl_DF' to `cl_LF'), you
  419. don't have to change the code, only the precision of the initial
  420. values. Also, many transcendental functions have been declared as
  421. returning a `cl_F' when the argument is a `cl_F', but such declarations
  422. are missing for the types `cl_SF', `cl_FF', `cl_DF', `cl_LF'. (Such
  423. declarations would be wrong if the floating point contagion rule
  424. happened to change in the future.)
  425. 
  426. File: cln.info, Node: Complex numbers, Next: Conversions, Prev: Floating-point numbers, Up: Ordinary number types
  427. Complex numbers
  428. ===============
  429. Complex numbers, as implemented by the class `cl_N', have a real part
  430. and an imaginary part, both real numbers. A complex number whose
  431. imaginary part is the exact number `0' is automatically converted to a
  432. real number.
  433. Complex numbers can arise from real numbers alone, for example through
  434. application of `sqrt' or transcendental functions.
  435. 
  436. File: cln.info, Node: Conversions, Prev: Complex numbers, Up: Ordinary number types
  437. Conversions
  438. ===========
  439. Conversions from any class to any its superclasses ("base classes" in
  440. C++ terminology) is done automatically.
  441. Conversions from the C built-in types `long' and `unsigned long' are
  442. provided for the classes `cl_I', `cl_RA', `cl_R', `cl_N' and
  443. `cl_number'.
  444. Conversions from the C built-in types `int' and `unsigned int' are
  445. provided for the classes `cl_I', `cl_RA', `cl_R', `cl_N' and
  446. `cl_number'. However, these conversions emphasize efficiency. Their
  447. range is therefore limited:
  448. - The conversion from `int' works only if the argument is < 2^29 and
  449. > -2^29.
  450. - The conversion from `unsigned int' works only if the argument is <
  451. 2^29.
  452. In a declaration like `cl_I x = 10;' the C++ compiler is able to do the
  453. conversion of `10' from `int' to `cl_I' at compile time already. On the
  454. other hand, code like `cl_I x = 1000000000;' is in error. So, if you
  455. want to be sure that an `int' whose magnitude is not guaranteed to be <
  456. 2^29 is correctly converted to a `cl_I', first convert it to a `long'.
  457. Similarly, if a large `unsigned int' is to be converted to a `cl_I',
  458. first convert it to an `unsigned long'.
  459. Conversions from the C built-in type `float' are provided for the
  460. classes `cl_FF', `cl_F', `cl_R', `cl_N' and `cl_number'.
  461. Conversions from the C built-in type `double' are provided for the
  462. classes `cl_DF', `cl_F', `cl_R', `cl_N' and `cl_number'.
  463. Conversions from `const char *' are provided for the classes `cl_I',
  464. `cl_RA', `cl_SF', `cl_FF', `cl_DF', `cl_LF', `cl_F', `cl_R', `cl_N'.
  465. The easiest way to specify a value which is outside of the range of the
  466. C++ built-in types is therefore to specify it as a string, like this:
  467. cl_I order_of_rubiks_cube_group = "43252003274489856000";
  468. Note that this conversion is done at runtime, not at compile-time.
  469. Conversions from `cl_I' to the C built-in types `int', `unsigned int',
  470. `long', `unsigned long' are provided through the functions
  471. `int cl_I_to_int (const cl_I& x)'
  472. `unsigned int cl_I_to_uint (const cl_I& x)'
  473. `long cl_I_to_long (const cl_I& x)'
  474. `unsigned long cl_I_to_ulong (const cl_I& x)'
  475. Returns `x' as element of the C type CTYPE. If `x' is not
  476. representable in the range of CTYPE, a runtime error occurs.
  477. Conversions from the classes `cl_I', `cl_RA', `cl_SF', `cl_FF',
  478. `cl_DF', `cl_LF', `cl_F' and `cl_R' to the C built-in types `float' and
  479. `double' are provided through the functions
  480. `float cl_float_approx (const TYPE& x)'
  481. `double cl_double_approx (const TYPE& x)'
  482. Returns an approximation of `x' of C type CTYPE. If `abs(x)' is
  483. too close to 0 (underflow), 0 is returned. If `abs(x)' is too
  484. large (overflow), an IEEE infinity is returned.
  485. Conversions from any class to any of its subclasses ("derived classes"
  486. in C++ terminology) are not provided. Instead, you can assert and check
  487. that a value belongs to a certain subclass, and return it as element of
  488. that class, using the `As' and `The' macros. `As(TYPE)(VALUE)' checks
  489. that VALUE belongs to TYPE and returns it as such. `The(TYPE)(VALUE)'
  490. assumes that VALUE belongs to TYPE and returns it as such. It is your
  491. responsibility to ensure that this assumption is valid. Example:
  492. cl_I x = ...;
  493. if (!(x >= 0)) abort();
  494. cl_I ten_x = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer.
  495. // In general, it would be a rational number.
  496. 
  497. File: cln.info, Node: Functions on numbers, Next: Input/Output, Prev: Ordinary number types, Up: Top
  498. Functions on numbers
  499. ********************
  500. Each of the number classes declares its mathematical operations in the
  501. corresponding include file. For example, if your code operates with
  502. objects of type `cl_I', it should `#include <cl_integer.h>'.
  503. * Menu:
  504. * Constructing numbers::
  505. * Elementary functions::
  506. * Elementary rational functions::
  507. * Elementary complex functions::
  508. * Comparisons::
  509. * Rounding functions::
  510. * Roots::
  511. * Transcendental functions::
  512. * Functions on integers::
  513. * Functions on floating-point numbers::
  514. * Conversion functions::
  515. * Random number generators::
  516. * Obfuscating operators::
  517. 
  518. File: cln.info, Node: Constructing numbers, Next: Elementary functions, Prev: Functions on numbers, Up: Functions on numbers
  519. Constructing numbers
  520. ====================
  521. Here is how to create number objects "from nothing".
  522. * Menu:
  523. * Constructing integers::
  524. * Constructing rational numbers::
  525. * Constructing floating-point numbers::
  526. * Constructing complex numbers::
  527. 
  528. File: cln.info, Node: Constructing integers, Next: Constructing rational numbers, Prev: Constructing numbers, Up: Constructing numbers
  529. Constructing integers
  530. ---------------------
  531. `cl_I' objects are most easily constructed from C integers and from
  532. strings. See *Note Conversions::.
  533. 
  534. File: cln.info, Node: Constructing rational numbers, Next: Constructing floating-point numbers, Prev: Constructing integers, Up: Constructing numbers
  535. Constructing rational numbers
  536. -----------------------------
  537. `cl_RA' objects can be constructed from strings. The syntax for
  538. rational numbers is described in *Note Internal and printed
  539. representation::. Another standard way to produce a rational number is
  540. through application of `operator /' or `recip' on integers.
  541. 
  542. File: cln.info, Node: Constructing floating-point numbers, Next: Constructing complex numbers, Prev: Constructing rational numbers, Up: Constructing numbers
  543. Constructing floating-point numbers
  544. -----------------------------------
  545. `cl_F' objects with low precision are most easily constructed from C
  546. `float' and `double'. See *Note Conversions::.
  547. To construct a `cl_F' with high precision, you can use the conversion
  548. from `const char *', but you have to specify the desired precision
  549. within the string. (See *Note Internal and printed representation::.)
  550. Example:
  551. cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
  552. will set `e' to the given value, with a precision of 40 decimal digits.
  553. The programmatic way to construct a `cl_F' with high precision is
  554. through the `cl_float' conversion function, see *Note Conversion to
  555. floating-point numbers::. For example, to compute `e' to 40 decimal
  556. places, first construct 1.0 to 40 decimal places and then apply the
  557. exponential function:
  558. cl_float_format_t precision = cl_float_format(40);
  559. cl_F e = exp(cl_float(1,precision));
  560. 
  561. File: cln.info, Node: Constructing complex numbers, Prev: Constructing floating-point numbers, Up: Constructing numbers
  562. Constructing complex numbers
  563. ----------------------------
  564. Non-real `cl_N' objects are normally constructed through the function
  565. cl_N complex (const cl_R& realpart, const cl_R& imagpart)
  566. See *Note Elementary complex functions::.
  567. 
  568. File: cln.info, Node: Elementary functions, Next: Elementary rational functions, Prev: Constructing numbers, Up: Functions on numbers
  569. Elementary functions
  570. ====================
  571. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  572. `cl_FF', `cl_DF', `cl_LF' defines the following operations:
  573. `TYPE operator + (const TYPE&, const TYPE&)'
  574. Addition.
  575. `TYPE operator - (const TYPE&, const TYPE&)'
  576. Subtraction.
  577. `TYPE operator - (const TYPE&)'
  578. Returns the negative of the argument.
  579. `TYPE plus1 (const TYPE& x)'
  580. Returns `x + 1'.
  581. `TYPE minus1 (const TYPE& x)'
  582. Returns `x - 1'.
  583. `TYPE operator * (const TYPE&, const TYPE&)'
  584. Multiplication.
  585. `TYPE square (const TYPE& x)'
  586. Returns `x * x'.
  587. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_F', `cl_SF', `cl_FF',
  588. `cl_DF', `cl_LF' defines the following operations:
  589. `TYPE operator / (const TYPE&, const TYPE&)'
  590. Division.
  591. `TYPE recip (const TYPE&)'
  592. Returns the reciprocal of the argument.
  593. The class `cl_I' doesn't define a `/' operation because in the C/C++
  594. language this operator, applied to integral types, denotes the `floor'
  595. or `truncate' operation (which one of these, is implementation
  596. dependent). (*Note Rounding functions::) Instead, `cl_I' defines an
  597. "exact quotient" function:
  598. `cl_I exquo (const cl_I& x, const cl_I& y)'
  599. Checks that `y' divides `x', and returns the quotient `x'/`y'.
  600. The following exponentiation functions are defined:
  601. `cl_I expt_pos (const cl_I& x, const cl_I& y)'
  602. `cl_RA expt_pos (const cl_RA& x, const cl_I& y)'
  603. `y' must be > 0. Returns `x^y'.
  604. `cl_RA expt (const cl_RA& x, const cl_I& y)'
  605. `cl_R expt (const cl_R& x, const cl_I& y)'
  606. `cl_N expt (const cl_N& x, const cl_I& y)'
  607. Returns `x^y'.
  608. Each of the classes `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF', `cl_FF',
  609. `cl_DF', `cl_LF' defines the following operation:
  610. `TYPE abs (const TYPE& x)'
  611. Returns the absolute value of `x'. This is `x' if `x >= 0', and
  612. `-x' if `x <= 0'.
  613. The class `cl_N' implements this as follows:
  614. `cl_R abs (const cl_N x)'
  615. Returns the absolute value of `x'.
  616. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  617. `cl_FF', `cl_DF', `cl_LF' defines the following operation:
  618. `TYPE signum (const TYPE& x)'
  619. Returns the sign of `x', in the same number format as `x'. This
  620. is defined as `x / abs(x)' if `x' is non-zero, and `x' if `x' is
  621. zero. If `x' is real, the value is either 0 or 1 or -1.
  622. 
  623. File: cln.info, Node: Elementary rational functions, Next: Elementary complex functions, Prev: Elementary functions, Up: Functions on numbers
  624. Elementary rational functions
  625. =============================
  626. Each of the classes `cl_RA', `cl_I' defines the following operations:
  627. `cl_I numerator (const TYPE& x)'
  628. Returns the numerator of `x'.
  629. `cl_I denominator (const TYPE& x)'
  630. Returns the denominator of `x'.
  631. The numerator and denominator of a rational number are normalized in
  632. such a way that they have no factor in common and the denominator is
  633. positive.
  634. 
  635. File: cln.info, Node: Elementary complex functions, Next: Comparisons, Prev: Elementary rational functions, Up: Functions on numbers
  636. Elementary complex functions
  637. ============================
  638. The class `cl_N' defines the following operation:
  639. `cl_N complex (const cl_R& a, const cl_R& b)'
  640. Returns the complex number `a+bi', that is, the complex number with
  641. real part `a' and imaginary part `b'.
  642. Each of the classes `cl_N', `cl_R' defines the following operations:
  643. `cl_R realpart (const TYPE& x)'
  644. Returns the real part of `x'.
  645. `cl_R imagpart (const TYPE& x)'
  646. Returns the imaginary part of `x'.
  647. `TYPE conjugate (const TYPE& x)'
  648. Returns the complex conjugate of `x'.
  649. We have the relations
  650. `x = complex(realpart(x), imagpart(x))'
  651. `conjugate(x) = complex(realpart(x), -imagpart(x))'
  652. 
  653. File: cln.info, Node: Comparisons, Next: Rounding functions, Prev: Elementary complex functions, Up: Functions on numbers
  654. Comparisons
  655. ===========
  656. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  657. `cl_FF', `cl_DF', `cl_LF' defines the following operations:
  658. `bool operator == (const TYPE&, const TYPE&)'
  659. `bool operator != (const TYPE&, const TYPE&)'
  660. Comparison, as in C and C++.
  661. `uint32 cl_equal_hashcode (const TYPE&)'
  662. Returns a 32-bit hash code that is the same for any two numbers
  663. which are the same according to `=='. This hash code depends on
  664. the number's value, not its type or precision.
  665. `cl_boolean zerop (const TYPE& x)'
  666. Compare against zero: `x == 0'
  667. Each of the classes `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF', `cl_FF',
  668. `cl_DF', `cl_LF' defines the following operations:
  669. `cl_signean cl_compare (const TYPE& x, const TYPE& y)'
  670. Compares `x' and `y'. Returns +1 if `x'>`y', -1 if `x'<`y', 0 if
  671. `x'=`y'.
  672. `bool operator <= (const TYPE&, const TYPE&)'
  673. `bool operator < (const TYPE&, const TYPE&)'
  674. `bool operator >= (const TYPE&, const TYPE&)'
  675. `bool operator > (const TYPE&, const TYPE&)'
  676. Comparison, as in C and C++.
  677. `cl_boolean minusp (const TYPE& x)'
  678. Compare against zero: `x < 0'
  679. `cl_boolean plusp (const TYPE& x)'
  680. Compare against zero: `x > 0'
  681. `TYPE max (const TYPE& x, const TYPE& y)'
  682. Return the maximum of `x' and `y'.
  683. `TYPE min (const TYPE& x, const TYPE& y)'
  684. Return the minimum of `x' and `y'.
  685. When a floating point number and a rational number are compared, the
  686. float is first converted to a rational number using the function
  687. `rational'. Since a floating point number actually represents an
  688. interval of real numbers, the result might be surprising. For example,
  689. `(cl_F)(cl_R)"1/3" == (cl_R)"1/3"' returns false because there is no
  690. floating point number whose value is exactly `1/3'.
  691. 
  692. File: cln.info, Node: Rounding functions, Next: Roots, Prev: Comparisons, Up: Functions on numbers
  693. Rounding functions
  694. ==================
  695. When a real number is to be converted to an integer, there is no "best"
  696. rounding. The desired rounding function depends on the application.
  697. The Common Lisp and ISO Lisp standards offer four rounding functions:
  698. `floor(x)'
  699. This is the largest integer <=`x'.
  700. `ceiling(x)'
  701. This is the smallest integer >=`x'.
  702. `truncate(x)'
  703. Among the integers between 0 and `x' (inclusive) the one nearest
  704. to `x'.
  705. `round(x)'
  706. The integer nearest to `x'. If `x' is exactly halfway between two
  707. integers, choose the even one.
  708. These functions have different advantages:
  709. `floor' and `ceiling' are translation invariant: `floor(x+n) = floor(x)
  710. + n' and `ceiling(x+n) = ceiling(x) + n' for every `x' and every
  711. integer `n'.
  712. On the other hand, `truncate' and `round' are symmetric: `truncate(-x)
  713. = -truncate(x)' and `round(-x) = -round(x)', and furthermore `round' is
  714. unbiased: on the "average", it rounds down exactly as often as it
  715. rounds up.
  716. The functions are related like this:
  717. `ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1' for rational
  718. numbers `m/n' (`m', `n' integers, `n'>0), and
  719. `truncate(x) = sign(x) * floor(abs(x))'
  720. Each of the classes `cl_R', `cl_RA', `cl_F', `cl_SF', `cl_FF', `cl_DF',
  721. `cl_LF' defines the following operations:
  722. `cl_I floor1 (const TYPE& x)'
  723. Returns `floor(x)'.
  724. `cl_I ceiling1 (const TYPE& x)'
  725. Returns `ceiling(x)'.
  726. `cl_I truncate1 (const TYPE& x)'
  727. Returns `truncate(x)'.
  728. `cl_I round1 (const TYPE& x)'
  729. Returns `round(x)'.
  730. Each of the classes `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF', `cl_FF',
  731. `cl_DF', `cl_LF' defines the following operations:
  732. `cl_I floor1 (const TYPE& x, const TYPE& y)'
  733. Returns `floor(x/y)'.
  734. `cl_I ceiling1 (const TYPE& x, const TYPE& y)'
  735. Returns `ceiling(x/y)'.
  736. `cl_I truncate1 (const TYPE& x, const TYPE& y)'
  737. Returns `truncate(x/y)'.
  738. `cl_I round1 (const TYPE& x, const TYPE& y)'
  739. Returns `round(x/y)'.
  740. These functions are called `floor1', ... here instead of `floor', ...,
  741. because on some systems, system dependent include files define `floor'
  742. and `ceiling' as macros.
  743. In many cases, one needs both the quotient and the remainder of a
  744. division. It is more efficient to compute both at the same time than
  745. to perform two divisions, one for quotient and the next one for the
  746. remainder. The following functions therefore return a structure
  747. containing both the quotient and the remainder. The suffix `2'
  748. indicates the number of "return values". The remainder is defined as
  749. follows:
  750. * for the computation of `quotient = floor(x)', `remainder = x -
  751. quotient',
  752. * for the computation of `quotient = floor(x,y)', `remainder = x -
  753. quotient*y',
  754. and similarly for the other three operations.
  755. Each of the classes `cl_R', `cl_RA', `cl_F', `cl_SF', `cl_FF', `cl_DF',
  756. `cl_LF' defines the following operations:
  757. `struct TYPE_div_t { cl_I quotient; TYPE remainder; };'
  758. `TYPE_div_t floor2 (const TYPE& x)'
  759. `TYPE_div_t ceiling2 (const TYPE& x)'
  760. `TYPE_div_t truncate2 (const TYPE& x)'
  761. `TYPE_div_t round2 (const TYPE& x)'
  762. Each of the classes `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF', `cl_FF',
  763. `cl_DF', `cl_LF' defines the following operations:
  764. `struct TYPE_div_t { cl_I quotient; TYPE remainder; };'
  765. `TYPE_div_t floor2 (const TYPE& x, const TYPE& y)'
  766. `TYPE_div_t ceiling2 (const TYPE& x, const TYPE& y)'
  767. `TYPE_div_t truncate2 (const TYPE& x, const TYPE& y)'
  768. `TYPE_div_t round2 (const TYPE& x, const TYPE& y)'
  769. Sometimes, one wants the quotient as a floating-point number (of the
  770. same format as the argument, if the argument is a float) instead of as
  771. an integer. The prefix `f' indicates this.
  772. Each of the classes `cl_F', `cl_SF', `cl_FF', `cl_DF', `cl_LF' defines
  773. the following operations:
  774. `TYPE ffloor (const TYPE& x)'
  775. `TYPE fceiling (const TYPE& x)'
  776. `TYPE ftruncate (const TYPE& x)'
  777. `TYPE fround (const TYPE& x)'
  778. and similarly for class `cl_R', but with return type `cl_F'.
  779. The class `cl_R' defines the following operations:
  780. `cl_F ffloor (const TYPE& x, const TYPE& y)'
  781. `cl_F fceiling (const TYPE& x, const TYPE& y)'
  782. `cl_F ftruncate (const TYPE& x, const TYPE& y)'
  783. `cl_F fround (const TYPE& x, const TYPE& y)'
  784. These functions also exist in versions which return both the quotient
  785. and the remainder. The suffix `2' indicates this.
  786. Each of the classes `cl_F', `cl_SF', `cl_FF', `cl_DF', `cl_LF' defines
  787. the following operations:
  788. `struct TYPE_fdiv_t { TYPE quotient; TYPE remainder; };'
  789. `TYPE_fdiv_t ffloor2 (const TYPE& x)'
  790. `TYPE_fdiv_t fceiling2 (const TYPE& x)'
  791. `TYPE_fdiv_t ftruncate2 (const TYPE& x)'
  792. `TYPE_fdiv_t fround2 (const TYPE& x)'
  793. and similarly for class `cl_R', but with quotient type `cl_F'.
  794. The class `cl_R' defines the following operations:
  795. `struct TYPE_fdiv_t { cl_F quotient; cl_R remainder; };'
  796. `TYPE_fdiv_t ffloor2 (const TYPE& x, const TYPE& y)'
  797. `TYPE_fdiv_t fceiling2 (const TYPE& x, const TYPE& y)'
  798. `TYPE_fdiv_t ftruncate2 (const TYPE& x, const TYPE& y)'
  799. `TYPE_fdiv_t fround2 (const TYPE& x, const TYPE& y)'
  800. Other applications need only the remainder of a division. The
  801. remainder of `floor' and `ffloor' is called `mod' (abbreviation of
  802. "modulo"). The remainder `truncate' and `ftruncate' is called `rem'
  803. (abbreviation of "remainder").
  804. * `mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y'
  805. * `rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y'
  806. If `x' and `y' are both >= 0, `mod(x,y) = rem(x,y) >= 0'. In general,
  807. `mod(x,y)' has the sign of `y' or is zero, and `rem(x,y)' has the sign
  808. of `x' or is zero.
  809. The classes `cl_R', `cl_I' define the following operations:
  810. `TYPE mod (const TYPE& x, const TYPE& y)'
  811. `TYPE rem (const TYPE& x, const TYPE& y)'
  812. 
  813. File: cln.info, Node: Roots, Next: Transcendental functions, Prev: Rounding functions, Up: Functions on numbers
  814. Roots
  815. =====
  816. Each of the classes `cl_R', `cl_F', `cl_SF', `cl_FF', `cl_DF', `cl_LF'
  817. defines the following operation:
  818. `TYPE sqrt (const TYPE& x)'
  819. `x' must be >= 0. This function returns the square root of `x',
  820. normalized to be >= 0. If `x' is the square of a rational number,
  821. `sqrt(x)' will be a rational number, else it will return a
  822. floating-point approximation.
  823. The classes `cl_RA', `cl_I' define the following operation:
  824. `cl_boolean sqrtp (const TYPE& x, TYPE* root)'
  825. This tests whether `x' is a perfect square. If so, it returns true
  826. and the exact square root in `*root', else it returns false.
  827. Furthermore, for integers, similarly:
  828. `cl_boolean isqrt (const TYPE& x, TYPE* root)'
  829. `x' should be >= 0. This function sets `*root' to `floor(sqrt(x))'
  830. and returns the same value as `sqrtp': the boolean value
  831. `(expt(*root,2) == x)'.
  832. For `n'th roots, the classes `cl_RA', `cl_I' define the following
  833. operation:
  834. `cl_boolean rootp (const TYPE& x, const cl_I& n, TYPE* root)'
  835. `x' must be >= 0. `n' must be > 0. This tests whether `x' is an
  836. `n'th power of a rational number. If so, it returns true and the
  837. exact root in `*root', else it returns false.
  838. The only square root function which accepts negative numbers is the one
  839. for class `cl_N':
  840. `cl_N sqrt (const cl_N& z)'
  841. Returns the square root of `z', as defined by the formula `sqrt(z)
  842. = exp(log(z)/2)'. Conversion to a floating-point type or to a
  843. complex number are done if necessary. The range of the result is
  844. the right half plane `realpart(sqrt(z)) >= 0' including the
  845. positive imaginary axis and 0, but excluding the negative
  846. imaginary axis. The result is an exact number only if `z' is an
  847. exact number.
  848. 
  849. File: cln.info, Node: Transcendental functions, Next: Functions on integers, Prev: Roots, Up: Functions on numbers
  850. Transcendental functions
  851. ========================
  852. The transcendental functions return an exact result if the argument is
  853. exact and the result is exact as well. Otherwise they must return
  854. inexact numbers even if the argument is exact. For example, `cos(0) =
  855. 1' returns the rational number `1'.
  856. * Menu:
  857. * Exponential and logarithmic functions::
  858. * Trigonometric functions::
  859. * Hyperbolic functions::
  860. * Euler gamma::
  861. * Riemann zeta::
  862. 
  863. File: cln.info, Node: Exponential and logarithmic functions, Next: Trigonometric functions, Prev: Transcendental functions, Up: Transcendental functions
  864. Exponential and logarithmic functions
  865. -------------------------------------
  866. `cl_R exp (const cl_R& x)'
  867. `cl_N exp (const cl_N& x)'
  868. Returns the exponential function of `x'. This is `e^x' where `e'
  869. is the base of the natural logarithms. The range of the result is
  870. the entire complex plane excluding 0.
  871. `cl_R ln (const cl_R& x)'
  872. `x' must be > 0. Returns the (natural) logarithm of x.
  873. `cl_N log (const cl_N& x)'
  874. Returns the (natural) logarithm of x. If `x' is real and positive,
  875. this is `ln(x)'. In general, `log(x) = log(abs(x)) + i*phase(x)'.
  876. The range of the result is the strip in the complex plane `-pi <
  877. imagpart(log(x)) <= pi'.
  878. `cl_R phase (const cl_N& x)'
  879. Returns the angle part of `x' in its polar representation as a
  880. complex number. That is, `phase(x) =
  881. atan(realpart(x),imagpart(x))'. This is also the imaginary part
  882. of `log(x)'. The range of the result is the interval `-pi <
  883. phase(x) <= pi'. The result will be an exact number only if
  884. `zerop(x)' or if `x' is real and positive.
  885. `cl_R log (const cl_R& a, const cl_R& b)'
  886. `a' and `b' must be > 0. Returns the logarithm of `a' with respect
  887. to base `b'. `log(a,b) = ln(a)/ln(b)'. The result can be exact
  888. only if `a = 1' or if `a' and `b' are both rational.
  889. `cl_N log (const cl_N& a, const cl_N& b)'
  890. Returns the logarithm of `a' with respect to base `b'. `log(a,b)
  891. = log(a)/log(b)'.
  892. `cl_N expt (const cl_N& x, const cl_N& y)'
  893. Exponentiation: Returns `x^y = exp(y*log(x))'.
  894. The constant e = exp(1) = 2.71828... is returned by the following
  895. functions:
  896. `cl_F cl_exp1 (cl_float_format_t f)'
  897. Returns e as a float of format `f'.
  898. `cl_F cl_exp1 (const cl_F& y)'
  899. Returns e in the float format of `y'.
  900. `cl_F cl_exp1 (void)'
  901. Returns e as a float of format `cl_default_float_format'.
  902. 
  903. File: cln.info, Node: Trigonometric functions, Next: Hyperbolic functions, Prev: Exponential and logarithmic functions, Up: Transcendental functions
  904. Trigonometric functions
  905. -----------------------
  906. `cl_R sin (const cl_R& x)'
  907. Returns `sin(x)'. The range of the result is the interval `-1 <=
  908. sin(x) <= 1'.
  909. `cl_N sin (const cl_N& z)'
  910. Returns `sin(z)'. The range of the result is the entire complex
  911. plane.
  912. `cl_R cos (const cl_R& x)'
  913. Returns `cos(x)'. The range of the result is the interval `-1 <=
  914. cos(x) <= 1'.
  915. `cl_N cos (const cl_N& x)'
  916. Returns `cos(z)'. The range of the result is the entire complex
  917. plane.
  918. `struct cl_cos_sin_t { cl_R cos; cl_R sin; };'
  919. `cl_cos_sin_t cl_cos_sin (const cl_R& x)'
  920. Returns both `sin(x)' and `cos(x)'. This is more efficient than
  921. computing them separately. The relation `cos^2 + sin^2 = 1' will
  922. hold only approximately.
  923. `cl_R tan (const cl_R& x)'
  924. `cl_N tan (const cl_N& x)'
  925. Returns `tan(x) = sin(x)/cos(x)'.
  926. `cl_N cis (const cl_R& x)'
  927. `cl_N cis (const cl_N& x)'
  928. Returns `exp(i*x)'. The name `cis' means "cos + i sin", because
  929. `e^(i*x) = cos(x) + i*sin(x)'.
  930. `cl_N asin (const cl_N& z)'
  931. Returns `arcsin(z)'. This is defined as `arcsin(z) =
  932. log(iz+sqrt(1-z^2))/i' and satisfies `arcsin(-z) = -arcsin(z)'.
  933. The range of the result is the strip in the complex domain `-pi/2
  934. <= realpart(arcsin(z)) <= pi/2', excluding the numbers with
  935. `realpart = -pi/2' and `imagpart < 0' and the numbers with
  936. `realpart = pi/2' and `imagpart > 0'.
  937. `cl_N acos (const cl_N& z)'
  938. Returns `arccos(z)'. This is defined as `arccos(z) = pi/2 -
  939. arcsin(z) = log(z+i*sqrt(1-z^2))/i' and satisfies `arccos(-z) = pi
  940. - arccos(z)'. The range of the result is the strip in the complex
  941. domain `0 <= realpart(arcsin(z)) <= pi', excluding the numbers
  942. with `realpart = 0' and `imagpart < 0' and the numbers with
  943. `realpart = pi' and `imagpart > 0'.
  944. `cl_R atan (const cl_R& x, const cl_R& y)'
  945. Returns the angle of the polar representation of the complex number
  946. `x+iy'. This is `atan(y/x)' if `x>0'. The range of the result is
  947. the interval `-pi < atan(x,y) <= pi'. The result will be an exact
  948. number only if `x > 0' and `y' is the exact `0'. WARNING: In
  949. Common Lisp, this function is called as `(atan y x)', with
  950. reversed order of arguments.
  951. `cl_R atan (const cl_R& x)'
  952. Returns `arctan(x)'. This is the same as `atan(1,x)'. The range of
  953. the result is the interval `-pi/2 < atan(x) < pi/2'. The result
  954. will be an exact number only if `x' is the exact `0'.
  955. `cl_N atan (const cl_N& z)'
  956. Returns `arctan(z)'. This is defined as `arctan(z) =
  957. (log(1+iz)-log(1-iz)) / 2i' and satisfies `arctan(-z) =
  958. -arctan(z)'. The range of the result is the strip in the complex
  959. domain `-pi/2 <= realpart(arctan(z)) <= pi/2', excluding the
  960. numbers with `realpart = -pi/2' and `imagpart >= 0' and the numbers
  961. with `realpart = pi/2' and `imagpart <= 0'.
  962. The constant pi = 3.14... is returned by the following functions:
  963. `cl_F cl_pi (cl_float_format_t f)'
  964. Returns pi as a float of format `f'.
  965. `cl_F cl_pi (const cl_F& y)'
  966. Returns pi in the float format of `y'.
  967. `cl_F cl_pi (void)'
  968. Returns pi as a float of format `cl_default_float_format'.
  969. 
  970. File: cln.info, Node: Hyperbolic functions, Next: Euler gamma, Prev: Trigonometric functions, Up: Transcendental functions
  971. Hyperbolic functions
  972. --------------------
  973. `cl_R sinh (const cl_R& x)'
  974. Returns `sinh(x)'.
  975. `cl_N sinh (const cl_N& z)'
  976. Returns `sinh(z)'. The range of the result is the entire complex
  977. plane.
  978. `cl_R cosh (const cl_R& x)'
  979. Returns `cosh(x)'. The range of the result is the interval
  980. `cosh(x) >= 1'.
  981. `cl_N cosh (const cl_N& z)'
  982. Returns `cosh(z)'. The range of the result is the entire complex
  983. plane.
  984. `struct cl_cosh_sinh_t { cl_R cosh; cl_R sinh; };'
  985. `cl_cosh_sinh_t cl_cosh_sinh (const cl_R& x)'
  986. Returns both `sinh(x)' and `cosh(x)'. This is more efficient than
  987. computing them separately. The relation `cosh^2 - sinh^2 = 1' will
  988. hold only approximately.
  989. `cl_R tanh (const cl_R& x)'
  990. `cl_N tanh (const cl_N& x)'
  991. Returns `tanh(x) = sinh(x)/cosh(x)'.
  992. `cl_N asinh (const cl_N& z)'
  993. Returns `arsinh(z)'. This is defined as `arsinh(z) =
  994. log(z+sqrt(1+z^2))' and satisfies `arsinh(-z) = -arsinh(z)'. The
  995. range of the result is the strip in the complex domain `-pi/2 <=
  996. imagpart(arsinh(z)) <= pi/2', excluding the numbers with `imagpart
  997. = -pi/2' and `realpart > 0' and the numbers with `imagpart = pi/2'
  998. and `realpart < 0'.
  999. `cl_N acosh (const cl_N& z)'
  1000. Returns `arcosh(z)'. This is defined as `arcosh(z) =
  1001. 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))'. The range of the result is
  1002. the half-strip in the complex domain `-pi < imagpart(arcosh(z)) <=
  1003. pi, realpart(arcosh(z)) >= 0', excluding the numbers with
  1004. `realpart = 0' and `-pi < imagpart < 0'.
  1005. `cl_N atanh (const cl_N& z)'
  1006. Returns `artanh(z)'. This is defined as `artanh(z) =
  1007. (log(1+z)-log(1-z)) / 2' and satisfies `artanh(-z) = -artanh(z)'.
  1008. The range of the result is the strip in the complex domain `-pi/2
  1009. <= imagpart(artanh(z)) <= pi/2', excluding the numbers with
  1010. `imagpart = -pi/2' and `realpart <= 0' and the numbers with
  1011. `imagpart = pi/2' and `realpart >= 0'.
  1012. 
  1013. File: cln.info, Node: Euler gamma, Next: Riemann zeta, Prev: Hyperbolic functions, Up: Transcendental functions
  1014. Euler gamma
  1015. -----------
  1016. Euler's constant C = 0.577... is returned by the following functions:
  1017. `cl_F cl_eulerconst (cl_float_format_t f)'
  1018. Returns Euler's constant as a float of format `f'.
  1019. `cl_F cl_eulerconst (const cl_F& y)'
  1020. Returns Euler's constant in the float format of `y'.
  1021. `cl_F cl_eulerconst (void)'
  1022. Returns Euler's constant as a float of format
  1023. `cl_default_float_format'.
  1024. Catalan's constant G = 0.915... is returned by the following functions:
  1025. `cl_F cl_catalanconst (cl_float_format_t f)'
  1026. Returns Catalan's constant as a float of format `f'.
  1027. `cl_F cl_catalanconst (const cl_F& y)'
  1028. Returns Catalan's constant in the float format of `y'.
  1029. `cl_F cl_catalanconst (void)'
  1030. Returns Catalan's constant as a float of format
  1031. `cl_default_float_format'.
  1032. 
  1033. File: cln.info, Node: Riemann zeta, Prev: Euler gamma, Up: Transcendental functions
  1034. Riemann zeta
  1035. ------------
  1036. Riemann's zeta function at an integral point `s>1' is returned by the
  1037. following functions:
  1038. `cl_F cl_zeta (int s, cl_float_format_t f)'
  1039. Returns Riemann's zeta function at `s' as a float of format `f'.
  1040. `cl_F cl_zeta (int s, const cl_F& y)'
  1041. Returns Riemann's zeta function at `s' in the float format of `y'.
  1042. `cl_F cl_zeta (int s)'
  1043. Returns Riemann's zeta function at `s' as a float of format
  1044. `cl_default_float_format'.
  1045. 
  1046. File: cln.info, Node: Functions on integers, Next: Functions on floating-point numbers, Prev: Transcendental functions, Up: Functions on numbers
  1047. Functions on integers
  1048. =====================
  1049. * Menu:
  1050. * Logical functions::
  1051. * Number theoretic functions::
  1052. * Combinatorial functions::
  1053. 
  1054. File: cln.info, Node: Logical functions, Next: Number theoretic functions, Prev: Functions on integers, Up: Functions on integers
  1055. Logical functions
  1056. -----------------
  1057. Integers, when viewed as in two's complement notation, can be thought as
  1058. infinite bit strings where the bits' values eventually are constant.
  1059. For example,
  1060. 17 = ......00010001
  1061. -6 = ......11111010
  1062. The logical operations view integers as such bit strings and operate on
  1063. each of the bit positions in parallel.
  1064. `cl_I lognot (const cl_I& x)'
  1065. `cl_I operator ~ (const cl_I& x)'
  1066. Logical not, like `~x' in C. This is the same as `-1-x'.
  1067. `cl_I logand (const cl_I& x, const cl_I& y)'
  1068. `cl_I operator & (const cl_I& x, const cl_I& y)'
  1069. Logical and, like `x & y' in C.
  1070. `cl_I logior (const cl_I& x, const cl_I& y)'
  1071. `cl_I operator | (const cl_I& x, const cl_I& y)'
  1072. Logical (inclusive) or, like `x | y' in C.
  1073. `cl_I logxor (const cl_I& x, const cl_I& y)'
  1074. `cl_I operator ^ (const cl_I& x, const cl_I& y)'
  1075. Exclusive or, like `x ^ y' in C.
  1076. `cl_I logeqv (const cl_I& x, const cl_I& y)'
  1077. Bitwise equivalence, like `~(x ^ y)' in C.
  1078. `cl_I lognand (const cl_I& x, const cl_I& y)'
  1079. Bitwise not and, like `~(x & y)' in C.
  1080. `cl_I lognor (const cl_I& x, const cl_I& y)'
  1081. Bitwise not or, like `~(x | y)' in C.
  1082. `cl_I logandc1 (const cl_I& x, const cl_I& y)'
  1083. Logical and, complementing the first argument, like `~x & y' in C.
  1084. `cl_I logandc2 (const cl_I& x, const cl_I& y)'
  1085. Logical and, complementing the second argument, like `x & ~y' in C.
  1086. `cl_I logorc1 (const cl_I& x, const cl_I& y)'
  1087. Logical or, complementing the first argument, like `~x | y' in C.
  1088. `cl_I logorc2 (const cl_I& x, const cl_I& y)'
  1089. Logical or, complementing the second argument, like `x | ~y' in C.
  1090. These operations are all available though the function
  1091. `cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)'
  1092. where `op' must have one of the 16 values (each one stands for a
  1093. function which combines two bits into one bit): `boole_clr',
  1094. `boole_set', `boole_1', `boole_2', `boole_c1', `boole_c2', `boole_and',
  1095. `boole_ior', `boole_xor', `boole_eqv', `boole_nand', `boole_nor',
  1096. `boole_andc1', `boole_andc2', `boole_orc1', `boole_orc2'.
  1097. Other functions that view integers as bit strings:
  1098. `cl_boolean logtest (const cl_I& x, const cl_I& y)'
  1099. Returns true if some bit is set in both `x' and `y', i.e. if
  1100. `logand(x,y) != 0'.
  1101. `cl_boolean logbitp (const cl_I& n, const cl_I& x)'
  1102. Returns true if the `n'th bit (from the right) of `x' is set. Bit
  1103. 0 is the least significant bit.
  1104. `uintL logcount (const cl_I& x)'
  1105. Returns the number of one bits in `x', if `x' >= 0, or the number
  1106. of zero bits in `x', if `x' < 0.
  1107. The following functions operate on intervals of bits in integers. The
  1108. type
  1109. struct cl_byte { uintL size; uintL position; };
  1110. represents the bit interval containing the bits
  1111. `position'...`position+size-1' of an integer. The constructor
  1112. `cl_byte(size,position)' constructs a `cl_byte'.
  1113. `cl_I ldb (const cl_I& n, const cl_byte& b)'
  1114. extracts the bits of `n' described by the bit interval `b' and
  1115. returns them as a nonnegative integer with `b.size' bits.
  1116. `cl_boolean ldb_test (const cl_I& n, const cl_byte& b)'
  1117. Returns true if some bit described by the bit interval `b' is set
  1118. in `n'.
  1119. `cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)'
  1120. Returns `n', with the bits described by the bit interval `b'
  1121. replaced by `newbyte'. Only the lowest `b.size' bits of `newbyte'
  1122. are relevant.
  1123. The functions `ldb' and `dpb' implicitly shift. The following functions
  1124. are their counterparts without shifting:
  1125. `cl_I mask_field (const cl_I& n, const cl_byte& b)'
  1126. returns an integer with the bits described by the bit interval `b'
  1127. copied from the corresponding bits in `n', the other bits zero.
  1128. `cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)'
  1129. returns an integer where the bits described by the bit interval `b'
  1130. come from `newbyte' and the other bits come from `n'.
  1131. The following relations hold:
  1132. `ldb (n, b) = mask_field(n, b) >> b.position',
  1133. `dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n,
  1134. b)',
  1135. `deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^
  1136. mask_field(new_byte,b)'.
  1137. The following operations on integers as bit strings are efficient
  1138. shortcuts for common arithmetic operations:
  1139. `cl_boolean oddp (const cl_I& x)'
  1140. Returns true if the least significant bit of `x' is 1. Equivalent
  1141. to `mod(x,2) != 0'.
  1142. `cl_boolean evenp (const cl_I& x)'
  1143. Returns true if the least significant bit of `x' is 0. Equivalent
  1144. to `mod(x,2) == 0'.
  1145. `cl_I operator << (const cl_I& x, const cl_I& n)'
  1146. Shifts `x' by `n' bits to the left. `n' should be >=0. Equivalent
  1147. to `x * expt(2,n)'.
  1148. `cl_I operator >> (const cl_I& x, const cl_I& n)'
  1149. Shifts `x' by `n' bits to the right. `n' should be >=0. Bits
  1150. shifted out to the right are thrown away. Equivalent to `floor(x
  1151. / expt(2,n))'.
  1152. `cl_I ash (const cl_I& x, const cl_I& y)'
  1153. Shifts `x' by `y' bits to the left (if `y'>=0) or by `-y' bits to
  1154. the right (if `y'<=0). In other words, this returns `floor(x *
  1155. expt(2,y))'.
  1156. `uintL integer_length (const cl_I& x)'
  1157. Returns the number of bits (excluding the sign bit) needed to
  1158. represent `x' in two's complement notation. This is the smallest n
  1159. >= 0 such that -2^n <= x < 2^n. If x > 0, this is the unique n > 0
  1160. such that 2^(n-1) <= x < 2^n.
  1161. `uintL ord2 (const cl_I& x)'
  1162. `x' must be non-zero. This function returns the number of 0 bits
  1163. at the right of `x' in two's complement notation. This is the
  1164. largest n >= 0 such that 2^n divides `x'.
  1165. `uintL power2p (const cl_I& x)'
  1166. `x' must be > 0. This function checks whether `x' is a power of 2.
  1167. If `x' = 2^(n-1), it returns n. Else it returns 0. (See also the
  1168. function `logp'.)
  1169. 
  1170. File: cln.info, Node: Number theoretic functions, Next: Combinatorial functions, Prev: Logical functions, Up: Functions on integers
  1171. Number theoretic functions
  1172. --------------------------
  1173. `uint32 gcd (uint32 a, uint32 b)'
  1174. `cl_I gcd (const cl_I& a, const cl_I& b)'
  1175. This function returns the greatest common divisor of `a' and `b',
  1176. normalized to be >= 0.
  1177. `cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)'
  1178. This function ("extended gcd") returns the greatest common divisor
  1179. `g' of `a' and `b' and at the same time the representation of `g'
  1180. as an integral linear combination of `a' and `b': `u' and `v' with
  1181. `u*a+v*b = g', `g' >= 0. `u' and `v' will be normalized to be of
  1182. smallest possible absolute value, in the following sense: If `a'
  1183. and `b' are non-zero, and `abs(a) != abs(b)', `u' and `v' will
  1184. satisfy the inequalities `abs(u) <= abs(b)/(2*g)', `abs(v) <=
  1185. abs(a)/(2*g)'.
  1186. `cl_I lcm (const cl_I& a, const cl_I& b)'
  1187. This function returns the least common multiple of `a' and `b',
  1188. normalized to be >= 0.
  1189. `cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l)'
  1190. `cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l)'
  1191. `a' must be > 0. `b' must be >0 and != 1. If log(a,b) is rational
  1192. number, this function returns true and sets *l = log(a,b), else it
  1193. returns false.
  1194. 
  1195. File: cln.info, Node: Combinatorial functions, Prev: Number theoretic functions, Up: Functions on integers
  1196. Combinatorial functions
  1197. -----------------------
  1198. `cl_I factorial (uintL n)'
  1199. `n' must be a small integer >= 0. This function returns the
  1200. factorial `n'! = `1*2*...*n'.
  1201. `cl_I doublefactorial (uintL n)'
  1202. `n' must be a small integer >= 0. This function returns the
  1203. doublefactorial `n'!! = `1*3*...*n' or `n'!! = `2*4*...*n',
  1204. respectively.
  1205. `cl_I binomial (uintL n, uintL k)'
  1206. `n' and `k' must be small integers >= 0. This function returns the
  1207. binomial coefficient (`n' choose `k') = `n'! / `k'! `(n-k)'! for
  1208. 0 <= k <= n, 0 else.
  1209. 
  1210. File: cln.info, Node: Functions on floating-point numbers, Next: Conversion functions, Prev: Functions on integers, Up: Functions on numbers
  1211. Functions on floating-point numbers
  1212. ===================================
  1213. Recall that a floating-point number consists of a sign `s', an exponent
  1214. `e' and a mantissa `m'. The value of the number is `(-1)^s * 2^e * m'.
  1215. Each of the classes `cl_F', `cl_SF', `cl_FF', `cl_DF', `cl_LF' defines
  1216. the following operations.
  1217. `TYPE scale_float (const TYPE& x, sintL delta)'
  1218. `TYPE scale_float (const TYPE& x, const cl_I& delta)'
  1219. Returns `x*2^delta'. This is more efficient than an explicit
  1220. multiplication because it copies `x' and modifies the exponent.
  1221. The following functions provide an abstract interface to the underlying
  1222. representation of floating-point numbers.
  1223. `sintL float_exponent (const TYPE& x)'
  1224. Returns the exponent `e' of `x'. For `x = 0.0', this is 0. For
  1225. `x' non-zero, this is the unique integer with `2^(e-1) <= abs(x) <
  1226. 2^e'.
  1227. `sintL float_radix (const TYPE& x)'
  1228. Returns the base of the floating-point representation. This is
  1229. always `2'.
  1230. `TYPE float_sign (const TYPE& x)'
  1231. Returns the sign `s' of `x' as a float. The value is 1 for `x' >=
  1232. 0, -1 for `x' < 0.
  1233. `uintL float_digits (const TYPE& x)'
  1234. Returns the number of mantissa bits in the floating-point
  1235. representation of `x', including the hidden bit. The value only
  1236. depends on the type of `x', not on its value.
  1237. `uintL float_precision (const TYPE& x)'
  1238. Returns the number of significant mantissa bits in the
  1239. floating-point representation of `x'. Since denormalized numbers
  1240. are not supported, this is the same as `float_digits(x)' if `x' is
  1241. non-zero, and 0 if `x' = 0.
  1242. The complete internal representation of a float is encoded in the type
  1243. `cl_decoded_float' (or `cl_decoded_sfloat', `cl_decoded_ffloat',
  1244. `cl_decoded_dfloat', `cl_decoded_lfloat', respectively), defined by
  1245. struct cl_decoded_TYPEfloat {
  1246. TYPE mantissa; cl_I exponent; TYPE sign;
  1247. };
  1248. and returned by the function
  1249. `cl_decoded_TYPEfloat decode_float (const TYPE& x)'
  1250. For `x' non-zero, this returns `(-1)^s', `e', `m' with `x = (-1)^s
  1251. * 2^e * m' and `0.5 <= m < 1.0'. For `x' = 0, it returns
  1252. `(-1)^s'=1, `e'=0, `m'=0. `e' is the same as returned by the
  1253. function `float_exponent'.
  1254. A complete decoding in terms of integers is provided as type
  1255. struct cl_idecoded_float {
  1256. cl_I mantissa; cl_I exponent; cl_I sign;
  1257. };
  1258. by the following function:
  1259. `cl_idecoded_float integer_decode_float (const TYPE& x)'
  1260. For `x' non-zero, this returns `(-1)^s', `e', `m' with `x = (-1)^s
  1261. * 2^e * m' and `m' an integer with `float_digits(x)' bits. For `x'
  1262. = 0, it returns `(-1)^s'=1, `e'=0, `m'=0. WARNING: The exponent
  1263. `e' is not the same as the one returned by the functions
  1264. `decode_float' and `float_exponent'.
  1265. Some other function, implemented only for class `cl_F':
  1266. `cl_F float_sign (const cl_F& x, const cl_F& y)'
  1267. This returns a floating point number whose precision and absolute
  1268. value is that of `y' and whose sign is that of `x'. If `x' is
  1269. zero, it is treated as positive. Same for `y'.
  1270. 
  1271. File: cln.info, Node: Conversion functions, Next: Random number generators, Prev: Functions on floating-point numbers, Up: Functions on numbers
  1272. Conversion functions
  1273. ====================
  1274. * Menu:
  1275. * Conversion to floating-point numbers::
  1276. * Conversion to rational numbers::
  1277. 
  1278. File: cln.info, Node: Conversion to floating-point numbers, Next: Conversion to rational numbers, Prev: Conversion functions, Up: Conversion functions
  1279. Conversion to floating-point numbers
  1280. ------------------------------------
  1281. The type `cl_float_format_t' describes a floating-point format.
  1282. `cl_float_format_t cl_float_format (uintL n)'
  1283. Returns the smallest float format which guarantees at least `n'
  1284. decimal digits in the mantissa (after the decimal point).
  1285. `cl_float_format_t cl_float_format (const cl_F& x)'
  1286. Returns the floating point format of `x'.
  1287. `cl_float_format_t cl_default_float_format'
  1288. Global variable: the default float format used when converting
  1289. rational numbers to floats.
  1290. To convert a real number to a float, each of the types `cl_R', `cl_F',
  1291. `cl_I', `cl_RA', `int', `unsigned int', `float', `double' defines the
  1292. following operations:
  1293. `cl_F cl_float (const TYPE&x, cl_float_format_t f)'
  1294. Returns `x' as a float of format `f'.
  1295. `cl_F cl_float (const TYPE&x, const cl_F& y)'
  1296. Returns `x' in the float format of `y'.
  1297. `cl_F cl_float (const TYPE&x)'
  1298. Returns `x' as a float of format `cl_default_float_format' if it
  1299. is an exact number, or `x' itself if it is already a float.
  1300. Of course, converting a number to a float can lose precision.
  1301. Every floating-point format has some characteristic numbers:
  1302. `cl_F most_positive_float (cl_float_format_t f)'
  1303. Returns the largest (most positive) floating point number in float
  1304. format `f'.
  1305. `cl_F most_negative_float (cl_float_format_t f)'
  1306. Returns the smallest (most negative) floating point number in
  1307. float format `f'.
  1308. `cl_F least_positive_float (cl_float_format_t f)'
  1309. Returns the least positive floating point number (i.e. > 0 but
  1310. closest to 0) in float format `f'.
  1311. `cl_F least_negative_float (cl_float_format_t f)'
  1312. Returns the least negative floating point number (i.e. < 0 but
  1313. closest to 0) in float format `f'.
  1314. `cl_F float_epsilon (cl_float_format_t f)'
  1315. Returns the smallest floating point number e > 0 such that `1+e !=
  1316. 1'.
  1317. `cl_F float_negative_epsilon (cl_float_format_t f)'
  1318. Returns the smallest floating point number e > 0 such that `1-e !=
  1319. 1'.
  1320. 
  1321. File: cln.info, Node: Conversion to rational numbers, Prev: Conversion to floating-point numbers, Up: Conversion functions
  1322. Conversion to rational numbers
  1323. ------------------------------
  1324. Each of the classes `cl_R', `cl_RA', `cl_F' defines the following
  1325. operation:
  1326. `cl_RA rational (const TYPE& x)'
  1327. Returns the value of `x' as an exact number. If `x' is already an
  1328. exact number, this is `x'. If `x' is a floating-point number, the
  1329. value is a rational number whose denominator is a power of 2.
  1330. In order to convert back, say, `(cl_F)(cl_R)"1/3"' to `1/3', there is
  1331. the function
  1332. `cl_RA rationalize (const cl_R& x)'
  1333. If `x' is a floating-point number, it actually represents an
  1334. interval of real numbers, and this function returns the rational
  1335. number with smallest denominator (and smallest numerator, in
  1336. magnitude) which lies in this interval. If `x' is already an
  1337. exact number, this function returns `x'.
  1338. If `x' is any float, one has
  1339. `cl_float(rational(x),x) = x'
  1340. `cl_float(rationalize(x),x) = x'
  1341. 
  1342. File: cln.info, Node: Random number generators, Next: Obfuscating operators, Prev: Conversion functions, Up: Functions on numbers
  1343. Random number generators
  1344. ========================
  1345. A random generator is a machine which produces (pseudo-)random numbers.
  1346. The include file `<cl_random.h>' defines a class `cl_random_state'
  1347. which contains the state of a random generator. If you make a copy of
  1348. the random number generator, the original one and the copy will produce
  1349. the same sequence of random numbers.
  1350. The following functions return (pseudo-)random numbers in different
  1351. formats. Calling one of these modifies the state of the random number
  1352. generator in a complicated but deterministic way.
  1353. The global variable
  1354. cl_random_state cl_default_random_state
  1355. contains a default random number generator. It is used when the
  1356. functions below are called without `cl_random_state' argument.
  1357. `uint32 random32 (cl_random_state& randomstate)'
  1358. `uint32 random32 ()'
  1359. Returns a random unsigned 32-bit number. All bits are equally
  1360. random.
  1361. `cl_I random_I (cl_random_state& randomstate, const cl_I& n)'
  1362. `cl_I random_I (const cl_I& n)'
  1363. `n' must be an integer > 0. This function returns a random integer
  1364. `x' in the range `0 <= x < n'.
  1365. `cl_F random_F (cl_random_state& randomstate, const cl_F& n)'
  1366. `cl_F random_F (const cl_F& n)'
  1367. `n' must be a float > 0. This function returns a random
  1368. floating-point number of the same format as `n' in the range `0 <=
  1369. x < n'.
  1370. `cl_R random_R (cl_random_state& randomstate, const cl_R& n)'
  1371. `cl_R random_R (const cl_R& n)'
  1372. Behaves like `random_I' if `n' is an integer and like `random_F'
  1373. if `n' is a float.
  1374. 
  1375. File: cln.info, Node: Obfuscating operators, Prev: Random number generators, Up: Functions on numbers
  1376. Obfuscating operators
  1377. =====================
  1378. The modifying C/C++ operators `+=', `-=', `*=', `/=', `&=', `|=', `^=',
  1379. `<<=', `>>=' are not available by default because their use tends to
  1380. make programs unreadable. It is trivial to get away without them.
  1381. However, if you feel that you absolutely need these operators to get
  1382. happy, then add
  1383. #define WANT_OBFUSCATING_OPERATORS
  1384. to the beginning of your source files, before the inclusion of any CLN
  1385. include files. This flag will enable the following operators:
  1386. For the classes `cl_N', `cl_R', `cl_RA', `cl_F', `cl_SF', `cl_FF',
  1387. `cl_DF', `cl_LF':
  1388. `TYPE& operator += (TYPE&, const TYPE&)'
  1389. `TYPE& operator -= (TYPE&, const TYPE&)'
  1390. `TYPE& operator *= (TYPE&, const TYPE&)'
  1391. `TYPE& operator /= (TYPE&, const TYPE&)'
  1392. For the class `cl_I':
  1393. `TYPE& operator += (TYPE&, const TYPE&)'
  1394. `TYPE& operator -= (TYPE&, const TYPE&)'
  1395. `TYPE& operator *= (TYPE&, const TYPE&)'
  1396. `TYPE& operator &= (TYPE&, const TYPE&)'
  1397. `TYPE& operator |= (TYPE&, const TYPE&)'
  1398. `TYPE& operator ^= (TYPE&, const TYPE&)'
  1399. `TYPE& operator <<= (TYPE&, const TYPE&)'
  1400. `TYPE& operator >>= (TYPE&, const TYPE&)'
  1401. For the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  1402. `cl_FF', `cl_DF', `cl_LF':
  1403. `TYPE& operator ++ (TYPE& x)'
  1404. The prefix operator `++x'.
  1405. `void operator ++ (TYPE& x, int)'
  1406. The postfix operator `x++'.
  1407. `TYPE& operator -- (TYPE& x)'
  1408. The prefix operator `--x'.
  1409. `void operator -- (TYPE& x, int)'
  1410. The postfix operator `x--'.
  1411. Note that by using these obfuscating operators, you wouldn't gain
  1412. efficiency: In CLN `x += y;' is exactly the same as `x = x+y;', not
  1413. more efficient.
  1414. 
  1415. File: cln.info, Node: Input/Output, Next: Rings, Prev: Functions on numbers, Up: Top
  1416. Input/Output
  1417. ************
  1418. * Menu:
  1419. * Internal and printed representation::
  1420. * Input functions::
  1421. * Output functions::
  1422. 
  1423. File: cln.info, Node: Internal and printed representation, Next: Input functions, Prev: Input/Output, Up: Input/Output
  1424. Internal and printed representation
  1425. ===================================
  1426. All computations deal with the internal representations of the numbers.
  1427. Every number has an external representation as a sequence of ASCII
  1428. characters. Several external representations may denote the same
  1429. number, for example, "20.0" and "20.000".
  1430. Converting an internal to an external representation is called
  1431. "printing", converting an external to an internal representation is
  1432. called "reading". In CLN, is it always true that conversion of an
  1433. internal to an external representation and then back to an internal
  1434. representation will yield the same internal representation.
  1435. Symbolically: `read(print(x)) == x'. This is called "print-read
  1436. consistency".
  1437. Different types of numbers have different external representations (case
  1438. is insignificant):
  1439. Integers
  1440. External representation: SIGN{DIGIT}+. The reader also accepts the
  1441. Common Lisp syntaxes SIGN{DIGIT}+`.' with a trailing dot for
  1442. decimal integers and the `#NR', `#b', `#o', `#x' prefixes.
  1443. Rational numbers
  1444. External representation: SIGN{DIGIT}+`/'{DIGIT}+. The `#NR',
  1445. `#b', `#o', `#x' prefixes are allowed here as well.
  1446. Floating-point numbers
  1447. External representation: SIGN{DIGIT}*EXPONENT or
  1448. SIGN{DIGIT}*`.'{DIGIT}*EXPONENT or SIGN{DIGIT}*`.'{DIGIT}+. A
  1449. precision specifier of the form _PREC may be appended. There must
  1450. be at least one digit in the non-exponent part. The exponent has
  1451. the syntax EXPMARKER EXPSIGN {DIGIT}+. The exponent marker is
  1452. `s' for short-floats,
  1453. `f' for single-floats,
  1454. `d' for double-floats,
  1455. `L' for long-floats,
  1456. or `e', which denotes a default float format. The precision
  1457. specifying suffix has the syntax _PREC where PREC denotes the
  1458. number of valid mantissa digits (in decimal, excluding leading
  1459. zeroes), cf. also function `cl_float_format'.
  1460. Complex numbers
  1461. External representation:
  1462. In algebraic notation: `REALPART+IMAGPARTi'. Of course, if
  1463. IMAGPART is negative, its printed representation begins with
  1464. a `-', and the `+' between REALPART and IMAGPART may be
  1465. omitted. Note that this notation cannot be used when the
  1466. IMAGPART is rational and the rational number's base is >18,
  1467. because the `i' is then read as a digit.
  1468. In Common Lisp notation: `#C(REALPART IMAGPART)'.
  1469. 
  1470. File: cln.info, Node: Input functions, Next: Output functions, Prev: Internal and printed representation, Up: Input/Output
  1471. Input functions
  1472. ===============
  1473. Including `<cl_io.h>' defines a type `cl_istream', which is the type of
  1474. the first argument to all input functions. Unless you build and use CLN
  1475. with the macro CL_IO_STDIO being defined, `cl_istream' is the same as
  1476. `istream&'.
  1477. The variable
  1478. `cl_istream cl_stdin' contains the standard input stream.
  1479. These are the simple input functions:
  1480. `int freadchar (cl_istream stream)'
  1481. Reads a character from `stream'. Returns `cl_EOF' (not a `char'!)
  1482. if the end of stream was encountered or an error occurred.
  1483. `int funreadchar (cl_istream stream, int c)'
  1484. Puts back `c' onto `stream'. `c' must be the result of the last
  1485. `freadchar' operation on `stream'.
  1486. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  1487. `cl_FF', `cl_DF', `cl_LF' defines, in `<cl_TYPE_io.h>', the following
  1488. input function:
  1489. `cl_istream operator>> (cl_istream stream, TYPE& result)'
  1490. Reads a number from `stream' and stores it in the `result'.
  1491. The most flexible input functions, defined in `<cl_TYPE_io.h>', are the
  1492. following:
  1493. `cl_N read_complex (cl_istream stream, const cl_read_flags& flags)'
  1494. `cl_R read_real (cl_istream stream, const cl_read_flags& flags)'
  1495. `cl_F read_float (cl_istream stream, const cl_read_flags& flags)'
  1496. `cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)'
  1497. `cl_I read_integer (cl_istream stream, const cl_read_flags& flags)'
  1498. Reads a number from `stream'. The `flags' are parameters which
  1499. affect the input syntax. Whitespace before the number is silently
  1500. skipped.
  1501. `cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)'
  1502. `cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)'
  1503. `cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)'
  1504. `cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)'
  1505. `cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)'
  1506. Reads a number from a string in memory. The `flags' are parameters
  1507. which affect the input syntax. The string starts at `string' and
  1508. ends at `string_limit' (exclusive limit). `string_limit' may also
  1509. be `NULL', denoting the entire string, i.e. equivalent to
  1510. `string_limit = string + strlen(string)'. If `end_of_parse' is
  1511. `NULL', the string in memory must contain exactly one number and
  1512. nothing more, else a fatal error will be signalled. If
  1513. `end_of_parse' is not `NULL', `*end_of_parse' will be assigned a
  1514. pointer past the last parsed character (i.e. `string_limit' if
  1515. nothing came after the number). Whitespace is not allowed.
  1516. The structure `cl_read_flags' contains the following fields:
  1517. `cl_read_syntax_t syntax'
  1518. The possible results of the read operation. Possible values are
  1519. `syntax_number', `syntax_real', `syntax_rational',
  1520. `syntax_integer', `syntax_float', `syntax_sfloat',
  1521. `syntax_ffloat', `syntax_dfloat', `syntax_lfloat'.
  1522. `cl_read_lsyntax_t lsyntax'
  1523. Specifies the language-dependent syntax variant for the read
  1524. operation. Possible values are
  1525. `lsyntax_standard'
  1526. accept standard algebraic notation only, no complex numbers,
  1527. `lsyntax_algebraic'
  1528. accept the algebraic notation `X+Yi' for complex numbers,
  1529. `lsyntax_commonlisp'
  1530. accept the `#b', `#o', `#x' syntaxes for binary, octal,
  1531. hexadecimal numbers, `#BASER' for rational numbers in a given
  1532. base, `#c(REALPART IMAGPART)' for complex numbers,
  1533. `lsyntax_all'
  1534. accept all of these extensions.
  1535. `unsigned int rational_base'
  1536. The base in which rational numbers are read.
  1537. `cl_float_format_t float_flags.default_float_format'
  1538. The float format used when reading floats with exponent marker `e'.
  1539. `cl_float_format_t float_flags.default_lfloat_format'
  1540. The float format used when reading floats with exponent marker `l'.
  1541. `cl_boolean float_flags.mantissa_dependent_float_format'
  1542. When this flag is true, floats specified with more digits than
  1543. corresponding to the exponent marker they contain, but without
  1544. _NNN suffix, will get a precision corresponding to their number of
  1545. significant digits.
  1546. 
  1547. File: cln.info, Node: Output functions, Prev: Input functions, Up: Input/Output
  1548. Output functions
  1549. ================
  1550. Including `<cl_io.h>' defines a type `cl_ostream', which is the type of
  1551. the first argument to all output functions. Unless you build and use
  1552. CLN with the macro CL_IO_STDIO being defined, `cl_ostream' is the same
  1553. as `ostream&'.
  1554. The variable
  1555. `cl_ostream cl_stdout' contains the standard output stream.
  1556. The variable
  1557. `cl_ostream cl_stderr' contains the standard error output stream.
  1558. These are the simple output functions:
  1559. `void fprintchar (cl_ostream stream, char c)'
  1560. Prints the character `x' literally on the `stream'.
  1561. `void fprint (cl_ostream stream, const char * string)'
  1562. Prints the `string' literally on the `stream'.
  1563. `void fprintdecimal (cl_ostream stream, int x)'
  1564. `void fprintdecimal (cl_ostream stream, const cl_I& x)'
  1565. Prints the integer `x' in decimal on the `stream'.
  1566. `void fprintbinary (cl_ostream stream, const cl_I& x)'
  1567. Prints the integer `x' in binary (base 2, without prefix) on the
  1568. `stream'.
  1569. `void fprintoctal (cl_ostream stream, const cl_I& x)'
  1570. Prints the integer `x' in octal (base 8, without prefix) on the
  1571. `stream'.
  1572. `void fprinthexadecimal (cl_ostream stream, const cl_I& x)'
  1573. Prints the integer `x' in hexadecimal (base 16, without prefix) on
  1574. the `stream'.
  1575. Each of the classes `cl_N', `cl_R', `cl_RA', `cl_I', `cl_F', `cl_SF',
  1576. `cl_FF', `cl_DF', `cl_LF' defines, in `<cl_TYPE_io.h>', the following
  1577. output functions:
  1578. `void fprint (cl_ostream stream, const TYPE& x)'
  1579. `cl_ostream operator<< (cl_ostream stream, const TYPE& x)'
  1580. Prints the number `x' on the `stream'. The output may depend on
  1581. the global printer settings in the variable
  1582. `cl_default_print_flags'. The `ostream' flags and settings
  1583. (flags, width and locale) are ignored.
  1584. The most flexible output function, defined in `<cl_TYPE_io.h>', are the
  1585. following:
  1586. void print_complex (cl_ostream stream, const cl_print_flags& flags,
  1587. const cl_N& z);
  1588. void print_real (cl_ostream stream, const cl_print_flags& flags,
  1589. const cl_R& z);
  1590. void print_float (cl_ostream stream, const cl_print_flags& flags,
  1591. const cl_F& z);
  1592. void print_rational (cl_ostream stream, const cl_print_flags& flags,
  1593. const cl_RA& z);
  1594. void print_integer (cl_ostream stream, const cl_print_flags& flags,
  1595. const cl_I& z);
  1596. Prints the number `x' on the `stream'. The `flags' are parameters which
  1597. affect the output.
  1598. The structure type `cl_print_flags' contains the following fields:
  1599. `unsigned int rational_base'
  1600. The base in which rational numbers are printed. Default is `10'.
  1601. `cl_boolean rational_readably'
  1602. If this flag is true, rational numbers are printed with radix
  1603. specifiers in Common Lisp syntax (`#NR' or `#b' or `#o' or `#x'
  1604. prefixes, trailing dot). Default is false.
  1605. `cl_boolean float_readably'
  1606. If this flag is true, type specific exponent markers have
  1607. precedence over 'E'. Default is false.
  1608. `cl_float_format_t default_float_format'
  1609. Floating point numbers of this format will be printed using the
  1610. 'E' exponent marker. Default is `cl_float_format_ffloat'.
  1611. `cl_boolean complex_readably'
  1612. If this flag is true, complex numbers will be printed using the
  1613. Common Lisp syntax `#C(REALPART IMAGPART)'. Default is false.
  1614. `cl_string univpoly_varname'
  1615. Univariate polynomials with no explicit indeterminate name will be
  1616. printed using this variable name. Default is `"x"'.
  1617. The global variable `cl_default_print_flags' contains the default
  1618. values, used by the function `fprint',
  1619. 
  1620. File: cln.info, Node: Rings, Next: Modular integers, Prev: Input/Output, Up: Top
  1621. Rings
  1622. *****
  1623. CLN has a class of abstract rings.
  1624. Ring
  1625. cl_ring
  1626. <cl_ring.h>
  1627. Rings can be compared for equality:
  1628. `bool operator== (const cl_ring&, const cl_ring&)'
  1629. `bool operator!= (const cl_ring&, const cl_ring&)'
  1630. These compare two rings for equality.
  1631. Given a ring `R', the following members can be used.
  1632. `void R->fprint (cl_ostream stream, const cl_ring_element& x)'
  1633. `cl_boolean R->equal (const cl_ring_element& x, const cl_ring_element& y)'
  1634. `cl_ring_element R->zero ()'
  1635. `cl_boolean R->zerop (const cl_ring_element& x)'
  1636. `cl_ring_element R->plus (const cl_ring_element& x, const cl_ring_element& y)'
  1637. `cl_ring_element R->minus (const cl_ring_element& x, const cl_ring_element& y)'
  1638. `cl_ring_element R->uminus (const cl_ring_element& x)'
  1639. `cl_ring_element R->one ()'
  1640. `cl_ring_element R->canonhom (const cl_I& x)'
  1641. `cl_ring_element R->mul (const cl_ring_element& x, const cl_ring_element& y)'
  1642. `cl_ring_element R->square (const cl_ring_element& x)'
  1643. `cl_ring_element R->expt_pos (const cl_ring_element& x, const cl_I& y)'
  1644. The following rings are built-in.
  1645. `cl_null_ring cl_0_ring'
  1646. The null ring, containing only zero.
  1647. `cl_complex_ring cl_C_ring'
  1648. The ring of complex numbers. This corresponds to the type `cl_N'.
  1649. `cl_real_ring cl_R_ring'
  1650. The ring of real numbers. This corresponds to the type `cl_R'.
  1651. `cl_rational_ring cl_RA_ring'
  1652. The ring of rational numbers. This corresponds to the type `cl_RA'.
  1653. `cl_integer_ring cl_I_ring'
  1654. The ring of integers. This corresponds to the type `cl_I'.
  1655. Type tests can be performed for any of `cl_C_ring', `cl_R_ring',
  1656. `cl_RA_ring', `cl_I_ring':
  1657. `cl_boolean instanceof (const cl_number& x, const cl_number_ring& R)'
  1658. Tests whether the given number is an element of the number ring R.
  1659. 
  1660. File: cln.info, Node: Modular integers, Next: Symbolic data types, Prev: Rings, Up: Top
  1661. Modular integers
  1662. ****************
  1663. * Menu:
  1664. * Modular integer rings::
  1665. * Functions on modular integers::
  1666. 
  1667. File: cln.info, Node: Modular integer rings, Next: Functions on modular integers, Prev: Modular integers, Up: Modular integers
  1668. Modular integer rings
  1669. =====================
  1670. CLN implements modular integers, i.e. integers modulo a fixed integer N.
  1671. The modulus is explicitly part of every modular integer. CLN doesn't
  1672. allow you to (accidentally) mix elements of different modular rings,
  1673. e.g. `(3 mod 4) + (2 mod 5)' will result in a runtime error. (Ideally
  1674. one would imagine a generic data type `cl_MI(N)', but C++ doesn't have
  1675. generic types. So one has to live with runtime checks.)
  1676. The class of modular integer rings is
  1677. Ring
  1678. cl_ring
  1679. <cl_ring.h>
  1680. |
  1681. |
  1682. Modular integer ring
  1683. cl_modint_ring
  1684. <cl_modinteger.h>
  1685. and the class of all modular integers (elements of modular integer
  1686. rings) is
  1687. Modular integer
  1688. cl_MI
  1689. <cl_modinteger.h>
  1690. Modular integer rings are constructed using the function
  1691. `cl_modint_ring cl_find_modint_ring (const cl_I& N)'
  1692. This function returns the modular ring `Z/NZ'. It takes care of
  1693. finding out about special cases of `N', like powers of two and odd
  1694. numbers for which Montgomery multiplication will be a win, and
  1695. precomputes any necessary auxiliary data for computing modulo `N'.
  1696. There is a cache table of rings, indexed by `N' (or, more
  1697. precisely, by `abs(N)'). This ensures that the precomputation
  1698. costs are reduced to a minimum.
  1699. Modular integer rings can be compared for equality:
  1700. `bool operator== (const cl_modint_ring&, const cl_modint_ring&)'
  1701. `bool operator!= (const cl_modint_ring&, const cl_modint_ring&)'
  1702. These compare two modular integer rings for equality. Two
  1703. different calls to `cl_find_modint_ring' with the same argument
  1704. necessarily return the same ring because it is memoized in the
  1705. cache table.
  1706. 
  1707. File: cln.info, Node: Functions on modular integers, Prev: Modular integer rings, Up: Modular integers
  1708. Functions on modular integers
  1709. =============================
  1710. Given a modular integer ring `R', the following members can be used.
  1711. `cl_I R->modulus'
  1712. This is the ring's modulus, normalized to be nonnegative: `abs(N)'.
  1713. `cl_MI R->zero()'
  1714. This returns `0 mod N'.
  1715. `cl_MI R->one()'
  1716. This returns `1 mod N'.
  1717. `cl_MI R->canonhom (const cl_I& x)'
  1718. This returns `x mod N'.
  1719. `cl_I R->retract (const cl_MI& x)'
  1720. This is a partial inverse function to `R->canonhom'. It returns the
  1721. standard representative (`>=0', `<N') of `x'.
  1722. `cl_MI R->random(cl_random_state& randomstate)'
  1723. `cl_MI R->random()'
  1724. This returns a random integer modulo `N'.
  1725. The following operations are defined on modular integers.
  1726. `cl_modint_ring x.ring ()'
  1727. Returns the ring to which the modular integer `x' belongs.
  1728. `cl_MI operator+ (const cl_MI&, const cl_MI&)'
  1729. Returns the sum of two modular integers. One of the arguments may
  1730. also be a plain integer.
  1731. `cl_MI operator- (const cl_MI&, const cl_MI&)'
  1732. Returns the difference of two modular integers. One of the
  1733. arguments may also be a plain integer.
  1734. `cl_MI operator- (const cl_MI&)'
  1735. Returns the negative of a modular integer.
  1736. `cl_MI operator* (const cl_MI&, const cl_MI&)'
  1737. Returns the product of two modular integers. One of the arguments
  1738. may also be a plain integer.
  1739. `cl_MI square (const cl_MI&)'
  1740. Returns the square of a modular integer.
  1741. `cl_MI recip (const cl_MI& x)'
  1742. Returns the reciprocal `x^-1' of a modular integer `x'. `x' must
  1743. be coprime to the modulus, otherwise an error message is issued.
  1744. `cl_MI div (const cl_MI& x, const cl_MI& y)'
  1745. Returns the quotient `x*y^-1' of two modular integers `x', `y'.
  1746. `y' must be coprime to the modulus, otherwise an error message is
  1747. issued.
  1748. `cl_MI expt_pos (const cl_MI& x, const cl_I& y)'
  1749. `y' must be > 0. Returns `x^y'.
  1750. `cl_MI expt (const cl_MI& x, const cl_I& y)'
  1751. Returns `x^y'. If `y' is negative, `x' must be coprime to the
  1752. modulus, else an error message is issued.
  1753. `cl_MI operator<< (const cl_MI& x, const cl_I& y)'
  1754. Returns `x*2^y'.
  1755. `cl_MI operator>> (const cl_MI& x, const cl_I& y)'
  1756. Returns `x*2^-y'. When `y' is positive, the modulus must be odd,
  1757. or an error message is issued.
  1758. `bool operator== (const cl_MI&, const cl_MI&)'
  1759. `bool operator!= (const cl_MI&, const cl_MI&)'
  1760. Compares two modular integers, belonging to the same modular
  1761. integer ring, for equality.
  1762. `cl_boolean zerop (const cl_MI& x)'
  1763. Returns true if `x' is `0 mod N'.
  1764. The following output functions are defined (see also the chapter on
  1765. input/output).
  1766. `void fprint (cl_ostream stream, const cl_MI& x)'
  1767. `cl_ostream operator<< (cl_ostream stream, const cl_MI& x)'
  1768. Prints the modular integer `x' on the `stream'. The output may
  1769. depend on the global printer settings in the variable
  1770. `cl_default_print_flags'.
  1771. 
  1772. File: cln.info, Node: Symbolic data types, Next: Univariate polynomials, Prev: Modular integers, Up: Top
  1773. Symbolic data types
  1774. *******************
  1775. CLN implements two symbolic (non-numeric) data types: strings and
  1776. symbols.
  1777. * Menu:
  1778. * Strings::
  1779. * Symbols::
  1780. 
  1781. File: cln.info, Node: Strings, Next: Symbols, Prev: Symbolic data types, Up: Symbolic data types
  1782. Strings
  1783. =======
  1784. The class
  1785. String
  1786. cl_string
  1787. <cl_string.h>
  1788. implements immutable strings.
  1789. Strings are constructed through the following constructors:
  1790. `cl_string (const char * s)'
  1791. Returns an immutable copy of the (zero-terminated) C string `s'.
  1792. `cl_string (const char * ptr, unsigned long len)'
  1793. Returns an immutable copy of the `len' characters at `ptr[0]',
  1794. ..., `ptr[len-1]'. NUL characters are allowed.
  1795. The following functions are available on strings:
  1796. `operator ='
  1797. Assignment from `cl_string' and `const char *'.
  1798. `s.length()'
  1799. `strlen(s)'
  1800. Returns the length of the string `s'.
  1801. `s[i]'
  1802. Returns the `i'th character of the string `s'. `i' must be in the
  1803. range `0 <= i < s.length()'.
  1804. `bool equal (const cl_string& s1, const cl_string& s2)'
  1805. Compares two strings for equality. One of the arguments may also
  1806. be a plain `const char *'.
  1807. 
  1808. File: cln.info, Node: Symbols, Prev: Strings, Up: Symbolic data types
  1809. Symbols
  1810. =======
  1811. Symbols are uniquified strings: all symbols with the same name are
  1812. shared. This means that comparison of two symbols is fast (effectively
  1813. just a pointer comparison), whereas comparison of two strings must in
  1814. the worst case walk both strings until their end. Symbols are used,
  1815. for example, as tags for properties, as names of variables in
  1816. polynomial rings, etc.
  1817. Symbols are constructed through the following constructor:
  1818. `cl_symbol (const cl_string& s)'
  1819. Looks up or creates a new symbol with a given name.
  1820. The following operations are available on symbols:
  1821. `cl_string (const cl_symbol& sym)'
  1822. Conversion to `cl_string': Returns the string which names the
  1823. symbol `sym'.
  1824. `bool equal (const cl_symbol& sym1, const cl_symbol& sym2)'
  1825. Compares two symbols for equality. This is very fast.
  1826. 
  1827. File: cln.info, Node: Univariate polynomials, Next: Internals, Prev: Symbolic data types, Up: Top
  1828. Univariate polynomials
  1829. **********************
  1830. * Menu:
  1831. * Univariate polynomial rings::
  1832. * Functions on univariate polynomials::
  1833. * Special polynomials::
  1834. 
  1835. File: cln.info, Node: Univariate polynomial rings, Next: Functions on univariate polynomials, Prev: Univariate polynomials, Up: Univariate polynomials
  1836. Univariate polynomial rings
  1837. ===========================
  1838. CLN implements univariate polynomials (polynomials in one variable)
  1839. over an arbitrary ring. The indeterminate variable may be either
  1840. unnamed (and will be printed according to
  1841. `cl_default_print_flags.univpoly_varname', which defaults to `x') or
  1842. carry a given name. The base ring and the indeterminate are explicitly
  1843. part of every polynomial. CLN doesn't allow you to (accidentally) mix
  1844. elements of different polynomial rings, e.g. `(a^2+1) * (b^3-1)' will
  1845. result in a runtime error. (Ideally this should return a multivariate
  1846. polynomial, but they are not yet implemented in CLN.)
  1847. The classes of univariate polynomial rings are
  1848. Ring
  1849. cl_ring
  1850. <cl_ring.h>
  1851. |
  1852. |
  1853. Univariate polynomial ring
  1854. cl_univpoly_ring
  1855. <cl_univpoly.h>
  1856. |
  1857. +----------------+-------------------+
  1858. | | |
  1859. Complex polynomial ring | Modular integer polynomial ring
  1860. cl_univpoly_complex_ring | cl_univpoly_modint_ring
  1861. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  1862. |
  1863. +----------------+
  1864. | |
  1865. Real polynomial ring |
  1866. cl_univpoly_real_ring |
  1867. <cl_univpoly_real.h> |
  1868. |
  1869. +----------------+
  1870. | |
  1871. Rational polynomial ring |
  1872. cl_univpoly_rational_ring |
  1873. <cl_univpoly_rational.h> |
  1874. |
  1875. +----------------+
  1876. |
  1877. Integer polynomial ring
  1878. cl_univpoly_integer_ring
  1879. <cl_univpoly_integer.h>
  1880. and the corresponding classes of univariate polynomials are
  1881. Univariate polynomial
  1882. cl_UP
  1883. <cl_univpoly.h>
  1884. |
  1885. +----------------+-------------------+
  1886. | | |
  1887. Complex polynomial | Modular integer polynomial
  1888. cl_UP_N | cl_UP_MI
  1889. <cl_univpoly_complex.h> | <cl_univpoly_modint.h>
  1890. |
  1891. +----------------+
  1892. | |
  1893. Real polynomial |
  1894. cl_UP_R |
  1895. <cl_univpoly_real.h> |
  1896. |
  1897. +----------------+
  1898. | |
  1899. Rational polynomial |
  1900. cl_UP_RA |
  1901. <cl_univpoly_rational.h> |
  1902. |
  1903. +----------------+
  1904. |
  1905. Integer polynomial
  1906. cl_UP_I
  1907. <cl_univpoly_integer.h>
  1908. Univariate polynomial rings are constructed using the functions
  1909. `cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R)'
  1910. `cl_univpoly_ring cl_find_univpoly_ring (const cl_ring& R, const cl_symbol& varname)'
  1911. This function returns the polynomial ring `R[X]', unnamed or named.
  1912. `R' may be an arbitrary ring. This function takes care of finding
  1913. out about special cases of `R', such as the rings of complex
  1914. numbers, real numbers, rational numbers, integers, or modular
  1915. integer rings. There is a cache table of rings, indexed by `R'
  1916. and `varname'. This ensures that two calls of this function with
  1917. the same arguments will return the same polynomial ring.
  1918. `cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R)'
  1919. `cl_univpoly_complex_ring cl_find_univpoly_ring (const cl_complex_ring& R, const cl_symbol& varname)'
  1920. `cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R)'
  1921. `cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& R, const cl_symbol& varname)'
  1922. `cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R)'
  1923. `cl_univpoly_rational_ring cl_find_univpoly_ring (const cl_rational_ring& R, const cl_symbol& varname)'
  1924. `cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R)'
  1925. `cl_univpoly_integer_ring cl_find_univpoly_ring (const cl_integer_ring& R, const cl_symbol& varname)'
  1926. `cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R)'
  1927. `cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& R, const cl_symbol& varname)'
  1928. These functions are equivalent to the general
  1929. `cl_find_univpoly_ring', only the return type is more specific,
  1930. according to the base ring's type.
  1931. 
  1932. File: cln.info, Node: Functions on univariate polynomials, Next: Special polynomials, Prev: Univariate polynomial rings, Up: Univariate polynomials
  1933. Functions on univariate polynomials
  1934. ===================================
  1935. Given a univariate polynomial ring `R', the following members can be
  1936. used.
  1937. `cl_ring R->basering()'
  1938. This returns the base ring, as passed to `cl_find_univpoly_ring'.
  1939. `cl_UP R->zero()'
  1940. This returns `0 in R', a polynomial of degree -1.
  1941. `cl_UP R->one()'
  1942. This returns `1 in R', a polynomial of degree <= 0.
  1943. `cl_UP R->canonhom (const cl_I& x)'
  1944. This returns `x in R', a polynomial of degree <= 0.
  1945. `cl_UP R->monomial (const cl_ring_element& x, uintL e)'
  1946. This returns a sparse polynomial: `x * X^e', where `X' is the
  1947. indeterminate.
  1948. `cl_UP R->create (sintL degree)'
  1949. Creates a new polynomial with a given degree. The zero polynomial
  1950. has degree `-1'. After creating the polynomial, you should put in
  1951. the coefficients, using the `set_coeff' member function, and then
  1952. call the `finalize' member function.
  1953. The following are the only destructive operations on univariate
  1954. polynomials.
  1955. `void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)'
  1956. This changes the coefficient of `X^index' in `x' to be `y'. After
  1957. changing a polynomial and before applying any "normal" operation
  1958. on it, you should call its `finalize' member function.
  1959. `void finalize (cl_UP& x)'
  1960. This function marks the endpoint of destructive modifications of a
  1961. polynomial. It normalizes the internal representation so that
  1962. subsequent computations have less overhead. Doing normal
  1963. computations on unnormalized polynomials may produce wrong results
  1964. or crash the program.
  1965. The following operations are defined on univariate polynomials.
  1966. `cl_univpoly_ring x.ring ()'
  1967. Returns the ring to which the univariate polynomial `x' belongs.
  1968. `cl_UP operator+ (const cl_UP&, const cl_UP&)'
  1969. Returns the sum of two univariate polynomials.
  1970. `cl_UP operator- (const cl_UP&, const cl_UP&)'
  1971. Returns the difference of two univariate polynomials.
  1972. `cl_UP operator- (const cl_UP&)'
  1973. Returns the negative of a univariate polynomial.
  1974. `cl_UP operator* (const cl_UP&, const cl_UP&)'
  1975. Returns the product of two univariate polynomials. One of the
  1976. arguments may also be a plain integer or an element of the base
  1977. ring.
  1978. `cl_UP square (const cl_UP&)'
  1979. Returns the square of a univariate polynomial.
  1980. `cl_UP expt_pos (const cl_UP& x, const cl_I& y)'
  1981. `y' must be > 0. Returns `x^y'.
  1982. `bool operator== (const cl_UP&, const cl_UP&)'
  1983. `bool operator!= (const cl_UP&, const cl_UP&)'
  1984. Compares two univariate polynomials, belonging to the same
  1985. univariate polynomial ring, for equality.
  1986. `cl_boolean zerop (const cl_UP& x)'
  1987. Returns true if `x' is `0 in R'.
  1988. `sintL degree (const cl_UP& x)'
  1989. Returns the degree of the polynomial. The zero polynomial has
  1990. degree `-1'.
  1991. `cl_ring_element coeff (const cl_UP& x, uintL index)'
  1992. Returns the coefficient of `X^index' in the polynomial `x'.
  1993. `cl_ring_element x (const cl_ring_element& y)'
  1994. Evaluation: If `x' is a polynomial and `y' belongs to the base
  1995. ring, then `x(y)' returns the value of the substitution of `y' into
  1996. `x'.
  1997. `cl_UP deriv (const cl_UP& x)'
  1998. Returns the derivative of the polynomial `x' with respect to the
  1999. indeterminate `X'.
  2000. The following output functions are defined (see also the chapter on
  2001. input/output).
  2002. `void fprint (cl_ostream stream, const cl_UP& x)'
  2003. `cl_ostream operator<< (cl_ostream stream, const cl_UP& x)'
  2004. Prints the univariate polynomial `x' on the `stream'. The output
  2005. may depend on the global printer settings in the variable
  2006. `cl_default_print_flags'.
  2007. 
  2008. File: cln.info, Node: Special polynomials, Prev: Functions on univariate polynomials, Up: Univariate polynomials
  2009. Special polynomials
  2010. ===================
  2011. The following functions return special polynomials.
  2012. `cl_UP_I cl_tschebychev (sintL n)'
  2013. Returns the n-th Tchebychev polynomial (n >= 0).
  2014. `cl_UP_I cl_hermite (sintL n)'
  2015. Returns the n-th Hermite polynomial (n >= 0).
  2016. `cl_UP_RA cl_legendre (sintL n)'
  2017. Returns the n-th Legendre polynomial (n >= 0).
  2018. `cl_UP_I cl_laguerre (sintL n)'
  2019. Returns the n-th Laguerre polynomial (n >= 0).
  2020. Information how to derive the differential equation satisfied by each
  2021. of these polynomials from their definition can be found in the
  2022. `doc/polynomial/' directory.
  2023. 
  2024. File: cln.info, Node: Internals, Next: Using the library, Prev: Univariate polynomials, Up: Top
  2025. Internals
  2026. *********
  2027. * Menu:
  2028. * Why C++ ?::
  2029. * Memory efficiency::
  2030. * Speed efficiency::
  2031. * Garbage collection::
  2032. 
  2033. File: cln.info, Node: Why C++ ?, Next: Memory efficiency, Prev: Internals, Up: Internals
  2034. Why C++ ?
  2035. =========
  2036. Using C++ as an implementation language provides
  2037. * Efficiency: It compiles to machine code.
  2038. * Portability: It runs on all platforms supporting a C++ compiler.
  2039. Because of the availability of GNU C++, this includes all
  2040. currently used 32-bit and 64-bit platforms, independently of the
  2041. quality of the vendor's C++ compiler.
  2042. * Type safety: The C++ compilers knows about the number types and
  2043. complains if, for example, you try to assign a float to an integer
  2044. variable. However, a drawback is that C++ doesn't know about
  2045. generic types, hence a restriction like that `operation+ (const
  2046. cl_MI&, const cl_MI&)' requires that both arguments belong to the
  2047. same modular ring cannot be expressed as a compile-time
  2048. information.
  2049. * Algebraic syntax: The elementary operations `+', `-', `*', `=',
  2050. `==', ... can be used in infix notation, which is more convenient
  2051. than Lisp notation `(+ x y)' or C notation `add(x,y,&z)'.
  2052. With these language features, there is no need for two separate
  2053. languages, one for the implementation of the library and one in which
  2054. the library's users can program. This means that a prototype
  2055. implementation of an algorithm can be integrated into the library
  2056. immediately after it has been tested and debugged. No need to rewrite
  2057. it in a low-level language after having prototyped in a high-level
  2058. language.
  2059. 
  2060. File: cln.info, Node: Memory efficiency, Next: Speed efficiency, Prev: Why C++ ?, Up: Internals
  2061. Memory efficiency
  2062. =================
  2063. In order to save memory allocations, CLN implements:
  2064. * Object sharing: An operation like `x+0' returns `x' without copying
  2065. it.
  2066. * Garbage collection: A reference counting mechanism makes sure that
  2067. any number object's storage is freed immediately when the last
  2068. reference to the object is gone.
  2069. * Small integers are represented as immediate values instead of
  2070. pointers to heap allocated storage. This means that integers `>
  2071. -2^29', `< 2^29' don't consume heap memory, unless they were
  2072. explicitly allocated on the heap.
  2073. 
  2074. File: cln.info, Node: Speed efficiency, Next: Garbage collection, Prev: Memory efficiency, Up: Internals
  2075. Speed efficiency
  2076. ================
  2077. Speed efficiency is obtained by the combination of the following tricks
  2078. and algorithms:
  2079. * Small integers, being represented as immediate values, don't
  2080. require memory access, just a couple of instructions for each
  2081. elementary operation.
  2082. * The kernel of CLN has been written in assembly language for some
  2083. CPUs (`i386', `m68k', `sparc', `mips', `arm').
  2084. * On all CPUs, CLN uses the superefficient low-level routines from
  2085. GNU GMP version 2.
  2086. * For large numbers, CLN uses, instead of the standard `O(N^2)'
  2087. algorithm, the Karatsuba multiplication, which is an `O(N^1.6)'
  2088. algorithm.
  2089. * For very large numbers (more than 12000 decimal digits), CLN uses
  2090. Sch�nhage-Strassen multiplication, which is an asymptotically
  2091. optimal multiplication algorithm.
  2092. * These fast multiplication algorithms also give improvements in the
  2093. speed of division and radix conversion.
  2094. 
  2095. File: cln.info, Node: Garbage collection, Prev: Speed efficiency, Up: Internals
  2096. Garbage collection
  2097. ==================
  2098. All the number classes are reference count classes: They only contain a
  2099. pointer to an object in the heap. Upon construction, assignment and
  2100. destruction of number objects, only the objects' reference count are
  2101. manipulated.
  2102. Memory occupied by number objects are automatically reclaimed as soon as
  2103. their reference count drops to zero.
  2104. For number rings, another strategy is implemented: There is a cache of,
  2105. for example, the modular integer rings. A modular integer ring is
  2106. destroyed only if its reference count dropped to zero and the cache is
  2107. about to be resized. The effect of this strategy is that recently used
  2108. rings remain cached, whereas undue memory consumption through cached
  2109. rings is avoided.
  2110. 
  2111. File: cln.info, Node: Using the library, Next: Customizing, Prev: Internals, Up: Top
  2112. Using the library
  2113. *****************
  2114. For the following discussion, we will assume that you have installed
  2115. the CLN source in `$CLN_DIR' and built it in `$CLN_TARGETDIR'. For
  2116. example, for me it's `CLN_DIR="$HOME/cln"' and
  2117. `CLN_TARGETDIR="$HOME/cln/linuxelf"'. You might define these as
  2118. environment variables, or directly substitute the appropriate values.
  2119. * Menu:
  2120. * Compiler options::
  2121. * Include files::
  2122. * An Example::
  2123. * Debugging support::
  2124. 
  2125. File: cln.info, Node: Compiler options, Next: Include files, Prev: Using the library, Up: Using the library
  2126. Compiler options
  2127. ================
  2128. Until you have installed CLN in a public place, the following options
  2129. are needed:
  2130. When you compile CLN application code, add the flags
  2131. -I$CLN_DIR/include -I$CLN_TARGETDIR/include
  2132. to the C++ compiler's command line (`make' variable CFLAGS or CXXFLAGS).
  2133. When you link CLN application code to form an executable, add the flags
  2134. $CLN_TARGETDIR/src/libcln.a
  2135. to the C/C++ compiler's command line (`make' variable LIBS).
  2136. If you did a `make install', the include files are installed in a
  2137. public directory (normally `/usr/local/include'), hence you don't need
  2138. special flags for compiling. The library has been installed to a public
  2139. directory as well (normally `/usr/local/lib'), hence when linking a CLN
  2140. application it is sufficient to give the flag `-lcln'.
  2141. 
  2142. File: cln.info, Node: Include files, Next: An Example, Prev: Compiler options, Up: Using the library
  2143. Include files
  2144. =============
  2145. Here is a summary of the include files and their contents.
  2146. `<cl_object.h>'
  2147. General definitions, reference counting, garbage collection.
  2148. `<cl_number.h>'
  2149. The class cl_number.
  2150. `<cl_complex.h>'
  2151. Functions for class cl_N, the complex numbers.
  2152. `<cl_real.h>'
  2153. Functions for class cl_R, the real numbers.
  2154. `<cl_float.h>'
  2155. Functions for class cl_F, the floats.
  2156. `<cl_sfloat.h>'
  2157. Functions for class cl_SF, the short-floats.
  2158. `<cl_ffloat.h>'
  2159. Functions for class cl_FF, the single-floats.
  2160. `<cl_dfloat.h>'
  2161. Functions for class cl_DF, the double-floats.
  2162. `<cl_lfloat.h>'
  2163. Functions for class cl_LF, the long-floats.
  2164. `<cl_rational.h>'
  2165. Functions for class cl_RA, the rational numbers.
  2166. `<cl_integer.h>'
  2167. Functions for class cl_I, the integers.
  2168. `<cl_io.h>'
  2169. Input/Output.
  2170. `<cl_complex_io.h>'
  2171. Input/Output for class cl_N, the complex numbers.
  2172. `<cl_real_io.h>'
  2173. Input/Output for class cl_R, the real numbers.
  2174. `<cl_float_io.h>'
  2175. Input/Output for class cl_F, the floats.
  2176. `<cl_sfloat_io.h>'
  2177. Input/Output for class cl_SF, the short-floats.
  2178. `<cl_ffloat_io.h>'
  2179. Input/Output for class cl_FF, the single-floats.
  2180. `<cl_dfloat_io.h>'
  2181. Input/Output for class cl_DF, the double-floats.
  2182. `<cl_lfloat_io.h>'
  2183. Input/Output for class cl_LF, the long-floats.
  2184. `<cl_rational_io.h>'
  2185. Input/Output for class cl_RA, the rational numbers.
  2186. `<cl_integer_io.h>'
  2187. Input/Output for class cl_I, the integers.
  2188. `<cl_input.h>'
  2189. Flags for customizing input operations.
  2190. `<cl_output.h>'
  2191. Flags for customizing output operations.
  2192. `<cl_malloc.h>'
  2193. `cl_malloc_hook', `cl_free_hook'.
  2194. `<cl_abort.h>'
  2195. `cl_abort'.
  2196. `<cl_condition.h>'
  2197. Conditions/exceptions.
  2198. `<cl_string.h>'
  2199. Strings.
  2200. `<cl_symbol.h>'
  2201. Symbols.
  2202. `<cl_proplist.h>'
  2203. Property lists.
  2204. `<cl_ring.h>'
  2205. General rings.
  2206. `<cl_null_ring.h>'
  2207. The null ring.
  2208. `<cl_complex_ring.h>'
  2209. The ring of complex numbers.
  2210. `<cl_real_ring.h>'
  2211. The ring of real numbers.
  2212. `<cl_rational_ring.h>'
  2213. The ring of rational numbers.
  2214. `<cl_integer_ring.h>'
  2215. The ring of integers.
  2216. `<cl_numtheory.h>'
  2217. Number threory functions.
  2218. `<cl_modinteger.h>'
  2219. Modular integers.
  2220. `<cl_V.h>'
  2221. Vectors.
  2222. `<cl_GV.h>'
  2223. General vectors.
  2224. `<cl_GV_number.h>'
  2225. General vectors over cl_number.
  2226. `<cl_GV_complex.h>'
  2227. General vectors over cl_N.
  2228. `<cl_GV_real.h>'
  2229. General vectors over cl_R.
  2230. `<cl_GV_rational.h>'
  2231. General vectors over cl_RA.
  2232. `<cl_GV_integer.h>'
  2233. General vectors over cl_I.
  2234. `<cl_GV_modinteger.h>'
  2235. General vectors of modular integers.
  2236. `<cl_SV.h>'
  2237. Simple vectors.
  2238. `<cl_SV_number.h>'
  2239. Simple vectors over cl_number.
  2240. `<cl_SV_complex.h>'
  2241. Simple vectors over cl_N.
  2242. `<cl_SV_real.h>'
  2243. Simple vectors over cl_R.
  2244. `<cl_SV_rational.h>'
  2245. Simple vectors over cl_RA.
  2246. `<cl_SV_integer.h>'
  2247. Simple vectors over cl_I.
  2248. `<cl_SV_ringelt.h>'
  2249. Simple vectors of general ring elements.
  2250. `<cl_univpoly.h>'
  2251. Univariate polynomials.
  2252. `<cl_univpoly_integer.h>'
  2253. Univariate polynomials over the integers.
  2254. `<cl_univpoly_rational.h>'
  2255. Univariate polynomials over the rational numbers.
  2256. `<cl_univpoly_real.h>'
  2257. Univariate polynomials over the real numbers.
  2258. `<cl_univpoly_complex.h>'
  2259. Univariate polynomials over the complex numbers.
  2260. `<cl_univpoly_modint.h>'
  2261. Univariate polynomials over modular integer rings.
  2262. `<cl_timing.h>'
  2263. Timing facilities.
  2264. `<cln.h>'
  2265. Includes all of the above.
  2266. 
  2267. File: cln.info, Node: An Example, Next: Debugging support, Prev: Include files, Up: Using the library
  2268. An Example
  2269. ==========
  2270. A function which computes the nth Fibonacci number can be written as
  2271. follows.
  2272. #include <cl_integer.h>
  2273. #include <cl_real.h>
  2274. // Returns F_n, computed as the nearest integer to
  2275. // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
  2276. const cl_I fibonacci (int n)
  2277. {
  2278. // Need a precision of ((1+sqrt(5))/2)^-n.
  2279. cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
  2280. cl_R sqrt5 = sqrt(cl_float(5,prec));
  2281. cl_R phi = (1+sqrt5)/2;
  2282. return round1( expt(phi,n)/sqrt5 );
  2283. }
  2284. Let's explain what is going on in detail.
  2285. The include file `<cl_integer.h>' is necessary because the type `cl_I'
  2286. is used in the function, and the include file `<cl_real.h>' is needed
  2287. for the type `cl_R' and the floating point number functions. The order
  2288. of the include files does not matter.
  2289. Then comes the function declaration. The argument is an `int', the
  2290. result an integer. The return type is defined as `const cl_I', not
  2291. simply `cl_I', because that allows the compiler to detect typos like
  2292. `fibonacci(n) = 100'. It would be possible to declare the return type
  2293. as `const cl_R' (real number) or even `const cl_N' (complex number). We
  2294. use the most specialized possible return type because functions which
  2295. call `fibonacci' will be able to profit from the compiler's type
  2296. analysis: Adding two integers is slightly more efficient than adding the
  2297. same objects declared as complex numbers, because it needs less type
  2298. dispatch. Also, when linking to CLN as a non-shared library, this
  2299. minimizes the size of the resulting executable program.
  2300. The result will be computed as expt(phi,n)/sqrt(5), rounded to the
  2301. nearest integer. In order to get a correct result, the absolute error
  2302. should be less than 1/2, i.e. the relative error should be less than
  2303. sqrt(5)/(2*expt(phi,n)). To this end, the first line computes a
  2304. floating point precision for sqrt(5) and phi.
  2305. Then sqrt(5) is computed by first converting the integer 5 to a
  2306. floating point number and than taking the square root. The converse,
  2307. first taking the square root of 5, and then converting to the desired
  2308. precision, would not work in CLN: The square root would be computed to
  2309. a default precision (normally single-float precision), and the
  2310. following conversion could not help about the lacking accuracy. This is
  2311. because CLN is not a symbolic computer algebra system and does not
  2312. represent sqrt(5) in a non-numeric way.
  2313. The type `cl_R' for sqrt5 and, in the following line, phi is the only
  2314. possible choice. You cannot write `cl_F' because the C++ compiler can
  2315. only infer that `cl_float(5,prec)' is a real number. You cannot write
  2316. `cl_N' because a `round1' does not exist for general complex numbers.
  2317. When the function returns, all the local variables in the function are
  2318. automatically reclaimed (garbage collected). Only the result survives
  2319. and gets passed to the caller.
  2320. 
  2321. File: cln.info, Node: Debugging support, Prev: An Example, Up: Using the library
  2322. Debugging support
  2323. =================
  2324. When debugging a CLN application with GNU `gdb', two facilities are
  2325. available from the library:
  2326. * The library does type checks, range checks, consistency checks at
  2327. many places. When one of these fails, the function `cl_abort()' is
  2328. called. Its default implementation is to perform an `exit(1)', so
  2329. you won't have a core dump. But for debugging, it is best to set a
  2330. breakpoint at this function:
  2331. (gdb) break cl_abort
  2332. When this breakpoint is hit, look at the stack's backtrace:
  2333. (gdb) where
  2334. * The debugger's normal `print' command doesn't know about CLN's
  2335. types and therefore prints mostly useless hexadecimal addresses.
  2336. CLN offers a function `cl_print', callable from the debugger, for
  2337. printing number objects. In order to get this function, you have
  2338. to define the macro `CL_DEBUG' and then include all the header
  2339. files for which you want `cl_print' debugging support. For example:
  2340. #define CL_DEBUG
  2341. #include <cl_string.h>
  2342. Now, if you have in your program a variable `cl_string s', and
  2343. inspect it under `gdb', the output may look like this:
  2344. (gdb) print s
  2345. $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
  2346. word = 134568800}}, }
  2347. (gdb) call cl_print(s)
  2348. (cl_string) ""
  2349. $8 = 134568800
  2350. Note that the output of `cl_print' goes to the program's error
  2351. output, not to gdb's standard output.
  2352. Note, however, that the above facility does not work with all CLN
  2353. types, only with number objects and similar. Therefore CLN offers
  2354. a member function `debug_print()' on all CLN types. The same macro
  2355. `CL_DEBUG' is needed for this member function to be implemented.
  2356. Under `gdb', you call it like this:
  2357. (gdb) print s
  2358. $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
  2359. word = 134568800}}, }
  2360. (gdb) call s.debug_print()
  2361. (cl_string) ""
  2362. (gdb) define cprint
  2363. >call ($1).debug_print()
  2364. >end
  2365. (gdb) cprint s
  2366. (cl_string) ""
  2367. Unfortunately, this feature does not seem to work under all
  2368. circumstances.
  2369. 
  2370. File: cln.info, Node: Customizing, Next: Index, Prev: Using the library, Up: Top
  2371. Customizing
  2372. ***********
  2373. * Menu:
  2374. * Error handling::
  2375. * Floating-point underflow::
  2376. * Customizing I/O::
  2377. * Customizing the memory allocator::
  2378. 
  2379. File: cln.info, Node: Error handling, Next: Floating-point underflow, Prev: Customizing, Up: Customizing
  2380. Error handling
  2381. ==============
  2382. When a fatal error occurs, an error message is output to the standard
  2383. error output stream, and the function `cl_abort' is called. The default
  2384. version of this function (provided in the library) terminates the
  2385. application. To catch such a fatal error, you need to define the
  2386. function `cl_abort' yourself, with the prototype
  2387. #include <cl_abort.h>
  2388. void cl_abort (void);
  2389. This function must not return control to its caller.
  2390. 
  2391. File: cln.info, Node: Floating-point underflow, Next: Customizing I/O, Prev: Error handling, Up: Customizing
  2392. Floating-point underflow
  2393. ========================
  2394. Floating point underflow denotes the situation when a floating-point
  2395. number is to be created which is so close to `0' that its exponent is
  2396. too low to be represented internally. By default, this causes a fatal
  2397. error. If you set the global variable
  2398. cl_boolean cl_inhibit_floating_point_underflow
  2399. to `cl_true', the error will be inhibited, and a floating-point zero
  2400. will be generated instead. The default value of
  2401. `cl_inhibit_floating_point_underflow' is `cl_false'.
  2402. 
  2403. File: cln.info, Node: Customizing I/O, Next: Customizing the memory allocator, Prev: Floating-point underflow, Up: Customizing
  2404. Customizing I/O
  2405. ===============
  2406. The output of the function `fprint' may be customized by changing the
  2407. value of the global variable `cl_default_print_flags'.
  2408. 
  2409. File: cln.info, Node: Customizing the memory allocator, Prev: Customizing I/O, Up: Customizing
  2410. Customizing the memory allocator
  2411. ================================
  2412. Every memory allocation of CLN is done through the function pointer
  2413. `cl_malloc_hook'. Freeing of this memory is done through the function
  2414. pointer `cl_free_hook'. The default versions of these functions,
  2415. provided in the library, call `malloc' and `free' and check the
  2416. `malloc' result against `NULL'. If you want to provide another memory
  2417. allocator, you need to define the variables `cl_malloc_hook' and
  2418. `cl_free_hook' yourself, like this:
  2419. #include <cl_malloc.h>
  2420. void* (*cl_malloc_hook) (size_t size) = ...;
  2421. void (*cl_free_hook) (void* ptr) = ...;
  2422. The `cl_malloc_hook' function must not return a `NULL' pointer.
  2423. It is not possible to change the memory allocator at runtime, because
  2424. it is already called at program startup by the constructors of some
  2425. global variables.
  2426. 
  2427. File: cln.info, Node: Index, Prev: Customizing, Up: Top
  2428. Index
  2429. *****
  2430. * Menu:
  2431. 
  2432. Tag Table:
  2433. Node: Top957
  2434. Node: Introduction3124
  2435. Node: Installation5626
  2436. Node: Prerequisites5920
  2437. Node: C++ compiler6118
  2438. Node: Make utility6833
  2439. Node: Sed utility7019
  2440. Node: Building the library7339
  2441. Node: Installing the library10650
  2442. Node: Cleaning up11373
  2443. Node: Ordinary number types11698
  2444. Node: Exact numbers14045
  2445. Node: Floating-point numbers15210
  2446. Node: Complex numbers18789
  2447. Node: Conversions19286
  2448. Node: Functions on numbers22752
  2449. Node: Constructing numbers23455
  2450. Node: Constructing integers23827
  2451. Node: Constructing rational numbers24117
  2452. Node: Constructing floating-point numbers24592
  2453. Node: Constructing complex numbers25712
  2454. Node: Elementary functions26076
  2455. Node: Elementary rational functions28543
  2456. Node: Elementary complex functions29115
  2457. Node: Comparisons29943
  2458. Node: Rounding functions31842
  2459. Node: Roots37619
  2460. Node: Transcendental functions39500
  2461. Node: Exponential and logarithmic functions40056
  2462. Node: Trigonometric functions42073
  2463. Node: Hyperbolic functions45416
  2464. Node: Euler gamma47489
  2465. Node: Riemann zeta48405
  2466. Node: Functions on integers48961
  2467. Node: Logical functions49249
  2468. Node: Number theoretic functions55202
  2469. Node: Combinatorial functions56569
  2470. Node: Functions on floating-point numbers57247
  2471. Node: Conversion functions60478
  2472. Node: Conversion to floating-point numbers60758
  2473. Node: Conversion to rational numbers62981
  2474. Node: Random number generators64035
  2475. Node: Obfuscating operators65709
  2476. Node: Input/Output67439
  2477. Node: Internal and printed representation67649
  2478. Node: Input functions70191
  2479. Node: Output functions74742
  2480. Node: Rings78478
  2481. Node: Modular integers80402
  2482. Node: Modular integer rings80602
  2483. Node: Functions on modular integers82692
  2484. Node: Symbolic data types85702
  2485. Node: Strings85965
  2486. Node: Symbols87030
  2487. Node: Univariate polynomials87932
  2488. Node: Univariate polynomial rings88190
  2489. Node: Functions on univariate polynomials93144
  2490. Node: Special polynomials96925
  2491. Node: Internals97645
  2492. Node: Why C++ ?97859
  2493. Node: Memory efficiency99360
  2494. Node: Speed efficiency100058
  2495. Node: Garbage collection101122
  2496. Node: Using the library101949
  2497. Node: Compiler options102483
  2498. Node: Include files103401
  2499. Node: An Example107042
  2500. Node: Debugging support110067
  2501. Node: Customizing112417
  2502. Node: Error handling112645
  2503. Node: Floating-point underflow113219
  2504. Node: Customizing I/O113858
  2505. Node: Customizing the memory allocator114151
  2506. Node: Index115108
  2507. 
  2508. End Tag Table