BigRationalExt

Trait BigRationalExt 

Source
pub trait BigRationalExt {
    // Required methods
    fn from_u64(value: u64) -> Self;
    fn from_frac_u64(numerator: u64, denominator: u64) -> Self;
    fn from_u128(value: u128) -> Self;
    fn from_frac_u128(numerator: u128, denominator: u128) -> Self;
    fn ceil_to_u128(&self) -> Option<u128>;
    fn from_usize(value: usize) -> Self;
    fn from_frac_usize(numerator: usize, denominator: usize) -> Self;
    fn log2_ceil(&self, binary_digits: usize) -> BigRational;
}
Expand description

Extension trait adding convenience constructors for BigRational.

Required Methods§

Source

fn from_u64(value: u64) -> Self

Creates a BigRational from a u64 numerator with denominator 1.

Source

fn from_frac_u64(numerator: u64, denominator: u64) -> Self

Creates a BigRational from a u64 numerator and denominator.

§Panics

Panics if the denominator is zero.

Source

fn from_u128(value: u128) -> Self

Creates a BigRational from a u128 numerator with denominator 1.

Source

fn from_frac_u128(numerator: u128, denominator: u128) -> Self

Creates a BigRational from a u128 numerator and denominator.

§Panics

Panics if the denominator is zero.

Source

fn ceil_to_u128(&self) -> Option<u128>

Returns the ceiling of the rational value as u128, saturating and treating negative values as zero.

Source

fn from_usize(value: usize) -> Self

Creates a BigRational from a usize numerator with denominator 1.

Source

fn from_frac_usize(numerator: usize, denominator: usize) -> Self

Creates a BigRational from a usize numerator and denominator.

§Panics

Panics if the denominator is zero.

Source

fn log2_ceil(&self, binary_digits: usize) -> BigRational

Computes the ceiling of log2 of this rational number with specified binary precision.

Returns log2(x) rounded up to the nearest value representable with binary_digits fractional bits in binary representation.

§Panics

Panics if the rational number is non-positive.

§Examples
use num_rational::BigRational;
use commonware_utils::rational::BigRationalExt;

let x = BigRational::from_frac_u64(3, 1); // 3
let result = x.log2_ceil(4);
// log2(3) ≈ 1.585, the algorithm computes a ceiling approximation
assert!(result >= BigRational::from_u64(1));
assert!(result <= BigRational::from_u64(2));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl BigRationalExt for BigRational

Source§

fn from_u64(value: u64) -> Self

Source§

fn from_frac_u64(numerator: u64, denominator: u64) -> Self

Source§

fn from_u128(value: u128) -> Self

Source§

fn from_frac_u128(numerator: u128, denominator: u128) -> Self

Source§

fn ceil_to_u128(&self) -> Option<u128>

Source§

fn from_usize(value: usize) -> Self

Source§

fn from_frac_usize(numerator: usize, denominator: usize) -> Self

Source§

fn log2_ceil(&self, binary_digits: usize) -> BigRational

Implementors§