You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4843 lines
143 KiB

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