tutorial-Numbers.xhtml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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>Numbers</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="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="tutorial-Functions.xhtml" title="Functions"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Numbers" epub:type="subchapter" id="Tutorial---Numbers">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Numbers</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <h3 id="idm139667880611536">Exact integers and fractions</h3>
  23. <p>Kawa has the usual syntax for decimal integers.
  24. Addition, subtraction, and multiplication
  25. are written using the usual <code class="literal">+</code>,
  26. <code class="literal">-</code>, and <code class="literal">*</code>,
  27. but these are all prefix functions that take a variable number of arguments:
  28. </p>
  29. <pre class="screen">(+ 1 2 3) ⇒ 6
  30. (- 10 3 4) ⇒ (- (- 10 3) 4) ⇒ 3
  31. (* 2 -6) ⇒ -12
  32. </pre>
  33. <p>Kawa has arbitrary-precision integers.
  34. </p>
  35. <p>Let us implement the <a class="ulink" href="http://en.wikipedia.org/wiki/Factorial" target="_top">factorial</a> function.
  36. Type in the following (we’ll look at the syntax shortly):
  37. </p>
  38. <pre class="screen"><span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(define (factorial x)</code></strong>
  39. <span class="prompt">#|(---:2|# </span><strong class="userinput"><code> (if (&lt; x 1) 1</code></strong>
  40. <span class="prompt">#|(---:3|# </span><strong class="userinput"><code> (* x (factorial (- x 1)))))</code></strong>
  41. </pre>
  42. <p>(The prompt changes to indicate a continuation line.)
  43. This binds the name <code class="literal">factorial</code>
  44. to a new function, with formal parameter <code class="literal">x</code>.
  45. This new function is immediately compiled to Java bytecodes,
  46. and later a JIT compiler may compile it to native code.
  47. </p>
  48. <p>A few tests:
  49. </p>
  50. <pre class="screen"><span class="prompt">#|kawa:4|# </span><strong class="userinput"><code>(list (factorial 3) (factorial 4))</code></strong>
  51. (6 24)
  52. <span class="prompt">#|kawa:5|# </span><strong class="userinput"><code>(factorial 30)</code></strong>
  53. 265252859812191058636308480000000
  54. </pre>
  55. <h3 id="idm139667880601184">Floating-point real numbers</h3>
  56. <p>Given what was said above about being able to add, subtract and multiply integers,
  57. the following may be unexpected:
  58. </p>
  59. <pre class="screen">#|kawa:1|# (/ 2 3)
  60. 2/3
  61. #|kawa:2|# (+ (/ 1 3) (/ 2 3))
  62. 1
  63. </pre>
  64. <p>In many languages, dividing two integers, as 2/3, would result in 0. At best,
  65. the result would be a floating point number, similar to 0.666667. Instead,
  66. Kawa has a <span class="emphasis"><em>rational</em></span> number type, which holds the results of divisions
  67. <span class="emphasis"><em>exactly</em></span>, as a proper fraction. Hence, adding one third to two thirds
  68. will always result in exactly one.
  69. </p>
  70. <p>Floating-point real numbers are known in Kawa as <span class="emphasis"><em>inexact</em></span> numbers, as
  71. they cannot be stored exactly. Consider:
  72. </p>
  73. <pre class="screen">#|kawa:3|# (exact? 2/3)
  74. #t
  75. #|kawa:4|# (exact? 0.33333333)
  76. #f
  77. #|kawa:5|# (exact-&gt;inexact 2/3)
  78. 0.6666666666666666
  79. </pre>
  80. <p>The first two examples check numbers for being <code class="literal">exact?</code>; there is a
  81. corresponding <code class="literal">inexact?</code> test. The last shows how an exact number can be
  82. converted to an inexact form.
  83. </p>
  84. <p>Numbers are converted between exact and inexact versions when required
  85. within operations or procedures:
  86. </p>
  87. <pre class="screen">#|kawa:6|# (+ 0.33333333 2/3)
  88. 0.9999999966666666
  89. #|kawa:7|# (inexact? (+ 0.33333333 2/3))
  90. #t
  91. #|kawa:8|# (sin 2/3)
  92. 0.618369803069737
  93. </pre>
  94. <h3 id="idm139667880593984">Complex numbers</h3>
  95. <p>A <span class="emphasis"><em>complex</em></span> number is made from two parts: a <span class="emphasis"><em>real</em></span> part and an
  96. <span class="emphasis"><em>imaginary</em></span> part. They are written <code class="literal">2+3i</code>. A complex number can
  97. be manipulated just like other numbers:
  98. </p>
  99. <pre class="screen">#|kawa:9|# (+ 2+3i 5+2i)
  100. 7+5i
  101. #|kawa:10|# (* 2+3i 4-3i)
  102. 17+6i
  103. #|kawa:11|# (integer? (+ 2+3i -3i))
  104. #t
  105. </pre>
  106. <p>Notice how in the last example the result is an integer, which Kawa recognises.
  107. </p>
  108. <p>Kawa also includes <a class="link" href="Quaternions.xhtml" title="Quaternions">quaternion</a> numbers.
  109. </p>
  110. <h3 id="idm139667880588928">Units and dimensions</h3>
  111. <p>In many applications, numbers have a <span class="emphasis"><em>unit</em></span>. For example, 5 might be
  112. a number of dollar bills, a weight on a scale, or a speed. Kawa enables us
  113. to represent numbers as <span class="emphasis"><em>quantities</em></span>: numbers along with their unit.
  114. For example, with weight, we might measure weight in pounds and ounces,
  115. where an ounce is 1/16 of a pound.
  116. </p>
  117. <p>Using Kawa, we can define units for our weight measurements, and specify the
  118. units along with numbers:
  119. </p>
  120. <pre class="screen">#|kawa:12|# (define-base-unit pound "Weight")
  121. #|kawa:13|# (define-unit ounce 0.0625pound)
  122. #|kawa:14|# 3pound
  123. 3.0pound
  124. #|kawa:15|# (+ 1pound 5ounce)
  125. 1.3125pound
  126. </pre>
  127. <p>In this example we define a base unit, the pound, and a unit based on it, the
  128. ounce, which is valued at 0.0625 pounds (one sixteenth). Numbers can then be
  129. written along with their unit (making them quantities). Arithmetic is possible
  130. with quantities, as shown in the last line, and Kawa will do the smart thing
  131. when combining units. In this case, 1 pound and 5 ounces is combined to make
  132. 1.3125 pounds.
  133. </p>
  134. </section>
  135. <footer>
  136. <div class="navfooter">
  137. <p>
  138. Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
  139. <p>
  140. Previous: <a accesskey="p" href="tutorial-Booleans.xhtml">Booleans</a></p>
  141. <p>
  142. Next: <a accesskey="n" href="tutorial-Functions.xhtml">Functions</a></p>
  143. </div>
  144. </footer>
  145. </body>
  146. </html>