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