02-数学功能
数学功能(Performing Math Functionality)
译文 · 基于 The Embedded Rust Book
原文链接: https://doc.rust-lang.org/stable/embedded-book/unsorted/math.html
若你想执行与数学相关的功能,例如计算平方根或指数,并且有完整的标准库可用,代码可能如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| //! 在有标准库支持时的一些数学函数
fn main() {
let float: f32 = 4.82832;
let floored_float = float.floor();
let sqrt_of_four = floored_float.sqrt();
let sinus_of_four = floored_float.sin();
let exponential_of_four = floored_float.exp();
println!("Floored test float {} to {}", float, floored_float);
println!("The square root of {} is {}", floored_float, sqrt_of_four);
println!("The sinus of four is {}", sinus_of_four);
println!(
"The exponential of four to the base e is {}",
exponential_of_four
)
}
|
没有标准库支持时,这些函数不可用。
可以改用像 libm 这样的外部 crate。示例代码将如下所示:
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
| #![no_main]
#![no_std]
use panic_halt as _;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use libm::{exp, floorf, sin, sqrtf};
#[entry]
fn main() -> ! {
let float = 4.82832;
let floored_float = floorf(float);
let sqrt_of_four = sqrtf(floored_float);
let sinus_of_four = sin(floored_float.into());
let exponential_of_four = exp(floored_float.into());
hprintln!("Floored test float {} to {}", float, floored_float).unwrap();
hprintln!("The square root of {} is {}", floored_float, sqrt_of_four).unwrap();
hprintln!("The sinus of four is {}", sinus_of_four).unwrap();
hprintln!(
"The exponential of four to the base e is {}",
exponential_of_four
)
.unwrap();
// 退出 QEMU
// 注意:不要在硬件上运行;它可能破坏 OpenOCD 状态
// debug::exit(debug::EXIT_SUCCESS);
loop {}
}
|
若需要在 MCU 上执行更复杂的操作,例如 DSP 信号处理或高级线性代数,以下 crate 可能有帮助: