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