Numerical-types.xhtml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Numerical types</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Numbers.xhtml" title="Quantities and Numbers"/>
  10. <link rel="next" href="Arithmetic-operations.xhtml" title="Arithmetic operations"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Numerical types" epub:type="subchapter" id="Numerical-types">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Numerical types</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Mathematically, numbers are arranged into a tower of subtypes
  23. in which each level is a subset of the level before it:
  24. number; complex number; real number; rational number; integer.
  25. </p>
  26. <p>For example, <code class="literal">3</code> is an integer. Therefore <code class="literal">3</code> is also a rational,
  27. a real, and a complex number. The same is true of the
  28. Scheme numbers that model 3. For Scheme numbers, these
  29. types are defined by the predicates <code class="literal">number?</code>, <code class="literal">complex?</code>,
  30. <code class="literal">real?</code>, <code class="literal">rational?</code>, and <code class="literal">integer?</code>.
  31. </p>
  32. <p>There is no simple relationship between a number’s type
  33. and its representation inside a computer. Although most
  34. implementations of Scheme will offer at least two different
  35. representations of 3, these different representations denote
  36. the same integer.
  37. </p>
  38. <p>Scheme’s numerical operations treat numbers as abstract
  39. data, as independent of their representation as possible.
  40. Although an implementation of Scheme may use multiple
  41. internal representations of numbers, this ought not to be
  42. apparent to a casual programmer writing simple programs.
  43. </p>
  44. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877211856" class="indexterm"/> <code class="function">number</code></p>
  45. <div class="blockquote">
  46. <blockquote class="blockquote">
  47. <p>The type of Scheme numbers.
  48. </p>
  49. </blockquote>
  50. </div>
  51. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877208880" class="indexterm"/> <code class="function">quantity</code></p>
  52. <div class="blockquote">
  53. <blockquote class="blockquote">
  54. <p>The type of quantities optionally with units.
  55. This is a sub-type of <code class="literal">number</code>.
  56. </p>
  57. </blockquote>
  58. </div>
  59. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877205456" class="indexterm"/> <code class="function">complex</code></p>
  60. <div class="blockquote">
  61. <blockquote class="blockquote">
  62. <p>The type of complex numbers.
  63. This is a sub-type of <code class="literal">quantity</code>.
  64. </p>
  65. </blockquote>
  66. </div>
  67. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877202048" class="indexterm"/> <code class="function">real</code></p>
  68. <div class="blockquote">
  69. <blockquote class="blockquote">
  70. <p>The type of real numbers.
  71. This is a sub-type of <code class="literal">complex</code>.
  72. </p>
  73. </blockquote>
  74. </div>
  75. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877198640" class="indexterm"/> <code class="function">rational</code></p>
  76. <div class="blockquote">
  77. <blockquote class="blockquote">
  78. <p>The type of exact rational numbers.
  79. This is a sub-type of <code class="literal">real</code>.
  80. </p>
  81. </blockquote>
  82. </div>
  83. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877195216" class="indexterm"/> <code class="function">integer</code></p>
  84. <div class="blockquote">
  85. <blockquote class="blockquote">
  86. <p>The type of exact Scheme integers.
  87. This is a sub-type of <code class="literal">rational</code>.
  88. </p>
  89. </blockquote>
  90. </div>
  91. <p>Kawa allows working with expressions of “primitive” types,
  92. which are supported by the JVM without object allocation,
  93. and using builtin arithmetic. Using these types may be much
  94. faster, assuming the compiler is able to infer
  95. that the variable or expression has primitive type.
  96. </p>
  97. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877190768" class="indexterm"/> <code class="function">long</code></p>
  98. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877188352" class="indexterm"/> <code class="function">int</code></p>
  99. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877186000" class="indexterm"/> <code class="function">short</code></p>
  100. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877183584" class="indexterm"/> <code class="function">byte</code></p>
  101. <div class="blockquote">
  102. <blockquote class="blockquote">
  103. <p>These are fixed-sized primitive signed exact integer types,
  104. of respectively 64, 32, 18, and 8 bits.
  105. If a value of one of these types needs to be converted to an
  106. object, the standard classes <code class="literal">java.lang.Long</code>, <code class="literal">java.lang.Integer</code>,
  107. <code class="literal">java.lang.Short</code>, or <code class="literal">java.lang.Byte</code>, respectively, are used.
  108. </p>
  109. </blockquote>
  110. </div>
  111. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877178704" class="indexterm"/> <code class="function">ulong</code></p>
  112. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877176288" class="indexterm"/> <code class="function">uint</code></p>
  113. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877173872" class="indexterm"/> <code class="function">ushort</code></p>
  114. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877171456" class="indexterm"/> <code class="function">ubyte</code></p>
  115. <div class="blockquote">
  116. <blockquote class="blockquote">
  117. <p>These are fixed-sized primitive unsigned exact integer types,
  118. of respectively 64, 32, 18, and 8 bits.
  119. These are presented at runtime using the corresponding
  120. signed types (<code class="literal">long</code>, <code class="literal">int</code>, <code class="literal">short</code>, or <code class="literal">byte</code>).
  121. However, for arithmetic the Kawa compiler generates code to perform the
  122. “mathematically correct” result, truncated to an unsigned result
  123. rather than signed.
  124. If a value of one of these types needs to be converted to an
  125. object, the classes <code class="literal">gnu.math.ULong</code>, <code class="literal">gnu.math.UInt</code>,
  126. <code class="literal">gnu.math.UShort</code>, or <code class="literal">gnu.math.UByte</code> is used.
  127. </p>
  128. </blockquote>
  129. </div>
  130. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877164336" class="indexterm"/> <code class="function">double</code></p>
  131. <p class="synopsis" kind="Type"><span class="kind">Type</span><span class="ignore">: </span><a id="idm139667877161920" class="indexterm"/> <code class="function">float</code></p>
  132. <div class="blockquote">
  133. <blockquote class="blockquote">
  134. <p>These are fixed-size primitive inexact floating-point real types,
  135. using the standard 64-bit or 32-bit IEEE representation.
  136. If a value of one of these types needs to be converted to an
  137. object, the standard classes <code class="literal">java.lang.Double</code>,
  138. or <code class="literal">java.lang.Float</code> is used.
  139. </p>
  140. </blockquote>
  141. </div>
  142. <section class="sect2" title="Exactness" epub:type="division" id="idm139667877158000">
  143. <div class="titlepage">
  144. <div>
  145. <div>
  146. <h3 class="title">Exactness</h3>
  147. </div>
  148. </div>
  149. </div>
  150. <a id="idm139667877157056" class="indexterm"/>
  151. <p>It is useful to distinguish between numbers that are represented
  152. exactly and those that might not be. For example,
  153. indexes into data structures must be known exactly, as
  154. must some polynomial coefficients in a symbolic algebra
  155. system. On the other hand, the results of measurements
  156. are inherently inexact, and irrational numbers may be approximated
  157. by rational and therefore inexact approximations.
  158. In order to catch uses of inexact numbers where exact numbers
  159. are required, Scheme explicitly distinguishes
  160. exact from inexact numbers. This distinction is orthogonal
  161. to the dimension of type.
  162. </p>
  163. <a id="idm139667877155280" class="indexterm"/>
  164. <a id="idm139667877154496" class="indexterm"/>
  165. <p>A Scheme number is <em class="firstterm">exact</em> if it was written as an exact
  166. constant or was derived from exact numbers using only exact operations.
  167. A number is <em class="firstterm">inexact</em> if it was written as
  168. an inexact constant, if it was derived using inexact ingredients,
  169. or if it was derived using inexact operations. Thus
  170. inexactness is a contagious property of a number.
  171. In particular, an <em class="firstterm">exact complex number</em> has an exact real part
  172. and an exact imaginary part; all other complex numbers
  173. are <em class="firstterm">inexact complex numbers</em>.
  174. </p>
  175. <p>If two implementations produce exact results for a computation
  176. that did not involve inexact intermediate results, the
  177. two ultimate results will be mathematically equal. This
  178. is generally not true of computations involving inexact
  179. numbers since approximate methods such as floating-point
  180. arithmetic may be used, but it is the duty of the implementation
  181. to make the result as close as practical to the
  182. mathematically ideal result.
  183. </p>
  184. <p>Rational operations such as <code class="literal">+</code> should always produce exact
  185. results when given exact arguments. If the operation
  186. is unable to produce an exact result, then it may either
  187. report the violation of an implementation restriction or it
  188. may silently coerce its result to an inexact value.
  189. </p>
  190. <p>Except for <code class="literal">exact</code>, the operations described in this section
  191. must generally return inexact results when given any inexact arguments.
  192. An operation may, however, return an
  193. exact result if it can prove that the value of the result is
  194. unaffected by the inexactness of its arguments. For example,
  195. multiplication of any number by an exact zero may
  196. produce an exact zero result, even if the other argument is inexact.
  197. </p>
  198. <p>Specifically, the expression <code class="literal">(* 0 +inf.0)</code> may return <code class="literal">0</code>,
  199. or <code class="literal">+nan.0</code>, or report that inexact numbers are not supported,
  200. or report that non-rational real numbers are not supported,
  201. or fail silently or noisily in other implementation-specific ways.
  202. </p>
  203. <p>The procedures listed below will always return exact integer
  204. results provided all their arguments are exact integers
  205. and the mathematically expected results are representable
  206. as exact integers within the implementation:
  207. <code class="literal">-</code>,
  208. <code class="literal">*</code>,
  209. <code class="literal">+</code>,
  210. <code class="literal">abs</code>,
  211. <code class="literal">ceiling</code>,
  212. <code class="literal">denominator</code>,
  213. <code class="literal">exact-integer-sqrt</code>,
  214. <code class="literal">expt</code>,
  215. <code class="literal">floor</code>,
  216. <code class="literal">floor/</code>,
  217. <code class="literal">floor-quotient</code>,
  218. <code class="literal">floor-remainder</code>,
  219. <code class="literal">gcd</code>,
  220. <code class="literal">lcm</code>,
  221. <code class="literal">max</code>,
  222. <code class="literal">min</code>,
  223. <code class="literal">modulo</code>,
  224. <code class="literal">numerator</code>,
  225. <code class="literal">quotient</code>,
  226. <code class="literal">rationalize</code>,
  227. <code class="literal">remainder</code>,
  228. <code class="literal">square</code>,
  229. <code class="literal">truncate</code>,
  230. <code class="literal">truncate/</code>,
  231. <code class="literal">truncate-quotient</code>,
  232. <code class="literal">truncate-remainder</code>.
  233. </p>
  234. </section>
  235. <section class="sect2" title="Numerical promotion and conversion" epub:type="division" id="idm139667877134800">
  236. <div class="titlepage">
  237. <div>
  238. <div>
  239. <h3 class="title">Numerical promotion and conversion</h3>
  240. </div>
  241. </div>
  242. </div>
  243. <p>When combining two values of different numeric types,
  244. the values are converted to the first line in the following
  245. that subsumes (follows) both types. The computation is done using
  246. values of that type, and so is the result.
  247. For example adding a <code class="literal">long</code> and a <code class="literal">float</code> converts the former
  248. to the latter, yielding a <code class="literal">float</code>.
  249. </p>
  250. <p>Note that <code class="literal">short</code>, <code class="literal">byte</code>, <code class="literal">ushort</code>, <code class="literal">ubyte</code>
  251. are converted to <code class="literal">int</code> regardless, even in the case of
  252. a single-operand operation, such as unary negation.
  253. Another exception is trancendental functions (such as <code class="literal">cos</code>),
  254. where integer operands are converted to <code class="literal">double</code>.
  255. </p>
  256. <div class="itemizedlist" epub:type="list">
  257. <ul class="itemizedlist" style="list-style-type: disc; ">
  258. <li class="listitem" epub:type="list-item">
  259. <p><code class="literal">int</code> subsumes <code class="literal">short</code>, <code class="literal">byte</code>, <code class="literal">ushort</code>, <code class="literal">ubyte</code>.
  260. </p>
  261. </li>
  262. <li class="listitem" epub:type="list-item">
  263. <p><code class="literal">uint</code>
  264. </p>
  265. </li>
  266. <li class="listitem" epub:type="list-item">
  267. <p><code class="literal">long</code>
  268. </p>
  269. </li>
  270. <li class="listitem" epub:type="list-item">
  271. <p><code class="literal">ulong</code>
  272. </p>
  273. </li>
  274. <li class="listitem" epub:type="list-item">
  275. <p><code class="literal">java.lang.BigInteger</code>
  276. </p>
  277. </li>
  278. <li class="listitem" epub:type="list-item">
  279. <p><code class="literal">integer</code> (i.e. <code class="literal">gnu.math.IntNum</code>)
  280. </p>
  281. </li>
  282. <li class="listitem" epub:type="list-item">
  283. <p><code class="literal">rational</code> (i.e. <code class="literal">gnu.math.RatNum</code>)
  284. </p>
  285. </li>
  286. <li class="listitem" epub:type="list-item">
  287. <p><code class="literal">float</code>
  288. </p>
  289. </li>
  290. <li class="listitem" epub:type="list-item">
  291. <p><code class="literal">double</code>
  292. </p>
  293. </li>
  294. <li class="listitem" epub:type="list-item">
  295. <p><code class="literal">gnu.math.FloNum</code>
  296. </p>
  297. </li>
  298. <li class="listitem" epub:type="list-item">
  299. <p><code class="literal">real</code> (i.e. <code class="literal">gnu.math.RealNum</code>)
  300. </p>
  301. </li>
  302. <li class="listitem" epub:type="list-item">
  303. <p><code class="literal">number</code>
  304. </p>
  305. </li>
  306. <li class="listitem" epub:type="list-item">
  307. <p><code class="literal">complex</code>
  308. </p>
  309. </li>
  310. <li class="listitem" epub:type="list-item">
  311. <p><code class="literal">quantity</code>
  312. </p>
  313. </li>
  314. </ul>
  315. </div>
  316. <p>When comparing a primitive signed integer value with a primitive unsigned
  317. integer (for example <code class="literal">&lt;</code> applied to a <code class="literal">int</code> and a <code class="literal">ulong</code>)
  318. the mathemically correct result is computed, as it converting both
  319. operands to <code class="literal">integer</code>.
  320. </p>
  321. </section>
  322. </section>
  323. <footer>
  324. <div class="navfooter">
  325. <ul>
  326. <li>
  327. <b class="toc">
  328. <a href="Numerical-types.xhtml#idm139667877158000">Exactness</a>
  329. </b>
  330. </li>
  331. <li>
  332. <b class="toc">
  333. <a href="Numerical-types.xhtml#idm139667877134800">Numerical promotion and conversion</a>
  334. </b>
  335. </li>
  336. </ul>
  337. <p>
  338. Up: <a accesskey="u" href="Numbers.xhtml">Quantities and Numbers</a></p>
  339. <p>
  340. Next: <a accesskey="n" href="Arithmetic-operations.xhtml">Arithmetic operations</a></p>
  341. </div>
  342. </footer>
  343. </body>
  344. </html>