当前位置:网站首页>【Rust每周一库】num-bigint - 大整数
【Rust每周一库】num-bigint - 大整数
2022-06-30 09:48:00 【51CTO】
我们知道rust标准类型里支持的最大整数是U128,那么如果想要使用超过这个大小的整数(多半是在编程作业/科学运算/区块链)该怎么办么?这个时候就需要我们的num-bigint库了。注意这个库被收录在num这个元库里。我们下面为了方便就引入num库。
[dependencies]
num = "0.3"
如果要单独使用num-bigint那么可以
[dependencies]
num-bigint = "0.3"
基本用法
我们以计算斐波纳切数列为例:
use num::bigint::BigUint;
use num::traits::{Zero, One};
use std::mem::replace;
// 计算斐波纳切数列的逻辑
fn fib(n: usize) -> BigUint {
letmut f0: BigUint = Zero::zero();
letmut f1: BigUint = One::one();
for _ in0..n {
let f2 = f0 + &f1; // 注意&
// f0 <- f1, f1 <- f2
f0 = replace(&mut f1, f2);
}
f0
}
fn main() {
println!("fib(1000) = {}", fib(1000));
}
随机大数
也可以配合rand生成随机大数。这个时候需要使用rand的feature,并且rand目前只能用0.7版本
[dependencies]
rand = "0.7"
num = { version = "0.3", features = ["rand"] }
然后就可以生成随机数了
use num::bigint::{ToBigInt, RandBigInt};
fn main() {
// 随机生成一个bigint
letmut rng = rand::thread_rng();
let a = rng.gen_bigint(1000);
// 生成一个特定范围的bigint
let low = -10000.to_bigint().unwrap();
let high = 10000.to_bigint().unwrap();
let b = rng.gen_bigint_range(&low, &high);
println!("{}", a * b);
}
以上就是num-bigint的基本用法。num库里还有一些别的实用的内容大家也可以去探索一下。
边栏推荐
- Node environment configuration
- After recording 7000 IELTS words in 100 sentences, there are only 1043 words (including simple words such as I and you)
- 从0使用keil5软件仿真调试GD32F305
- 2022第六季完美童模 合肥赛区 初赛圆满落幕
- 基于强化学习的股票量化交易Automated-Stock-Trading-Ensemble-Strategy
- ‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
- MIT-6874-Deep Learning in the Life Sciences Week4
- 长城数艺数字藏品平台发布创世徽章
- 6.Redis新数据类型
- GNN动手实践(二):复现图注意力网络GAT
猜你喜欢

Dyson design award, changing the world with sustainable design
[email protected]基于51系列单片机的智能仪器教具"/>技能梳理[email protected]基于51系列单片机的智能仪器教具
![[C language quick start] let you know C language and get started with zero basics ③](/img/ab/8dee3ee264429fc57ce18fdf8b2ea0.png)
[C language quick start] let you know C language and get started with zero basics ③

G code explanation | list of the most important G code commands

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

unable to convert expression into double array

"Kunming City coffee map" activity was launched again

Getting started with X86 - take over bare metal control

NLopt--非线性优化--原理介绍及使用方法

光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
随机推荐
"Kunming City coffee map" activity was launched again
GD32 RT-Thread RTC驱动函数
JS obtient la chaîne spécifiée spécifiant la position du caractère & sous - chaîne spécifiant la plage de position du caractère 【 détails simples 】
ModuleNotFoundError: No module named ‘_ swigfaiss‘
unable to convert expression into double array
技能梳理[email protected]体感机械臂
Some domestic image sources
MySQL advanced SQL statement of database (2)
The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
我在鹅厂淘到了一波“炼丹神器”,开发者快打包
逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展
The rising star of Goldshell STC box
Test memory read rate
WGet -- 404 not found due to spaces in URL
光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
KOREANO ESSENTIAL打造气质职场范
OSError: [Errno 28] No space left on device
AttributeError: ‘Version‘ object has no attribute ‘major‘
GeoffreyHinton:我的五十年深度学习生涯与研究心法