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

怎么把dw做的网站分享给别网站 建设 内容

怎么把dw做的网站分享给别,网站 建设 内容,37网页游戏中心,wordpress微信主题一、源码 代码实现了一个类型级别的负数系统,允许在编译期进行负数运算的类型检查。 //! 负数类型及其算术运算实现 //! Negative number type and its arithmetic operations implementation //! //! 本模块定义了类型系统中的负数类型,并为其实现了基…

一、源码

代码实现了一个类型级别的负数系统,允许在编译期进行负数运算的类型检查。

//! 负数类型及其算术运算实现
//! 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>;
  • 遵循数学除法规则

  • 特别注意没有实现除以零的情况

四、设计特点

  1. 类型安全:所有运算在编译期进行类型检查

  2. 零开销:使用PhantomData,运行时无额外开销

  3. 数学正确:严格遵循数学运算规则

  4. 模块化设计:与零相关的运算分离到zero.rs模块

  5. 完整文档:包含中英文注释和示例

五、使用示例

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类型系统的强大能力,可以在编译期捕获数学运算的类型约束和规则。


文章转载自:

http://BxiKzISO.jqrhz.cn
http://l81W6KAp.jqrhz.cn
http://gcKsJMUP.jqrhz.cn
http://vrKK9DR0.jqrhz.cn
http://7jeiRmuY.jqrhz.cn
http://qlKUVU6d.jqrhz.cn
http://RlhPWTxI.jqrhz.cn
http://BTjzM17m.jqrhz.cn
http://yUKo7lIX.jqrhz.cn
http://tlPEe8IZ.jqrhz.cn
http://w6iEp5VZ.jqrhz.cn
http://5CNFN6F0.jqrhz.cn
http://k4x1ABpy.jqrhz.cn
http://Gka4mVP0.jqrhz.cn
http://vFjjl6C3.jqrhz.cn
http://yv6YVgUO.jqrhz.cn
http://Ycg0ZWoJ.jqrhz.cn
http://xpGIWCEO.jqrhz.cn
http://f18kQExb.jqrhz.cn
http://K6dcnulW.jqrhz.cn
http://w5rp3Lxt.jqrhz.cn
http://nGdAQS8v.jqrhz.cn
http://jEBDaB38.jqrhz.cn
http://DbaVgNfe.jqrhz.cn
http://p4AHOLNl.jqrhz.cn
http://cE4iSnkW.jqrhz.cn
http://BbSxAC32.jqrhz.cn
http://4Ky55K0C.jqrhz.cn
http://fifQrOOa.jqrhz.cn
http://vJjbyEX0.jqrhz.cn
http://www.dtcms.com/wzjs/655161.html

相关文章:

  • 静宁网站建设可口可乐软文范例
  • 360网站建设价位聊城网站建设哪家专业
  • 模板网站建设多少钱沈阳网站关键词优化多少钱
  • 免费发布网站建设信息百度竞价推广账户优化
  • 做舞台灯光的在哪些网站接订单呢建网页服务公司
  • 江苏网站设计方案怎么查网站是哪个公司做的
  • 自己做网站主机网站建设介绍语
  • 网页设计和网站建设的课程设计广西建设厅证书查询
  • 灌云县建设局网站网站后台管理系统 源码
  • 网站建设基本流程心得搜索引擎营销的主要方式有哪些?
  • 刘涛给孩子网站做的广告网站建设方案书 人员资金安排
  • 网站推广文章范例如何创立一个网站
  • 卓进网站做多语言网站不会翻译怎么办
  • 有企业信息的网站360网站提交收录入口
  • 太原这边有做网站的吗湖南省网站备案登记
  • 软件做网站 编程自己写wordpress 主题域名授权
  • ui界面设计培训班谷歌排名优化入门教程
  • 很多搜索词网站怎样做谷歌广告平台
  • 给网站网站做推广犯法跨国采购网外贸论坛
  • 网站建设自助建站企业一般注册公司要多少钱
  • 综合购物网站建站商机互联网站建设
  • 官方网站建设必要性如何改变wordpress字体颜色
  • 购买手表网站东营住房和城乡建设厅官网
  • 如何自己做众筹网站网站行业认证怎么做
  • 四川成都最新消息台州网站优化方案
  • 专业建设验收网站英国做网站的人
  • 太原网站制作案例乐清比较好的设计公司
  • 温州网站制作设计建设厅官方网站河南
  • 虹口上海网站建设在线设计平台源码
  • 公司网站备案号织梦网站做seo优化