Skip to main content

SNumber

Enum SNumber 

Source
pub enum SNumber {
    Int(i64),
    Float(f64),
}
Expand description

This models a numeric Scratch value and is therefore returned by numeric operations or where it is known a value is neither a text nor a boolean.

Variants§

§

Int(i64)

§

Float(f64)

Implementations§

Source§

impl SNumber

Source

pub fn q_add_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber

If both numbers are finite integers numbers are added as integers if the result fits into an i64.

Otherwise, the below table applys. p and q are arbitraty finite floats.

(line + column is shown in cell)

+NaN+∞-∞+q-q+0-0
NaN0+∞-∞+q-q+0-0
+∞+∞+∞NaN+∞+∞+∞-∞
-∞-∞NaN-∞-∞-∞-∞-∞
+p+p+∞-∞+p+q+p+(-q)+p+p
-p-p+∞-∞-p+q-p+(-q)-p-p
+0+0+∞-∞+q-q00
-0-0+∞-∞+q-q00
Source§

impl SNumber

Source

pub fn q_div_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber

Integers are automatically divided as floats (if the remainder != 0) but I think this shouldn’t be considered a quirk as some people would actually like it (?)

The division rules are not necessarily identical to IEEE 754.

  • n·m / m → n (integer division without remainder)

If both numbers are finite integers and the division would have a remainder of 0 the numbers are divided as integers. Otherwise, the below table applys. p and q are arbitraty finite floats.

(line / column is shown in cell)

/NaN+∞-∞+q-q+0-0
NaNNaN+0-0+0-0NaNNaN
+∞+∞NaNNaN+∞-∞+∞-∞
-∞-∞NaNNaN-∞+∞-∞+∞
+p+∞+0-0p/q-p/q+∞-∞
-p-∞-0+0-p/qp/q-∞+∞
+0NaN+0-0+0-0NaNNaN
-0NaN-0+0-0+0NaNNaN
Source§

impl SNumber

Source

pub fn q_mul_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber
where Q: QuirkSink<SNumberToFloatQ> + QuirkSink<IntegerMulWouldFailQ>,

If both numbers are finite integers numbers are multiplied as integers if the result fits into an i64.

Otherwise, the below table applys. p and q are arbitraty finite floats.

(line * column is shown in cell)

*NaN+∞-∞+q-q+0-0
NaN0000000
+∞0+∞-∞+∞-∞NaNNaN
-∞0-∞+∞-∞+∞NaNNaN
+p0+∞-∞(+p)*(+q)(+p)*(-q)00
-p0-∞+∞(-p)*(+q)(-p)*(-q)00
+00NaNNaN0000
-00NaNNaN0000
Source§

impl SNumber

Source

pub fn q_sub_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber

If both numbers are finite integers numbers are subtracted as integers if the result fits into an i64.

Otherwise, the below table applys. p and q are arbitraty finite floats.

(line - column is shown in cell)

-NaN+∞-∞+q-q+0-0
NaN0-∞+∞-q+q-0+0
+∞+∞NaN+∞+∞+∞+∞-∞
-∞-∞-∞NaN-∞-∞-∞-∞
+p+p-∞+∞+p-(+q)+p-(-q)+p+p
-p-p-∞+∞-p-(+q)-p-(-q)-p-p
+0+0-∞+∞-q+q00
-0-0-∞+∞-q+q00
Source§

impl SNumber

Source

pub fn q_eq<Q>(&self, other: &SNumber, sink: &mut Q) -> bool

Compares two Scratch numbers for equality

  • [t] -∞ == -∞ → true
  • [f] -∞ == +∞ → false
  • [f] +∞ == -∞ → false
  • [t] +∞ == +∞ → true
  • [t] NaN == NaN → true
  • [f] NaN == number → false
  • [f] number == NaN → false

If both values are integers, integer equality is used. If both values are finite floating point numbers, floating point equality is used.

NaN is only equal to NaN.

(short value in [] for easy reference)

Source§

impl SNumber

Source

pub fn q_lt<Q>(&self, other: &SNumber, sink: &mut Q) -> bool

Compares two numbers and checks if the first is smaller than the second

The ordering seems to be: -∞ < number < +∞ < NaN < NaN (NaN is smaller than itself)

  • [t] NaN < NaN → true
  • [f] NaN < ±∞ → false
  • [f] NaN < number → false
  • [t] -∞ < NaN → true
  • [f] -∞ < -∞ → false
  • [t] -∞ < +∞ → true
  • [t] -∞ < number → true
  • [t] +∞ < NaN → true
  • [f] +∞ < other than NaN → false
  • [t] number < NaN → true
  • [t] number < +∞ → true
  • [f] number < -∞ → false

(short value in [] for easy reference)

Source

pub fn q_gt<Q>(&self, other: &SNumber, sink: &mut Q) -> bool

like Self::q_lt with swapped arguments

Source§

impl SNumber

Source

pub const fn svalue(self) -> SValue

Source§

impl SNumber

Source

pub fn q_as_float<Q>(&self, sink: &mut Q) -> f64

Source§

impl SNumber

Source

pub fn int_or_border<Q>(&self, sink: &mut Q) -> i64

Source§

impl SNumber

Source

pub fn q_exp<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_ln<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_log10<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_power_of_10<Q>(&self, sink: &mut Q) -> SNumber

This computes 10^value where value is the number in self

If the stored number is an integer, the exponent can losslessly be interpreted as u32 and the result fits into i64 the integer function i64::checked_pow is used.

Otherwise f64::powf is used, NaN will be treated as 0.

Source§

impl SNumber

Source§

impl SNumber

Source

pub fn floor(&self) -> SNumber

Source

pub fn ceil(&self) -> SNumber

Source

pub fn round(&self) -> SNumber

Source§

impl SNumber

Source

pub fn sqrt(&self) -> SNumber

Source§

impl SNumber

Source

pub fn q_cos<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_sin<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_tan<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_acos<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_asin<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_atan<Q>(&self, sink: &mut Q) -> SNumber

Source§

impl SNumber

Source

pub fn abs(&self) -> SNumber

Trait Implementations§

Source§

impl Clone for SNumber

Source§

fn clone(&self) -> SNumber

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for SNumber

Source§

impl Debug for SNumber

Source§

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

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

impl Display for SNumber

Source§

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

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

impl From<Number> for SNumber

Available on crate feature serde_json only.
Source§

fn from(value: Number) -> SNumber

Converts to this type from the input type.
Source§

impl From<SNumber> for SValue

Source§

fn from(value: SNumber) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SNumber

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for SNumber

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.