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,
impl<T> Rational<T>where
T: Signed,
Sourcepub fn new(num: T, den: T) -> Self
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 }
Sourcepub fn new_unchecked(num: T, den: T) -> Self
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 }
pub fn abs(self) -> Self
pub fn eval(self) -> T
pub fn map<U>(self, f: impl FnMut(T) -> U) -> Rational<U>where
U: Signed,
pub fn map_unchecked<U>(self, f: impl FnMut(T) -> U) -> Rational<U>where
U: Signed,
pub fn map_eval<U>(self, f: impl FnMut(T) -> U) -> <U as Div>::Outputwhere
U: Div,
Trait Implementations§
Source§impl<T> AddAssign for Rational<T>where
T: Signed,
impl<T> AddAssign for Rational<T>where
T: Signed,
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moreSource§impl<T> Bounded for Rational<T>where
T: Signed,
impl<T> Bounded for Rational<T>where
T: Signed,
fn maximum() -> Self
fn minimum() -> Self
fn is_maximum(&self) -> bool
fn is_minimum(&self) -> bool
fn set_maximum(&mut self)
fn set_minimum(&mut self)
Source§impl<T> DivAssign for Rational<T>where
T: Signed,
impl<T> DivAssign for Rational<T>where
T: Signed,
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/=
operation. Read moreSource§impl<T> MulAssign for Rational<T>where
T: Signed,
impl<T> MulAssign for Rational<T>where
T: Signed,
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*=
operation. Read moreSource§impl<T> Ord for Rational<T>where
T: Signed,
impl<T> Ord for Rational<T>where
T: Signed,
Source§impl<T> PartialOrd for Rational<T>where
T: Signed,
impl<T> PartialOrd for Rational<T>where
T: Signed,
Source§impl<T> SubAssign for Rational<T>where
T: Signed,
impl<T> SubAssign for Rational<T>where
T: Signed,
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-=
operation. Read moreimpl<T> Copy for Rational<T>
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> AsTotalOrd for Twhere
T: PartialOrd,
impl<T> AsTotalOrd for Twhere
T: PartialOrd,
fn as_total_ord(&self) -> TotalOrd<&T>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more