| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Values and conversions - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Values and conversions - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html">Integers</a></li> |
| 25 | <li><a href="values.html" aria-current="page">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Raz · lesson 4 of 15</p><h1>Values and conversions</h1> |
| 42 | <p>This program uses several of the basic types at once, with each conversion written where it happens.</p> |
| 43 | <div class="file"><span>Values.raz</span></div><pre class="code"><code><span class="kw">module</span> Values; |
| 44 | <span class="kw">import</span> Fmt; |
| 45 | |
| 46 | <span class="kw">const</span> Max = <span class="number">100</span>; <span class="comment">(* untyped: it takes the type it meets *)</span> |
| 47 | |
| 48 | <span class="kw">proc</span> main*; |
| 49 | <span class="kw">var</span> n: <span class="ty">INT</span>; b: <span class="ty">U8</span>; big: <span class="ty">I64</span>; x: <span class="ty">F64</span>; c: <span class="ty">I32</span>; ok: <span class="ty">BOOL</span>; |
| 50 | <span class="kw">begin</span> |
| 51 | n := Max * <span class="number">3</span>; |
| 52 | b := <span class="number">250</span>; |
| 53 | b := b + <span class="number">10</span>; <span class="comment">(* wraps, in a U8: 4 *)</span> |
| 54 | big := n; <span class="comment">(* an INT goes into an I64 *)</span> |
| 55 | x := <span class="ty">F64</span>(n) / <span class="number">7.0</span>; <span class="comment">(* nothing else converts unasked *)</span> |
| 56 | c := <span class="string">'\u{E9}'</span>; <span class="comment">(* a character is its code point *)</span> |
| 57 | ok := (n > Max) <span class="kw">and</span> (b # <span class="number">0</span>); |
| 58 | Fmt.print(<span class="string">"{} {} {} {} {} {}\n"</span>, [Fmt.int(n), Fmt.int(<span class="ty">I64</span>(b)), |
| 59 | Fmt.int(big), Fmt.real(x, <span class="number">3</span>), Fmt.int(<span class="ty">I64</span>(c)), |
| 60 | Fmt.bool(ok)]); |
| 61 | <span class="kw">end</span>; |
| 62 | |
| 63 | <span class="kw">end</span> Values.</code></pre> |
| 64 | <pre class="output"><code>300 4 300 42.857 233 true |
| 65 | </code></pre> |
| 66 | <h2>Conversions are written</h2> |
| 67 | <p>Two types never mix but where nothing can be lost: an <code>INT</code> goes into an <code>I64</code> wherever it meets one, so with <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>; and a <code>U8</code>, <code>I8</code> or <code>I16</code> into an <code>INT</code>, unless an operator made it, so <code>n := s[i]</code> needs no conversion. With <code>i: I32</code> and a slice <code>s</code>, <code>i < LEN(s)</code> is an error. A type's name converts: <code>F64(n)</code>, <code>I64(b)</code>, <code>U8(x)</code>; an <code>I64</code> becomes an <code>INT</code> only so, <code>INT(pos)</code>, since an <code>INT</code> may be smaller on another machine. The <a href="integers.html">lesson on integers</a> says what each width conversion does.</p> |
| 68 | <p>A number written in the program is untyped until it meets a type, and <code>1.5</code> is a real, which an integer never becomes without a conversion.</p> |
| 69 | <div class="file"><span>Mix.raz</span></div><pre class="code"><code><span class="kw">module</span> Mix; |
| 70 | <span class="kw">import</span> Out; |
| 71 | |
| 72 | <span class="kw">proc</span> main*; |
| 73 | <span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="kw">const</span> <span class="ty">U8</span>; |
| 74 | <span class="kw">begin</span> |
| 75 | s := <span class="string">"four"</span>; |
| 76 | i := <span class="number">3</span>; |
| 77 | <span class="kw">if</span> i < <span class="ty">LEN</span>(s) <span class="kw">then</span> Out.str(<span class="string">"smaller\n"</span>); <span class="kw">end</span>; |
| 78 | <span class="kw">end</span>; |
| 79 | |
| 80 | <span class="kw">end</span> Mix.</code></pre> |
| 81 | <pre class="output term"><code><span class="cmd">$ raz Mix.raz</span> |
| 82 | Mix.raz:9:8: error: I32 and INT are different types |
| 83 | 9 | if i < LEN(s) then Out.str("smaller\n"); end; |
| 84 | | ^^^^^^^^^^ |
| 85 | help: convert one to the other's type, as in INT(i) |
| 86 | raz: 1 error</code></pre> |
| 87 | <p>The fix is the conversion, written where it happens: <code>if INT(i) < LEN(s)</code>. It costs nothing at run time and says what is meant.</p> |
| 88 | <h2>What converts to what</h2> |
| 89 | <p>Everything else that converts does so with the type's name, and the rules are short:</p> |
| 90 | <table><thead><tr><th>From</th><th>To</th><th>Result</th></tr></thead><tbody><tr><td data-label="From">integer</td><td data-label="To">integer</td><td data-label="Result">cut to the new width, or extended by the sign (signed) or zeros (unsigned)</td></tr><tr><td data-label="From">integer</td><td data-label="To"><code>F32</code>, <code>F64</code></td><td data-label="Result">the nearest real</td></tr><tr><td data-label="From"><code>F32</code>, <code>F64</code></td><td data-label="To">integer</td><td data-label="Result">rounded toward zero</td></tr><tr><td data-label="From"><code>F32</code></td><td data-label="To"><code>F64</code></td><td data-label="Result">the same value; the other way, the nearest <code>F32</code></td></tr><tr><td data-label="From">enumeration</td><td data-label="To">integer</td><td data-label="Result">its position; and back, if there is a name at that position</td></tr><tr><td data-label="From">set</td><td data-label="To">integer</td><td data-label="Result">its bits; and back</td></tr><tr><td data-label="From"><code>INT</code>, <code>UINT</code></td><td data-label="To">pointer</td><td data-label="Result">the address; and back</td></tr></tbody></table> |
| 91 | <p>There is no conversion between <code>BOOL</code> and an integer: say what you mean with <code>if</code>. A <code>BOOL</code> is <code>true</code> or <code>false</code>, and <code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s: they bind below the relations, so <code>i < n and a[i] # 0</code> needs no parentheses, and <code>and</code> and <code>or</code> evaluate their right operand only when it decides the result. <code>#</code>, not <code><></code>, is "different".</p> |
| 92 | <div class="file"><span>Truth.raz</span></div><pre class="code"><code><span class="kw">module</span> Truth; |
| 93 | <span class="kw">import</span> Out; |
| 94 | |
| 95 | <span class="kw">proc</span> positive(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>; |
| 96 | <span class="kw">begin</span> |
| 97 | <span class="kw">return</span> n > <span class="number">0</span>; |
| 98 | <span class="kw">end</span>; |
| 99 | |
| 100 | <span class="kw">proc</span> main*; |
| 101 | <span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; found: <span class="ty">BOOL</span>; |
| 102 | <span class="kw">begin</span> |
| 103 | a := [<span class="number">4</span>, <span class="number">0</span>, <span class="number">9</span>]; |
| 104 | i := <span class="number">0</span>; |
| 105 | <span class="kw">while</span> (i < <span class="ty">LEN</span>(a)) <span class="kw">and</span> (a[i] # <span class="number">0</span>) <span class="kw">do</span> <span class="ty">INC</span>(i); <span class="kw">end</span>; <span class="comment">(* never reads a[3] *)</span> |
| 106 | Out.int(i); Out.ln; |
| 107 | found := positive(i) <span class="kw">or</span> positive(-<span class="number">1</span>); |
| 108 | <span class="kw">if</span> found <span class="kw">and</span> <span class="kw">not</span> positive(<span class="number">0</span>) <span class="kw">then</span> Out.str(<span class="string">"true and true\n"</span>); <span class="kw">end</span>; |
| 109 | <span class="kw">end</span>; |
| 110 | |
| 111 | <span class="kw">end</span> Truth.</code></pre> |
| 112 | <pre class="output"><code>1 |
| 113 | true and true |
| 114 | </code></pre> |
| 115 | <h2>Reals</h2> |
| 116 | <p><code>F32</code> and <code>F64</code> are the reals. A real constant is written with a point, <code>1.5</code> or <code>2.0E-3</code>. <code>Fmt.real(x, places)</code> writes one with that many digits after the point, and <code>Math</code> has the functions.</p> |
| 117 | <div class="file"><span>Reals.raz</span></div><pre class="code"><code><span class="kw">module</span> Reals; |
| 118 | <span class="kw">import</span> Fmt, Math; |
| 119 | |
| 120 | <span class="kw">proc</span> main*; |
| 121 | <span class="kw">var</span> n: <span class="ty">INT</span>; x: <span class="ty">F64</span>; y: <span class="ty">F32</span>; |
| 122 | <span class="kw">begin</span> |
| 123 | n := <span class="number">22</span>; |
| 124 | x := <span class="ty">F64</span>(n) / <span class="number">7.0</span>; |
| 125 | y := <span class="ty">F32</span>(x); |
| 126 | Fmt.print(<span class="string">"22/7 = {}\n"</span>, [Fmt.real(x, <span class="number">6</span>)]); |
| 127 | Fmt.print(<span class="string">"as F32 = {}\n"</span>, [Fmt.real(<span class="ty">F64</span>(y), <span class="number">6</span>)]); |
| 128 | Fmt.print(<span class="string">"sqrt 2 = {}, pi = {}\n"</span>, [Fmt.real(Math.sqrt(<span class="number">2.0</span>), <span class="number">5</span>), Fmt.real(Math.pi, <span class="number">5</span>)]); |
| 129 | Fmt.print(<span class="string">"2^10 = {}, log2 of that = {}\n"</span>, [Fmt.real(Math.pow(<span class="number">2.0</span>, <span class="number">10.0</span>), <span class="number">1</span>), Fmt.real(Math.log2(<span class="number">1024.0</span>), <span class="number">1</span>)]); |
| 130 | Fmt.print(<span class="string">"rounded: {} {}\n"</span>, [Fmt.real(Math.round(<span class="number">2.5</span>), <span class="number">1</span>), Fmt.real(Math.round(-<span class="number">2.5</span>), <span class="number">1</span>)]); |
| 131 | Fmt.print(<span class="string">"truncated: {} {}\n"</span>, [Fmt.int(<span class="ty">I64</span>(<span class="number">2.9</span>)), Fmt.int(<span class="ty">I64</span>(-<span class="number">2.9</span>))]); |
| 132 | <span class="kw">end</span>; |
| 133 | |
| 134 | <span class="kw">end</span> Reals.</code></pre> |
| 135 | <pre class="output"><code>22/7 = 3.142857 |
| 136 | as F32 = 3.142857 |
| 137 | sqrt 2 = 1.41421, pi = 3.14159 |
| 138 | 2^10 = 1024.0, log2 of that = 10.0 |
| 139 | rounded: 3.0 -3.0 |
| 140 | truncated: 2 -2 |
| 141 | </code></pre> |
| 142 | <p>A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's <code>0.0</code> and a <code>BOOL</code>'s <code>false</code> included, so there is no uninitialised value to be surprised by.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="integers.html">← Integers</a><a href="index.html">Contents</a><a class="next" href="procedures.html">Procedures and several results →</a></div></article></div> |
| 143 | </main> |
| 144 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 145 | </body> |
| 146 | </html> |