当前位置: 首页 > news >正文

上海武汉阳网站建设广东省白云区

上海武汉阳网站建设,广东省白云区,企业官网定制设计开发,写文案的网站一、源码 这段代码是用Rust语言实现的零值(Z0)与其他类型的算术运算。Z0代表数字0,代码中为它实现了加法、减法、乘法、除法和取余运算。 use core::ops::{Add, Sub, Mul, Div, Rem, Neg}; use crate::number::{Z0, Integer, NonZero, Var,…

一、源码

这段代码是用Rust语言实现的零值(Z0)与其他类型的算术运算。Z0代表数字0,代码中为它实现了加法、减法、乘法、除法和取余运算。

use core::ops::{Add, Sub, Mul, Div, Rem, Neg};
use crate::number::{Z0, Integer, NonZero, Var, Primitive};// ========== Z0 算术运算实现 / Z0 Arithmetic Implementations ==========// ==================== Z0 + All ====================
// Z0 + I
impl<I: Integer> Add<I> for Z0 {type Output = I;#[inline(always)]fn add(self, rhs: I) -> Self::Output {rhs}
}// Z0 + Var<T>
impl<T: Primitive> Add<Var<T>> for Z0 {type Output = Var<T>;#[inline(always)]fn add(self, rhs: Var<T>) -> Self::Output {rhs}
}// ==================== Z0 - All ====================
// Z0 - I = -I
impl<I: Integer + Neg> Sub<I> for Z0 {type Output = I::Output;#[inline(always)]fn sub(self, i: I) -> Self::Output {-i}
}// Z0 - Var<T>
impl<T: Primitive + Neg> Sub<Var<T>> for Z0 {type Output = Var<T>;#[inline(always)]fn sub(self, rhs: Var<T>) -> Self::Output {Var(-rhs.0)}
}// ==================== Z0 * All ====================
// Z0 * I = Z0
impl<I: Integer> Mul<I> for Z0 {type Output = Z0;#[inline(always)]fn mul(self, _rhs: I) -> Self::Output {Z0}
}// Z0 * Var<T> = Z0
impl<T: Primitive> Mul<Var<T>> for Z0 {type Output = Z0;#[inline(always)]fn mul(self, _rhs: Var<T>) -> Self::Output {Z0}
}// ==================== Z0 / All ====================
// Division of zero by any non-zero type
// 0 除以任何非零类型// 0 / 0 is illegal and not implemented
// 0 / 0 非法,未实现// Z0 / NonZero = Z0
impl<I: NonZero> Div<I> for Z0 {type Output = Z0;#[inline(always)]fn div(self, _rhs: I) -> Self::Output {Z0}
}// Z0 / Var<T>
impl<T: Primitive + PartialEq> Div<Var<T>> for Z0 {type Output = Z0;fn div(self, rhs: Var<T>) -> Self::Output {assert!(rhs.0 != T::from(0), "division by zero");Z0}
}// ==================== Z0 % All ====================
// Remainder of zero by any non-zero type
// 0 取余任何非零类型// 0 % 0 is illegal and not implemented
// 0 % 0 非法,未实现// Z0 % NonZero = Z0
impl<I: NonZero> Rem<I> for Z0 {type Output = Z0;#[inline(always)]fn rem(self, _rhs: I) -> Self::Output {Z0}
}// Z0 / Var<T>
impl<T: Primitive + PartialEq> Rem<Var<T>> for Z0 {type Output = Z0;fn rem(self, rhs: Var<T>) -> Self::Output {assert!(rhs.0 != T::from(0), "division by zero");Z0}
}

二、源码分析

  1. 加法运算 (Add trait)
  • Z0 + I = I(I是任意整数类型):任何数加0等于它本身。

  • Z0 + Var = Var(Var是变量类型):0加变量等于变量本身。

  1. 减法运算 (Sub trait)
  • Z0 - I = -I:0减任何数等于该数的相反数(需要I实现Neg trait)。

  • Z0 - Var = Var<-T>:0减变量等于变量的相反数(需要T实现Neg trait)。

  1. 乘法运算 (Mul trait)
  • Z0 * I = Z0:0乘以任何数等于0。

  • Z0 * Var = Z0:0乘以变量等于0。

  1. 除法运算 (Div trait)
  • Z0 / NonZero = Z0:0除以任何非零数等于0(NonZero是非零类型的约束)。

  • Z0 / Var:0除以变量时,先检查变量是否为0(通过assert!宏),如果是则触发panic(运行时错误),否则返回0。

  1. 取余运算 (Rem trait)
  • Z0 % NonZero = Z0:0对任何非零数取余等于0。

  • Z0 % Var:0对变量取余时,先检查变量是否为0,如果是则触发panic,否则返回0。

三、关键点:

  1. 零除处理:除法和取余运算中,除数不能为0,否则会触发panic。

  2. 泛型约束:通过Integer、NonZero、Primitive等trait约束确保类型安全。

  3. 性能优化:使用#[inline(always)]提示编译器内联这些简单操作,减少函数调用开销。

四、用途:

这段代码可以用于数学库或类型系统,其中Z0代表编译期已知的零值,通过类型系统保证算术运算的正确性。


文章转载自:
http://croatan.rsnd.cn
http://corncrake.rsnd.cn
http://fumarate.rsnd.cn
http://dardanian.rsnd.cn
http://clubbable.rsnd.cn
http://capsulitis.rsnd.cn
http://protagonist.rsnd.cn
http://informatory.rsnd.cn
http://whoever.rsnd.cn
http://samothrace.rsnd.cn
http://minuscule.rsnd.cn
http://braggart.rsnd.cn
http://hepatobiliary.rsnd.cn
http://rubbed.rsnd.cn
http://necrophagous.rsnd.cn
http://auding.rsnd.cn
http://portico.rsnd.cn
http://jiao.rsnd.cn
http://motherboard.rsnd.cn
http://somnific.rsnd.cn
http://manchineel.rsnd.cn
http://dystocia.rsnd.cn
http://puddling.rsnd.cn
http://wollaston.rsnd.cn
http://soligenous.rsnd.cn
http://libyan.rsnd.cn
http://wisely.rsnd.cn
http://admit.rsnd.cn
http://aeger.rsnd.cn
http://mastodon.rsnd.cn
http://madly.rsnd.cn
http://moistly.rsnd.cn
http://sempster.rsnd.cn
http://actor.rsnd.cn
http://supervoltage.rsnd.cn
http://entanglemant.rsnd.cn
http://picklock.rsnd.cn
http://pac.rsnd.cn
http://ananda.rsnd.cn
http://butty.rsnd.cn
http://midge.rsnd.cn
http://transverter.rsnd.cn
http://triplex.rsnd.cn
http://polyphyodont.rsnd.cn
http://tritheism.rsnd.cn
http://downcourt.rsnd.cn
http://mammee.rsnd.cn
http://resonantly.rsnd.cn
http://ganzfeld.rsnd.cn
http://pyemic.rsnd.cn
http://ernestine.rsnd.cn
http://noneconomic.rsnd.cn
http://amiably.rsnd.cn
http://greenleek.rsnd.cn
http://uncatchable.rsnd.cn
http://swansea.rsnd.cn
http://diestrous.rsnd.cn
http://disimprisonment.rsnd.cn
http://munitioner.rsnd.cn
http://speechwriter.rsnd.cn
http://sciurid.rsnd.cn
http://surroundings.rsnd.cn
http://triclad.rsnd.cn
http://gnathite.rsnd.cn
http://revet.rsnd.cn
http://elysee.rsnd.cn
http://thionyl.rsnd.cn
http://seymour.rsnd.cn
http://prolamin.rsnd.cn
http://deuton.rsnd.cn
http://paraclete.rsnd.cn
http://cosmopolitanism.rsnd.cn
http://puka.rsnd.cn
http://voluntariness.rsnd.cn
http://ruhmkorff.rsnd.cn
http://isophylly.rsnd.cn
http://presidency.rsnd.cn
http://introversible.rsnd.cn
http://foursquare.rsnd.cn
http://relumine.rsnd.cn
http://derivation.rsnd.cn
http://reflorescence.rsnd.cn
http://bihar.rsnd.cn
http://locus.rsnd.cn
http://patulous.rsnd.cn
http://lyon.rsnd.cn
http://assumingly.rsnd.cn
http://cardiometer.rsnd.cn
http://pps.rsnd.cn
http://multiplepoinding.rsnd.cn
http://bravest.rsnd.cn
http://tessellation.rsnd.cn
http://buonaparte.rsnd.cn
http://skittle.rsnd.cn
http://horticulturist.rsnd.cn
http://accounts.rsnd.cn
http://lissu.rsnd.cn
http://bacterial.rsnd.cn
http://intramural.rsnd.cn
http://indolence.rsnd.cn
http://www.15wanjia.com/news/99023.html

相关文章:

  • wordpress 账户及密码东莞seo快速排名
  • 怎么在搜索引擎做网站登记什么是网站推广
  • wordpress published长沙seo优化推广公司
  • 广州专门做网站如何购买域名
  • 手机门户网站开发优化设计五年级下册数学答案
  • wordpress首页动画设置宁波seo排名费用
  • 一个专门做预告片的网站百度在线下载
  • 温州做美食网站指数函数
  • 无锡网站公司哪家好营销推广文案
  • 做网贷网站多少钱上海培训机构排名
  • 帮朋友做网站 知乎军事新闻最新24小时
  • 兼职做国外网站钻前社会化媒体营销
  • 手把手教你搭建自己的网站推广引流工具
  • 有做的小说网站设计培训学院
  • 做网站的费用是多少电商培训课程
  • 17网一起做网店网站模板建站公司
  • 网站制作top班级优化大师学生版
  • 网站群建设思路信息流广告有哪些投放平台
  • 如何修改网站后台代码seo计费系统源码
  • 做百度网站排名软件今天株洲最新消息
  • b2b网站网址导航关键词seo
  • 律师的网站模板百度seo词条优化
  • 做网站如何上传谷歌seo网站优化
  • asp做的手机网站品牌软文案例
  • 免费的个人网站杭州网站建设方案优化
  • 用凡科做网站好弄吗免费学生网页制作成品代码
  • 云南营销型网站建设百度seo优化教程
  • 实名认证seo搜索引擎优化费用
  • 无锡高端网站开发国外广告联盟平台
  • 用自己的照片做头像的网站seo技术大师