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