ShortFloat

Struct ShortFloat 

Source
pub struct ShortFloat { /* private fields */ }
Expand description

模拟nars.entity.ShortFloat

  • 🚩使用u320~4294967296的范围覆盖0~10000²
  • ✨原生支持四则运算
  • 🎯在基本的「证据数」基础上,添加更多NAL细节功能
  • 🚩【2024-05-02 16:05:04】搬迁自crate::entity::BudgetValue
  • 🚩【2024-05-02 17:48:30】现在全部抛弃基于「不可变引用」的运算
    • ⚠️混合「传可变引用」和「直接传值」的代码将过于冗杂(并且造成接口不统一)
    • 📌在实现了Copy之后,将值的复制看作是「随处可用」的
  • 🚩【2024-05-03 11:11:48】现在将其概念与「短浮点」合并

§⚠️与OpenNARS不同的一点:浮点舍入问题

!📝OpenNARS的实现是「四舍五入」,而NARust的实现是「向下截断」

  • ❗即便在构造时采用了Float::round,但实际效果仍然与OpenNARS不同
    • ⚡为性能考量,许多运算最后的舍入操作仍然是四舍五入(整数除法,避免转换为浮点)
  • 📄这导致0.1 * 0.0005在OpenNARS中等于0.0001而在NARust中为0

OpenNARS中可行的推理:

IN: <A --> B>. %1.00;0.10% {6 : 3}
IN: <B --> C>. %1.00;0.01% {6 : 4}
1
OUT: <A --> C>. %1.00;0.00% {7 : 4;3}
OUT: <C --> A>. %1.00;0.00% {7 : 4;3}

§📄OpenNARS

A float value in [0, 1], with 4 digits accuracy.

Implementations§

Source§

impl ShortFloat

Source

pub const ZERO: Self

常量「0」

Source

pub const ONE: Self

常量「1」

Source

pub const HALF: Self

常量「1/2」

Source

pub fn new(value: u32) -> Result<Self, ShortFloatError>

以0~10000的整数创建(有检查)

Source

pub fn is_in_range(value: Float) -> bool

🆕判断浮点数是否在范围内

  • 📝判断「是否在范围外」直接使用「不在范围内」的逻辑
    • 📄clippy提示「manual !RangeInclusive::contains implementation」
  • ✅对NaN会默认返回false,故无需担心
Source

pub fn value_float(&self) -> Float

模拟getValue

  • 🚩获取浮点值
  • 🚩【2024-05-03 10:51:09】更名为value_float以暂时避免与「短浮点」的value重名
§📄OpenNARS

To access the value as float

@return The current value in float

Source

pub fn value_short(&self) -> u32

🆕获取短整数(只读)

  • 🎯用于在「其它地方的impl实现」中增强性能(直接读取内部数值)
Source

pub fn set_value(&mut self, value: Float) -> Result<(), ShortFloatError>

模拟ShortFloat.setValue

  • 🚩设置浮点值(有检查)
Source

pub fn set_value_unchecked(&mut self, value: Float)

🆕设置浮点值(无检查)

  • ⚠️必须确保值在范围内
§📄OpenNARS

Set new value, rounded, with validity checking

@param v The new value

Source

pub fn float_to_short_value(value: Float) -> Result<u32, ShortFloatError>

🆕浮点转换为「短整数」(有检查)

  • 🎯提取共用逻辑,以同时用于「构造」和「赋值」
  • ✅无需考虑「NaN」「无限」等值:Self::is_in_range会自动判断
Source

pub fn float_to_short_value_unchecked(value: Float) -> u32

🆕浮点转换为「短整数」(无检查)

  • 🎯提取共用逻辑,以同时用于「构造」和「赋值」
  • ⚠️必须确保值在范围内
Source

pub fn is_valid_short(short: u32) -> bool

🆕判断短整数是否合法

  • 🚩直接判断「是否小于等于最大值」
Source

pub fn is_valid(&self) -> bool

🆕判断自身值是否合法

Source

pub fn check_valid(&self) -> Result<(), ShortFloatError>

🆕检查自身值是否合法

  • 🚩判断自身值是否合法,然后返回Result
Source

pub fn validate(self) -> Result<Self, ShortFloatError>

🆕检查自身值是否合法,并返回自身

Source

pub fn from_float(value: Float) -> Self

从浮点到自身转换(不检查,直接panic)

  • ❌在实现TryFrom时,无法通过From实现:conflicting implementations of trait std::convert::TryFrom<f64> for type entity::short_float::ShortFloat

! ⚠️在「范围越界」时直接panic

  • 🎯降低代码冗余量(减少过多的「错误处理」)
conflicting implementation in crate `core`:
- impl<T, U> std::convert::TryFrom<U> for T
where U: std::convert::Into<T>;
Source

pub fn to_float(&self) -> Float

Source

pub fn set(&mut self, new_value: Self)

Source

pub fn is_zero(&self) -> bool

Source

pub fn is_one(&self) -> bool

Source

pub fn is_half(&self) -> bool

Source

pub fn value(&self) -> Float

Source§

impl ShortFloat

【派生】用于「短浮点」的实用方法

  • 🚩【2024-06-20 22:21:57】现在直接对「短浮点」实现功能,不再搞特征那一套
    • 💭实际上也可以重启「短浮点特征」,但此方面Rust比Java方便,故无需徒增复杂性

§📄OpenNARS

Common functions on real numbers, mostly in [0,1].

Source

pub fn and(self, value: Self) -> Self

模拟UtilityFunctions.and

§📄OpenNARS

A function where the output is conjunctively determined by the inputs

@param arr The inputs, each in [0, 1] @return The output that is no larger than each input

Source

pub fn and_multi(values: impl IntoIterator<Item = Self>) -> Self

🆕多个值相与

  • 🚩直接派生自「两个值相与」
    • 📝「扩展逻辑与」遵循交换律和结合律
Source

pub fn or(self, value: Self) -> Self

模拟UtilityFunctions.or

  • 🚩扩展逻辑「或」
  • 🚩【2024-05-02 17:53:22】利用德摩根律行事
    • 💭可能会有性能损失
  • 🚩现在直接使用ShortFloat基于的std::ops::BitOr特征
§📄OpenNARS

A function where the output is disjunctively determined by the inputs

@param arr The inputs, each in [0, 1] @return The output that is no smaller than each input

Source

pub fn or_multi(values: impl IntoIterator<Item = Self>) -> Self

🆕多个值相或

  • 🚩直接派生自「多个值相与」
    • 📝「扩展逻辑或」遵循交换律和结合律
    • ⚡优化:无需重复进行逻辑非
Source

pub fn arithmetical_average(values: impl IntoIterator<Item = Self>) -> Self

复刻OpenNARS nars.inference.UtilityFunctions.aveAri

  • 🚩求代数平均值
  • ❌不能用impl IntoIterator<Item = Self>:要计算长度
  • ⚠️迭代器不能为空
§📄OpenNARS

A function where the output is the arithmetic average the inputs

@param arr The inputs, each in [0, 1] @return The arithmetic average the inputs

Source

pub fn geometrical_average(values: impl IntoIterator<Item = Self>) -> Self

复刻OpenNARS nars.inference.UtilityFunctions.aveGeo

  • 🚩求几何平均值
  • ❌不能用impl IntoIterator<Item = Self>:要计算长度
§📄OpenNARS

A function where the output is the geometric average the inputs

@param arr The inputs, each in [0, 1] @return The geometric average the inputs

Source

pub fn w2c(w: Float) -> Self

从真值的「w值」到「c值」

  • 📄超参数Parameters.HORIZON参见[crate::control::Parameters]
§📄OpenNARS

A function to convert weight to confidence

@param w Weight of evidence, a non-negative real number @return The corresponding confidence, in [0, 1)

Source

pub fn w2c_float(w: Float) -> Float

🎯能利用尽量直接用,避免重复转换

Source

pub fn W2C1() -> ShortFloat

在改版OpenNARS中是常量,在此处因为「常量函数难以构建」改为变量

Source

pub fn W2C1_float() -> Float

在改版OpenNARS中是常量,在此处因为「常量函数难以构建」改为变量

Source

pub fn c2w(&self) -> Float

从真值的「c值」到「w值」

  • 📌此处的c就是self
§📄OpenNARS

A function to convert confidence to weight

@param c confidence, in [0, 1) @return The corresponding weight of evidence, a non-negative real number

Source

pub fn max_from(&mut self, other: Self)

🆕「最大值合并」

  • 🎯用于(统一)OpenNARSmerge的重复调用
  • 🚩现在已经在「短浮点」中要求了Ord
  • 📝【2024-05-03 14:55:29】虽然现在「预算函数」以「直接创建新值」为主范式,
    • 但在用到该函数的merge方法上,仍然是「修改」语义——需要可变引用

Trait Implementations§

Source§

impl Add for ShortFloat

Source§

fn add(self, rhs: Self) -> Self::Output

内部值相加,但会检查越界

§Panics

! ⚠️可能会有「数值溢出」的panic

Source§

type Output = ShortFloat

The resulting type after applying the + operator.
Source§

impl BitAnd for ShortFloat

实现「NAL逻辑与」

  • 🚩【2024-05-03 11:31:18】对clippy允许「令人疑惑的代数实现」 ? 💭是否可以自动派生(主要是受到「孤儿规则」的限制)
Source§

type Output = ShortFloat

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitOr for ShortFloat

实现「NAL逻辑或」

  • 🚩【2024-05-03 11:31:18】对clippy允许「令人疑惑的代数实现」 ? 💭是否可以自动派生(主要是受到「孤儿规则」的限制)
Source§

type Output = ShortFloat

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl Clone for ShortFloat

Source§

fn clone(&self) -> ShortFloat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ShortFloat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ShortFloat

Source§

fn default() -> ShortFloat

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for ShortFloat

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for ShortFloat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<usize> for ShortFloat

整数除法

Source§

fn div(self, rhs: usize) -> Self::Output

  • 🚩暂且调用两次转换,在保证「使用方便」的同时保证「效果等同」
Source§

type Output = ShortFloat

The resulting type after applying the / operator.
Source§

impl Div for ShortFloat

Source§

fn div(self, rhs: Self) -> Self::Output

内部值相除,会检查越界

§Panics

! ⚠️可能会有「数值溢出」的panic

Source§

type Output = ShortFloat

The resulting type after applying the / operator.
Source§

impl EvidentNumber for ShortFloat

实现「证据数值」

Source§

fn zero() -> Self

常数「0」 Read more
Source§

fn one() -> Self

常数「1」 Read more
Source§

fn root(self, n: usize) -> Self

n次开根 Read more
Source§

fn is_valid(&self) -> bool

判断其是否合法 Read more
Source§

fn try_validate(&self) -> Result<&Self, &str>

尝试验证值是否合法 Read more
Source§

fn validate(&self) -> &Self

(强制)验证其是否合法 Read more
Source§

impl Hash for ShortFloat

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<usize> for ShortFloat

整数乘法

Source§

fn mul(self, rhs: usize) -> Self::Output

  • 🚩暂且调用两次转换,在保证「使用方便」的同时保证「效果等同」
Source§

type Output = ShortFloat

The resulting type after applying the * operator.
Source§

impl Mul for ShortFloat

Source§

fn mul(self, rhs: Self) -> Self::Output

内部值相乘,无需检查越界

  • ✅0~1的数对乘法封闭,故无需任何检查
  • ⚠️乘法在最后「除以最大值」时,采用「向下取整」的方式
  • ⚠️因为乘法可能会造成上界溢出,故需要转换为「双倍位类型」
    • 🚩现在直接设置为「双倍位类型」
Source§

type Output = ShortFloat

The resulting type after applying the * operator.
Source§

impl Not for ShortFloat

实现「NAL逻辑非」 ? 💭是否可以自动派生(主要是受到「孤儿规则」的限制)

Source§

type Output = ShortFloat

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Ord for ShortFloat

Source§

fn cmp(&self, other: &ShortFloat) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ShortFloat

Source§

fn eq(&self, other: &ShortFloat) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ShortFloat

Source§

fn partial_cmp(&self, other: &ShortFloat) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for ShortFloat

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for ShortFloat

Source§

fn sub(self, rhs: Self) -> Self::Output

内部值相减,无需检查越界

  • 📌不会减去负值,只会「小于0」越界
§Panics

! ⚠️可能会有「数值溢出」的panic

Source§

type Output = ShortFloat

The resulting type after applying the - operator.
Source§

impl TryFrom<f64> for ShortFloat

实现「从浮点到『短浮点』的直接转换」 🚩直接通过「构造函数+尝试转换」实现

Source§

type Error = ShortFloatError

The type returned in the event of a conversion error.
Source§

fn try_from(value: Float) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for ShortFloat

Source§

impl Eq for ShortFloat

Source§

impl StructuralPartialEq for ShortFloat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> BoostWithOption for T

Source§

fn option<C>(self, criterion: C) -> Option<Self>
where C: FnOnce(&Self) -> bool,

根据某条件把自身变为可选值 Read more
Source§

fn some(self) -> Option<Self>

将自身封装为Some Read more
Source§

fn none(self) -> Option<Self>

将自身封装为None Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> JoinTo for T

Source§

fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个「目标字串」中,中途不创建任何辅助字符串 Read more
Source§

fn join_to_new<S>(self, sep: impl AsRef<str>) -> String
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个新字串中 Read more
Source§

fn join_to_multi<S>(self, out: &mut String, sep: &[impl AsRef<str>])
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个「目标字串」中,使用多个分隔符,中途不创建任何辅助字符串 Read more
Source§

fn join_to_multi_new<S>(self, sep: &[impl AsRef<str>]) -> String
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个新字串中,使用多个分隔符 Read more
Source§

impl<T> JoinTo for T

Source§

fn join_to<S>(self, out: &mut String, sep: impl AsRef<str>)
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个「目标字串」中,中途不创建任何辅助字符串 Read more
Source§

fn join_to_new<S>(self, sep: impl AsRef<str>) -> String
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个新字串中 Read more
Source§

fn join_to_multi<S>(self, out: &mut String, sep: &[impl AsRef<str>])
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个「目标字串」中,使用多个分隔符,中途不创建任何辅助字符串 Read more
Source§

fn join_to_multi_new<S>(self, sep: &[impl AsRef<str>]) -> String
where Self: Sized + Iterator<Item = S>, S: AsRef<str>,

将字串集中拼接到一个新字串中,使用多个分隔符 Read more
Source§

impl<T> ToDebug for T
where T: Debug,

Source§

fn to_debug(&self) -> String

将对象转换为「用Debug格式化的字符串」

Source§

impl<T> ToDebug for T
where T: Debug,

Source§

fn to_debug(&self) -> String

将对象转换为「用Debug格式化的字符串」

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Void for T

Source§

fn void(self)

Source§

impl<T> Void for T

Source§

fn void(self)

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,