Systems in Rust
I am a weapons-grade finitist. I don’t believe in numbers larger than two.
itertools
fields.py
u
\(n\)count()
-c
-c
flag to Python to run a script directly at command line.;
$ time python3 -c "from itertools import count; print(0 in count())"
True
real 0m0.013s
user 0m0.014s
sys 0m0.000s
$ time python3 -c "from itertools import count; print(1000 in count())"
True
real 0m0.012s
user 0m0.012s
sys 0m0.000s
10^\(n\) | real |
---|---|
1 | 00.012 |
2 | 00.012 |
3 | 00.012 |
4 | 00.013 |
5 | 00.013 |
6 | 00.027 |
7 | 00.164 |
8 | 01.498 |
9 | 16.810 |
Let’s say you have a variable of type u8 that can hold values between 0 and 255. If you try to change the variable to a value outside that range, such as 256, integer overflow will occur, which can result in one of two behaviors. When you’re compiling in debug mode, Rust includes checks for integer overflow that cause your program to panic at runtime if this behavior occurs. Rust uses the term panicking when a program exits with an error…
When you’re compiling in release mode with the –release flag, Rust does not include checks for integer overflow that cause panics. Instead, if overflow occurs, Rust performs two’s complement wrapping. In short, values greater than the maximum value the type can hold “wrap around” to the minimum of the values the type can hold. In the case of a u8, the value 256 becomes 0, the value 257 becomes 1, and so on. The program won’t panic, but the variable will have a value that probably isn’t what you were expecting it to have. Relying on integer overflow’s wrapping behavior is considered an error.
The program won’t panic, but the variable will have a value that probably isn’t what you were expecting it to have. Relying on integer overflow’s wrapping behavior is considered an error.
To explicitly handle the possibility of overflow, you can use these families of methods provided by the standard library for primitive numeric types:
wrapping_*
methods, such as wrapping_add
.checked_*
methods.overflowing_*
methods.saturating_*
methods.f16
and ix
i32
upper bound, we can test it.time cargo run
Compiling num v0.1.0 (/home/user/tmp/num)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/num`
thread 'main' panicked at src/main.rs:3:11:
attempt to add with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
real 0m4.943s
user 0m4.914s
sys 0m0.079s
i32
u64
?
--release
flag.
time cargo run --release
u
\(n\)s and i
\(n\)’s in Rust are rings
u
\(n\) \(\nRightarrow a + b > a\)$ cargo run -- 10 20
Compiling num v0.1.0 (/home/user/tmp/num)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
Running `target/debug/num 10 20`
[src/main.rs:5:5] a + b = 30
30
- that makes sense!$ cargo run -- 100 200
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/num 100 200`
thread 'main' panicked at src/main.rs:5:10:
attempt to add with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ cargo run --release -- 100 200
Compiling num v0.1.0 (/home/user/tmp/num)
Finished `release` profile [optimized] target(s) in 0.25s
Running `target/release/num 100 200`
[src/main.rs:5:5] a + b = 44
44
? From Whence?\[ 100 + 200 \equiv 44 \pmod{2^8} \] - Or perhaps
i32::MAX
, u64::MAX
, etc.u64
models infinite \(\mathbb{Z}\)ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
Diff | 7 |
3
is more than 0
ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
Carry | 0 | 1 | 0 |
Diff | 7 |
ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
Carry | 0 | 1 | 0 |
Diff | 6 | 7 |
ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
Carry | 1 | 0 | 0 |
Diff | 6 | 7 |
ft | in. | .in | |
---|---|---|---|
A’ja | 6 | 4 | 0 |
Mean | 5 | 9 | 3 |
Carry | 1 | 0 | 0 |
Diff | 0 | 6 | 7 |
u64::MAX
by:
u8
s as digits in base 256 arithmeticdivmod
)1 | 3 | 9 | 5 | |
---|---|---|---|---|
3 | - | - | - | - |
4 | - | - | - | - |
1 | 3 | 9 | 5 | |
---|---|---|---|---|
3 | 3 | 9 | 27 | 15 |
4 | 4 | 12 | 36 | 20 |
100 | 30 | 9 | .5 | |
---|---|---|---|---|
30 | 3000 | 900 | 270 | 15 |
4 | 400 | 120 | 36 | 2 |
100 | 30 | 9 | .5 | |
---|---|---|---|---|
30 | 3000 | 900 | 270 | 15 |
4 | 400 | 120 | 36 | 2 |
\[ \begin{align*} 5& \times 4 \times 10^{-1} &= 2&\\ +5& \times 3 \times 10^{0} &= 15&\\ +9& \times 4 \times 10^{0} &= 36&\\ +9& \times 3 \times 10^{1} &= 270&\\ +3& \times 4 \times 10^{1} &= 120&\\ +3& \times 3 \times 10^{2} &= 900&\\ +1& \times 4 \times 10^{2} &= 400&\\ +1& \times 3 \times 10^{3} &= 3000&\\ \end{align*} \]
\[ \begin{align*} 139.5& = &1 * 10^2 + &3 * 10^1 + &9 * 10^0& + 5 * 10^{-1}\\ 34& = &&3 * 10^1 + &4 * 10^0&\\ \end{align*} \]
Take \(x = 10\) \[ \begin{align*} 139.5& = &1 * x^2 + &3 * x + &9& + 5 * x^{-1}\\ 34& = &&3 * x + &4&\\ \end{align*} \]
That is polynomial; can work with those.
\[ (x^2 + 3x + 9 + 5x^{-1})(3x + 4) \]
\[ (x^2 + 3x + 9 + 5x^{-1})(3x) + (x^2 + 3x + 9 + 5x^{-1})(4) \]
\[ (3x^3 + 9x^2 + 27x + 15) + (4x^2 + 12x + 36 + 20x^{-1}) \]
\[ 3x^3 + 13x^2 + 39x + 51 + 20x^{-1} \]
\[ 3x^3 + 13x^2 + 39x + 51x^0 + 20x^{-1} \]
f16
, we can create something to hold numbers.
+
and -
.
(&ix, &ix) -> ix
) to use the autotester.