1 \input texinfo @c -*-texinfo-*-
4 @settitle CLN, a Class Library for Numbers
5 @c @setchapternewpage off
6 @c I hate putting "@noindent" in front of every paragraph.
7 @c For `info' and TeX only.
11 @dircategory Mathematics
13 * CLN: (cln). Class Library for Numbers (C++).
18 @c Don't need the other types of indices.
33 This manual documents @sc{cln}, a Class Library for Numbers.
35 Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
36 Richard B. Kreckel, @code{<kreckel@@ginac.de>}.
38 Copyright (C) Bruno Haible 1988 - 2024.
39 Copyright (C) Richard B. Kreckel 2000 - 2024.
40 Copyright (C) Alexei Sheplyakov 2008 - 2010.
42 Permission is granted to make and distribute verbatim copies of
43 this manual provided the copyright notice and this permission notice
44 are preserved on all copies.
47 Permission is granted to process this file through TeX and print the
48 results, provided the printed document carries copying permission
49 notice identical to this one except for the removal of this paragraph
50 (this paragraph not being relevant to the printed manual).
53 Permission is granted to copy and distribute modified versions of this
54 manual under the conditions for verbatim copying, provided that the entire
55 resulting derived work is distributed under the terms of a permission
56 notice identical to this one.
58 Permission is granted to copy and distribute translations of this manual
59 into another language, under the above conditions for modified versions,
60 except that this permission notice may be stated in a translation approved
66 @c prevent ugly black rectangles on overfull hbox lines:
69 @title CLN, a Class Library for Numbers
71 @author @uref{http://www.ginac.de/CLN}
73 @vskip 0pt plus 1filll
74 Copyright @copyright{} Bruno Haible 1988 - 2024.
76 Copyright @copyright{} Richard B. Kreckel 2000 - 2024.
78 Copyright @copyright{} Alexei Sheplyakov 2008 - 2010.
81 Published by Bruno Haible, @code{<haible@@clisp.cons.org>} and
82 Richard B. Kreckel, @code{<kreckel@@ginac.de>}.
84 Permission is granted to make and distribute verbatim copies of
85 this manual provided the copyright notice and this permission notice
86 are preserved on all copies.
88 Permission is granted to copy and distribute modified versions of this
89 manual under the conditions for verbatim copying, provided that the entire
90 resulting derived work is distributed under the terms of a permission
91 notice identical to this one.
93 Permission is granted to copy and distribute translations of this manual
94 into another language, under the above conditions for modified versions,
95 except that this permission notice may be stated in a translation approved
110 * Ordinary number types::
111 * Functions on numbers::
115 * Symbolic data types::
116 * Univariate polynomials::
118 * Using the library::
123 --- The Detailed Node Listing ---
128 * Building the library::
129 * Installing the library::
140 * Using the GNU MP Library::
142 Ordinary number types
145 * Floating-point numbers::
151 * Constructing numbers::
152 * Elementary functions::
153 * Elementary rational functions::
154 * Elementary complex functions::
156 * Rounding functions::
158 * Transcendental functions::
159 * Functions on integers::
160 * Functions on floating-point numbers::
161 * Conversion functions::
162 * Random number generators::
163 * Modifying operators::
167 * Constructing integers::
168 * Constructing rational numbers::
169 * Constructing floating-point numbers::
170 * Constructing complex numbers::
172 Transcendental functions
174 * Exponential and logarithmic functions::
175 * Trigonometric functions::
176 * Hyperbolic functions::
180 Functions on integers
182 * Logical functions::
183 * Number theoretic functions::
184 * Combinatorial functions::
188 * Conversion to floating-point numbers::
189 * Conversion to rational numbers::
193 * Internal and printed representation::
199 * Modular integer rings::
200 * Functions on modular integers::
207 Univariate polynomials
209 * Univariate polynomial rings::
210 * Functions on univariate polynomials::
211 * Special polynomials::
216 * Memory efficiency::
218 * Garbage collection::
225 * Debugging support::
226 * Reporting Problems::
231 * Floating-point underflow::
233 * Customizing the memory allocator::
239 @chapter Introduction
242 CLN is a library for computations with all kinds of numbers.
243 It has a rich set of number classes:
247 Integers (with unlimited precision),
253 Floating-point numbers:
263 Long float (with unlimited precision),
270 Modular integers (integers modulo a fixed integer),
273 Univariate polynomials.
277 The subtypes of the complex numbers among these are exactly the
278 types of numbers known to the Common Lisp language. Therefore
279 @code{CLN} can be used for Common Lisp implementations, giving
280 @samp{CLN} another meaning: it becomes an abbreviation of
281 ``Common Lisp Numbers''.
284 The CLN package implements
288 Elementary functions (@code{+}, @code{-}, @code{*}, @code{/}, @code{sqrt},
289 comparisons, @dots{}),
292 Logical functions (logical @code{and}, @code{or}, @code{not}, @dots{}),
295 Transcendental functions (exponential, logarithmic, trigonometric, hyperbolic
296 functions and their inverse functions).
300 CLN is a C++ library. Using C++ as an implementation language provides
304 efficiency: it compiles to machine code,
306 type safety: the C++ compiler knows about the number types and complains
307 if, for example, you try to assign a float to an integer variable.
309 algebraic syntax: You can use the @code{+}, @code{-}, @code{*}, @code{=},
310 @code{==}, @dots{} operators as in C or C++.
314 CLN is memory efficient:
318 Small integers and short floats are immediate, not heap allocated.
320 Heap-allocated memory is reclaimed through an automatic, non-interruptive
325 CLN is speed efficient:
329 The kernel of CLN has been written in assembly language for some CPUs
330 (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
333 On all CPUs, CLN may be configured to use the superefficient low-level
334 routines from GNU GMP version 3.
336 It uses Karatsuba multiplication, which is significantly faster
337 for large numbers than the standard multiplication algorithm.
339 For very large numbers (more than 12000 decimal digits), it uses
341 Sch{@"o}nhage-Strassen
342 @cindex Sch{@"o}nhage-Strassen multiplication
346 @cindex Schoenhage-Strassen multiplication
348 multiplication, which is an asymptotically optimal multiplication
349 algorithm, for multiplication, division and radix conversion.
351 @cindex binary splitting
352 It uses binary splitting for fast evaluation of series of rational
353 numbers as they occur in the evaluation of elementary functions and some
358 CLN aims at being easily integrated into larger software packages:
362 The garbage collection imposes no burden on the main application.
364 The library provides hooks for memory allocation and throws exceptions
368 All non-macro identifiers are hidden in namespace @code{cln} in
369 order to avoid name clashes.
374 @chapter Installation
376 This section describes how to install the CLN package on your system.
381 * Building the library::
382 * Installing the library::
386 @node Prerequisites, Building the library, Installation, Installation
387 @section Prerequisites
396 @subsection C++ compiler
398 To build CLN, you need a C++11 compiler.
399 GNU @code{g++ 4.8.1} or newer is recommended.
401 The following C++ features are used:
402 classes, member functions, overloading of functions and operators,
403 constructors and destructors, inline, const, multiple inheritance,
404 templates and namespaces.
406 The following C++ features are not used:
407 @code{new}, @code{delete}, virtual inheritance.
409 CLN relies on semi-automatic ordering of initializations of static and
410 global variables, a feature which I could implement for GNU g++
411 only. Also, it is not known whether this semi-automatic ordering works
412 on all platforms when a non-GNU assembler is being used.
415 @subsection Make utility
418 To build CLN, you also need to have GNU @code{make} installed.
421 @subsection Sed utility
424 To build CLN on HP-UX, you also need to have GNU @code{sed} installed.
425 This is because the libtool script, which creates the CLN library, relies
426 on @code{sed}, and the vendor's @code{sed} utility on these systems is too
430 @node Building the library
431 @section Building the library
433 As with any autoconfiguring GNU software, installation is as easy as this:
441 If on your system, @samp{make} is not GNU @code{make}, you have to use
442 @samp{gmake} instead of @samp{make} above.
444 The @code{configure} command checks out some features of your system and
445 C++ compiler and builds the @code{Makefile}s. The @code{make} command
446 builds the library. This step may take about half an hour on an average
447 workstation. The @code{make check} runs some test to check that no
448 important subroutine has been miscompiled.
450 The @code{configure} command accepts options. To get a summary of them, try
456 Some of the options are explained in detail in the @samp{INSTALL.generic} file.
458 You can specify the C compiler, the C++ compiler and their options through
459 the following environment variables when running @code{configure}:
463 Specifies the C compiler.
466 Flags to be given to the C compiler when compiling programs (not when linking).
469 Specifies the C++ compiler.
472 Flags to be given to the C++ compiler when compiling programs (not when linking).
475 Flags to be given to the C/C++ preprocessor.
478 Flags to be given to the linker.
484 $ CC="gcc" CFLAGS="-O" CXX="g++" CXXFLAGS="-O" ./configure
487 $ CC=gcc CFLAGS="-O2 -finline-limit=1000" \
488 CXX=g++ CXXFLAGS="-O2 -finline-limit=1000" \
489 CPPFLAGS="-DNO_ASM" ./configure
492 $ CC="gcc-9" CFLAGS="-O2" CXX="g++-9" CXXFLAGS="-O2" ./configure
495 Note that for these environment variables to take effect, you have to set
496 them (assuming a Bourne-compatible shell) on the same line as the
497 @code{configure} command. If you made the settings in earlier shell
498 commands, you have to @code{export} the environment variables before
499 calling @code{configure}. In a @code{csh} shell, you have to use the
500 @samp{setenv} command for setting each of the environment variables.
502 Currently CLN works only with the GNU @code{g++} compiler, and only in
503 optimizing mode. So you should specify at least @code{-O} in the
504 CXXFLAGS, or no CXXFLAGS at all. If CXXFLAGS is not set, CLN will be
505 compiled with @code{-O}.
507 The assembler language kernel can be turned off by specifying
508 @code{-DNO_ASM} in the CPPFLAGS. If @code{make check} reports any
509 problems, you may try to clean up (see @ref{Cleaning up}) and configure
510 and compile again, this time with @code{-DNO_ASM}.
512 If you use @code{g++} 3.2.x or earlier, I recommend adding
513 @samp{-finline-limit=1000} to the CXXFLAGS. This is essential for good
516 If you use @code{g++} from gcc-3.0.4 or older on Sparc, add either
517 @samp{-O}, @samp{-O1} or @samp{-O2 -fno-schedule-insns} to the
518 CXXFLAGS. With full @samp{-O2}, @code{g++} miscompiles the division
519 routines. Also, do not use gcc-3.0 on Sparc for compiling CLN, it
522 Also, please do not compile CLN with @code{g++} using the @code{-O3}
523 optimization level. This leads to inferior code quality.
525 Some newer versions of @code{g++} require quite an amount of memory.
526 You might need some swap space if your machine doesn't have 512 MB of
529 By default, both a shared and a static library are built. You can build
530 CLN as a static (or shared) library only, by calling @code{configure}
531 with the option @samp{--disable-shared} (or @samp{--disable-static}).
532 While shared libraries are usually more convenient to use, they may not
533 work on all architectures. Try disabling them if you run into linker
534 problems. Also, they are generally slightly slower than static
535 libraries so runtime-critical applications should be linked statically.
539 * Using the GNU MP Library::
542 @node Using the GNU MP Library
543 @subsection Using the GNU MP Library
546 CLN may be configured to make use of a preinstalled @code{gmp} library
547 for some low-level routines. Please make sure that you have at least
548 @code{gmp} version 3.0 installed since earlier versions are unsupported
549 and likely not to work. Using @code{gmp} is known to be quite a boost
550 for CLN's performance.
552 By default, CLN will autodetect @code{gmp} and use it. If you do not
553 want CLN to make use of a preinstalled @code{gmp} library, then you can
554 explicitly specify so by calling @code{configure} with the option
555 @samp{--without-gmp}.
557 If you have installed the @code{gmp} library and its header files in
558 some place where the compiler cannot find it by default, you must help
559 @code{configure} and specify the prefix that was used when @code{gmp}
560 was configured. Here is an example:
563 $ ./configure --with-gmp=/opt/gmp-4.2.2
566 This assumes that the @code{gmp} header files have been installed in
567 @file{/opt/gmp-4.2.2/include/} and the library in
568 @file{/opt/gmp-4.2.2/lib/}. More uncommon GMP installations can be
569 handled by setting CPPFLAGS and LDFLAGS appropriately prior to running
573 @node Installing the library
574 @section Installing the library
577 As with any autoconfiguring GNU software, installation is as easy as this:
583 The @samp{make install} command installs the library and the include files
584 into public places (@file{/usr/local/lib/} and @file{/usr/local/include/},
585 if you haven't specified a @code{--prefix} option to @code{configure}).
586 This step may require superuser privileges.
588 If you have already built the library and wish to install it, but didn't
589 specify @code{--prefix=@dots{}} at configure time, just re-run
590 @code{configure}, giving it the same options as the first time, plus
591 the @code{--prefix=@dots{}} option.
597 You can remove system-dependent files generated by @code{make} through
603 You can remove all files generated by @code{make}, thus reverting to a
604 virgin distribution of CLN, through
611 @node Ordinary number types
612 @chapter Ordinary number types
614 CLN implements the following class hierarchy:
622 Real or complex number
631 +-------------------+-------------------+
633 Rational number Floating-point number
635 <cln/rational.h> <cln/float.h>
637 | +--------------+--------------+--------------+
639 cl_I Short-Float Single-Float Double-Float Long-Float
640 <cln/integer.h> cl_SF cl_FF cl_DF cl_LF
641 <cln/sfloat.h> <cln/ffloat.h> <cln/dfloat.h> <cln/lfloat.h>
644 @cindex @code{cl_number}
645 @cindex abstract class
646 The base class @code{cl_number} is an abstract base class.
647 It is not useful to declare a variable of this type except if you want
648 to completely disable compile-time type checking and use run-time type
653 @cindex complex number
654 The class @code{cl_N} comprises real and complex numbers. There is
655 no special class for complex numbers since complex numbers with imaginary
656 part @code{0} are automatically converted to real numbers.
659 The class @code{cl_R} comprises real numbers of different kinds. It is an
663 @cindex rational number
665 The class @code{cl_RA} comprises exact real numbers: rational numbers, including
666 integers. There is no special class for non-integral rational numbers
667 since rational numbers with denominator @code{1} are automatically converted
671 The class @code{cl_F} implements floating-point approximations to real numbers.
672 It is an abstract class.
677 * Floating-point numbers::
683 @section Exact numbers
686 Some numbers are represented as exact numbers: there is no loss of information
687 when such a number is converted from its mathematical value to its internal
688 representation. On exact numbers, the elementary operations (@code{+},
689 @code{-}, @code{*}, @code{/}, comparisons, @dots{}) compute the completely
692 In CLN, the exact numbers are:
696 rational numbers (including integers),
698 complex numbers whose real and imaginary parts are both rational numbers.
701 Rational numbers are always normalized to the form
702 @code{@var{numerator}/@var{denominator}} where the numerator and denominator
703 are coprime integers and the denominator is positive. If the resulting
704 denominator is @code{1}, the rational number is converted to an integer.
706 @cindex immediate numbers
707 Small integers (typically in the range @code{-2^29}@dots{}@code{2^29-1},
708 for 32-bit machines) are especially efficient, because they consume no heap
709 allocation. Otherwise the distinction between these immediate integers
710 (called ``fixnums'') and heap allocated integers (called ``bignums'')
711 is completely transparent.
714 @node Floating-point numbers
715 @section Floating-point numbers
716 @cindex floating-point number
718 Not all real numbers can be represented exactly. (There is an easy mathematical
719 proof for this: Only a countable set of numbers can be stored exactly in
720 a computer, even if one assumes that it has unlimited storage. But there
721 are uncountably many real numbers.) So some approximation is needed.
722 CLN implements ordinary floating-point numbers, with mantissa and exponent.
724 @cindex rounding error
725 The elementary operations (@code{+}, @code{-}, @code{*}, @code{/}, @dots{})
726 only return approximate results. For example, the value of the expression
727 @code{(cl_F) 0.3 + (cl_F) 0.4} prints as @samp{0.70000005}, not as
728 @samp{0.7}. Rounding errors like this one are inevitable when computing
729 with floating-point numbers.
731 Nevertheless, CLN rounds the floating-point results of the operations @code{+},
732 @code{-}, @code{*}, @code{/}, @code{sqrt} according to the ``round-to-even''
733 rule: It first computes the exact mathematical result and then returns the
734 floating-point number which is nearest to this. If two floating-point numbers
735 are equally distant from the ideal result, the one with a @code{0} in its least
736 significant mantissa bit is chosen.
738 Similarly, testing floating point numbers for equality @samp{x == y}
739 is gambling with random errors. Better check for @samp{abs(x - y) < epsilon}
740 for some well-chosen @code{epsilon}.
742 Floating point numbers come in four flavors:
747 Short floats, type @code{cl_SF}.
748 They have 1 sign bit, 8 exponent bits (including the exponent's sign),
749 and 17 mantissa bits (including the ``hidden'' bit).
750 They don't consume heap allocation.
754 Single floats, type @code{cl_FF}.
755 They have 1 sign bit, 8 exponent bits (including the exponent's sign),
756 and 24 mantissa bits (including the ``hidden'' bit).
757 In CLN, they are represented as IEEE single-precision floating point numbers.
758 This corresponds closely to the C/C++ type @samp{float}.
762 Double floats, type @code{cl_DF}.
763 They have 1 sign bit, 11 exponent bits (including the exponent's sign),
764 and 53 mantissa bits (including the ``hidden'' bit).
765 In CLN, they are represented as IEEE double-precision floating point numbers.
766 This corresponds closely to the C/C++ type @samp{double}.
770 Long floats, type @code{cl_LF}.
771 They have 1 sign bit, 32 exponent bits (including the exponent's sign),
772 and n mantissa bits (including the ``hidden'' bit), where n >= 64.
773 The precision of a long float is unlimited, but once created, a long float
774 has a fixed precision. (No ``lazy recomputation''.)
777 Of course, computations with long floats are more expensive than those
778 with smaller floating-point formats.
780 CLN does not implement features like NaNs, denormalized numbers and
781 gradual underflow. If the exponent range of some floating-point type
782 is too limited for your application, choose another floating-point type
783 with larger exponent range.
786 As a user of CLN, you can forget about the differences between the
787 four floating-point types and just declare all your floating-point
788 variables as being of type @code{cl_F}. This has the advantage that
789 when you change the precision of some computation (say, from @code{cl_DF}
790 to @code{cl_LF}), you don't have to change the code, only the precision
791 of the initial values. Also, many transcendental functions have been
792 declared as returning a @code{cl_F} when the argument is a @code{cl_F},
793 but such declarations are missing for the types @code{cl_SF}, @code{cl_FF},
794 @code{cl_DF}, @code{cl_LF}. (Such declarations would be wrong if
795 the floating point contagion rule happened to change in the future.)
798 @node Complex numbers
799 @section Complex numbers
800 @cindex complex number
802 Complex numbers, as implemented by the class @code{cl_N}, have a real
803 part and an imaginary part, both real numbers. A complex number whose
804 imaginary part is the exact number @code{0} is automatically converted
807 Complex numbers can arise from real numbers alone, for example
808 through application of @code{sqrt} or transcendental functions.
815 Conversions from any class to any its superclasses (``base classes'' in
816 C++ terminology) is done automatically.
818 Conversions from the C built-in types @samp{long} and @samp{unsigned long}
819 are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
820 @code{cl_N} and @code{cl_number}.
822 Conversions from the C built-in types @samp{int} and @samp{unsigned int}
823 are provided for the classes @code{cl_I}, @code{cl_RA}, @code{cl_R},
824 @code{cl_N} and @code{cl_number}. However, these conversions emphasize
825 efficiency. On 32-bit systems, their range is therefore limited:
829 The conversion from @samp{int} works only if the argument is < 2^29 and >= -2^29.
831 The conversion from @samp{unsigned int} works only if the argument is < 2^29.
834 In a declaration like @samp{cl_I x = 10;} the C++ compiler is able to
835 do the conversion of @code{10} from @samp{int} to @samp{cl_I} at compile time
836 already. On the other hand, code like @samp{cl_I x = 1000000000;} is
837 in error on 32-bit machines.
838 So, if you want to be sure that an @samp{int} whose magnitude is not guaranteed
839 to be < 2^29 is correctly converted to a @samp{cl_I}, first convert it to a
840 @samp{long}. Similarly, if a large @samp{unsigned int} is to be converted to a
841 @samp{cl_I}, first convert it to an @samp{unsigned long}. On 64-bit machines
842 there is no such restriction. There, conversions from arbitrary 32-bit @samp{int}
843 values always works correctly.
845 Conversions from the C built-in type @samp{float} are provided for the classes
846 @code{cl_FF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
848 Conversions from the C built-in type @samp{double} are provided for the classes
849 @code{cl_DF}, @code{cl_F}, @code{cl_R}, @code{cl_N} and @code{cl_number}.
851 Conversions from @samp{const char *} are provided for the classes
852 @code{cl_I}, @code{cl_RA},
853 @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F},
854 @code{cl_R}, @code{cl_N}.
855 The easiest way to specify a value which is outside of the range of the
856 C++ built-in types is therefore to specify it as a string, like this:
859 cl_I order_of_rubiks_cube_group = "43252003274489856000";
861 Note that this conversion is done at runtime, not at compile-time.
863 Conversions from @code{cl_I} to the C built-in types @samp{int},
864 @samp{unsigned int}, @samp{long}, @samp{unsigned long} are provided through
868 @item int cl_I_to_int (const cl_I& x)
869 @cindex @code{cl_I_to_int ()}
870 @itemx unsigned int cl_I_to_uint (const cl_I& x)
871 @cindex @code{cl_I_to_uint ()}
872 @itemx long cl_I_to_long (const cl_I& x)
873 @cindex @code{cl_I_to_long ()}
874 @itemx unsigned long cl_I_to_ulong (const cl_I& x)
875 @cindex @code{cl_I_to_ulong ()}
876 Returns @code{x} as element of the C type @var{ctype}. If @code{x} is not
877 representable in the range of @var{ctype}, a runtime error occurs.
880 Conversions from the classes @code{cl_I}, @code{cl_RA},
881 @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}, @code{cl_F} and
883 to the C built-in types @samp{float} and @samp{double} are provided through
887 @item float float_approx (const @var{type}& x)
888 @cindex @code{float_approx ()}
889 @itemx double double_approx (const @var{type}& x)
890 @cindex @code{double_approx ()}
891 Returns an approximation of @code{x} of C type @var{ctype}.
892 If @code{abs(x)} is too close to 0 (underflow), 0 is returned.
893 If @code{abs(x)} is too large (overflow), an IEEE infinity is returned.
896 Conversions from any class to any of its subclasses (``derived classes'' in
897 C++ terminology) are not provided. Instead, you can assert and check
898 that a value belongs to a certain subclass, and return it as element of that
899 class, using the @samp{As} and @samp{The} macros.
901 @cindex @code{As()()}
902 @code{As(@var{type})(@var{value})} checks that @var{value} belongs to
903 @var{type} and returns it as such.
904 @cindex @code{The()()}
905 @code{The(@var{type})(@var{value})} assumes that @var{value} belongs to
906 @var{type} and returns it as such. It is your responsibility to ensure
907 that this assumption is valid. Since macros and namespaces don't go
908 together well, there is an equivalent to @samp{The}: the template
916 if (!(x >= 0)) abort();
917 cl_I ten_x_a = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer.
918 // In general, it would be a rational number.
919 cl_I ten_x_b = the<cl_I>(expt(10,x)); // The same as above.
924 @node Functions on numbers
925 @chapter Functions on numbers
927 Each of the number classes declares its mathematical operations in the
928 corresponding include file. For example, if your code operates with
929 objects of type @code{cl_I}, it should @code{#include <cln/integer.h>}.
933 * Constructing numbers::
934 * Elementary functions::
935 * Elementary rational functions::
936 * Elementary complex functions::
938 * Rounding functions::
940 * Transcendental functions::
941 * Functions on integers::
942 * Functions on floating-point numbers::
943 * Conversion functions::
944 * Random number generators::
945 * Modifying operators::
948 @node Constructing numbers
949 @section Constructing numbers
951 Here is how to create number objects ``from nothing''.
955 * Constructing integers::
956 * Constructing rational numbers::
957 * Constructing floating-point numbers::
958 * Constructing complex numbers::
961 @node Constructing integers
962 @subsection Constructing integers
964 @code{cl_I} objects are most easily constructed from C integers and from
965 strings. See @ref{Conversions}.
968 @node Constructing rational numbers
969 @subsection Constructing rational numbers
971 @code{cl_RA} objects can be constructed from strings. The syntax
972 for rational numbers is described in @ref{Internal and printed representation}.
973 Another standard way to produce a rational number is through application
974 of @samp{operator /} or @samp{recip} on integers.
977 @node Constructing floating-point numbers
978 @subsection Constructing floating-point numbers
980 @code{cl_F} objects with low precision are most easily constructed from
981 C @samp{float} and @samp{double}. See @ref{Conversions}.
983 To construct a @code{cl_F} with high precision, you can use the conversion
984 from @samp{const char *}, but you have to specify the desired precision
985 within the string. (See @ref{Internal and printed representation}.)
988 cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
990 will set @samp{e} to the given value, with a precision of 40 decimal digits.
992 The programmatic way to construct a @code{cl_F} with high precision is
993 through the @code{cl_float} conversion function, see
994 @ref{Conversion to floating-point numbers}. For example, to compute
995 @code{e} to 40 decimal places, first construct 1.0 to 40 decimal places
996 and then apply the exponential function:
998 float_format_t precision = float_format(40);
999 cl_F e = exp(cl_float(1,precision));
1003 @node Constructing complex numbers
1004 @subsection Constructing complex numbers
1006 Non-real @code{cl_N} objects are normally constructed through the function
1008 cl_N complex (const cl_R& realpart, const cl_R& imagpart)
1010 See @ref{Elementary complex functions}.
1013 @node Elementary functions
1014 @section Elementary functions
1016 Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
1017 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1018 defines the following operations:
1021 @item @var{type} operator + (const @var{type}&, const @var{type}&)
1022 @cindex @code{operator + ()}
1025 @item @var{type} operator - (const @var{type}&, const @var{type}&)
1026 @cindex @code{operator - ()}
1029 @item @var{type} operator - (const @var{type}&)
1030 Returns the negative of the argument.
1032 @item @var{type} plus1 (const @var{type}& x)
1033 @cindex @code{plus1 ()}
1034 Returns @code{x + 1}.
1036 @item @var{type} minus1 (const @var{type}& x)
1037 @cindex @code{minus1 ()}
1038 Returns @code{x - 1}.
1040 @item @var{type} operator * (const @var{type}&, const @var{type}&)
1041 @cindex @code{operator * ()}
1044 @item @var{type} square (const @var{type}& x)
1045 @cindex @code{square ()}
1046 Returns @code{x * x}.
1049 Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
1050 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1051 defines the following operations:
1054 @item @var{type} operator / (const @var{type}&, const @var{type}&)
1055 @cindex @code{operator / ()}
1058 @item @var{type} recip (const @var{type}&)
1059 @cindex @code{recip ()}
1060 Returns the reciprocal of the argument.
1063 The class @code{cl_I} doesn't define a @samp{/} operation because
1064 in the C/C++ language this operator, applied to integral types,
1065 denotes the @samp{floor} or @samp{truncate} operation (which one of these,
1066 is implementation dependent). (@xref{Rounding functions}.)
1067 Instead, @code{cl_I} defines an ``exact quotient'' function:
1070 @item cl_I exquo (const cl_I& x, const cl_I& y)
1071 @cindex @code{exquo ()}
1072 Checks that @code{y} divides @code{x}, and returns the quotient @code{x}/@code{y}.
1075 The following exponentiation functions are defined:
1078 @item cl_I expt_pos (const cl_I& x, const cl_I& y)
1079 @cindex @code{expt_pos ()}
1080 @itemx cl_RA expt_pos (const cl_RA& x, const cl_I& y)
1081 @code{y} must be > 0. Returns @code{x^y}.
1083 @item cl_RA expt (const cl_RA& x, const cl_I& y)
1084 @cindex @code{expt ()}
1085 @itemx cl_R expt (const cl_R& x, const cl_I& y)
1086 @itemx cl_N expt (const cl_N& x, const cl_I& y)
1090 Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
1091 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1092 defines the following operation:
1095 @item @var{type} abs (const @var{type}& x)
1096 @cindex @code{abs ()}
1097 Returns the absolute value of @code{x}.
1098 This is @code{x} if @code{x >= 0}, and @code{-x} if @code{x <= 0}.
1101 The class @code{cl_N} implements this as follows:
1104 @item cl_R abs (const cl_N x)
1105 Returns the absolute value of @code{x}.
1108 Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
1109 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1110 defines the following operation:
1113 @item @var{type} signum (const @var{type}& x)
1114 @cindex @code{signum ()}
1115 Returns the sign of @code{x}, in the same number format as @code{x}.
1116 This is defined as @code{x / abs(x)} if @code{x} is non-zero, and
1117 @code{x} if @code{x} is zero. If @code{x} is real, the value is either
1122 @node Elementary rational functions
1123 @section Elementary rational functions
1125 Each of the classes @code{cl_RA}, @code{cl_I} defines the following operations:
1128 @item cl_I numerator (const @var{type}& x)
1129 @cindex @code{numerator ()}
1130 Returns the numerator of @code{x}.
1132 @item cl_I denominator (const @var{type}& x)
1133 @cindex @code{denominator ()}
1134 Returns the denominator of @code{x}.
1137 The numerator and denominator of a rational number are normalized in such
1138 a way that they have no factor in common and the denominator is positive.
1141 @node Elementary complex functions
1142 @section Elementary complex functions
1144 The class @code{cl_N} defines the following operation:
1147 @item cl_N complex (const cl_R& a, const cl_R& b)
1148 @cindex @code{complex ()}
1149 Returns the complex number @code{a+bi}, that is, the complex number with
1150 real part @code{a} and imaginary part @code{b}.
1153 Each of the classes @code{cl_N}, @code{cl_R} defines the following operations:
1156 @item cl_R realpart (const @var{type}& x)
1157 @cindex @code{realpart ()}
1158 Returns the real part of @code{x}.
1160 @item cl_R imagpart (const @var{type}& x)
1161 @cindex @code{imagpart ()}
1162 Returns the imaginary part of @code{x}.
1164 @item @var{type} conjugate (const @var{type}& x)
1165 @cindex @code{conjugate ()}
1166 Returns the complex conjugate of @code{x}.
1169 We have the relations
1173 @code{x = complex(realpart(x), imagpart(x))}
1175 @code{conjugate(x) = complex(realpart(x), -imagpart(x))}
1180 @section Comparisons
1183 Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
1184 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1185 defines the following operations:
1188 @item bool operator == (const @var{type}&, const @var{type}&)
1189 @cindex @code{operator == ()}
1190 @itemx bool operator != (const @var{type}&, const @var{type}&)
1191 @cindex @code{operator != ()}
1192 Comparison, as in C and C++.
1194 @item uint32 equal_hashcode (const @var{type}&)
1195 @cindex @code{equal_hashcode ()}
1196 Returns a 32-bit hash code that is the same for any two numbers which are
1197 the same according to @code{==}. This hash code depends on the number's value,
1198 not its type or precision.
1200 @item bool zerop (const @var{type}& x)
1201 @cindex @code{zerop ()}
1202 Compare against zero: @code{x == 0}
1205 Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
1206 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1207 defines the following operations:
1210 @item cl_signean compare (const @var{type}& x, const @var{type}& y)
1211 @cindex @code{compare ()}
1212 Compares @code{x} and @code{y}. Returns +1 if @code{x}>@code{y},
1213 -1 if @code{x}<@code{y}, 0 if @code{x}=@code{y}.
1215 @item bool operator <= (const @var{type}&, const @var{type}&)
1216 @cindex @code{operator <= ()}
1217 @itemx bool operator < (const @var{type}&, const @var{type}&)
1218 @cindex @code{operator < ()}
1219 @itemx bool operator >= (const @var{type}&, const @var{type}&)
1220 @cindex @code{operator >= ()}
1221 @itemx bool operator > (const @var{type}&, const @var{type}&)
1222 @cindex @code{operator > ()}
1223 Comparison, as in C and C++.
1225 @item bool minusp (const @var{type}& x)
1226 @cindex @code{minusp ()}
1227 Compare against zero: @code{x < 0}
1229 @item bool plusp (const @var{type}& x)
1230 @cindex @code{plusp ()}
1231 Compare against zero: @code{x > 0}
1233 @item @var{type} max (const @var{type}& x, const @var{type}& y)
1234 @cindex @code{max ()}
1235 Return the maximum of @code{x} and @code{y}.
1237 @item @var{type} min (const @var{type}& x, const @var{type}& y)
1238 @cindex @code{min ()}
1239 Return the minimum of @code{x} and @code{y}.
1242 When a floating point number and a rational number are compared, the float
1243 is first converted to a rational number using the function @code{rational}.
1244 Since a floating point number actually represents an interval of real numbers,
1245 the result might be surprising.
1246 For example, @code{(cl_F)(cl_R)"1/3" == (cl_R)"1/3"} returns false because
1247 there is no floating point number whose value is exactly @code{1/3}.
1250 @node Rounding functions
1251 @section Rounding functions
1254 When a real number is to be converted to an integer, there is no ``best''
1255 rounding. The desired rounding function depends on the application.
1256 The Common Lisp and ISO Lisp standards offer four rounding functions:
1260 This is the largest integer <=@code{x}.
1263 This is the smallest integer >=@code{x}.
1266 Among the integers between 0 and @code{x} (inclusive) the one nearest to @code{x}.
1269 The integer nearest to @code{x}. If @code{x} is exactly halfway between two
1270 integers, choose the even one.
1273 These functions have different advantages:
1275 @code{floor} and @code{ceiling} are translation invariant:
1276 @code{floor(x+n) = floor(x) + n} and @code{ceiling(x+n) = ceiling(x) + n}
1277 for every @code{x} and every integer @code{n}.
1279 On the other hand, @code{truncate} and @code{round} are symmetric:
1280 @code{truncate(-x) = -truncate(x)} and @code{round(-x) = -round(x)},
1281 and furthermore @code{round} is unbiased: on the ``average'', it rounds
1282 down exactly as often as it rounds up.
1284 The functions are related like this:
1288 @code{ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1}
1289 for rational numbers @code{m/n} (@code{m}, @code{n} integers, @code{n}>0), and
1291 @code{truncate(x) = sign(x) * floor(abs(x))}
1294 Each of the classes @code{cl_R}, @code{cl_RA},
1295 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1296 defines the following operations:
1299 @item cl_I floor1 (const @var{type}& x)
1300 @cindex @code{floor1 ()}
1301 Returns @code{floor(x)}.
1302 @item cl_I ceiling1 (const @var{type}& x)
1303 @cindex @code{ceiling1 ()}
1304 Returns @code{ceiling(x)}.
1305 @item cl_I truncate1 (const @var{type}& x)
1306 @cindex @code{truncate1 ()}
1307 Returns @code{truncate(x)}.
1308 @item cl_I round1 (const @var{type}& x)
1309 @cindex @code{round1 ()}
1310 Returns @code{round(x)}.
1313 Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
1314 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1315 defines the following operations:
1318 @item cl_I floor1 (const @var{type}& x, const @var{type}& y)
1319 Returns @code{floor(x/y)}.
1320 @item cl_I ceiling1 (const @var{type}& x, const @var{type}& y)
1321 Returns @code{ceiling(x/y)}.
1322 @item cl_I truncate1 (const @var{type}& x, const @var{type}& y)
1323 Returns @code{truncate(x/y)}.
1324 @item cl_I round1 (const @var{type}& x, const @var{type}& y)
1325 Returns @code{round(x/y)}.
1328 These functions are called @samp{floor1}, @dots{} here instead of
1329 @samp{floor}, @dots{}, because on some systems, system dependent include
1330 files define @samp{floor} and @samp{ceiling} as macros.
1332 In many cases, one needs both the quotient and the remainder of a division.
1333 It is more efficient to compute both at the same time than to perform
1334 two divisions, one for quotient and the next one for the remainder.
1335 The following functions therefore return a structure containing both
1336 the quotient and the remainder. The suffix @samp{2} indicates the number
1337 of ``return values''. The remainder is defined as follows:
1341 for the computation of @code{quotient = floor(x)},
1342 @code{remainder = x - quotient},
1344 for the computation of @code{quotient = floor(x,y)},
1345 @code{remainder = x - quotient*y},
1348 and similarly for the other three operations.
1350 Each of the classes @code{cl_R}, @code{cl_RA},
1351 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1352 defines the following operations:
1355 @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
1356 @itemx @var{type}_div_t floor2 (const @var{type}& x)
1357 @itemx @var{type}_div_t ceiling2 (const @var{type}& x)
1358 @itemx @var{type}_div_t truncate2 (const @var{type}& x)
1359 @itemx @var{type}_div_t round2 (const @var{type}& x)
1362 Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_I},
1363 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1364 defines the following operations:
1367 @item struct @var{type}_div_t @{ cl_I quotient; @var{type} remainder; @};
1368 @itemx @var{type}_div_t floor2 (const @var{type}& x, const @var{type}& y)
1369 @cindex @code{floor2 ()}
1370 @itemx @var{type}_div_t ceiling2 (const @var{type}& x, const @var{type}& y)
1371 @cindex @code{ceiling2 ()}
1372 @itemx @var{type}_div_t truncate2 (const @var{type}& x, const @var{type}& y)
1373 @cindex @code{truncate2 ()}
1374 @itemx @var{type}_div_t round2 (const @var{type}& x, const @var{type}& y)
1375 @cindex @code{round2 ()}
1378 Sometimes, one wants the quotient as a floating-point number (of the
1379 same format as the argument, if the argument is a float) instead of as
1380 an integer. The prefix @samp{f} indicates this.
1383 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1384 defines the following operations:
1387 @item @var{type} ffloor (const @var{type}& x)
1388 @cindex @code{ffloor ()}
1389 @itemx @var{type} fceiling (const @var{type}& x)
1390 @cindex @code{fceiling ()}
1391 @itemx @var{type} ftruncate (const @var{type}& x)
1392 @cindex @code{ftruncate ()}
1393 @itemx @var{type} fround (const @var{type}& x)
1394 @cindex @code{fround ()}
1397 and similarly for class @code{cl_R}, but with return type @code{cl_F}.
1399 The class @code{cl_R} defines the following operations:
1402 @item cl_F ffloor (const @var{type}& x, const @var{type}& y)
1403 @itemx cl_F fceiling (const @var{type}& x, const @var{type}& y)
1404 @itemx cl_F ftruncate (const @var{type}& x, const @var{type}& y)
1405 @itemx cl_F fround (const @var{type}& x, const @var{type}& y)
1408 These functions also exist in versions which return both the quotient
1409 and the remainder. The suffix @samp{2} indicates this.
1412 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1413 defines the following operations:
1414 @cindex @code{cl_F_fdiv_t}
1415 @cindex @code{cl_SF_fdiv_t}
1416 @cindex @code{cl_FF_fdiv_t}
1417 @cindex @code{cl_DF_fdiv_t}
1418 @cindex @code{cl_LF_fdiv_t}
1421 @item struct @var{type}_fdiv_t @{ @var{type} quotient; @var{type} remainder; @};
1422 @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x)
1423 @cindex @code{ffloor2 ()}
1424 @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x)
1425 @cindex @code{fceiling2 ()}
1426 @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x)
1427 @cindex @code{ftruncate2 ()}
1428 @itemx @var{type}_fdiv_t fround2 (const @var{type}& x)
1429 @cindex @code{fround2 ()}
1431 and similarly for class @code{cl_R}, but with quotient type @code{cl_F}.
1432 @cindex @code{cl_R_fdiv_t}
1434 The class @code{cl_R} defines the following operations:
1437 @item struct @var{type}_fdiv_t @{ cl_F quotient; cl_R remainder; @};
1438 @itemx @var{type}_fdiv_t ffloor2 (const @var{type}& x, const @var{type}& y)
1439 @itemx @var{type}_fdiv_t fceiling2 (const @var{type}& x, const @var{type}& y)
1440 @itemx @var{type}_fdiv_t ftruncate2 (const @var{type}& x, const @var{type}& y)
1441 @itemx @var{type}_fdiv_t fround2 (const @var{type}& x, const @var{type}& y)
1444 Other applications need only the remainder of a division.
1445 The remainder of @samp{floor} and @samp{ffloor} is called @samp{mod}
1446 (abbreviation of ``modulo''). The remainder @samp{truncate} and
1447 @samp{ftruncate} is called @samp{rem} (abbreviation of ``remainder'').
1451 @code{mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y}
1453 @code{rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y}
1456 If @code{x} and @code{y} are both >= 0, @code{mod(x,y) = rem(x,y) >= 0}.
1457 In general, @code{mod(x,y)} has the sign of @code{y} or is zero,
1458 and @code{rem(x,y)} has the sign of @code{x} or is zero.
1460 The classes @code{cl_R}, @code{cl_I} define the following operations:
1463 @item @var{type} mod (const @var{type}& x, const @var{type}& y)
1464 @cindex @code{mod ()}
1465 @itemx @var{type} rem (const @var{type}& x, const @var{type}& y)
1466 @cindex @code{rem ()}
1473 Each of the classes @code{cl_R},
1474 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
1475 defines the following operation:
1478 @item @var{type} sqrt (const @var{type}& x)
1479 @cindex @code{sqrt ()}
1480 @code{x} must be >= 0. This function returns the square root of @code{x},
1481 normalized to be >= 0. If @code{x} is the square of a rational number,
1482 @code{sqrt(x)} will be a rational number, else it will return a
1483 floating-point approximation.
1486 The classes @code{cl_RA}, @code{cl_I} define the following operation:
1489 @item bool sqrtp (const @var{type}& x, @var{type}* root)
1490 @cindex @code{sqrtp ()}
1491 This tests whether @code{x} is a perfect square. If so, it returns true
1492 and the exact square root in @code{*root}, else it returns false.
1495 Furthermore, for integers, similarly:
1498 @item bool isqrt (const @var{type}& x, @var{type}* root)
1499 @cindex @code{isqrt ()}
1500 @code{x} should be >= 0. This function sets @code{*root} to
1501 @code{floor(sqrt(x))} and returns the same value as @code{sqrtp}:
1502 the boolean value @code{(expt(*root,2) == x)}.
1505 For @code{n}th roots, the classes @code{cl_RA}, @code{cl_I}
1506 define the following operation:
1509 @item bool rootp (const @var{type}& x, const cl_I& n, @var{type}* root)
1510 @cindex @code{rootp ()}
1511 @code{x} must be >= 0. @code{n} must be > 0.
1512 This tests whether @code{x} is an @code{n}th power of a rational number.
1513 If so, it returns true and the exact root in @code{*root}, else it returns
1517 The only square root function which accepts negative numbers is the one
1518 for class @code{cl_N}:
1521 @item cl_N sqrt (const cl_N& z)
1522 @cindex @code{sqrt ()}
1523 Returns the square root of @code{z}, as defined by the formula
1524 @code{sqrt(z) = exp(log(z)/2)}. Conversion to a floating-point type
1525 or to a complex number are done if necessary. The range of the result is the
1526 right half plane @code{realpart(sqrt(z)) >= 0}
1527 including the positive imaginary axis and 0, but excluding
1528 the negative imaginary axis.
1529 The result is an exact number only if @code{z} is an exact number.
1533 @node Transcendental functions
1534 @section Transcendental functions
1535 @cindex transcendental functions
1537 The transcendental functions return an exact result if the argument
1538 is exact and the result is exact as well. Otherwise they must return
1539 inexact numbers even if the argument is exact.
1540 For example, @code{cos(0) = 1} returns the rational number @code{1}.
1544 * Exponential and logarithmic functions::
1545 * Trigonometric functions::
1546 * Hyperbolic functions::
1551 @node Exponential and logarithmic functions
1552 @subsection Exponential and logarithmic functions
1555 @item cl_R exp (const cl_R& x)
1556 @cindex @code{exp ()}
1557 @itemx cl_N exp (const cl_N& x)
1558 Returns the exponential function of @code{x}. This is @code{e^x} where
1559 @code{e} is the base of the natural logarithms. The range of the result
1560 is the entire complex plane excluding 0.
1562 @item cl_R ln (const cl_R& x)
1563 @cindex @code{ln ()}
1564 @code{x} must be > 0. Returns the (natural) logarithm of x.
1566 @item cl_N log (const cl_N& x)
1567 @cindex @code{log ()}
1568 Returns the (natural) logarithm of x. If @code{x} is real and positive,
1569 this is @code{ln(x)}. In general, @code{log(x) = log(abs(x)) + i*phase(x)}.
1570 The range of the result is the strip in the complex plane
1571 @code{-pi < imagpart(log(x)) <= pi}.
1573 @item cl_R phase (const cl_N& x)
1574 @cindex @code{phase ()}
1575 Returns the angle part of @code{x} in its polar representation as a
1576 complex number. That is, @code{phase(x) = atan(realpart(x),imagpart(x))}.
1577 This is also the imaginary part of @code{log(x)}.
1578 The range of the result is the interval @code{-pi < phase(x) <= pi}.
1579 The result will be an exact number only if @code{zerop(x)} or
1580 if @code{x} is real and positive.
1582 @item cl_R log (const cl_R& a, const cl_R& b)
1583 @code{a} and @code{b} must be > 0. Returns the logarithm of @code{a} with
1584 respect to base @code{b}. @code{log(a,b) = ln(a)/ln(b)}.
1585 The result can be exact only if @code{a = 1} or if @code{a} and @code{b}
1588 @item cl_N log (const cl_N& a, const cl_N& b)
1589 Returns the logarithm of @code{a} with respect to base @code{b}.
1590 @code{log(a,b) = log(a)/log(b)}.
1592 @item cl_N expt (const cl_N& x, const cl_N& y)
1593 @cindex @code{expt ()}
1594 Exponentiation: Returns @code{x^y = exp(y*log(x))}.
1597 The constant e = exp(1) = 2.71828@dots{} is returned by the following functions:
1600 @item cl_F exp1 (float_format_t f)
1601 @cindex @code{exp1 ()}
1602 Returns e as a float of format @code{f}.
1604 @item cl_F exp1 (const cl_F& y)
1605 Returns e in the float format of @code{y}.
1607 @item cl_F exp1 (void)
1608 Returns e as a float of format @code{default_float_format}.
1612 @node Trigonometric functions
1613 @subsection Trigonometric functions
1616 @item cl_R sin (const cl_R& x)
1617 @cindex @code{sin ()}
1618 Returns @code{sin(x)}. The range of the result is the interval
1619 @code{-1 <= sin(x) <= 1}.
1621 @item cl_N sin (const cl_N& z)
1622 Returns @code{sin(z)}. The range of the result is the entire complex plane.
1624 @item cl_R cos (const cl_R& x)
1625 @cindex @code{cos ()}
1626 Returns @code{cos(x)}. The range of the result is the interval
1627 @code{-1 <= cos(x) <= 1}.
1629 @item cl_N cos (const cl_N& x)
1630 Returns @code{cos(z)}. The range of the result is the entire complex plane.
1632 @item struct cos_sin_t @{ cl_R cos; cl_R sin; @};
1633 @cindex @code{cos_sin_t}
1634 @itemx cos_sin_t cos_sin (const cl_R& x)
1635 Returns both @code{sin(x)} and @code{cos(x)}. This is more efficient than
1636 @cindex @code{cos_sin ()}
1637 computing them separately. The relation @code{cos^2 + sin^2 = 1} will
1638 hold only approximately.
1640 @item cl_R tan (const cl_R& x)
1641 @cindex @code{tan ()}
1642 @itemx cl_N tan (const cl_N& x)
1643 Returns @code{tan(x) = sin(x)/cos(x)}.
1645 @item cl_N cis (const cl_R& x)
1646 @cindex @code{cis ()}
1647 @itemx cl_N cis (const cl_N& x)
1648 Returns @code{exp(i*x)}. The name @samp{cis} means ``cos + i sin'', because
1649 @code{e^(i*x) = cos(x) + i*sin(x)}.
1652 @cindex @code{asin ()}
1653 @item cl_N asin (const cl_N& z)
1654 Returns @code{arcsin(z)}. This is defined as
1655 @code{arcsin(z) = log(iz+sqrt(1-z^2))/i} and satisfies
1656 @code{arcsin(-z) = -arcsin(z)}.
1657 The range of the result is the strip in the complex domain
1658 @code{-pi/2 <= realpart(arcsin(z)) <= pi/2}, excluding the numbers
1659 with @code{realpart = -pi/2} and @code{imagpart < 0} and the numbers
1660 with @code{realpart = pi/2} and @code{imagpart > 0}.
1662 Proof: This follows from arcsin(z) = arsinh(iz)/i and the corresponding
1666 @item cl_N acos (const cl_N& z)
1667 @cindex @code{acos ()}
1668 Returns @code{arccos(z)}. This is defined as
1669 @code{arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i}
1672 @code{arccos(z) = 2*log(sqrt((1+z)/2)+i*sqrt((1-z)/2))/i}
1674 and satisfies @code{arccos(-z) = pi - arccos(z)}.
1675 The range of the result is the strip in the complex domain
1676 @code{0 <= realpart(arcsin(z)) <= pi}, excluding the numbers
1677 with @code{realpart = 0} and @code{imagpart < 0} and the numbers
1678 with @code{realpart = pi} and @code{imagpart > 0}.
1680 Proof: This follows from the results about arcsin.
1684 @cindex @code{atan ()}
1685 @item cl_R atan (const cl_R& x, const cl_R& y)
1686 Returns the angle of the polar representation of the complex number
1687 @code{x+iy}. This is @code{atan(y/x)} if @code{x>0}. The range of
1688 the result is the interval @code{-pi < atan(x,y) <= pi}. The result will
1689 be an exact number only if @code{x > 0} and @code{y} is the exact @code{0}.
1690 WARNING: In Common Lisp, this function is called as @code{(atan y x)},
1691 with reversed order of arguments.
1693 @item cl_R atan (const cl_R& x)
1694 Returns @code{arctan(x)}. This is the same as @code{atan(1,x)}. The range
1695 of the result is the interval @code{-pi/2 < atan(x) < pi/2}. The result
1696 will be an exact number only if @code{x} is the exact @code{0}.
1698 @item cl_N atan (const cl_N& z)
1699 Returns @code{arctan(z)}. This is defined as
1700 @code{arctan(z) = (log(1+iz)-log(1-iz)) / 2i} and satisfies
1701 @code{arctan(-z) = -arctan(z)}. The range of the result is
1702 the strip in the complex domain
1703 @code{-pi/2 <= realpart(arctan(z)) <= pi/2}, excluding the numbers
1704 with @code{realpart = -pi/2} and @code{imagpart >= 0} and the numbers
1705 with @code{realpart = pi/2} and @code{imagpart <= 0}.
1707 Proof: arctan(z) = artanh(iz)/i, we know the range of the artanh function.
1713 @cindex Archimedes' constant
1714 Archimedes' constant pi = 3.14@dots{} is returned by the following functions:
1717 @item cl_F pi (float_format_t f)
1718 @cindex @code{pi ()}
1719 Returns pi as a float of format @code{f}.
1721 @item cl_F pi (const cl_F& y)
1722 Returns pi in the float format of @code{y}.
1724 @item cl_F pi (void)
1725 Returns pi as a float of format @code{default_float_format}.
1729 @node Hyperbolic functions
1730 @subsection Hyperbolic functions
1733 @item cl_R sinh (const cl_R& x)
1734 @cindex @code{sinh ()}
1735 Returns @code{sinh(x)}.
1737 @item cl_N sinh (const cl_N& z)
1738 Returns @code{sinh(z)}. The range of the result is the entire complex plane.
1740 @item cl_R cosh (const cl_R& x)
1741 @cindex @code{cosh ()}
1742 Returns @code{cosh(x)}. The range of the result is the interval
1743 @code{cosh(x) >= 1}.
1745 @item cl_N cosh (const cl_N& z)
1746 Returns @code{cosh(z)}. The range of the result is the entire complex plane.
1748 @item struct cosh_sinh_t @{ cl_R cosh; cl_R sinh; @};
1749 @cindex @code{cosh_sinh_t}
1750 @itemx cosh_sinh_t cosh_sinh (const cl_R& x)
1751 @cindex @code{cosh_sinh ()}
1752 Returns both @code{sinh(x)} and @code{cosh(x)}. This is more efficient than
1753 computing them separately. The relation @code{cosh^2 - sinh^2 = 1} will
1754 hold only approximately.
1756 @item cl_R tanh (const cl_R& x)
1757 @cindex @code{tanh ()}
1758 @itemx cl_N tanh (const cl_N& x)
1759 Returns @code{tanh(x) = sinh(x)/cosh(x)}.
1761 @item cl_N asinh (const cl_N& z)
1762 @cindex @code{asinh ()}
1763 Returns @code{arsinh(z)}. This is defined as
1764 @code{arsinh(z) = log(z+sqrt(1+z^2))} and satisfies
1765 @code{arsinh(-z) = -arsinh(z)}.
1767 Proof: Knowing the range of log, we know -pi < imagpart(arsinh(z)) <= pi.
1768 Actually, z+sqrt(1+z^2) can never be real and <0, so
1769 -pi < imagpart(arsinh(z)) < pi.
1770 We have (z+sqrt(1+z^2))*(-z+sqrt(1+(-z)^2)) = (1+z^2)-z^2 = 1, hence the
1771 logs of both factors sum up to 0 mod 2*pi*i, hence to 0.
1773 The range of the result is the strip in the complex domain
1774 @code{-pi/2 <= imagpart(arsinh(z)) <= pi/2}, excluding the numbers
1775 with @code{imagpart = -pi/2} and @code{realpart > 0} and the numbers
1776 with @code{imagpart = pi/2} and @code{realpart < 0}.
1778 Proof: Write z = x+iy. Because of arsinh(-z) = -arsinh(z), we may assume
1779 that z is in Range(sqrt), that is, x>=0 and, if x=0, then y>=0.
1780 If x > 0, then Re(z+sqrt(1+z^2)) = x + Re(sqrt(1+z^2)) >= x > 0,
1781 so -pi/2 < imagpart(log(z+sqrt(1+z^2))) < pi/2.
1782 If x = 0 and y >= 0, arsinh(z) = log(i*y+sqrt(1-y^2)).
1783 If y <= 1, the realpart is 0 and the imagpart is >= 0 and <= pi/2.
1784 If y >= 1, the imagpart is pi/2 and the realpart is
1785 log(y+sqrt(y^2-1)) >= log(y) >= 0.
1788 Moreover, if z is in Range(sqrt),
1789 log(sqrt(1+z^2)+z) = 2 artanh(z/(1+sqrt(1+z^2)))
1790 (for a proof, see file src/cl_C_asinh.cc).
1793 @item cl_N acosh (const cl_N& z)
1794 @cindex @code{acosh ()}
1795 Returns @code{arcosh(z)}. This is defined as
1796 @code{arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))}.
1797 The range of the result is the half-strip in the complex domain
1798 @code{-pi < imagpart(arcosh(z)) <= pi, realpart(arcosh(z)) >= 0},
1799 excluding the numbers with @code{realpart = 0} and @code{-pi < imagpart < 0}.
1801 Proof: sqrt((z+1)/2) and sqrt((z-1)/2)) lie in Range(sqrt), hence does
1802 their sum, hence its log has an imagpart <= pi/2 and > -pi/2.
1803 If z is in Range(sqrt), we have
1804 sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1)
1805 ==> (sqrt((z+1)/2)+sqrt((z-1)/2))^2 = (z+1)/2 + sqrt(z^2-1) + (z-1)/2
1807 ==> arcosh(z) = log(z+sqrt(z^2-1)) mod 2*pi*i
1808 and since the imagpart of both expressions is > -pi, <= pi
1809 ==> arcosh(z) = log(z+sqrt(z^2-1))
1810 To prove that the realpart of this is >= 0, write z = x+iy with x>=0,
1811 z^2-1 = u+iv with u = x^2-y^2-1, v = 2xy,
1812 sqrt(z^2-1) = p+iq with p = sqrt((sqrt(u^2+v^2)+u)/2) >= 0,
1813 q = sqrt((sqrt(u^2+v^2)-u)/2) * sign(v),
1814 then |z+sqrt(z^2-1)|^2 = |x+iy + p+iq|^2
1816 = x^2 + 2xp + p^2 + y^2 + 2yq + q^2
1817 >= x^2 + p^2 + y^2 + q^2 (since x>=0, p>=0, yq>=0)
1818 = x^2 + y^2 + sqrt(u^2+v^2)
1823 hence realpart(log(z+sqrt(z^2-1))) = log(|z+sqrt(z^2-1)|) >= 0.
1824 Equality holds only if y = 0 and u <= 0, i.e. 0 <= x < 1.
1825 In this case arcosh(z) = log(x+i*sqrt(1-x^2)) has imagpart >=0.
1826 Otherwise, -z is in Range(sqrt).
1827 If y != 0, sqrt((z+1)/2) = i^sign(y) * sqrt((-z-1)/2),
1828 sqrt((z-1)/2) = i^sign(y) * sqrt((-z+1)/2),
1829 hence arcosh(z) = sign(y)*pi/2*i + arcosh(-z),
1830 and this has realpart > 0.
1831 If y = 0 and -1<=x<=0, we still have sqrt(z+1)*sqrt(z-1) = sqrt(z^2-1),
1832 ==> arcosh(z) = log(z+sqrt(z^2-1)) = log(x+i*sqrt(1-x^2))
1833 has realpart = 0 and imagpart > 0.
1834 If y = 0 and x<=-1, however, sqrt(z+1)*sqrt(z-1) = - sqrt(z^2-1),
1835 ==> arcosh(z) = log(z-sqrt(z^2-1)) = pi*i + arcosh(-z).
1836 This has realpart >= 0 and imagpart = pi.
1839 @item cl_N atanh (const cl_N& z)
1840 @cindex @code{atanh ()}
1841 Returns @code{artanh(z)}. This is defined as
1842 @code{artanh(z) = (log(1+z)-log(1-z)) / 2} and satisfies
1843 @code{artanh(-z) = -artanh(z)}. The range of the result is
1844 the strip in the complex domain
1845 @code{-pi/2 <= imagpart(artanh(z)) <= pi/2}, excluding the numbers
1846 with @code{imagpart = -pi/2} and @code{realpart <= 0} and the numbers
1847 with @code{imagpart = pi/2} and @code{realpart >= 0}.
1849 Proof: Write z = x+iy. Examine
1850 imagpart(artanh(z)) = (atan(1+x,y) - atan(1-x,-y))/2.
1852 x > 1 ==> imagpart = -pi/2, realpart = 1/2 log((x+1)/(x-1)) > 0,
1853 x < -1 ==> imagpart = pi/2, realpart = 1/2 log((-x-1)/(-x+1)) < 0,
1854 |x| < 1 ==> imagpart = 0
1857 = (atan(1+x,y) - atan(1-x,-y))/2
1858 = ((pi/2 - atan((1+x)/y)) - (-pi/2 - atan((1-x)/-y)))/2
1859 = (pi - atan((1+x)/y) - atan((1-x)/y))/2
1860 > (pi - pi/2 - pi/2 )/2 = 0
1861 and (1+x)/y > (1-x)/y
1862 ==> atan((1+x)/y) > atan((-1+x)/y) = - atan((1-x)/y)
1863 ==> imagpart < pi/2.
1864 Hence 0 < imagpart < pi/2.
1866 By artanh(z) = -artanh(-z) and case 2, -pi/2 < imagpart < 0.
1872 @subsection Euler gamma
1873 @cindex Euler's constant
1875 Euler's constant C = 0.577@dots{} is returned by the following functions:
1878 @item cl_F eulerconst (float_format_t f)
1879 @cindex @code{eulerconst ()}
1880 Returns Euler's constant as a float of format @code{f}.
1882 @item cl_F eulerconst (const cl_F& y)
1883 Returns Euler's constant in the float format of @code{y}.
1885 @item cl_F eulerconst (void)
1886 Returns Euler's constant as a float of format @code{default_float_format}.
1889 Catalan's constant G = 0.915@dots{} is returned by the following functions:
1890 @cindex Catalan's constant
1893 @item cl_F catalanconst (float_format_t f)
1894 @cindex @code{catalanconst ()}
1895 Returns Catalan's constant as a float of format @code{f}.
1897 @item cl_F catalanconst (const cl_F& y)
1898 Returns Catalan's constant in the float format of @code{y}.
1900 @item cl_F catalanconst (void)
1901 Returns Catalan's constant as a float of format @code{default_float_format}.
1906 @subsection Riemann zeta
1907 @cindex Riemann's zeta
1909 Riemann's zeta function at an integral point @code{s>1} is returned by the
1910 following functions:
1913 @item cl_F zeta (int s, float_format_t f)
1914 @cindex @code{zeta ()}
1915 Returns Riemann's zeta function at @code{s} as a float of format @code{f}.
1917 @item cl_F zeta (int s, const cl_F& y)
1918 Returns Riemann's zeta function at @code{s} in the float format of @code{y}.
1920 @item cl_F zeta (int s)
1921 Returns Riemann's zeta function at @code{s} as a float of format
1922 @code{default_float_format}.
1926 @node Functions on integers
1927 @section Functions on integers
1930 * Logical functions::
1931 * Number theoretic functions::
1932 * Combinatorial functions::
1935 @node Logical functions
1936 @subsection Logical functions
1938 Integers, when viewed as in two's complement notation, can be thought as
1939 infinite bit strings where the bits' values eventually are constant.
1946 The logical operations view integers as such bit strings and operate
1947 on each of the bit positions in parallel.
1950 @item cl_I lognot (const cl_I& x)
1951 @cindex @code{lognot ()}
1952 @itemx cl_I operator ~ (const cl_I& x)
1953 @cindex @code{operator ~ ()}
1954 Logical not, like @code{~x} in C. This is the same as @code{-1-x}.
1956 @item cl_I logand (const cl_I& x, const cl_I& y)
1957 @cindex @code{logand ()}
1958 @itemx cl_I operator & (const cl_I& x, const cl_I& y)
1959 @cindex @code{operator & ()}
1960 Logical and, like @code{x & y} in C.
1962 @item cl_I logior (const cl_I& x, const cl_I& y)
1963 @cindex @code{logior ()}
1964 @itemx cl_I operator | (const cl_I& x, const cl_I& y)
1965 @cindex @code{operator | ()}
1966 Logical (inclusive) or, like @code{x | y} in C.
1968 @item cl_I logxor (const cl_I& x, const cl_I& y)
1969 @cindex @code{logxor ()}
1970 @itemx cl_I operator ^ (const cl_I& x, const cl_I& y)
1971 @cindex @code{operator ^ ()}
1972 Exclusive or, like @code{x ^ y} in C.
1974 @item cl_I logeqv (const cl_I& x, const cl_I& y)
1975 @cindex @code{logeqv ()}
1976 Bitwise equivalence, like @code{~(x ^ y)} in C.
1978 @item cl_I lognand (const cl_I& x, const cl_I& y)
1979 @cindex @code{lognand ()}
1980 Bitwise not and, like @code{~(x & y)} in C.
1982 @item cl_I lognor (const cl_I& x, const cl_I& y)
1983 @cindex @code{lognor ()}
1984 Bitwise not or, like @code{~(x | y)} in C.
1986 @item cl_I logandc1 (const cl_I& x, const cl_I& y)
1987 @cindex @code{logandc1 ()}
1988 Logical and, complementing the first argument, like @code{~x & y} in C.
1990 @item cl_I logandc2 (const cl_I& x, const cl_I& y)
1991 @cindex @code{logandc2 ()}
1992 Logical and, complementing the second argument, like @code{x & ~y} in C.
1994 @item cl_I logorc1 (const cl_I& x, const cl_I& y)
1995 @cindex @code{logorc1 ()}
1996 Logical or, complementing the first argument, like @code{~x | y} in C.
1998 @item cl_I logorc2 (const cl_I& x, const cl_I& y)
1999 @cindex @code{logorc2 ()}
2000 Logical or, complementing the second argument, like @code{x | ~y} in C.
2003 These operations are all available though the function
2005 @item cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)
2006 @cindex @code{boole ()}
2008 where @code{op} must have one of the 16 values (each one stands for a function
2009 which combines two bits into one bit): @code{boole_clr}, @code{boole_set},
2010 @code{boole_1}, @code{boole_2}, @code{boole_c1}, @code{boole_c2},
2011 @code{boole_and}, @code{boole_ior}, @code{boole_xor}, @code{boole_eqv},
2012 @code{boole_nand}, @code{boole_nor}, @code{boole_andc1}, @code{boole_andc2},
2013 @code{boole_orc1}, @code{boole_orc2}.
2014 @cindex @code{boole_clr}
2015 @cindex @code{boole_set}
2016 @cindex @code{boole_1}
2017 @cindex @code{boole_2}
2018 @cindex @code{boole_c1}
2019 @cindex @code{boole_c2}
2020 @cindex @code{boole_and}
2021 @cindex @code{boole_xor}
2022 @cindex @code{boole_eqv}
2023 @cindex @code{boole_nand}
2024 @cindex @code{boole_nor}
2025 @cindex @code{boole_andc1}
2026 @cindex @code{boole_andc2}
2027 @cindex @code{boole_orc1}
2028 @cindex @code{boole_orc2}
2031 Other functions that view integers as bit strings:
2034 @item bool logtest (const cl_I& x, const cl_I& y)
2035 @cindex @code{logtest ()}
2036 Returns true if some bit is set in both @code{x} and @code{y}, i.e. if
2037 @code{logand(x,y) != 0}.
2039 @item bool logbitp (const cl_I& n, const cl_I& x)
2040 @cindex @code{logbitp ()}
2041 Returns true if the @code{n}th bit (from the right) of @code{x} is set.
2042 Bit 0 is the least significant bit.
2044 @item uintC logcount (const cl_I& x)
2045 @cindex @code{logcount ()}
2046 Returns the number of one bits in @code{x}, if @code{x} >= 0, or
2047 the number of zero bits in @code{x}, if @code{x} < 0.
2050 The following functions operate on intervals of bits in integers.
2053 struct cl_byte @{ uintC size; uintC position; @};
2055 @cindex @code{cl_byte}
2056 represents the bit interval containing the bits
2057 @code{position}@dots{}@code{position+size-1} of an integer.
2058 The constructor @code{cl_byte(size,position)} constructs a @code{cl_byte}.
2061 @item cl_I ldb (const cl_I& n, const cl_byte& b)
2062 @cindex @code{ldb ()}
2063 extracts the bits of @code{n} described by the bit interval @code{b}
2064 and returns them as a nonnegative integer with @code{b.size} bits.
2066 @item bool ldb_test (const cl_I& n, const cl_byte& b)
2067 @cindex @code{ldb_test ()}
2068 Returns true if some bit described by the bit interval @code{b} is set in
2071 @item cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
2072 @cindex @code{dpb ()}
2073 Returns @code{n}, with the bits described by the bit interval @code{b}
2074 replaced by @code{newbyte}. Only the lowest @code{b.size} bits of
2075 @code{newbyte} are relevant.
2078 The functions @code{ldb} and @code{dpb} implicitly shift. The following
2079 functions are their counterparts without shifting:
2082 @item cl_I mask_field (const cl_I& n, const cl_byte& b)
2083 @cindex @code{mask_field ()}
2084 returns an integer with the bits described by the bit interval @code{b}
2085 copied from the corresponding bits in @code{n}, the other bits zero.
2087 @item cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
2088 @cindex @code{deposit_field ()}
2089 returns an integer where the bits described by the bit interval @code{b}
2090 come from @code{newbyte} and the other bits come from @code{n}.
2093 The following relations hold:
2097 @code{ldb (n, b) = mask_field(n, b) >> b.position},
2099 @code{dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)},
2101 @code{deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)}.
2104 The following operations on integers as bit strings are efficient shortcuts
2105 for common arithmetic operations:
2108 @item bool oddp (const cl_I& x)
2109 @cindex @code{oddp ()}
2110 Returns true if the least significant bit of @code{x} is 1. Equivalent to
2111 @code{mod(x,2) != 0}.
2113 @item bool evenp (const cl_I& x)
2114 @cindex @code{evenp ()}
2115 Returns true if the least significant bit of @code{x} is 0. Equivalent to
2116 @code{mod(x,2) == 0}.
2118 @item cl_I operator << (const cl_I& x, const cl_I& n)
2119 @cindex @code{operator << ()}
2120 Shifts @code{x} by @code{n} bits to the left. @code{n} should be >=0.
2121 Equivalent to @code{x * expt(2,n)}.
2123 @item cl_I operator >> (const cl_I& x, const cl_I& n)
2124 @cindex @code{operator >> ()}
2125 Shifts @code{x} by @code{n} bits to the right. @code{n} should be >=0.
2126 Bits shifted out to the right are thrown away.
2127 Equivalent to @code{floor(x / expt(2,n))}.
2129 @item cl_I ash (const cl_I& x, const cl_I& y)
2130 @cindex @code{ash ()}
2131 Shifts @code{x} by @code{y} bits to the left (if @code{y}>=0) or
2132 by @code{-y} bits to the right (if @code{y}<=0). In other words, this
2133 returns @code{floor(x * expt(2,y))}.
2135 @item uintC integer_length (const cl_I& x)
2136 @cindex @code{integer_length ()}
2137 Returns the number of bits (excluding the sign bit) needed to represent @code{x}
2138 in two's complement notation. This is the smallest n >= 0 such that
2139 -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
2142 @item uintC ord2 (const cl_I& x)
2143 @cindex @code{ord2 ()}
2144 @code{x} must be non-zero. This function returns the number of 0 bits at the
2145 right of @code{x} in two's complement notation. This is the largest n >= 0
2146 such that 2^n divides @code{x}.
2148 @item uintC power2p (const cl_I& x)
2149 @cindex @code{power2p ()}
2150 @code{x} must be > 0. This function checks whether @code{x} is a power of 2.
2151 If @code{x} = 2^(n-1), it returns n. Else it returns 0.
2152 (See also the function @code{logp}.)
2156 @node Number theoretic functions
2157 @subsection Number theoretic functions
2160 @item uint32 gcd (unsigned long a, unsigned long b)
2161 @cindex @code{gcd ()}
2162 @itemx cl_I gcd (const cl_I& a, const cl_I& b)
2163 This function returns the greatest common divisor of @code{a} and @code{b},
2164 normalized to be >= 0.
2166 @item cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)
2167 @cindex @code{xgcd ()}
2168 This function (``extended gcd'') returns the greatest common divisor @code{g} of
2169 @code{a} and @code{b} and at the same time the representation of @code{g}
2170 as an integral linear combination of @code{a} and @code{b}:
2171 @code{u} and @code{v} with @code{u*a+v*b = g}, @code{g} >= 0.
2172 @code{u} and @code{v} will be normalized to be of smallest possible absolute
2173 value, in the following sense: If @code{a} and @code{b} are non-zero, and
2174 @code{abs(a) != abs(b)}, @code{u} and @code{v} will satisfy the inequalities
2175 @code{abs(u) <= abs(b)/(2*g)}, @code{abs(v) <= abs(a)/(2*g)}.
2177 @item cl_I lcm (const cl_I& a, const cl_I& b)
2178 @cindex @code{lcm ()}
2179 This function returns the least common multiple of @code{a} and @code{b},
2180 normalized to be >= 0.
2182 @item bool logp (const cl_I& a, const cl_I& b, cl_RA* l)
2183 @cindex @code{logp ()}
2184 @itemx bool logp (const cl_RA& a, const cl_RA& b, cl_RA* l)
2185 @code{a} must be > 0. @code{b} must be >0 and != 1. If log(a,b) is
2186 rational number, this function returns true and sets *l = log(a,b), else
2189 @item int jacobi (signed long a, signed long b)
2190 @cindex @code{jacobi()}
2191 @itemx int jacobi (const cl_I& a, const cl_I& b)
2192 Returns the Jacobi symbol
2194 $\left({a\over b}\right)$,
2199 @code{a,b} must be integers, @code{b>0} and odd. The result is 0
2202 @item bool isprobprime (const cl_I& n)
2204 @cindex @code{isprobprime()}
2205 Returns true if @code{n} is a small prime or passes the Miller-Rabin
2206 primality test. The probability of a false positive is 1:10^30.
2208 @item cl_I nextprobprime (const cl_R& x)
2209 @cindex @code{nextprobprime()}
2210 Returns the smallest probable prime >=@code{x}.
2214 @node Combinatorial functions
2215 @subsection Combinatorial functions
2218 @item cl_I factorial (uintL n)
2219 @cindex @code{factorial ()}
2220 @code{n} must be a small integer >= 0. This function returns the factorial
2221 @code{n}! = @code{1*2*@dots{}*n}.
2223 @item cl_I doublefactorial (uintL n)
2224 @cindex @code{doublefactorial ()}
2225 @code{n} must be a small integer >= 0. This function returns the
2226 doublefactorial @code{n}!! = @code{1*3*@dots{}*n} or
2227 @code{n}!! = @code{2*4*@dots{}*n}, respectively.
2229 @item cl_I binomial (uintL n, uintL k)
2230 @cindex @code{binomial ()}
2231 @code{n} and @code{k} must be small integers >= 0. This function returns the
2232 binomial coefficient
2234 ${n \choose k} = {n! \over k! (n-k)!}$
2237 (@code{n} choose @code{k}) = @code{n}! / @code{k}! @code{(n-k)}!
2239 for 0 <= k <= n, 0 else.
2243 @node Functions on floating-point numbers
2244 @section Functions on floating-point numbers
2246 Recall that a floating-point number consists of a sign @code{s}, an
2247 exponent @code{e} and a mantissa @code{m}. The value of the number is
2248 @code{(-1)^s * 2^e * m}.
2251 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
2252 defines the following operations.
2255 @item @var{type} scale_float (const @var{type}& x, sintC delta)
2256 @cindex @code{scale_float ()}
2257 @itemx @var{type} scale_float (const @var{type}& x, const cl_I& delta)
2258 Returns @code{x*2^delta}. This is more efficient than an explicit multiplication
2259 because it copies @code{x} and modifies the exponent.
2262 The following functions provide an abstract interface to the underlying
2263 representation of floating-point numbers.
2266 @item sintE float_exponent (const @var{type}& x)
2267 @cindex @code{float_exponent ()}
2268 Returns the exponent @code{e} of @code{x}.
2269 For @code{x = 0.0}, this is 0. For @code{x} non-zero, this is the unique
2270 integer with @code{2^(e-1) <= abs(x) < 2^e}.
2272 @item sintL float_radix (const @var{type}& x)
2273 @cindex @code{float_radix ()}
2274 Returns the base of the floating-point representation. This is always @code{2}.
2276 @item @var{type} float_sign (const @var{type}& x)
2277 @cindex @code{float_sign ()}
2278 Returns the sign @code{s} of @code{x} as a float. The value is 1 for
2279 @code{x} >= 0, -1 for @code{x} < 0.
2281 @item uintC float_digits (const @var{type}& x)
2282 @cindex @code{float_digits ()}
2283 Returns the number of mantissa bits in the floating-point representation
2284 of @code{x}, including the hidden bit. The value only depends on the type
2285 of @code{x}, not on its value.
2287 @item uintC float_precision (const @var{type}& x)
2288 @cindex @code{float_precision ()}
2289 Returns the number of significant mantissa bits in the floating-point
2290 representation of @code{x}. Since denormalized numbers are not supported,
2291 this is the same as @code{float_digits(x)} if @code{x} is non-zero, and
2295 The complete internal representation of a float is encoded in the type
2296 @cindex @code{decoded_float}
2297 @cindex @code{decoded_sfloat}
2298 @cindex @code{decoded_ffloat}
2299 @cindex @code{decoded_dfloat}
2300 @cindex @code{decoded_lfloat}
2301 @code{decoded_float} (or @code{decoded_sfloat}, @code{decoded_ffloat},
2302 @code{decoded_dfloat}, @code{decoded_lfloat}, respectively), defined by
2304 struct decoded_@var{type}float @{
2305 @var{type} mantissa; cl_I exponent; @var{type} sign;
2309 and returned by the function
2312 @item decoded_@var{type}float decode_float (const @var{type}& x)
2313 @cindex @code{decode_float ()}
2314 For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
2315 @code{x = (-1)^s * 2^e * m} and @code{0.5 <= m < 1.0}. For @code{x} = 0,
2316 it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
2317 @code{e} is the same as returned by the function @code{float_exponent}.
2320 A complete decoding in terms of integers is provided as type
2321 @cindex @code{cl_idecoded_float}
2323 struct cl_idecoded_float @{
2324 cl_I mantissa; cl_I exponent; cl_I sign;
2327 by the following function:
2330 @item cl_idecoded_float integer_decode_float (const @var{type}& x)
2331 @cindex @code{integer_decode_float ()}
2332 For @code{x} non-zero, this returns @code{(-1)^s}, @code{e}, @code{m} with
2333 @code{x = (-1)^s * 2^e * m} and @code{m} an integer with @code{float_digits(x)}
2334 bits. For @code{x} = 0, it returns @code{(-1)^s}=1, @code{e}=0, @code{m}=0.
2335 WARNING: The exponent @code{e} is not the same as the one returned by
2336 the functions @code{decode_float} and @code{float_exponent}.
2339 Some other function, implemented only for class @code{cl_F}:
2342 @item cl_F float_sign (const cl_F& x, const cl_F& y)
2343 @cindex @code{float_sign ()}
2344 This returns a floating point number whose precision and absolute value
2345 is that of @code{y} and whose sign is that of @code{x}. If @code{x} is
2346 zero, it is treated as positive. Same for @code{y}.
2350 @node Conversion functions
2351 @section Conversion functions
2355 * Conversion to floating-point numbers::
2356 * Conversion to rational numbers::
2359 @node Conversion to floating-point numbers
2360 @subsection Conversion to floating-point numbers
2362 The type @code{float_format_t} describes a floating-point format.
2363 @cindex @code{float_format_t}
2366 @item float_format_t float_format (uintE n)
2367 @cindex @code{float_format ()}
2368 Returns the smallest float format which guarantees at least @code{n}
2369 decimal digits in the mantissa (after the decimal point).
2371 @item float_format_t float_format (const cl_F& x)
2372 Returns the floating point format of @code{x}.
2374 @item float_format_t default_float_format
2375 @cindex @code{default_float_format}
2376 Global variable: the default float format used when converting rational numbers
2380 To convert a real number to a float, each of the types
2381 @code{cl_R}, @code{cl_F}, @code{cl_I}, @code{cl_RA},
2382 @code{int}, @code{unsigned int}, @code{float}, @code{double}
2383 defines the following operations:
2386 @item cl_F cl_float (const @var{type}&x, float_format_t f)
2387 @cindex @code{cl_float ()}
2388 Returns @code{x} as a float of format @code{f}.
2389 @item cl_F cl_float (const @var{type}&x, const cl_F& y)
2390 Returns @code{x} in the float format of @code{y}.
2391 @item cl_F cl_float (const @var{type}&x)
2392 Returns @code{x} as a float of format @code{default_float_format} if
2393 it is an exact number, or @code{x} itself if it is already a float.
2396 Of course, converting a number to a float can lose precision.
2398 Every floating-point format has some characteristic numbers:
2401 @item cl_F most_positive_float (float_format_t f)
2402 @cindex @code{most_positive_float ()}
2403 Returns the largest (most positive) floating point number in float format @code{f}.
2405 @item cl_F most_negative_float (float_format_t f)
2406 @cindex @code{most_negative_float ()}
2407 Returns the smallest (most negative) floating point number in float format @code{f}.
2409 @item cl_F least_positive_float (float_format_t f)
2410 @cindex @code{least_positive_float ()}
2411 Returns the least positive floating point number (i.e. > 0 but closest to 0)
2412 in float format @code{f}.
2414 @item cl_F least_negative_float (float_format_t f)
2415 @cindex @code{least_negative_float ()}
2416 Returns the least negative floating point number (i.e. < 0 but closest to 0)
2417 in float format @code{f}.
2419 @item cl_F float_epsilon (float_format_t f)
2420 @cindex @code{float_epsilon ()}
2421 Returns the smallest floating point number e > 0 such that @code{1+e != 1}.
2423 @item cl_F float_negative_epsilon (float_format_t f)
2424 @cindex @code{float_negative_epsilon ()}
2425 Returns the smallest floating point number e > 0 such that @code{1-e != 1}.
2429 @node Conversion to rational numbers
2430 @subsection Conversion to rational numbers
2432 Each of the classes @code{cl_R}, @code{cl_RA}, @code{cl_F}
2433 defines the following operation:
2436 @item cl_RA rational (const @var{type}& x)
2437 @cindex @code{rational ()}
2438 Returns the value of @code{x} as an exact number. If @code{x} is already
2439 an exact number, this is @code{x}. If @code{x} is a floating-point number,
2440 the value is a rational number whose denominator is a power of 2.
2443 In order to convert back, say, @code{(cl_F)(cl_R)"1/3"} to @code{1/3}, there is
2447 @item cl_RA rationalize (const cl_R& x)
2448 @cindex @code{rationalize ()}
2449 If @code{x} is a floating-point number, it actually represents an interval
2450 of real numbers, and this function returns the rational number with
2451 smallest denominator (and smallest numerator, in magnitude)
2452 which lies in this interval.
2453 If @code{x} is already an exact number, this function returns @code{x}.
2456 If @code{x} is any float, one has
2460 @code{cl_float(rational(x),x) = x}
2462 @code{cl_float(rationalize(x),x) = x}
2466 @node Random number generators
2467 @section Random number generators
2470 A random generator is a machine which produces (pseudo-)random numbers.
2471 The include file @code{<cln/random.h>} defines a class @code{random_state}
2472 which contains the state of a random generator. If you make a copy
2473 of the random number generator, the original one and the copy will produce
2474 the same sequence of random numbers.
2476 The following functions return (pseudo-)random numbers in different formats.
2477 Calling one of these modifies the state of the random number generator in
2478 a complicated but deterministic way.
2481 @cindex @code{random_state}
2482 @cindex @code{default_random_state}
2484 random_state default_random_state
2486 contains a default random number generator. It is used when the functions
2487 below are called without @code{random_state} argument.
2490 @item uint32 random32 (random_state& randomstate)
2491 @itemx uint32 random32 ()
2492 @cindex @code{random32 ()}
2493 Returns a random unsigned 32-bit number. All bits are equally random.
2495 @item cl_I random_I (random_state& randomstate, const cl_I& n)
2496 @itemx cl_I random_I (const cl_I& n)
2497 @cindex @code{random_I ()}
2498 @code{n} must be an integer > 0. This function returns a random integer @code{x}
2499 in the range @code{0 <= x < n}.
2501 @item cl_F random_F (random_state& randomstate, const cl_F& n)
2502 @itemx cl_F random_F (const cl_F& n)
2503 @cindex @code{random_F ()}
2504 @code{n} must be a float > 0. This function returns a random floating-point
2505 number of the same format as @code{n} in the range @code{0 <= x < n}.
2507 @item cl_R random_R (random_state& randomstate, const cl_R& n)
2508 @itemx cl_R random_R (const cl_R& n)
2509 @cindex @code{random_R ()}
2510 Behaves like @code{random_I} if @code{n} is an integer and like @code{random_F}
2511 if @code{n} is a float.
2515 @node Modifying operators
2516 @section Modifying operators
2517 @cindex modifying operators
2519 The modifying C/C++ operators @code{+=}, @code{-=}, @code{*=}, @code{/=},
2520 @code{&=}, @code{|=}, @code{^=}, @code{<<=}, @code{>>=}
2523 For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA},
2524 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
2527 @item @var{type}& operator += (@var{type}&, const @var{type}&)
2528 @cindex @code{operator += ()}
2529 @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
2530 @cindex @code{operator -= ()}
2531 @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
2532 @cindex @code{operator *= ()}
2533 @itemx @var{type}& operator /= (@var{type}&, const @var{type}&)
2534 @cindex @code{operator /= ()}
2537 For the class @code{cl_I}:
2540 @item @var{type}& operator += (@var{type}&, const @var{type}&)
2541 @itemx @var{type}& operator -= (@var{type}&, const @var{type}&)
2542 @itemx @var{type}& operator *= (@var{type}&, const @var{type}&)
2543 @itemx @var{type}& operator &= (@var{type}&, const @var{type}&)
2544 @cindex @code{operator &= ()}
2545 @itemx @var{type}& operator |= (@var{type}&, const @var{type}&)
2546 @cindex @code{operator |= ()}
2547 @itemx @var{type}& operator ^= (@var{type}&, const @var{type}&)
2548 @cindex @code{operator ^= ()}
2549 @itemx @var{type}& operator <<= (@var{type}&, const @var{type}&)
2550 @cindex @code{operator <<= ()}
2551 @itemx @var{type}& operator >>= (@var{type}&, const @var{type}&)
2552 @cindex @code{operator >>= ()}
2555 For the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
2556 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}:
2559 @item @var{type}& operator ++ (@var{type}& x)
2560 @cindex @code{operator ++ ()}
2561 The prefix operator @code{++x}.
2563 @item void operator ++ (@var{type}& x, int)
2564 The postfix operator @code{x++}.
2566 @item @var{type}& operator -- (@var{type}& x)
2567 @cindex @code{operator -- ()}
2568 The prefix operator @code{--x}.
2570 @item void operator -- (@var{type}& x, int)
2571 The postfix operator @code{x--}.
2574 Note that by using these modifying operators, you don't gain efficiency:
2575 In CLN @samp{x += y;} is exactly the same as @samp{x = x+y;}, not more
2580 @chapter Input/Output
2581 @cindex Input/Output
2584 * Internal and printed representation::
2586 * Output functions::
2589 @node Internal and printed representation
2590 @section Internal and printed representation
2591 @cindex representation
2593 All computations deal with the internal representations of the numbers.
2595 Every number has an external representation as a sequence of ASCII characters.
2596 Several external representations may denote the same number, for example,
2597 "20.0" and "20.000".
2599 Converting an internal to an external representation is called ``printing'',
2601 converting an external to an internal representation is called ``reading''.
2603 In CLN, it is always true that conversion of an internal to an external
2604 representation and then back to an internal representation will yield the
2605 same internal representation. Symbolically: @code{read(print(x)) == x}.
2606 This is called ``print-read consistency''.
2608 Different types of numbers have different external representations (case
2613 External representation: @var{sign}@{@var{digit}@}+. The reader also accepts the
2614 Common Lisp syntaxes @var{sign}@{@var{digit}@}+@code{.} with a trailing dot
2615 for decimal integers
2616 and the @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes.
2618 @item Rational numbers
2619 External representation: @var{sign}@{@var{digit}@}+@code{/}@{@var{digit}@}+.
2620 The @code{#@var{n}R}, @code{#b}, @code{#o}, @code{#x} prefixes are allowed
2623 @item Floating-point numbers
2624 External representation: @var{sign}@{@var{digit}@}*@var{exponent} or
2625 @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}*@var{exponent} or
2626 @var{sign}@{@var{digit}@}*@code{.}@{@var{digit}@}+. A precision specifier
2627 of the form _@var{prec} may be appended. There must be at least
2628 one digit in the non-exponent part. The exponent has the syntax
2629 @var{expmarker} @var{expsign} @{@var{digit}@}+.
2630 The exponent marker is
2634 @samp{s} for short-floats,
2636 @samp{f} for single-floats,
2638 @samp{d} for double-floats,
2640 @samp{L} for long-floats,
2643 or @samp{e}, which denotes a default float format. The precision specifying
2644 suffix has the syntax _@var{prec} where @var{prec} denotes the number of
2645 valid mantissa digits (in decimal, excluding leading zeroes), cf. also
2646 function @samp{float_format}.
2648 @item Complex numbers
2649 External representation:
2652 In algebraic notation: @code{@var{realpart}+@var{imagpart}i}. Of course,
2653 if @var{imagpart} is negative, its printed representation begins with
2654 a @samp{-}, and the @samp{+} between @var{realpart} and @var{imagpart}
2655 may be omitted. Note that this notation cannot be used when the @var{imagpart}
2656 is rational and the rational number's base is >18, because the @samp{i}
2657 is then read as a digit.
2659 In Common Lisp notation: @code{#C(@var{realpart} @var{imagpart})}.
2664 @node Input functions
2665 @section Input functions
2667 Including @code{<cln/io.h>} defines flexible input functions:
2670 @item cl_N read_complex (std::istream& stream, const cl_read_flags& flags)
2671 @itemx cl_R read_real (std::istream& stream, const cl_read_flags& flags)
2672 @itemx cl_F read_float (std::istream& stream, const cl_read_flags& flags)
2673 @itemx cl_RA read_rational (std::istream& stream, const cl_read_flags& flags)
2674 @itemx cl_I read_integer (std::istream& stream, const cl_read_flags& flags)
2675 Reads a number from @code{stream}. The @code{flags} are parameters which
2676 affect the input syntax. Whitespace before the number is silently skipped.
2678 @item cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
2679 @itemx cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
2680 @itemx cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
2681 @itemx cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
2682 @itemx cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)
2683 Reads a number from a string in memory. The @code{flags} are parameters which
2684 affect the input syntax. The string starts at @code{string} and ends at
2685 @code{string_limit} (exclusive limit). @code{string_limit} may also be
2686 @code{NULL}, denoting the entire string, i.e. equivalent to
2687 @code{string_limit = string + strlen(string)}. If @code{end_of_parse} is
2688 @code{NULL}, the string in memory must contain exactly one number and nothing
2689 more, else an exception will be thrown. If @code{end_of_parse}
2690 is not @code{NULL}, @code{*end_of_parse} will be assigned a pointer past
2691 the last parsed character (i.e. @code{string_limit} if nothing came after
2692 the number). Whitespace is not allowed.
2695 The structure @code{cl_read_flags} contains the following fields:
2698 @item cl_read_syntax_t syntax
2699 The possible results of the read operation. Possible values are
2700 @code{syntax_number}, @code{syntax_real}, @code{syntax_rational},
2701 @code{syntax_integer}, @code{syntax_float}, @code{syntax_sfloat},
2702 @code{syntax_ffloat}, @code{syntax_dfloat}, @code{syntax_lfloat}.
2704 @item cl_read_lsyntax_t lsyntax
2705 Specifies the language-dependent syntax variant for the read operation.
2709 @item lsyntax_standard
2710 accept standard algebraic notation only, no complex numbers,
2711 @item lsyntax_algebraic
2712 accept the algebraic notation @code{@var{x}+@var{y}i} for complex numbers,
2713 @item lsyntax_commonlisp
2714 accept the @code{#b}, @code{#o}, @code{#x} syntaxes for binary, octal,
2715 hexadecimal numbers,
2716 @code{#@var{base}R} for rational numbers in a given base,
2717 @code{#c(@var{realpart} @var{imagpart})} for complex numbers,
2719 accept all of these extensions.
2722 @item unsigned int rational_base
2723 The base in which rational numbers are read.
2725 @item float_format_t float_flags.default_float_format
2726 The float format used when reading floats with exponent marker @samp{e}.
2728 @item float_format_t float_flags.default_lfloat_format
2729 The float format used when reading floats with exponent marker @samp{l}.
2731 @item bool float_flags.mantissa_dependent_float_format
2732 When this flag is true, floats specified with more digits than corresponding
2733 to the exponent marker they contain, but without @var{_nnn} suffix, will get a
2734 precision corresponding to their number of significant digits.
2738 @node Output functions
2739 @section Output functions
2741 Including @code{<cln/io.h>} defines a number of simple output functions
2742 that write to @code{std::ostream&}:
2745 @item void fprintchar (std::ostream& stream, char c)
2746 Prints the character @code{x} literally on the @code{stream}.
2748 @item void fprint (std::ostream& stream, const char * string)
2749 Prints the @code{string} literally on the @code{stream}.
2751 @item void fprintdecimal (std::ostream& stream, int x)
2752 @itemx void fprintdecimal (std::ostream& stream, const cl_I& x)
2753 Prints the integer @code{x} in decimal on the @code{stream}.
2755 @item void fprintbinary (std::ostream& stream, const cl_I& x)
2756 Prints the integer @code{x} in binary (base 2, without prefix)
2757 on the @code{stream}.
2759 @item void fprintoctal (std::ostream& stream, const cl_I& x)
2760 Prints the integer @code{x} in octal (base 8, without prefix)
2761 on the @code{stream}.
2763 @item void fprinthexadecimal (std::ostream& stream, const cl_I& x)
2764 Prints the integer @code{x} in hexadecimal (base 16, without prefix)
2765 on the @code{stream}.
2768 Each of the classes @code{cl_N}, @code{cl_R}, @code{cl_RA}, @code{cl_I},
2769 @code{cl_F}, @code{cl_SF}, @code{cl_FF}, @code{cl_DF}, @code{cl_LF}
2770 defines, in @code{<cln/@var{type}_io.h>}, the following output functions:
2773 @item void fprint (std::ostream& stream, const @var{type}& x)
2774 @itemx std::ostream& operator<< (std::ostream& stream, const @var{type}& x)
2775 Prints the number @code{x} on the @code{stream}. The output may depend
2776 on the global printer settings in the variable @code{default_print_flags}.
2777 The @code{ostream} flags and settings (flags, width and locale) are
2781 The most flexible output function, defined in @code{<cln/@var{type}_io.h>},
2784 void print_complex (std::ostream& stream, const cl_print_flags& flags,
2786 void print_real (std::ostream& stream, const cl_print_flags& flags,
2788 void print_float (std::ostream& stream, const cl_print_flags& flags,
2790 void print_rational (std::ostream& stream, const cl_print_flags& flags,
2792 void print_integer (std::ostream& stream, const cl_print_flags& flags,
2795 Prints the number @code{x} on the @code{stream}. The @code{flags} are
2796 parameters which affect the output.
2798 The structure type @code{cl_print_flags} contains the following fields:
2801 @item unsigned int rational_base
2802 The base in which rational numbers are printed. Default is @code{10}.
2804 @item bool rational_readably
2805 If this flag is true, rational numbers are printed with radix specifiers in
2806 Common Lisp syntax (@code{#@var{n}R} or @code{#b} or @code{#o} or @code{#x}
2807 prefixes, trailing dot). Default is false.
2809 @item bool float_readably
2810 If this flag is true, type specific exponent markers have precedence over 'E'.
2813 @item float_format_t default_float_format
2814 Floating point numbers of this format will be printed using the 'E' exponent
2815 marker. Default is @code{float_format_ffloat}.
2817 @item bool complex_readably
2818 If this flag is true, complex numbers will be printed using the Common Lisp
2819 syntax @code{#C(@var{realpart} @var{imagpart})}. Default is false.
2821 @item cl_string univpoly_varname
2822 Univariate polynomials with no explicit indeterminate name will be printed
2823 using this variable name. Default is @code{"x"}.
2826 The global variable @code{default_print_flags} contains the default values,
2827 used by the function @code{fprint}.
2833 CLN has a class of abstract rings.
2841 Rings can be compared for equality:
2844 @item bool operator== (const cl_ring&, const cl_ring&)
2845 @itemx bool operator!= (const cl_ring&, const cl_ring&)
2846 These compare two rings for equality.
2849 Given a ring @code{R}, the following members can be used.
2852 @item void R->fprint (std::ostream& stream, const cl_ring_element& x)
2853 @cindex @code{fprint ()}
2854 @itemx bool R->equal (const cl_ring_element& x, const cl_ring_element& y)
2855 @cindex @code{equal ()}
2856 @itemx cl_ring_element R->zero ()
2857 @cindex @code{zero ()}
2858 @itemx bool R->zerop (const cl_ring_element& x)
2859 @cindex @code{zerop ()}
2860 @itemx cl_ring_element R->plus (const cl_ring_element& x, const cl_ring_element& y)
2861 @cindex @code{plus ()}
2862 @itemx cl_ring_element R->minus (const cl_ring_element& x, const cl_ring_element& y)
2863 @cindex @code{minus ()}
2864 @itemx cl_ring_element R->uminus (const cl_ring_element& x)
2865 @cindex @code{uminus ()}
2866 @itemx cl_ring_element R->one ()
2867 @cindex @code{one ()}
2868 @itemx cl_ring_element R->canonhom (const cl_I& x)
2869 @cindex @code{canonhom ()}
2870 @itemx cl_ring_element R->mul (const cl_ring_element& x, const cl_ring_element& y)
2871 @cindex @code{mul ()}
2872 @itemx cl_ring_element R->square (const cl_ring_element& x)
2873 @cindex @code{square ()}
2874 @itemx cl_ring_element R->expt_pos (const cl_ring_element& x, const cl_I& y)
2875 @cindex @code{expt_pos ()}
2878 The following rings are built-in.
2881 @item cl_null_ring cl_0_ring
2882 The null ring, containing only zero.
2884 @item cl_complex_ring cl_C_ring
2885 The ring of complex numbers. This corresponds to the type @code{cl_N}.
2887 @item cl_real_ring cl_R_ring
2888 The ring of real numbers. This corresponds to the type @code{cl_R}.
2890 @item cl_rational_ring cl_RA_ring
2891 The ring of rational numbers. This corresponds to the type @code{cl_RA}.
2893 @item cl_integer_ring cl_I_ring
2894 The ring of integers. This corresponds to the type @code{cl_I}.
2897 Type tests can be performed for any of @code{cl_C_ring}, @code{cl_R_ring},
2898 @code{cl_RA_ring}, @code{cl_I_ring}:
2901 @item bool instanceof (const cl_number& x, const cl_number_ring& R)
2902 @cindex @code{instanceof ()}
2903 Tests whether the given number is an element of the number ring R.
2907 @node Modular integers
2908 @chapter Modular integers
2909 @cindex modular integer
2912 * Modular integer rings::
2913 * Functions on modular integers::
2916 @node Modular integer rings
2917 @section Modular integer rings
2920 CLN implements modular integers, i.e. integers modulo a fixed integer N.
2921 The modulus is explicitly part of every modular integer. CLN doesn't
2922 allow you to (accidentally) mix elements of different modular rings,
2923 e.g. @code{(3 mod 4) + (2 mod 5)} will result in a runtime error.
2924 (Ideally one would imagine a generic data type @code{cl_MI(N)}, but C++
2925 doesn't have generic types. So one has to live with runtime checks.)
2927 The class of modular integer rings is
2935 Modular integer ring
2939 @cindex @code{cl_modint_ring}
2941 and the class of all modular integers (elements of modular integer rings) is
2949 Modular integer rings are constructed using the function
2952 @item cl_modint_ring find_modint_ring (const cl_I& N)
2953 @cindex @code{find_modint_ring ()}
2954 This function returns the modular ring @samp{Z/NZ}. It takes care
2955 of finding out about special cases of @code{N}, like powers of two
2956 and odd numbers for which Montgomery multiplication will be a win,
2957 @cindex Montgomery multiplication
2958 and precomputes any necessary auxiliary data for computing modulo @code{N}.
2959 There is a cache table of rings, indexed by @code{N} (or, more precisely,
2960 by @code{abs(N)}). This ensures that the precomputation costs are reduced
2964 Modular integer rings can be compared for equality:
2967 @item bool operator== (const cl_modint_ring&, const cl_modint_ring&)
2968 @cindex @code{operator == ()}
2969 @itemx bool operator!= (const cl_modint_ring&, const cl_modint_ring&)
2970 @cindex @code{operator != ()}
2971 These compare two modular integer rings for equality. Two different calls
2972 to @code{find_modint_ring} with the same argument necessarily return the
2973 same ring because it is memoized in the cache table.
2976 @node Functions on modular integers
2977 @section Functions on modular integers
2979 Given a modular integer ring @code{R}, the following members can be used.
2982 @item cl_I R->modulus
2983 @cindex @code{modulus}
2984 This is the ring's modulus, normalized to be nonnegative: @code{abs(N)}.
2986 @item cl_MI R->zero()
2987 @cindex @code{zero ()}
2988 This returns @code{0 mod N}.
2990 @item cl_MI R->one()
2991 @cindex @code{one ()}
2992 This returns @code{1 mod N}.
2994 @item cl_MI R->canonhom (const cl_I& x)
2995 @cindex @code{canonhom ()}
2996 This returns @code{x mod N}.
2998 @item cl_I R->retract (const cl_MI& x)
2999 @cindex @code{retract ()}
3000 This is a partial inverse function to @code{R->canonhom}. It returns the
3001 standard representative (@code{>=0}, @code{<N}) of @code{x}.
3003 @item cl_MI R->random(random_state& randomstate)
3004 @itemx cl_MI R->random()
3005 @cindex @code{random ()}
3006 This returns a random integer modulo @code{N}.
3009 The following operations are defined on modular integers.
3012 @item cl_modint_ring x.ring ()
3013 @cindex @code{ring ()}
3014 Returns the ring to which the modular integer @code{x} belongs.
3016 @item cl_MI operator+ (const cl_MI&, const cl_MI&)
3017 @cindex @code{operator + ()}
3018 Returns the sum of two modular integers. One of the arguments may also
3021 @item cl_MI operator- (const cl_MI&, const cl_MI&)
3022 @cindex @code{operator - ()}
3023 Returns the difference of two modular integers. One of the arguments may also
3026 @item cl_MI operator- (const cl_MI&)
3027 Returns the negative of a modular integer.
3029 @item cl_MI operator* (const cl_MI&, const cl_MI&)
3030 @cindex @code{operator * ()}
3031 Returns the product of two modular integers. One of the arguments may also
3034 @item cl_MI square (const cl_MI&)
3035 @cindex @code{square ()}
3036 Returns the square of a modular integer.
3038 @item cl_MI recip (const cl_MI& x)
3039 @cindex @code{recip ()}
3040 Returns the reciprocal @code{x^-1} of a modular integer @code{x}. @code{x}
3041 must be coprime to the modulus, otherwise an error message is issued.
3043 @item cl_MI div (const cl_MI& x, const cl_MI& y)
3044 @cindex @code{div ()}
3045 Returns the quotient @code{x*y^-1} of two modular integers @code{x}, @code{y}.
3046 @code{y} must be coprime to the modulus, otherwise an error message is issued.
3048 @item cl_MI expt_pos (const cl_MI& x, const cl_I& y)
3049 @cindex @code{expt_pos ()}
3050 @code{y} must be > 0. Returns @code{x^y}.
3052 @item cl_MI expt (const cl_MI& x, const cl_I& y)
3053 @cindex @code{expt ()}
3054 Returns @code{x^y}. If @code{y} is negative, @code{x} must be coprime to the
3055 modulus, else an error message is issued.
3057 @item cl_MI operator<< (const cl_MI& x, const cl_I& y)
3058 @cindex @code{operator << ()}
3059 Returns @code{x*2^y}.
3061 @item cl_MI operator>> (const cl_MI& x, const cl_I& y)
3062 @cindex @code{operator >> ()}
3063 Returns @code{x*2^-y}. When @code{y} is positive, the modulus must be odd,
3064 or an error message is issued.
3066 @item bool operator== (const cl_MI&, const cl_MI&)
3067 @cindex @code{operator == ()}
3068 @itemx bool operator!= (const cl_MI&, const cl_MI&)
3069 @cindex @code{operator != ()}
3070 Compares two modular integers, belonging to the same modular integer ring,
3073 @item bool zerop (const cl_MI& x)
3074 @cindex @code{zerop ()}
3075 Returns true if @code{x} is @code{0 mod N}.
3078 The following output functions are defined (see also the chapter on
3082 @item void fprint (std::ostream& stream, const cl_MI& x)
3083 @cindex @code{fprint ()}
3084 @itemx std::ostream& operator<< (std::ostream& stream, const cl_MI& x)
3085 @cindex @code{operator << ()}
3086 Prints the modular integer @code{x} on the @code{stream}. The output may depend
3087 on the global printer settings in the variable @code{default_print_flags}.
3091 @node Symbolic data types
3092 @chapter Symbolic data types
3093 @cindex symbolic type
3095 CLN implements two symbolic (non-numeric) data types: strings and symbols.
3105 @cindex @code{cl_string}
3115 implements immutable strings.
3117 Strings are constructed through the following constructors:
3120 @item cl_string (const char * s)
3121 Returns an immutable copy of the (zero-terminated) C string @code{s}.
3123 @item cl_string (const char * ptr, unsigned long len)
3124 Returns an immutable copy of the @code{len} characters at
3125 @code{ptr[0]}, @dots{}, @code{ptr[len-1]}. NUL characters are allowed.
3128 The following functions are available on strings:
3132 Assignment from @code{cl_string} and @code{const char *}.
3135 @cindex @code{size()}
3137 @cindex @code{strlen ()}
3138 Returns the length of the string @code{s}.
3141 @cindex @code{operator [] ()}
3142 Returns the @code{i}th character of the string @code{s}.
3143 @code{i} must be in the range @code{0 <= i < s.size()}.
3145 @item bool equal (const cl_string& s1, const cl_string& s2)
3146 @cindex @code{equal ()}
3147 Compares two strings for equality. One of the arguments may also be a
3148 plain @code{const char *}.
3154 @cindex @code{cl_symbol}
3156 Symbols are uniquified strings: all symbols with the same name are shared.
3157 This means that comparison of two symbols is fast (effectively just a pointer
3158 comparison), whereas comparison of two strings must in the worst case walk
3159 both strings until their end.
3160 Symbols are used, for example, as tags for properties, as names of variables
3161 in polynomial rings, etc.
3163 Symbols are constructed through the following constructor:
3166 @item cl_symbol (const cl_string& s)
3167 Looks up or creates a new symbol with a given name.
3170 The following operations are available on symbols:
3173 @item cl_string (const cl_symbol& sym)
3174 Conversion to @code{cl_string}: Returns the string which names the symbol
3177 @item bool equal (const cl_symbol& sym1, const cl_symbol& sym2)
3178 @cindex @code{equal ()}
3179 Compares two symbols for equality. This is very fast.
3183 @node Univariate polynomials
3184 @chapter Univariate polynomials
3186 @cindex univariate polynomial
3189 * Univariate polynomial rings::
3190 * Functions on univariate polynomials::
3191 * Special polynomials::
3194 @node Univariate polynomial rings
3195 @section Univariate polynomial rings
3197 CLN implements univariate polynomials (polynomials in one variable) over an
3198 arbitrary ring. The indeterminate variable may be either unnamed (and will be
3199 printed according to @code{default_print_flags.univpoly_varname}, which
3200 defaults to @samp{x}) or carry a given name. The base ring and the
3201 indeterminate are explicitly part of every polynomial. CLN doesn't allow you to
3202 (accidentally) mix elements of different polynomial rings, e.g.
3203 @code{(a^2+1) * (b^3-1)} will result in a runtime error. (Ideally this should
3204 return a multivariate polynomial, but they are not yet implemented in CLN.)
3206 The classes of univariate polynomial rings are
3214 Univariate polynomial ring
3218 +----------------+-------------------+
3220 Complex polynomial ring | Modular integer polynomial ring
3221 cl_univpoly_complex_ring | cl_univpoly_modint_ring
3222 <cln/univpoly_complex.h> | <cln/univpoly_modint.h>
3226 Real polynomial ring |
3227 cl_univpoly_real_ring |
3228 <cln/univpoly_real.h> |
3232 Rational polynomial ring |
3233 cl_univpoly_rational_ring |
3234 <cln/univpoly_rational.h> |
3238 Integer polynomial ring
3239 cl_univpoly_integer_ring
3240 <cln/univpoly_integer.h>
3243 and the corresponding classes of univariate polynomials are
3246 Univariate polynomial
3250 +----------------+-------------------+
3252 Complex polynomial | Modular integer polynomial
3254 <cln/univpoly_complex.h> | <cln/univpoly_modint.h>
3260 <cln/univpoly_real.h> |
3264 Rational polynomial |
3266 <cln/univpoly_rational.h> |
3272 <cln/univpoly_integer.h>
3275 Univariate polynomial rings are constructed using the functions
3278 @item cl_univpoly_ring find_univpoly_ring (const cl_ring& R)
3279 @itemx cl_univpoly_ring find_univpoly_ring (const cl_ring& R, const cl_symbol& varname)
3280 This function returns the polynomial ring @samp{R[X]}, unnamed or named.
3281 @code{R} may be an arbitrary ring. This function takes care of finding out
3282 about special cases of @code{R}, such as the rings of complex numbers,
3283 real numbers, rational numbers, integers, or modular integer rings.
3284 There is a cache table of rings, indexed by @code{R} and @code{varname}.
3285 This ensures that two calls of this function with the same arguments will
3286 return the same polynomial ring.
3288 @item cl_univpoly_complex_ring find_univpoly_ring (const cl_complex_ring& R)
3289 @cindex @code{find_univpoly_ring ()}
3290 @item cl_univpoly_complex_ring find_univpoly_ring (const cl_complex_ring& R, const cl_symbol& varname)
3291 @item cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& R)
3292 @item cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& R, const cl_symbol& varname)
3293 @item cl_univpoly_rational_ring find_univpoly_ring (const cl_rational_ring& R)
3294 @item cl_univpoly_rational_ring find_univpoly_ring (const cl_rational_ring& R, const cl_symbol& varname)
3295 @item cl_univpoly_integer_ring find_univpoly_ring (const cl_integer_ring& R)
3296 @item cl_univpoly_integer_ring find_univpoly_ring (const cl_integer_ring& R, const cl_symbol& varname)
3297 @item cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& R)
3298 @item cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& R, const cl_symbol& varname)
3299 These functions are equivalent to the general @code{find_univpoly_ring},
3300 only the return type is more specific, according to the base ring's type.
3303 @node Functions on univariate polynomials
3304 @section Functions on univariate polynomials
3306 Given a univariate polynomial ring @code{R}, the following members can be used.
3309 @item cl_ring R->basering()
3310 @cindex @code{basering ()}
3311 This returns the base ring, as passed to @samp{find_univpoly_ring}.
3313 @item cl_UP R->zero()
3314 @cindex @code{zero ()}
3315 This returns @code{0 in R}, a polynomial of degree -1.
3317 @item cl_UP R->one()
3318 @cindex @code{one ()}
3319 This returns @code{1 in R}, a polynomial of degree == 0.
3321 @item cl_UP R->canonhom (const cl_I& x)
3322 @cindex @code{canonhom ()}
3323 This returns @code{x in R}, a polynomial of degree <= 0.
3325 @item cl_UP R->monomial (const cl_ring_element& x, uintL e)
3326 @cindex @code{monomial ()}
3327 This returns a sparse polynomial: @code{x * X^e}, where @code{X} is the
3330 @item cl_UP R->create (sintL degree)
3331 @cindex @code{create ()}
3332 Creates a new polynomial with a given degree. The zero polynomial has degree
3333 @code{-1}. After creating the polynomial, you should put in the coefficients,
3334 using the @code{set_coeff} member function, and then call the @code{finalize}
3338 The following are the only destructive operations on univariate polynomials.
3341 @item void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)
3342 @cindex @code{set_coeff ()}
3343 This changes the coefficient of @code{X^index} in @code{x} to be @code{y}.
3344 After changing a polynomial and before applying any "normal" operation on it,
3345 you should call its @code{finalize} member function.
3347 @item void finalize (cl_UP& x)
3348 @cindex @code{finalize ()}
3349 This function marks the endpoint of destructive modifications of a polynomial.
3350 It normalizes the internal representation so that subsequent computations have
3351 less overhead. Doing normal computations on unnormalized polynomials may
3352 produce wrong results or crash the program.
3355 The following operations are defined on univariate polynomials.
3358 @item cl_univpoly_ring x.ring ()
3359 @cindex @code{ring ()}
3360 Returns the ring to which the univariate polynomial @code{x} belongs.
3362 @item cl_UP operator+ (const cl_UP&, const cl_UP&)
3363 @cindex @code{operator + ()}
3364 Returns the sum of two univariate polynomials.
3366 @item cl_UP operator- (const cl_UP&, const cl_UP&)
3367 @cindex @code{operator - ()}
3368 Returns the difference of two univariate polynomials.
3370 @item cl_UP operator- (const cl_UP&)
3371 Returns the negative of a univariate polynomial.
3373 @item cl_UP operator* (const cl_UP&, const cl_UP&)
3374 @cindex @code{operator * ()}
3375 Returns the product of two univariate polynomials. One of the arguments may
3376 also be a plain integer or an element of the base ring.
3378 @item cl_UP square (const cl_UP&)
3379 @cindex @code{square ()}
3380 Returns the square of a univariate polynomial.
3382 @item cl_UP expt_pos (const cl_UP& x, const cl_I& y)
3383 @cindex @code{expt_pos ()}
3384 @code{y} must be > 0. Returns @code{x^y}.
3386 @item bool operator== (const cl_UP&, const cl_UP&)
3387 @cindex @code{operator == ()}
3388 @itemx bool operator!= (const cl_UP&, const cl_UP&)
3389 @cindex @code{operator != ()}
3390 Compares two univariate polynomials, belonging to the same univariate
3391 polynomial ring, for equality.
3393 @item bool zerop (const cl_UP& x)
3394 @cindex @code{zerop ()}
3395 Returns true if @code{x} is @code{0 in R}.
3397 @item sintL degree (const cl_UP& x)
3398 @cindex @code{degree ()}
3399 Returns the degree of the polynomial. The zero polynomial has degree @code{-1}.
3401 @item sintL ldegree (const cl_UP& x)
3402 @cindex @code{degree ()}
3403 Returns the low degree of the polynomial. This is the degree of the first
3404 non-vanishing polynomial coefficient. The zero polynomial has ldegree @code{-1}.
3406 @item cl_ring_element coeff (const cl_UP& x, uintL index)
3407 @cindex @code{coeff ()}
3408 Returns the coefficient of @code{X^index} in the polynomial @code{x}.
3410 @item cl_ring_element x (const cl_ring_element& y)
3411 @cindex @code{operator () ()}
3412 Evaluation: If @code{x} is a polynomial and @code{y} belongs to the base ring,
3413 then @samp{x(y)} returns the value of the substitution of @code{y} into
3416 @item cl_UP deriv (const cl_UP& x)
3417 @cindex @code{deriv ()}
3418 Returns the derivative of the polynomial @code{x} with respect to the
3419 indeterminate @code{X}.
3422 The following output functions are defined (see also the chapter on
3426 @item void fprint (std::ostream& stream, const cl_UP& x)
3427 @cindex @code{fprint ()}
3428 @itemx std::ostream& operator<< (std::ostream& stream, const cl_UP& x)
3429 @cindex @code{operator << ()}
3430 Prints the univariate polynomial @code{x} on the @code{stream}. The output may
3431 depend on the global printer settings in the variable
3432 @code{default_print_flags}.
3435 @node Special polynomials
3436 @section Special polynomials
3438 The following functions return special polynomials.
3441 @item cl_UP_I tschebychev (sintL n)
3442 @cindex @code{tschebychev ()}
3443 @cindex Chebyshev polynomial
3444 Returns the n-th Chebyshev polynomial (n >= 0).
3446 @item cl_UP_I hermite (sintL n)
3447 @cindex @code{hermite ()}
3448 @cindex Hermite polynomial
3449 Returns the n-th Hermite polynomial (n >= 0).
3451 @item cl_UP_RA legendre (sintL n)
3452 @cindex @code{legendre ()}
3453 @cindex Legende polynomial
3454 Returns the n-th Legendre polynomial (n >= 0).
3456 @item cl_UP_I laguerre (sintL n)
3457 @cindex @code{laguerre ()}
3458 @cindex Laguerre polynomial
3459 Returns the n-th Laguerre polynomial (n >= 0).
3462 Information how to derive the differential equation satisfied by each
3463 of these polynomials from their definition can be found in the
3464 @code{doc/polynomial/} directory.
3472 * Memory efficiency::
3473 * Speed efficiency::
3474 * Garbage collection::
3481 Using C++ as an implementation language provides
3485 Efficiency: It compiles to machine code.
3489 Portability: It runs on all platforms supporting a C++ compiler. Because
3490 of the availability of GNU C++, this includes all currently used 32-bit and
3491 64-bit platforms, independently of the quality of the vendor's C++ compiler.
3494 Type safety: The C++ compilers knows about the number types and complains if,
3495 for example, you try to assign a float to an integer variable. However,
3496 a drawback is that C++ doesn't know about generic types, hence a restriction
3497 like that @code{operator+ (const cl_MI&, const cl_MI&)} requires that both
3498 arguments belong to the same modular ring cannot be expressed as a compile-time
3502 Algebraic syntax: The elementary operations @code{+}, @code{-}, @code{*},
3503 @code{=}, @code{==}, ... can be used in infix notation, which is more
3504 convenient than Lisp notation @samp{(+ x y)} or C notation @samp{add(x,y,&z)}.
3507 With these language features, there is no need for two separate languages,
3508 one for the implementation of the library and one in which the library's users
3509 can program. This means that a prototype implementation of an algorithm
3510 can be integrated into the library immediately after it has been tested and
3511 debugged. No need to rewrite it in a low-level language after having prototyped
3512 in a high-level language.
3515 @node Memory efficiency
3516 @section Memory efficiency
3518 In order to save memory allocations, CLN implements:
3522 Object sharing: An operation like @code{x+0} returns @code{x} without copying
3525 @cindex garbage collection
3526 @cindex reference counting
3527 Garbage collection: A reference counting mechanism makes sure that any
3528 number object's storage is freed immediately when the last reference to the
3531 @cindex immediate numbers
3532 Small integers are represented as immediate values instead of pointers
3533 to heap allocated storage. This means that integers @code{>= -2^29},
3534 @code{< 2^29} don't consume heap memory, unless they were explicitly allocated
3539 @node Speed efficiency
3540 @section Speed efficiency
3542 Speed efficiency is obtained by the combination of the following tricks
3547 Small integers, being represented as immediate values, don't require
3548 memory access, just a couple of instructions for each elementary operation.
3550 The kernel of CLN has been written in assembly language for some CPUs
3551 (@code{i386}, @code{m68k}, @code{sparc}, @code{mips}, @code{arm}).
3553 On all CPUs, CLN may be configured to use the superefficient low-level
3554 routines from GNU GMP version 3.
3556 For large numbers, CLN uses, instead of the standard @code{O(N^2)}
3557 algorithm, the Karatsuba multiplication, which is an
3568 For very large numbers (more than 12000 decimal digits), CLN uses
3570 Sch{@"o}nhage-Strassen
3571 @cindex Sch{@"o}nhage-Strassen multiplication
3575 @cindex Schoenhage-Strassen multiplication
3577 multiplication, which is an asymptotically optimal multiplication
3580 These fast multiplication algorithms also give improvements in the speed
3581 of division and radix conversion.
3585 @node Garbage collection
3586 @section Garbage collection
3587 @cindex garbage collection
3589 All the number classes are reference count classes: They only contain a pointer
3590 to an object in the heap. Upon construction, assignment and destruction of
3591 number objects, only the objects' reference count are manipulated.
3593 Memory occupied by number objects are automatically reclaimed as soon as
3594 their reference count drops to zero.
3596 For number rings, another strategy is implemented: There is a cache of,
3597 for example, the modular integer rings. A modular integer ring is destroyed
3598 only if its reference count dropped to zero and the cache is about to be
3599 resized. The effect of this strategy is that recently used rings remain
3600 cached, whereas undue memory consumption through cached rings is avoided.
3603 @node Using the library
3604 @chapter Using the library
3606 For the following discussion, we will assume that you have installed
3607 the CLN source in @code{$CLN_DIR} and built it in @code{$CLN_TARGETDIR}.
3608 For example, for me it's @code{CLN_DIR="$HOME/cln"} and
3609 @code{CLN_TARGETDIR="$HOME/cln/linuxelf"}. You might define these as
3610 environment variables, or directly substitute the appropriate values.
3614 * Compiler options::
3617 * Debugging support::
3618 * Reporting Problems::
3621 @node Compiler options
3622 @section Compiler options
3623 @cindex compiler options
3625 Until you have installed CLN in a public place, the following options are
3628 When you compile CLN application code, add the flags
3630 -I$CLN_DIR/include -I$CLN_TARGETDIR/include
3632 to the C++ compiler's command line (@code{make} variable CFLAGS or CXXFLAGS).
3633 When you link CLN application code to form an executable, add the flags
3635 $CLN_TARGETDIR/src/libcln.a
3637 to the C/C++ compiler's command line (@code{make} variable LIBS).
3639 If you did a @code{make install}, the include files are installed in a
3640 public directory (normally @code{/usr/local/include}), hence you don't
3641 need special flags for compiling. The library has been installed to a
3642 public directory as well (normally @code{/usr/local/lib}), hence when
3643 linking a CLN application it is sufficient to give the flag @code{-lcln}.
3645 @cindex @code{pkg-config}
3646 To make the creation of software packages that use CLN easier, the
3647 @code{pkg-config} utility can be used. CLN provides all the necessary
3648 metainformation in a file called @code{cln.pc} (installed in
3649 @code{/usr/local/lib/pkgconfig} by default). A program using CLN can
3650 be compiled and linked using @footnote{If you installed CLN to
3651 non-standard location @var{prefix}, you need to set the
3652 @env{PKG_CONFIG_PATH} environment variable to @var{prefix}/lib/pkgconfig
3655 g++ `pkg-config --libs cln` `pkg-config --cflags cln` prog.cc -o prog
3658 Software using GNU autoconf can check for CLN with the
3659 @code{PKG_CHECK_MODULES} macro supplied with @code{pkg-config}.
3661 PKG_CHECK_MODULES([CLN], [cln >= @var{MIN-VERSION}])
3663 This will check for CLN version at least @var{MIN-VERSION}. If the
3664 required version was found, the variables @var{CLN_CFLAGS} and
3665 @var{CLN_LIBS} are set. Otherwise the configure script aborts. If this
3666 is not the desired behaviour, use the following code instead
3667 @footnote{See the @code{pkg-config} documentation for more details.}
3669 PKG_CHECK_MODULES([CLN], [cln >= @var{MIN-VERSION}], [],
3670 [AC_MSG_WARNING([No suitable version of CLN can be found])])
3675 @section Include files
3676 @cindex include files
3677 @cindex header files
3679 Here is a summary of the include files and their contents.
3682 @item <cln/object.h>
3683 General definitions, reference counting, garbage collection.
3684 @item <cln/number.h>
3685 The class cl_number.
3686 @item <cln/complex.h>
3687 Functions for class cl_N, the complex numbers.
3689 Functions for class cl_R, the real numbers.
3691 Functions for class cl_F, the floats.
3692 @item <cln/sfloat.h>
3693 Functions for class cl_SF, the short-floats.
3694 @item <cln/ffloat.h>
3695 Functions for class cl_FF, the single-floats.
3696 @item <cln/dfloat.h>
3697 Functions for class cl_DF, the double-floats.
3698 @item <cln/lfloat.h>
3699 Functions for class cl_LF, the long-floats.
3700 @item <cln/rational.h>
3701 Functions for class cl_RA, the rational numbers.
3702 @item <cln/integer.h>
3703 Functions for class cl_I, the integers.
3706 @item <cln/complex_io.h>
3707 Input/Output for class cl_N, the complex numbers.
3708 @item <cln/real_io.h>
3709 Input/Output for class cl_R, the real numbers.
3710 @item <cln/float_io.h>
3711 Input/Output for class cl_F, the floats.
3712 @item <cln/sfloat_io.h>
3713 Input/Output for class cl_SF, the short-floats.
3714 @item <cln/ffloat_io.h>
3715 Input/Output for class cl_FF, the single-floats.
3716 @item <cln/dfloat_io.h>
3717 Input/Output for class cl_DF, the double-floats.
3718 @item <cln/lfloat_io.h>
3719 Input/Output for class cl_LF, the long-floats.
3720 @item <cln/rational_io.h>
3721 Input/Output for class cl_RA, the rational numbers.
3722 @item <cln/integer_io.h>
3723 Input/Output for class cl_I, the integers.
3725 Flags for customizing input operations.
3726 @item <cln/output.h>
3727 Flags for customizing output operations.
3728 @item <cln/malloc.h>
3729 @code{malloc_hook}, @code{free_hook}.
3730 @item <cln/exception.h>
3731 Exception base class.
3732 @item <cln/condition.h>
3734 @item <cln/string.h>
3736 @item <cln/symbol.h>
3738 @item <cln/proplist.h>
3742 @item <cln/null_ring.h>
3744 @item <cln/complex_ring.h>
3745 The ring of complex numbers.
3746 @item <cln/real_ring.h>
3747 The ring of real numbers.
3748 @item <cln/rational_ring.h>
3749 The ring of rational numbers.
3750 @item <cln/integer_ring.h>
3751 The ring of integers.
3752 @item <cln/numtheory.h>
3753 Number threory functions.
3754 @item <cln/modinteger.h>
3760 @item <cln/GV_number.h>
3761 General vectors over cl_number.
3762 @item <cln/GV_complex.h>
3763 General vectors over cl_N.
3764 @item <cln/GV_real.h>
3765 General vectors over cl_R.
3766 @item <cln/GV_rational.h>
3767 General vectors over cl_RA.
3768 @item <cln/GV_integer.h>
3769 General vectors over cl_I.
3770 @item <cln/GV_modinteger.h>
3771 General vectors of modular integers.
3774 @item <cln/SV_number.h>
3775 Simple vectors over cl_number.
3776 @item <cln/SV_complex.h>
3777 Simple vectors over cl_N.
3778 @item <cln/SV_real.h>
3779 Simple vectors over cl_R.
3780 @item <cln/SV_rational.h>
3781 Simple vectors over cl_RA.
3782 @item <cln/SV_integer.h>
3783 Simple vectors over cl_I.
3784 @item <cln/SV_ringelt.h>
3785 Simple vectors of general ring elements.
3786 @item <cln/univpoly.h>
3787 Univariate polynomials.
3788 @item <cln/univpoly_integer.h>
3789 Univariate polynomials over the integers.
3790 @item <cln/univpoly_rational.h>
3791 Univariate polynomials over the rational numbers.
3792 @item <cln/univpoly_real.h>
3793 Univariate polynomials over the real numbers.
3794 @item <cln/univpoly_complex.h>
3795 Univariate polynomials over the complex numbers.
3796 @item <cln/univpoly_modint.h>
3797 Univariate polynomials over modular integer rings.
3798 @item <cln/timing.h>
3801 Includes all of the above.
3808 A function which computes the nth Fibonacci number can be written as follows.
3809 @cindex Fibonacci number
3812 #include <cln/integer.h>
3813 #include <cln/real.h>
3814 using namespace cln;
3816 // Returns F_n, computed as the nearest integer to
3817 // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
3818 const cl_I fibonacci (int n)
3820 // Need a precision of ((1+sqrt(5))/2)^-n.
3821 float_format_t prec = float_format((int)(0.208987641*n+5));
3822 cl_R sqrt5 = sqrt(cl_float(5,prec));
3823 cl_R phi = (1+sqrt5)/2;
3824 return round1( expt(phi,n)/sqrt5 );
3828 Let's explain what is going on in detail.
3830 The include file @code{<cln/integer.h>} is necessary because the type
3831 @code{cl_I} is used in the function, and the include file @code{<cln/real.h>}
3832 is needed for the type @code{cl_R} and the floating point number functions.
3833 The order of the include files does not matter. In order not to write
3834 out @code{cln::}@var{foo} in this simple example we can safely import
3835 the whole namespace @code{cln}.
3837 Then comes the function declaration. The argument is an @code{int}, the
3838 result an integer. The return type is defined as @samp{const cl_I}, not
3839 simply @samp{cl_I}, because that allows the compiler to detect typos like
3840 @samp{fibonacci(n) = 100}. It would be possible to declare the return
3841 type as @code{const cl_R} (real number) or even @code{const cl_N} (complex
3842 number). We use the most specialized possible return type because functions
3843 which call @samp{fibonacci} will be able to profit from the compiler's type
3844 analysis: Adding two integers is slightly more efficient than adding the
3845 same objects declared as complex numbers, because it needs less type
3846 dispatch. Also, when linking to CLN as a non-shared library, this minimizes
3847 the size of the resulting executable program.
3849 The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
3850 integer. In order to get a correct result, the absolute error should be less
3851 than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
3852 To this end, the first line computes a floating point precision for sqrt(5)
3855 Then sqrt(5) is computed by first converting the integer 5 to a floating point
3856 number and than taking the square root. The converse, first taking the square
3857 root of 5, and then converting to the desired precision, would not work in
3858 CLN: The square root would be computed to a default precision (normally
3859 single-float precision), and the following conversion could not help about
3860 the lacking accuracy. This is because CLN is not a symbolic computer algebra
3861 system and does not represent sqrt(5) in a non-numeric way.
3863 The type @code{cl_R} for sqrt5 and, in the following line, phi is the only
3864 possible choice. You cannot write @code{cl_F} because the C++ compiler can
3865 only infer that @code{cl_float(5,prec)} is a real number. You cannot write
3866 @code{cl_N} because a @samp{round1} does not exist for general complex
3869 When the function returns, all the local variables in the function are
3870 automatically reclaimed (garbage collected). Only the result survives and
3871 gets passed to the caller.
3873 The file @code{fibonacci.cc} in the subdirectory @code{examples}
3874 contains this implementation together with an even faster algorithm.
3876 @node Debugging support
3877 @section Debugging support
3880 When debugging a CLN application with GNU @code{gdb}, two facilities are
3881 available from the library:
3884 @item The library does type checks, range checks, consistency checks at
3885 many places. When one of these fails, an exception of a type derived from
3886 @code{runtime_exception} is thrown. When an exception is cought, the stack
3887 has already been unwound, so it is may not be possible to tell at which
3888 point the exception was thrown. For debugging, it is best to set up a
3889 catchpoint at the event of throwning a C++ exception:
3893 When this catchpoint is hit, look at the stack's backtrace:
3897 When control over the type of exception is required, it may be possible
3898 to set a breakpoint at the @code{g++} runtime library function
3899 @code{__raise_exception}. Refer to the documentation of GNU @code{gdb}
3902 @item The debugger's normal @code{print} command doesn't know about
3903 CLN's types and therefore prints mostly useless hexadecimal addresses.
3904 CLN offers a function @code{cl_print}, callable from the debugger,
3905 for printing number objects. In order to get this function, you have
3906 to define the macro @samp{CL_DEBUG} and then include all the header files
3907 for which you want @code{cl_print} debugging support. For example:
3908 @cindex @code{CL_DEBUG}
3911 #include <cln/string.h>
3913 Now, if you have in your program a variable @code{cl_string s}, and
3914 inspect it under @code{gdb}, the output may look like this:
3917 $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
3918 word = 134568800@}@}, @}
3919 (gdb) call cl_print(s)
3923 Note that the output of @code{cl_print} goes to the program's error output,
3924 not to gdb's standard output.
3926 Note, however, that the above facility does not work with all CLN types,
3927 only with number objects and similar. Therefore CLN offers a member function
3928 @code{debug_print()} on all CLN types. The same macro @samp{CL_DEBUG}
3929 is needed for this member function to be implemented. Under @code{gdb},
3930 you call it like this:
3931 @cindex @code{debug_print ()}
3934 $7 = @{<cl_gcpointer> = @{ = @{pointer = 0x8055b60, heappointer = 0x8055b60,
3935 word = 134568800@}@}, @}
3936 (gdb) call s.debug_print()
3939 >call ($1).debug_print()
3944 Unfortunately, this feature does not seem to work under all circumstances.
3947 @node Reporting Problems
3948 @section Reporting Problems
3950 @cindex mailing list
3952 If you encounter any problem, please don't hesitate to send a detailed
3953 bugreport to the @code{cln-list@@ginac.de} mailing list. Please think
3954 about your bug: consider including a short description of your operating
3955 system and compilation environment with corresponding version numbers. A
3956 description of your configuration options may also be helpful. Also, a
3957 short test program together with the output you get and the output you
3958 expect will help us to reproduce it quickly. Finally, do not forget to
3959 report the version number of CLN.
3963 @chapter Customizing
3968 * Floating-point underflow::
3970 * Customizing the memory allocator::
3973 @node Error handling
3974 @section Error handling
3976 @cindex error handling
3978 @cindex @code{runtime_exception}
3979 CLN signals abnormal situations by throwning exceptions. All exceptions
3980 thrown by the library are of type @code{runtime_exception} or of a
3981 derived type. Class @code{cln::runtime_exception} in turn is derived
3982 from the C++ standard library class @code{std::runtime_error} and
3983 inherits the @code{.what()} member function that can be used to query
3984 details about the cause of error.
3986 The most important classes thrown by the library are
3988 @cindex @code{floating_point_exception}
3989 @cindex @code{read_number_exception}
3991 Exception base class
3995 +----------------+----------------+
3997 Malformed number input Floating-point error
3998 read_number_exception floating_poing_exception
3999 <cln/number_io.h> <cln/float.h>
4002 CLN has many more exception classes that allow for more fine-grained
4003 control but I refrain from documenting them all here. They are all
4004 declared in the public header files and they are all subclasses of the
4005 above exceptions, so catching those you are always on the safe side.
4008 @node Floating-point underflow
4009 @section Floating-point underflow
4012 @cindex @code{floating_point_underflow_exception}
4013 Floating point underflow denotes the situation when a floating-point
4014 number is to be created which is so close to @code{0} that its exponent
4015 is too low to be represented internally. By default, this causes the
4016 exception @code{floating_point_underflow_exception} (subclass of
4017 @code{floating_point_exception}) to be thrown. If you set the global
4020 bool cl_inhibit_floating_point_underflow
4022 to @code{true}, the exception will be inhibited, and a floating-point
4023 zero will be generated instead. The default value of
4024 @code{cl_inhibit_floating_point_underflow} is @code{false}.
4027 @node Customizing I/O
4028 @section Customizing I/O
4030 The output of the function @code{fprint} may be customized by changing the
4031 value of the global variable @code{default_print_flags}.
4032 @cindex @code{default_print_flags}
4035 @node Customizing the memory allocator
4036 @section Customizing the memory allocator
4038 Every memory allocation of CLN is done through the function pointer
4039 @code{malloc_hook}. Freeing of this memory is done through the function
4040 pointer @code{free_hook}. The default versions of these functions,
4041 provided in the library, call @code{malloc} and @code{free} and check
4042 the @code{malloc} result against @code{NULL}.
4043 If you want to provide another memory allocator, you need to define
4044 the variables @code{malloc_hook} and @code{free_hook} yourself,
4047 #include <cln/malloc.h>
4049 void* (*malloc_hook) (size_t size) = @dots{};
4050 void (*free_hook) (void* ptr) = @dots{};
4053 @cindex @code{malloc_hook ()}
4054 @cindex @code{free_hook ()}
4055 The @code{cl_malloc_hook} function must not return a @code{NULL} pointer.
4057 It is not possible to change the memory allocator at runtime, because
4058 it is already called at program startup by the constructors of some
4066 @node Index, , Customizing, Top