docs

core::num

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

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

  • bigfloat float.
  • bigint int, the arbitrary-precision integer.
  • half f16 (IEEE 754 binary16) and bf16 (bfloat16), storage types held as their 16 bits.
  • niche wrappers 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.

  • pub re: T§

    Source

    The real part.

  • pub im: T§

    Source

    The imaginary part.

  • pub const fn new(re: T, im: T) -> Complex<T>§

    Source

    re + im * i.

  • pub const fn i() -> Complex<T>§

    Source

    i.

  • pub fn from_polar(r: T, theta: T) -> Complex<T>§

    Source

    The number of modulus r and argument theta: r * e^(i theta).

  • pub fn abs(self) -> T§

    Source

    The modulus |z| (hypot(re, im)).

  • pub const fn norm_sqr(self) -> T§

    Source

    re^2 + im^2.

  • pub fn arg(self) -> T§

    Source

    The argument in (-pi, pi] (atan2(im, re)).

  • pub const fn conj(self) -> Complex<T>§

    Source

    The conjugate re - im * i.

  • pub const fn recip(self) -> Complex<T>§

    Source

    1 / self.

  • pub fn exp(self) -> Complex<T>§

    Source

    e^self.

  • pub fn ln(self) -> Complex<T>§

    Source

    The principal natural logarithm: ln|z| + arg(z) * i.

  • pub fn sqrt(self) -> Complex<T>§

    Source

    The principal square root (re >= 0).

  • pub fn powi(self, n: i32) -> Complex<T>§

    Source

    self^n by repeated squaring.

  • pub fn pow(self, w: Complex<T>) -> Complex<T>§

    Source

    self^w on the principal branch: e^(w * ln self); 0^w is 0.

  • pub const fn is_nan(self) -> bool§

    Source

    true when either part is NaN.

Trait implementations 13

Traits

ZeroSource

pub trait Zero

The additive identity - where Iterator::sum starts (an empty sum is 0).

OneSource

pub trait One

The multiplicative identity - where Iterator::product starts.

NumSource

pub trait Num: Copy + Zero + One + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self> + PartialEq + PartialOrd

Arithmetic 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>

41 members →

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).

nabi
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).

nabi
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).

Implementations

impl f64

76 members →

impl f32

76 members →

impl i8

76 members →

impl i16

76 members →

impl i32

76 members →

impl i64

76 members →

impl isize

85 members →

impl usize

78 members →

impl u8

69 members →

impl u16

69 members →

impl u32

69 members →

impl u64

69 members →