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

【PhysUnits】15.8 引入P1后的减法运算(sub.rs)

一、源码

这段代码实现了一个类型级别的二进制数减法系统,包含标准减法和带借位减法。

use core::ops::{Neg, Not, Sub};
use super::basic::{Z0, N1, P1, B0, B1, Integer, NonZero};
use super::add1::Add1;
use super::sub1::Sub1;
use super::standardization::{IfB0, IfB1};// ==================== 带借位减法 Trait ====================
/// 带借位减法运算
/// Subtraction with borrow operation
///
/// 表示 a - b - 1 的运算 (相当于 a - (b + 1))
/// Represents the operation of a - b - 1 (equivalent to a - (b + 1))
/// 说明:有借位表示有低位数,本位 NonZero
pub trait SubWithBorrow<Rhs> {type Output;
}// ==================== 带借位减法实现 ====================// ========== 带借位P1 - NonZero ==========// P1 - I (带借位) = P1-I-1=-I
impl<I:NonZero + Neg> SubWithBorrow<I> for P1 {type Output = I::Output;
}// ========== 带借位N1 - NonZero ==========
// N1 - I (带借位,非0) = !I-1 (即N1+(!I+1)-1)
impl<I: NonZero + Not> SubWithBorrow<I> for N1
where<I as Not>::Output:Sub1,
{type Output = <I::Output as Sub1>::Output;
}// ========== 带借位B0 - NonZero ==========
// B0 - P1 (带借位)
impl<H: NonZero + Sub1> SubWithBorrow<P1> for B0<H>
whereH::Output: IfB0,
{type Output = <H::Output as IfB0>::Output;
}// B0 - N1 (带借位)
impl<H: NonZero> SubWithBorrow<N1> for B0<H>
{type Output = Self;
}// B0 - B0 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B0<H2>> for B0<H1>
whereH1::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;
}// B0 - B1 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B1<H2>> for B0<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;
}// ========== 带借位B1 - NonZero ==========
// B1 - P1 (带借位)
impl<H: NonZero + Sub1> SubWithBorrow<P1> for B1<H>
whereH::Output: IfB1,
{type Output = <H::Output as IfB1>::Output;
}// B1 - N1 (带借位)
impl<H: NonZero> SubWithBorrow<N1> for B1<H>
{type Output = Self;
}// B1 - B0 (带借位)
impl<H1: NonZero + Sub<H2>, H2: NonZero> SubWithBorrow<B0<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;
}// B1 - B1 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B1<H2>> for B1<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;
}// ==================== 标准减法实现 (Sub trait) ====================// ========== Z0 - All ==========
// Z0 - I = -I
impl<I: Integer + Neg> Sub<I> for Z0 {type Output = I::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== P1 - All ==========
// P1 - I = -(I-P1)
impl<I: Integer + Sub1> Sub<I> for P1
where<I as Sub1>::Output: Neg,
{type Output = <I::Output as Neg>::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== N1 - All ==========
// N1 - I = -(I+P1)
impl<I: Integer + Add1> Sub<I> for N1
where<I as Add1>::Output: Neg,
{type Output = <I::Output as Neg>::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== B0 - All ==========
// B0 - Z0
impl<H: NonZero> Sub<Z0> for B0<H> {type Output = Self;fn sub(self, _: Z0) -> Self::Output {self}
}// B0 - P1
impl<H: NonZero + Sub1> Sub<P1> for B0<H>
where<H as Sub1>::Output: IfB1,
{type Output = <H::Output as IfB1>::Output;fn sub(self, _: P1) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - N1
impl<H: NonZero + IfB1> Sub<N1> for B0<H>{type Output = H::Output;fn sub(self, _: N1) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - B0
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B0<H2>> for B0<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;fn sub(self, _: B0<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - B1 (需要借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> Sub<B1<H2>> for B0<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;fn sub(self, _: B1<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// ========== B1 - All ==========
// B1-Z0
impl<H: NonZero> Sub<Z0> for B1<H> {type Output = Self;fn sub(self, _: Z0) -> Self::Output {self}
}// B1 - P1
impl<H: NonZero + IfB0> Sub<P1> for B1<H> {type Output = H::Output;fn sub(self, _: P1) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - N1
impl<H: NonZero + Add1> Sub<N1> for B1<H>
where <H as Add1>::Output: IfB0,
{type Output = <H::Output as IfB0>::Output;fn sub(self, _: N1) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - B0
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B0<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;fn sub(self, _: B0<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - B1
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B1<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;fn sub(self, _: B1<H2>) -> Self::Output {unreachable!("Type-level operation")}
}

二、核心设计

类型系统基础
use super::basic::{Z0, N1, P1, B0, B1};  // 基础类型:
// Z0 = 零, N1 = -1, P1 = +1
// B0<H> = 二进制数低位为0 (如 10)
// B1<H> = 二进制数低位为1 (如 11)
关键Trait定义
pub trait SubWithBorrow<Rhs> {  // 带借位减法(a-b-1)type Output;
}impl core::ops::Sub<Rhs> {      // 标准减法(a-b)type Output;
}

三、带借位减法实现

特殊值处理
// P1 - I (带借位) = -I
impl<I: NonZero + Neg> SubWithBorrow<I> for P1 {type Output = I::Output;  // 直接取负
}// N1 - I (带借位) = !I - 1
impl<I: NonZero + Not> SubWithBorrow<I> for N1 
whereI::Output: Sub1  // 按位取反后减1
{type Output = <I::Output as Sub1>::Output;
}
二进制数处理
// B0 - B1 的递归借位处理
impl<H1, H2> SubWithBorrow<B1<H2>> for B0<H1>
whereH1: SubWithBorrow<H2>,      // 高位递归H1::Output: IfB1            // 结果标准化
{type Output = B1<H1::Output>;  // 借位后变B1
}// B1 - B1 的递归处理
impl<H1, H2> SubWithBorrow<B1<H2>> for B1<H1>
whereH1: Sub<H2>,                // 普通减法H1::Output: IfB0            // 结果标准化
{type Output = B0<H1::Output>;
}

四、标准减法实现

运算符重载模式
impl<I> Sub<I> for Z0 {  // 零减任何数type Output = I::Neg; // 直接取负fn sub(self, _: I) -> Self::Output { unreachable!() }
}impl<H> Sub<Z0> for B0<H> {  // 二进制数减零type Output = Self;fn sub(self, _: Z0) -> Self { self }
}
递归减法
// B1 - B0 的递归处理
impl<H1, H2> Sub<B0<H2>> for B1<H1>
whereH1: Sub<H2>,                // 高位相减H1::Output: IfB1            // 结果标准化
{type Output = B1<H1::Output>;
}

五、标准化处理

通过 IfB0/IfB1 trait 处理特殊情况:

B0<Z0> => Z0   // 消除前导零
B1<N1> => N1   // 二进制补码优化
B1<Z0> => P1   // 单比特正数

六、设计亮点

  1. 双重减法系统:
  • 标准减法:a - b

  • 带借位减法:a - b - 1(处理低位借位)

  1. 完备的数学规则:
// 负数减法转换为加法
impl<I: Add1> Sub<I> for N1 {type Output = Neg<I::Add1>;
}
  1. 递归类型处理:
// 处理二进制数的递归减法
impl<H1, H2> Sub<B1<H2>> for B1<H1> {type Output = B0<H1::Sub<H2>>;
}

七、典型运算示例

type A = B1<B0<Z0>>;  // 二进制10(2)
type B = B1<Z0>;      // 二进制1(1)
type Diff = <A as Sub<B>>::Output;  // 得到P1 (1)// 带借位减法示例
type C = B0<B1<Z0>>;  // 二进制01(1)
type D = B1<Z0>;      // 二进制1(1)
type BorrowDiff = <C as SubWithBorrow<D>>::Output;  // 得到N1 (-1)

八、异常处理机制

所有运行时方法都标记为:

fn sub(self, _: Rhs) -> Self::Output {unreachable!("Type-level operation")  // 确保不会实际执行
}

这个系统通过编译期类型计算实现了:

  • 任意长度二进制数减法

  • 自动借位处理

  • 结果标准化

  • 零运行时开销

适用于需要编译期数值验证的场景,如协议编解码等。

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

相关文章:

  • 图文详解Java集合面试题
  • TDengine 基于 TDgpt 的 AI 应用实战
  • 【论文阅读 | PR 2024 |ICAFusion:迭代交叉注意力引导的多光谱目标检测特征融合】
  • vue3中的ref和reactive
  • pc端小卡片功能-原生JavaScript金融信息与节日日历
  • 2024 CKA模拟系统制作 | Step-By-Step | 16、题目搭建-sidecar 代理容器日志
  • 工作流引擎-06-流程引擎(Process Engine)对比 Flowable、Activiti 与 Camunda 全维度对比分析
  • 一位汽车行业从业人员对Simulink热度变化的观察与讨论 (2024)
  • 中国风展示工作总结商务通用PPT模版
  • M-OFDM模糊函数原理及仿真
  • 过滤攻击-聚合数据
  • [Windows]在Win上安装bash和zsh - 一个脚本搞定
  • Maven(黑马)
  • YOLOv7 辅助检测头与重参数化解析2025.6.1
  • 鸿蒙HarmonyOS —(cordova)研发方案详解
  • 数论——质数和合数及求质数
  • 工程的焊接技术
  • 哈尔滨工业大学提出ADSUNet—红外暗弱小目标邻帧检测新框架
  • Altium Disigner(16.1)学习-原理图绘制以及必要操作
  • 批量导出CAD属性块信息生成到excel——CAD C#二次开发(插件实现)
  • Leetcode 3568. Minimum Moves to Clean the Classroom
  • DAY 35 超大力王爱学Python
  • 用Python实现一个简单的远程桌面服务端和客户端
  • xPSR
  • 通俗易懂的 JS DOM 操作指南:从创建到挂载
  • Python进阶与常用库:探索高效编程的奥秘
  • Redis-6.2.9 Sentinel 哨兵配置
  • WSL2 安装与Docker安装
  • 基于微信小程序的scratch学习系统
  • 图像数据如何表示为概率单纯形