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
impl Number
Sourcepub fn decompose(self) -> Option<(Sign, u64, i32)>
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
impl Number
Sourcepub fn to_int32(self) -> i32
pub fn to_int32(self) -> i32
ToInt32. https://tc39.es/ecma262/#sec-toint32
Sourcepub fn to_uint32(self) -> u32
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
impl Number
Sourcepub fn pow(self, exponent: Self) -> Self
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
impl Number
Sourcepub fn to_integer_or_infinity(self) -> Number
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.
Trait Implementations§
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.
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§impl From<f64> for Number
The one way in: every NaN becomes Number::NAN, everything else
keeps its bits.
impl From<f64> for Number
The one way in: every NaN becomes Number::NAN, everything else
keeps its bits.
Source§impl From<u32> for Number
Exact: every u32 is a Number. ToUint32’s results come back this way.
impl From<u32> for Number
Exact: every u32 is a Number. ToUint32’s results come back this way.