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

【unitrix】 3.3 算术取负运算(neg.rs)

一、源码

这段代码是用Rust语言实现的一个类型系统级别的算术取负运算(-运算符),通过Rust的特性(trait)系统来实现。

use core::ops::{Not, Neg};
use crate::number::{Z0, P1, N1, B0, B1, NonZero, Float, Var, PrimitiveInt, Primitive};// ==================== 算术取负(-运算符) ====================
impl Neg for Z0 { // Z0 (0) 的取负type Output = Z0;fn neg(self) -> Self::Output {Z0}
}impl Neg for P1 {type Output = N1;fn neg(self) -> Self::Output {N1}
}impl Neg for N1 { // N1 (-1) 的取负,用P1替换B1<Z0>type Output = P1;fn neg(self) -> Self::Output {P1}
}impl<H: NonZero + Neg> Neg for B0<H>{type Output = B0<H::Output>;fn neg(self) -> Self::Output {B0::new()}
}impl<H: NonZero + Not> Neg for B1<H>{type Output = B1<H::Output>;fn neg(self) -> Self::Output {B1::new()}
}// 浮点数取反
impl<Mantissa: Neg, Exponent> Neg for Float<Mantissa, Exponent> {type Output = Float<Mantissa::Output, Exponent>;fn neg(self) -> Self::Output {Float::new()}
}// -Var<T> = Var<-T>
impl<T: Primitive + Neg> Neg for Var<T> {type Output = Var<<T as Neg>::Output>;#[inline(always)]fn neg(self) -> Self::Output {Var(-self.0)}
}

二、源码分析

  1. 导入依赖
use core::ops::{Not, Neg};
use crate::number::{Z0, P1, N1, B0, B1, NonZero, Float, Var, PrimitiveInt, Primitive};
  • Neg trait:这是Rust标准库中的算术取负运算符trait。

  • Not trait:这是Rust标准库中的位取反运算符trait(虽然这里主要用Neg,但Not可能在其他地方用到)。

  • 其他类型(Z0, P1, N1, B0, B1, Float, Var等)是自定义的类型,用于表示不同的数字或数值。

  1. 基础类型实现
    这部分实现了基本数字类型的算术取负运算。
a. 零取负
impl Neg for Z0 { // Z0 (0) 的取负type Output = Z0;fn neg(self) -> Self::Output {Z0}
}
  • Z0表示数字0。

  • 算术取负运算-0的结果仍然是0(用Z0表示)。

b. 正一取负
impl Neg for P1 {type Output = N1;fn neg(self) -> Self::Output {N1}
}
  • P1表示数字1。

  • 算术取负运算-1的结果是-1(用N1表示)。

c. 负一取负
impl Neg for N1 { // N1 (-1) 的取负,用P1替换B1<Z0>type Output = P1;fn neg(self) -> Self::Output {P1}
}
  • N1表示数字-1。

  • 算术取负运算-(-1)的结果是1(用P1表示)。

  1. 递归类型实现
    这部分处理二进制数的算术取负,通过递归的方式处理更高位的二进制数。
a. 二进制0前缀取负
impl<H: NonZero + Neg> Neg for B0<H>{type Output = B0<H::Output>;fn neg(self) -> Self::Output {B0::new()}
}
  • B0表示一个以0为前缀的二进制数(例如B0表示10,即-2)。

取负后变为B0<-H>(即前缀0保持不变,剩余部分取负)。

b. 二进制1前缀取负

impl<H: NonZero + Not> Neg for B1<H>{type Output = B1<H::Output>;fn neg(self) -> Self::Output {B1::new()}
}
  • B1表示一个以1为前缀的二进制数(例如B1表示01,即1)。

  • 取负后变为B1<!H>(即前缀1保持不变,剩余部分取反,这里用Not可能是为了优化或特殊处理)。

  1. 浮点数取负
impl<Mantissa: Neg, Exponent> Neg for Float<Mantissa, Exponent> {type Output = Float<Mantissa::Output, Exponent>;fn neg(self) -> Self::Output {Float::new()}
}
  • Float<Mantissa, Exponent>表示一个浮点数,由尾数(Mantissa)和指数(Exponent)组成。

  • 取负运算只对尾数部分取负,指数部分保持不变。

  1. 变量类型取负
impl<T: Primitive + Neg> Neg for Var<T> {type Output = Var<<T as Neg>::Output>;#[inline(always)]fn neg(self) -> Self::Output {Var(-self.0)}
}
  • Var是一个包装类型,用于表示变量。

  • 如果T实现了Neg,则Var也可以进行算术取负运算,结果为Var<-T>。

三、总结

这段代码实现了一个类型级别的算术取负运算系统,支持:

  1. 基本数字(0、1、-1)的取负。

  2. 二进制数的递归取负(B0和B1)。

  3. 浮点数的取负(只对尾数取负)。

  4. 变量类型的取负。

这种技术常用于类型安全的数学库或嵌入式DSL(领域特定语言)中,确保编译期就能完成某些计算或检查。

http://www.lryc.cn/news/572094.html

相关文章:

  • Kafka副本机制源码深度剖析:从数据同步到故障转移
  • craw14ai 框架的入门讲解和实战指南——基于Python的智能爬虫框架,集成AI(如NLP/OCR)实现自动化数据采集与处理
  • 3.1 Hector_mapping初体验
  • 前端如何通过 Blob 下载 Excel 文件
  • 容器运行时保护:用Falco构建云原生安全防线
  • CFG的前世今生
  • Docker 日志
  • 技术文章大纲:SpringBoot自动化部署实战
  • 《状压DP(01矩阵约束问题)》基础概念
  • 计算机网络:(五)信道复用技术,数字传输系统,宽带接入技术
  • 03 面试官考察与 CAP 有关的分布式理论
  • 开源ChatBI :深入解密 Spring AI Alibaba 的中文NL2SQL智能引擎
  • 基于RAGFlow构建Text2SQL的实战教程
  • 密室出逃消消乐小游戏微信流量主小程序开源
  • 如何将文件从安卓设备传输到电脑?
  • XMOS基于边缘AI+DSP+MCU+I/O智算芯片的音频解决方案矩阵引领行业创新潮流
  • 吴恩达机器学习笔记:正则化2
  • 从Excel到知识图谱再到数据分析:数据驱动智能体构建指南
  • SCRM软件数据分析功能使用指南:从数据挖掘到商业决策
  • Spark 技术与实战学习心得:从入门到实践的深度探索
  • CppCon 2017 学习:Effective Qt: 2017 Edition
  • 锂电池保护板测试仪:守护电池安全的幕后保障
  • 【css】设置了margin-top为负数,div被img覆盖的解决方法
  • django调用 paramiko powershell 获取cpu 个数
  • IPv4编址及IPv4路由基础
  • Pinia + Vue Router 权限控制(终极完整版)
  • 无监督学习中的特征选择与检测(FSD)在医疗动线流程优化中的应用
  • 2025-05-05-80x86汇编语言环境配置
  • 使用随机森林实现目标检测
  • AI时代SEO关键词革新