Skip to main content

Rational

Struct Rational 

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

A rational number represented as a fraction (numerator / denominator).

This type is commonly used to represent:

  • Time bases (e.g., 1/90000 for MPEG-TS, 1/1000 for milliseconds)
  • Frame rates (e.g., 30000/1001 for 29.97 fps)
  • Aspect ratios (e.g., 16/9)

§Invariants

  • Denominator is always positive (sign is in numerator)
  • Zero denominator is handled gracefully (returns infinity/NaN for conversions)

§Examples

use ff_format::Rational;

// Common time base for MPEG-TS
let time_base = Rational::new(1, 90000);

// 29.97 fps (NTSC)
let fps = Rational::new(30000, 1001);
assert!((fps.as_f64() - 29.97).abs() < 0.01);

// Invert to get frame duration
let frame_duration = fps.invert();
assert_eq!(frame_duration.num(), 1001);
assert_eq!(frame_duration.den(), 30000);

Implementations§

Source§

impl Rational

Source

pub const fn new(num: i32, den: i32) -> Rational

Creates a new rational number.

The denominator is normalized to always be positive (the sign is moved to the numerator).

§Panics

Does not panic. A zero denominator is allowed but will result in infinity or NaN when converted to floating-point.

§Examples
use ff_format::Rational;

let r = Rational::new(1, 2);
assert_eq!(r.num(), 1);
assert_eq!(r.den(), 2);

// Negative denominator is normalized
let r = Rational::new(1, -2);
assert_eq!(r.num(), -1);
assert_eq!(r.den(), 2);
Source

pub const fn zero() -> Rational

Creates a rational number representing zero (0/1).

§Examples
use ff_format::Rational;

let zero = Rational::zero();
assert_eq!(zero.as_f64(), 0.0);
assert!(zero.is_zero());
Source

pub const fn one() -> Rational

Creates a rational number representing one (1/1).

§Examples
use ff_format::Rational;

let one = Rational::one();
assert_eq!(one.as_f64(), 1.0);
Source

pub const fn num(&self) -> i32

Returns the numerator.

§Examples
use ff_format::Rational;

let r = Rational::new(3, 4);
assert_eq!(r.num(), 3);
Source

pub const fn den(&self) -> i32

Returns the denominator.

The denominator is always non-negative.

§Examples
use ff_format::Rational;

let r = Rational::new(3, 4);
assert_eq!(r.den(), 4);
Source

pub fn as_f64(self) -> f64

Converts the rational number to a floating-point value.

Returns f64::INFINITY, f64::NEG_INFINITY, or f64::NAN for edge cases (division by zero).

§Examples
use ff_format::Rational;

let r = Rational::new(1, 4);
assert_eq!(r.as_f64(), 0.25);

let r = Rational::new(1, 3);
assert!((r.as_f64() - 0.333333).abs() < 0.001);
Source

pub fn as_f32(self) -> f32

Converts the rational number to a single-precision floating-point value.

§Examples
use ff_format::Rational;

let r = Rational::new(1, 2);
assert_eq!(r.as_f32(), 0.5);
Source

pub const fn invert(self) -> Rational

Returns the inverse (reciprocal) of this rational number.

§Examples
use ff_format::Rational;

let r = Rational::new(3, 4);
let inv = r.invert();
assert_eq!(inv.num(), 4);
assert_eq!(inv.den(), 3);

// Negative values
let r = Rational::new(-3, 4);
let inv = r.invert();
assert_eq!(inv.num(), -4);
assert_eq!(inv.den(), 3);
Source

pub const fn is_zero(self) -> bool

Returns true if this rational number is zero.

§Examples
use ff_format::Rational;

assert!(Rational::new(0, 1).is_zero());
assert!(Rational::new(0, 100).is_zero());
assert!(!Rational::new(1, 100).is_zero());
Source

pub const fn is_positive(self) -> bool

Returns true if this rational number is positive.

§Examples
use ff_format::Rational;

assert!(Rational::new(1, 2).is_positive());
assert!(!Rational::new(-1, 2).is_positive());
assert!(!Rational::new(0, 1).is_positive());
Source

pub const fn is_negative(self) -> bool

Returns true if this rational number is negative.

§Examples
use ff_format::Rational;

assert!(Rational::new(-1, 2).is_negative());
assert!(!Rational::new(1, 2).is_negative());
assert!(!Rational::new(0, 1).is_negative());
Source

pub const fn abs(self) -> Rational

Returns the absolute value of this rational number.

§Examples
use ff_format::Rational;

assert_eq!(Rational::new(-3, 4).abs(), Rational::new(3, 4));
assert_eq!(Rational::new(3, 4).abs(), Rational::new(3, 4));
Source

pub fn reduce(self) -> Rational

Reduces the rational to its lowest terms using GCD.

§Examples
use ff_format::Rational;

let r = Rational::new(4, 8);
let reduced = r.reduce();
assert_eq!(reduced.num(), 1);
assert_eq!(reduced.den(), 2);

Trait Implementations§

Source§

impl Add for Rational

Source§

type Output = Rational

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Rational) -> <Rational as Add>::Output

Performs the + operation. Read more
Source§

impl Clone for Rational

Source§

fn clone(&self) -> Rational

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Rational

Source§

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

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

impl Default for Rational

Source§

fn default() -> Rational

Returns the default rational number (1/1).

Source§

impl Display for Rational

Source§

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

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

impl Div<i32> for Rational

Source§

type Output = Rational

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i32) -> <Rational as Div<i32>>::Output

Performs the / operation. Read more
Source§

impl Div for Rational

Source§

type Output = Rational

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Rational) -> <Rational as Div>::Output

Performs the / operation. Read more
Source§

impl From<(i32, i32)> for Rational

Source§

fn from(_: (i32, i32)) -> Rational

Converts to this type from the input type.
Source§

impl From<i32> for Rational

Source§

fn from(n: i32) -> Rational

Converts to this type from the input type.
Source§

impl Hash for Rational

Source§

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

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 Mul<i32> for Rational

Source§

type Output = Rational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i32) -> <Rational as Mul<i32>>::Output

Performs the * operation. Read more
Source§

impl Mul for Rational

Source§

type Output = Rational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Rational) -> <Rational as Mul>::Output

Performs the * operation. Read more
Source§

impl Neg for Rational

Source§

type Output = Rational

The resulting type after applying the - operator.
Source§

fn neg(self) -> <Rational as Neg>::Output

Performs the unary - operation. Read more
Source§

impl Ord for Rational

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · 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 Rational

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Rational

Source§

fn partial_cmp(&self, other: &Rational) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Sub for Rational

Source§

type Output = Rational

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Rational) -> <Rational as Sub>::Output

Performs the - operation. Read more
Source§

impl Copy for Rational

Source§

impl Eq for Rational

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