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