RustTips比较数值内容比较与类型转换浮点类型比较可以用这些运算符比较数值><==!=>=<=无法比较不同类型的值fnmain(){leta:i32=10;letb:u16=100;ifa<b{//报错
> < == != >= <=
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < b { // 报错 mismatched types
println!("Ten is less than one hundred.");
}
}
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < (b as i32) {
println!("Ten is less than one hundred.");
}
}
注意:从比较小的类型转成比较大的类型通常是比较安全的
fn main() {
let a: i32 = 10;
let b: u16 = 100;
if a < (b as i32) {
println!("10 is less than 100.")
}
let c : i32 = 1203414;
println!("{}", c as i8);
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.22s
Running `target/debug/rust_compare_numerical_values`
10 is less than 100.
-42
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
use std::convert::TryInto;
fn main() {
let a: i32 = 10;
let b: u16 = 100;
let b_ = b.try_into().unwrap();
if a < b_ {
println!("10 is less than 100.")
}
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s
Running `target/debug/rust_compare_numerical_values`
10 is less than 100.
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
std::convert::TryInto Trait
std::cmp::PartialEq
,而其它数值类型还实现了std::cmp::Eq
fn main() {
assert!(0.1 + 0.2 == 0.3);
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/rust_compare_numerical_values`
thread 'main' panicked at 'assertion failed: 0.1 + 0.2 == 0.3', src/main.rs:2:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
例子
fn main() {
let abc: (f32, f32, f32) = (0.1, 0.2, 0.3);
let xyz: (f64, f64, f64) = (0.1, 0.2, 0.3);
println!("abc (f32)");
println!(" 0.1 + 0.2: {:x}", (abc.0 + abc.1).to_bits());
println!(" 0.3: {:x}", (abc.2).to_bits());
println!();
println!("xyz (f64)");
println!(" 0.1 + 0.2: {:x}", (xyz.0 + xyz.1).to_bits());
println!(" 0.3: {:x}", (xyz.2).to_bits());
println!();
assert!(abc.0 + abc.1 == abc.2);
assert!(xyz.0 + xyz.1 == xyz.2);
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.89s
Running `target/debug/rust_compare_numerical_values`
abc (f32)
0.1 + 0.2: 3e99999a
0.3: 3e99999a
xyz (f64)
0.1 + 0.2: 3fd3333333333334
0.3: 3fd3333333333333
thread 'main' panicked at 'assertion failed: xyz.0 + xyz.1 == xyz.2', src/main.rs:16:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
fn main() {
let result: f32 = 0.1 + 0.2;
let desired: f32 = 0.3;
let abs_diff = (desired - result).abs();
assert!(abs_diff <= f32::EPSILON)
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/rust_compare_numerical_values`
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
例子
fn main() {
let result: f64 = 0.1 + 0.2;
let desired: f64 = 0.3;
print!("{}\n", desired - result);
let abs_diff = (desired - result).abs();
assert!(abs_diff <= f64::EPSILON)
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.13s
Running `target/debug/rust_compare_numerical_values`
-0.00000000000000005551115123125783
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
fn main() {
let x = (-42.0_f32).sqrt();
assert_eq!(x, x);
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.38s
Running `target/debug/rust_compare_numerical_values`
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `NaN`,
right: `NaN`', src/main.rs:4:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
fn main() {
let x: f32 = 1.0 / 0.0;
println!("{}", x);
assert!(x.is_finite());
}
运行
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜ cargo run
Compiling rust_compare_numerical_values v0.1.0 (/Users/qiaopengjun/rust/rust_compare_numerical_values)
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
Running `target/debug/rust_compare_numerical_values`
inf
thread 'main' panicked at 'assertion failed: x.is_finite()', src/main.rs:6:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
rust_compare_numerical_values on master [?] is 📦 0.1.0 via 🦀 1.67.1 via 🅒 base
➜
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!