3个版本
| 0.1.2 | 2020年5月11日 |
|---|---|
| 0.1.1 | 2020年5月11日 |
| 0.1.0 | 2020年5月8日 |
#1609 in 算法
每月39次下载
12KB
185 行
另一个度量单位库 
此crate为no_std程序提供类型安全的科学基本单位和常量。
示例
use yaum::time::*;
use yaum::length::*;
use yaum::velocity::*;
// Simple arithmetic
assert_eq!(1.0 * kph, 1.0 * km / h);
assert_eq!(1.0 * km / min, 60.0 * km / h);
// Read value in a given unit
assert_eq!(60.0, (1.0 * min).s());
assert_eq!(1_000.0, (1.0 * km).m());
当前支持的单位
time: 时间angle: 角度,角速度frequency: 频率,采样频率length: 长度velocity: 速度,加速度
使用宏impl_unit!、convert_div!和convert_unit!定义自定义单位和转换。
# #[macro_use] extern crate yaum;
use yaum::*;
use yaum::time::*;
yaum::impl_unit!(ByteSize, {
B: 1.0,
kB: 1024.0,
MB: 1024.0 * 1024.0
});
yaum::impl_unit!(BitSize, {
b: 1.0,
kb: 1024.0,
Mb: 1024.0 * 1024.0
});
// define conversion between the two units (1 byte = 8 bits):
yaum::convert_unit!(ByteSize, BitSize, 8.0);
yaum::impl_unit!(BitSpeed, {
bps: 1.0,
kbps: 1024.0,
Mbps: 1024.0 * 1024.0
});
// define relationship between units (BitSpeed = BitSize/Time)
yaum::convert_div!(BitSize, Time, BitSpeed);
fn main() {
assert_eq!(8.0 * b, (1.0 * B).into());
assert_eq!(1.0 * kbps, 1.0 * kb/s);
}
精度
默认情况下,单位是在f32之上实现的。启用double_precision功能以使用f64。
[dependencies.yaum]
version = "0.1.0"
default-features = false
features = ["double_precision"]