【unitrix】 2.2 类型级浮点数特殊值NotANumber(not_a_number.rs)

一、源码

这段代码为 Rust 中的 NotANumber 类型实现了各种算术运算。

/// 类型系统编译时运行,后期可能将NotANumber修改为错误类型
/// The type system runs at compile time, NotANumber may be changed to an error type later

use core::ops::{Add, Sub, Mul, Div, Rem, Neg};
use crate::number::{NotANumber, TypedNum, Var, Primitive};

// ==============================================
// NotANumber 算术运算实现
// NotANumber Arithmetic Implementations
// ==============================================

// ----------------------------
// 一元负号运算实现
// Unary negation operation implementation
// ----------------------------
impl Neg for NotANumber {
    type Output = NotANumber;
    
    /// 对NotANumber取负,结果仍为NotANumber
    /// Negating NotANumber still yields NotANumber
    fn neg(self) -> Self::Output {
        NotANumber
    }
}

// ==============================================
// NotANumber 加法运算实现
// NotANumber Addition Implementations
// ==============================================

// NotANumber + TypedNum
impl<F: TypedNum> Add<F> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 与任何类型数字相加,结果为 NotANumber
    /// Adding NotANumber with any typed number results in NotANumber
    #[inline(always)]
    fn add(self, _rhs: F) -> Self::Output {
        NotANumber
    }
}

// NotANumber + Var
impl<T: Primitive> Add<Var<T>> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 与变量相加,结果为 NotANumber
    /// Adding NotANumber with a variable results in NotANumber
    #[inline(always)]
    fn add(self, _rhs: Var<T>) -> Self::Output {
        NotANumber
    }
}

// ==============================================
// NotANumber 减法运算实现
// NotANumber Subtraction Implementations
// ==============================================

// NotANumber - TypedNum
impl<F: TypedNum> Sub<F> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 减去任何类型数字,结果为 NotANumber
    /// Subtracting any typed number from NotANumber results in NotANumber
    #[inline(always)]
    fn sub(self, _rhs: F) -> Self::Output {
        NotANumber
    }
}

// NotANumber - Var
impl<T: Primitive> Sub<Var<T>> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 减去变量,结果为 NotANumber
    /// Subtracting a variable from NotANumber results in NotANumber
    #[inline(always)]
    fn sub(self, _rhs: Var<T>) -> Self::Output {
        NotANumber
    }
}

// ==============================================
// NotANumber 乘法运算实现
// NotANumber Multiplication Implementations
// ==============================================

// NotANumber * TypedNum
impl<F: TypedNum> Mul<F> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 乘以任何类型数字,结果为 NotANumber
    /// Multiplying NotANumber by any typed number results in NotANumber
    #[inline(always)]
    fn mul(self, _rhs: F) -> Self::Output {
        NotANumber
    }
}

// NotANumber * Var
impl<T: Primitive> Mul<Var<T>> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 乘以变量,结果为 NotANumber
    /// Multiplying NotANumber by a variable results in NotANumber
    #[inline(always)]
    fn mul(self, _rhs: Var<T>) -> Self::Output {
        NotANumber
    }
}

// ==============================================
// NotANumber 除法运算实现
// NotANumber Division Implementations
// ==============================================

// NotANumber / TypedNum
impl<F: TypedNum> Div<F> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 除以任何类型数字,结果为 NotANumber
    /// Dividing NotANumber by any typed number results in NotANumber
    #[inline(always)]
    fn div(self, _rhs: F) -> Self::Output {
        NotANumber
    }
}

// NotANumber / Var
impl<T: Primitive> Div<Var<T>> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 除以变量,结果为 NotANumber
    /// Dividing NotANumber by a variable results in NotANumber
    #[inline(always)]
    fn div(self, _rhs: Var<T>) -> Self::Output {
        NotANumber
    }
}

// ==============================================
// NotANumber 取余运算实现
// NotANumber Remainder Implementations
// ==============================================

// NotANumber % TypedNum
impl<F: TypedNum> Rem<F> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 对任何类型数字取余,结果为 NotANumber
    /// Remainder of NotANumber divided by any typed number is NotANumber
    #[inline(always)]
    fn rem(self, _rhs: F) -> Self::Output {
        NotANumber
    }
}

// NotANumber % Var
impl<T: Primitive> Rem<Var<T>> for NotANumber {
    type Output = NotANumber;
    
    /// NotANumber 对变量取余,结果为 NotANumber
    /// Remainder of NotANumber divided by a variable is NotANumber
    #[inline(always)]
    fn rem(self, _rhs: Var<T>) -> Self::Output {
        NotANumber
    }
}

二、代码分析

  1. 基本概念
  • NotANumber 是一个表示"非数字"的特殊类型,类似于浮点数中的 NaN(Not a Number)

  • 类型系统在编译时运行,未来可能将 NotANumber 改为错误类型

  1. 实现的操作
    代码为 NotANumber 实现了以下操作(通过 trait 实现):
  • 一元负号运算 (Neg)

  • 加法 (Add)

  • 减法 (Sub)

  • 乘法 (Mul)

  • 除法 (Div)

  • 取余 (Rem)

  1. 实现特点
    对于所有运算,规则都是一致的:
  • 无论操作数是什么(TypedNum 或 Var)

  • 无论进行什么运算

  • 结果永远是 NotANumber

  1. 具体实现细节
  • 每个操作都实现了对应的 trait(如 Add、Sub 等)

  • 每种操作都有两个实现:

    • 与 TypedNum(任何实现了 TypedNum trait 的类型)的运算

    • 与 Var(其中 T 需要实现 Primitive trait)的运算

  • 所有方法都用 #[inline(always)] 标记,提示编译器总是内联这些简单操作

  • 操作都直接忽略右操作数 (_rhs),直接返回 NotANumber

  1. 设计意图
    这种实现遵循了 NaN 的常见行为模式:
  • 任何涉及 NaN 的运算结果都是 NaN

  • 保持了运算的数学一致性

  • 提供了一种错误传播机制(类似于浮点数中的 NaN 传播)

  • 注释提到未来可能改为错误类型,说明这是一个过渡设计

  1. 使用场景
    这种设计可以在数学计算库或类型系统中使用,用于:
  • 处理无效的数学运算

  • 提供类型安全的错误传播

  • 在编译时捕获可能的数学错误

总结:这段代码实现了 NotANumber 类型的算术运算,遵循"任何涉及非数字的运算结果都是非数字"的原则,为类型系统提供了一种处理无效运算的方式。

你可能感兴趣的:(我的unitrix库,rust)