Struct Rational

Source
pub struct Rational<T>
where T: Signed,
{ pub num: T, pub den: T, }

Fields§

§num: T§den: T

Implementations§

Source§

impl<T> Rational<T>
where T: Signed,

Source

pub fn new(num: T, den: T) -> Self

Examples found in repository?
crates/competitive/src/num/rational.rs (line 82)
78    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Rational<U>
79    where
80        U: Signed,
81    {
82        Rational::new(f(self.num), f(self.den))
83    }
84    pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> Rational<U>
85    where
86        U: Signed,
87    {
88        Rational::new_unchecked(f(self.num), f(self.den))
89    }
90    pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
91    where
92        U: Div,
93    {
94        f(self.num) / f(self.den)
95    }
96}
97
98impl<T> Bounded for Rational<T>
99where
100    T: Signed,
101{
102    fn maximum() -> Self {
103        Self::new_unchecked(T::one(), T::zero())
104    }
105    fn minimum() -> Self {
106        Self::new_unchecked(-T::one(), T::zero())
107    }
108}
109
110impl<T> Zero for Rational<T>
111where
112    T: Signed,
113{
114    fn zero() -> Self {
115        Self::new_unchecked(T::zero(), T::one())
116    }
117}
118impl<T> One for Rational<T>
119where
120    T: Signed,
121{
122    fn one() -> Self {
123        Self::new_unchecked(T::one(), T::one())
124    }
125}
126
127impl<T> Add for Rational<T>
128where
129    T: Signed,
130{
131    type Output = Self;
132    fn add(self, rhs: Self) -> Self::Output {
133        Self::new(self.num * rhs.den + self.den * rhs.num, self.den * rhs.den)
134    }
135}
136impl<T> Sub for Rational<T>
137where
138    T: Signed,
139{
140    type Output = Self;
141    fn sub(self, rhs: Self) -> Self::Output {
142        Self::new(self.num * rhs.den - self.den * rhs.num, self.den * rhs.den)
143    }
144}
145impl<T> Mul for Rational<T>
146where
147    T: Signed,
148{
149    type Output = Self;
150    fn mul(self, rhs: Self) -> Self::Output {
151        Self::new(self.num * rhs.num, self.den * rhs.den)
152    }
153}
154impl<T> Div for Rational<T>
155where
156    T: Signed,
157{
158    type Output = Self;
159    fn div(self, rhs: Self) -> Self::Output {
160        Self::new(self.num * rhs.den, self.den * rhs.num)
161    }
Source

pub fn new_unchecked(num: T, den: T) -> Self

Examples found in repository?
crates/competitive/src/num/rational.rs (line 67)
64    pub fn new(num: T, den: T) -> Self {
65        let g = num.abs().unsigned().gcd(den.abs().unsigned()).signed();
66        let g = if den.is_negative() { -g } else { g };
67        Self::new_unchecked(num / g, den / g)
68    }
69    pub fn new_unchecked(num: T, den: T) -> Self {
70        Self { num, den }
71    }
72    pub fn abs(self) -> Self {
73        Self::new_unchecked(self.num.abs(), self.den)
74    }
75    pub fn eval(self) -> T {
76        self.num / self.den
77    }
78    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Rational<U>
79    where
80        U: Signed,
81    {
82        Rational::new(f(self.num), f(self.den))
83    }
84    pub fn map_unchecked<U>(self, mut f: impl FnMut(T) -> U) -> Rational<U>
85    where
86        U: Signed,
87    {
88        Rational::new_unchecked(f(self.num), f(self.den))
89    }
90    pub fn map_eval<U>(self, mut f: impl FnMut(T) -> U) -> <U as Div>::Output
91    where
92        U: Div,
93    {
94        f(self.num) / f(self.den)
95    }
96}
97
98impl<T> Bounded for Rational<T>
99where
100    T: Signed,
101{
102    fn maximum() -> Self {
103        Self::new_unchecked(T::one(), T::zero())
104    }
105    fn minimum() -> Self {
106        Self::new_unchecked(-T::one(), T::zero())
107    }
108}
109
110impl<T> Zero for Rational<T>
111where
112    T: Signed,
113{
114    fn zero() -> Self {
115        Self::new_unchecked(T::zero(), T::one())
116    }
117}
118impl<T> One for Rational<T>
119where
120    T: Signed,
121{
122    fn one() -> Self {
123        Self::new_unchecked(T::one(), T::one())
124    }
125}
126
127impl<T> Add for Rational<T>
128where
129    T: Signed,
130{
131    type Output = Self;
132    fn add(self, rhs: Self) -> Self::Output {
133        Self::new(self.num * rhs.den + self.den * rhs.num, self.den * rhs.den)
134    }
135}
136impl<T> Sub for Rational<T>
137where
138    T: Signed,
139{
140    type Output = Self;
141    fn sub(self, rhs: Self) -> Self::Output {
142        Self::new(self.num * rhs.den - self.den * rhs.num, self.den * rhs.den)
143    }
144}
145impl<T> Mul for Rational<T>
146where
147    T: Signed,
148{
149    type Output = Self;
150    fn mul(self, rhs: Self) -> Self::Output {
151        Self::new(self.num * rhs.num, self.den * rhs.den)
152    }
153}
154impl<T> Div for Rational<T>
155where
156    T: Signed,
157{
158    type Output = Self;
159    fn div(self, rhs: Self) -> Self::Output {
160        Self::new(self.num * rhs.den, self.den * rhs.num)
161    }
162}
163impl<T> Neg for Rational<T>
164where
165    T: Signed,
166{
167    type Output = Self;
168    fn neg(self) -> Self::Output {
169        Self::new_unchecked(-self.num, self.den)
170    }
Source

pub fn abs(self) -> Self

Source

pub fn eval(self) -> T

Source

pub fn map<U>(self, f: impl FnMut(T) -> U) -> Rational<U>
where U: Signed,

Source

pub fn map_unchecked<U>(self, f: impl FnMut(T) -> U) -> Rational<U>
where U: Signed,

Source

pub fn map_eval<U>(self, f: impl FnMut(T) -> U) -> <U as Div>::Output
where U: Div,

Trait Implementations§

Source§

impl<T> Add for Rational<T>
where T: Signed,

Source§

type Output = Rational<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T> AddAssign for Rational<T>
where T: Signed,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<T> Bounded for Rational<T>
where T: Signed,

Source§

fn maximum() -> Self

Source§

fn minimum() -> Self

Source§

fn is_maximum(&self) -> bool

Source§

fn is_minimum(&self) -> bool

Source§

fn set_maximum(&mut self)

Source§

fn set_minimum(&mut self)

Source§

impl<T> Clone for Rational<T>
where T: Signed + Clone,

Source§

fn clone(&self) -> Rational<T>

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<T> Debug for Rational<T>
where T: Signed + Debug,

Source§

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

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

impl<T> Div for Rational<T>
where T: Signed,

Source§

type Output = Rational<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> DivAssign for Rational<T>
where T: Signed,

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl<T> Mul for Rational<T>
where T: Signed,

Source§

type Output = Rational<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> MulAssign for Rational<T>
where T: Signed,

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<T> Neg for Rational<T>
where T: Signed,

Source§

type Output = Rational<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T> One for Rational<T>
where T: Signed,

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

Source§

impl<T> Ord for Rational<T>
where T: Signed,

Source§

fn cmp(&self, other: &Self) -> 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<T> PartialEq for Rational<T>
where T: Signed,

Source§

fn eq(&self, other: &Self) -> 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<T> PartialOrd for Rational<T>
where T: Signed,

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 · 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<T> Sub for Rational<T>
where T: Signed,

Source§

type Output = Rational<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> SubAssign for Rational<T>
where T: Signed,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T> Zero for Rational<T>
where T: Signed,

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool
where Self: PartialEq,

Source§

fn set_zero(&mut self)

Source§

impl<T> Copy for Rational<T>
where T: Signed + Copy,

Source§

impl<T> Eq for Rational<T>
where T: Signed,

Auto Trait Implementations§

§

impl<T> Freeze for Rational<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Rational<T>
where T: RefUnwindSafe,

§

impl<T> Send for Rational<T>
where T: Send,

§

impl<T> Sync for Rational<T>
where T: Sync,

§

impl<T> Unpin for Rational<T>
where T: Unpin,

§

impl<T> UnwindSafe for Rational<T>
where T: UnwindSafe,

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> AsTotalOrd for T
where T: PartialOrd,

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> PartialOrdExt for T
where T: PartialOrd,

Source§

fn chmin(&mut self, other: T)

Source§

fn chmax(&mut self, other: T)

Source§

fn minmax(self, other: T) -> (T, T)

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.