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