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.

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