Skip to main content

Number

Struct Number 

Source
pub struct Number(/* private fields */);
Expand description

A JavaScript number: an f64 that holds one NaN.

FunctionalScript has one NaN: nothing in the language tells two apart, so a sign or payload a host operation left on one — -NaN flips the sign, x86’s 0 / 0 sets it — is dropped where an f64 becomes a Number, and every operator’s result passes back through the same conversion. The field is private, so there is no other way to build one, and a NaN-boxing VM, which keeps its boxed values in the negative quiet NaNs, can store the bits as they are.

This is the VM’s number: a literal or a parsed string becomes one once, and the VM computes on it from there — the ECMAScript operations on a Number are its methods and operators. An f64 is read back out only where a leaf needs IEEE 754 arithmetic Rust already has, and at a test’s edge to look at the bits.

PartialOrd is IEEE 754’s: a NaN compares with nothing, which is exactly the undefined a relational operator’s abstract comparison answers.

Implementations§

Source§

impl Number

Source

pub fn decompose(self) -> Option<(Sign, u64, i32)>

self as ±significand · 2^exponent, exactly: a normal value’s significand has the implicit leading bit folded in, a subnormal’s is the fraction as it is, and both zeros are a 0 significand with their own sign. None for a NaN and the infinities.

Not reduced to lowest terms: significand may end in zero bits.

Source§

impl Number

Source

pub fn to_int32(self) -> i32

Source

pub fn to_uint32(self) -> u32

ToUint32. https://tc39.es/ecma262/#sec-touint32 — used by <</>> (for the shift-count operand) and >>> (for both operands, since it has no signed form).

Source§

impl Number

Source

pub fn pow(self, exponent: Self) -> Self

Math.pow(self, exponent), which is self ** exponent: the spec’s Number::exponentiate. Diverges from f64::powf (and C99’s pow, which powf follows) in exactly two spots: a NaN exponent is NaN regardless of the base (C99 special-cases pow(1, y) = 1 even for a NaN y), and an infinite exponent against a base of magnitude 1 is NaN (C99 gives pow(±1, ±∞) = 1). Every other case — zero/infinite base or exponent, the sign and parity rules, a negative base with a non-integer exponent giving NaN — already matches powf exactly, so only these two get a special case. Not a core::ops trait — Rust has no operator for exponentiation — so a plain method, the same as BigInt::pow and Any::pow.

Source§

impl Number

Source

pub fn to_integer_or_infinity(self) -> Number

ToIntegerOrInfinity (https://tc39.es/ecma262/#sec-tointegerorinfinity) of a number: NaN and either zero are 0, an infinity is itself, and everything else is truncated toward zero. The specification’s answer is a mathematical value, so a -0 never comes out of it: -0.5 truncates to 0, not -0, and the addition below is what folds the sign — -0.0 + 0.0 is 0.0 in IEEE 754.

Source§

impl Number

The names are JavaScript’s, in Rust’s casing: Number.NaN is NAN, Number.isFinite is is_finite, and each does what its original does.

Source

pub const NAN: Self

Number.NaN — the one NaN: quiet, positive, empty payload.

Source

pub fn is_nan(self) -> bool

Number.isNaN(self).

Source

pub fn is_finite(self) -> bool

Number.isFinite(self): neither a NaN nor an infinity.

Trait Implementations§

Source§

impl Add for Number

Source§

type Output = Number

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Number

Source§

fn clone(&self) -> Number

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 Number

Source§

impl Debug for Number

The bare f64, as every other Unpacked payload prints bare: a numeric Any reads 1.0, not Number(1.0), in a test’s failure message.

Source§

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

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

impl Div for Number

Source§

type Output = Number

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<A: IVm> From<Number> for Unpacked<A>

Source§

fn from(value: Number) -> Self

Converts to this type from the input type.
Source§

impl From<Number> for f64

Source§

fn from(v: Number) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Number

Number(true) is 1 and Number(false) is 0: ToNumber of a boolean.

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Number

The one way in: every NaN becomes Number::NAN, everything else keeps its bits.

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Number

Exact: every i32 is a Number. ToInt32’s results come back this way.

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Number

Exact: every u32 is a Number. ToUint32’s results come back this way.

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl Mul for Number

Source§

type Output = Number

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Neg for Number

Source§

type Output = Number

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Number

Source§

fn eq(&self, other: &Number) -> 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 Number

Source§

fn partial_cmp(&self, other: &Number) -> 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
Source§

impl Rem for Number

Source§

type Output = Number

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self

Performs the % operation. Read more
Source§

impl StructuralPartialEq for Number

Source§

impl Sub for Number

Source§

type Output = Number

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<A: IVm> TryFrom<Any<A>> for Number

Source§

type Error = Any<A>

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

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

Performs the conversion.

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

Source§

fn to_any<A: IVm>(self) -> Any<A>
where Self: Into<Unpacked<A>>,

Source§

impl<T> ToArray for T

Source§

fn to_array<A: IVm>(self) -> Array<A>
where Self: Sized + IntoIterator<Item = Any<A>>,

Source§

impl<T> ToObject for T

Source§

fn to_object<A: IVm>(self) -> Object<A>
where Self: Sized + IntoIterator<Item = Property<A>>,

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

Source§

fn try_to_string<A: IVm>(self) -> Result<String<A>, Any<A>>
where Self: Sized + IntoIterator<Item = Result<u16, Any<A>>>,

Source§

fn to_string<A: IVm>(self) -> String<A>
where Self: Sized + IntoIterator<Item = u16>,

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.