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

【unitrix】 1.9 Z0与其它类型的算术运算(arith_ops.rs)

一、源码

这段代码是用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代表编译期已知的零值,通过类型系统保证算术运算的正确性。

相关文章:

  • 李沐--动手学深度学习 LSTM
  • 前端错误捕获
  • 模板方法模式Template Method Pattern
  • 移动应用开发实验室web组大一下期末考核题解
  • Vela sensor uORB 框架学习
  • 适配器模式Adapter Pattern
  • Java中如何使用lambda表达式分类groupby
  • STL容器分类总结
  • 探索RAGFlow:解锁生成式AI的无限潜能(2/6)
  • 第二十章 Centos8的使用
  • DP刷题练习(三)
  • linux thermal framework(4)_thermal governor
  • Linux 忘记root密码如何解决-linux025
  • 2.1 Windows VS2019编译FFmpeg 4.4.1
  • PCL 生成圆柱面点云
  • 人工智能-准确率(Precision)、召回率(Recall) 和 F1 分数
  • YOLO优化之双池化下采样融合块、注意力引导逆残差块
  • 第20篇:数据库中间件的热点 Key 缓存一致性策略与分布式协调机制
  • Spring Boot 整合 Swagger 快速生成 API 文档的最佳实践
  • Axure应用交互设计:中继器数据向多种类型元件赋值
  • 95资料库/南京seo代理
  • 自己做的网站怎么在百度上搜到/搜索引擎关键词的工具
  • 小微企业做网站/苏州百度推广分公司电话
  • 做网站用什么语言最好/百度广告搜索推广
  • 新网站建设总结/南宁网站seo
  • 网站制作协议书/互联网推广员是做什么的