【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.pswp.cn/news/907805.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

npm install命令都做了哪些事情

npm install&#xff08;或其简写 npm i&#xff09;是 Node.js 项目中最重要的命令之一&#xff0c;它负责安装项目所需的所有依赖项。下面我将详细解释这个命令的完整执行过程和底层机制&#xff0c;让你彻底理解它背后的工作原理。 一、npm install 的完整工作流程 1. 依赖…

mkdir: cannot create directory ‘gitlab-stu’: No space left on device

Linux中创建目录时报错“mkdir: cannot create directory ‘gitlab-stu’: No space left on device”&#xff0c;磁盘空间不足。 使用df命令查看&#xff0c;发现 / 下面use%占满了&#xff1a; 查看inode使用情况&#xff1a; 可以看到docker的数据大部分存放在/var/lib/do…

中国高分辨率高质量地面NO2数据集(2008-2023)

时间分辨率&#xff1a;日空间分辨率&#xff1a;1km - 10km共享方式&#xff1a;开放获取数据大小&#xff1a;15.36 GB数据时间范围&#xff1a;2008-01-01 — 2023-12-31元数据更新时间&#xff1a;2024-08-19 数据集摘要 ChinaHighNO2数据集是中国高分辨率高质量近地表空气…

Redis实战-基于redis和lua脚本实现分布式锁以及Redission源码解析【万字长文】

前言&#xff1a; 在上篇博客中&#xff0c;我们探讨了单机模式下如何通过悲观锁&#xff08;synchronized&#xff09;实现"一人一单"功能。然而&#xff0c;在分布式系统或集群环境下&#xff0c;单纯依赖JVM级别的锁机制会出现线程并发安全问题&#xff0c;因为这…

剪枝中的 `break` 与 `return` 区别详解

在回溯算法的剪枝操作中&#xff1a; if (sum candidates[i] > target) break;这个 break 既不等效于 return&#xff0c;也不会终止整个回溯过程。它只会终止当前层循环的后续迭代&#xff0c;而不会影响其他分支的回溯。让我用图解和示例详细说明&#xff1a; &#x1…

计算机网络第1章(下):网络性能指标与分层模型全面解析

目录 一、计算机网络的性能指标1.1 性能指标1&#xff1a;速率1.2 性能指标2&#xff1a;带宽1.3 性能指标3&#xff1a;吞吐量1.4 性能指标4&#xff1a;时延1.5 性能指标5&#xff1a;时延带宽积1.6 性能指标6&#xff1a;往返时延1.7 性能指标7&#xff1a;信道利用率 二、计…

C#数字图像处理(二)

文章目录 1.灰度直方图1.1 灰度直方图定义1.2 灰度直方图编程实例 2.线性点运算2.1线性点运算定义2.2 线性点运算编程实例 3.全等级直方图灰度拉伸3.1 灰度拉伸定义3.2 灰度拉伸编程实例 4.直方图均衡化4.1 直方图均衡化定义4.2 直方图均衡化编程实例 5.直方图匹配5.1 直方图匹…

训练中常见的运动强度分类

概述 有氧运动是耐力基础&#xff0c;乳酸阈值是耐力突破的关键&#xff0c;提升乳酸阈值可以延缓疲劳&#xff0c;无氧运动侧重速度和力量&#xff0c;混氧和最大摄氧量用于细化训练强度和评估潜力。 分类强度供能系统乳酸浓度训练目标有氧运动低&#xff08;60%-80% HR&…

数智管理学(十五)

第五章 数智化时代的组织结构模型 第一节 传统金字塔型结构向分布式网络型的演变 在当今数智化时代&#xff0c;企业所处的市场环境发生了翻天覆地的变化&#xff0c;技术创新日新月异&#xff0c;客户需求日益多样化和个性化&#xff0c;市场竞争愈发激烈。传统的金字塔型组…

AAA基础配置

文章目录 组网需求组网拓扑实验步骤测试结果配置文件 组网需求 为组网安全&#xff0c;经常会使用AAA技术&#xff0c;本次以CE12800交换机Window为例&#xff0c;实现AAA本地认证登录 组网拓扑 实验步骤 配置接口IP&#xff0c;连通终端进入AAA视图配置用户名密码配置账户权…

基于微信小程序的云校园信息服务平台设计与实现(源码+定制+开发)云端校园服务系统开发 面向师生的校园事务小程序设计与实现 融合微信生态的智慧校园管理系统开发

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…

RV1126-OPENCV Mat理解和AT函数

一.Mat概念 Mat 是整个图像存储的核心也是所有图像处理的最基础的类&#xff0c;Mat 主要存储图像的矩阵类型&#xff0c;包括向量、矩阵、灰度或者彩色图像等等。Mat由两部分组成&#xff1a;矩阵头&#xff0c;矩阵数据。矩阵头是存储图像的长度、宽度、色彩信息等头部信息&a…

23、Swift框架微调实战(3)-Qwen2.5-VL-7B LORA微调OCR数据集

一、模型介绍 Qwen2.5-VL 是阿里通义千问团队开源的视觉语言模型,具有3B、7B和72B三种不同规模,能够识别常见物体、分析图像中的文本、图表等元素,并具备作为视觉Agent的能力。 Qwen2.5-VL 具备作为视觉Agent的能力,可以推理并动态使用工具,初步操作电脑和手机。在视频处…

能按需拆分 PDF 为多个文档的工具

软件介绍 彩凤 PDF 拆分精灵是一款具备 PDF 拆分功能的软件。 功能特点 PDF 拆分功能较为常见&#xff0c;很多 PDF 软件都具备&#xff0c;例如 DC 软件提取 PDF 较为方便&#xff0c;但它不能从一个 PDF 里提取出多个 PDF。据印象&#xff0c;其他 PDF 软件也似乎没有能从…

Apache Kafka 实现原理深度解析:生产、存储与消费全流程

Apache Kafka 实现原理深度解析&#xff1a;生产、存储与消费全流程 引言 Apache Kafka 作为分布式流处理平台的核心&#xff0c;其高吞吐、低延迟、持久化存储的设计使其成为现代数据管道的事实标准。本文将从消息生产、持久化存储、消息消费三个阶段拆解 Kafka 的核心实现原…

【Vue 3全栈实战】从组合式API到企业级架构设计

目录 &#x1f31f; 前言&#x1f3d7;️ 技术背景与价值&#x1fa79; 当前技术痛点&#x1f6e0;️ 解决方案概述&#x1f465; 目标读者说明 &#x1f9e0; 一、技术原理剖析&#x1f4ca; 核心概念图解&#x1f4a1; 核心作用讲解&#x1f527; 关键技术模块说明⚖️ 技术选…

支持功能安全ASIL-B的矩阵管理芯片IS32LT3365,助力ADB大灯系统轻松实现功能安全等级

随着自动驾驶技术的快速发展&#xff0c;汽车前灯智能化也越来越高。自适应远光灯 (ADB) 作为一种智能照明系统&#xff0c;在提升驾驶安全性和舒适性方面发挥着重要作用。ADB 系统通过摄像头和传感器获取前方道路信息&#xff0c;例如来车的位置、距离和速度&#xff0c;并根据…

基于 Flickr30k-Entities 数据集 的 Phrase Localization

以下示例基于 Flickr30k-Entities 数据集中的标注&#xff0c;以及近期&#xff08;以 TransVG &#xff08;Li et al. 2021&#xff09;为例&#xff09;在短语定位&#xff08;Phrase Grounding&#xff09;任务上的评测结果&#xff0c;展示了单张图片中若干名词短语的定位情…

Java Spring Boot 自定义注解详解与实践

目录 一、自定义注解的场景与优势1.1 场景1.2 优势 二、创建自定义注解2.1 定义注解2.2 创建注解处理器 三、使用自定义注解3.1 在业务方法上使用注解3.2 配置类加载注解 四、总结 在 Spring Boot 中&#xff0c;自定义注解为我们提供了一种灵活且强大的方式来简化开发、增强代…

YOLOv5 环境配置指南

系统要求 Windows/Linux/MacOSNVIDIA GPU (推荐) 或 CPUPython 3.8CUDA 11.8 (如果使用 GPU) 安装步骤 1. 安装 Conda 如果还没有安装 Conda&#xff0c;请先从官网下载并安装 Miniconda。 2. 创建虚拟环境 # 创建名为 yolov5 的新环境&#xff0c;使用 Python 3.8 conda…