Systems in Rust
5/2
is 2.5
or perhaps 2½.max(count())
Pinocchiosf64
) holds 15 decimal places and I only need 3, so I have nothing to worry about”1\[ \frac{x}{\sqrt{x^2+y^2}} \leq 1 \]
\[a \times 10^b\]
\[a \times 10^b\]
Speed of light: The speed of light in a vacuum is approximately \(300,000,000 \text{ m/s}\) \[ 3 \times 10^8 \text{ m/s} \]
Mass of an electron: The mass of an electron is approximately \(0.00000000000000000000000000091093837 \text{ g}\). \[ 9.1093837 \times 10^{-28} \text{ g} \]
0b0
to 0b10000000
to 0b10010010000111111011011
01000000010010010000111111011011
0b0
to 0b10000000
to 0b10010010000111111011011
()
>>> _ = [print(f"{f32(i/10):1.100}") for i in range(1,4)]
0.100000001490116119384765625
0.20000000298023223876953125
0.300000011920928955078125
>>> _ = [print(f"{f64(i/10):1.100}") for i in range(1,4)]
0.1000000000000000055511151231257827021181583404541015625
0.200000000000000011102230246251565404236316680908203125
0.299999999999999988897769753748434595763683319091796875
$ cat src/main.rs
fn main() {
let x:f32 = 1.0;
println!("{:032b}", x.to_bits());
}
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/bits`
00111111100000000000000000000000
0
01111111
NaN
, inf
, etc.f32
as having “32 bits of precision”.$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/bits`
00111111100000000000000000000001
1.00000011920928955078125000000000
Format | Sign | Exp. | Mant. | Bits | Bias | Prec. | Significance |
---|---|---|---|---|---|---|---|
Half | 1 | 5 | 10 | 16 | 15 | 11 | 3-4 |
Single | 1 | 8 | 23 | 32 | 127 | 24 | 6-9 |
Double | 1 | 11 | 52 | 64 | 1023 | 53 | 15-17 |
Quad | 1 | 15 | 112 | 128 | 16383 | 113 | 33-36 |
src/main.rs
std::nextafter
for which Rust crates exist.The number of floats from 0.0
>>> from numpy import pi as pi
>>> # copy paste Wolfram|Alpha
>>> '3.14159265358979323846264338327950288419716939937510582097494459230781640628'
'3.14159265358979323846264338327950288419716939937510582097494459230781640628'
>>> f'{pi:1.100}'
'3.141592653589793115997963468544185161590576171875'
unwrap
.let x = (0..).find(|&i| f64::powi(2.0, i) + 1.0 == f64::powi(2.0, i)).unwrap();
let base = f64::powi(2.0, x);
// Prints the result (2^53 = 9007199254740992.0)
println!("2.0 ** x + 0.0 = {:.1}", base);
println!("2.0 ** x + 1.0 = {:.1}", base + 1.0);
find
before, this is an LLM donation to our knowledge base.2 ** 53
and there are 52 bits of mantissa precision.The gap between the two floating-point numbers nearest to x, even if x is one of )them.
pi
in f32
, around \(2.9 \times 10^{-8}\)src/lib.rs
fn heron(a:f32, b:f32, c:f32) -> f32 {
let s = (a + b + c)/2.0_f32;
return (s * (s - a) * (s - b) * (s - c)).sqrt();
}
fn kahan(a:f32, b:f32, c:f32) -> f32 {
let mut s = vec![a,b,c];
s.sort_by(|a, b| a.partial_cmp(b).unwrap()); // "partial"? Why not `.sort`?
let mut prod = s[0] + s[1] + s[2];
prod *= s[2] - (s[0] - s[1]);
prod *= s[2] + (s[0] - s[1]);
prod *= s[0] + (s[1] - s[2]);
return prod.sqrt() / 4.0;
}
src/main.rs
[src/main.rs:21:5] heron(3.0,4.0,5.0) = 6.0
[src/main.rs:22:5] kahan(3.0,4.0,5.0) = 6.0
[src/main.rs:25:5] heron(a,a,c) = 781.25006
[src/main.rs:26:5] kahan(a,a,c) = 500.0
f64
(vs f32
) we get 499.9999999999994<
in Rust as you may provide a NaN
.IEEE 754 Exception | Result when traps disabled | Argument to trap handler |
---|---|---|
overflow | \(\pm \infty\) or \(\pm x_{\text{max}}\) | \(\text{round}(x 2^{-a})\) |
underflow | \(0, \pm 2^{\text{min}}\) or denormalized | \(\text{round}(x 2^{a})\) |
divide by zero | \(\pm \infty\) | invalid operation |
invalid | \(\text{NaN}\) | invalid operation |
inexact | \(\text{round}(x)\) | \(\text{round}(x)\) |