当前位置: 首页 > 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代表编译期已知的零值,通过类型系统保证算术运算的正确性。

http://www.xdnf.cn/news/1048897.html

相关文章:

  • ArcGIS中批量获取输入面图层A中各要素的四至点的实现方法
  • 前端面试专栏-主流框架:8.React Hooks原理与使用规范
  • 在idea上打包DolphinScheduler
  • 三次贝塞尔曲线,二次贝塞尔曲线有什么区别
  • 全国产超小体积RK3576核心板,支持RK3576+FPGA,支持AI与实时控制
  • Python OpenGL文字渲染——SDL(高效+无限缩放)
  • 【三刷C语言】数据的存储
  • 动态规划之爬楼梯(二)
  • 行列式的性质 线性代数
  • 【Docker基础】Docker核心概念:命名空间(Namespace)之PID详解
  • springboot3-笔记总结
  • 大小模型协同
  • const 指针
  • Adguard安卓版:全方位广告拦截与隐私保护
  • 函数指针与指针函数:本质区别与高级应用
  • Kubernetes架构解析
  • LeetCode 48. 旋转图像
  • 算法导论第六章:堆排序与优先队列的艺术
  • MySQL进阶篇
  • Redis中的set底层实现
  • LeetCode 高频 SQL 50 题(基础版)之 【子查询】· 下
  • PMP考试中的100个关键点
  • Some chunks are larger than 500 KiB after minification. Consider
  • Java中如何使用lambda表达式分类groupby
  • 面经的疑难杂症
  • 『uniapp』onThemeChange监听主题样式,动态主题不正确生效,样式被覆盖的坑
  • 如何提高电脑打字速度?
  • 前端错误捕获
  • Vue3相关知识3
  • Mysql基础入门\期末速成