raz / site / content / tour / 04-values.txt

101 lines · 3.6 KiB · raw · history · last change a5145f64

1title: Values and conversions
2part: Raz
3summary: Nothing converts unasked; what converts to what; booleans and reals.
4---
5This program uses several of the basic types at once, with each conversion written where it happens.
6
7```raz Values run
8```
9
10## Conversions are written
11
12Two types never mix but where nothing can be lost: an `INT` goes into an `I64` wherever it meets one, so with `pos: I64`, `pos + LEN(s)` is an `I64`; and a `U8`, `I8` or `I16` into an `INT`, unless an operator made it, so `n := s[i]` needs no conversion. With `i: I32` and a slice `s`, `i < LEN(s)` is an error. A type's name converts: `F64(n)`, `I64(b)`, `U8(x)`; an `I64` becomes an `INT` only so, `INT(pos)`, since an `INT` may be smaller on another machine. The [lesson on integers](integers.html) says what each width conversion does.
13
14A number written in the program is untyped until it meets a type, and `1.5` is a real, which an integer never becomes without a conversion.
15
16```raz Mix
17module Mix;
18import Out;
19
20proc main*;
21var i: I32; s: []const U8;
22begin
23 s := "four";
24 i := 3;
25 if i < LEN(s) then Out.str("smaller\n"); end;
26end;
27
28end Mix.
29```
30
31```sh
32$ raz Mix.raz
33```
34
35The fix is the conversion, written where it happens: `if INT(i) < LEN(s)`. It costs nothing at run time and says what is meant.
36
37## What converts to what
38
39Everything else that converts does so with the type's name, and the rules are short:
40
41| From | To | Result |
42|---|---|---|
43| integer | integer | cut to the new width, or extended by the sign (signed) or zeros (unsigned) |
44| integer | `F32`, `F64` | the nearest real |
45| `F32`, `F64` | integer | rounded toward zero |
46| `F32` | `F64` | the same value; the other way, the nearest `F32` |
47| enumeration | integer | its position; and back, if there is a name at that position |
48| set | integer | its bits; and back |
49| `INT`, `UINT` | pointer | the address; and back |
50
51There is no conversion between `BOOL` and an integer: say what you mean with `if`. A `BOOL` is `true` or `false`, and `and`, `or` and `not` are for `BOOL`s: they bind below the relations, so `i < n and a[i] # 0` needs no parentheses, and `and` and `or` evaluate their right operand only when it decides the result. `#`, not `<>`, is "different".
52
53```raz Truth run
54module Truth;
55import Out;
56
57proc positive(n: INT): BOOL;
58begin
59 return n > 0;
60end;
61
62proc main*;
63var a: [3]INT; i: INT; found: BOOL;
64begin
65 a := [4, 0, 9];
66 i := 0;
67 while (i < LEN(a)) and (a[i] # 0) do INC(i); end; (* never reads a[3] *)
68 Out.int(i); Out.ln;
69 found := positive(i) or positive(-1);
70 if found and not positive(0) then Out.str("true and true\n"); end;
71end;
72
73end Truth.
74```
75
76## Reals
77
78`F32` and `F64` are the reals. A real constant is written with a point, `1.5` or `2.0E-3`. `Fmt.real(x, places)` writes one with that many digits after the point, and `Math` has the functions.
79
80```raz Reals run
81module Reals;
82import Fmt, Math;
83
84proc main*;
85var n: INT; x: F64; y: F32;
86begin
87 n := 22;
88 x := F64(n) / 7.0;
89 y := F32(x);
90 Fmt.print("22/7 = {}\n", [Fmt.real(x, 6)]);
91 Fmt.print("as F32 = {}\n", [Fmt.real(F64(y), 6)]);
92 Fmt.print("sqrt 2 = {}, pi = {}\n", [Fmt.real(Math.sqrt(2.0), 5), Fmt.real(Math.pi, 5)]);
93 Fmt.print("2^10 = {}, log2 of that = {}\n", [Fmt.real(Math.pow(2.0, 10.0), 1), Fmt.real(Math.log2(1024.0), 1)]);
94 Fmt.print("rounded: {} {}\n", [Fmt.real(Math.round(2.5), 1), Fmt.real(Math.round(-2.5), 1)]);
95 Fmt.print("truncated: {} {}\n", [Fmt.int(I64(2.9)), Fmt.int(I64(-2.9))]);
96end;
97
98end Reals.
99```
100
101A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's `0.0` and a `BOOL`'s `false` included, so there is no uninitialised value to be surprised by.