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.

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