aboutsummaryrefslogtreecommitdiff
path: root/docs/integers.md
blob: bdef827dfc9d07e6f7fb839052bc781dd681dbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Integers

Tomo has five types of integers:

- `Int`: the default integer type, which uses an efficient tagged 29-bit
  integer value for small numbers, and falls back to a bigint implementation
  when values are too large to fit in 29-bits. The bigint implementation uses
  the GNU MP library. These integers are fast for small numbers and guaranteed
  to always be correct and never overflow.
- `Int8`/`Int16`/`Int32`/`Int64`: Fixed-size integers that take up `N` bits.
  These integers must be explicitly constructed using their type name (e.g.
  `Int64(5)`) and are subject to overflowing on arithmetic operations. If an
  overflow occurs, a runtime error will be raised.
- In cases where it is possible to infer that an integer literal should be
  used as a fixed-size integer, the literal will be converted at compile time
  to the appropriate fixed-size integer type and checked to ensure that it
  can fit in the needed size. For example, if you declare a variable as
  `x := Int64(0)` and later do `x + 1`, it's inferred that the `1` is a 64-bit
  integer literal.

Runtime conversion between integer types (casting) can be done explicitly by
calling the target type as a function: `Int32(x)`. For fixed-width types, the
conversion function also accepts a second parameter, `truncate`. If `truncate`
is `no` (the default), conversion will create a runtime error if the value is
too large to fit in the target type. If `truncate` is `yes`, then the resulting
value will be a truncated form of the input value.

Integers support the standard math operations (`x+y`, `x-y`, `x*y`, `x/y`) as
well as powers/exponentiation (`x^y`), modulus (`x mod y` and `x mod1 y`), and
bitwise operations: `x and y`, `x or y`, `x xor y`, `x << y`, `x >> y`, `x >>>
y` (unsigned right shift), and `x <<< y` (unsighted left shift). The operators
`and`, `or`, and `xor` are _bitwise_, not logical operators.

## Integer Literals

The simplest form of integer literal is a string of digits, which is inferred
to have type `Int` (unbounded size).

```tomo
>>> 123456789012345678901234567890
= 123456789012345678901234567890 : Int
```

Underscores may also be used to visually break up the integer for readability:

```tomo
a_million := 1_000_000
```

Hexadecimal, octal, and binary integer literals are also supported:

```tomo
hex := 0x123F
octal := 0o644
binary := 0b10101
```

For fixed-sized integers, use the type's name as a constructor:

```tomo
my_int64 := Int64(12345)
my_int32 := Int32(12345)
my_int16 := Int32(12345)
my_int8 := Int32(123)
```

A compiler error will be raised if you attempt to construct a value that cannot
fit in the specified integer size (e.g. `Int8(99999)`).

## A Note on Division

Unlike some other languages (including C), Tomo uses a mathematically
consistent definition of division called [Euclidean
Division](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf)
that upholds the following invariants for all inputs:

```tomo
quotient := numerator / denominator
remainder := numerator mod denominator

# Modulus always gives a non-negative result:
>> remainder >= 0
= yes

# The numerator can be reconstructed sensibly:
>> numerator == denominator * quotient + remainder
= yes
```

Importantly, these invariants hold for both positive and negative numerators
and denominators. When the numerator and denominator are both positive, you
will not notice any difference from how integer division and modulus work in
other programming languages. However, the behavior is a bit different when
negative numbers are involved. Integer division rounds _down_ instead of
rounding _towards zero_, and modulus never gives negative results:

```tomo
>> quotient := -1 / 5
= -1

>> remainder := -1 mod 5
= 4

>> -1 == 5 * -1 + 4
= yes
```

```tomo
>> quotient := 16 / -5
= -3

>> remainder := -1 mod 5
= 1

>> 16 == -5 * -3 + 1
= yes
```

## Integer Functions

Each integer type has its own version of the following functions. Functions
can be called either on the type itself: `Int.sqrt(x)` or as a method call:
`x.sqrt()`. Method call syntax is preferred.

- [`func abs(x: Int -> Int)`](#abs)
- [`func choose(n: Int, k: Int -> Int)`](#choose)
- [`func clamped(x, low, high: Int -> Int)`](#clamped)
- [`func factorial(n: Int -> Text)`](#factorial)
- [`func format(i: Int, digits: Int = 0 -> Text)`](#format)
- [`func hex(i: Int, digits: Int = 0, uppercase: Bool = yes, prefix: Bool = yes -> Text)`](#hex)
- [`func is_prime(x: Int, reps: Int = 50 -> Bool)`](#is_prime)
- [`func next_prime(x: Int -> Int)`](#next_prime)
- [`func octal(i: Int, digits: Int = 0, prefix: Bool = yes -> Text)`](#octal)
- [`func onward(first: Int, step: Int = 1 -> Text)`](#onward)
- [`func parse(text: Text -> Int?)`](#parse)
- [`func prev_prime(x: Int -> Int)`](#prev_prime)
- [`func sqrt(x: Int -> Int)`](#sqrt)
- [`func to(first: Int, last: Int, step : Int? = none -> func(->Int?))`](#to)

### `abs`
Calculates the absolute value of an integer.

```tomo
func abs(x: Int -> Int)
```

- `x`: The integer whose absolute value is to be calculated.

**Returns:**  
The absolute value of `x`.

**Example:**  
```tomo
>> (-10).abs()
= 10
```

---

### `choose`
Computes the binomial coefficient of the given numbers (the equivalent of `n`
choose `k` in combinatorics). This is equal to `n.factorial()/(k.factorial() *
(n-k).factorial())`.

```tomo
func choose(n: Int, k: Int -> Int)
```

- `n`: The number of things to choose from.
- `k`: The number of things to be chosen.

**Returns:**  
The binomial coefficient, equivalent to the number of ways to uniquely choose
`k` objects from among `n` objects, ignoring order.

**Example:**  
```tomo
>> (4).choose(2)
= 6
```

---

### `clamped`
Returns the given number clamped between two values so that it is within
that range.

```tomo
func clamped(x, low, high: Int -> Int)
```

- `x`: The integer to clamp.
- `low`: The lowest value the result can take.
- `high`: The highest value the result can take.

**Returns:**  
The first argument clamped between the other two arguments.

**Example:**  
```tomo
>> (2).clamped(5, 10)
= 5
```

---

### `factorial`
Computes the factorial of an integer.

```tomo
func factorial(n: Int -> Text)
```

- `n`: The integer to compute the factorial of.

**Returns:**  
The factorial of the given integer.

**Example:**  
```tomo
>> (10).factorial()
= 3628800
```

---

### `format`
Formats an integer as a string with a specified number of digits.

```tomo
func format(i: Int, digits: Int = 0 -> Text)
```

- `i`: The integer to be formatted.
- `digits`: The minimum number of digits to which the integer should be padded. Default is `0`.

**Returns:**  
A string representation of the integer, padded to the specified number of digits.

**Example:**  
```tomo
>> (42).format(digits=5)
= "00042"
```

---

### `hex`
Converts an integer to its hexadecimal representation.

```tomo
func hex(i: Int, digits: Int = 0, uppercase: Bool = yes, prefix: Bool = yes -> Text)
```

- `i`: The integer to be converted.
- `digits`: The minimum number of digits in the output string. Default is `0`.
- `uppercase`: Whether to use uppercase letters for hexadecimal digits. Default is `yes`.
- `prefix`: Whether to include a "0x" prefix. Default is `yes`.

**Returns:**  
The hexadecimal string representation of the integer.

**Example:**  
```tomo
>> (255).hex(digits=4, uppercase=yes, prefix=yes)
= "0x00FF"
```

---

### `is_prime`
Determines if an integer is a prime number.

**Note:**
This function is _probabilistic_. With the default arguments, the chances of
getting an incorrect answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.

```tomo
func is_prime(x: Int, reps: Int = 50 -> Bool)
```

- `x`: The integer to be checked.
- `reps`: The number of repetitions for primality tests. Default is `50`.

**Returns:**  
`yes` if `x` is a prime number, `no` otherwise.

**Example:**  
```tomo
>> (7).is_prime()
= yes
>> (6).is_prime()
= no
```

---

### `next_prime`
Finds the next prime number greater than the given integer.

**Note:**
This function is _probabilistic_, but the chances of getting an incorrect
answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.

```tomo
func next_prime(x: Int -> Int)
```

- `x`: The integer after which to find the next prime.

**Returns:**  
The next prime number greater than `x`.

**Example:**  
```tomo
>> (11).next_prime()
= 13
```

---

### `octal`
Converts an integer to its octal representation.

```tomo
func octal(i: Int, digits: Int = 0, prefix: Bool = yes -> Text)
```

- `i`: The integer to be converted.
- `digits`: The minimum number of digits in the output string. Default is `0`.
- `prefix`: Whether to include a "0o" prefix. Default is `yes`.

**Returns:**  
The octal string representation of the integer.

**Example:**  
```tomo
>> (64).octal(digits=4, prefix=yes)
= "0o0100"
```

---

### `onward`
Return an iterator that counts infinitely from the starting integer (with an
optional step size).

```tomo
func onward(first: Int, step: Int = 1 -> Text)
```

- `first`: The starting integer.
- `step`: The increment step size (default: 1).

**Returns:**  
An iterator function that counts onward from the starting integer.

**Example:**  
```tomo
nums : &[Int] = &[]
for i in (5).onward():
    nums.insert(i)
    stop if i == 10
>> nums[]
= [5, 6, 7, 8, 9, 10]
```

---

### `parse`
Converts a text representation of an integer into an integer.

```tomo
func parse(text: Text -> Int?)
```

- `text`: The text containing the integer.

**Returns:**  
The integer represented by the text. If the given text contains a value outside
of the representable range or if the entire text can't be parsed as an integer,
`none` will be returned.

**Example:**  
```tomo
>> Int.parse("123")
= 123 : Int?
>> Int.parse("0xFF")
= 255 : Int?

# Can't parse:
>> Int.parse("asdf")
= none : Int?

# Outside valid range:
>> Int8.parse("9999999")
= none : Int8?
```

---

### `prev_prime`
Finds the previous prime number less than the given integer.
If there is no previous prime number (i.e. if a number less than `2` is
provided), then the function will create a runtime error.

**Note:**
This function is _probabilistic_, but the chances of getting an incorrect
answer are astronomically small (on the order of 10^(-30)).
See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)
for more details.

```tomo
func prev_prime(x: Int -> Int)
```

- `x`: The integer before which to find the previous prime.

**Returns:**  
The previous prime number less than `x`.

**Example:**  
```tomo
>> (11).prev_prime()
= 7
```

---

### `sqrt`
Calculates the square root of an integer.

```tomo
func sqrt(x: Int -> Int)
```

- `x`: The integer whose square root is to be calculated.

**Returns:**  
The integer part of the square root of `x`.

**Example:**  
```tomo
>> (16).sqrt()
= 4
>> (17).sqrt()
= 4
```

---

### `to`
Returns an iterator function that iterates over the range of numbers specified.
Iteration is assumed to be nonempty and

```tomo
func to(first: Int, last: Int, step : Int? = none -> func(->Int?))
```

- `first`: The starting value of the range.
- `last`: The ending value of the range.
- `step`: An optional step size to use. If unspecified or `none`, the step will be inferred to be `+1` if `last >= first`, otherwise `-1`.

**Returns:**  
An iterator function that returns each integer in the given range (inclusive).

**Example:**  
```tomo
>> (2).to(5)
= func(->Int?)
>> [x for x in (2).to(5)]
= [2, 3, 4, 5]
>> [x for x in (5).to(2)]
= [5, 4, 3, 2]

>> [x for x in (2).to(5, step=2)]
= [2, 4]
```