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