core::num
On this page
the primitive types' operator / comparison trait
impls. They make T: Add / T: Ord bounds accept primitives. Each
method is #[intrinsic("nabi.OP.TYPE")]: a primitive a + b and a
by-name call (x.add(y), T::add(a, b)) both lower to the machine op.
The body is the fallback and what a fn value of the method runs.
One block per (type, trait); keep it regular. The inherent methods
(checked_add, count_ones, floor, ..) live in ints / floats.
Modules
bigfloatfloat.bigintint, the arbitrary-precision integer.halff16(IEEE 754 binary16) andbf16(bfloat16), storage types held as their 16 bits.nichewrappers that leave out values:NonZero<T>(0),NonNan<F>(NaN),Finite<F>(NaN, +/-inf),Positive<T>(negatives).
Structs
ComplexSource
pub struct Complex<T>A complex number re + im * i. Arithmetic with another Complex or
with a real T on either side (2.0 * z, z + 1.0); == compares
both parts. Shown as 1+2i / 1-2i.
The real part.
The imaginary part.
re + im * i.i.The number of modulus
rand argumenttheta:r * e^(i theta).The modulus
|z|(hypot(re, im)).re^2 + im^2.The argument in
(-pi, pi](atan2(im, re)).The conjugate
re - im * i.1 / self.e^self.The principal natural logarithm:
ln|z| + arg(z) * i.The principal square root (
re >= 0).self^nby repeated squaring.self^won the principal branch:e^(w * ln self);0^wis 0.truewhen either part is NaN.
Trait implementations 13
impl<T: FloatingPoint> Zero for Complex<T>impl<T: FloatingPoint> One for Complex<T>impl<T: FloatingPoint> Add for Complex<T>impl<T: FloatingPoint> Sub for Complex<T>impl<T: FloatingPoint> Mul for Complex<T>impl<T: FloatingPoint> Div for Complex<T>impl<T: FloatingPoint> Neg for Complex<T>impl<T: FloatingPoint> Add<T> for Complex<T>impl<T: FloatingPoint> Sub<T> for Complex<T>impl<T: FloatingPoint> Mul<T> for Complex<T>impl<T: FloatingPoint> Div<T> for Complex<T>impl<T: FloatingPoint + Display> Display for Complex<T>impl<T: Debug> Debug for Complex<T>
Traits
ZeroSource
pub trait ZeroThe additive identity - where Iterator::sum starts (an empty sum is
0).
0ofSelf.
OneSource
pub trait OneThe multiplicative identity - where Iterator::product starts.
1ofSelf.
NumSource
pub trait Num: Copy + Zero + One + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self> + PartialEq + PartialOrdArithmetic every primitive number type has: + - * /, 0, 1 and
comparison. The integer and float primitives implement it; write
T: Num for code over any of them.
FloatingPoint
pub trait FloatingPoint: Num + Neg<Output = Self>Functions
combSource
pub const fn comb(n: u64, k: u64) -> int The number of ways to choose k of n items without order: n! / (k! (n - k)!), 0 when k > n. Exact, as an int. Python:
math.comb(n, k).
use core::num;
assert(num::comb(5, 2) == 10 && num::comb(3, 5) == 0);
assert(num::comb(100, 50) == 100891344545564193334812497256);
assert(num::perm(5, 2) == 20);
permSource
pub const fn perm(n: u64, k: u64) -> int The number of ordered choices of k of n items: n! / (n - k)!,
0 when k > n. Python: math.perm(n, k).
fsumSource
pub fn fsum<I: IntoIterator<Item = f64>>(xs: I) -> f64 The sum of xs rounded once, as if added exactly: no error builds
up, whatever the order or the magnitudes. An infinity or NaN among
them gives the plain IEEE result (inf - inf is NaN), and so does a
running sum past the range ([1e308, 1e308, -1e308] is inf). About three times the cost of a plain
loop; Array::sum (pairwise) is the fast accurate one. Python:
math.fsum(xs).
use core::num;
let xs = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];
assert(num::fsum(xs) == 1.0);
assert(num::fsum([1e100, 1.0, -1e100]) == 1.0);
linspaceSource
pub fn linspace<T: FloatingPoint>(start: T, stop: T, n: usize) -> List<T> n evenly spaced values from start to stop, both included (one
value, start, when n is 1; none when 0). Value i is
start + i * step with step = (stop - start) / (n - 1), and the
last is stop exactly. NumPy / Julia: linspace(start, stop, n).
arangeSource
pub fn arange<T: FloatingPoint>(start: T, stop: T, step: T) -> List<T> The values start + i * step for i from 0 while they stay before
stop (ceil((stop - start) / step) of them); each is computed from
i, so no error builds up. A zero or NaN step panics. NumPy:
arange(start, stop, step).