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.

401 lines
13 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
  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 - 3. Ordinary number types</TITLE>
  5. </HEAD>
  6. <BODY>
  7. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_2.html">previous</A>, <A HREF="cln_4.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="SEC11" HREF="cln_toc.html#TOC11">3. Ordinary number types</A></H1>
  10. <P>
  11. CLN implements the following class hierarchy:
  12. <PRE>
  13. Number
  14. cl_number
  15. &#60;cl_number.h&#62;
  16. |
  17. |
  18. Real or complex number
  19. cl_N
  20. &#60;cl_complex.h&#62;
  21. |
  22. |
  23. Real number
  24. cl_R
  25. &#60;cl_real.h&#62;
  26. |
  27. +-------------------+-------------------+
  28. | |
  29. Rational number Floating-point number
  30. cl_RA cl_F
  31. &#60;cl_rational.h&#62; &#60;cl_float.h&#62;
  32. | |
  33. | +-------------+-------------+-------------+
  34. Integer | | | |
  35. cl_I Short-Float Single-Float Double-Float Long-Float
  36. &#60;cl_integer.h&#62; cl_SF cl_FF cl_DF cl_LF
  37. &#60;cl_sfloat.h&#62; &#60;cl_ffloat.h&#62; &#60;cl_dfloat.h&#62; &#60;cl_lfloat.h&#62;
  38. </PRE>
  39. <P>
  40. <A NAME="IDX7"></A>
  41. <A NAME="IDX8"></A>
  42. The base class <CODE>cl_number</CODE> is an abstract base class.
  43. It is not useful to declare a variable of this type except if you want
  44. to completely disable compile-time type checking and use run-time type
  45. checking instead.
  46. <P>
  47. <A NAME="IDX9"></A>
  48. <A NAME="IDX10"></A>
  49. <A NAME="IDX11"></A>
  50. The class <CODE>cl_N</CODE> comprises real and complex numbers. There is
  51. no special class for complex numbers since complex numbers with imaginary
  52. part <CODE>0</CODE> are automatically converted to real numbers.
  53. <P>
  54. <A NAME="IDX12"></A>
  55. The class <CODE>cl_R</CODE> comprises real numbers of different kinds. It is an
  56. abstract class.
  57. <P>
  58. <A NAME="IDX13"></A>
  59. <A NAME="IDX14"></A>
  60. <A NAME="IDX15"></A>
  61. The class <CODE>cl_RA</CODE> comprises exact real numbers: rational numbers, including
  62. integers. There is no special class for non-integral rational numbers
  63. since rational numbers with denominator <CODE>1</CODE> are automatically converted
  64. to integers.
  65. <P>
  66. <A NAME="IDX16"></A>
  67. The class <CODE>cl_F</CODE> implements floating-point approximations to real numbers.
  68. It is an abstract class.
  69. <H2><A NAME="SEC12" HREF="cln_toc.html#TOC12">3.1 Exact numbers</A></H2>
  70. <P>
  71. <A NAME="IDX17"></A>
  72. <P>
  73. Some numbers are represented as exact numbers: there is no loss of information
  74. when such a number is converted from its mathematical value to its internal
  75. representation. On exact numbers, the elementary operations (<CODE>+</CODE>,
  76. <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, comparisons, ...) compute the completely
  77. correct result.
  78. <P>
  79. In CLN, the exact numbers are:
  80. <UL>
  81. <LI>
  82. rational numbers (including integers),
  83. <LI>
  84. complex numbers whose real and imaginary parts are both rational numbers.
  85. </UL>
  86. <P>
  87. Rational numbers are always normalized to the form
  88. <CODE><VAR>numerator</VAR>/<VAR>denominator</VAR></CODE> where the numerator and denominator
  89. are coprime integers and the denominator is positive. If the resulting
  90. denominator is <CODE>1</CODE>, the rational number is converted to an integer.
  91. <P>
  92. Small integers (typically in the range <CODE>-2^30</CODE>...<CODE>2^30-1</CODE>,
  93. for 32-bit machines) are especially efficient, because they consume no heap
  94. allocation. Otherwise the distinction between these immediate integers
  95. (called "fixnums") and heap allocated integers (called "bignums")
  96. is completely transparent.
  97. <H2><A NAME="SEC13" HREF="cln_toc.html#TOC13">3.2 Floating-point numbers</A></H2>
  98. <P>
  99. <A NAME="IDX18"></A>
  100. <P>
  101. Not all real numbers can be represented exactly. (There is an easy mathematical
  102. proof for this: Only a countable set of numbers can be stored exactly in
  103. a computer, even if one assumes that it has unlimited storage. But there
  104. are uncountably many real numbers.) So some approximation is needed.
  105. CLN implements ordinary floating-point numbers, with mantissa and exponent.
  106. <P>
  107. <A NAME="IDX19"></A>
  108. The elementary operations (<CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, ...)
  109. only return approximate results. For example, the value of the expression
  110. <CODE>(cl_F) 0.3 + (cl_F) 0.4</CODE> prints as <SAMP>`0.70000005'</SAMP>, not as
  111. <SAMP>`0.7'</SAMP>. Rounding errors like this one are inevitable when computing
  112. with floating-point numbers.
  113. <P>
  114. Nevertheless, CLN rounds the floating-point results of the operations <CODE>+</CODE>,
  115. <CODE>-</CODE>, <CODE>*</CODE>, <CODE>/</CODE>, <CODE>sqrt</CODE> according to the "round-to-even"
  116. rule: It first computes the exact mathematical result and then returns the
  117. floating-point number which is nearest to this. If two floating-point numbers
  118. are equally distant from the ideal result, the one with a <CODE>0</CODE> in its least
  119. significant mantissa bit is chosen.
  120. <P>
  121. Similarly, testing floating point numbers for equality <SAMP>`x == y'</SAMP>
  122. is gambling with random errors. Better check for <SAMP>`abs(x - y) &#60; epsilon'</SAMP>
  123. for some well-chosen <CODE>epsilon</CODE>.
  124. <P>
  125. Floating point numbers come in four flavors:
  126. <UL>
  127. <LI>
  128. <A NAME="IDX20"></A>
  129. Short floats, type <CODE>cl_SF</CODE>.
  130. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  131. and 17 mantissa bits (including the "hidden" bit).
  132. They don't consume heap allocation.
  133. <LI>
  134. <A NAME="IDX21"></A>
  135. Single floats, type <CODE>cl_FF</CODE>.
  136. They have 1 sign bit, 8 exponent bits (including the exponent's sign),
  137. and 24 mantissa bits (including the "hidden" bit).
  138. In CLN, they are represented as IEEE single-precision floating point numbers.
  139. This corresponds closely to the C/C++ type <SAMP>`float'</SAMP>.
  140. <LI>
  141. <A NAME="IDX22"></A>
  142. Double floats, type <CODE>cl_DF</CODE>.
  143. They have 1 sign bit, 11 exponent bits (including the exponent's sign),
  144. and 53 mantissa bits (including the "hidden" bit).
  145. In CLN, they are represented as IEEE double-precision floating point numbers.
  146. This corresponds closely to the C/C++ type <SAMP>`double'</SAMP>.
  147. <LI>
  148. <A NAME="IDX23"></A>
  149. Long floats, type <CODE>cl_LF</CODE>.
  150. They have 1 sign bit, 32 exponent bits (including the exponent's sign),
  151. and n mantissa bits (including the "hidden" bit), where n &#62;= 64.
  152. The precision of a long float is unlimited, but once created, a long float
  153. has a fixed precision. (No "lazy recomputation".)
  154. </UL>
  155. <P>
  156. Of course, computations with long floats are more expensive than those
  157. with smaller floating-point formats.
  158. <P>
  159. CLN does not implement features like NaNs, denormalized numbers and
  160. gradual underflow. If the exponent range of some floating-point type
  161. is too limited for your application, choose another floating-point type
  162. with larger exponent range.
  163. <P>
  164. <A NAME="IDX24"></A>
  165. As a user of CLN, you can forget about the differences between the
  166. four floating-point types and just declare all your floating-point
  167. variables as being of type <CODE>cl_F</CODE>. This has the advantage that
  168. when you change the precision of some computation (say, from <CODE>cl_DF</CODE>
  169. to <CODE>cl_LF</CODE>), you don't have to change the code, only the precision
  170. of the initial values. Also, many transcendental functions have been
  171. declared as returning a <CODE>cl_F</CODE> when the argument is a <CODE>cl_F</CODE>,
  172. but such declarations are missing for the types <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>,
  173. <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>. (Such declarations would be wrong if
  174. the floating point contagion rule happened to change in the future.)
  175. <H2><A NAME="SEC14" HREF="cln_toc.html#TOC14">3.3 Complex numbers</A></H2>
  176. <P>
  177. <A NAME="IDX25"></A>
  178. <P>
  179. Complex numbers, as implemented by the class <CODE>cl_N</CODE>, have a real
  180. part and an imaginary part, both real numbers. A complex number whose
  181. imaginary part is the exact number <CODE>0</CODE> is automatically converted
  182. to a real number.
  183. <P>
  184. Complex numbers can arise from real numbers alone, for example
  185. through application of <CODE>sqrt</CODE> or transcendental functions.
  186. <H2><A NAME="SEC15" HREF="cln_toc.html#TOC15">3.4 Conversions</A></H2>
  187. <P>
  188. <A NAME="IDX26"></A>
  189. <P>
  190. Conversions from any class to any its superclasses ("base classes" in
  191. C++ terminology) is done automatically.
  192. <P>
  193. Conversions from the C built-in types <SAMP>`long'</SAMP> and <SAMP>`unsigned long'</SAMP>
  194. are provided for the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_R</CODE>,
  195. <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
  196. <P>
  197. Conversions from the C built-in types <SAMP>`int'</SAMP> and <SAMP>`unsigned int'</SAMP>
  198. are provided for the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>, <CODE>cl_R</CODE>,
  199. <CODE>cl_N</CODE> and <CODE>cl_number</CODE>. However, these conversions emphasize
  200. efficiency. Their range is therefore limited:
  201. <UL>
  202. <LI>
  203. The conversion from <SAMP>`int'</SAMP> works only if the argument is &#60; 2^29 and &#62; -2^29.
  204. <LI>
  205. The conversion from <SAMP>`unsigned int'</SAMP> works only if the argument is &#60; 2^29.
  206. </UL>
  207. <P>
  208. In a declaration like <SAMP>`cl_I x = 10;'</SAMP> the C++ compiler is able to
  209. do the conversion of <CODE>10</CODE> from <SAMP>`int'</SAMP> to <SAMP>`cl_I'</SAMP> at compile time
  210. already. On the other hand, code like <SAMP>`cl_I x = 1000000000;'</SAMP> is
  211. in error.
  212. So, if you want to be sure that an <SAMP>`int'</SAMP> whose magnitude is not guaranteed
  213. to be &#60; 2^29 is correctly converted to a <SAMP>`cl_I'</SAMP>, first convert it to a
  214. <SAMP>`long'</SAMP>. Similarly, if a large <SAMP>`unsigned int'</SAMP> is to be converted to a
  215. <SAMP>`cl_I'</SAMP>, first convert it to an <SAMP>`unsigned long'</SAMP>.
  216. <P>
  217. Conversions from the C built-in type <SAMP>`float'</SAMP> are provided for the classes
  218. <CODE>cl_FF</CODE>, <CODE>cl_F</CODE>, <CODE>cl_R</CODE>, <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
  219. <P>
  220. Conversions from the C built-in type <SAMP>`double'</SAMP> are provided for the classes
  221. <CODE>cl_DF</CODE>, <CODE>cl_F</CODE>, <CODE>cl_R</CODE>, <CODE>cl_N</CODE> and <CODE>cl_number</CODE>.
  222. <P>
  223. Conversions from <SAMP>`const char *'</SAMP> are provided for the classes
  224. <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
  225. <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>, <CODE>cl_F</CODE>,
  226. <CODE>cl_R</CODE>, <CODE>cl_N</CODE>.
  227. The easiest way to specify a value which is outside of the range of the
  228. C++ built-in types is therefore to specify it as a string, like this:
  229. <A NAME="IDX27"></A>
  230. <PRE>
  231. cl_I order_of_rubiks_cube_group = "43252003274489856000";
  232. </PRE>
  233. <P>
  234. Note that this conversion is done at runtime, not at compile-time.
  235. <P>
  236. Conversions from <CODE>cl_I</CODE> to the C built-in types <SAMP>`int'</SAMP>,
  237. <SAMP>`unsigned int'</SAMP>, <SAMP>`long'</SAMP>, <SAMP>`unsigned long'</SAMP> are provided through
  238. the functions
  239. <DL COMPACT>
  240. <DT><CODE>int cl_I_to_int (const cl_I&#38; x)</CODE>
  241. <DD>
  242. <A NAME="IDX28"></A>
  243. <DT><CODE>unsigned int cl_I_to_uint (const cl_I&#38; x)</CODE>
  244. <DD>
  245. <A NAME="IDX29"></A>
  246. <DT><CODE>long cl_I_to_long (const cl_I&#38; x)</CODE>
  247. <DD>
  248. <A NAME="IDX30"></A>
  249. <DT><CODE>unsigned long cl_I_to_ulong (const cl_I&#38; x)</CODE>
  250. <DD>
  251. <A NAME="IDX31"></A>
  252. Returns <CODE>x</CODE> as element of the C type <VAR>ctype</VAR>. If <CODE>x</CODE> is not
  253. representable in the range of <VAR>ctype</VAR>, a runtime error occurs.
  254. </DL>
  255. <P>
  256. Conversions from the classes <CODE>cl_I</CODE>, <CODE>cl_RA</CODE>,
  257. <CODE>cl_SF</CODE>, <CODE>cl_FF</CODE>, <CODE>cl_DF</CODE>, <CODE>cl_LF</CODE>, <CODE>cl_F</CODE> and
  258. <CODE>cl_R</CODE>
  259. to the C built-in types <SAMP>`float'</SAMP> and <SAMP>`double'</SAMP> are provided through
  260. the functions
  261. <DL COMPACT>
  262. <DT><CODE>float cl_float_approx (const <VAR>type</VAR>&#38; x)</CODE>
  263. <DD>
  264. <A NAME="IDX32"></A>
  265. <DT><CODE>double cl_double_approx (const <VAR>type</VAR>&#38; x)</CODE>
  266. <DD>
  267. <A NAME="IDX33"></A>
  268. Returns an approximation of <CODE>x</CODE> of C type <VAR>ctype</VAR>.
  269. If <CODE>abs(x)</CODE> is too close to 0 (underflow), 0 is returned.
  270. If <CODE>abs(x)</CODE> is too large (overflow), an IEEE infinity is returned.
  271. </DL>
  272. <P>
  273. Conversions from any class to any of its subclasses ("derived classes" in
  274. C++ terminology) are not provided. Instead, you can assert and check
  275. that a value belongs to a certain subclass, and return it as element of that
  276. class, using the <SAMP>`As'</SAMP> and <SAMP>`The'</SAMP> macros.
  277. <A NAME="IDX34"></A>
  278. <CODE>As(<VAR>type</VAR>)(<VAR>value</VAR>)</CODE> checks that <VAR>value</VAR> belongs to
  279. <VAR>type</VAR> and returns it as such.
  280. <A NAME="IDX35"></A>
  281. <CODE>The(<VAR>type</VAR>)(<VAR>value</VAR>)</CODE> assumes that <VAR>value</VAR> belongs to
  282. <VAR>type</VAR> and returns it as such. It is your responsibility to ensure
  283. that this assumption is valid.
  284. Example:
  285. <PRE>
  286. cl_I x = ...;
  287. if (!(x &#62;= 0)) abort();
  288. cl_I ten_x = The(cl_I)(expt(10,x)); // If x &#62;= 0, 10^x is an integer.
  289. // In general, it would be a rational number.
  290. </PRE>
  291. <P><HR><P>
  292. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_2.html">previous</A>, <A HREF="cln_4.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  293. </BODY>
  294. </HTML>