【PhysUnits】4.5 负数类型(Neg<P>)算术运算(negative.rs)
一、源码
代码实现了一个类型级别的负数系统,允许在编译期进行负数运算的类型检查。
//! 负数类型及其算术运算实现
//! Negative number type and its arithmetic operations implementation
//!
//! 本模块定义了类型系统中的负数类型,并为其实现了基本算术运算。
//! This module defines the negative number type in the type system and implements basic arithmetic operations for it.
//!
//! 注意:与零相关的运算(如 N + 0, N - 0 等)在 zero.rs 模块中实现。
//! Note: Operations involving zero (e.g. N + 0, N - 0, etc.) are implemented in the zero.rs module.use core::ops::{Add, Sub, Mul, Div};
use Positive; // 正数类型 / Positive number type
use NonZero; // 非零类型 / Non-zero type
use Integer; // 整数类型 / Integer typeuse crate::sealed::Sealed;/// 实现Sealed trait表示这是一个私有类型
/// Implementing Sealed trait indicates this is a private type
impl<P: Positive> Sealed for Neg<P> {}/// 负数类型是非零类型
/// Negative numbers are non-zero types
impl<P: Positive> NonZero for Neg<P> {}/// 负数类型,P是对应的正数类型
/// Negative number type, where P is the corresponding positive type
///
/// # 示例
/// # Example
/// ```
/// use type_arithmetic::{Neg, P1};
///
/// type N1 = Neg<P1>; // -1
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct Neg<P: Positive>(PhantomData<P>);// ========== 加法运算实现 ==========
// ========== Addition Operations ==========/// 负数相加: -a + -b = -(a + b)
/// Negative addition: -a + -b = -(a + b)
///
/// 注意:与零相加的实现在zero.rs中
/// Note: Addition with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Add<Neg<P2>> for Neg<P1>
whereP1: Add<P2>,
{type Output = Neg<<P1 as Add<P2>>::Output>;#[inline]fn add(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}/// 负数加正数: -a + b = b - a
/// Negative plus positive: -a + b = b - a
///
/// 结果可能是正数或负数,取决于b和a的大小关系
/// Result could be positive or negative, depending on the relative sizes of b and a
impl<P1: Positive, P2: Positive> Add<P2> for Neg<P1>
whereP2: Sub<P1>,<P2 as Sub<P1>>::Output: Integer,
{type Output = <P2 as Sub<P1>>::Output;#[inline]fn add(self, _rhs: P2) -> Self::Output {Self::Output::default()}
}/// 正数加负数: a + (-b) = a - b
/// Positive plus negative: a + (-b) = a - b
///
/// 结果可能是正数或负数,取决于a和b的大小关系
/// Result could be positive or negative, depending on the relative sizes of a and b
impl<P1: Positive, P2: Positive> Add<Neg<P2>> for P1
whereP1: Sub<P2>,<P1 as Sub<P2>>::Output: Integer,
{type Output = <P1 as Sub<P2>>::Output;#[inline]fn add(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}// ========== 减法运算实现 ==========
// ========== Subtraction Operations ==========/// 负数相减: -a - (-b) = b - a
/// Negative subtraction: -a - (-b) = b - a
///
/// 结果可能是正数或负数,取决于b和a的大小关系
/// Result could be positive or negative, depending on the relative sizes of b and a
///
/// 注意:与零相减的实现在zero.rs中
/// Note: Subtraction with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Sub<Neg<P2>> for Neg<P1>
whereP2: Sub<P1>,<P2 as Sub<P1>>::Output: Integer,
{type Output = <P2 as Sub<P1>>::Output;#[inline]fn sub(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数减正数: -a - b = -(a + b)
/// Negative minus positive: -a - b = -(a + b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Sub<P2> for Neg<P1>
whereP1: Add<P2>,
{type Output = Neg<<P1 as Add<P2>>::Output>;#[inline]fn sub(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数减负数: a - (-b) = a + b
/// Positive minus negative: a - (-b) = a + b
///
/// 结果总是正数
/// Result is always positive
impl<P1: Positive, P2: Positive> Sub<Neg<P2>> for P1
whereP1: Add<P2>,
{type Output = <P1 as Add<P2>>::Output;#[inline]fn sub(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}// ========== 乘法运算实现 ==========
// ========== Multiplication Operations ==========/// 负数相乘: (-a) * (-b) = a * b
/// Negative multiplication: (-a) * (-b) = a * b
///
/// 负负得正
/// Negative times negative equals positive
///
/// 注意:与零相乘的实现在zero.rs中
/// Note: Multiplication with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Mul<Neg<P2>> for Neg<P1>
whereP1: Mul<P2>,
{type Output = <P1 as Mul<P2>>::Output;#[inline]fn mul(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数乘正数: (-a) * b = -(a * b)
/// Negative times positive: (-a) * b = -(a * b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Mul<P2> for Neg<P1>
whereP1: Mul<P2>,<P1 as Mul<P2>>::Output: Positive,
{type Output = Neg<<P1 as Mul<P2>>::Output>;#[inline]fn mul(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数乘负数: a * (-b) = -(a * b)
/// Positive times negative: a * (-b) = -(a * b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Mul<Neg<P2>> for P1
whereP1: Mul<P2>,<P1 as Mul<P2>>::Output: Positive,
{type Output = Neg<<P1 as Mul<P2>>::Output>;#[inline]fn mul(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}// ========== 除法运算实现 ==========
// ========== Division Operations ==========/// 负数相除: (-a) / (-b) = a / b
/// Negative division: (-a) / (-b) = a / b
///
/// 负负得正
/// Negative divided by negative equals positive
///
/// 注意:除以零和零除以负数的实现在zero.rs中
/// Note: Division by zero and zero divided by negative are implemented in zero.rs
impl<P1: Positive, P2: Positive> Div<Neg<P2>> for Neg<P1>
whereP1: Div<P2>,
{type Output = <P1 as Div<P2>>::Output;#[inline]fn div(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数除正数: (-a) / b = -(a / b)
/// Negative divided by positive: (-a) / b = -(a / b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Div<P2> for Neg<P1>
whereP1: Div<P2>,<P1 as Div<P2>>::Output: Positive,
{type Output = Neg<<P1 as Div<P2>>::Output>;#[inline]fn div(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数除负数: a / (-b) = -(a / b)
/// Positive divided by negative: a / (-b) = -(a / b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Div<Neg<P2>> for P1
whereP1: Div<P2>,<P1 as Div<P2>>::Output: Positive,
{type Output = Neg<<P1 as Div<P2>>::Output>;#[inline]fn div(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}
二、基本结构
类型定义
pub struct Neg<P: Positive>(PhantomData<P>);
-
Neg
表示负数类型,其中P是相应的正数类型
-
使用PhantomData来持有类型参数但不占用运行时空间
-
例如Neg表示-1,其中P1表示正数1
标记trait实现
impl<P: Positive> Sealed for Neg<P> {}
impl<P: Positive> NonZero for Neg<P> {}
-
Sealed trait防止外部代码实现这些trait
-
NonZero标记表示负数永远是非零值
三、算术运算实现
加法运算
// -a + -b = -(a + b)
impl<P1, P2> Add<Neg<P2>> for Neg<P1> where P1: Add<P2> {type Output = Neg<<P1 as Add<P2>>::Output>;
}// -a + b = b - a
impl<P1, P2> Add<P2> for Neg<P1> where P2: Sub<P1> {type Output = <P2 as Sub<P1>>::Output;
}// a + (-b) = a - b
impl<P1, P2> Add<Neg<P2>> for P1 where P1: Sub<P2> {type Output = <P1 as Sub<P2>>::Output;
-
实现了三种加法组合:负数+负数、负数+正数、正数+负数
-
每种情况都遵循数学规则并返回正确的类型
减法运算
// -a - (-b) = b - a
impl<P1, P2> Sub<Neg<P2>> for Neg<P1> where P2: Sub<P1> {type Output = <P2 as Sub<P1>>::Output;
}// -a - b = -(a + b)
impl<P1, P2> Sub<P2> for Neg<P1> where P1: Add<P2> {type Output = Neg<<P1 as Add<P2>>::Output>;
}// a - (-b) = a + b
impl<P1, P2> Sub<Neg<P2>> for P1 where P1: Add<P2> {type Output = <P1 as Add<P2>>::Output;
-
实现了三种减法组合
-
特别注意-a - (-b)转换为b - a的巧妙处理
乘法运算
// (-a) * (-b) = a * b
impl<P1, P2> Mul<Neg<P2>> for Neg<P1> where P1: Mul<P2> {type Output = <P1 as Mul<P2>>::Output;
}// (-a) * b = -(a * b)
impl<P1, P2> Mul<P2> for Neg<P1> where P1: Mul<P2> {type Output = Neg<<P1 as Mul<P2>>::Output>;
}// a * (-b) = -(a * b)
impl<P1, P2> Mul<Neg<P2>> for P1 where P1: Mul<P2> {type Output = Neg<<P1 as Mul<P2>>::Output>;
-
完全遵循数学乘法规则
-
负负得正,正负得负
除法运算
// (-a) / (-b) = a / b
impl<P1, P2> Div<Neg<P2>> for Neg<P1> where P1: Div<P2> {type Output = <P1 as Div<P2>>::Output;
}// (-a) / b = -(a / b)
impl<P1, P2> Div<P2> for Neg<P1> where P1: Div<P2> {type Output = Neg<<P1 as Div<P2>>::Output>;
}// a / (-b) = -(a / b)
impl<P1, P2> Div<Neg<P2>> for P1 where P1: Div<P2> {type Output = Neg<<P1 as Div<P2>>::Output>;
-
遵循数学除法规则
-
特别注意没有实现除以零的情况
四、设计特点
-
类型安全:所有运算在编译期进行类型检查
-
零开销:使用PhantomData,运行时无额外开销
-
数学正确:严格遵循数学运算规则
-
模块化设计:与零相关的运算分离到zero.rs模块
-
完整文档:包含中英文注释和示例
五、使用示例
type N1 = Neg<P1>; // -1
type N2 = Neg<P2>; // -2// 类型级别运算
type Sum = <N1 as Add<N2>>::Output; // -1 + -2 = -3 (Neg<P3>)
type Diff = <N1 as Sub<P2>>::Output; // -1 - 2 = -3 (Neg<P3>)
type Product = <N1 as Mul<N2>>::Output; // -1 * -2 = 2 (P2)
这个实现展示了Rust类型系统的强大能力,可以在编译期捕获数学运算的类型约束和规则。