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.

1804 lines
57 KiB

25 years ago
  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from cln.texi on 14 January 2000 -->
  4. <TITLE>CLN, a Class Library for Numbers - 4. Functions on numbers</TITLE>
  5. </HEAD>
  6. <BODY>
  7. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_3.html">previous</A>, <A HREF="cln_5.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  8. <P><HR><P>
  9. <H1><A NAME="SEC15" HREF="cln_toc.html#TOC15">4. Functions on numbers</A></H1>
  10. <P>
  11. Each of the number classes declares its mathematical operations in the
  12. corresponding include file. For example, if your code operates with
  13. objects of type <CODE>cl_I</CODE>, it should <CODE>#include &#60;cl_integer.h&#62;</CODE>.
  14. <H2><A NAME="SEC16" HREF="cln_toc.html#TOC16">4.1 Constructing numbers</A></H2>
  15. <P>
  16. Here is how to create number objects "from nothing".
  17. <H3><A NAME="SEC17" HREF="cln_toc.html#TOC17">4.1.1 Constructing integers</A></H3>
  18. <P>
  19. <CODE>cl_I</CODE> objects are most easily constructed from C integers and from
  20. strings. See section <A HREF="cln_3.html#SEC14">3.4 Conversions</A>.
  21. <H3><A NAME="SEC18" HREF="cln_toc.html#TOC18">4.1.2 Constructing rational numbers</A></H3>
  22. <P>
  23. <CODE>cl_RA</CODE> objects can be constructed from strings. The syntax
  24. for rational numbers is described in section <A HREF="cln_5.html#SEC44">5.1 Internal and printed representation</A>.
  25. Another standard way to produce a rational number is through application
  26. of <SAMP>`operator /'</SAMP> or <SAMP>`recip'</SAMP> on integers.
  27. <H3><A NAME="SEC19" HREF="cln_toc.html#TOC19">4.1.3 Constructing floating-point numbers</A></H3>
  28. <P>
  29. <CODE>cl_F</CODE> objects with low precision are most easily constructed from
  30. C <SAMP>`float'</SAMP> and <SAMP>`double'</SAMP>. See section <A HREF="cln_3.html#SEC14">3.4 Conversions</A>.
  31. <P>
  32. To construct a <CODE>cl_F</CODE> with high precision, you can use the conversion
  33. from <SAMP>`const char *'</SAMP>, but you have to specify the desired precision
  34. within the string. (See section <A HREF="cln_5.html#SEC44">5.1 Internal and printed representation</A>.)
  35. Example:
  36. <PRE>
  37. cl_F e = "0.271828182845904523536028747135266249775724709369996e+1_40";
  38. </PRE>
  39. <P>
  40. will set <SAMP>`e'</SAMP> to the given value, with a precision of 40 decimal digits.
  41. <P>
  42. The programmatic way to construct a <CODE>cl_F</CODE> with high precision is
  43. through the <CODE>cl_float</CODE> conversion function, see
  44. section <A HREF="cln_4.html#SEC39">4.11.1 Conversion to floating-point numbers</A>. For example, to compute
  45. <CODE>e</CODE> to 40 decimal places, first construct 1.0 to 40 decimal places
  46. and then apply the exponential function:
  47. <PRE>
  48. cl_float_format_t precision = cl_float_format(40);
  49. cl_F e = exp(cl_float(1,precision));
  50. </PRE>
  51. <H3><A NAME="SEC20" HREF="cln_toc.html#TOC20">4.1.4 Constructing complex numbers</A></H3>
  52. <P>
  53. Non-real <CODE>cl_N</CODE> objects are normally constructed through the function
  54. <PRE>
  55. cl_N complex (const cl_R&#38; realpart, const cl_R&#38; imagpart)
  56. </PRE>
  57. <P>
  58. See section <A HREF="cln_4.html#SEC23">4.4 Elementary complex functions</A>.
  59. <H2><A NAME="SEC21" HREF="cln_toc.html#TOC21">4.2 Elementary functions</A></H2>
  60. <P>
  61. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  62. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  63. defines the following operations:
  64. <DL COMPACT>
  65. <DT><CODE><VAR>type</VAR> operator + (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  66. <DD>
  67. Addition.
  68. <DT><CODE><VAR>type</VAR> operator - (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  69. <DD>
  70. Subtraction.
  71. <DT><CODE><VAR>type</VAR> operator - (const <VAR>type</VAR>&#38;)</CODE>
  72. <DD>
  73. Returns the negative of the argument.
  74. <DT><CODE><VAR>type</VAR> plus1 (const <VAR>type</VAR>&#38; x)</CODE>
  75. <DD>
  76. Returns <CODE>x + 1</CODE>.
  77. <DT><CODE><VAR>type</VAR> minus1 (const <VAR>type</VAR>&#38; x)</CODE>
  78. <DD>
  79. Returns <CODE>x - 1</CODE>.
  80. <DT><CODE><VAR>type</VAR> operator * (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  81. <DD>
  82. Multiplication.
  83. <DT><CODE><VAR>type</VAR> square (const <VAR>type</VAR>&#38; x)</CODE>
  84. <DD>
  85. Returns <CODE>x * x</CODE>.
  86. </DL>
  87. <P>
  88. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
  89. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  90. defines the following operations:
  91. <DL COMPACT>
  92. <DT><CODE><VAR>type</VAR> operator / (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  93. <DD>
  94. Division.
  95. <DT><CODE><VAR>type</VAR> recip (const <VAR>type</VAR>&#38;)</CODE>
  96. <DD>
  97. Returns the reciprocal of the argument.
  98. </DL>
  99. <P>
  100. The class <CODE>cl_I</CODE> doesn't define a <SAMP>`/'</SAMP> operation because
  101. in the C/C++ language this operator, applied to integral types,
  102. denotes the <SAMP>`floor'</SAMP> or <SAMP>`truncate'</SAMP> operation (which one of these,
  103. is implementation dependent). (See section <A HREF="cln_4.html#SEC25">4.6 Rounding functions</A>)
  104. Instead, <CODE>cl_I</CODE> defines an "exact quotient" function:
  105. <DL COMPACT>
  106. <DT><CODE>cl_I exquo (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  107. <DD>
  108. Checks that <CODE>y</CODE> divides <CODE>x</CODE>, and returns the quotient <CODE>x</CODE>/<CODE>y</CODE>.
  109. </DL>
  110. <P>
  111. The following exponentiation functions are defined:
  112. <DL COMPACT>
  113. <DT><CODE>cl_I expt_pos (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  114. <DD>
  115. <DT><CODE>cl_RA expt_pos (const cl_RA&#38; x, const cl_I&#38; y)</CODE>
  116. <DD>
  117. <CODE>y</CODE> must be &#62; 0. Returns <CODE>x^y</CODE>.
  118. <DT><CODE>cl_RA expt (const cl_RA&#38; x, const cl_I&#38; y)</CODE>
  119. <DD>
  120. <DT><CODE>cl_R expt (const cl_R&#38; x, const cl_I&#38; y)</CODE>
  121. <DD>
  122. <DT><CODE>cl_N expt (const cl_N&#38; x, const cl_I&#38; y)</CODE>
  123. <DD>
  124. Returns <CODE>x^y</CODE>.
  125. </DL>
  126. <P>
  127. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  128. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  129. defines the following operation:
  130. <DL COMPACT>
  131. <DT><CODE><VAR>type</VAR> abs (const <VAR>type</VAR>&#38; x)</CODE>
  132. <DD>
  133. Returns the absolute value of <CODE>x</CODE>.
  134. This is <CODE>x</CODE> if <CODE>x &#62;= 0</CODE>, and <CODE>-x</CODE> if <CODE>x &#60;= 0</CODE>.
  135. </DL>
  136. <P>
  137. The class <CODE>cl_N</CODE> implements this as follows:
  138. <DL COMPACT>
  139. <DT><CODE>cl_R abs (const cl_N x)</CODE>
  140. <DD>
  141. Returns the absolute value of <CODE>x</CODE>.
  142. </DL>
  143. <P>
  144. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  145. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  146. defines the following operation:
  147. <DL COMPACT>
  148. <DT><CODE><VAR>type</VAR> signum (const <VAR>type</VAR>&#38; x)</CODE>
  149. <DD>
  150. Returns the sign of <CODE>x</CODE>, in the same number format as <CODE>x</CODE>.
  151. This is defined as <CODE>x / abs(x)</CODE> if <CODE>x</CODE> is non-zero, and
  152. <CODE>x</CODE> if <CODE>x</CODE> is zero. If <CODE>x</CODE> is real, the value is either
  153. 0 or 1 or -1.
  154. </DL>
  155. <H2><A NAME="SEC22" HREF="cln_toc.html#TOC22">4.3 Elementary rational functions</A></H2>
  156. <P>
  157. Each of the classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE> defines the following operations:
  158. <DL COMPACT>
  159. <DT><CODE>cl_I numerator (const <VAR>type</VAR>&#38; x)</CODE>
  160. <DD>
  161. Returns the numerator of <CODE>x</CODE>.
  162. <DT><CODE>cl_I denominator (const <VAR>type</VAR>&#38; x)</CODE>
  163. <DD>
  164. Returns the denominator of <CODE>x</CODE>.
  165. </DL>
  166. <P>
  167. The numerator and denominator of a rational number are normalized in such
  168. a way that they have no factor in common and the denominator is positive.
  169. <H2><A NAME="SEC23" HREF="cln_toc.html#TOC23">4.4 Elementary complex functions</A></H2>
  170. <P>
  171. The class <CODE>cl_N</CODE> defines the following operation:
  172. <DL COMPACT>
  173. <DT><CODE>cl_N complex (const cl_R&#38; a, const cl_R&#38; b)</CODE>
  174. <DD>
  175. Returns the complex number <CODE>a+bi</CODE>, that is, the complex number with
  176. real part <CODE>a</CODE> and imaginary part <CODE>b</CODE>.
  177. </DL>
  178. <P>
  179. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE> defines the following operations:
  180. <DL COMPACT>
  181. <DT><CODE>cl_R realpart (const <VAR>type</VAR>&#38; x)</CODE>
  182. <DD>
  183. Returns the real part of <CODE>x</CODE>.
  184. <DT><CODE>cl_R imagpart (const <VAR>type</VAR>&#38; x)</CODE>
  185. <DD>
  186. Returns the imaginary part of <CODE>x</CODE>.
  187. <DT><CODE><VAR>type</VAR> conjugate (const <VAR>type</VAR>&#38; x)</CODE>
  188. <DD>
  189. Returns the complex conjugate of <CODE>x</CODE>.
  190. </DL>
  191. <P>
  192. We have the relations
  193. <UL>
  194. <LI>
  195. <CODE>x = complex(realpart(x), imagpart(x))</CODE>
  196. <LI>
  197. <CODE>conjugate(x) = complex(realpart(x), -imagpart(x))</CODE>
  198. </UL>
  199. <H2><A NAME="SEC24" HREF="cln_toc.html#TOC24">4.5 Comparisons</A></H2>
  200. <P>
  201. Each of the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  202. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  203. defines the following operations:
  204. <DL COMPACT>
  205. <DT><CODE>bool operator == (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  206. <DD>
  207. <DT><CODE>bool operator != (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  208. <DD>
  209. Comparison, as in C and C++.
  210. <DT><CODE>uint32 cl_equal_hashcode (const <VAR>type</VAR>&#38;)</CODE>
  211. <DD>
  212. Returns a 32-bit hash code that is the same for any two numbers which are
  213. the same according to <CODE>==</CODE>. This hash code depends on the number's value,
  214. not its type or precision.
  215. <DT><CODE>cl_boolean zerop (const <VAR>type</VAR>&#38; x)</CODE>
  216. <DD>
  217. Compare against zero: <CODE>x == 0</CODE>
  218. </DL>
  219. <P>
  220. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  221. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  222. defines the following operations:
  223. <DL COMPACT>
  224. <DT><CODE>cl_signean cl_compare (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  225. <DD>
  226. Compares <CODE>x</CODE> and <CODE>y</CODE>. Returns +1 if <CODE>x</CODE>&#62;<CODE>y</CODE>,
  227. -1 if <CODE>x</CODE>&#60;<CODE>y</CODE>, 0 if <CODE>x</CODE>=<CODE>y</CODE>.
  228. <DT><CODE>bool operator &#60;= (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  229. <DD>
  230. <DT><CODE>bool operator &#60; (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  231. <DD>
  232. <DT><CODE>bool operator &#62;= (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  233. <DD>
  234. <DT><CODE>bool operator &#62; (const <VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  235. <DD>
  236. Comparison, as in C and C++.
  237. <DT><CODE>cl_boolean minusp (const <VAR>type</VAR>&#38; x)</CODE>
  238. <DD>
  239. Compare against zero: <CODE>x &#60; 0</CODE>
  240. <DT><CODE>cl_boolean plusp (const <VAR>type</VAR>&#38; x)</CODE>
  241. <DD>
  242. Compare against zero: <CODE>x &#62; 0</CODE>
  243. <DT><CODE><VAR>type</VAR> max (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  244. <DD>
  245. Return the maximum of <CODE>x</CODE> and <CODE>y</CODE>.
  246. <DT><CODE><VAR>type</VAR> min (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  247. <DD>
  248. Return the minimum of <CODE>x</CODE> and <CODE>y</CODE>.
  249. </DL>
  250. <P>
  251. When a floating point number and a rational number are compared, the float
  252. is first converted to a rational number using the function <CODE>rational</CODE>.
  253. Since a floating point number actually represents an interval of real numbers,
  254. the result might be surprising.
  255. For example, <CODE>(cl_F)(cl_R)"1/3" == (cl_R)"1/3"</CODE> returns false because
  256. there is no floating point number whose value is exactly <CODE>1/3</CODE>.
  257. <H2><A NAME="SEC25" HREF="cln_toc.html#TOC25">4.6 Rounding functions</A></H2>
  258. <P>
  259. When a real number is to be converted to an integer, there is no "best"
  260. rounding. The desired rounding function depends on the application.
  261. The Common Lisp and ISO Lisp standards offer four rounding functions:
  262. <DL COMPACT>
  263. <DT><CODE>floor(x)</CODE>
  264. <DD>
  265. This is the largest integer &#60;=<CODE>x</CODE>.
  266. <DT><CODE>ceiling(x)</CODE>
  267. <DD>
  268. This is the smallest integer &#62;=<CODE>x</CODE>.
  269. <DT><CODE>truncate(x)</CODE>
  270. <DD>
  271. Among the integers between 0 and <CODE>x</CODE> (inclusive) the one nearest to <CODE>x</CODE>.
  272. <DT><CODE>round(x)</CODE>
  273. <DD>
  274. The integer nearest to <CODE>x</CODE>. If <CODE>x</CODE> is exactly halfway between two
  275. integers, choose the even one.
  276. </DL>
  277. <P>
  278. These functions have different advantages:
  279. <P>
  280. <CODE>floor</CODE> and <CODE>ceiling</CODE> are translation invariant:
  281. <CODE>floor(x+n) = floor(x) + n</CODE> and <CODE>ceiling(x+n) = ceiling(x) + n</CODE>
  282. for every <CODE>x</CODE> and every integer <CODE>n</CODE>.
  283. <P>
  284. On the other hand, <CODE>truncate</CODE> and <CODE>round</CODE> are symmetric:
  285. <CODE>truncate(-x) = -truncate(x)</CODE> and <CODE>round(-x) = -round(x)</CODE>,
  286. and furthermore <CODE>round</CODE> is unbiased: on the "average", it rounds
  287. down exactly as often as it rounds up.
  288. <P>
  289. The functions are related like this:
  290. <UL>
  291. <LI>
  292. <CODE>ceiling(m/n) = floor((m+n-1)/n) = floor((m-1)/n)+1</CODE>
  293. for rational numbers <CODE>m/n</CODE> (<CODE>m</CODE>, <CODE>n</CODE> integers, <CODE>n</CODE>&#62;0), and
  294. <LI>
  295. <CODE>truncate(x) = sign(x) * floor(abs(x))</CODE>
  296. </UL>
  297. <P>
  298. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
  299. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  300. defines the following operations:
  301. <DL COMPACT>
  302. <DT><CODE>cl_I floor1 (const <VAR>type</VAR>&#38; x)</CODE>
  303. <DD>
  304. Returns <CODE>floor(x)</CODE>.
  305. <DT><CODE>cl_I ceiling1 (const <VAR>type</VAR>&#38; x)</CODE>
  306. <DD>
  307. Returns <CODE>ceiling(x)</CODE>.
  308. <DT><CODE>cl_I truncate1 (const <VAR>type</VAR>&#38; x)</CODE>
  309. <DD>
  310. Returns <CODE>truncate(x)</CODE>.
  311. <DT><CODE>cl_I round1 (const <VAR>type</VAR>&#38; x)</CODE>
  312. <DD>
  313. Returns <CODE>round(x)</CODE>.
  314. </DL>
  315. <P>
  316. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  317. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  318. defines the following operations:
  319. <DL COMPACT>
  320. <DT><CODE>cl_I floor1 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  321. <DD>
  322. Returns <CODE>floor(x/y)</CODE>.
  323. <DT><CODE>cl_I ceiling1 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  324. <DD>
  325. Returns <CODE>ceiling(x/y)</CODE>.
  326. <DT><CODE>cl_I truncate1 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  327. <DD>
  328. Returns <CODE>truncate(x/y)</CODE>.
  329. <DT><CODE>cl_I round1 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  330. <DD>
  331. Returns <CODE>round(x/y)</CODE>.
  332. </DL>
  333. <P>
  334. These functions are called <SAMP>`floor1'</SAMP>, ... here instead of
  335. <SAMP>`floor'</SAMP>, ..., because on some systems, system dependent include
  336. files define <SAMP>`floor'</SAMP> and <SAMP>`ceiling'</SAMP> as macros.
  337. <P>
  338. In many cases, one needs both the quotient and the remainder of a division.
  339. It is more efficient to compute both at the same time than to perform
  340. two divisions, one for quotient and the next one for the remainder.
  341. The following functions therefore return a structure containing both
  342. the quotient and the remainder. The suffix <SAMP>`2'</SAMP> indicates the number
  343. of "return values". The remainder is defined as follows:
  344. <UL>
  345. <LI>
  346. for the computation of <CODE>quotient = floor(x)</CODE>,
  347. <CODE>remainder = x - quotient</CODE>,
  348. <LI>
  349. for the computation of <CODE>quotient = floor(x,y)</CODE>,
  350. <CODE>remainder = x - quotient*y</CODE>,
  351. </UL>
  352. <P>
  353. and similarly for the other three operations.
  354. <P>
  355. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
  356. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  357. defines the following operations:
  358. <DL COMPACT>
  359. <DT><CODE>struct <VAR>type</VAR>_div_t { cl_I quotient; <VAR>type</VAR> remainder; };</CODE>
  360. <DD>
  361. <DT><CODE><VAR>type</VAR>_div_t floor2 (const <VAR>type</VAR>&#38; x)</CODE>
  362. <DD>
  363. <DT><CODE><VAR>type</VAR>_div_t ceiling2 (const <VAR>type</VAR>&#38; x)</CODE>
  364. <DD>
  365. <DT><CODE><VAR>type</VAR>_div_t truncate2 (const <VAR>type</VAR>&#38; x)</CODE>
  366. <DD>
  367. <DT><CODE><VAR>type</VAR>_div_t round2 (const <VAR>type</VAR>&#38; x)</CODE>
  368. <DD>
  369. </DL>
  370. <P>
  371. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  372. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  373. defines the following operations:
  374. <DL COMPACT>
  375. <DT><CODE>struct <VAR>type</VAR>_div_t { cl_I quotient; <VAR>type</VAR> remainder; };</CODE>
  376. <DD>
  377. <DT><CODE><VAR>type</VAR>_div_t floor2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  378. <DD>
  379. <DT><CODE><VAR>type</VAR>_div_t ceiling2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  380. <DD>
  381. <DT><CODE><VAR>type</VAR>_div_t truncate2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  382. <DD>
  383. <DT><CODE><VAR>type</VAR>_div_t round2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  384. <DD>
  385. </DL>
  386. <P>
  387. Sometimes, one wants the quotient as a floating-point number (of the
  388. same format as the argument, if the argument is a float) instead of as
  389. an integer. The prefix <SAMP>`f'</SAMP> indicates this.
  390. <P>
  391. Each of the classes
  392. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  393. defines the following operations:
  394. <DL COMPACT>
  395. <DT><CODE><VAR>type</VAR> ffloor (const <VAR>type</VAR>&#38; x)</CODE>
  396. <DD>
  397. <DT><CODE><VAR>type</VAR> fceiling (const <VAR>type</VAR>&#38; x)</CODE>
  398. <DD>
  399. <DT><CODE><VAR>type</VAR> ftruncate (const <VAR>type</VAR>&#38; x)</CODE>
  400. <DD>
  401. <DT><CODE><VAR>type</VAR> fround (const <VAR>type</VAR>&#38; x)</CODE>
  402. <DD>
  403. </DL>
  404. <P>
  405. and similarly for class <CODE>cl_R</CODE>, but with return type <CODE>cl_F</CODE>.
  406. <P>
  407. The class <CODE>cl_R</CODE> defines the following operations:
  408. <DL COMPACT>
  409. <DT><CODE>cl_F ffloor (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  410. <DD>
  411. <DT><CODE>cl_F fceiling (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  412. <DD>
  413. <DT><CODE>cl_F ftruncate (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  414. <DD>
  415. <DT><CODE>cl_F fround (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  416. <DD>
  417. </DL>
  418. <P>
  419. These functions also exist in versions which return both the quotient
  420. and the remainder. The suffix <SAMP>`2'</SAMP> indicates this.
  421. <P>
  422. Each of the classes
  423. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  424. defines the following operations:
  425. <DL COMPACT>
  426. <DT><CODE>struct <VAR>type</VAR>_fdiv_t { <VAR>type</VAR> quotient; <VAR>type</VAR> remainder; };</CODE>
  427. <DD>
  428. <DT><CODE><VAR>type</VAR>_fdiv_t ffloor2 (const <VAR>type</VAR>&#38; x)</CODE>
  429. <DD>
  430. <DT><CODE><VAR>type</VAR>_fdiv_t fceiling2 (const <VAR>type</VAR>&#38; x)</CODE>
  431. <DD>
  432. <DT><CODE><VAR>type</VAR>_fdiv_t ftruncate2 (const <VAR>type</VAR>&#38; x)</CODE>
  433. <DD>
  434. <DT><CODE><VAR>type</VAR>_fdiv_t fround2 (const <VAR>type</VAR>&#38; x)</CODE>
  435. <DD>
  436. </DL>
  437. <P>
  438. and similarly for class <CODE>cl_R</CODE>, but with quotient type <CODE>cl_F</CODE>.
  439. <P>
  440. The class <CODE>cl_R</CODE> defines the following operations:
  441. <DL COMPACT>
  442. <DT><CODE>struct <VAR>type</VAR>_fdiv_t { cl_F quotient; cl_R remainder; };</CODE>
  443. <DD>
  444. <DT><CODE><VAR>type</VAR>_fdiv_t ffloor2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  445. <DD>
  446. <DT><CODE><VAR>type</VAR>_fdiv_t fceiling2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  447. <DD>
  448. <DT><CODE><VAR>type</VAR>_fdiv_t ftruncate2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  449. <DD>
  450. <DT><CODE><VAR>type</VAR>_fdiv_t fround2 (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  451. <DD>
  452. </DL>
  453. <P>
  454. Other applications need only the remainder of a division.
  455. The remainder of <SAMP>`floor'</SAMP> and <SAMP>`ffloor'</SAMP> is called <SAMP>`mod'</SAMP>
  456. (abbreviation of "modulo"). The remainder <SAMP>`truncate'</SAMP> and
  457. <SAMP>`ftruncate'</SAMP> is called <SAMP>`rem'</SAMP> (abbreviation of "remainder").
  458. <UL>
  459. <LI>
  460. <CODE>mod(x,y) = floor2(x,y).remainder = x - floor(x/y)*y</CODE>
  461. <LI>
  462. <CODE>rem(x,y) = truncate2(x,y).remainder = x - truncate(x/y)*y</CODE>
  463. </UL>
  464. <P>
  465. If <CODE>x</CODE> and <CODE>y</CODE> are both &#62;= 0, <CODE>mod(x,y) = rem(x,y) &#62;= 0</CODE>.
  466. In general, <CODE>mod(x,y)</CODE> has the sign of <CODE>y</CODE> or is zero,
  467. and <CODE>rem(x,y)</CODE> has the sign of <CODE>x</CODE> or is zero.
  468. <P>
  469. The classes <CODE>cl_R</CODE>, <CODE>cl_I</CODE> define the following operations:
  470. <DL COMPACT>
  471. <DT><CODE><VAR>type</VAR> mod (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  472. <DD>
  473. <DT><CODE><VAR>type</VAR> rem (const <VAR>type</VAR>&#38; x, const <VAR>type</VAR>&#38; y)</CODE>
  474. <DD>
  475. </DL>
  476. <H2><A NAME="SEC26" HREF="cln_toc.html#TOC26">4.7 Roots</A></H2>
  477. <P>
  478. Each of the classes <CODE>cl_R</CODE>,
  479. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  480. defines the following operation:
  481. <DL COMPACT>
  482. <DT><CODE><VAR>type</VAR> sqrt (const <VAR>type</VAR>&#38; x)</CODE>
  483. <DD>
  484. <CODE>x</CODE> must be &#62;= 0. This function returns the square root of <CODE>x</CODE>,
  485. normalized to be &#62;= 0. If <CODE>x</CODE> is the square of a rational number,
  486. <CODE>sqrt(x)</CODE> will be a rational number, else it will return a
  487. floating-point approximation.
  488. </DL>
  489. <P>
  490. The classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE> define the following operation:
  491. <DL COMPACT>
  492. <DT><CODE>cl_boolean sqrtp (const <VAR>type</VAR>&#38; x, <VAR>type</VAR>* root)</CODE>
  493. <DD>
  494. This tests whether <CODE>x</CODE> is a perfect square. If so, it returns true
  495. and the exact square root in <CODE>*root</CODE>, else it returns false.
  496. </DL>
  497. <P>
  498. Furthermore, for integers, similarly:
  499. <DL COMPACT>
  500. <DT><CODE>cl_boolean isqrt (const <VAR>type</VAR>&#38; x, <VAR>type</VAR>* root)</CODE>
  501. <DD>
  502. <CODE>x</CODE> should be &#62;= 0. This function sets <CODE>*root</CODE> to
  503. <CODE>floor(sqrt(x))</CODE> and returns the same value as <CODE>sqrtp</CODE>:
  504. the boolean value <CODE>(expt(*root,2) == x)</CODE>.
  505. </DL>
  506. <P>
  507. For <CODE>n</CODE>th roots, the classes <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>
  508. define the following operation:
  509. <DL COMPACT>
  510. <DT><CODE>cl_boolean rootp (const <VAR>type</VAR>&#38; x, const cl_I&#38; n, <VAR>type</VAR>* root)</CODE>
  511. <DD>
  512. <CODE>x</CODE> must be &#62;= 0. <CODE>n</CODE> must be &#62; 0.
  513. This tests whether <CODE>x</CODE> is an <CODE>n</CODE>th power of a rational number.
  514. If so, it returns true and the exact root in <CODE>*root</CODE>, else it returns
  515. false.
  516. </DL>
  517. <P>
  518. The only square root function which accepts negative numbers is the one
  519. for class <CODE>cl_N</CODE>:
  520. <DL COMPACT>
  521. <DT><CODE>cl_N sqrt (const cl_N&#38; z)</CODE>
  522. <DD>
  523. Returns the square root of <CODE>z</CODE>, as defined by the formula
  524. <CODE>sqrt(z) = exp(log(z)/2)</CODE>. Conversion to a floating-point type
  525. or to a complex number are done if necessary. The range of the result is the
  526. right half plane <CODE>realpart(sqrt(z)) &#62;= 0</CODE>
  527. including the positive imaginary axis and 0, but excluding
  528. the negative imaginary axis.
  529. The result is an exact number only if <CODE>z</CODE> is an exact number.
  530. </DL>
  531. <H2><A NAME="SEC27" HREF="cln_toc.html#TOC27">4.8 Transcendental functions</A></H2>
  532. <P>
  533. The transcendental functions return an exact result if the argument
  534. is exact and the result is exact as well. Otherwise they must return
  535. inexact numbers even if the argument is exact.
  536. For example, <CODE>cos(0) = 1</CODE> returns the rational number <CODE>1</CODE>.
  537. <H3><A NAME="SEC28" HREF="cln_toc.html#TOC28">4.8.1 Exponential and logarithmic functions</A></H3>
  538. <DL COMPACT>
  539. <DT><CODE>cl_R exp (const cl_R&#38; x)</CODE>
  540. <DD>
  541. <DT><CODE>cl_N exp (const cl_N&#38; x)</CODE>
  542. <DD>
  543. Returns the exponential function of <CODE>x</CODE>. This is <CODE>e^x</CODE> where
  544. <CODE>e</CODE> is the base of the natural logarithms. The range of the result
  545. is the entire complex plane excluding 0.
  546. <DT><CODE>cl_R ln (const cl_R&#38; x)</CODE>
  547. <DD>
  548. <CODE>x</CODE> must be &#62; 0. Returns the (natural) logarithm of x.
  549. <DT><CODE>cl_N log (const cl_N&#38; x)</CODE>
  550. <DD>
  551. Returns the (natural) logarithm of x. If <CODE>x</CODE> is real and positive,
  552. this is <CODE>ln(x)</CODE>. In general, <CODE>log(x) = log(abs(x)) + i*phase(x)</CODE>.
  553. The range of the result is the strip in the complex plane
  554. <CODE>-pi &#60; imagpart(log(x)) &#60;= pi</CODE>.
  555. <DT><CODE>cl_R phase (const cl_N&#38; x)</CODE>
  556. <DD>
  557. Returns the angle part of <CODE>x</CODE> in its polar representation as a
  558. complex number. That is, <CODE>phase(x) = atan(realpart(x),imagpart(x))</CODE>.
  559. This is also the imaginary part of <CODE>log(x)</CODE>.
  560. The range of the result is the interval <CODE>-pi &#60; phase(x) &#60;= pi</CODE>.
  561. The result will be an exact number only if <CODE>zerop(x)</CODE> or
  562. if <CODE>x</CODE> is real and positive.
  563. <DT><CODE>cl_R log (const cl_R&#38; a, const cl_R&#38; b)</CODE>
  564. <DD>
  565. <CODE>a</CODE> and <CODE>b</CODE> must be &#62; 0. Returns the logarithm of <CODE>a</CODE> with
  566. respect to base <CODE>b</CODE>. <CODE>log(a,b) = ln(a)/ln(b)</CODE>.
  567. The result can be exact only if <CODE>a = 1</CODE> or if <CODE>a</CODE> and <CODE>b</CODE>
  568. are both rational.
  569. <DT><CODE>cl_N log (const cl_N&#38; a, const cl_N&#38; b)</CODE>
  570. <DD>
  571. Returns the logarithm of <CODE>a</CODE> with respect to base <CODE>b</CODE>.
  572. <CODE>log(a,b) = log(a)/log(b)</CODE>.
  573. <DT><CODE>cl_N expt (const cl_N&#38; x, const cl_N&#38; y)</CODE>
  574. <DD>
  575. Exponentiation: Returns <CODE>x^y = exp(y*log(x))</CODE>.
  576. </DL>
  577. <P>
  578. The constant e = exp(1) = 2.71828... is returned by the following functions:
  579. <DL COMPACT>
  580. <DT><CODE>cl_F cl_exp1 (cl_float_format_t f)</CODE>
  581. <DD>
  582. Returns e as a float of format <CODE>f</CODE>.
  583. <DT><CODE>cl_F cl_exp1 (const cl_F&#38; y)</CODE>
  584. <DD>
  585. Returns e in the float format of <CODE>y</CODE>.
  586. <DT><CODE>cl_F cl_exp1 (void)</CODE>
  587. <DD>
  588. Returns e as a float of format <CODE>cl_default_float_format</CODE>.
  589. </DL>
  590. <H3><A NAME="SEC29" HREF="cln_toc.html#TOC29">4.8.2 Trigonometric functions</A></H3>
  591. <DL COMPACT>
  592. <DT><CODE>cl_R sin (const cl_R&#38; x)</CODE>
  593. <DD>
  594. Returns <CODE>sin(x)</CODE>. The range of the result is the interval
  595. <CODE>-1 &#60;= sin(x) &#60;= 1</CODE>.
  596. <DT><CODE>cl_N sin (const cl_N&#38; z)</CODE>
  597. <DD>
  598. Returns <CODE>sin(z)</CODE>. The range of the result is the entire complex plane.
  599. <DT><CODE>cl_R cos (const cl_R&#38; x)</CODE>
  600. <DD>
  601. Returns <CODE>cos(x)</CODE>. The range of the result is the interval
  602. <CODE>-1 &#60;= cos(x) &#60;= 1</CODE>.
  603. <DT><CODE>cl_N cos (const cl_N&#38; x)</CODE>
  604. <DD>
  605. Returns <CODE>cos(z)</CODE>. The range of the result is the entire complex plane.
  606. <DT><CODE>struct cl_cos_sin_t { cl_R cos; cl_R sin; };</CODE>
  607. <DD>
  608. <DT><CODE>cl_cos_sin_t cl_cos_sin (const cl_R&#38; x)</CODE>
  609. <DD>
  610. Returns both <CODE>sin(x)</CODE> and <CODE>cos(x)</CODE>. This is more efficient than
  611. computing them separately. The relation <CODE>cos^2 + sin^2 = 1</CODE> will
  612. hold only approximately.
  613. <DT><CODE>cl_R tan (const cl_R&#38; x)</CODE>
  614. <DD>
  615. <DT><CODE>cl_N tan (const cl_N&#38; x)</CODE>
  616. <DD>
  617. Returns <CODE>tan(x) = sin(x)/cos(x)</CODE>.
  618. <DT><CODE>cl_N cis (const cl_R&#38; x)</CODE>
  619. <DD>
  620. <DT><CODE>cl_N cis (const cl_N&#38; x)</CODE>
  621. <DD>
  622. Returns <CODE>exp(i*x)</CODE>. The name <SAMP>`cis'</SAMP> means "cos + i sin", because
  623. <CODE>e^(i*x) = cos(x) + i*sin(x)</CODE>.
  624. <DT><CODE>cl_N asin (const cl_N&#38; z)</CODE>
  625. <DD>
  626. Returns <CODE>arcsin(z)</CODE>. This is defined as
  627. <CODE>arcsin(z) = log(iz+sqrt(1-z^2))/i</CODE> and satisfies
  628. <CODE>arcsin(-z) = -arcsin(z)</CODE>.
  629. The range of the result is the strip in the complex domain
  630. <CODE>-pi/2 &#60;= realpart(arcsin(z)) &#60;= pi/2</CODE>, excluding the numbers
  631. with <CODE>realpart = -pi/2</CODE> and <CODE>imagpart &#60; 0</CODE> and the numbers
  632. with <CODE>realpart = pi/2</CODE> and <CODE>imagpart &#62; 0</CODE>.
  633. <DT><CODE>cl_N acos (const cl_N&#38; z)</CODE>
  634. <DD>
  635. Returns <CODE>arccos(z)</CODE>. This is defined as
  636. <CODE>arccos(z) = pi/2 - arcsin(z) = log(z+i*sqrt(1-z^2))/i</CODE>
  637. and satisfies <CODE>arccos(-z) = pi - arccos(z)</CODE>.
  638. The range of the result is the strip in the complex domain
  639. <CODE>0 &#60;= realpart(arcsin(z)) &#60;= pi</CODE>, excluding the numbers
  640. with <CODE>realpart = 0</CODE> and <CODE>imagpart &#60; 0</CODE> and the numbers
  641. with <CODE>realpart = pi</CODE> and <CODE>imagpart &#62; 0</CODE>.
  642. <DT><CODE>cl_R atan (const cl_R&#38; x, const cl_R&#38; y)</CODE>
  643. <DD>
  644. Returns the angle of the polar representation of the complex number
  645. <CODE>x+iy</CODE>. This is <CODE>atan(y/x)</CODE> if <CODE>x&#62;0</CODE>. The range of
  646. the result is the interval <CODE>-pi &#60; atan(x,y) &#60;= pi</CODE>. The result will
  647. be an exact number only if <CODE>x &#62; 0</CODE> and <CODE>y</CODE> is the exact <CODE>0</CODE>.
  648. WARNING: In Common Lisp, this function is called as <CODE>(atan y x)</CODE>,
  649. with reversed order of arguments.
  650. <DT><CODE>cl_R atan (const cl_R&#38; x)</CODE>
  651. <DD>
  652. Returns <CODE>arctan(x)</CODE>. This is the same as <CODE>atan(1,x)</CODE>. The range
  653. of the result is the interval <CODE>-pi/2 &#60; atan(x) &#60; pi/2</CODE>. The result
  654. will be an exact number only if <CODE>x</CODE> is the exact <CODE>0</CODE>.
  655. <DT><CODE>cl_N atan (const cl_N&#38; z)</CODE>
  656. <DD>
  657. Returns <CODE>arctan(z)</CODE>. This is defined as
  658. <CODE>arctan(z) = (log(1+iz)-log(1-iz)) / 2i</CODE> and satisfies
  659. <CODE>arctan(-z) = -arctan(z)</CODE>. The range of the result is
  660. the strip in the complex domain
  661. <CODE>-pi/2 &#60;= realpart(arctan(z)) &#60;= pi/2</CODE>, excluding the numbers
  662. with <CODE>realpart = -pi/2</CODE> and <CODE>imagpart &#62;= 0</CODE> and the numbers
  663. with <CODE>realpart = pi/2</CODE> and <CODE>imagpart &#60;= 0</CODE>.
  664. </DL>
  665. <P>
  666. The constant pi = 3.14... is returned by the following functions:
  667. <DL COMPACT>
  668. <DT><CODE>cl_F cl_pi (cl_float_format_t f)</CODE>
  669. <DD>
  670. Returns pi as a float of format <CODE>f</CODE>.
  671. <DT><CODE>cl_F cl_pi (const cl_F&#38; y)</CODE>
  672. <DD>
  673. Returns pi in the float format of <CODE>y</CODE>.
  674. <DT><CODE>cl_F cl_pi (void)</CODE>
  675. <DD>
  676. Returns pi as a float of format <CODE>cl_default_float_format</CODE>.
  677. </DL>
  678. <H3><A NAME="SEC30" HREF="cln_toc.html#TOC30">4.8.3 Hyperbolic functions</A></H3>
  679. <DL COMPACT>
  680. <DT><CODE>cl_R sinh (const cl_R&#38; x)</CODE>
  681. <DD>
  682. Returns <CODE>sinh(x)</CODE>.
  683. <DT><CODE>cl_N sinh (const cl_N&#38; z)</CODE>
  684. <DD>
  685. Returns <CODE>sinh(z)</CODE>. The range of the result is the entire complex plane.
  686. <DT><CODE>cl_R cosh (const cl_R&#38; x)</CODE>
  687. <DD>
  688. Returns <CODE>cosh(x)</CODE>. The range of the result is the interval
  689. <CODE>cosh(x) &#62;= 1</CODE>.
  690. <DT><CODE>cl_N cosh (const cl_N&#38; z)</CODE>
  691. <DD>
  692. Returns <CODE>cosh(z)</CODE>. The range of the result is the entire complex plane.
  693. <DT><CODE>struct cl_cosh_sinh_t { cl_R cosh; cl_R sinh; };</CODE>
  694. <DD>
  695. <DT><CODE>cl_cosh_sinh_t cl_cosh_sinh (const cl_R&#38; x)</CODE>
  696. <DD>
  697. Returns both <CODE>sinh(x)</CODE> and <CODE>cosh(x)</CODE>. This is more efficient than
  698. computing them separately. The relation <CODE>cosh^2 - sinh^2 = 1</CODE> will
  699. hold only approximately.
  700. <DT><CODE>cl_R tanh (const cl_R&#38; x)</CODE>
  701. <DD>
  702. <DT><CODE>cl_N tanh (const cl_N&#38; x)</CODE>
  703. <DD>
  704. Returns <CODE>tanh(x) = sinh(x)/cosh(x)</CODE>.
  705. <DT><CODE>cl_N asinh (const cl_N&#38; z)</CODE>
  706. <DD>
  707. Returns <CODE>arsinh(z)</CODE>. This is defined as
  708. <CODE>arsinh(z) = log(z+sqrt(1+z^2))</CODE> and satisfies
  709. <CODE>arsinh(-z) = -arsinh(z)</CODE>.
  710. The range of the result is the strip in the complex domain
  711. <CODE>-pi/2 &#60;= imagpart(arsinh(z)) &#60;= pi/2</CODE>, excluding the numbers
  712. with <CODE>imagpart = -pi/2</CODE> and <CODE>realpart &#62; 0</CODE> and the numbers
  713. with <CODE>imagpart = pi/2</CODE> and <CODE>realpart &#60; 0</CODE>.
  714. <DT><CODE>cl_N acosh (const cl_N&#38; z)</CODE>
  715. <DD>
  716. Returns <CODE>arcosh(z)</CODE>. This is defined as
  717. <CODE>arcosh(z) = 2*log(sqrt((z+1)/2)+sqrt((z-1)/2))</CODE>.
  718. The range of the result is the half-strip in the complex domain
  719. <CODE>-pi &#60; imagpart(arcosh(z)) &#60;= pi, realpart(arcosh(z)) &#62;= 0</CODE>,
  720. excluding the numbers with <CODE>realpart = 0</CODE> and <CODE>-pi &#60; imagpart &#60; 0</CODE>.
  721. <DT><CODE>cl_N atanh (const cl_N&#38; z)</CODE>
  722. <DD>
  723. Returns <CODE>artanh(z)</CODE>. This is defined as
  724. <CODE>artanh(z) = (log(1+z)-log(1-z)) / 2</CODE> and satisfies
  725. <CODE>artanh(-z) = -artanh(z)</CODE>. The range of the result is
  726. the strip in the complex domain
  727. <CODE>-pi/2 &#60;= imagpart(artanh(z)) &#60;= pi/2</CODE>, excluding the numbers
  728. with <CODE>imagpart = -pi/2</CODE> and <CODE>realpart &#60;= 0</CODE> and the numbers
  729. with <CODE>imagpart = pi/2</CODE> and <CODE>realpart &#62;= 0</CODE>.
  730. </DL>
  731. <H3><A NAME="SEC31" HREF="cln_toc.html#TOC31">4.8.4 Euler gamma</A></H3>
  732. <P>
  733. Euler's constant C = 0.577... is returned by the following functions:
  734. <DL COMPACT>
  735. <DT><CODE>cl_F cl_eulerconst (cl_float_format_t f)</CODE>
  736. <DD>
  737. Returns Euler's constant as a float of format <CODE>f</CODE>.
  738. <DT><CODE>cl_F cl_eulerconst (const cl_F&#38; y)</CODE>
  739. <DD>
  740. Returns Euler's constant in the float format of <CODE>y</CODE>.
  741. <DT><CODE>cl_F cl_eulerconst (void)</CODE>
  742. <DD>
  743. Returns Euler's constant as a float of format <CODE>cl_default_float_format</CODE>.
  744. </DL>
  745. <P>
  746. Catalan's constant G = 0.915... is returned by the following functions:
  747. <DL COMPACT>
  748. <DT><CODE>cl_F cl_catalanconst (cl_float_format_t f)</CODE>
  749. <DD>
  750. Returns Catalan's constant as a float of format <CODE>f</CODE>.
  751. <DT><CODE>cl_F cl_catalanconst (const cl_F&#38; y)</CODE>
  752. <DD>
  753. Returns Catalan's constant in the float format of <CODE>y</CODE>.
  754. <DT><CODE>cl_F cl_catalanconst (void)</CODE>
  755. <DD>
  756. Returns Catalan's constant as a float of format <CODE>cl_default_float_format</CODE>.
  757. </DL>
  758. <H3><A NAME="SEC32" HREF="cln_toc.html#TOC32">4.8.5 Riemann zeta</A></H3>
  759. <P>
  760. Riemann's zeta function at an integral point <CODE>s&#62;1</CODE> is returned by the
  761. following functions:
  762. <DL COMPACT>
  763. <DT><CODE>cl_F cl_zeta (int s, cl_float_format_t f)</CODE>
  764. <DD>
  765. Returns Riemann's zeta function at <CODE>s</CODE> as a float of format <CODE>f</CODE>.
  766. <DT><CODE>cl_F cl_zeta (int s, const cl_F&#38; y)</CODE>
  767. <DD>
  768. Returns Riemann's zeta function at <CODE>s</CODE> in the float format of <CODE>y</CODE>.
  769. <DT><CODE>cl_F cl_zeta (int s)</CODE>
  770. <DD>
  771. Returns Riemann's zeta function at <CODE>s</CODE> as a float of format
  772. <CODE>cl_default_float_format</CODE>.
  773. </DL>
  774. <H2><A NAME="SEC33" HREF="cln_toc.html#TOC33">4.9 Functions on integers</A></H2>
  775. <H3><A NAME="SEC34" HREF="cln_toc.html#TOC34">4.9.1 Logical functions</A></H3>
  776. <P>
  777. Integers, when viewed as in two's complement notation, can be thought as
  778. infinite bit strings where the bits' values eventually are constant.
  779. For example,
  780. <PRE>
  781. 17 = ......00010001
  782. -6 = ......11111010
  783. </PRE>
  784. <P>
  785. The logical operations view integers as such bit strings and operate
  786. on each of the bit positions in parallel.
  787. <DL COMPACT>
  788. <DT><CODE>cl_I lognot (const cl_I&#38; x)</CODE>
  789. <DD>
  790. <DT><CODE>cl_I operator ~ (const cl_I&#38; x)</CODE>
  791. <DD>
  792. Logical not, like <CODE>~x</CODE> in C. This is the same as <CODE>-1-x</CODE>.
  793. <DT><CODE>cl_I logand (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  794. <DD>
  795. <DT><CODE>cl_I operator &#38; (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  796. <DD>
  797. Logical and, like <CODE>x &#38; y</CODE> in C.
  798. <DT><CODE>cl_I logior (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  799. <DD>
  800. <DT><CODE>cl_I operator | (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  801. <DD>
  802. Logical (inclusive) or, like <CODE>x | y</CODE> in C.
  803. <DT><CODE>cl_I logxor (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  804. <DD>
  805. <DT><CODE>cl_I operator ^ (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  806. <DD>
  807. Exclusive or, like <CODE>x ^ y</CODE> in C.
  808. <DT><CODE>cl_I logeqv (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  809. <DD>
  810. Bitwise equivalence, like <CODE>~(x ^ y)</CODE> in C.
  811. <DT><CODE>cl_I lognand (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  812. <DD>
  813. Bitwise not and, like <CODE>~(x &#38; y)</CODE> in C.
  814. <DT><CODE>cl_I lognor (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  815. <DD>
  816. Bitwise not or, like <CODE>~(x | y)</CODE> in C.
  817. <DT><CODE>cl_I logandc1 (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  818. <DD>
  819. Logical and, complementing the first argument, like <CODE>~x &#38; y</CODE> in C.
  820. <DT><CODE>cl_I logandc2 (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  821. <DD>
  822. Logical and, complementing the second argument, like <CODE>x &#38; ~y</CODE> in C.
  823. <DT><CODE>cl_I logorc1 (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  824. <DD>
  825. Logical or, complementing the first argument, like <CODE>~x | y</CODE> in C.
  826. <DT><CODE>cl_I logorc2 (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  827. <DD>
  828. Logical or, complementing the second argument, like <CODE>x | ~y</CODE> in C.
  829. </DL>
  830. <P>
  831. These operations are all available though the function
  832. <DL COMPACT>
  833. <DT><CODE>cl_I boole (cl_boole op, const cl_I&#38; x, const cl_I&#38; y)</CODE>
  834. <DD>
  835. </DL>
  836. <P>
  837. where <CODE>op</CODE> must have one of the 16 values (each one stands for a function
  838. which combines two bits into one bit): <CODE>boole_clr</CODE>, <CODE>boole_set</CODE>,
  839. <CODE>boole_1</CODE>, <CODE>boole_2</CODE>, <CODE>boole_c1</CODE>, <CODE>boole_c2</CODE>,
  840. <CODE>boole_and</CODE>, <CODE>boole_ior</CODE>, <CODE>boole_xor</CODE>, <CODE>boole_eqv</CODE>,
  841. <CODE>boole_nand</CODE>, <CODE>boole_nor</CODE>, <CODE>boole_andc1</CODE>, <CODE>boole_andc2</CODE>,
  842. <CODE>boole_orc1</CODE>, <CODE>boole_orc2</CODE>.
  843. <P>
  844. Other functions that view integers as bit strings:
  845. <DL COMPACT>
  846. <DT><CODE>cl_boolean logtest (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  847. <DD>
  848. Returns true if some bit is set in both <CODE>x</CODE> and <CODE>y</CODE>, i.e. if
  849. <CODE>logand(x,y) != 0</CODE>.
  850. <DT><CODE>cl_boolean logbitp (const cl_I&#38; n, const cl_I&#38; x)</CODE>
  851. <DD>
  852. Returns true if the <CODE>n</CODE>th bit (from the right) of <CODE>x</CODE> is set.
  853. Bit 0 is the least significant bit.
  854. <DT><CODE>uintL logcount (const cl_I&#38; x)</CODE>
  855. <DD>
  856. Returns the number of one bits in <CODE>x</CODE>, if <CODE>x</CODE> &#62;= 0, or
  857. the number of zero bits in <CODE>x</CODE>, if <CODE>x</CODE> &#60; 0.
  858. </DL>
  859. <P>
  860. The following functions operate on intervals of bits in integers.
  861. The type
  862. <PRE>
  863. struct cl_byte { uintL size; uintL position; };
  864. </PRE>
  865. <P>
  866. represents the bit interval containing the bits
  867. <CODE>position</CODE>...<CODE>position+size-1</CODE> of an integer.
  868. The constructor <CODE>cl_byte(size,position)</CODE> constructs a <CODE>cl_byte</CODE>.
  869. <DL COMPACT>
  870. <DT><CODE>cl_I ldb (const cl_I&#38; n, const cl_byte&#38; b)</CODE>
  871. <DD>
  872. extracts the bits of <CODE>n</CODE> described by the bit interval <CODE>b</CODE>
  873. and returns them as a nonnegative integer with <CODE>b.size</CODE> bits.
  874. <DT><CODE>cl_boolean ldb_test (const cl_I&#38; n, const cl_byte&#38; b)</CODE>
  875. <DD>
  876. Returns true if some bit described by the bit interval <CODE>b</CODE> is set in
  877. <CODE>n</CODE>.
  878. <DT><CODE>cl_I dpb (const cl_I&#38; newbyte, const cl_I&#38; n, const cl_byte&#38; b)</CODE>
  879. <DD>
  880. Returns <CODE>n</CODE>, with the bits described by the bit interval <CODE>b</CODE>
  881. replaced by <CODE>newbyte</CODE>. Only the lowest <CODE>b.size</CODE> bits of
  882. <CODE>newbyte</CODE> are relevant.
  883. </DL>
  884. <P>
  885. The functions <CODE>ldb</CODE> and <CODE>dpb</CODE> implicitly shift. The following
  886. functions are their counterparts without shifting:
  887. <DL COMPACT>
  888. <DT><CODE>cl_I mask_field (const cl_I&#38; n, const cl_byte&#38; b)</CODE>
  889. <DD>
  890. returns an integer with the bits described by the bit interval <CODE>b</CODE>
  891. copied from the corresponding bits in <CODE>n</CODE>, the other bits zero.
  892. <DT><CODE>cl_I deposit_field (const cl_I&#38; newbyte, const cl_I&#38; n, const cl_byte&#38; b)</CODE>
  893. <DD>
  894. returns an integer where the bits described by the bit interval <CODE>b</CODE>
  895. come from <CODE>newbyte</CODE> and the other bits come from <CODE>n</CODE>.
  896. </DL>
  897. <P>
  898. The following relations hold:
  899. <UL>
  900. <LI>
  901. <CODE>ldb (n, b) = mask_field(n, b) &#62;&#62; b.position</CODE>,
  902. <LI>
  903. <CODE>dpb (newbyte, n, b) = deposit_field (newbyte &#60;&#60; b.position, n, b)</CODE>,
  904. <LI>
  905. <CODE>deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)</CODE>.
  906. </UL>
  907. <P>
  908. The following operations on integers as bit strings are efficient shortcuts
  909. for common arithmetic operations:
  910. <DL COMPACT>
  911. <DT><CODE>cl_boolean oddp (const cl_I&#38; x)</CODE>
  912. <DD>
  913. Returns true if the least significant bit of <CODE>x</CODE> is 1. Equivalent to
  914. <CODE>mod(x,2) != 0</CODE>.
  915. <DT><CODE>cl_boolean evenp (const cl_I&#38; x)</CODE>
  916. <DD>
  917. Returns true if the least significant bit of <CODE>x</CODE> is 0. Equivalent to
  918. <CODE>mod(x,2) == 0</CODE>.
  919. <DT><CODE>cl_I operator &#60;&#60; (const cl_I&#38; x, const cl_I&#38; n)</CODE>
  920. <DD>
  921. Shifts <CODE>x</CODE> by <CODE>n</CODE> bits to the left. <CODE>n</CODE> should be &#62;=0.
  922. Equivalent to <CODE>x * expt(2,n)</CODE>.
  923. <DT><CODE>cl_I operator &#62;&#62; (const cl_I&#38; x, const cl_I&#38; n)</CODE>
  924. <DD>
  925. Shifts <CODE>x</CODE> by <CODE>n</CODE> bits to the right. <CODE>n</CODE> should be &#62;=0.
  926. Bits shifted out to the right are thrown away.
  927. Equivalent to <CODE>floor(x / expt(2,n))</CODE>.
  928. <DT><CODE>cl_I ash (const cl_I&#38; x, const cl_I&#38; y)</CODE>
  929. <DD>
  930. Shifts <CODE>x</CODE> by <CODE>y</CODE> bits to the left (if <CODE>y</CODE>&#62;=0) or
  931. by <CODE>-y</CODE> bits to the right (if <CODE>y</CODE>&#60;=0). In other words, this
  932. returns <CODE>floor(x * expt(2,y))</CODE>.
  933. <DT><CODE>uintL integer_length (const cl_I&#38; x)</CODE>
  934. <DD>
  935. Returns the number of bits (excluding the sign bit) needed to represent <CODE>x</CODE>
  936. in two's complement notation. This is the smallest n &#62;= 0 such that
  937. -2^n &#60;= x &#60; 2^n. If x &#62; 0, this is the unique n &#62; 0 such that
  938. 2^(n-1) &#60;= x &#60; 2^n.
  939. <DT><CODE>uintL ord2 (const cl_I&#38; x)</CODE>
  940. <DD>
  941. <CODE>x</CODE> must be non-zero. This function returns the number of 0 bits at the
  942. right of <CODE>x</CODE> in two's complement notation. This is the largest n &#62;= 0
  943. such that 2^n divides <CODE>x</CODE>.
  944. <DT><CODE>uintL power2p (const cl_I&#38; x)</CODE>
  945. <DD>
  946. <CODE>x</CODE> must be &#62; 0. This function checks whether <CODE>x</CODE> is a power of 2.
  947. If <CODE>x</CODE> = 2^(n-1), it returns n. Else it returns 0.
  948. (See also the function <CODE>logp</CODE>.)
  949. </DL>
  950. <H3><A NAME="SEC35" HREF="cln_toc.html#TOC35">4.9.2 Number theoretic functions</A></H3>
  951. <DL COMPACT>
  952. <DT><CODE>uint32 gcd (uint32 a, uint32 b)</CODE>
  953. <DD>
  954. <DT><CODE>cl_I gcd (const cl_I&#38; a, const cl_I&#38; b)</CODE>
  955. <DD>
  956. This function returns the greatest common divisor of <CODE>a</CODE> and <CODE>b</CODE>,
  957. normalized to be &#62;= 0.
  958. <DT><CODE>cl_I xgcd (const cl_I&#38; a, const cl_I&#38; b, cl_I* u, cl_I* v)</CODE>
  959. <DD>
  960. This function ("extended gcd") returns the greatest common divisor <CODE>g</CODE> of
  961. <CODE>a</CODE> and <CODE>b</CODE> and at the same time the representation of <CODE>g</CODE>
  962. as an integral linear combination of <CODE>a</CODE> and <CODE>b</CODE>:
  963. <CODE>u</CODE> and <CODE>v</CODE> with <CODE>u*a+v*b = g</CODE>, <CODE>g</CODE> &#62;= 0.
  964. <CODE>u</CODE> and <CODE>v</CODE> will be normalized to be of smallest possible absolute
  965. value, in the following sense: If <CODE>a</CODE> and <CODE>b</CODE> are non-zero, and
  966. <CODE>abs(a) != abs(b)</CODE>, <CODE>u</CODE> and <CODE>v</CODE> will satisfy the inequalities
  967. <CODE>abs(u) &#60;= abs(b)/(2*g)</CODE>, <CODE>abs(v) &#60;= abs(a)/(2*g)</CODE>.
  968. <DT><CODE>cl_I lcm (const cl_I&#38; a, const cl_I&#38; b)</CODE>
  969. <DD>
  970. This function returns the least common multiple of <CODE>a</CODE> and <CODE>b</CODE>,
  971. normalized to be &#62;= 0.
  972. <DT><CODE>cl_boolean logp (const cl_I&#38; a, const cl_I&#38; b, cl_RA* l)</CODE>
  973. <DD>
  974. <DT><CODE>cl_boolean logp (const cl_RA&#38; a, const cl_RA&#38; b, cl_RA* l)</CODE>
  975. <DD>
  976. <CODE>a</CODE> must be &#62; 0. <CODE>b</CODE> must be &#62;0 and != 1. If log(a,b) is
  977. rational number, this function returns true and sets *l = log(a,b), else
  978. it returns false.
  979. </DL>
  980. <H3><A NAME="SEC36" HREF="cln_toc.html#TOC36">4.9.3 Combinatorial functions</A></H3>
  981. <DL COMPACT>
  982. <DT><CODE>cl_I factorial (uintL n)</CODE>
  983. <DD>
  984. <CODE>n</CODE> must be a small integer &#62;= 0. This function returns the factorial
  985. <CODE>n</CODE>! = <CODE>1*2*...*n</CODE>.
  986. <DT><CODE>cl_I doublefactorial (uintL n)</CODE>
  987. <DD>
  988. <CODE>n</CODE> must be a small integer &#62;= 0. This function returns the
  989. doublefactorial <CODE>n</CODE>!! = <CODE>1*3*...*n</CODE> or
  990. <CODE>n</CODE>!! = <CODE>2*4*...*n</CODE>, respectively.
  991. <DT><CODE>cl_I binomial (uintL n, uintL k)</CODE>
  992. <DD>
  993. <CODE>n</CODE> and <CODE>k</CODE> must be small integers &#62;= 0. This function returns the
  994. binomial coefficient
  995. (<CODE>n</CODE> choose <CODE>k</CODE>) = <CODE>n</CODE>! / <CODE>k</CODE>! <CODE>(n-k)</CODE>!
  996. for 0 &#60;= k &#60;= n, 0 else.
  997. </DL>
  998. <H2><A NAME="SEC37" HREF="cln_toc.html#TOC37">4.10 Functions on floating-point numbers</A></H2>
  999. <P>
  1000. Recall that a floating-point number consists of a sign <CODE>s</CODE>, an
  1001. exponent <CODE>e</CODE> and a mantissa <CODE>m</CODE>. The value of the number is
  1002. <CODE>(-1)^s * 2^e * m</CODE>.
  1003. <P>
  1004. Each of the classes
  1005. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>
  1006. defines the following operations.
  1007. <DL COMPACT>
  1008. <DT><CODE><VAR>type</VAR> scale_float (const <VAR>type</VAR>&#38; x, sintL delta)</CODE>
  1009. <DD>
  1010. <DT><CODE><VAR>type</VAR> scale_float (const <VAR>type</VAR>&#38; x, const cl_I&#38; delta)</CODE>
  1011. <DD>
  1012. Returns <CODE>x*2^delta</CODE>. This is more efficient than an explicit multiplication
  1013. because it copies <CODE>x</CODE> and modifies the exponent.
  1014. </DL>
  1015. <P>
  1016. The following functions provide an abstract interface to the underlying
  1017. representation of floating-point numbers.
  1018. <DL COMPACT>
  1019. <DT><CODE>sintL float_exponent (const <VAR>type</VAR>&#38; x)</CODE>
  1020. <DD>
  1021. Returns the exponent <CODE>e</CODE> of <CODE>x</CODE>.
  1022. For <CODE>x = 0.0</CODE>, this is 0. For <CODE>x</CODE> non-zero, this is the unique
  1023. integer with <CODE>2^(e-1) &#60;= abs(x) &#60; 2^e</CODE>.
  1024. <DT><CODE>sintL float_radix (const <VAR>type</VAR>&#38; x)</CODE>
  1025. <DD>
  1026. Returns the base of the floating-point representation. This is always <CODE>2</CODE>.
  1027. <DT><CODE><VAR>type</VAR> float_sign (const <VAR>type</VAR>&#38; x)</CODE>
  1028. <DD>
  1029. Returns the sign <CODE>s</CODE> of <CODE>x</CODE> as a float. The value is 1 for
  1030. <CODE>x</CODE> &#62;= 0, -1 for <CODE>x</CODE> &#60; 0.
  1031. <DT><CODE>uintL float_digits (const <VAR>type</VAR>&#38; x)</CODE>
  1032. <DD>
  1033. Returns the number of mantissa bits in the floating-point representation
  1034. of <CODE>x</CODE>, including the hidden bit. The value only depends on the type
  1035. of <CODE>x</CODE>, not on its value.
  1036. <DT><CODE>uintL float_precision (const <VAR>type</VAR>&#38; x)</CODE>
  1037. <DD>
  1038. Returns the number of significant mantissa bits in the floating-point
  1039. representation of <CODE>x</CODE>. Since denormalized numbers are not supported,
  1040. this is the same as <CODE>float_digits(x)</CODE> if <CODE>x</CODE> is non-zero, and
  1041. 0 if <CODE>x</CODE> = 0.
  1042. </DL>
  1043. <P>
  1044. The complete internal representation of a float is encoded in the type
  1045. <CODE>cl_decoded_float</CODE> (or <CODE>cl_decoded_sfloat</CODE>, <CODE>cl_decoded_ffloat</CODE>,
  1046. <CODE>cl_decoded_dfloat</CODE>, <CODE>cl_decoded_lfloat</CODE>, respectively), defined by
  1047. <PRE>
  1048. struct cl_decoded_<VAR>type</VAR>float {
  1049. <VAR>type</VAR> mantissa; cl_I exponent; <VAR>type</VAR> sign;
  1050. };
  1051. </PRE>
  1052. <P>
  1053. and returned by the function
  1054. <DL COMPACT>
  1055. <DT><CODE>cl_decoded_<VAR>type</VAR>float decode_float (const <VAR>type</VAR>&#38; x)</CODE>
  1056. <DD>
  1057. For <CODE>x</CODE> non-zero, this returns <CODE>(-1)^s</CODE>, <CODE>e</CODE>, <CODE>m</CODE> with
  1058. <CODE>x = (-1)^s * 2^e * m</CODE> and <CODE>0.5 &#60;= m &#60; 1.0</CODE>. For <CODE>x</CODE> = 0,
  1059. it returns <CODE>(-1)^s</CODE>=1, <CODE>e</CODE>=0, <CODE>m</CODE>=0.
  1060. <CODE>e</CODE> is the same as returned by the function <CODE>float_exponent</CODE>.
  1061. </DL>
  1062. <P>
  1063. A complete decoding in terms of integers is provided as type
  1064. <PRE>
  1065. struct cl_idecoded_float {
  1066. cl_I mantissa; cl_I exponent; cl_I sign;
  1067. };
  1068. </PRE>
  1069. <P>
  1070. by the following function:
  1071. <DL COMPACT>
  1072. <DT><CODE>cl_idecoded_float integer_decode_float (const <VAR>type</VAR>&#38; x)</CODE>
  1073. <DD>
  1074. For <CODE>x</CODE> non-zero, this returns <CODE>(-1)^s</CODE>, <CODE>e</CODE>, <CODE>m</CODE> with
  1075. <CODE>x = (-1)^s * 2^e * m</CODE> and <CODE>m</CODE> an integer with <CODE>float_digits(x)</CODE>
  1076. bits. For <CODE>x</CODE> = 0, it returns <CODE>(-1)^s</CODE>=1, <CODE>e</CODE>=0, <CODE>m</CODE>=0.
  1077. WARNING: The exponent <CODE>e</CODE> is not the same as the one returned by
  1078. the functions <CODE>decode_float</CODE> and <CODE>float_exponent</CODE>.
  1079. </DL>
  1080. <P>
  1081. Some other function, implemented only for class <CODE>cl_F</CODE>:
  1082. <DL COMPACT>
  1083. <DT><CODE>cl_F float_sign (const cl_F&#38; x, const cl_F&#38; y)</CODE>
  1084. <DD>
  1085. This returns a floating point number whose precision and absolute value
  1086. is that of <CODE>y</CODE> and whose sign is that of <CODE>x</CODE>. If <CODE>x</CODE> is
  1087. zero, it is treated as positive. Same for <CODE>y</CODE>.
  1088. </DL>
  1089. <H2><A NAME="SEC38" HREF="cln_toc.html#TOC38">4.11 Conversion functions</A></H2>
  1090. <H3><A NAME="SEC39" HREF="cln_toc.html#TOC39">4.11.1 Conversion to floating-point numbers</A></H3>
  1091. <P>
  1092. The type <CODE>cl_float_format_t</CODE> describes a floating-point format.
  1093. <DL COMPACT>
  1094. <DT><CODE>cl_float_format_t cl_float_format (uintL n)</CODE>
  1095. <DD>
  1096. Returns the smallest float format which guarantees at least <CODE>n</CODE>
  1097. decimal digits in the mantissa (after the decimal point).
  1098. <DT><CODE>cl_float_format_t cl_float_format (const cl_F&#38; x)</CODE>
  1099. <DD>
  1100. Returns the floating point format of <CODE>x</CODE>.
  1101. <DT><CODE>cl_float_format_t cl_default_float_format</CODE>
  1102. <DD>
  1103. Global variable: the default float format used when converting rational numbers
  1104. to floats.
  1105. </DL>
  1106. <P>
  1107. To convert a real number to a float, each of the types
  1108. <CODE>cl_R</CODE>, <CODE>cl_F</CODE>, <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
  1109. <CODE>int</CODE>, <CODE>unsigned int</CODE>, <CODE>float</CODE>, <CODE>double</CODE>
  1110. defines the following operations:
  1111. <DL COMPACT>
  1112. <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&#38;x, cl_float_format_t f)</CODE>
  1113. <DD>
  1114. Returns <CODE>x</CODE> as a float of format <CODE>f</CODE>.
  1115. <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&#38;x, const cl_F&#38; y)</CODE>
  1116. <DD>
  1117. Returns <CODE>x</CODE> in the float format of <CODE>y</CODE>.
  1118. <DT><CODE>cl_F cl_float (const <VAR>type</VAR>&#38;x)</CODE>
  1119. <DD>
  1120. Returns <CODE>x</CODE> as a float of format <CODE>cl_default_float_format</CODE> if
  1121. it is an exact number, or <CODE>x</CODE> itself if it is already a float.
  1122. </DL>
  1123. <P>
  1124. Of course, converting a number to a float can lose precision.
  1125. <P>
  1126. Every floating-point format has some characteristic numbers:
  1127. <DL COMPACT>
  1128. <DT><CODE>cl_F most_positive_float (cl_float_format_t f)</CODE>
  1129. <DD>
  1130. Returns the largest (most positive) floating point number in float format <CODE>f</CODE>.
  1131. <DT><CODE>cl_F most_negative_float (cl_float_format_t f)</CODE>
  1132. <DD>
  1133. Returns the smallest (most negative) floating point number in float format <CODE>f</CODE>.
  1134. <DT><CODE>cl_F least_positive_float (cl_float_format_t f)</CODE>
  1135. <DD>
  1136. Returns the least positive floating point number (i.e. &#62; 0 but closest to 0)
  1137. in float format <CODE>f</CODE>.
  1138. <DT><CODE>cl_F least_negative_float (cl_float_format_t f)</CODE>
  1139. <DD>
  1140. Returns the least negative floating point number (i.e. &#60; 0 but closest to 0)
  1141. in float format <CODE>f</CODE>.
  1142. <DT><CODE>cl_F float_epsilon (cl_float_format_t f)</CODE>
  1143. <DD>
  1144. Returns the smallest floating point number e &#62; 0 such that <CODE>1+e != 1</CODE>.
  1145. <DT><CODE>cl_F float_negative_epsilon (cl_float_format_t f)</CODE>
  1146. <DD>
  1147. Returns the smallest floating point number e &#62; 0 such that <CODE>1-e != 1</CODE>.
  1148. </DL>
  1149. <H3><A NAME="SEC40" HREF="cln_toc.html#TOC40">4.11.2 Conversion to rational numbers</A></H3>
  1150. <P>
  1151. Each of the classes <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_F</CODE>
  1152. defines the following operation:
  1153. <DL COMPACT>
  1154. <DT><CODE>cl_RA rational (const <VAR>type</VAR>&#38; x)</CODE>
  1155. <DD>
  1156. Returns the value of <CODE>x</CODE> as an exact number. If <CODE>x</CODE> is already
  1157. an exact number, this is <CODE>x</CODE>. If <CODE>x</CODE> is a floating-point number,
  1158. the value is a rational number whose denominator is a power of 2.
  1159. </DL>
  1160. <P>
  1161. In order to convert back, say, <CODE>(cl_F)(cl_R)"1/3"</CODE> to <CODE>1/3</CODE>, there is
  1162. the function
  1163. <DL COMPACT>
  1164. <DT><CODE>cl_RA rationalize (const cl_R&#38; x)</CODE>
  1165. <DD>
  1166. If <CODE>x</CODE> is a floating-point number, it actually represents an interval
  1167. of real numbers, and this function returns the rational number with
  1168. smallest denominator (and smallest numerator, in magnitude)
  1169. which lies in this interval.
  1170. If <CODE>x</CODE> is already an exact number, this function returns <CODE>x</CODE>.
  1171. </DL>
  1172. <P>
  1173. If <CODE>x</CODE> is any float, one has
  1174. <UL>
  1175. <LI>
  1176. <CODE>cl_float(rational(x),x) = x</CODE>
  1177. <LI>
  1178. <CODE>cl_float(rationalize(x),x) = x</CODE>
  1179. </UL>
  1180. <H2><A NAME="SEC41" HREF="cln_toc.html#TOC41">4.12 Random number generators</A></H2>
  1181. <P>
  1182. A random generator is a machine which produces (pseudo-)random numbers.
  1183. The include file <CODE>&#60;cl_random.h&#62;</CODE> defines a class <CODE>cl_random_state</CODE>
  1184. which contains the state of a random generator. If you make a copy
  1185. of the random number generator, the original one and the copy will produce
  1186. the same sequence of random numbers.
  1187. <P>
  1188. The following functions return (pseudo-)random numbers in different formats.
  1189. Calling one of these modifies the state of the random number generator in
  1190. a complicated but deterministic way.
  1191. <P>
  1192. The global variable
  1193. <PRE>
  1194. cl_random_state cl_default_random_state
  1195. </PRE>
  1196. <P>
  1197. contains a default random number generator. It is used when the functions
  1198. below are called without <CODE>cl_random_state</CODE> argument.
  1199. <DL COMPACT>
  1200. <DT><CODE>uint32 random32 (cl_random_state&#38; randomstate)</CODE>
  1201. <DD>
  1202. <DT><CODE>uint32 random32 ()</CODE>
  1203. <DD>
  1204. Returns a random unsigned 32-bit number. All bits are equally random.
  1205. <DT><CODE>cl_I random_I (cl_random_state&#38; randomstate, const cl_I&#38; n)</CODE>
  1206. <DD>
  1207. <DT><CODE>cl_I random_I (const cl_I&#38; n)</CODE>
  1208. <DD>
  1209. <CODE>n</CODE> must be an integer &#62; 0. This function returns a random integer <CODE>x</CODE>
  1210. in the range <CODE>0 &#60;= x &#60; n</CODE>.
  1211. <DT><CODE>cl_F random_F (cl_random_state&#38; randomstate, const cl_F&#38; n)</CODE>
  1212. <DD>
  1213. <DT><CODE>cl_F random_F (const cl_F&#38; n)</CODE>
  1214. <DD>
  1215. <CODE>n</CODE> must be a float &#62; 0. This function returns a random floating-point
  1216. number of the same format as <CODE>n</CODE> in the range <CODE>0 &#60;= x &#60; n</CODE>.
  1217. <DT><CODE>cl_R random_R (cl_random_state&#38; randomstate, const cl_R&#38; n)</CODE>
  1218. <DD>
  1219. <DT><CODE>cl_R random_R (const cl_R&#38; n)</CODE>
  1220. <DD>
  1221. Behaves like <CODE>random_I</CODE> if <CODE>n</CODE> is an integer and like <CODE>random_F</CODE>
  1222. if <CODE>n</CODE> is a float.
  1223. </DL>
  1224. <H2><A NAME="SEC42" HREF="cln_toc.html#TOC42">4.13 Obfuscating operators</A></H2>
  1225. <P>
  1226. The modifying C/C++ operators <CODE>+=</CODE>, <CODE>-=</CODE>, <CODE>*=</CODE>, <CODE>/=</CODE>,
  1227. <CODE>&#38;=</CODE>, <CODE>|=</CODE>, <CODE>^=</CODE>, <CODE>&#60;&#60;=</CODE>, <CODE>&#62;&#62;=</CODE>
  1228. are not available by default because their
  1229. use tends to make programs unreadable. It is trivial to get away without
  1230. them. However, if you feel that you absolutely need these operators
  1231. to get happy, then add
  1232. <PRE>
  1233. #define WANT_OBFUSCATING_OPERATORS
  1234. </PRE>
  1235. <P>
  1236. to the beginning of your source files, before the inclusion of any CLN
  1237. include files. This flag will enable the following operators:
  1238. <P>
  1239. For the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>,
  1240. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>:
  1241. <DL COMPACT>
  1242. <DT><CODE><VAR>type</VAR>&#38; operator += (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1243. <DD>
  1244. <DT><CODE><VAR>type</VAR>&#38; operator -= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1245. <DD>
  1246. <DT><CODE><VAR>type</VAR>&#38; operator *= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1247. <DD>
  1248. <DT><CODE><VAR>type</VAR>&#38; operator /= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1249. <DD>
  1250. </DL>
  1251. <P>
  1252. For the class <CODE>cl_I</CODE>:
  1253. <DL COMPACT>
  1254. <DT><CODE><VAR>type</VAR>&#38; operator += (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1255. <DD>
  1256. <DT><CODE><VAR>type</VAR>&#38; operator -= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1257. <DD>
  1258. <DT><CODE><VAR>type</VAR>&#38; operator *= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1259. <DD>
  1260. <DT><CODE><VAR>type</VAR>&#38; operator &#38;= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1261. <DD>
  1262. <DT><CODE><VAR>type</VAR>&#38; operator |= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1263. <DD>
  1264. <DT><CODE><VAR>type</VAR>&#38; operator ^= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1265. <DD>
  1266. <DT><CODE><VAR>type</VAR>&#38; operator &#60;&#60;= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1267. <DD>
  1268. <DT><CODE><VAR>type</VAR>&#38; operator &#62;&#62;= (<VAR>type</VAR>&#38;, const <VAR>type</VAR>&#38;)</CODE>
  1269. <DD>
  1270. </DL>
  1271. <P>
  1272. For the classes <CODE>cl_N</CODE>, <CODE>cl_R</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_I</CODE>,
  1273. <CODE>cl_F</CODE>, <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>:
  1274. <DL COMPACT>
  1275. <DT><CODE><VAR>type</VAR>&#38; operator ++ (<VAR>type</VAR>&#38; x)</CODE>
  1276. <DD>
  1277. The prefix operator <CODE>++x</CODE>.
  1278. <DT><CODE>void operator ++ (<VAR>type</VAR>&#38; x, int)</CODE>
  1279. <DD>
  1280. The postfix operator <CODE>x++</CODE>.
  1281. <DT><CODE><VAR>type</VAR>&#38; operator -- (<VAR>type</VAR>&#38; x)</CODE>
  1282. <DD>
  1283. The prefix operator <CODE>--x</CODE>.
  1284. <DT><CODE>void operator -- (<VAR>type</VAR>&#38; x, int)</CODE>
  1285. <DD>
  1286. The postfix operator <CODE>x--</CODE>.
  1287. </DL>
  1288. <P>
  1289. Note that by using these obfuscating operators, you wouldn't gain efficiency:
  1290. In CLN <SAMP>`x += y;'</SAMP> is exactly the same as <SAMP>`x = x+y;'</SAMP>, not more
  1291. efficient.
  1292. <P><HR><P>
  1293. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_3.html">previous</A>, <A HREF="cln_5.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  1294. </BODY>
  1295. </HTML>