Simplify types for ration impls
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
This commit is contained in:
parent
3323c2ff23
commit
4f5dcddcc5
@ -99,7 +99,9 @@ pub trait Int:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A Trait representing unsigned integer primitives
|
/// A Trait representing unsigned integer primitives
|
||||||
pub trait Unsigned: Int {
|
pub trait Unsigned:
|
||||||
|
Int + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
|
||||||
|
{
|
||||||
/// Find the greatest common denominator of two numbers
|
/// Find the greatest common denominator of two numbers
|
||||||
fn gcd(a: Self, b: Self) -> Self;
|
fn gcd(a: Self, b: Self) -> Self;
|
||||||
|
|
||||||
|
@ -144,19 +144,13 @@ impl<T: Unsigned> Frac<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> PartialOrd for Frac<T>
|
impl<T: Unsigned> PartialOrd for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Ord for Frac<T>
|
impl<T: Unsigned> Ord for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
if self.sign != other.sign {
|
if self.sign != other.sign {
|
||||||
return if self.sign == Positive {
|
return if self.sign == Positive {
|
||||||
@ -191,10 +185,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Mul for Frac<T>
|
impl<T: Unsigned> Mul for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, rhs: Self) -> Self {
|
fn mul(self, rhs: Self) -> Self {
|
||||||
@ -206,19 +197,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> MulAssign for Frac<T>
|
impl<T: Unsigned> MulAssign for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn mul_assign(&mut self, rhs: Self) {
|
fn mul_assign(&mut self, rhs: Self) {
|
||||||
*self = *self * rhs
|
*self = *self * rhs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Div for Frac<T>
|
impl<T: Unsigned> Div for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn div(self, rhs: Self) -> Self {
|
fn div(self, rhs: Self) -> Self {
|
||||||
@ -230,19 +215,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DivAssign for Frac<T>
|
impl<T: Unsigned> DivAssign for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn div_assign(&mut self, rhs: Self) {
|
fn div_assign(&mut self, rhs: Self) {
|
||||||
*self = *self / rhs
|
*self = *self / rhs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Add for Frac<T>
|
impl<T: Unsigned> Add for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
@ -276,19 +255,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> AddAssign for Frac<T>
|
impl<T: Unsigned> AddAssign for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn add_assign(&mut self, rhs: Self) {
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
*self = *self + rhs
|
*self = *self + rhs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Sub for Frac<T>
|
impl<T: Unsigned> Sub for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
@ -324,10 +297,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SubAssign for Frac<T>
|
impl<T: Unsigned> SubAssign for Frac<T> {
|
||||||
where
|
|
||||||
T: Unsigned + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
|
|
||||||
{
|
|
||||||
fn sub_assign(&mut self, rhs: Self) {
|
fn sub_assign(&mut self, rhs: Self) {
|
||||||
*self = *self - rhs
|
*self = *self - rhs
|
||||||
}
|
}
|
||||||
@ -368,8 +338,8 @@ mod tests {
|
|||||||
assert_eq!(frac!(3 / 2), frac!(1 1/2));
|
assert_eq!(frac!(3 / 2), frac!(1 1/2));
|
||||||
assert_eq!(frac!(3 / 1), frac!(3));
|
assert_eq!(frac!(3 / 1), frac!(3));
|
||||||
|
|
||||||
assert_eq!(-frac!(1/2), frac!(-1/2));
|
assert_eq!(-frac!(1 / 2), frac!(-1 / 2));
|
||||||
assert_eq!(-frac!(1/2), frac!(1/-2));
|
assert_eq!(-frac!(1 / 2), frac!(1 / -2));
|
||||||
assert_eq!(frac!(1/2), frac!(-1/-2));
|
assert_eq!(frac!(1 / 2), frac!(-1 / -2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ fn sub_test() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cmp_test() {
|
fn cmp_test() {
|
||||||
assert!(frac!(1/2) <= frac!(1/2));
|
assert!(frac!(1 / 2) <= frac!(1 / 2));
|
||||||
assert!(frac!(0) < frac!(1));
|
assert!(frac!(0) < frac!(1));
|
||||||
assert!(-frac!(5 / 3) < frac!(1 / 10_000));
|
assert!(-frac!(5 / 3) < frac!(1 / 10_000));
|
||||||
assert!(frac!(1 / 10_000) > -frac!(10));
|
assert!(frac!(1 / 10_000) > -frac!(10));
|
||||||
@ -67,22 +67,22 @@ fn zero_denom() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn op_assign() {
|
fn op_assign() {
|
||||||
// Addition
|
// Addition
|
||||||
let mut quart = frac!(1/4);
|
let mut quart = frac!(1 / 4);
|
||||||
quart += frac!(1/4);
|
quart += frac!(1 / 4);
|
||||||
assert_eq!(frac!(1/2), quart);
|
assert_eq!(frac!(1 / 2), quart);
|
||||||
|
|
||||||
// Subtraction
|
// Subtraction
|
||||||
let mut half = frac!(1/2);
|
let mut half = frac!(1 / 2);
|
||||||
half -= frac!(1/4);
|
half -= frac!(1 / 4);
|
||||||
assert_eq!(frac!(1/4), half);
|
assert_eq!(frac!(1 / 4), half);
|
||||||
|
|
||||||
// Multiplication
|
// Multiplication
|
||||||
let mut half = frac!(1/2);
|
let mut half = frac!(1 / 2);
|
||||||
half *= frac!(1/2);
|
half *= frac!(1 / 2);
|
||||||
assert_eq!(frac!(1/4), half);
|
assert_eq!(frac!(1 / 4), half);
|
||||||
|
|
||||||
// Division
|
// Division
|
||||||
let mut quart = frac!(1/4);
|
let mut quart = frac!(1 / 4);
|
||||||
quart /= frac!(4);
|
quart /= frac!(4);
|
||||||
assert_eq!(frac!(1/16), quart);
|
assert_eq!(frac!(1 / 16), quart);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user