3 <!-- Created by texi2html 1.56k from cln.texi on 28 August 2000 -->
5 <TITLE>CLN, a Class Library for Numbers</TITLE>
8 <H1>CLN, a Class Library for Numbers</H1>
9 <ADDRESS>by Bruno Haible</ADDRESS>
12 <H1>Table of Contents</H1>
14 <LI><A NAME="TOC1" HREF="cln.html#SEC1">1. Introduction</A>
15 <LI><A NAME="TOC2" HREF="cln.html#SEC2">2. Installation</A>
17 <LI><A NAME="TOC3" HREF="cln.html#SEC3">2.1 Prerequisites</A>
19 <LI><A NAME="TOC4" HREF="cln.html#SEC4">2.1.1 C++ compiler</A>
20 <LI><A NAME="TOC5" HREF="cln.html#SEC5">2.1.2 Make utility</A>
21 <LI><A NAME="TOC6" HREF="cln.html#SEC6">2.1.3 Sed utility</A>
23 <LI><A NAME="TOC7" HREF="cln.html#SEC7">2.2 Building the library</A>
25 <LI><A NAME="TOC8" HREF="cln.html#SEC8">2.2.1 Using the GNU MP Library</A>
27 <LI><A NAME="TOC9" HREF="cln.html#SEC9">2.3 Installing the library</A>
28 <LI><A NAME="TOC10" HREF="cln.html#SEC10">2.4 Cleaning up</A>
30 <LI><A NAME="TOC11" HREF="cln.html#SEC11">3. Ordinary number types</A>
32 <LI><A NAME="TOC12" HREF="cln.html#SEC12">3.1 Exact numbers</A>
33 <LI><A NAME="TOC13" HREF="cln.html#SEC13">3.2 Floating-point numbers</A>
34 <LI><A NAME="TOC14" HREF="cln.html#SEC14">3.3 Complex numbers</A>
35 <LI><A NAME="TOC15" HREF="cln.html#SEC15">3.4 Conversions</A>
37 <LI><A NAME="TOC16" HREF="cln.html#SEC16">4. Functions on numbers</A>
39 <LI><A NAME="TOC17" HREF="cln.html#SEC17">4.1 Constructing numbers</A>
41 <LI><A NAME="TOC18" HREF="cln.html#SEC18">4.1.1 Constructing integers</A>
42 <LI><A NAME="TOC19" HREF="cln.html#SEC19">4.1.2 Constructing rational numbers</A>
43 <LI><A NAME="TOC20" HREF="cln.html#SEC20">4.1.3 Constructing floating-point numbers</A>
44 <LI><A NAME="TOC21" HREF="cln.html#SEC21">4.1.4 Constructing complex numbers</A>
46 <LI><A NAME="TOC22" HREF="cln.html#SEC22">4.2 Elementary functions</A>
47 <LI><A NAME="TOC23" HREF="cln.html#SEC23">4.3 Elementary rational functions</A>
48 <LI><A NAME="TOC24" HREF="cln.html#SEC24">4.4 Elementary complex functions</A>
49 <LI><A NAME="TOC25" HREF="cln.html#SEC25">4.5 Comparisons</A>
50 <LI><A NAME="TOC26" HREF="cln.html#SEC26">4.6 Rounding functions</A>
51 <LI><A NAME="TOC27" HREF="cln.html#SEC27">4.7 Roots</A>
52 <LI><A NAME="TOC28" HREF="cln.html#SEC28">4.8 Transcendental functions</A>
54 <LI><A NAME="TOC29" HREF="cln.html#SEC29">4.8.1 Exponential and logarithmic functions</A>
55 <LI><A NAME="TOC30" HREF="cln.html#SEC30">4.8.2 Trigonometric functions</A>
56 <LI><A NAME="TOC31" HREF="cln.html#SEC31">4.8.3 Hyperbolic functions</A>
57 <LI><A NAME="TOC32" HREF="cln.html#SEC32">4.8.4 Euler gamma</A>
58 <LI><A NAME="TOC33" HREF="cln.html#SEC33">4.8.5 Riemann zeta</A>
60 <LI><A NAME="TOC34" HREF="cln.html#SEC34">4.9 Functions on integers</A>
62 <LI><A NAME="TOC35" HREF="cln.html#SEC35">4.9.1 Logical functions</A>
63 <LI><A NAME="TOC36" HREF="cln.html#SEC36">4.9.2 Number theoretic functions</A>
64 <LI><A NAME="TOC37" HREF="cln.html#SEC37">4.9.3 Combinatorial functions</A>
66 <LI><A NAME="TOC38" HREF="cln.html#SEC38">4.10 Functions on floating-point numbers</A>
67 <LI><A NAME="TOC39" HREF="cln.html#SEC39">4.11 Conversion functions</A>
69 <LI><A NAME="TOC40" HREF="cln.html#SEC40">4.11.1 Conversion to floating-point numbers</A>
70 <LI><A NAME="TOC41" HREF="cln.html#SEC41">4.11.2 Conversion to rational numbers</A>
72 <LI><A NAME="TOC42" HREF="cln.html#SEC42">4.12 Random number generators</A>
73 <LI><A NAME="TOC43" HREF="cln.html#SEC43">4.13 Obfuscating operators</A>
75 <LI><A NAME="TOC44" HREF="cln.html#SEC44">5. Input/Output</A>
77 <LI><A NAME="TOC45" HREF="cln.html#SEC45">5.1 Internal and printed representation</A>
78 <LI><A NAME="TOC46" HREF="cln.html#SEC46">5.2 Input functions</A>
79 <LI><A NAME="TOC47" HREF="cln.html#SEC47">5.3 Output functions</A>
81 <LI><A NAME="TOC48" HREF="cln.html#SEC48">6. Rings</A>
82 <LI><A NAME="TOC49" HREF="cln.html#SEC49">7. Modular integers</A>
84 <LI><A NAME="TOC50" HREF="cln.html#SEC50">7.1 Modular integer rings</A>
85 <LI><A NAME="TOC51" HREF="cln.html#SEC51">7.2 Functions on modular integers</A>
87 <LI><A NAME="TOC52" HREF="cln.html#SEC52">8. Symbolic data types</A>
89 <LI><A NAME="TOC53" HREF="cln.html#SEC53">8.1 Strings</A>
90 <LI><A NAME="TOC54" HREF="cln.html#SEC54">8.2 Symbols</A>
92 <LI><A NAME="TOC55" HREF="cln.html#SEC55">9. Univariate polynomials</A>
94 <LI><A NAME="TOC56" HREF="cln.html#SEC56">9.1 Univariate polynomial rings</A>
95 <LI><A NAME="TOC57" HREF="cln.html#SEC57">9.2 Functions on univariate polynomials</A>
96 <LI><A NAME="TOC58" HREF="cln.html#SEC58">9.3 Special polynomials</A>
98 <LI><A NAME="TOC59" HREF="cln.html#SEC59">10. Internals</A>
100 <LI><A NAME="TOC60" HREF="cln.html#SEC60">10.1 Why C++ ?</A>
101 <LI><A NAME="TOC61" HREF="cln.html#SEC61">10.2 Memory efficiency</A>
102 <LI><A NAME="TOC62" HREF="cln.html#SEC62">10.3 Speed efficiency</A>
103 <LI><A NAME="TOC63" HREF="cln.html#SEC63">10.4 Garbage collection</A>
105 <LI><A NAME="TOC64" HREF="cln.html#SEC64">11. Using the library</A>
107 <LI><A NAME="TOC65" HREF="cln.html#SEC65">11.1 Compiler options</A>
108 <LI><A NAME="TOC66" HREF="cln.html#SEC66">11.2 Compatibility to old CLN versions</A>
109 <LI><A NAME="TOC67" HREF="cln.html#SEC67">11.3 Include files</A>
110 <LI><A NAME="TOC68" HREF="cln.html#SEC68">11.4 An Example</A>
111 <LI><A NAME="TOC69" HREF="cln.html#SEC69">11.5 Debugging support</A>
113 <LI><A NAME="TOC70" HREF="cln.html#SEC70">12. Customizing</A>
115 <LI><A NAME="TOC71" HREF="cln.html#SEC71">12.1 Error handling</A>
116 <LI><A NAME="TOC72" HREF="cln.html#SEC72">12.2 Floating-point underflow</A>
117 <LI><A NAME="TOC73" HREF="cln.html#SEC73">12.3 Customizing I/O</A>
118 <LI><A NAME="TOC74" HREF="cln.html#SEC74">12.4 Customizing the memory allocator</A>
120 <LI><A NAME="TOC75" HREF="cln.html#SEC75">Index</A>
125 <H1><A NAME="SEC1" HREF="cln.html#TOC1">1. Introduction</A></H1>
128 CLN is a library for computations with all kinds of numbers.
129 It has a rich set of number classes:
136 Integers (with unlimited precision),
144 Floating-point numbers:
159 Long float (with unlimited precision),
168 Modular integers (integers modulo a fixed integer),
172 Univariate polynomials.
176 The subtypes of the complex numbers among these are exactly the
177 types of numbers known to the Common Lisp language. Therefore
178 <CODE>CLN</CODE> can be used for Common Lisp implementations, giving
179 <SAMP>`CLN'</SAMP> another meaning: it becomes an abbreviation of
180 "Common Lisp Numbers".
184 The CLN package implements
191 Elementary functions (<CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, <CODE>sqrt</CODE>,
196 Logical functions (logical <CODE>and</CODE>, <CODE>or</CODE>, <CODE>not</CODE>, ...),
200 Transcendental functions (exponential, logarithmic, trigonometric, hyperbolic
201 functions and their inverse functions).
205 CLN is a C++ library. Using C++ as an implementation language provides
212 efficiency: it compiles to machine code,
215 type safety: the C++ compiler knows about the number types and complains
216 if, for example, you try to assign a float to an integer variable.
219 algebraic syntax: You can use the <CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>, <CODE>=</CODE>,
220 <CODE>==</CODE>, ... operators as in C or C++.
224 CLN is memory efficient:
231 Small integers and short floats are immediate, not heap allocated.
234 Heap-allocated memory is reclaimed through an automatic, non-interruptive
239 CLN is speed efficient:
246 The kernel of CLN has been written in assembly language for some CPUs
247 (<CODE>i386</CODE>, <CODE>m68k</CODE>, <CODE>sparc</CODE>, <CODE>mips</CODE>, <CODE>arm</CODE>).
251 On all CPUs, CLN may be configured to use the superefficient low-level
252 routines from GNU GMP version 3.
255 It uses Karatsuba multiplication, which is significantly faster
256 for large numbers than the standard multiplication algorithm.
259 For very large numbers (more than 12000 decimal digits), it uses
262 multiplication, which is an asymptotically optimal multiplication
263 algorithm, for multiplication, division and radix conversion.
267 CLN aims at being easily integrated into larger software packages:
274 The garbage collection imposes no burden on the main application.
277 The library provides hooks for memory allocation and exceptions.
281 All non-macro identifiers are hidden in namespace <CODE>cln</CODE> in
282 order to avoid name clashes.
287 <H1><A NAME="SEC2" HREF="cln.html#TOC2">2. Installation</A></H1>
290 This section describes how to install the CLN package on your system.
295 <H2><A NAME="SEC3" HREF="cln.html#TOC3">2.1 Prerequisites</A></H2>
299 <H3><A NAME="SEC4" HREF="cln.html#TOC4">2.1.1 C++ compiler</A></H3>
302 To build CLN, you need a C++ compiler.
303 Actually, you need GNU <CODE>g++ 2.90</CODE> or newer, the EGCS compilers will
305 I recommend GNU <CODE>g++ 2.95</CODE> or newer.
309 The following C++ features are used:
310 classes, member functions, overloading of functions and operators,
311 constructors and destructors, inline, const, multiple inheritance,
312 templates and namespaces.
316 The following C++ features are not used:
317 <CODE>new</CODE>, <CODE>delete</CODE>, virtual inheritance, exceptions.
321 CLN relies on semi-automatic ordering of initializations
322 of static and global variables, a feature which I could
323 implement for GNU g++ only.
328 <H3><A NAME="SEC5" HREF="cln.html#TOC5">2.1.2 Make utility</A></H3>
334 To build CLN, you also need to have GNU <CODE>make</CODE> installed.
339 <H3><A NAME="SEC6" HREF="cln.html#TOC6">2.1.3 Sed utility</A></H3>
345 To build CLN on HP-UX, you also need to have GNU <CODE>sed</CODE> installed.
346 This is because the libtool script, which creates the CLN library, relies
347 on <CODE>sed</CODE>, and the vendor's <CODE>sed</CODE> utility on these systems is too
353 <H2><A NAME="SEC7" HREF="cln.html#TOC7">2.2 Building the library</A></H2>
356 As with any autoconfiguring GNU software, installation is as easy as this:
367 If on your system, <SAMP>`make'</SAMP> is not GNU <CODE>make</CODE>, you have to use
368 <SAMP>`gmake'</SAMP> instead of <SAMP>`make'</SAMP> above.
372 The <CODE>configure</CODE> command checks out some features of your system and
373 C++ compiler and builds the <CODE>Makefile</CODE>s. The <CODE>make</CODE> command
374 builds the library. This step may take 4 hours on an average workstation.
375 The <CODE>make check</CODE> runs some test to check that no important subroutine
376 has been miscompiled.
380 The <CODE>configure</CODE> command accepts options. To get a summary of them, try
389 Some of the options are explained in detail in the <SAMP>`INSTALL.generic'</SAMP> file.
393 You can specify the C compiler, the C++ compiler and their options through
394 the following environment variables when running <CODE>configure</CODE>:
401 Specifies the C compiler.
403 <DT><CODE>CFLAGS</CODE>
405 Flags to be given to the C compiler when compiling programs (not when linking).
409 Specifies the C++ compiler.
411 <DT><CODE>CXXFLAGS</CODE>
413 Flags to be given to the C++ compiler when compiling programs (not when linking).
422 $ CC="gcc" CFLAGS="-O" CXX="g++" CXXFLAGS="-O" ./configure
423 $ CC="gcc -V egcs-2.91.60" CFLAGS="-O -g" \
424 CXX="g++ -V egcs-2.91.60" CXXFLAGS="-O -g" ./configure
425 $ CC="gcc -V 2.95.2" CFLAGS="-O2 -fno-exceptions" \
426 CXX="g++ -V 2.95.2" CFLAGS="-O2 -fno-exceptions" ./configure
430 Note that for these environment variables to take effect, you have to set
431 them (assuming a Bourne-compatible shell) on the same line as the
432 <CODE>configure</CODE> command. If you made the settings in earlier shell
433 commands, you have to <CODE>export</CODE> the environment variables before
434 calling <CODE>configure</CODE>. In a <CODE>csh</CODE> shell, you have to use the
435 <SAMP>`setenv'</SAMP> command for setting each of the environment variables.
439 Currently CLN works only with the GNU <CODE>g++</CODE> compiler, and only in
440 optimizing mode. So you should specify at least <CODE>-O</CODE> in the CXXFLAGS,
441 or no CXXFLAGS at all. (If CXXFLAGS is not set, CLN will use <CODE>-O</CODE>.)
445 If you use <CODE>g++</CODE> version 2.8.x or egcs-2.91.x (a.k.a. egcs-1.1) or
446 gcc-2.95.x, I recommend adding <SAMP>`-fno-exceptions'</SAMP> to the CXXFLAGS.
447 This will likely generate better code.
451 If you use <CODE>g++</CODE> version egcs-2.91.x (egcs-1.1) or gcc-2.95.x on Sparc,
452 add either <SAMP>`-O'</SAMP>, <SAMP>`-O1'</SAMP> or <SAMP>`-O2 -fno-schedule-insns'</SAMP> to the
453 CXXFLAGS. With full <SAMP>`-O2'</SAMP>, <CODE>g++</CODE> miscompiles the division routines.
454 Also, if you have <CODE>g++</CODE> version egcs-1.1.1 or older on Sparc, you must
455 specify <SAMP>`--disable-shared'</SAMP> because <CODE>g++</CODE> would miscompile parts of
460 By default, both a shared and a static library are built. You can build
461 CLN as a static (or shared) library only, by calling <CODE>configure</CODE> with
462 the option <SAMP>`--disable-shared'</SAMP> (or <SAMP>`--disable-static'</SAMP>). While
463 shared libraries are usually more convenient to use, they may not work
464 on all architectures. Try disabling them if you run into linker
465 problems. Also, they are generally somewhat slower than static
466 libraries so runtime-critical applications should be linked statically.
471 <H3><A NAME="SEC8" HREF="cln.html#TOC8">2.2.1 Using the GNU MP Library</A></H3>
477 Starting with version 1.1, CLN may be configured to make use of a
478 preinstalled <CODE>gmp</CODE> library. Please make sure that you have at
479 least <CODE>gmp</CODE> version 3.0 installed since earlier versions are
480 unsupported and likely not to work. Enabling this feature by calling
481 <CODE>configure</CODE> with the option <SAMP>`--with-gmp'</SAMP> is known to be quite
482 a boost for CLN's performance.
486 If you have installed the <CODE>gmp</CODE> library and its header file in
487 some place where your compiler cannot find it by default, you must help
488 <CODE>configure</CODE> by setting <CODE>CPPFLAGS</CODE> and <CODE>LDFLAGS</CODE>. Here is
494 $ CC="gcc" CFLAGS="-O2" CXX="g++" CXXFLAGS="-O2 -fno-exceptions" \
495 CPPFLAGS="-I/opt/gmp/include" LDFLAGS="-L/opt/gmp/lib" ./configure --with-gmp
500 <H2><A NAME="SEC9" HREF="cln.html#TOC9">2.3 Installing the library</A></H2>
506 As with any autoconfiguring GNU software, installation is as easy as this:
515 The <SAMP>`make install'</SAMP> command installs the library and the include files
516 into public places (<TT>`/usr/local/lib/'</TT> and <TT>`/usr/local/include/'</TT>,
517 if you haven't specified a <CODE>--prefix</CODE> option to <CODE>configure</CODE>).
518 This step may require superuser privileges.
522 If you have already built the library and wish to install it, but didn't
523 specify <CODE>--prefix=...</CODE> at configure time, just re-run
524 <CODE>configure</CODE>, giving it the same options as the first time, plus
525 the <CODE>--prefix=...</CODE> option.
530 <H2><A NAME="SEC10" HREF="cln.html#TOC10">2.4 Cleaning up</A></H2>
533 You can remove system-dependent files generated by <CODE>make</CODE> through
542 You can remove all files generated by <CODE>make</CODE>, thus reverting to a
543 virgin distribution of CLN, through
553 <H1><A NAME="SEC11" HREF="cln.html#TOC11">3. Ordinary number types</A></H1>
556 CLN implements the following class hierarchy:
563 <cln/number.h>
566 Real or complex number
568 <cln/complex.h>
575 +-------------------+-------------------+
577 Rational number Floating-point number
579 <cln/rational.h> <cln/float.h>
581 | +--------------+--------------+--------------+
583 cl_I Short-Float Single-Float Double-Float Long-Float
584 <cln/integer.h> cl_SF cl_FF cl_DF cl_LF
585 <cln/sfloat.h> <cln/ffloat.h> <cln/dfloat.h> <cln/lfloat.h>
591 The base class <CODE>cl_number</CODE> is an abstract base class.
592 It is not useful to declare a variable of this type except if you want
593 to completely disable compile-time type checking and use run-time type
601 The class <CODE>cl_N</CODE> comprises real and complex numbers. There is
602 no special class for complex numbers since complex numbers with imaginary
603 part <CODE>0</CODE> are automatically converted to real numbers.
608 The class <CODE>cl_R</CODE> comprises real numbers of different kinds. It is an
616 The class <CODE>cl_RA</CODE> comprises exact real numbers: rational numbers, including
617 integers. There is no special class for non-integral rational numbers
618 since rational numbers with denominator <CODE>1</CODE> are automatically converted
624 The class <CODE>cl_F</CODE> implements floating-point approximations to real numbers.
625 It is an abstract class.
630 <H2><A NAME="SEC12" HREF="cln.html#TOC12">3.1 Exact numbers</A></H2>
636 Some numbers are represented as exact numbers: there is no loss of information
637 when such a number is converted from its mathematical value to its internal
638 representation. On exact numbers, the elementary operations (<CODE>+</CODE>,
639 <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, comparisons, ...) compute the completely
644 In CLN, the exact numbers are:
651 rational numbers (including integers),
654 complex numbers whose real and imaginary parts are both rational numbers.
658 Rational numbers are always normalized to the form
659 <CODE><VAR>numerator</VAR>/<VAR>denominator</VAR></CODE> where the numerator and denominator
660 are coprime integers and the denominator is positive. If the resulting
661 denominator is <CODE>1</CODE>, the rational number is converted to an integer.
665 Small integers (typically in the range <CODE>-2^30</CODE>...<CODE>2^30-1</CODE>,
666 for 32-bit machines) are especially efficient, because they consume no heap
667 allocation. Otherwise the distinction between these immediate integers
668 (called "fixnums") and heap allocated integers (called "bignums")
669 is completely transparent.
674 <H2><A NAME="SEC13" HREF="cln.html#TOC13">3.2 Floating-point numbers</A></H2>
680 Not all real numbers can be represented exactly. (There is an easy mathematical
681 proof for this: Only a countable set of numbers can be stored exactly in
682 a computer, even if one assumes that it has unlimited storage. But there
683 are uncountably many real numbers.) So some approximation is needed.
684 CLN implements ordinary floating-point numbers, with mantissa and exponent.
689 The elementary operations (<CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, ...)
690 only return approximate results. For example, the value of the expression
691 <CODE>(cl_F) 0.3 + (cl_F) 0.4</CODE> prints as <SAMP>`0.70000005'</SAMP>, not as
692 <SAMP>`0.7'</SAMP>. Rounding errors like this one are inevitable when computing
693 with floating-point numbers.
697 Nevertheless, CLN rounds the floating-point results of the operations <CODE>+</CODE>,
698 <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, <CODE>sqrt</CODE> according to the "round-to-even"
699 rule: It first computes the exact mathematical result and then returns the
700 floating-point number which is nearest to this. If two floating-point numbers
701 are equally distant from the ideal result, the one with a <CODE>0</CODE> in its least
702 significant mantissa bit is chosen.
706 Similarly, testing floating point numbers for equality <SAMP>`x == y'</SAMP>
707 is gambling with random errors. Better check for <SAMP>`abs(x - y) < epsilon'</SAMP>
708 for some well-chosen <CODE>epsilon</CODE>.
712 Floating point numbers come in four flavors:
720 Short floats, type <CODE>cl_SF</CODE>.
721 They have 1 sign bit, 8 exponent bits (including the exponent's sign),
722 and 17 mantissa bits (including the "hidden" bit).
723 They don't consume heap allocation.
728 Single floats, type <CODE>cl_FF</CODE>.
729 They have 1 sign bit, 8 exponent bits (including the exponent's sign),
730 and 24 mantissa bits (including the "hidden" bit).
731 In CLN, they are represented as IEEE single-precision floating point numbers.
732 This corresponds closely to the C/C++ type <SAMP>`float'</SAMP>.
737 Double floats, type <CODE>cl_DF</CODE>.
738 They have 1 sign bit, 11 exponent bits (including the exponent's sign),
739 and 53 mantissa bits (including the "hidden" bit).
740 In CLN, they are represented as IEEE double-precision floating point numbers.
741 This corresponds closely to the C/C++ type <SAMP>`double'</SAMP>.
746 Long floats, type <CODE>cl_LF</CODE>.
747 They have 1 sign bit, 32 exponent bits (including the exponent's sign),
748 and n mantissa bits (including the "hidden" bit), where n >= 64.
749 The precision of a long float is unlimited, but once created, a long float
750 has a fixed precision. (No "lazy recomputation".)
754 Of course, computations with long floats are more expensive than those
755 with smaller floating-point formats.
759 CLN does not implement features like NaNs, denormalized numbers and
760 gradual underflow. If the exponent range of some floating-point type
761 is too limited for your application, choose another floating-point type
762 with larger exponent range.
767 As a user of CLN, you can forget about the differences between the
768 four floating-point types and just declare all your floating-point
769 variables as being of type <CODE>cl_F</CODE>. This has the advantage that
770 when you change the precision of some computation (say, from <CODE>cl_DF</CODE>
771 to <CODE>cl_LF</CODE>), you don't have to change the code, only the precision
772 of the initial values. Also, many transcendental functions have been
773 declared as returning a <CODE>cl_F</CODE> when the argument is a <CODE>cl_F</CODE>,
774 but such declarations are missing for the types <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>,
775 <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>. (Such declarations would be wrong if
776 the floating point contagion rule happened to change in the future.)
781 <H2><A NAME="SEC14" HREF="cln.html#TOC14">3.3 Complex numbers</A></H2>
787 Complex numbers, as implemented by the class <CODE>cl_N</CODE>, have a real
788 part and an imaginary part, both real numbers. A complex number whose
789 imaginary part is the exact number <CODE>0</CODE> is automatically converted
794 Complex numbers can arise from real numbers alone, for example
795 through application of <CODE>sqrt</CODE> or transcendental functions.
800 <H2><A NAME="SEC15" HREF="cln.html#TOC15">3.4 Conversions</A></H2>
806 Conversions from any class to any its superclasses ("base classes" in
807 C++ terminology) is done automatically.
811 Conversions from the C built-in types <SAMP>`long'</SAMP> and <SAMP>`unsigned long'</SAMP>
812 are provided for the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_R</CODE>,
813 <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
817 Conversions from the C built-in types <SAMP>`int'</SAMP> and <SAMP>`unsigned int'</SAMP>
818 are provided for the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_R</CODE>,
819 <CODE>cl_N</CODE> and <CODE>cl_number</CODE>. However, these conversions emphasize
820 efficiency. Their range is therefore limited:
827 The conversion from <SAMP>`int'</SAMP> works only if the argument is < 2^29 and > -2^29.
830 The conversion from <SAMP>`unsigned int'</SAMP> works only if the argument is < 2^29.
834 In a declaration like <SAMP>`cl_I x = 10;'</SAMP> the C++ compiler is able to
835 do the conversion of <CODE>10</CODE> from <SAMP>`int'</SAMP> to <SAMP>`cl_I'</SAMP> at compile time
836 already. On the other hand, code like <SAMP>`cl_I x = 1000000000;'</SAMP> is
838 So, if you want to be sure that an <SAMP>`int'</SAMP> whose magnitude is not guaranteed
839 to be < 2^29 is correctly converted to a <SAMP>`cl_I'</SAMP>, first convert it to a
840 <SAMP>`long'</SAMP>. Similarly, if a large <SAMP>`unsigned int'</SAMP> is to be converted to a
841 <SAMP>`cl_I'</SAMP>, first convert it to an <SAMP>`unsigned long'</SAMP>.
845 Conversions from the C built-in type <SAMP>`float'</SAMP> are provided for the classes
846 <CODE>cl_FF</CODE>, <CODE>cl_F</CODE>, <CODE>cl_R</CODE>, <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
850 Conversions from the C built-in type <SAMP>`double'</SAMP> are provided for the classes
851 <CODE>cl_DF</CODE>, <CODE>cl_F</CODE>, <CODE>cl_R</CODE>, <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
855 Conversions from <SAMP>`const char *'</SAMP> are provided for the classes
856 <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
857 <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>, <CODE>cl_F</CODE>,
858 <CODE>cl_R</CODE>, <CODE>cl_N</CODE>.
859 The easiest way to specify a value which is outside of the range of the
860 C++ built-in types is therefore to specify it as a string, like this:
864 cl_I order_of_rubiks_cube_group = "43252003274489856000";
868 Note that this conversion is done at runtime, not at compile-time.
872 Conversions from <CODE>cl_I</CODE> to the C built-in types <SAMP>`int'</SAMP>,
873 <SAMP>`unsigned int'</SAMP>, <SAMP>`long'</SAMP>, <SAMP>`unsigned long'</SAMP> are provided through
879 <DT><CODE>int cl_I_to_int (const cl_I& x)</CODE>
882 <DT><CODE>unsigned int cl_I_to_uint (const cl_I& x)</CODE>
885 <DT><CODE>long cl_I_to_long (const cl_I& x)</CODE>
888 <DT><CODE>unsigned long cl_I_to_ulong (const cl_I& x)</CODE>
891 Returns <CODE>x</CODE> as element of the C type <VAR>ctype</VAR>. If <CODE>x</CODE> is not
892 representable in the range of <VAR>ctype</VAR>, a runtime error occurs.
896 Conversions from the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
897 <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>, <CODE>cl_F</CODE> and
899 to the C built-in types <SAMP>`float'</SAMP> and <SAMP>`double'</SAMP> are provided through
905 <DT><CODE>float float_approx (const <VAR>type</VAR>& x)</CODE>
908 <DT><CODE>double double_approx (const <VAR>type</VAR>& x)</CODE>
911 Returns an approximation of <CODE>x</CODE> of C type <VAR>ctype</VAR>.
912 If <CODE>abs(x)</CODE> is too close to 0 (underflow), 0 is returned.
913 If <CODE>abs(x)</CODE> is too large (overflow), an IEEE infinity is returned.
917 Conversions from any class to any of its subclasses ("derived classes" in
918 C++ terminology) are not provided. Instead, you can assert and check
919 that a value belongs to a certain subclass, and return it as element of that
920 class, using the <SAMP>`As'</SAMP> and <SAMP>`The'</SAMP> macros.
922 <CODE>As(<VAR>type</VAR>)(<VAR>value</VAR>)</CODE> checks that <VAR>value</VAR> belongs to
923 <VAR>type</VAR> and returns it as such.
925 <CODE>The(<VAR>type</VAR>)(<VAR>value</VAR>)</CODE> assumes that <VAR>value</VAR> belongs to
926 <VAR>type</VAR> and returns it as such. It is your responsibility to ensure
927 that this assumption is valid.
934 if (!(x >= 0)) abort();
935 cl_I ten_x = The(cl_I)(expt(10,x)); // If x >= 0, 10^x is an integer.
936 // In general, it would be a rational number.
941 <H1><A NAME="SEC16" HREF="cln.html#TOC16">4. Functions on numbers</A></H1>
944 Each of the number classes declares its mathematical operations in the
945 corresponding include file. For example, if your code operates with
946 objects of type <CODE>cl_I</CODE>, it should <CODE>#include <cln/integer.h></CODE>.
951 <H2><A NAME="SEC17" HREF="cln.html#TOC17">4.1 Constructing numbers</A></H2>
954 Here is how to create number objects "from nothing".
959 <H3><A NAME="SEC18" HREF="cln.html#TOC18">4.1.1 Constructing integers</A></H3>
962 <CODE>cl_I</CODE> objects are most easily constructed from C integers and from
963 strings. See section <A HREF="cln.html#SEC15">3.4 Conversions</A>.
968 <H3><A NAME="SEC19" HREF="cln.html#TOC19">4.1.2 Constructing rational numbers</A></H3>
971 <CODE>cl_RA</CODE> objects can be constructed from strings. The syntax
972 for rational numbers is described in section <A HREF="cln.html#SEC45">5.1 Internal and printed representation</A>.
973 Another standard way to produce a rational number is through application
974 of <SAMP>`operator /'</SAMP> or <SAMP>`recip'</SAMP> on integers.
979 <H3><A NAME="SEC20" HREF="cln.html#TOC20">4.1.3 Constructing floating-point numbers</A></H3>
982 <CODE>cl_F</CODE> objects with low precision are most easily constructed from
983 C <SAMP>`float'</SAMP> and <SAMP>`double'</SAMP>. See section <A HREF="cln.html#SEC15">3.4 Conversions</A>.
987 To construct a <CODE>cl_F</CODE> with high precision, you can use the conversion
988 from <SAMP>`const char *'</SAMP>, but you have to specify the desired precision
989 within the string. (See section <A HREF="cln.html#SEC45">5.1 Internal and printed representation</A>.)
993 cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
997 will set <SAMP>`e'</SAMP> to the given value, with a precision of 40 decimal digits.
1001 The programmatic way to construct a <CODE>cl_F</CODE> with high precision is
1002 through the <CODE>cl_float</CODE> conversion function, see
1003 section <A HREF="cln.html#SEC40">4.11.1 Conversion to floating-point numbers</A>. For example, to compute
1004 <CODE>e</CODE> to 40 decimal places, first construct 1.0 to 40 decimal places
1005 and then apply the exponential function:
1008 cl_float_format_t precision = cl_float_format(40);
1009 cl_F e = exp(cl_float(1,precision));
1014 <H3><A NAME="SEC21" HREF="cln.html#TOC21">4.1.4 Constructing complex numbers</A></H3>
1017 Non-real <CODE>cl_N</CODE> objects are normally constructed through the function
1020 cl_N complex (const cl_R& realpart, const cl_R& imagpart)
1024 See section <A HREF="cln.html#SEC24">4.4 Elementary complex functions</A>.
1029 <H2><A NAME="SEC22" HREF="cln.html#TOC22">4.2 Elementary functions</A></H2>
1032 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1033 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1034 defines the following operations:
1039 <DT><CODE><VAR>type</VAR> operator + (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1041 <A NAME="IDX37"></A>
1044 <DT><CODE><VAR>type</VAR> operator - (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1046 <A NAME="IDX38"></A>
1049 <DT><CODE><VAR>type</VAR> operator - (const <VAR>type</VAR>&)</CODE>
1051 Returns the negative of the argument.
1053 <DT><CODE><VAR>type</VAR> plus1 (const <VAR>type</VAR>& x)</CODE>
1055 <A NAME="IDX39"></A>
1056 Returns <CODE>x + 1</CODE>.
1058 <DT><CODE><VAR>type</VAR> minus1 (const <VAR>type</VAR>& x)</CODE>
1060 <A NAME="IDX40"></A>
1061 Returns <CODE>x - 1</CODE>.
1063 <DT><CODE><VAR>type</VAR> operator * (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1065 <A NAME="IDX41"></A>
1068 <DT><CODE><VAR>type</VAR> square (const <VAR>type</VAR>& x)</CODE>
1070 <A NAME="IDX42"></A>
1071 Returns <CODE>x * x</CODE>.
1075 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
1076 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1077 defines the following operations:
1082 <DT><CODE><VAR>type</VAR> operator / (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1084 <A NAME="IDX43"></A>
1087 <DT><CODE><VAR>type</VAR> recip (const <VAR>type</VAR>&)</CODE>
1089 <A NAME="IDX44"></A>
1090 Returns the reciprocal of the argument.
1094 The class <CODE>cl_I</CODE> doesn't define a <SAMP>`/'</SAMP> operation because
1095 in the C/C++ language this operator, applied to integral types,
1096 denotes the <SAMP>`floor'</SAMP> or <SAMP>`truncate'</SAMP> operation (which one of these,
1097 is implementation dependent). (See section <A HREF="cln.html#SEC26">4.6 Rounding functions</A>.)
1098 Instead, <CODE>cl_I</CODE> defines an "exact quotient" function:
1103 <DT><CODE>cl_I exquo (const cl_I& x, const cl_I& y)</CODE>
1105 <A NAME="IDX45"></A>
1106 Checks that <CODE>y</CODE> divides <CODE>x</CODE>, and returns the quotient <CODE>x</CODE>/<CODE>y</CODE>.
1110 The following exponentiation functions are defined:
1115 <DT><CODE>cl_I expt_pos (const cl_I& x, const cl_I& y)</CODE>
1117 <A NAME="IDX46"></A>
1118 <DT><CODE>cl_RA expt_pos (const cl_RA& x, const cl_I& y)</CODE>
1120 <CODE>y</CODE> must be > 0. Returns <CODE>x^y</CODE>.
1122 <DT><CODE>cl_RA expt (const cl_RA& x, const cl_I& y)</CODE>
1124 <A NAME="IDX47"></A>
1125 <DT><CODE>cl_R expt (const cl_R& x, const cl_I& y)</CODE>
1127 <DT><CODE>cl_N expt (const cl_N& x, const cl_I& y)</CODE>
1129 Returns <CODE>x^y</CODE>.
1133 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1134 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1135 defines the following operation:
1140 <DT><CODE><VAR>type</VAR> abs (const <VAR>type</VAR>& x)</CODE>
1142 <A NAME="IDX48"></A>
1143 Returns the absolute value of <CODE>x</CODE>.
1144 This is <CODE>x</CODE> if <CODE>x >= 0</CODE>, and <CODE>-x</CODE> if <CODE>x <= 0</CODE>.
1148 The class <CODE>cl_N</CODE> implements this as follows:
1153 <DT><CODE>cl_R abs (const cl_N x)</CODE>
1155 Returns the absolute value of <CODE>x</CODE>.
1159 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1160 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1161 defines the following operation:
1166 <DT><CODE><VAR>type</VAR> signum (const <VAR>type</VAR>& x)</CODE>
1168 <A NAME="IDX49"></A>
1169 Returns the sign of <CODE>x</CODE>, in the same number format as <CODE>x</CODE>.
1170 This is defined as <CODE>x / abs(x)</CODE> if <CODE>x</CODE> is non-zero, and
1171 <CODE>x</CODE> if <CODE>x</CODE> is zero. If <CODE>x</CODE> is real, the value is either
1177 <H2><A NAME="SEC23" HREF="cln.html#TOC23">4.3 Elementary rational functions</A></H2>
1180 Each of the classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE> defines the following operations:
1185 <DT><CODE>cl_I numerator (const <VAR>type</VAR>& x)</CODE>
1187 <A NAME="IDX50"></A>
1188 Returns the numerator of <CODE>x</CODE>.
1190 <DT><CODE>cl_I denominator (const <VAR>type</VAR>& x)</CODE>
1192 <A NAME="IDX51"></A>
1193 Returns the denominator of <CODE>x</CODE>.
1197 The numerator and denominator of a rational number are normalized in such
1198 a way that they have no factor in common and the denominator is positive.
1203 <H2><A NAME="SEC24" HREF="cln.html#TOC24">4.4 Elementary complex functions</A></H2>
1206 The class <CODE>cl_N</CODE> defines the following operation:
1211 <DT><CODE>cl_N complex (const cl_R& a, const cl_R& b)</CODE>
1213 <A NAME="IDX52"></A>
1214 Returns the complex number <CODE>a+bi</CODE>, that is, the complex number with
1215 real part <CODE>a</CODE> and imaginary part <CODE>b</CODE>.
1219 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE> defines the following operations:
1224 <DT><CODE>cl_R realpart (const <VAR>type</VAR>& x)</CODE>
1226 <A NAME="IDX53"></A>
1227 Returns the real part of <CODE>x</CODE>.
1229 <DT><CODE>cl_R imagpart (const <VAR>type</VAR>& x)</CODE>
1231 <A NAME="IDX54"></A>
1232 Returns the imaginary part of <CODE>x</CODE>.
1234 <DT><CODE><VAR>type</VAR> conjugate (const <VAR>type</VAR>& x)</CODE>
1236 <A NAME="IDX55"></A>
1237 Returns the complex conjugate of <CODE>x</CODE>.
1241 We have the relations
1248 <CODE>x = complex(realpart(x), imagpart(x))</CODE>
1251 <CODE>conjugate(x) = complex(realpart(x), -imagpart(x))</CODE>
1256 <H2><A NAME="SEC25" HREF="cln.html#TOC25">4.5 Comparisons</A></H2>
1258 <A NAME="IDX56"></A>
1262 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1263 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1264 defines the following operations:
1269 <DT><CODE>bool operator == (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1271 <A NAME="IDX57"></A>
1272 <DT><CODE>bool operator != (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1274 <A NAME="IDX58"></A>
1275 Comparison, as in C and C++.
1277 <DT><CODE>uint32 equal_hashcode (const <VAR>type</VAR>&)</CODE>
1279 <A NAME="IDX59"></A>
1280 Returns a 32-bit hash code that is the same for any two numbers which are
1281 the same according to <CODE>==</CODE>. This hash code depends on the number's value,
1282 not its type or precision.
1284 <DT><CODE>cl_boolean zerop (const <VAR>type</VAR>& x)</CODE>
1286 <A NAME="IDX60"></A>
1287 Compare against zero: <CODE>x == 0</CODE>
1291 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1292 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1293 defines the following operations:
1298 <DT><CODE>cl_signean compare (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1300 <A NAME="IDX61"></A>
1301 Compares <CODE>x</CODE> and <CODE>y</CODE>. Returns +1 if <CODE>x</CODE>><CODE>y</CODE>,
1302 -1 if <CODE>x</CODE><<CODE>y</CODE>, 0 if <CODE>x</CODE>=<CODE>y</CODE>.
1304 <DT><CODE>bool operator <= (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1306 <A NAME="IDX62"></A>
1307 <DT><CODE>bool operator < (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1309 <A NAME="IDX63"></A>
1310 <DT><CODE>bool operator >= (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1312 <A NAME="IDX64"></A>
1313 <DT><CODE>bool operator > (const <VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
1315 <A NAME="IDX65"></A>
1316 Comparison, as in C and C++.
1318 <DT><CODE>cl_boolean minusp (const <VAR>type</VAR>& x)</CODE>
1320 <A NAME="IDX66"></A>
1321 Compare against zero: <CODE>x < 0</CODE>
1323 <DT><CODE>cl_boolean plusp (const <VAR>type</VAR>& x)</CODE>
1325 <A NAME="IDX67"></A>
1326 Compare against zero: <CODE>x > 0</CODE>
1328 <DT><CODE><VAR>type</VAR> max (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1330 <A NAME="IDX68"></A>
1331 Return the maximum of <CODE>x</CODE> and <CODE>y</CODE>.
1333 <DT><CODE><VAR>type</VAR> min (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1335 <A NAME="IDX69"></A>
1336 Return the minimum of <CODE>x</CODE> and <CODE>y</CODE>.
1340 When a floating point number and a rational number are compared, the float
1341 is first converted to a rational number using the function <CODE>rational</CODE>.
1342 Since a floating point number actually represents an interval of real numbers,
1343 the result might be surprising.
1344 For example, <CODE>(cl_F)(cl_R)"1/3" == (cl_R)"1/3"</CODE> returns false because
1345 there is no floating point number whose value is exactly <CODE>1/3</CODE>.
1350 <H2><A NAME="SEC26" HREF="cln.html#TOC26">4.6 Rounding functions</A></H2>
1352 <A NAME="IDX70"></A>
1356 When a real number is to be converted to an integer, there is no "best"
1357 rounding. The desired rounding function depends on the application.
1358 The Common Lisp and ISO Lisp standards offer four rounding functions:
1363 <DT><CODE>floor(x)</CODE>
1365 This is the largest integer <=<CODE>x</CODE>.
1367 <DT><CODE>ceiling(x)</CODE>
1369 This is the smallest integer >=<CODE>x</CODE>.
1371 <DT><CODE>truncate(x)</CODE>
1373 Among the integers between 0 and <CODE>x</CODE> (inclusive) the one nearest to <CODE>x</CODE>.
1375 <DT><CODE>round(x)</CODE>
1377 The integer nearest to <CODE>x</CODE>. If <CODE>x</CODE> is exactly halfway between two
1378 integers, choose the even one.
1382 These functions have different advantages:
1386 <CODE>floor</CODE> and <CODE>ceiling</CODE> are translation invariant:
1387 <CODE>floor(x+n) = floor(x) + n</CODE> and <CODE>ceiling(x+n) = ceiling(x) + n</CODE>
1388 for every <CODE>x</CODE> and every integer <CODE>n</CODE>.
1392 On the other hand, <CODE>truncate</CODE> and <CODE>round</CODE> are symmetric:
1393 <CODE>truncate(-x) = -truncate(x)</CODE> and <CODE>round(-x) = -round(x)</CODE>,
1394 and furthermore <CODE>round</CODE> is unbiased: on the "average", it rounds
1395 down exactly as often as it rounds up.
1399 The functions are related like this:
1406 <CODE>ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1</CODE>
1407 for rational numbers <CODE>m/n</CODE> (<CODE>m</CODE>, <CODE>n</CODE> integers, <CODE>n</CODE>>0), and
1410 <CODE>truncate(x) = sign(x) * floor(abs(x))</CODE>
1414 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
1415 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1416 defines the following operations:
1421 <DT><CODE>cl_I floor1 (const <VAR>type</VAR>& x)</CODE>
1423 <A NAME="IDX71"></A>
1424 Returns <CODE>floor(x)</CODE>.
1425 <DT><CODE>cl_I ceiling1 (const <VAR>type</VAR>& x)</CODE>
1427 <A NAME="IDX72"></A>
1428 Returns <CODE>ceiling(x)</CODE>.
1429 <DT><CODE>cl_I truncate1 (const <VAR>type</VAR>& x)</CODE>
1431 <A NAME="IDX73"></A>
1432 Returns <CODE>truncate(x)</CODE>.
1433 <DT><CODE>cl_I round1 (const <VAR>type</VAR>& x)</CODE>
1435 <A NAME="IDX74"></A>
1436 Returns <CODE>round(x)</CODE>.
1440 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1441 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1442 defines the following operations:
1447 <DT><CODE>cl_I floor1 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1449 Returns <CODE>floor(x/y)</CODE>.
1450 <DT><CODE>cl_I ceiling1 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1452 Returns <CODE>ceiling(x/y)</CODE>.
1453 <DT><CODE>cl_I truncate1 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1455 Returns <CODE>truncate(x/y)</CODE>.
1456 <DT><CODE>cl_I round1 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1458 Returns <CODE>round(x/y)</CODE>.
1462 These functions are called <SAMP>`floor1'</SAMP>, ... here instead of
1463 <SAMP>`floor'</SAMP>, ..., because on some systems, system dependent include
1464 files define <SAMP>`floor'</SAMP> and <SAMP>`ceiling'</SAMP> as macros.
1468 In many cases, one needs both the quotient and the remainder of a division.
1469 It is more efficient to compute both at the same time than to perform
1470 two divisions, one for quotient and the next one for the remainder.
1471 The following functions therefore return a structure containing both
1472 the quotient and the remainder. The suffix <SAMP>`2'</SAMP> indicates the number
1473 of "return values". The remainder is defined as follows:
1480 for the computation of <CODE>quotient = floor(x)</CODE>,
1481 <CODE>remainder = x - quotient</CODE>,
1484 for the computation of <CODE>quotient = floor(x,y)</CODE>,
1485 <CODE>remainder = x - quotient*y</CODE>,
1489 and similarly for the other three operations.
1493 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
1494 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1495 defines the following operations:
1500 <DT><CODE>struct <VAR>type</VAR>_div_t { cl_I quotient; <VAR>type</VAR> remainder; };</CODE>
1502 <DT><CODE><VAR>type</VAR>_div_t floor2 (const <VAR>type</VAR>& x)</CODE>
1504 <DT><CODE><VAR>type</VAR>_div_t ceiling2 (const <VAR>type</VAR>& x)</CODE>
1506 <DT><CODE><VAR>type</VAR>_div_t truncate2 (const <VAR>type</VAR>& x)</CODE>
1508 <DT><CODE><VAR>type</VAR>_div_t round2 (const <VAR>type</VAR>& x)</CODE>
1513 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
1514 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1515 defines the following operations:
1520 <DT><CODE>struct <VAR>type</VAR>_div_t { cl_I quotient; <VAR>type</VAR> remainder; };</CODE>
1522 <DT><CODE><VAR>type</VAR>_div_t floor2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1524 <A NAME="IDX75"></A>
1525 <DT><CODE><VAR>type</VAR>_div_t ceiling2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1527 <A NAME="IDX76"></A>
1528 <DT><CODE><VAR>type</VAR>_div_t truncate2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1530 <A NAME="IDX77"></A>
1531 <DT><CODE><VAR>type</VAR>_div_t round2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1533 <A NAME="IDX78"></A>
1537 Sometimes, one wants the quotient as a floating-point number (of the
1538 same format as the argument, if the argument is a float) instead of as
1539 an integer. The prefix <SAMP>`f'</SAMP> indicates this.
1544 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1545 defines the following operations:
1550 <DT><CODE><VAR>type</VAR> ffloor (const <VAR>type</VAR>& x)</CODE>
1552 <A NAME="IDX79"></A>
1553 <DT><CODE><VAR>type</VAR> fceiling (const <VAR>type</VAR>& x)</CODE>
1555 <A NAME="IDX80"></A>
1556 <DT><CODE><VAR>type</VAR> ftruncate (const <VAR>type</VAR>& x)</CODE>
1558 <A NAME="IDX81"></A>
1559 <DT><CODE><VAR>type</VAR> fround (const <VAR>type</VAR>& x)</CODE>
1561 <A NAME="IDX82"></A>
1565 and similarly for class <CODE>cl_R</CODE>, but with return type <CODE>cl_F</CODE>.
1569 The class <CODE>cl_R</CODE> defines the following operations:
1574 <DT><CODE>cl_F ffloor (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1576 <DT><CODE>cl_F fceiling (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1578 <DT><CODE>cl_F ftruncate (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1580 <DT><CODE>cl_F fround (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1585 These functions also exist in versions which return both the quotient
1586 and the remainder. The suffix <SAMP>`2'</SAMP> indicates this.
1591 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1592 defines the following operations:
1593 <A NAME="IDX83"></A>
1594 <A NAME="IDX84"></A>
1595 <A NAME="IDX85"></A>
1596 <A NAME="IDX86"></A>
1597 <A NAME="IDX87"></A>
1602 <DT><CODE>struct <VAR>type</VAR>_fdiv_t { <VAR>type</VAR> quotient; <VAR>type</VAR> remainder; };</CODE>
1604 <DT><CODE><VAR>type</VAR>_fdiv_t ffloor2 (const <VAR>type</VAR>& x)</CODE>
1606 <A NAME="IDX88"></A>
1607 <DT><CODE><VAR>type</VAR>_fdiv_t fceiling2 (const <VAR>type</VAR>& x)</CODE>
1609 <A NAME="IDX89"></A>
1610 <DT><CODE><VAR>type</VAR>_fdiv_t ftruncate2 (const <VAR>type</VAR>& x)</CODE>
1612 <A NAME="IDX90"></A>
1613 <DT><CODE><VAR>type</VAR>_fdiv_t fround2 (const <VAR>type</VAR>& x)</CODE>
1615 <A NAME="IDX91"></A>
1618 and similarly for class <CODE>cl_R</CODE>, but with quotient type <CODE>cl_F</CODE>.
1619 <A NAME="IDX92"></A>
1623 The class <CODE>cl_R</CODE> defines the following operations:
1628 <DT><CODE>struct <VAR>type</VAR>_fdiv_t { cl_F quotient; cl_R remainder; };</CODE>
1630 <DT><CODE><VAR>type</VAR>_fdiv_t ffloor2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1632 <DT><CODE><VAR>type</VAR>_fdiv_t fceiling2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1634 <DT><CODE><VAR>type</VAR>_fdiv_t ftruncate2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1636 <DT><CODE><VAR>type</VAR>_fdiv_t fround2 (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1641 Other applications need only the remainder of a division.
1642 The remainder of <SAMP>`floor'</SAMP> and <SAMP>`ffloor'</SAMP> is called <SAMP>`mod'</SAMP>
1643 (abbreviation of "modulo"). The remainder <SAMP>`truncate'</SAMP> and
1644 <SAMP>`ftruncate'</SAMP> is called <SAMP>`rem'</SAMP> (abbreviation of "remainder").
1651 <CODE>mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y</CODE>
1654 <CODE>rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y</CODE>
1658 If <CODE>x</CODE> and <CODE>y</CODE> are both >= 0, <CODE>mod(x,y) = rem(x,y) >= 0</CODE>.
1659 In general, <CODE>mod(x,y)</CODE> has the sign of <CODE>y</CODE> or is zero,
1660 and <CODE>rem(x,y)</CODE> has the sign of <CODE>x</CODE> or is zero.
1664 The classes <CODE>cl_R</CODE>, <CODE>cl_I</CODE> define the following operations:
1669 <DT><CODE><VAR>type</VAR> mod (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1671 <A NAME="IDX93"></A>
1672 <DT><CODE><VAR>type</VAR> rem (const <VAR>type</VAR>& x, const <VAR>type</VAR>& y)</CODE>
1674 <A NAME="IDX94"></A>
1679 <H2><A NAME="SEC27" HREF="cln.html#TOC27">4.7 Roots</A></H2>
1682 Each of the classes <CODE>cl_R</CODE>,
1683 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
1684 defines the following operation:
1689 <DT><CODE><VAR>type</VAR> sqrt (const <VAR>type</VAR>& x)</CODE>
1691 <A NAME="IDX95"></A>
1692 <CODE>x</CODE> must be >= 0. This function returns the square root of <CODE>x</CODE>,
1693 normalized to be >= 0. If <CODE>x</CODE> is the square of a rational number,
1694 <CODE>sqrt(x)</CODE> will be a rational number, else it will return a
1695 floating-point approximation.
1699 The classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE> define the following operation:
1704 <DT><CODE>cl_boolean sqrtp (const <VAR>type</VAR>& x, <VAR>type</VAR>* root)</CODE>
1706 <A NAME="IDX96"></A>
1707 This tests whether <CODE>x</CODE> is a perfect square. If so, it returns true
1708 and the exact square root in <CODE>*root</CODE>, else it returns false.
1712 Furthermore, for integers, similarly:
1717 <DT><CODE>cl_boolean isqrt (const <VAR>type</VAR>& x, <VAR>type</VAR>* root)</CODE>
1719 <A NAME="IDX97"></A>
1720 <CODE>x</CODE> should be >= 0. This function sets <CODE>*root</CODE> to
1721 <CODE>floor(sqrt(x))</CODE> and returns the same value as <CODE>sqrtp</CODE>:
1722 the boolean value <CODE>(expt(*root,2) == x)</CODE>.
1726 For <CODE>n</CODE>th roots, the classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>
1727 define the following operation:
1732 <DT><CODE>cl_boolean rootp (const <VAR>type</VAR>& x, const cl_I& n, <VAR>type</VAR>* root)</CODE>
1734 <A NAME="IDX98"></A>
1735 <CODE>x</CODE> must be >= 0. <CODE>n</CODE> must be > 0.
1736 This tests whether <CODE>x</CODE> is an <CODE>n</CODE>th power of a rational number.
1737 If so, it returns true and the exact root in <CODE>*root</CODE>, else it returns
1742 The only square root function which accepts negative numbers is the one
1743 for class <CODE>cl_N</CODE>:
1748 <DT><CODE>cl_N sqrt (const cl_N& z)</CODE>
1750 <A NAME="IDX99"></A>
1751 Returns the square root of <CODE>z</CODE>, as defined by the formula
1752 <CODE>sqrt(z) = exp(log(z)/2)</CODE>. Conversion to a floating-point type
1753 or to a complex number are done if necessary. The range of the result is the
1754 right half plane <CODE>realpart(sqrt(z)) >= 0</CODE>
1755 including the positive imaginary axis and 0, but excluding
1756 the negative imaginary axis.
1757 The result is an exact number only if <CODE>z</CODE> is an exact number.
1762 <H2><A NAME="SEC28" HREF="cln.html#TOC28">4.8 Transcendental functions</A></H2>
1764 <A NAME="IDX100"></A>
1768 The transcendental functions return an exact result if the argument
1769 is exact and the result is exact as well. Otherwise they must return
1770 inexact numbers even if the argument is exact.
1771 For example, <CODE>cos(0) = 1</CODE> returns the rational number <CODE>1</CODE>.
1776 <H3><A NAME="SEC29" HREF="cln.html#TOC29">4.8.1 Exponential and logarithmic functions</A></H3>
1780 <DT><CODE>cl_R exp (const cl_R& x)</CODE>
1782 <A NAME="IDX101"></A>
1783 <DT><CODE>cl_N exp (const cl_N& x)</CODE>
1785 Returns the exponential function of <CODE>x</CODE>. This is <CODE>e^x</CODE> where
1786 <CODE>e</CODE> is the base of the natural logarithms. The range of the result
1787 is the entire complex plane excluding 0.
1789 <DT><CODE>cl_R ln (const cl_R& x)</CODE>
1791 <A NAME="IDX102"></A>
1792 <CODE>x</CODE> must be > 0. Returns the (natural) logarithm of x.
1794 <DT><CODE>cl_N log (const cl_N& x)</CODE>
1796 <A NAME="IDX103"></A>
1797 Returns the (natural) logarithm of x. If <CODE>x</CODE> is real and positive,
1798 this is <CODE>ln(x)</CODE>. In general, <CODE>log(x) = log(abs(x)) + i*phase(x)</CODE>.
1799 The range of the result is the strip in the complex plane
1800 <CODE>-pi < imagpart(log(x)) <= pi</CODE>.
1802 <DT><CODE>cl_R phase (const cl_N& x)</CODE>
1804 <A NAME="IDX104"></A>
1805 Returns the angle part of <CODE>x</CODE> in its polar representation as a
1806 complex number. That is, <CODE>phase(x) = atan(realpart(x),imagpart(x))</CODE>.
1807 This is also the imaginary part of <CODE>log(x)</CODE>.
1808 The range of the result is the interval <CODE>-pi < phase(x) <= pi</CODE>.
1809 The result will be an exact number only if <CODE>zerop(x)</CODE> or
1810 if <CODE>x</CODE> is real and positive.
1812 <DT><CODE>cl_R log (const cl_R& a, const cl_R& b)</CODE>
1814 <CODE>a</CODE> and <CODE>b</CODE> must be > 0. Returns the logarithm of <CODE>a</CODE> with
1815 respect to base <CODE>b</CODE>. <CODE>log(a,b) = ln(a)/ln(b)</CODE>.
1816 The result can be exact only if <CODE>a = 1</CODE> or if <CODE>a</CODE> and <CODE>b</CODE>
1819 <DT><CODE>cl_N log (const cl_N& a, const cl_N& b)</CODE>
1821 Returns the logarithm of <CODE>a</CODE> with respect to base <CODE>b</CODE>.
1822 <CODE>log(a,b) = log(a)/log(b)</CODE>.
1824 <DT><CODE>cl_N expt (const cl_N& x, const cl_N& y)</CODE>
1826 <A NAME="IDX105"></A>
1827 Exponentiation: Returns <CODE>x^y = exp(y*log(x))</CODE>.
1831 The constant e = exp(1) = 2.71828... is returned by the following functions:
1836 <DT><CODE>cl_F exp1 (cl_float_format_t f)</CODE>
1838 <A NAME="IDX106"></A>
1839 Returns e as a float of format <CODE>f</CODE>.
1841 <DT><CODE>cl_F exp1 (const cl_F& y)</CODE>
1843 Returns e in the float format of <CODE>y</CODE>.
1845 <DT><CODE>cl_F exp1 (void)</CODE>
1847 Returns e as a float of format <CODE>default_float_format</CODE>.
1852 <H3><A NAME="SEC30" HREF="cln.html#TOC30">4.8.2 Trigonometric functions</A></H3>
1856 <DT><CODE>cl_R sin (const cl_R& x)</CODE>
1858 <A NAME="IDX107"></A>
1859 Returns <CODE>sin(x)</CODE>. The range of the result is the interval
1860 <CODE>-1 <= sin(x) <= 1</CODE>.
1862 <DT><CODE>cl_N sin (const cl_N& z)</CODE>
1864 Returns <CODE>sin(z)</CODE>. The range of the result is the entire complex plane.
1866 <DT><CODE>cl_R cos (const cl_R& x)</CODE>
1868 <A NAME="IDX108"></A>
1869 Returns <CODE>cos(x)</CODE>. The range of the result is the interval
1870 <CODE>-1 <= cos(x) <= 1</CODE>.
1872 <DT><CODE>cl_N cos (const cl_N& x)</CODE>
1874 Returns <CODE>cos(z)</CODE>. The range of the result is the entire complex plane.
1876 <DT><CODE>struct cos_sin_t { cl_R cos; cl_R sin; };</CODE>
1878 <A NAME="IDX109"></A>
1879 <DT><CODE>cos_sin_t cos_sin (const cl_R& x)</CODE>
1881 Returns both <CODE>sin(x)</CODE> and <CODE>cos(x)</CODE>. This is more efficient than
1882 <A NAME="IDX110"></A>
1883 computing them separately. The relation <CODE>cos^2 + sin^2 = 1</CODE> will
1884 hold only approximately.
1886 <DT><CODE>cl_R tan (const cl_R& x)</CODE>
1888 <A NAME="IDX111"></A>
1889 <DT><CODE>cl_N tan (const cl_N& x)</CODE>
1891 Returns <CODE>tan(x) = sin(x)/cos(x)</CODE>.
1893 <DT><CODE>cl_N cis (const cl_R& x)</CODE>
1895 <A NAME="IDX112"></A>
1896 <DT><CODE>cl_N cis (const cl_N& x)</CODE>
1898 Returns <CODE>exp(i*x)</CODE>. The name <SAMP>`cis'</SAMP> means "cos + i sin", because
1899 <CODE>e^(i*x) = cos(x) + i*sin(x)</CODE>.
1901 <A NAME="IDX113"></A>
1902 <A NAME="IDX114"></A>
1903 <DT><CODE>cl_N asin (const cl_N& z)</CODE>
1905 Returns <CODE>arcsin(z)</CODE>. This is defined as
1906 <CODE>arcsin(z) = log(iz+sqrt(1-z^2))/i</CODE> and satisfies
1907 <CODE>arcsin(-z) = -arcsin(z)</CODE>.
1908 The range of the result is the strip in the complex domain
1909 <CODE>-pi/2 <= realpart(arcsin(z)) <= pi/2</CODE>, excluding the numbers
1910 with <CODE>realpart = -pi/2</CODE> and <CODE>imagpart < 0</CODE> and the numbers
1911 with <CODE>realpart = pi/2</CODE> and <CODE>imagpart > 0</CODE>.
1913 <DT><CODE>cl_N acos (const cl_N& z)</CODE>
1915 <A NAME="IDX115"></A>
1916 Returns <CODE>arccos(z)</CODE>. This is defined as
1917 <CODE>arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i</CODE>
1918 and satisfies <CODE>arccos(-z) = pi - arccos(z)</CODE>.
1919 The range of the result is the strip in the complex domain
1920 <CODE>0 <= realpart(arcsin(z)) <= pi</CODE>, excluding the numbers
1921 with <CODE>realpart = 0</CODE> and <CODE>imagpart < 0</CODE> and the numbers
1922 with <CODE>realpart = pi</CODE> and <CODE>imagpart > 0</CODE>.
1924 <A NAME="IDX116"></A>
1925 <A NAME="IDX117"></A>
1926 <DT><CODE>cl_R atan (const cl_R& x, const cl_R& y)</CODE>
1928 Returns the angle of the polar representation of the complex number
1929 <CODE>x+iy</CODE>. This is <CODE>atan(y/x)</CODE> if <CODE>x>0</CODE>. The range of
1930 the result is the interval <CODE>-pi < atan(x,y) <= pi</CODE>. The result will
1931 be an exact number only if <CODE>x > 0</CODE> and <CODE>y</CODE> is the exact <CODE>0</CODE>.
1932 WARNING: In Common Lisp, this function is called as <CODE>(atan y x)</CODE>,
1933 with reversed order of arguments.
1935 <DT><CODE>cl_R atan (const cl_R& x)</CODE>
1937 Returns <CODE>arctan(x)</CODE>. This is the same as <CODE>atan(1,x)</CODE>. The range
1938 of the result is the interval <CODE>-pi/2 < atan(x) < pi/2</CODE>. The result
1939 will be an exact number only if <CODE>x</CODE> is the exact <CODE>0</CODE>.
1941 <DT><CODE>cl_N atan (const cl_N& z)</CODE>
1943 Returns <CODE>arctan(z)</CODE>. This is defined as
1944 <CODE>arctan(z) = (log(1+iz)-log(1-iz)) / 2i</CODE> and satisfies
1945 <CODE>arctan(-z) = -arctan(z)</CODE>. The range of the result is
1946 the strip in the complex domain
1947 <CODE>-pi/2 <= realpart(arctan(z)) <= pi/2</CODE>, excluding the numbers
1948 with <CODE>realpart = -pi/2</CODE> and <CODE>imagpart >= 0</CODE> and the numbers
1949 with <CODE>realpart = pi/2</CODE> and <CODE>imagpart <= 0</CODE>.
1954 <A NAME="IDX118"></A>
1955 <A NAME="IDX119"></A>
1956 Archimedes' constant pi = 3.14... is returned by the following functions:
1961 <DT><CODE>cl_F pi (cl_float_format_t f)</CODE>
1963 <A NAME="IDX120"></A>
1964 Returns pi as a float of format <CODE>f</CODE>.
1966 <DT><CODE>cl_F pi (const cl_F& y)</CODE>
1968 Returns pi in the float format of <CODE>y</CODE>.
1970 <DT><CODE>cl_F pi (void)</CODE>
1972 Returns pi as a float of format <CODE>default_float_format</CODE>.
1977 <H3><A NAME="SEC31" HREF="cln.html#TOC31">4.8.3 Hyperbolic functions</A></H3>
1981 <DT><CODE>cl_R sinh (const cl_R& x)</CODE>
1983 <A NAME="IDX121"></A>
1984 Returns <CODE>sinh(x)</CODE>.
1986 <DT><CODE>cl_N sinh (const cl_N& z)</CODE>
1988 Returns <CODE>sinh(z)</CODE>. The range of the result is the entire complex plane.
1990 <DT><CODE>cl_R cosh (const cl_R& x)</CODE>
1992 <A NAME="IDX122"></A>
1993 Returns <CODE>cosh(x)</CODE>. The range of the result is the interval
1994 <CODE>cosh(x) >= 1</CODE>.
1996 <DT><CODE>cl_N cosh (const cl_N& z)</CODE>
1998 Returns <CODE>cosh(z)</CODE>. The range of the result is the entire complex plane.
2000 <DT><CODE>struct cosh_sinh_t { cl_R cosh; cl_R sinh; };</CODE>
2002 <A NAME="IDX123"></A>
2003 <DT><CODE>cosh_sinh_t cosh_sinh (const cl_R& x)</CODE>
2005 <A NAME="IDX124"></A>
2006 Returns both <CODE>sinh(x)</CODE> and <CODE>cosh(x)</CODE>. This is more efficient than
2007 computing them separately. The relation <CODE>cosh^2 - sinh^2 = 1</CODE> will
2008 hold only approximately.
2010 <DT><CODE>cl_R tanh (const cl_R& x)</CODE>
2012 <A NAME="IDX125"></A>
2013 <DT><CODE>cl_N tanh (const cl_N& x)</CODE>
2015 Returns <CODE>tanh(x) = sinh(x)/cosh(x)</CODE>.
2017 <DT><CODE>cl_N asinh (const cl_N& z)</CODE>
2019 <A NAME="IDX126"></A>
2020 Returns <CODE>arsinh(z)</CODE>. This is defined as
2021 <CODE>arsinh(z) = log(z+sqrt(1+z^2))</CODE> and satisfies
2022 <CODE>arsinh(-z) = -arsinh(z)</CODE>.
2023 The range of the result is the strip in the complex domain
2024 <CODE>-pi/2 <= imagpart(arsinh(z)) <= pi/2</CODE>, excluding the numbers
2025 with <CODE>imagpart = -pi/2</CODE> and <CODE>realpart > 0</CODE> and the numbers
2026 with <CODE>imagpart = pi/2</CODE> and <CODE>realpart < 0</CODE>.
2028 <DT><CODE>cl_N acosh (const cl_N& z)</CODE>
2030 <A NAME="IDX127"></A>
2031 Returns <CODE>arcosh(z)</CODE>. This is defined as
2032 <CODE>arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))</CODE>.
2033 The range of the result is the half-strip in the complex domain
2034 <CODE>-pi < imagpart(arcosh(z)) <= pi, realpart(arcosh(z)) >= 0</CODE>,
2035 excluding the numbers with <CODE>realpart = 0</CODE> and <CODE>-pi < imagpart < 0</CODE>.
2037 <DT><CODE>cl_N atanh (const cl_N& z)</CODE>
2039 <A NAME="IDX128"></A>
2040 Returns <CODE>artanh(z)</CODE>. This is defined as
2041 <CODE>artanh(z) = (log(1+z)-log(1-z)) / 2</CODE> and satisfies
2042 <CODE>artanh(-z) = -artanh(z)</CODE>. The range of the result is
2043 the strip in the complex domain
2044 <CODE>-pi/2 <= imagpart(artanh(z)) <= pi/2</CODE>, excluding the numbers
2045 with <CODE>imagpart = -pi/2</CODE> and <CODE>realpart <= 0</CODE> and the numbers
2046 with <CODE>imagpart = pi/2</CODE> and <CODE>realpart >= 0</CODE>.
2051 <H3><A NAME="SEC32" HREF="cln.html#TOC32">4.8.4 Euler gamma</A></H3>
2053 <A NAME="IDX129"></A>
2057 Euler's constant C = 0.577... is returned by the following functions:
2062 <DT><CODE>cl_F eulerconst (cl_float_format_t f)</CODE>
2064 <A NAME="IDX130"></A>
2065 Returns Euler's constant as a float of format <CODE>f</CODE>.
2067 <DT><CODE>cl_F eulerconst (const cl_F& y)</CODE>
2069 Returns Euler's constant in the float format of <CODE>y</CODE>.
2071 <DT><CODE>cl_F eulerconst (void)</CODE>
2073 Returns Euler's constant as a float of format <CODE>default_float_format</CODE>.
2077 Catalan's constant G = 0.915... is returned by the following functions:
2078 <A NAME="IDX131"></A>
2083 <DT><CODE>cl_F catalanconst (cl_float_format_t f)</CODE>
2085 <A NAME="IDX132"></A>
2086 Returns Catalan's constant as a float of format <CODE>f</CODE>.
2088 <DT><CODE>cl_F catalanconst (const cl_F& y)</CODE>
2090 Returns Catalan's constant in the float format of <CODE>y</CODE>.
2092 <DT><CODE>cl_F catalanconst (void)</CODE>
2094 Returns Catalan's constant as a float of format <CODE>default_float_format</CODE>.
2099 <H3><A NAME="SEC33" HREF="cln.html#TOC33">4.8.5 Riemann zeta</A></H3>
2101 <A NAME="IDX133"></A>
2105 Riemann's zeta function at an integral point <CODE>s>1</CODE> is returned by the
2106 following functions:
2111 <DT><CODE>cl_F zeta (int s, cl_float_format_t f)</CODE>
2113 <A NAME="IDX134"></A>
2114 Returns Riemann's zeta function at <CODE>s</CODE> as a float of format <CODE>f</CODE>.
2116 <DT><CODE>cl_F zeta (int s, const cl_F& y)</CODE>
2118 Returns Riemann's zeta function at <CODE>s</CODE> in the float format of <CODE>y</CODE>.
2120 <DT><CODE>cl_F zeta (int s)</CODE>
2122 Returns Riemann's zeta function at <CODE>s</CODE> as a float of format
2123 <CODE>default_float_format</CODE>.
2128 <H2><A NAME="SEC34" HREF="cln.html#TOC34">4.9 Functions on integers</A></H2>
2132 <H3><A NAME="SEC35" HREF="cln.html#TOC35">4.9.1 Logical functions</A></H3>
2135 Integers, when viewed as in two's complement notation, can be thought as
2136 infinite bit strings where the bits' values eventually are constant.
2145 The logical operations view integers as such bit strings and operate
2146 on each of the bit positions in parallel.
2151 <DT><CODE>cl_I lognot (const cl_I& x)</CODE>
2153 <A NAME="IDX135"></A>
2154 <DT><CODE>cl_I operator ~ (const cl_I& x)</CODE>
2156 <A NAME="IDX136"></A>
2157 Logical not, like <CODE>~x</CODE> in C. This is the same as <CODE>-1-x</CODE>.
2159 <DT><CODE>cl_I logand (const cl_I& x, const cl_I& y)</CODE>
2161 <A NAME="IDX137"></A>
2162 <DT><CODE>cl_I operator & (const cl_I& x, const cl_I& y)</CODE>
2164 <A NAME="IDX138"></A>
2165 Logical and, like <CODE>x & y</CODE> in C.
2167 <DT><CODE>cl_I logior (const cl_I& x, const cl_I& y)</CODE>
2169 <A NAME="IDX139"></A>
2170 <DT><CODE>cl_I operator | (const cl_I& x, const cl_I& y)</CODE>
2172 <A NAME="IDX140"></A>
2173 Logical (inclusive) or, like <CODE>x | y</CODE> in C.
2175 <DT><CODE>cl_I logxor (const cl_I& x, const cl_I& y)</CODE>
2177 <A NAME="IDX141"></A>
2178 <DT><CODE>cl_I operator ^ (const cl_I& x, const cl_I& y)</CODE>
2180 <A NAME="IDX142"></A>
2181 Exclusive or, like <CODE>x ^ y</CODE> in C.
2183 <DT><CODE>cl_I logeqv (const cl_I& x, const cl_I& y)</CODE>
2185 <A NAME="IDX143"></A>
2186 Bitwise equivalence, like <CODE>~(x ^ y)</CODE> in C.
2188 <DT><CODE>cl_I lognand (const cl_I& x, const cl_I& y)</CODE>
2190 <A NAME="IDX144"></A>
2191 Bitwise not and, like <CODE>~(x & y)</CODE> in C.
2193 <DT><CODE>cl_I lognor (const cl_I& x, const cl_I& y)</CODE>
2195 <A NAME="IDX145"></A>
2196 Bitwise not or, like <CODE>~(x | y)</CODE> in C.
2198 <DT><CODE>cl_I logandc1 (const cl_I& x, const cl_I& y)</CODE>
2200 <A NAME="IDX146"></A>
2201 Logical and, complementing the first argument, like <CODE>~x & y</CODE> in C.
2203 <DT><CODE>cl_I logandc2 (const cl_I& x, const cl_I& y)</CODE>
2205 <A NAME="IDX147"></A>
2206 Logical and, complementing the second argument, like <CODE>x & ~y</CODE> in C.
2208 <DT><CODE>cl_I logorc1 (const cl_I& x, const cl_I& y)</CODE>
2210 <A NAME="IDX148"></A>
2211 Logical or, complementing the first argument, like <CODE>~x | y</CODE> in C.
2213 <DT><CODE>cl_I logorc2 (const cl_I& x, const cl_I& y)</CODE>
2215 <A NAME="IDX149"></A>
2216 Logical or, complementing the second argument, like <CODE>x | ~y</CODE> in C.
2220 These operations are all available though the function
2223 <DT><CODE>cl_I boole (cl_boole op, const cl_I& x, const cl_I& y)</CODE>
2225 <A NAME="IDX150"></A>
2228 where <CODE>op</CODE> must have one of the 16 values (each one stands for a function
2229 which combines two bits into one bit): <CODE>boole_clr</CODE>, <CODE>boole_set</CODE>,
2230 <CODE>boole_1</CODE>, <CODE>boole_2</CODE>, <CODE>boole_c1</CODE>, <CODE>boole_c2</CODE>,
2231 <CODE>boole_and</CODE>, <CODE>boole_ior</CODE>, <CODE>boole_xor</CODE>, <CODE>boole_eqv</CODE>,
2232 <CODE>boole_nand</CODE>, <CODE>boole_nor</CODE>, <CODE>boole_andc1</CODE>, <CODE>boole_andc2</CODE>,
2233 <CODE>boole_orc1</CODE>, <CODE>boole_orc2</CODE>.
2234 <A NAME="IDX151"></A>
2235 <A NAME="IDX152"></A>
2236 <A NAME="IDX153"></A>
2237 <A NAME="IDX154"></A>
2238 <A NAME="IDX155"></A>
2239 <A NAME="IDX156"></A>
2240 <A NAME="IDX157"></A>
2241 <A NAME="IDX158"></A>
2242 <A NAME="IDX159"></A>
2243 <A NAME="IDX160"></A>
2244 <A NAME="IDX161"></A>
2245 <A NAME="IDX162"></A>
2246 <A NAME="IDX163"></A>
2247 <A NAME="IDX164"></A>
2248 <A NAME="IDX165"></A>
2252 Other functions that view integers as bit strings:
2257 <DT><CODE>cl_boolean logtest (const cl_I& x, const cl_I& y)</CODE>
2259 <A NAME="IDX166"></A>
2260 Returns true if some bit is set in both <CODE>x</CODE> and <CODE>y</CODE>, i.e. if
2261 <CODE>logand(x,y) != 0</CODE>.
2263 <DT><CODE>cl_boolean logbitp (const cl_I& n, const cl_I& x)</CODE>
2265 <A NAME="IDX167"></A>
2266 Returns true if the <CODE>n</CODE>th bit (from the right) of <CODE>x</CODE> is set.
2267 Bit 0 is the least significant bit.
2269 <DT><CODE>uintL logcount (const cl_I& x)</CODE>
2271 <A NAME="IDX168"></A>
2272 Returns the number of one bits in <CODE>x</CODE>, if <CODE>x</CODE> >= 0, or
2273 the number of zero bits in <CODE>x</CODE>, if <CODE>x</CODE> < 0.
2277 The following functions operate on intervals of bits in integers.
2281 struct cl_byte { uintL size; uintL position; };
2285 <A NAME="IDX169"></A>
2286 represents the bit interval containing the bits
2287 <CODE>position</CODE>...<CODE>position+size-1</CODE> of an integer.
2288 The constructor <CODE>cl_byte(size,position)</CODE> constructs a <CODE>cl_byte</CODE>.
2293 <DT><CODE>cl_I ldb (const cl_I& n, const cl_byte& b)</CODE>
2295 <A NAME="IDX170"></A>
2296 extracts the bits of <CODE>n</CODE> described by the bit interval <CODE>b</CODE>
2297 and returns them as a nonnegative integer with <CODE>b.size</CODE> bits.
2299 <DT><CODE>cl_boolean ldb_test (const cl_I& n, const cl_byte& b)</CODE>
2301 <A NAME="IDX171"></A>
2302 Returns true if some bit described by the bit interval <CODE>b</CODE> is set in
2305 <DT><CODE>cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)</CODE>
2307 <A NAME="IDX172"></A>
2308 Returns <CODE>n</CODE>, with the bits described by the bit interval <CODE>b</CODE>
2309 replaced by <CODE>newbyte</CODE>. Only the lowest <CODE>b.size</CODE> bits of
2310 <CODE>newbyte</CODE> are relevant.
2314 The functions <CODE>ldb</CODE> and <CODE>dpb</CODE> implicitly shift. The following
2315 functions are their counterparts without shifting:
2320 <DT><CODE>cl_I mask_field (const cl_I& n, const cl_byte& b)</CODE>
2322 <A NAME="IDX173"></A>
2323 returns an integer with the bits described by the bit interval <CODE>b</CODE>
2324 copied from the corresponding bits in <CODE>n</CODE>, the other bits zero.
2326 <DT><CODE>cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)</CODE>
2328 <A NAME="IDX174"></A>
2329 returns an integer where the bits described by the bit interval <CODE>b</CODE>
2330 come from <CODE>newbyte</CODE> and the other bits come from <CODE>n</CODE>.
2334 The following relations hold:
2341 <CODE>ldb (n, b) = mask_field(n, b) >> b.position</CODE>,
2344 <CODE>dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)</CODE>,
2347 <CODE>deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)</CODE>.
2351 The following operations on integers as bit strings are efficient shortcuts
2352 for common arithmetic operations:
2357 <DT><CODE>cl_boolean oddp (const cl_I& x)</CODE>
2359 <A NAME="IDX175"></A>
2360 Returns true if the least significant bit of <CODE>x</CODE> is 1. Equivalent to
2361 <CODE>mod(x,2) != 0</CODE>.
2363 <DT><CODE>cl_boolean evenp (const cl_I& x)</CODE>
2365 <A NAME="IDX176"></A>
2366 Returns true if the least significant bit of <CODE>x</CODE> is 0. Equivalent to
2367 <CODE>mod(x,2) == 0</CODE>.
2369 <DT><CODE>cl_I operator << (const cl_I& x, const cl_I& n)</CODE>
2371 <A NAME="IDX177"></A>
2372 Shifts <CODE>x</CODE> by <CODE>n</CODE> bits to the left. <CODE>n</CODE> should be >=0.
2373 Equivalent to <CODE>x * expt(2,n)</CODE>.
2375 <DT><CODE>cl_I operator >> (const cl_I& x, const cl_I& n)</CODE>
2377 <A NAME="IDX178"></A>
2378 Shifts <CODE>x</CODE> by <CODE>n</CODE> bits to the right. <CODE>n</CODE> should be >=0.
2379 Bits shifted out to the right are thrown away.
2380 Equivalent to <CODE>floor(x / expt(2,n))</CODE>.
2382 <DT><CODE>cl_I ash (const cl_I& x, const cl_I& y)</CODE>
2384 <A NAME="IDX179"></A>
2385 Shifts <CODE>x</CODE> by <CODE>y</CODE> bits to the left (if <CODE>y</CODE>>=0) or
2386 by <CODE>-y</CODE> bits to the right (if <CODE>y</CODE><=0). In other words, this
2387 returns <CODE>floor(x * expt(2,y))</CODE>.
2389 <DT><CODE>uintL integer_length (const cl_I& x)</CODE>
2391 <A NAME="IDX180"></A>
2392 Returns the number of bits (excluding the sign bit) needed to represent <CODE>x</CODE>
2393 in two's complement notation. This is the smallest n >= 0 such that
2394 -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
2395 2^(n-1) <= x < 2^n.
2397 <DT><CODE>uintL ord2 (const cl_I& x)</CODE>
2399 <A NAME="IDX181"></A>
2400 <CODE>x</CODE> must be non-zero. This function returns the number of 0 bits at the
2401 right of <CODE>x</CODE> in two's complement notation. This is the largest n >= 0
2402 such that 2^n divides <CODE>x</CODE>.
2404 <DT><CODE>uintL power2p (const cl_I& x)</CODE>
2406 <A NAME="IDX182"></A>
2407 <CODE>x</CODE> must be > 0. This function checks whether <CODE>x</CODE> is a power of 2.
2408 If <CODE>x</CODE> = 2^(n-1), it returns n. Else it returns 0.
2409 (See also the function <CODE>logp</CODE>.)
2414 <H3><A NAME="SEC36" HREF="cln.html#TOC36">4.9.2 Number theoretic functions</A></H3>
2418 <DT><CODE>uint32 gcd (uint32 a, uint32 b)</CODE>
2420 <A NAME="IDX183"></A>
2421 <DT><CODE>cl_I gcd (const cl_I& a, const cl_I& b)</CODE>
2423 This function returns the greatest common divisor of <CODE>a</CODE> and <CODE>b</CODE>,
2424 normalized to be >= 0.
2426 <DT><CODE>cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v)</CODE>
2428 <A NAME="IDX184"></A>
2429 This function ("extended gcd") returns the greatest common divisor <CODE>g</CODE> of
2430 <CODE>a</CODE> and <CODE>b</CODE> and at the same time the representation of <CODE>g</CODE>
2431 as an integral linear combination of <CODE>a</CODE> and <CODE>b</CODE>:
2432 <CODE>u</CODE> and <CODE>v</CODE> with <CODE>u*a+v*b = g</CODE>, <CODE>g</CODE> >= 0.
2433 <CODE>u</CODE> and <CODE>v</CODE> will be normalized to be of smallest possible absolute
2434 value, in the following sense: If <CODE>a</CODE> and <CODE>b</CODE> are non-zero, and
2435 <CODE>abs(a) != abs(b)</CODE>, <CODE>u</CODE> and <CODE>v</CODE> will satisfy the inequalities
2436 <CODE>abs(u) <= abs(b)/(2*g)</CODE>, <CODE>abs(v) <= abs(a)/(2*g)</CODE>.
2438 <DT><CODE>cl_I lcm (const cl_I& a, const cl_I& b)</CODE>
2440 <A NAME="IDX185"></A>
2441 This function returns the least common multiple of <CODE>a</CODE> and <CODE>b</CODE>,
2442 normalized to be >= 0.
2444 <DT><CODE>cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l)</CODE>
2446 <A NAME="IDX186"></A>
2447 <DT><CODE>cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l)</CODE>
2449 <CODE>a</CODE> must be > 0. <CODE>b</CODE> must be >0 and != 1. If log(a,b) is
2450 rational number, this function returns true and sets *l = log(a,b), else
2456 <H3><A NAME="SEC37" HREF="cln.html#TOC37">4.9.3 Combinatorial functions</A></H3>
2460 <DT><CODE>cl_I factorial (uintL n)</CODE>
2462 <A NAME="IDX187"></A>
2463 <CODE>n</CODE> must be a small integer >= 0. This function returns the factorial
2464 <CODE>n</CODE>! = <CODE>1*2*...*n</CODE>.
2466 <DT><CODE>cl_I doublefactorial (uintL n)</CODE>
2468 <A NAME="IDX188"></A>
2469 <CODE>n</CODE> must be a small integer >= 0. This function returns the
2470 doublefactorial <CODE>n</CODE>!! = <CODE>1*3*...*n</CODE> or
2471 <CODE>n</CODE>!! = <CODE>2*4*...*n</CODE>, respectively.
2473 <DT><CODE>cl_I binomial (uintL n, uintL k)</CODE>
2475 <A NAME="IDX189"></A>
2476 <CODE>n</CODE> and <CODE>k</CODE> must be small integers >= 0. This function returns the
2477 binomial coefficient
2478 (<CODE>n</CODE> choose <CODE>k</CODE>) = <CODE>n</CODE>! / <CODE>k</CODE>! <CODE>(n-k)</CODE>!
2479 for 0 <= k <= n, 0 else.
2484 <H2><A NAME="SEC38" HREF="cln.html#TOC38">4.10 Functions on floating-point numbers</A></H2>
2487 Recall that a floating-point number consists of a sign <CODE>s</CODE>, an
2488 exponent <CODE>e</CODE> and a mantissa <CODE>m</CODE>. The value of the number is
2489 <CODE>(-1)^s * 2^e * m</CODE>.
2494 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
2495 defines the following operations.
2500 <DT><CODE><VAR>type</VAR> scale_float (const <VAR>type</VAR>& x, sintL delta)</CODE>
2502 <A NAME="IDX190"></A>
2503 <DT><CODE><VAR>type</VAR> scale_float (const <VAR>type</VAR>& x, const cl_I& delta)</CODE>
2505 Returns <CODE>x*2^delta</CODE>. This is more efficient than an explicit multiplication
2506 because it copies <CODE>x</CODE> and modifies the exponent.
2510 The following functions provide an abstract interface to the underlying
2511 representation of floating-point numbers.
2516 <DT><CODE>sintL float_exponent (const <VAR>type</VAR>& x)</CODE>
2518 <A NAME="IDX191"></A>
2519 Returns the exponent <CODE>e</CODE> of <CODE>x</CODE>.
2520 For <CODE>x = 0.0</CODE>, this is 0. For <CODE>x</CODE> non-zero, this is the unique
2521 integer with <CODE>2^(e-1) <= abs(x) < 2^e</CODE>.
2523 <DT><CODE>sintL float_radix (const <VAR>type</VAR>& x)</CODE>
2525 <A NAME="IDX192"></A>
2526 Returns the base of the floating-point representation. This is always <CODE>2</CODE>.
2528 <DT><CODE><VAR>type</VAR> float_sign (const <VAR>type</VAR>& x)</CODE>
2530 <A NAME="IDX193"></A>
2531 Returns the sign <CODE>s</CODE> of <CODE>x</CODE> as a float. The value is 1 for
2532 <CODE>x</CODE> >= 0, -1 for <CODE>x</CODE> < 0.
2534 <DT><CODE>uintL float_digits (const <VAR>type</VAR>& x)</CODE>
2536 <A NAME="IDX194"></A>
2537 Returns the number of mantissa bits in the floating-point representation
2538 of <CODE>x</CODE>, including the hidden bit. The value only depends on the type
2539 of <CODE>x</CODE>, not on its value.
2541 <DT><CODE>uintL float_precision (const <VAR>type</VAR>& x)</CODE>
2543 <A NAME="IDX195"></A>
2544 Returns the number of significant mantissa bits in the floating-point
2545 representation of <CODE>x</CODE>. Since denormalized numbers are not supported,
2546 this is the same as <CODE>float_digits(x)</CODE> if <CODE>x</CODE> is non-zero, and
2547 0 if <CODE>x</CODE> = 0.
2551 The complete internal representation of a float is encoded in the type
2552 <A NAME="IDX196"></A>
2553 <A NAME="IDX197"></A>
2554 <A NAME="IDX198"></A>
2555 <A NAME="IDX199"></A>
2556 <A NAME="IDX200"></A>
2557 <CODE>decoded_float</CODE> (or <CODE>decoded_sfloat</CODE>, <CODE>decoded_ffloat</CODE>,
2558 <CODE>decoded_dfloat</CODE>, <CODE>decoded_lfloat</CODE>, respectively), defined by
2561 struct decoded_<VAR>type</VAR>float {
2562 <VAR>type</VAR> mantissa; cl_I exponent; <VAR>type</VAR> sign;
2567 and returned by the function
2572 <DT><CODE>decoded_<VAR>type</VAR>float decode_float (const <VAR>type</VAR>& x)</CODE>
2574 <A NAME="IDX201"></A>
2575 For <CODE>x</CODE> non-zero, this returns <CODE>(-1)^s</CODE>, <CODE>e</CODE>, <CODE>m</CODE> with
2576 <CODE>x = (-1)^s * 2^e * m</CODE> and <CODE>0.5 <= m < 1.0</CODE>. For <CODE>x</CODE> = 0,
2577 it returns <CODE>(-1)^s</CODE>=1, <CODE>e</CODE>=0, <CODE>m</CODE>=0.
2578 <CODE>e</CODE> is the same as returned by the function <CODE>float_exponent</CODE>.
2582 A complete decoding in terms of integers is provided as type
2583 <A NAME="IDX202"></A>
2586 struct cl_idecoded_float {
2587 cl_I mantissa; cl_I exponent; cl_I sign;
2592 by the following function:
2597 <DT><CODE>cl_idecoded_float integer_decode_float (const <VAR>type</VAR>& x)</CODE>
2599 <A NAME="IDX203"></A>
2600 For <CODE>x</CODE> non-zero, this returns <CODE>(-1)^s</CODE>, <CODE>e</CODE>, <CODE>m</CODE> with
2601 <CODE>x = (-1)^s * 2^e * m</CODE> and <CODE>m</CODE> an integer with <CODE>float_digits(x)</CODE>
2602 bits. For <CODE>x</CODE> = 0, it returns <CODE>(-1)^s</CODE>=1, <CODE>e</CODE>=0, <CODE>m</CODE>=0.
2603 WARNING: The exponent <CODE>e</CODE> is not the same as the one returned by
2604 the functions <CODE>decode_float</CODE> and <CODE>float_exponent</CODE>.
2608 Some other function, implemented only for class <CODE>cl_F</CODE>:
2613 <DT><CODE>cl_F float_sign (const cl_F& x, const cl_F& y)</CODE>
2615 <A NAME="IDX204"></A>
2616 This returns a floating point number whose precision and absolute value
2617 is that of <CODE>y</CODE> and whose sign is that of <CODE>x</CODE>. If <CODE>x</CODE> is
2618 zero, it is treated as positive. Same for <CODE>y</CODE>.
2623 <H2><A NAME="SEC39" HREF="cln.html#TOC39">4.11 Conversion functions</A></H2>
2625 <A NAME="IDX205"></A>
2630 <H3><A NAME="SEC40" HREF="cln.html#TOC40">4.11.1 Conversion to floating-point numbers</A></H3>
2633 The type <CODE>cl_float_format_t</CODE> describes a floating-point format.
2634 <A NAME="IDX206"></A>
2639 <DT><CODE>cl_float_format_t cl_float_format (uintL n)</CODE>
2641 <A NAME="IDX207"></A>
2642 Returns the smallest float format which guarantees at least <CODE>n</CODE>
2643 decimal digits in the mantissa (after the decimal point).
2645 <DT><CODE>cl_float_format_t cl_float_format (const cl_F& x)</CODE>
2647 Returns the floating point format of <CODE>x</CODE>.
2649 <DT><CODE>cl_float_format_t default_float_format</CODE>
2651 <A NAME="IDX208"></A>
2652 Global variable: the default float format used when converting rational numbers
2657 To convert a real number to a float, each of the types
2658 <CODE>cl_R</CODE>, <CODE>cl_F</CODE>, <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
2659 <CODE>int</CODE>, <CODE>unsigned int</CODE>, <CODE>float</CODE>, <CODE>double</CODE>
2660 defines the following operations:
2665 <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&x, cl_float_format_t f)</CODE>
2667 <A NAME="IDX209"></A>
2668 Returns <CODE>x</CODE> as a float of format <CODE>f</CODE>.
2669 <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&x, const cl_F& y)</CODE>
2671 Returns <CODE>x</CODE> in the float format of <CODE>y</CODE>.
2672 <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&x)</CODE>
2674 Returns <CODE>x</CODE> as a float of format <CODE>default_float_format</CODE> if
2675 it is an exact number, or <CODE>x</CODE> itself if it is already a float.
2679 Of course, converting a number to a float can lose precision.
2683 Every floating-point format has some characteristic numbers:
2688 <DT><CODE>cl_F most_positive_float (cl_float_format_t f)</CODE>
2690 <A NAME="IDX210"></A>
2691 Returns the largest (most positive) floating point number in float format <CODE>f</CODE>.
2693 <DT><CODE>cl_F most_negative_float (cl_float_format_t f)</CODE>
2695 <A NAME="IDX211"></A>
2696 Returns the smallest (most negative) floating point number in float format <CODE>f</CODE>.
2698 <DT><CODE>cl_F least_positive_float (cl_float_format_t f)</CODE>
2700 <A NAME="IDX212"></A>
2701 Returns the least positive floating point number (i.e. > 0 but closest to 0)
2702 in float format <CODE>f</CODE>.
2704 <DT><CODE>cl_F least_negative_float (cl_float_format_t f)</CODE>
2706 <A NAME="IDX213"></A>
2707 Returns the least negative floating point number (i.e. < 0 but closest to 0)
2708 in float format <CODE>f</CODE>.
2710 <DT><CODE>cl_F float_epsilon (cl_float_format_t f)</CODE>
2712 <A NAME="IDX214"></A>
2713 Returns the smallest floating point number e > 0 such that <CODE>1+e != 1</CODE>.
2715 <DT><CODE>cl_F float_negative_epsilon (cl_float_format_t f)</CODE>
2717 <A NAME="IDX215"></A>
2718 Returns the smallest floating point number e > 0 such that <CODE>1-e != 1</CODE>.
2723 <H3><A NAME="SEC41" HREF="cln.html#TOC41">4.11.2 Conversion to rational numbers</A></H3>
2726 Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_F</CODE>
2727 defines the following operation:
2732 <DT><CODE>cl_RA rational (const <VAR>type</VAR>& x)</CODE>
2734 <A NAME="IDX216"></A>
2735 Returns the value of <CODE>x</CODE> as an exact number. If <CODE>x</CODE> is already
2736 an exact number, this is <CODE>x</CODE>. If <CODE>x</CODE> is a floating-point number,
2737 the value is a rational number whose denominator is a power of 2.
2741 In order to convert back, say, <CODE>(cl_F)(cl_R)"1/3"</CODE> to <CODE>1/3</CODE>, there is
2747 <DT><CODE>cl_RA rationalize (const cl_R& x)</CODE>
2749 <A NAME="IDX217"></A>
2750 If <CODE>x</CODE> is a floating-point number, it actually represents an interval
2751 of real numbers, and this function returns the rational number with
2752 smallest denominator (and smallest numerator, in magnitude)
2753 which lies in this interval.
2754 If <CODE>x</CODE> is already an exact number, this function returns <CODE>x</CODE>.
2758 If <CODE>x</CODE> is any float, one has
2765 <CODE>cl_float(rational(x),x) = x</CODE>
2768 <CODE>cl_float(rationalize(x),x) = x</CODE>
2773 <H2><A NAME="SEC42" HREF="cln.html#TOC42">4.12 Random number generators</A></H2>
2776 A random generator is a machine which produces (pseudo-)random numbers.
2777 The include file <CODE><cln/random.h></CODE> defines a class <CODE>random_state</CODE>
2778 which contains the state of a random generator. If you make a copy
2779 of the random number generator, the original one and the copy will produce
2780 the same sequence of random numbers.
2784 The following functions return (pseudo-)random numbers in different formats.
2785 Calling one of these modifies the state of the random number generator in
2786 a complicated but deterministic way.
2791 <A NAME="IDX218"></A>
2792 <A NAME="IDX219"></A>
2795 random_state default_random_state
2799 contains a default random number generator. It is used when the functions
2800 below are called without <CODE>random_state</CODE> argument.
2805 <DT><CODE>uint32 random32 (random_state& randomstate)</CODE>
2807 <DT><CODE>uint32 random32 ()</CODE>
2809 <A NAME="IDX220"></A>
2810 Returns a random unsigned 32-bit number. All bits are equally random.
2812 <DT><CODE>cl_I random_I (random_state& randomstate, const cl_I& n)</CODE>
2814 <DT><CODE>cl_I random_I (const cl_I& n)</CODE>
2816 <A NAME="IDX221"></A>
2817 <CODE>n</CODE> must be an integer > 0. This function returns a random integer <CODE>x</CODE>
2818 in the range <CODE>0 <= x < n</CODE>.
2820 <DT><CODE>cl_F random_F (random_state& randomstate, const cl_F& n)</CODE>
2822 <DT><CODE>cl_F random_F (const cl_F& n)</CODE>
2824 <A NAME="IDX222"></A>
2825 <CODE>n</CODE> must be a float > 0. This function returns a random floating-point
2826 number of the same format as <CODE>n</CODE> in the range <CODE>0 <= x < n</CODE>.
2828 <DT><CODE>cl_R random_R (random_state& randomstate, const cl_R& n)</CODE>
2830 <DT><CODE>cl_R random_R (const cl_R& n)</CODE>
2832 <A NAME="IDX223"></A>
2833 Behaves like <CODE>random_I</CODE> if <CODE>n</CODE> is an integer and like <CODE>random_F</CODE>
2834 if <CODE>n</CODE> is a float.
2839 <H2><A NAME="SEC43" HREF="cln.html#TOC43">4.13 Obfuscating operators</A></H2>
2841 <A NAME="IDX224"></A>
2845 The modifying C/C++ operators <CODE>+=</CODE>, <CODE>-=</CODE>, <CODE>*=</CODE>, <CODE>/=</CODE>,
2846 <CODE>&=</CODE>, <CODE>|=</CODE>, <CODE>^=</CODE>, <CODE><<=</CODE>, <CODE>>>=</CODE>
2847 are not available by default because their
2848 use tends to make programs unreadable. It is trivial to get away without
2849 them. However, if you feel that you absolutely need these operators
2850 to get happy, then add
2853 #define WANT_OBFUSCATING_OPERATORS
2857 <A NAME="IDX225"></A>
2858 to the beginning of your source files, before the inclusion of any CLN
2859 include files. This flag will enable the following operators:
2863 For the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
2864 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>:
2869 <DT><CODE><VAR>type</VAR>& operator += (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2871 <A NAME="IDX226"></A>
2872 <DT><CODE><VAR>type</VAR>& operator -= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2874 <A NAME="IDX227"></A>
2875 <DT><CODE><VAR>type</VAR>& operator *= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2877 <A NAME="IDX228"></A>
2878 <DT><CODE><VAR>type</VAR>& operator /= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2880 <A NAME="IDX229"></A>
2884 For the class <CODE>cl_I</CODE>:
2889 <DT><CODE><VAR>type</VAR>& operator += (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2891 <DT><CODE><VAR>type</VAR>& operator -= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2893 <DT><CODE><VAR>type</VAR>& operator *= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2895 <DT><CODE><VAR>type</VAR>& operator &= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2897 <A NAME="IDX230"></A>
2898 <DT><CODE><VAR>type</VAR>& operator |= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2900 <A NAME="IDX231"></A>
2901 <DT><CODE><VAR>type</VAR>& operator ^= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2903 <A NAME="IDX232"></A>
2904 <DT><CODE><VAR>type</VAR>& operator <<= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2906 <A NAME="IDX233"></A>
2907 <DT><CODE><VAR>type</VAR>& operator >>= (<VAR>type</VAR>&, const <VAR>type</VAR>&)</CODE>
2909 <A NAME="IDX234"></A>
2913 For the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
2914 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>:
2919 <DT><CODE><VAR>type</VAR>& operator ++ (<VAR>type</VAR>& x)</CODE>
2921 <A NAME="IDX235"></A>
2922 The prefix operator <CODE>++x</CODE>.
2924 <DT><CODE>void operator ++ (<VAR>type</VAR>& x, int)</CODE>
2926 The postfix operator <CODE>x++</CODE>.
2928 <DT><CODE><VAR>type</VAR>& operator -- (<VAR>type</VAR>& x)</CODE>
2930 <A NAME="IDX236"></A>
2931 The prefix operator <CODE>--x</CODE>.
2933 <DT><CODE>void operator -- (<VAR>type</VAR>& x, int)</CODE>
2935 The postfix operator <CODE>x--</CODE>.
2939 Note that by using these obfuscating operators, you wouldn't gain efficiency:
2940 In CLN <SAMP>`x += y;'</SAMP> is exactly the same as <SAMP>`x = x+y;'</SAMP>, not more
2946 <H1><A NAME="SEC44" HREF="cln.html#TOC44">5. Input/Output</A></H1>
2948 <A NAME="IDX237"></A>
2953 <H2><A NAME="SEC45" HREF="cln.html#TOC45">5.1 Internal and printed representation</A></H2>
2955 <A NAME="IDX238"></A>
2959 All computations deal with the internal representations of the numbers.
2963 Every number has an external representation as a sequence of ASCII characters.
2964 Several external representations may denote the same number, for example,
2965 "20.0" and "20.000".
2969 Converting an internal to an external representation is called "printing",
2970 <A NAME="IDX239"></A>
2971 converting an external to an internal representation is called "reading".
2972 <A NAME="IDX240"></A>
2973 In CLN, it is always true that conversion of an internal to an external
2974 representation and then back to an internal representation will yield the
2975 same internal representation. Symbolically: <CODE>read(print(x)) == x</CODE>.
2976 This is called "print-read consistency".
2980 Different types of numbers have different external representations (case
2988 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+. The reader also accepts the
2989 Common Lisp syntaxes <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>.</CODE> with a trailing dot
2990 for decimal integers
2991 and the <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes.
2993 <DT>Rational numbers
2995 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}+<CODE>/</CODE>{<VAR>digit</VAR>}+.
2996 The <CODE>#<VAR>n</VAR>R</CODE>, <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> prefixes are allowed
2999 <DT>Floating-point numbers
3001 External representation: <VAR>sign</VAR>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
3002 <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}*<VAR>exponent</VAR> or
3003 <VAR>sign</VAR>{<VAR>digit</VAR>}*<CODE>.</CODE>{<VAR>digit</VAR>}+. A precision specifier
3004 of the form _<VAR>prec</VAR> may be appended. There must be at least
3005 one digit in the non-exponent part. The exponent has the syntax
3006 <VAR>expmarker</VAR> <VAR>expsign</VAR> {<VAR>digit</VAR>}+.
3007 The exponent marker is
3013 <SAMP>`s'</SAMP> for short-floats,
3016 <SAMP>`f'</SAMP> for single-floats,
3019 <SAMP>`d'</SAMP> for double-floats,
3022 <SAMP>`L'</SAMP> for long-floats,
3025 or <SAMP>`e'</SAMP>, which denotes a default float format. The precision specifying
3026 suffix has the syntax _<VAR>prec</VAR> where <VAR>prec</VAR> denotes the number of
3027 valid mantissa digits (in decimal, excluding leading zeroes), cf. also
3028 function <SAMP>`cl_float_format'</SAMP>.
3032 External representation:
3037 In algebraic notation: <CODE><VAR>realpart</VAR>+<VAR>imagpart</VAR>i</CODE>. Of course,
3038 if <VAR>imagpart</VAR> is negative, its printed representation begins with
3039 a <SAMP>`-'</SAMP>, and the <SAMP>`+'</SAMP> between <VAR>realpart</VAR> and <VAR>imagpart</VAR>
3040 may be omitted. Note that this notation cannot be used when the <VAR>imagpart</VAR>
3041 is rational and the rational number's base is >18, because the <SAMP>`i'</SAMP>
3042 is then read as a digit.
3045 In Common Lisp notation: <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>.
3052 <H2><A NAME="SEC46" HREF="cln.html#TOC46">5.2 Input functions</A></H2>
3055 Including <CODE><cln/io.h></CODE> defines a type <CODE>cl_istream</CODE>, which is
3056 the type of the first argument to all input functions. <CODE>cl_istream</CODE>
3057 is the same as <CODE>std::istream&</CODE>.
3066 <CODE>cl_istream stdin</CODE>
3070 contains the standard input stream.
3074 These are the simple input functions:
3079 <DT><CODE>int freadchar (cl_istream stream)</CODE>
3081 Reads a character from <CODE>stream</CODE>. Returns <CODE>cl_EOF</CODE> (not a <SAMP>`char'</SAMP>!)
3082 if the end of stream was encountered or an error occurred.
3084 <DT><CODE>int funreadchar (cl_istream stream, int c)</CODE>
3086 Puts back <CODE>c</CODE> onto <CODE>stream</CODE>. <CODE>c</CODE> must be the result of the
3087 last <CODE>freadchar</CODE> operation on <CODE>stream</CODE>.
3091 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
3092 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
3093 defines, in <CODE><cln/<VAR>type</VAR>_io.h></CODE>, the following input function:
3098 <DT><CODE>cl_istream operator>> (cl_istream stream, <VAR>type</VAR>& result)</CODE>
3100 Reads a number from <CODE>stream</CODE> and stores it in the <CODE>result</CODE>.
3104 The most flexible input functions, defined in <CODE><cln/<VAR>type</VAR>_io.h></CODE>,
3110 <DT><CODE>cl_N read_complex (cl_istream stream, const cl_read_flags& flags)</CODE>
3112 <DT><CODE>cl_R read_real (cl_istream stream, const cl_read_flags& flags)</CODE>
3114 <DT><CODE>cl_F read_float (cl_istream stream, const cl_read_flags& flags)</CODE>
3116 <DT><CODE>cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)</CODE>
3118 <DT><CODE>cl_I read_integer (cl_istream stream, const cl_read_flags& flags)</CODE>
3120 Reads a number from <CODE>stream</CODE>. The <CODE>flags</CODE> are parameters which
3121 affect the input syntax. Whitespace before the number is silently skipped.
3123 <DT><CODE>cl_N read_complex (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
3125 <DT><CODE>cl_R read_real (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
3127 <DT><CODE>cl_F read_float (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
3129 <DT><CODE>cl_RA read_rational (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
3131 <DT><CODE>cl_I read_integer (const cl_read_flags& flags, const char * string, const char * string_limit, const char * * end_of_parse)</CODE>
3133 Reads a number from a string in memory. The <CODE>flags</CODE> are parameters which
3134 affect the input syntax. The string starts at <CODE>string</CODE> and ends at
3135 <CODE>string_limit</CODE> (exclusive limit). <CODE>string_limit</CODE> may also be
3136 <CODE>NULL</CODE>, denoting the entire string, i.e. equivalent to
3137 <CODE>string_limit = string + strlen(string)</CODE>. If <CODE>end_of_parse</CODE> is
3138 <CODE>NULL</CODE>, the string in memory must contain exactly one number and nothing
3139 more, else a fatal error will be signalled. If <CODE>end_of_parse</CODE>
3140 is not <CODE>NULL</CODE>, <CODE>*end_of_parse</CODE> will be assigned a pointer past
3141 the last parsed character (i.e. <CODE>string_limit</CODE> if nothing came after
3142 the number). Whitespace is not allowed.
3146 The structure <CODE>cl_read_flags</CODE> contains the following fields:
3151 <DT><CODE>cl_read_syntax_t syntax</CODE>
3153 The possible results of the read operation. Possible values are
3154 <CODE>syntax_number</CODE>, <CODE>syntax_real</CODE>, <CODE>syntax_rational</CODE>,
3155 <CODE>syntax_integer</CODE>, <CODE>syntax_float</CODE>, <CODE>syntax_sfloat</CODE>,
3156 <CODE>syntax_ffloat</CODE>, <CODE>syntax_dfloat</CODE>, <CODE>syntax_lfloat</CODE>.
3158 <DT><CODE>cl_read_lsyntax_t lsyntax</CODE>
3160 Specifies the language-dependent syntax variant for the read operation.
3165 <DT><CODE>lsyntax_standard</CODE>
3167 accept standard algebraic notation only, no complex numbers,
3168 <DT><CODE>lsyntax_algebraic</CODE>
3170 accept the algebraic notation <CODE><VAR>x</VAR>+<VAR>y</VAR>i</CODE> for complex numbers,
3171 <DT><CODE>lsyntax_commonlisp</CODE>
3173 accept the <CODE>#b</CODE>, <CODE>#o</CODE>, <CODE>#x</CODE> syntaxes for binary, octal,
3174 hexadecimal numbers,
3175 <CODE>#<VAR>base</VAR>R</CODE> for rational numbers in a given base,
3176 <CODE>#c(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE> for complex numbers,
3177 <DT><CODE>lsyntax_all</CODE>
3179 accept all of these extensions.
3182 <DT><CODE>unsigned int rational_base</CODE>
3184 The base in which rational numbers are read.
3186 <DT><CODE>cl_float_format_t float_flags.default_float_format</CODE>
3188 The float format used when reading floats with exponent marker <SAMP>`e'</SAMP>.
3190 <DT><CODE>cl_float_format_t float_flags.default_lfloat_format</CODE>
3192 The float format used when reading floats with exponent marker <SAMP>`l'</SAMP>.
3194 <DT><CODE>cl_boolean float_flags.mantissa_dependent_float_format</CODE>
3196 When this flag is true, floats specified with more digits than corresponding
3197 to the exponent marker they contain, but without <VAR>_nnn</VAR> suffix, will get a
3198 precision corresponding to their number of significant digits.
3203 <H2><A NAME="SEC47" HREF="cln.html#TOC47">5.3 Output functions</A></H2>
3206 Including <CODE><cln/io.h></CODE> defines a type <CODE>cl_ostream</CODE>, which is
3207 the type of the first argument to all output functions. <CODE>cl_ostream</CODE>
3208 is the same as <CODE>std::ostream&</CODE>.
3217 <CODE>cl_ostream stdout</CODE>
3221 contains the standard output stream.
3230 <CODE>cl_ostream stderr</CODE>
3234 contains the standard error output stream.
3238 These are the simple output functions:
3243 <DT><CODE>void fprintchar (cl_ostream stream, char c)</CODE>
3245 Prints the character <CODE>x</CODE> literally on the <CODE>stream</CODE>.
3247 <DT><CODE>void fprint (cl_ostream stream, const char * string)</CODE>
3249 Prints the <CODE>string</CODE> literally on the <CODE>stream</CODE>.
3251 <DT><CODE>void fprintdecimal (cl_ostream stream, int x)</CODE>
3253 <DT><CODE>void fprintdecimal (cl_ostream stream, const cl_I& x)</CODE>
3255 Prints the integer <CODE>x</CODE> in decimal on the <CODE>stream</CODE>.
3257 <DT><CODE>void fprintbinary (cl_ostream stream, const cl_I& x)</CODE>
3259 Prints the integer <CODE>x</CODE> in binary (base 2, without prefix)
3260 on the <CODE>stream</CODE>.
3262 <DT><CODE>void fprintoctal (cl_ostream stream, const cl_I& x)</CODE>
3264 Prints the integer <CODE>x</CODE> in octal (base 8, without prefix)
3265 on the <CODE>stream</CODE>.
3267 <DT><CODE>void fprinthexadecimal (cl_ostream stream, const cl_I& x)</CODE>
3269 Prints the integer <CODE>x</CODE> in hexadecimal (base 16, without prefix)
3270 on the <CODE>stream</CODE>.
3274 Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
3275 <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
3276 defines, in <CODE><cln/<VAR>type</VAR>_io.h></CODE>, the following output functions:
3281 <DT><CODE>void fprint (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
3283 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const <VAR>type</VAR>& x)</CODE>
3285 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
3286 on the global printer settings in the variable <CODE>default_print_flags</CODE>.
3287 The <CODE>ostream</CODE> flags and settings (flags, width and locale) are
3292 The most flexible output function, defined in <CODE><cln/<VAR>type</VAR>_io.h></CODE>,
3296 void print_complex (cl_ostream stream, const cl_print_flags& flags,
3298 void print_real (cl_ostream stream, const cl_print_flags& flags,
3300 void print_float (cl_ostream stream, const cl_print_flags& flags,
3302 void print_rational (cl_ostream stream, const cl_print_flags& flags,
3303 const cl_RA& z);
3304 void print_integer (cl_ostream stream, const cl_print_flags& flags,
3309 Prints the number <CODE>x</CODE> on the <CODE>stream</CODE>. The <CODE>flags</CODE> are
3310 parameters which affect the output.
3314 The structure type <CODE>cl_print_flags</CODE> contains the following fields:
3319 <DT><CODE>unsigned int rational_base</CODE>
3321 The base in which rational numbers are printed. Default is <CODE>10</CODE>.
3323 <DT><CODE>cl_boolean rational_readably</CODE>
3325 If this flag is true, rational numbers are printed with radix specifiers in
3326 Common Lisp syntax (<CODE>#<VAR>n</VAR>R</CODE> or <CODE>#b</CODE> or <CODE>#o</CODE> or <CODE>#x</CODE>
3327 prefixes, trailing dot). Default is false.
3329 <DT><CODE>cl_boolean float_readably</CODE>
3331 If this flag is true, type specific exponent markers have precedence over 'E'.
3334 <DT><CODE>cl_float_format_t default_float_format</CODE>
3336 Floating point numbers of this format will be printed using the 'E' exponent
3337 marker. Default is <CODE>cl_float_format_ffloat</CODE>.
3339 <DT><CODE>cl_boolean complex_readably</CODE>
3341 If this flag is true, complex numbers will be printed using the Common Lisp
3342 syntax <CODE>#C(<VAR>realpart</VAR> <VAR>imagpart</VAR>)</CODE>. Default is false.
3344 <DT><CODE>cl_string univpoly_varname</CODE>
3346 Univariate polynomials with no explicit indeterminate name will be printed
3347 using this variable name. Default is <CODE>"x"</CODE>.
3351 The global variable <CODE>default_print_flags</CODE> contains the default values,
3352 used by the function <CODE>fprint</CODE>.
3357 <H1><A NAME="SEC48" HREF="cln.html#TOC48">6. Rings</A></H1>
3360 CLN has a class of abstract rings.
3367 <cln/ring.h>
3371 Rings can be compared for equality:
3376 <DT><CODE>bool operator== (const cl_ring&, const cl_ring&)</CODE>
3378 <DT><CODE>bool operator!= (const cl_ring&, const cl_ring&)</CODE>
3380 These compare two rings for equality.
3384 Given a ring <CODE>R</CODE>, the following members can be used.
3389 <DT><CODE>void R->fprint (cl_ostream stream, const cl_ring_element& x)</CODE>
3391 <A NAME="IDX241"></A>
3392 <DT><CODE>cl_boolean R->equal (const cl_ring_element& x, const cl_ring_element& y)</CODE>
3394 <A NAME="IDX242"></A>
3395 <DT><CODE>cl_ring_element R->zero ()</CODE>
3397 <A NAME="IDX243"></A>
3398 <DT><CODE>cl_boolean R->zerop (const cl_ring_element& x)</CODE>
3400 <A NAME="IDX244"></A>
3401 <DT><CODE>cl_ring_element R->plus (const cl_ring_element& x, const cl_ring_element& y)</CODE>
3403 <A NAME="IDX245"></A>
3404 <DT><CODE>cl_ring_element R->minus (const cl_ring_element& x, const cl_ring_element& y)</CODE>
3406 <A NAME="IDX246"></A>
3407 <DT><CODE>cl_ring_element R->uminus (const cl_ring_element& x)</CODE>
3409 <A NAME="IDX247"></A>
3410 <DT><CODE>cl_ring_element R->one ()</CODE>
3412 <A NAME="IDX248"></A>
3413 <DT><CODE>cl_ring_element R->canonhom (const cl_I& x)</CODE>
3415 <A NAME="IDX249"></A>
3416 <DT><CODE>cl_ring_element R->mul (const cl_ring_element& x, const cl_ring_element& y)</CODE>
3418 <A NAME="IDX250"></A>
3419 <DT><CODE>cl_ring_element R->square (const cl_ring_element& x)</CODE>
3421 <A NAME="IDX251"></A>
3422 <DT><CODE>cl_ring_element R->expt_pos (const cl_ring_element& x, const cl_I& y)</CODE>
3424 <A NAME="IDX252"></A>
3428 The following rings are built-in.
3433 <DT><CODE>cl_null_ring cl_0_ring</CODE>
3435 The null ring, containing only zero.
3437 <DT><CODE>cl_complex_ring cl_C_ring</CODE>
3439 The ring of complex numbers. This corresponds to the type <CODE>cl_N</CODE>.
3441 <DT><CODE>cl_real_ring cl_R_ring</CODE>
3443 The ring of real numbers. This corresponds to the type <CODE>cl_R</CODE>.
3445 <DT><CODE>cl_rational_ring cl_RA_ring</CODE>
3447 The ring of rational numbers. This corresponds to the type <CODE>cl_RA</CODE>.
3449 <DT><CODE>cl_integer_ring cl_I_ring</CODE>
3451 The ring of integers. This corresponds to the type <CODE>cl_I</CODE>.
3455 Type tests can be performed for any of <CODE>cl_C_ring</CODE>, <CODE>cl_R_ring</CODE>,
3456 <CODE>cl_RA_ring</CODE>, <CODE>cl_I_ring</CODE>:
3461 <DT><CODE>cl_boolean instanceof (const cl_number& x, const cl_number_ring& R)</CODE>
3463 <A NAME="IDX253"></A>
3464 Tests whether the given number is an element of the number ring R.
3469 <H1><A NAME="SEC49" HREF="cln.html#TOC49">7. Modular integers</A></H1>
3471 <A NAME="IDX254"></A>
3476 <H2><A NAME="SEC50" HREF="cln.html#TOC50">7.1 Modular integer rings</A></H2>
3478 <A NAME="IDX255"></A>
3482 CLN implements modular integers, i.e. integers modulo a fixed integer N.
3483 The modulus is explicitly part of every modular integer. CLN doesn't
3484 allow you to (accidentally) mix elements of different modular rings,
3485 e.g. <CODE>(3 mod 4) + (2 mod 5)</CODE> will result in a runtime error.
3486 (Ideally one would imagine a generic data type <CODE>cl_MI(N)</CODE>, but C++
3487 doesn't have generic types. So one has to live with runtime checks.)
3491 The class of modular integer rings is
3498 <cln/ring.h>
3501 Modular integer ring
3503 <cln/modinteger.h>
3507 <A NAME="IDX256"></A>
3511 and the class of all modular integers (elements of modular integer rings) is
3518 <cln/modinteger.h>
3522 Modular integer rings are constructed using the function
3527 <DT><CODE>cl_modint_ring find_modint_ring (const cl_I& N)</CODE>
3529 <A NAME="IDX257"></A>
3530 This function returns the modular ring <SAMP>`Z/NZ'</SAMP>. It takes care
3531 of finding out about special cases of <CODE>N</CODE>, like powers of two
3532 and odd numbers for which Montgomery multiplication will be a win,
3533 <A NAME="IDX258"></A>
3534 and precomputes any necessary auxiliary data for computing modulo <CODE>N</CODE>.
3535 There is a cache table of rings, indexed by <CODE>N</CODE> (or, more precisely,
3536 by <CODE>abs(N)</CODE>). This ensures that the precomputation costs are reduced
3541 Modular integer rings can be compared for equality:
3546 <DT><CODE>bool operator== (const cl_modint_ring&, const cl_modint_ring&)</CODE>
3548 <A NAME="IDX259"></A>
3549 <DT><CODE>bool operator!= (const cl_modint_ring&, const cl_modint_ring&)</CODE>
3551 <A NAME="IDX260"></A>
3552 These compare two modular integer rings for equality. Two different calls
3553 to <CODE>find_modint_ring</CODE> with the same argument necessarily return the
3554 same ring because it is memoized in the cache table.
3559 <H2><A NAME="SEC51" HREF="cln.html#TOC51">7.2 Functions on modular integers</A></H2>
3562 Given a modular integer ring <CODE>R</CODE>, the following members can be used.
3567 <DT><CODE>cl_I R->modulus</CODE>
3569 <A NAME="IDX261"></A>
3570 This is the ring's modulus, normalized to be nonnegative: <CODE>abs(N)</CODE>.
3572 <DT><CODE>cl_MI R->zero()</CODE>
3574 <A NAME="IDX262"></A>
3575 This returns <CODE>0 mod N</CODE>.
3577 <DT><CODE>cl_MI R->one()</CODE>
3579 <A NAME="IDX263"></A>
3580 This returns <CODE>1 mod N</CODE>.
3582 <DT><CODE>cl_MI R->canonhom (const cl_I& x)</CODE>
3584 <A NAME="IDX264"></A>
3585 This returns <CODE>x mod N</CODE>.
3587 <DT><CODE>cl_I R->retract (const cl_MI& x)</CODE>
3589 <A NAME="IDX265"></A>
3590 This is a partial inverse function to <CODE>R->canonhom</CODE>. It returns the
3591 standard representative (<CODE>>=0</CODE>, <CODE><N</CODE>) of <CODE>x</CODE>.
3593 <DT><CODE>cl_MI R->random(random_state& randomstate)</CODE>
3595 <DT><CODE>cl_MI R->random()</CODE>
3597 <A NAME="IDX266"></A>
3598 This returns a random integer modulo <CODE>N</CODE>.
3602 The following operations are defined on modular integers.
3607 <DT><CODE>cl_modint_ring x.ring ()</CODE>
3609 <A NAME="IDX267"></A>
3610 Returns the ring to which the modular integer <CODE>x</CODE> belongs.
3612 <DT><CODE>cl_MI operator+ (const cl_MI&, const cl_MI&)</CODE>
3614 <A NAME="IDX268"></A>
3615 Returns the sum of two modular integers. One of the arguments may also
3618 <DT><CODE>cl_MI operator- (const cl_MI&, const cl_MI&)</CODE>
3620 <A NAME="IDX269"></A>
3621 Returns the difference of two modular integers. One of the arguments may also
3624 <DT><CODE>cl_MI operator- (const cl_MI&)</CODE>
3626 Returns the negative of a modular integer.
3628 <DT><CODE>cl_MI operator* (const cl_MI&, const cl_MI&)</CODE>
3630 <A NAME="IDX270"></A>
3631 Returns the product of two modular integers. One of the arguments may also
3634 <DT><CODE>cl_MI square (const cl_MI&)</CODE>
3636 <A NAME="IDX271"></A>
3637 Returns the square of a modular integer.
3639 <DT><CODE>cl_MI recip (const cl_MI& x)</CODE>
3641 <A NAME="IDX272"></A>
3642 Returns the reciprocal <CODE>x^-1</CODE> of a modular integer <CODE>x</CODE>. <CODE>x</CODE>
3643 must be coprime to the modulus, otherwise an error message is issued.
3645 <DT><CODE>cl_MI div (const cl_MI& x, const cl_MI& y)</CODE>
3647 <A NAME="IDX273"></A>
3648 Returns the quotient <CODE>x*y^-1</CODE> of two modular integers <CODE>x</CODE>, <CODE>y</CODE>.
3649 <CODE>y</CODE> must be coprime to the modulus, otherwise an error message is issued.
3651 <DT><CODE>cl_MI expt_pos (const cl_MI& x, const cl_I& y)</CODE>
3653 <A NAME="IDX274"></A>
3654 <CODE>y</CODE> must be > 0. Returns <CODE>x^y</CODE>.
3656 <DT><CODE>cl_MI expt (const cl_MI& x, const cl_I& y)</CODE>
3658 <A NAME="IDX275"></A>
3659 Returns <CODE>x^y</CODE>. If <CODE>y</CODE> is negative, <CODE>x</CODE> must be coprime to the
3660 modulus, else an error message is issued.
3662 <DT><CODE>cl_MI operator<< (const cl_MI& x, const cl_I& y)</CODE>
3664 <A NAME="IDX276"></A>
3665 Returns <CODE>x*2^y</CODE>.
3667 <DT><CODE>cl_MI operator>> (const cl_MI& x, const cl_I& y)</CODE>
3669 <A NAME="IDX277"></A>
3670 Returns <CODE>x*2^-y</CODE>. When <CODE>y</CODE> is positive, the modulus must be odd,
3671 or an error message is issued.
3673 <DT><CODE>bool operator== (const cl_MI&, const cl_MI&)</CODE>
3675 <A NAME="IDX278"></A>
3676 <DT><CODE>bool operator!= (const cl_MI&, const cl_MI&)</CODE>
3678 <A NAME="IDX279"></A>
3679 Compares two modular integers, belonging to the same modular integer ring,
3682 <DT><CODE>cl_boolean zerop (const cl_MI& x)</CODE>
3684 <A NAME="IDX280"></A>
3685 Returns true if <CODE>x</CODE> is <CODE>0 mod N</CODE>.
3689 The following output functions are defined (see also the chapter on
3695 <DT><CODE>void fprint (cl_ostream stream, const cl_MI& x)</CODE>
3697 <A NAME="IDX281"></A>
3698 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const cl_MI& x)</CODE>
3700 <A NAME="IDX282"></A>
3701 Prints the modular integer <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
3702 on the global printer settings in the variable <CODE>default_print_flags</CODE>.
3707 <H1><A NAME="SEC52" HREF="cln.html#TOC52">8. Symbolic data types</A></H1>
3709 <A NAME="IDX283"></A>
3713 CLN implements two symbolic (non-numeric) data types: strings and symbols.
3718 <H2><A NAME="SEC53" HREF="cln.html#TOC53">8.1 Strings</A></H2>
3720 <A NAME="IDX284"></A>
3721 <A NAME="IDX285"></A>
3732 <cln/string.h>
3736 implements immutable strings.
3740 Strings are constructed through the following constructors:
3745 <DT><CODE>cl_string (const char * s)</CODE>
3747 Returns an immutable copy of the (zero-terminated) C string <CODE>s</CODE>.
3749 <DT><CODE>cl_string (const char * ptr, unsigned long len)</CODE>
3751 Returns an immutable copy of the <CODE>len</CODE> characters at
3752 <CODE>ptr[0]</CODE>, ..., <CODE>ptr[len-1]</CODE>. NUL characters are allowed.
3756 The following functions are available on strings:
3761 <DT><CODE>operator =</CODE>
3763 Assignment from <CODE>cl_string</CODE> and <CODE>const char *</CODE>.
3765 <DT><CODE>s.length()</CODE>
3767 <A NAME="IDX286"></A>
3768 <DT><CODE>strlen(s)</CODE>
3770 <A NAME="IDX287"></A>
3771 Returns the length of the string <CODE>s</CODE>.
3773 <DT><CODE>s[i]</CODE>
3775 <A NAME="IDX288"></A>
3776 Returns the <CODE>i</CODE>th character of the string <CODE>s</CODE>.
3777 <CODE>i</CODE> must be in the range <CODE>0 <= i < s.length()</CODE>.
3779 <DT><CODE>bool equal (const cl_string& s1, const cl_string& s2)</CODE>
3781 <A NAME="IDX289"></A>
3782 Compares two strings for equality. One of the arguments may also be a
3783 plain <CODE>const char *</CODE>.
3788 <H2><A NAME="SEC54" HREF="cln.html#TOC54">8.2 Symbols</A></H2>
3790 <A NAME="IDX290"></A>
3791 <A NAME="IDX291"></A>
3795 Symbols are uniquified strings: all symbols with the same name are shared.
3796 This means that comparison of two symbols is fast (effectively just a pointer
3797 comparison), whereas comparison of two strings must in the worst case walk
3798 both strings until their end.
3799 Symbols are used, for example, as tags for properties, as names of variables
3800 in polynomial rings, etc.
3804 Symbols are constructed through the following constructor:
3809 <DT><CODE>cl_symbol (const cl_string& s)</CODE>
3811 Looks up or creates a new symbol with a given name.
3815 The following operations are available on symbols:
3820 <DT><CODE>cl_string (const cl_symbol& sym)</CODE>
3822 Conversion to <CODE>cl_string</CODE>: Returns the string which names the symbol
3825 <DT><CODE>bool equal (const cl_symbol& sym1, const cl_symbol& sym2)</CODE>
3827 <A NAME="IDX292"></A>
3828 Compares two symbols for equality. This is very fast.
3833 <H1><A NAME="SEC55" HREF="cln.html#TOC55">9. Univariate polynomials</A></H1>
3835 <A NAME="IDX293"></A>
3836 <A NAME="IDX294"></A>
3841 <H2><A NAME="SEC56" HREF="cln.html#TOC56">9.1 Univariate polynomial rings</A></H2>
3844 CLN implements univariate polynomials (polynomials in one variable) over an
3845 arbitrary ring. The indeterminate variable may be either unnamed (and will be
3846 printed according to <CODE>default_print_flags.univpoly_varname</CODE>, which
3847 defaults to <SAMP>`x'</SAMP>) or carry a given name. The base ring and the
3848 indeterminate are explicitly part of every polynomial. CLN doesn't allow you to
3849 (accidentally) mix elements of different polynomial rings, e.g.
3850 <CODE>(a^2+1) * (b^3-1)</CODE> will result in a runtime error. (Ideally this should
3851 return a multivariate polynomial, but they are not yet implemented in CLN.)
3855 The classes of univariate polynomial rings are
3862 <cln/ring.h>
3865 Univariate polynomial ring
3867 <cln/univpoly.h>
3869 +----------------+-------------------+
3871 Complex polynomial ring | Modular integer polynomial ring
3872 cl_univpoly_complex_ring | cl_univpoly_modint_ring
3873 <cln/univpoly_complex.h> | <cln/univpoly_modint.h>
3877 Real polynomial ring |
3878 cl_univpoly_real_ring |
3879 <cln/univpoly_real.h> |
3883 Rational polynomial ring |
3884 cl_univpoly_rational_ring |
3885 <cln/univpoly_rational.h> |
3889 Integer polynomial ring
3890 cl_univpoly_integer_ring
3891 <cln/univpoly_integer.h>
3895 and the corresponding classes of univariate polynomials are
3900 Univariate polynomial
3902 <cln/univpoly.h>
3904 +----------------+-------------------+
3906 Complex polynomial | Modular integer polynomial
3908 <cln/univpoly_complex.h> | <cln/univpoly_modint.h>
3914 <cln/univpoly_real.h> |
3918 Rational polynomial |
3920 <cln/univpoly_rational.h> |
3926 <cln/univpoly_integer.h>
3930 Univariate polynomial rings are constructed using the functions
3935 <DT><CODE>cl_univpoly_ring find_univpoly_ring (const cl_ring& R)</CODE>
3937 <DT><CODE>cl_univpoly_ring find_univpoly_ring (const cl_ring& R, const cl_symbol& varname)</CODE>
3939 This function returns the polynomial ring <SAMP>`R[X]'</SAMP>, unnamed or named.
3940 <CODE>R</CODE> may be an arbitrary ring. This function takes care of finding out
3941 about special cases of <CODE>R</CODE>, such as the rings of complex numbers,
3942 real numbers, rational numbers, integers, or modular integer rings.
3943 There is a cache table of rings, indexed by <CODE>R</CODE> and <CODE>varname</CODE>.
3944 This ensures that two calls of this function with the same arguments will
3945 return the same polynomial ring.
3947 <DT><CODE>cl_univpoly_complex_ring find_univpoly_ring (const cl_complex_ring& R)</CODE>
3949 <A NAME="IDX295"></A>
3950 <DT><CODE>cl_univpoly_complex_ring find_univpoly_ring (const cl_complex_ring& R, const cl_symbol& varname)</CODE>
3952 <DT><CODE>cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& R)</CODE>
3954 <DT><CODE>cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& R, const cl_symbol& varname)</CODE>
3956 <DT><CODE>cl_univpoly_rational_ring find_univpoly_ring (const cl_rational_ring& R)</CODE>
3958 <DT><CODE>cl_univpoly_rational_ring find_univpoly_ring (const cl_rational_ring& R, const cl_symbol& varname)</CODE>
3960 <DT><CODE>cl_univpoly_integer_ring find_univpoly_ring (const cl_integer_ring& R)</CODE>
3962 <DT><CODE>cl_univpoly_integer_ring find_univpoly_ring (const cl_integer_ring& R, const cl_symbol& varname)</CODE>
3964 <DT><CODE>cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& R)</CODE>
3966 <DT><CODE>cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& R, const cl_symbol& varname)</CODE>
3968 These functions are equivalent to the general <CODE>find_univpoly_ring</CODE>,
3969 only the return type is more specific, according to the base ring's type.
3974 <H2><A NAME="SEC57" HREF="cln.html#TOC57">9.2 Functions on univariate polynomials</A></H2>
3977 Given a univariate polynomial ring <CODE>R</CODE>, the following members can be used.
3982 <DT><CODE>cl_ring R->basering()</CODE>
3984 <A NAME="IDX296"></A>
3985 This returns the base ring, as passed to <SAMP>`find_univpoly_ring'</SAMP>.
3987 <DT><CODE>cl_UP R->zero()</CODE>
3989 <A NAME="IDX297"></A>
3990 This returns <CODE>0 in R</CODE>, a polynomial of degree -1.
3992 <DT><CODE>cl_UP R->one()</CODE>
3994 <A NAME="IDX298"></A>
3995 This returns <CODE>1 in R</CODE>, a polynomial of degree <= 0.
3997 <DT><CODE>cl_UP R->canonhom (const cl_I& x)</CODE>
3999 <A NAME="IDX299"></A>
4000 This returns <CODE>x in R</CODE>, a polynomial of degree <= 0.
4002 <DT><CODE>cl_UP R->monomial (const cl_ring_element& x, uintL e)</CODE>
4004 <A NAME="IDX300"></A>
4005 This returns a sparse polynomial: <CODE>x * X^e</CODE>, where <CODE>X</CODE> is the
4008 <DT><CODE>cl_UP R->create (sintL degree)</CODE>
4010 <A NAME="IDX301"></A>
4011 Creates a new polynomial with a given degree. The zero polynomial has degree
4012 <CODE>-1</CODE>. After creating the polynomial, you should put in the coefficients,
4013 using the <CODE>set_coeff</CODE> member function, and then call the <CODE>finalize</CODE>
4018 The following are the only destructive operations on univariate polynomials.
4023 <DT><CODE>void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)</CODE>
4025 <A NAME="IDX302"></A>
4026 This changes the coefficient of <CODE>X^index</CODE> in <CODE>x</CODE> to be <CODE>y</CODE>.
4027 After changing a polynomial and before applying any "normal" operation on it,
4028 you should call its <CODE>finalize</CODE> member function.
4030 <DT><CODE>void finalize (cl_UP& x)</CODE>
4032 <A NAME="IDX303"></A>
4033 This function marks the endpoint of destructive modifications of a polynomial.
4034 It normalizes the internal representation so that subsequent computations have
4035 less overhead. Doing normal computations on unnormalized polynomials may
4036 produce wrong results or crash the program.
4040 The following operations are defined on univariate polynomials.
4045 <DT><CODE>cl_univpoly_ring x.ring ()</CODE>
4047 <A NAME="IDX304"></A>
4048 Returns the ring to which the univariate polynomial <CODE>x</CODE> belongs.
4050 <DT><CODE>cl_UP operator+ (const cl_UP&, const cl_UP&)</CODE>
4052 <A NAME="IDX305"></A>
4053 Returns the sum of two univariate polynomials.
4055 <DT><CODE>cl_UP operator- (const cl_UP&, const cl_UP&)</CODE>
4057 <A NAME="IDX306"></A>
4058 Returns the difference of two univariate polynomials.
4060 <DT><CODE>cl_UP operator- (const cl_UP&)</CODE>
4062 Returns the negative of a univariate polynomial.
4064 <DT><CODE>cl_UP operator* (const cl_UP&, const cl_UP&)</CODE>
4066 <A NAME="IDX307"></A>
4067 Returns the product of two univariate polynomials. One of the arguments may
4068 also be a plain integer or an element of the base ring.
4070 <DT><CODE>cl_UP square (const cl_UP&)</CODE>
4072 <A NAME="IDX308"></A>
4073 Returns the square of a univariate polynomial.
4075 <DT><CODE>cl_UP expt_pos (const cl_UP& x, const cl_I& y)</CODE>
4077 <A NAME="IDX309"></A>
4078 <CODE>y</CODE> must be > 0. Returns <CODE>x^y</CODE>.
4080 <DT><CODE>bool operator== (const cl_UP&, const cl_UP&)</CODE>
4082 <A NAME="IDX310"></A>
4083 <DT><CODE>bool operator!= (const cl_UP&, const cl_UP&)</CODE>
4085 <A NAME="IDX311"></A>
4086 Compares two univariate polynomials, belonging to the same univariate
4087 polynomial ring, for equality.
4089 <DT><CODE>cl_boolean zerop (const cl_UP& x)</CODE>
4091 <A NAME="IDX312"></A>
4092 Returns true if <CODE>x</CODE> is <CODE>0 in R</CODE>.
4094 <DT><CODE>sintL degree (const cl_UP& x)</CODE>
4096 <A NAME="IDX313"></A>
4097 Returns the degree of the polynomial. The zero polynomial has degree <CODE>-1</CODE>.
4099 <DT><CODE>cl_ring_element coeff (const cl_UP& x, uintL index)</CODE>
4101 <A NAME="IDX314"></A>
4102 Returns the coefficient of <CODE>X^index</CODE> in the polynomial <CODE>x</CODE>.
4104 <DT><CODE>cl_ring_element x (const cl_ring_element& y)</CODE>
4106 <A NAME="IDX315"></A>
4107 Evaluation: If <CODE>x</CODE> is a polynomial and <CODE>y</CODE> belongs to the base ring,
4108 then <SAMP>`x(y)'</SAMP> returns the value of the substitution of <CODE>y</CODE> into
4111 <DT><CODE>cl_UP deriv (const cl_UP& x)</CODE>
4113 <A NAME="IDX316"></A>
4114 Returns the derivative of the polynomial <CODE>x</CODE> with respect to the
4115 indeterminate <CODE>X</CODE>.
4119 The following output functions are defined (see also the chapter on
4125 <DT><CODE>void fprint (cl_ostream stream, const cl_UP& x)</CODE>
4127 <A NAME="IDX317"></A>
4128 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const cl_UP& x)</CODE>
4130 <A NAME="IDX318"></A>
4131 Prints the univariate polynomial <CODE>x</CODE> on the <CODE>stream</CODE>. The output may
4132 depend on the global printer settings in the variable
4133 <CODE>default_print_flags</CODE>.
4138 <H2><A NAME="SEC58" HREF="cln.html#TOC58">9.3 Special polynomials</A></H2>
4141 The following functions return special polynomials.
4146 <DT><CODE>cl_UP_I tschebychev (sintL n)</CODE>
4148 <A NAME="IDX319"></A>
4149 <A NAME="IDX320"></A>
4150 Returns the n-th Chebyshev polynomial (n >= 0).
4152 <DT><CODE>cl_UP_I hermite (sintL n)</CODE>
4154 <A NAME="IDX321"></A>
4155 <A NAME="IDX322"></A>
4156 Returns the n-th Hermite polynomial (n >= 0).
4158 <DT><CODE>cl_UP_RA legendre (sintL n)</CODE>
4160 <A NAME="IDX323"></A>
4161 <A NAME="IDX324"></A>
4162 Returns the n-th Legendre polynomial (n >= 0).
4164 <DT><CODE>cl_UP_I laguerre (sintL n)</CODE>
4166 <A NAME="IDX325"></A>
4167 <A NAME="IDX326"></A>
4168 Returns the n-th Laguerre polynomial (n >= 0).
4172 Information how to derive the differential equation satisfied by each
4173 of these polynomials from their definition can be found in the
4174 <CODE>doc/polynomial/</CODE> directory.
4179 <H1><A NAME="SEC59" HREF="cln.html#TOC59">10. Internals</A></H1>
4183 <H2><A NAME="SEC60" HREF="cln.html#TOC60">10.1 Why C++ ?</A></H2>
4185 <A NAME="IDX327"></A>
4189 Using C++ as an implementation language provides
4196 Efficiency: It compiles to machine code.
4200 <A NAME="IDX328"></A>
4201 Portability: It runs on all platforms supporting a C++ compiler. Because
4202 of the availability of GNU C++, this includes all currently used 32-bit and
4203 64-bit platforms, independently of the quality of the vendor's C++ compiler.
4207 Type safety: The C++ compilers knows about the number types and complains if,
4208 for example, you try to assign a float to an integer variable. However,
4209 a drawback is that C++ doesn't know about generic types, hence a restriction
4210 like that <CODE>operator+ (const cl_MI&, const cl_MI&)</CODE> requires that both
4211 arguments belong to the same modular ring cannot be expressed as a compile-time
4216 Algebraic syntax: The elementary operations <CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>,
4217 <CODE>=</CODE>, <CODE>==</CODE>, ... can be used in infix notation, which is more
4218 convenient than Lisp notation <SAMP>`(+ x y)'</SAMP> or C notation <SAMP>`add(x,y,&z)'</SAMP>.
4222 With these language features, there is no need for two separate languages,
4223 one for the implementation of the library and one in which the library's users
4224 can program. This means that a prototype implementation of an algorithm
4225 can be integrated into the library immediately after it has been tested and
4226 debugged. No need to rewrite it in a low-level language after having prototyped
4227 in a high-level language.
4232 <H2><A NAME="SEC61" HREF="cln.html#TOC61">10.2 Memory efficiency</A></H2>
4235 In order to save memory allocations, CLN implements:
4242 Object sharing: An operation like <CODE>x+0</CODE> returns <CODE>x</CODE> without copying
4246 <A NAME="IDX329"></A>
4247 <A NAME="IDX330"></A>
4248 Garbage collection: A reference counting mechanism makes sure that any
4249 number object's storage is freed immediately when the last reference to the
4253 Small integers are represented as immediate values instead of pointers
4254 to heap allocated storage. This means that integers <CODE>> -2^29</CODE>,
4255 <CODE>< 2^29</CODE> don't consume heap memory, unless they were explicitly allocated
4261 <H2><A NAME="SEC62" HREF="cln.html#TOC62">10.3 Speed efficiency</A></H2>
4264 Speed efficiency is obtained by the combination of the following tricks
4272 Small integers, being represented as immediate values, don't require
4273 memory access, just a couple of instructions for each elementary operation.
4276 The kernel of CLN has been written in assembly language for some CPUs
4277 (<CODE>i386</CODE>, <CODE>m68k</CODE>, <CODE>sparc</CODE>, <CODE>mips</CODE>, <CODE>arm</CODE>).
4280 On all CPUs, CLN may be configured to use the superefficient low-level
4281 routines from GNU GMP version 3.
4284 For large numbers, CLN uses, instead of the standard <CODE>O(N^2)</CODE>
4285 algorithm, the Karatsuba multiplication, which is an
4286 <CODE>O(N^1.6)</CODE>
4290 For very large numbers (more than 12000 decimal digits), CLN uses
4292 <A NAME="IDX331"></A>
4293 multiplication, which is an asymptotically optimal multiplication
4297 These fast multiplication algorithms also give improvements in the speed
4298 of division and radix conversion.
4303 <H2><A NAME="SEC63" HREF="cln.html#TOC63">10.4 Garbage collection</A></H2>
4305 <A NAME="IDX332"></A>
4309 All the number classes are reference count classes: They only contain a pointer
4310 to an object in the heap. Upon construction, assignment and destruction of
4311 number objects, only the objects' reference count are manipulated.
4315 Memory occupied by number objects are automatically reclaimed as soon as
4316 their reference count drops to zero.
4320 For number rings, another strategy is implemented: There is a cache of,
4321 for example, the modular integer rings. A modular integer ring is destroyed
4322 only if its reference count dropped to zero and the cache is about to be
4323 resized. The effect of this strategy is that recently used rings remain
4324 cached, whereas undue memory consumption through cached rings is avoided.
4329 <H1><A NAME="SEC64" HREF="cln.html#TOC64">11. Using the library</A></H1>
4332 For the following discussion, we will assume that you have installed
4333 the CLN source in <CODE>$CLN_DIR</CODE> and built it in <CODE>$CLN_TARGETDIR</CODE>.
4334 For example, for me it's <CODE>CLN_DIR="$HOME/cln"</CODE> and
4335 <CODE>CLN_TARGETDIR="$HOME/cln/linuxelf"</CODE>. You might define these as
4336 environment variables, or directly substitute the appropriate values.
4341 <H2><A NAME="SEC65" HREF="cln.html#TOC65">11.1 Compiler options</A></H2>
4343 <A NAME="IDX333"></A>
4347 Until you have installed CLN in a public place, the following options are
4352 When you compile CLN application code, add the flags
4355 -I$CLN_DIR/include -I$CLN_TARGETDIR/include
4359 to the C++ compiler's command line (<CODE>make</CODE> variable CFLAGS or CXXFLAGS).
4360 When you link CLN application code to form an executable, add the flags
4363 $CLN_TARGETDIR/src/libcln.a
4367 to the C/C++ compiler's command line (<CODE>make</CODE> variable LIBS).
4371 If you did a <CODE>make install</CODE>, the include files are installed in a
4372 public directory (normally <CODE>/usr/local/include</CODE>), hence you don't
4373 need special flags for compiling. The library has been installed to a
4374 public directory as well (normally <CODE>/usr/local/lib</CODE>), hence when
4375 linking a CLN application it is sufficient to give the flag <CODE>-lcln</CODE>.
4380 <H2><A NAME="SEC66" HREF="cln.html#TOC66">11.2 Compatibility to old CLN versions</A></H2>
4382 <A NAME="IDX334"></A>
4383 <A NAME="IDX335"></A>
4387 As of CLN version 1.1 all non-macro identifiers were hidden in namespace
4388 <CODE>cln</CODE> in order to avoid potential name clashes with other C++
4389 libraries. If you have an old application, you will have to manually
4390 port it to the new scheme. The following principles will help during
4396 All headers are now in a separate subdirectory. Instead of including
4397 <CODE>cl_</CODE><VAR>something</VAR><CODE>.h</CODE>, include
4398 <CODE>cln/</CODE><VAR>something</VAR><CODE>.h</CODE> now.
4401 All public identifiers (typenames and functions) have lost their
4402 <CODE>cl_</CODE> prefix. Exceptions are all the typenames of number types,
4403 (cl_N, cl_I, cl_MI, ...), rings, symbolic types (cl_string,
4404 cl_symbol) and polynomials (cl_UP_<VAR>type</VAR>). (This is because their
4405 names would not be mnemonic enough once the namespace <CODE>cln</CODE> is
4406 imported. Even in a namespace we favor <CODE>cl_N</CODE> over <CODE>N</CODE>.)
4409 All public <EM>functions</EM> that had by a <CODE>cl_</CODE> in their name still
4410 carry that <CODE>cl_</CODE> if it is intrinsic part of a typename (as in
4411 <CODE>cl_I_to_int ()</CODE>).
4415 When developing other libraries, please keep in mind not to import the
4416 namespace <CODE>cln</CODE> in one of your public header files by saying
4417 <CODE>using namespace cln;</CODE>. This would propagate to other applications
4418 and can cause name clashes there.
4423 <H2><A NAME="SEC67" HREF="cln.html#TOC67">11.3 Include files</A></H2>
4425 <A NAME="IDX336"></A>
4426 <A NAME="IDX337"></A>
4430 Here is a summary of the include files and their contents.
4435 <DT><CODE><cln/object.h></CODE>
4437 General definitions, reference counting, garbage collection.
4438 <DT><CODE><cln/number.h></CODE>
4440 The class cl_number.
4441 <DT><CODE><cln/complex.h></CODE>
4443 Functions for class cl_N, the complex numbers.
4444 <DT><CODE><cln/real.h></CODE>
4446 Functions for class cl_R, the real numbers.
4447 <DT><CODE><cln/float.h></CODE>
4449 Functions for class cl_F, the floats.
4450 <DT><CODE><cln/sfloat.h></CODE>
4452 Functions for class cl_SF, the short-floats.
4453 <DT><CODE><cln/ffloat.h></CODE>
4455 Functions for class cl_FF, the single-floats.
4456 <DT><CODE><cln/dfloat.h></CODE>
4458 Functions for class cl_DF, the double-floats.
4459 <DT><CODE><cln/lfloat.h></CODE>
4461 Functions for class cl_LF, the long-floats.
4462 <DT><CODE><cln/rational.h></CODE>
4464 Functions for class cl_RA, the rational numbers.
4465 <DT><CODE><cln/integer.h></CODE>
4467 Functions for class cl_I, the integers.
4468 <DT><CODE><cln/io.h></CODE>
4471 <DT><CODE><cln/complex_io.h></CODE>
4473 Input/Output for class cl_N, the complex numbers.
4474 <DT><CODE><cln/real_io.h></CODE>
4476 Input/Output for class cl_R, the real numbers.
4477 <DT><CODE><cln/float_io.h></CODE>
4479 Input/Output for class cl_F, the floats.
4480 <DT><CODE><cln/sfloat_io.h></CODE>
4482 Input/Output for class cl_SF, the short-floats.
4483 <DT><CODE><cln/ffloat_io.h></CODE>
4485 Input/Output for class cl_FF, the single-floats.
4486 <DT><CODE><cln/dfloat_io.h></CODE>
4488 Input/Output for class cl_DF, the double-floats.
4489 <DT><CODE><cln/lfloat_io.h></CODE>
4491 Input/Output for class cl_LF, the long-floats.
4492 <DT><CODE><cln/rational_io.h></CODE>
4494 Input/Output for class cl_RA, the rational numbers.
4495 <DT><CODE><cln/integer_io.h></CODE>
4497 Input/Output for class cl_I, the integers.
4498 <DT><CODE><cln/input.h></CODE>
4500 Flags for customizing input operations.
4501 <DT><CODE><cln/output.h></CODE>
4503 Flags for customizing output operations.
4504 <DT><CODE><cln/malloc.h></CODE>
4506 <CODE>malloc_hook</CODE>, <CODE>free_hook</CODE>.
4507 <DT><CODE><cln/abort.h></CODE>
4509 <CODE>cl_abort</CODE>.
4510 <DT><CODE><cln/condition.h></CODE>
4512 Conditions/exceptions.
4513 <DT><CODE><cln/string.h></CODE>
4516 <DT><CODE><cln/symbol.h></CODE>
4519 <DT><CODE><cln/proplist.h></CODE>
4522 <DT><CODE><cln/ring.h></CODE>
4525 <DT><CODE><cln/null_ring.h></CODE>
4528 <DT><CODE><cln/complex_ring.h></CODE>
4530 The ring of complex numbers.
4531 <DT><CODE><cln/real_ring.h></CODE>
4533 The ring of real numbers.
4534 <DT><CODE><cln/rational_ring.h></CODE>
4536 The ring of rational numbers.
4537 <DT><CODE><cln/integer_ring.h></CODE>
4539 The ring of integers.
4540 <DT><CODE><cln/numtheory.h></CODE>
4542 Number threory functions.
4543 <DT><CODE><cln/modinteger.h></CODE>
4546 <DT><CODE><cln/V.h></CODE>
4549 <DT><CODE><cln/GV.h></CODE>
4552 <DT><CODE><cln/GV_number.h></CODE>
4554 General vectors over cl_number.
4555 <DT><CODE><cln/GV_complex.h></CODE>
4557 General vectors over cl_N.
4558 <DT><CODE><cln/GV_real.h></CODE>
4560 General vectors over cl_R.
4561 <DT><CODE><cln/GV_rational.h></CODE>
4563 General vectors over cl_RA.
4564 <DT><CODE><cln/GV_integer.h></CODE>
4566 General vectors over cl_I.
4567 <DT><CODE><cln/GV_modinteger.h></CODE>
4569 General vectors of modular integers.
4570 <DT><CODE><cln/SV.h></CODE>
4573 <DT><CODE><cln/SV_number.h></CODE>
4575 Simple vectors over cl_number.
4576 <DT><CODE><cln/SV_complex.h></CODE>
4578 Simple vectors over cl_N.
4579 <DT><CODE><cln/SV_real.h></CODE>
4581 Simple vectors over cl_R.
4582 <DT><CODE><cln/SV_rational.h></CODE>
4584 Simple vectors over cl_RA.
4585 <DT><CODE><cln/SV_integer.h></CODE>
4587 Simple vectors over cl_I.
4588 <DT><CODE><cln/SV_ringelt.h></CODE>
4590 Simple vectors of general ring elements.
4591 <DT><CODE><cln/univpoly.h></CODE>
4593 Univariate polynomials.
4594 <DT><CODE><cln/univpoly_integer.h></CODE>
4596 Univariate polynomials over the integers.
4597 <DT><CODE><cln/univpoly_rational.h></CODE>
4599 Univariate polynomials over the rational numbers.
4600 <DT><CODE><cln/univpoly_real.h></CODE>
4602 Univariate polynomials over the real numbers.
4603 <DT><CODE><cln/univpoly_complex.h></CODE>
4605 Univariate polynomials over the complex numbers.
4606 <DT><CODE><cln/univpoly_modint.h></CODE>
4608 Univariate polynomials over modular integer rings.
4609 <DT><CODE><cln/timing.h></CODE>
4612 <DT><CODE><cln/cln.h></CODE>
4614 Includes all of the above.
4619 <H2><A NAME="SEC68" HREF="cln.html#TOC68">11.4 An Example</A></H2>
4622 A function which computes the nth Fibonacci number can be written as follows.
4623 <A NAME="IDX338"></A>
4628 #include <cln/integer.h>
4629 #include <cln/real.h>
4630 using namespace cln;
4632 // Returns F_n, computed as the nearest integer to
4633 // ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
4634 const cl_I fibonacci (int n)
4636 // Need a precision of ((1+sqrt(5))/2)^-n.
4637 cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
4638 cl_R sqrt5 = sqrt(cl_float(5,prec));
4639 cl_R phi = (1+sqrt5)/2;
4640 return round1( expt(phi,n)/sqrt5 );
4645 Let's explain what is going on in detail.
4649 The include file <CODE><cln/integer.h></CODE> is necessary because the type
4650 <CODE>cl_I</CODE> is used in the function, and the include file <CODE><cln/real.h></CODE>
4651 is needed for the type <CODE>cl_R</CODE> and the floating point number functions.
4652 The order of the include files does not matter. In order not to write out
4653 <CODE>cln::</CODE><VAR>foo</VAR> we can safely import the whole namespace <CODE>cln</CODE>.
4657 Then comes the function declaration. The argument is an <CODE>int</CODE>, the
4658 result an integer. The return type is defined as <SAMP>`const cl_I'</SAMP>, not
4659 simply <SAMP>`cl_I'</SAMP>, because that allows the compiler to detect typos like
4660 <SAMP>`fibonacci(n) = 100'</SAMP>. It would be possible to declare the return
4661 type as <CODE>const cl_R</CODE> (real number) or even <CODE>const cl_N</CODE> (complex
4662 number). We use the most specialized possible return type because functions
4663 which call <SAMP>`fibonacci'</SAMP> will be able to profit from the compiler's type
4664 analysis: Adding two integers is slightly more efficient than adding the
4665 same objects declared as complex numbers, because it needs less type
4666 dispatch. Also, when linking to CLN as a non-shared library, this minimizes
4667 the size of the resulting executable program.
4671 The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest
4672 integer. In order to get a correct result, the absolute error should be less
4673 than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)).
4674 To this end, the first line computes a floating point precision for sqrt(5)
4679 Then sqrt(5) is computed by first converting the integer 5 to a floating point
4680 number and than taking the square root. The converse, first taking the square
4681 root of 5, and then converting to the desired precision, would not work in
4682 CLN: The square root would be computed to a default precision (normally
4683 single-float precision), and the following conversion could not help about
4684 the lacking accuracy. This is because CLN is not a symbolic computer algebra
4685 system and does not represent sqrt(5) in a non-numeric way.
4689 The type <CODE>cl_R</CODE> for sqrt5 and, in the following line, phi is the only
4690 possible choice. You cannot write <CODE>cl_F</CODE> because the C++ compiler can
4691 only infer that <CODE>cl_float(5,prec)</CODE> is a real number. You cannot write
4692 <CODE>cl_N</CODE> because a <SAMP>`round1'</SAMP> does not exist for general complex
4697 When the function returns, all the local variables in the function are
4698 automatically reclaimed (garbage collected). Only the result survives and
4699 gets passed to the caller.
4703 The file <CODE>fibonacci.cc</CODE> in the subdirectory <CODE>examples</CODE>
4704 contains this implementation together with an even faster algorithm.
4709 <H2><A NAME="SEC69" HREF="cln.html#TOC69">11.5 Debugging support</A></H2>
4711 <A NAME="IDX339"></A>
4715 When debugging a CLN application with GNU <CODE>gdb</CODE>, two facilities are
4716 available from the library:
4721 <LI>The library does type checks, range checks, consistency checks at
4723 many places. When one of these fails, the function <CODE>cl_abort()</CODE> is
4724 called. Its default implementation is to perform an <CODE>exit(1)</CODE>, so
4725 you won't have a core dump. But for debugging, it is best to set a
4726 breakpoint at this function:
4729 (gdb) break cl_abort
4732 When this breakpoint is hit, look at the stack's backtrace:
4738 <LI>The debugger's normal <CODE>print</CODE> command doesn't know about
4740 CLN's types and therefore prints mostly useless hexadecimal addresses.
4741 CLN offers a function <CODE>cl_print</CODE>, callable from the debugger,
4742 for printing number objects. In order to get this function, you have
4743 to define the macro <SAMP>`CL_DEBUG'</SAMP> and then include all the header files
4744 for which you want <CODE>cl_print</CODE> debugging support. For example:
4745 <A NAME="IDX340"></A>
4749 #include <cln/string.h>
4752 Now, if you have in your program a variable <CODE>cl_string s</CODE>, and
4753 inspect it under <CODE>gdb</CODE>, the output may look like this:
4757 $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
4758 word = 134568800}}, }
4759 (gdb) call cl_print(s)
4764 Note that the output of <CODE>cl_print</CODE> goes to the program's error output,
4765 not to gdb's standard output.
4767 Note, however, that the above facility does not work with all CLN types,
4768 only with number objects and similar. Therefore CLN offers a member function
4769 <CODE>debug_print()</CODE> on all CLN types. The same macro <SAMP>`CL_DEBUG'</SAMP>
4770 is needed for this member function to be implemented. Under <CODE>gdb</CODE>,
4771 you call it like this:
4772 <A NAME="IDX341"></A>
4776 $7 = {<cl_gcpointer> = { = {pointer = 0x8055b60, heappointer = 0x8055b60,
4777 word = 134568800}}, }
4778 (gdb) call s.debug_print()
4781 >call ($1).debug_print()
4787 Unfortunately, this feature does not seem to work under all circumstances.
4792 <H1><A NAME="SEC70" HREF="cln.html#TOC70">12. Customizing</A></H1>
4794 <A NAME="IDX342"></A>
4799 <H2><A NAME="SEC71" HREF="cln.html#TOC71">12.1 Error handling</A></H2>
4802 When a fatal error occurs, an error message is output to the standard error
4803 output stream, and the function <CODE>cl_abort</CODE> is called. The default
4804 version of this function (provided in the library) terminates the application.
4805 To catch such a fatal error, you need to define the function <CODE>cl_abort</CODE>
4806 yourself, with the prototype
4809 #include <cln/abort.h>
4810 void cl_abort (void);
4814 <A NAME="IDX343"></A>
4815 This function must not return control to its caller.
4820 <H2><A NAME="SEC72" HREF="cln.html#TOC72">12.2 Floating-point underflow</A></H2>
4822 <A NAME="IDX344"></A>
4826 Floating point underflow denotes the situation when a floating-point number
4827 is to be created which is so close to <CODE>0</CODE> that its exponent is too
4828 low to be represented internally. By default, this causes a fatal error.
4829 If you set the global variable
4832 cl_boolean cl_inhibit_floating_point_underflow
4836 to <CODE>cl_true</CODE>, the error will be inhibited, and a floating-point zero
4837 will be generated instead. The default value of
4838 <CODE>cl_inhibit_floating_point_underflow</CODE> is <CODE>cl_false</CODE>.
4843 <H2><A NAME="SEC73" HREF="cln.html#TOC73">12.3 Customizing I/O</A></H2>
4846 The output of the function <CODE>fprint</CODE> may be customized by changing the
4847 value of the global variable <CODE>default_print_flags</CODE>.
4848 <A NAME="IDX345"></A>
4853 <H2><A NAME="SEC74" HREF="cln.html#TOC74">12.4 Customizing the memory allocator</A></H2>
4856 Every memory allocation of CLN is done through the function pointer
4857 <CODE>malloc_hook</CODE>. Freeing of this memory is done through the function
4858 pointer <CODE>free_hook</CODE>. The default versions of these functions,
4859 provided in the library, call <CODE>malloc</CODE> and <CODE>free</CODE> and check
4860 the <CODE>malloc</CODE> result against <CODE>NULL</CODE>.
4861 If you want to provide another memory allocator, you need to define
4862 the variables <CODE>malloc_hook</CODE> and <CODE>free_hook</CODE> yourself,
4866 #include <cln/malloc.h>
4868 void* (*malloc_hook) (size_t size) = ...;
4869 void (*free_hook) (void* ptr) = ...;
4874 <A NAME="IDX346"></A>
4875 <A NAME="IDX347"></A>
4876 The <CODE>cl_malloc_hook</CODE> function must not return a <CODE>NULL</CODE> pointer.
4880 It is not possible to change the memory allocator at runtime, because
4881 it is already called at program startup by the constructors of some
4887 <H1><A NAME="SEC75" HREF="cln.html#TOC75">Index</A></H1>
4895 This document was generated on 28 August 2000 using
4896 <A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A> 1.56k.