Skip to main content

Ratio

Struct Ratio 

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

An exact non-negative rational over Ticks, always in lowest terms.

Non-negative because the value domain is unsigned (Rule Z, N12) and every quantity this crate builds on rationals — durations, ratios of periods, redshifts, density parameters — is non-negative. Differences that could go negative are taken with Ratio::abs_diff, which makes the sign loss explicit at the call site rather than silent in the type.

Arithmetic is exact. Where a result would leave the domain the operation fails with UCAL-E0021; it never wraps and never approximates (Rules O, E). Operands are pre-reduced by common factors before multiplying, which keeps intermediates small enough that overflow is rare in practice without ever making it silent.

Implementations§

Source§

impl Ratio

Source

pub fn new(num: Ticks, den: Ticks) -> Result<Ratio>

Construct and reduce. UCAL-E0070 if the denominator is zero.

Source

pub fn from_int(n: Ticks) -> Ratio

A whole number.

Source

pub fn from_u64(n: u64) -> Ratio

A small whole number.

Source

pub fn zero() -> Ratio

Zero.

Source

pub fn one() -> Ratio

One.

Source

pub fn from_decimal_str(s: &str) -> Result<Ratio>

Parse an exact decimal such as "365.242190" or "1089.80".

§10.2 requires redshift inputs to parse exactly: 1100, 1089.80 and 0.5 are all exact rationals, and there is no float path in or out. Rejects anything that is not digits with at most one decimal point.

Source

pub fn numer(&self) -> &Ticks

The numerator, in lowest terms.

Source

pub fn denom(&self) -> &Ticks

The denominator, in lowest terms. Never zero.

Source

pub fn is_zero(&self) -> bool

Whether this is exactly zero.

Source

pub fn is_integer(&self) -> bool

Whether the value is a whole number.

Source

pub fn floor(&self) -> Ticks

The whole part, truncated toward zero.

Source

pub fn frac(&self) -> Ratio

The fractional part, self - floor(self).

Source

pub fn add(&self, other: &Ratio) -> Result<Ratio>

self + other, exact.

Source

pub fn sub(&self, other: &Ratio) -> Result<Ratio>

self - other. UCAL-E0020 if the result would be negative — the type is non-negative by construction, so this is Rule Z applied to rationals.

Source

pub fn abs_diff(&self, other: &Ratio) -> Result<Ratio>

|self - other|, exact.

Source

pub fn mul(&self, other: &Ratio) -> Result<Ratio>

self * other, exact.

Source

pub fn div(&self, other: &Ratio) -> Result<Ratio>

self / other. UCAL-E0070 if other is zero.

Source

pub fn recip(&self) -> Result<Ratio>

1 / self. UCAL-E0070 if self is zero.

Source

pub fn cmp_exact(&self, other: &Ratio) -> Ordering

Exact comparison.

Cross-multiplies in the widened type, so comparison is total and can never fail for overflow — which matters, because a comparison that could error would make Ratio unusable as an interval endpoint.

Source

pub fn snap(&self, digits: u32, mode: Rounding) -> Result<Ratio>

The closest rational with denominator 10^digits, rounded in the stated direction.

Exact rational accumulation is unbounded. Summing n terms whose denominators are mutually coprime grows the common denominator like their product, so a few dozen terms is enough to overflow any fixed-width integer — long before a quadrature sum of thousands of panels finishes.

A certified sum therefore snaps each partial result back to a fixed decimal grid, rounding outward: Rounding::Trunc for a value that bounds from below, Rounding::Ceil for one that bounds from above. The accumulator stays bounded and the enclosure stays rigorous, at the cost of one grid step per snap — which is why callers choose a grid far finer than the width they are trying to certify.

This is not a rendering: the result is still an exact Ratio, and Rule R is untouched. It is a deliberate outward relaxation, and the only place this crate discards information inside a computation rather than at its edge.

Source

pub fn to_decimal_string(&self, digits: u32, mode: Rounding) -> Result<String>

Render to digits decimal places under an explicit mode (Rule R).

This is the only place a Ratio becomes inexact, and it is a rendering, never a construction. UCAL-W0001 territory: the caller is told the mode because it had to choose one.

Source

pub fn to_ratio_string(&self) -> String

Render as numerator/denominator, which is always exact.

Trait Implementations§

Source§

impl Clone for Ratio

Source§

fn clone(&self) -> Ratio

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Ratio

Source§

impl Debug for Ratio

Source§

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

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

impl Eq for Ratio

Source§

impl Ord for Ratio

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Ratio

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Ratio

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 StructuralPartialEq for Ratio

Auto Trait Implementations§

§

impl Freeze for Ratio

§

impl RefUnwindSafe for Ratio

§

impl Send for Ratio

§

impl Sync for Ratio

§

impl Unpin for Ratio

§

impl UnsafeUnpin for Ratio

§

impl UnwindSafe for Ratio

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<U> As for U

Source§

fn as_<T>(self) -> T
where T: CastFrom<U>, U: Sized,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. 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> 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, 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.