Skip to main content

Natural

Struct Natural 

Source
pub struct Natural { /* private fields */ }
Expand description

Natural number, specifically well-suited for counting satisfying assignments

When counting the number of satisfying assignments in a decision diagram, the numbers often have many trailing zeros (in binary representation). Based on this observation, we decompose numbers as m × 2^e with a mantissa m and an exponent e. We require that both m and e are natural numbers. Further, m is odd unless the represented number is zero, in which case both m and e are zero.

The exponent e is stored as a u64 and the mantissa m as an array of u64 “digits.” In case m fits into a single u64 digit, m is stored inline, i.e., without any heap allocation.

Conceptually, this type is similar to arbitrary precision floats. However, we do not require the user to choose the precision, instead we always represent numbers exactly.

To account for computation errors (e.g., an exponent that cannot be represented as a u64). this type includes a NaN value. It is undefined if this value is larger or smaller than actual natural numbers. Therefore, this type does not implement Ord but only PartialOrd.

Also note that the implementation of Shr is tailored to the application in counting satisfying assignments, where it is used as an exact division by (a power of) two. If a 1-bit would get lost by a shift to the right, i.e., the division result would not be exact, the computation result is NaN to make the error obvious. A context for such an error may be pretending that a Boolean function has a smaller domain than it actually has.

Implementations§

Source§

impl Natural

Source

pub const ZERO: Self

The number 0

Source

pub fn from_le_digits(digits: &[u64]) -> Self

Create a Natural from a sequence of digits

Here, le stands for little endian, i.e., the first element of digits is the least significant digit.

Source

pub fn mantissa(&self) -> &[u64]

Get the mantissa

The returned sequence starts with the least significant digit and has minimal length, but at least one element. So unless the mantissa is 0, the most significant digit (i.e., the last element) is non-zero.

Source

pub fn exp(&self) -> u64

Get the exponent

Source

pub fn is_nan(&self) -> bool

Check if the number is NaN

Source

pub fn bit_width(&self) -> u128

Compute the number of bits needed to represent the number explicitly, that is 1 + floor(log₂(self)).

Trait Implementations§

Source§

impl Add for Natural

Source§

type Output = Natural

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Binary for Natural

Source§

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

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

impl Clone for Natural

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Natural

Source§

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

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

impl Display for Natural

Source§

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

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

impl Drop for Natural

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Natural

Source§

impl From<&Natural> for f64

Source§

fn from(value: &Natural) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Natural

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Natural

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Natural

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Natural

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Natural

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl Hash for Natural

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IsFloatingPoint for Natural

cbindgen:ignore

Source§

const FLOATING_POINT: bool = false

true iff the underlying type is a floating point number
Source§

const MIN_EXP: i32 = 0

One greater than the minimum possible normal power of 2 exponent, see f64::MIN_EXP for instance. 0 for integers
Source§

impl LowerHex for Natural

Source§

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

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

impl Octal for Natural

Source§

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

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

impl PartialEq for Natural

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Natural

Source§

fn partial_cmp(&self, other: &Self) -> 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 Send for Natural

Source§

impl Shl<u32> for Natural

Source§

type Output = Natural

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: u32) -> Self

Performs the << operation. Read more
Source§

impl Shl<u64> for Natural

Source§

type Output = Natural

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: u64) -> Self

Performs the << operation. Read more
Source§

impl Shr<u32> for Natural

Source§

type Output = Natural

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: u32) -> Self

Performs the >> operation. Read more
Source§

impl Shr<u64> for Natural

Source§

type Output = Natural

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: u64) -> Self

Performs the >> operation. Read more
Source§

impl Sync for Natural

Source§

impl TryFrom<&Natural> for UBig

Source§

type Error = NotRepresentable

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

fn try_from(value: &Natural) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Natural> for u128

Source§

type Error = NotRepresentable

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

fn try_from(value: &Natural) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Natural> for u64

Source§

type Error = NotRepresentable

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

fn try_from(value: &Natural) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl UpperHex for Natural

Source§

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

Formats the value using the given formatter. Read more

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> SatCountNumber for T
where T: Add<Output = T> + Shl<u32, Output = T> + Clone + Shr<u32, Output = T> + From<u32> + IsFloatingPoint,

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
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.