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§
Implementations§
Source§impl SNumber
impl SNumber
Sourcepub fn q_add_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber
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 | |
|---|---|---|---|---|---|---|---|---|
NaN | 0 | +∞ | -∞ | +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 | -q | 0 | 0 | |
-0 | -0 | +∞ | -∞ | +q | -q | 0 | 0 |
Source§impl SNumber
impl SNumber
Sourcepub fn q_div_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_div_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
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 | |
|---|---|---|---|---|---|---|---|---|
NaN | NaN | +0 | -0 | +0 | -0 | NaN | NaN | |
+∞ | +∞ | NaN | NaN | +∞ | -∞ | +∞ | -∞ | |
-∞ | -∞ | NaN | NaN | -∞ | +∞ | -∞ | +∞ | |
| +p | +∞ | +0 | -0 | p/q | -p/q | +∞ | -∞ | |
| -p | -∞ | -0 | +0 | -p/q | p/q | -∞ | +∞ | |
+0 | NaN | +0 | -0 | +0 | -0 | NaN | NaN | |
-0 | NaN | -0 | +0 | -0 | +0 | NaN | NaN |
Source§impl SNumber
impl SNumber
Sourcepub fn q_mul_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber
pub fn q_mul_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber
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 | |
|---|---|---|---|---|---|---|---|---|
NaN | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
+∞ | 0 | +∞ | -∞ | +∞ | -∞ | NaN | NaN | |
-∞ | 0 | -∞ | +∞ | -∞ | +∞ | NaN | NaN | |
| +p | 0 | +∞ | -∞ | (+p)*(+q) | (+p)*(-q) | 0 | 0 | |
| -p | 0 | -∞ | +∞ | (-p)*(+q) | (-p)*(-q) | 0 | 0 | |
+0 | 0 | NaN | NaN | 0 | 0 | 0 | 0 | |
-0 | 0 | NaN | NaN | 0 | 0 | 0 | 0 |
Source§impl SNumber
impl SNumber
Sourcepub fn q_sub_numbers<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumber
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 | |
|---|---|---|---|---|---|---|---|---|
NaN | 0 | -∞ | +∞ | -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 | +q | 0 | 0 | |
-0 | -0 | -∞ | +∞ | -q | +q | 0 | 0 |
Source§impl SNumber
impl SNumber
Sourcepub fn q_eq<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_eq<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
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
impl SNumber
Sourcepub fn q_lt<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_lt<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
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)
Sourcepub fn q_gt<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_gt<Q>(&self, other: &SNumber, sink: &mut Q) -> boolwhere
Q: QuirkSink<SNumberToFloatQ>,
like Self::q_lt with swapped arguments
Source§impl SNumber
impl SNumber
pub fn q_as_float<Q>(&self, sink: &mut Q) -> f64where
Q: QuirkSink<SNumberToFloatQ>,
Source§impl SNumber
impl SNumber
pub fn int_or_border<Q>(&self, sink: &mut Q) -> i64where
Q: QuirkSink<NumberTooBigForIntQ>,
Source§impl SNumber
impl SNumber
pub fn q_exp<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_ln<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_log10<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
Sourcepub fn q_power_of_10<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_power_of_10<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
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
impl SNumber
Sourcepub fn q_modulo<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_modulo<Q>(&self, other: &SNumber, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
This mimics the JS remainder operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder
Source§impl SNumber
impl SNumber
pub fn q_cos<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_sin<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_tan<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_acos<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_asin<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
pub fn q_atan<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SNumberToFloatQ>,
Trait Implementations§
impl Copy for SNumber
Auto Trait Implementations§
impl Freeze for SNumber
impl RefUnwindSafe for SNumber
impl Send for SNumber
impl Sync for SNumber
impl Unpin for SNumber
impl UnsafeUnpin for SNumber
impl UnwindSafe for SNumber
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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