basics arifmetic
This commit is contained in:
1 parent
c0b72c71cc
commit
5965a89979
3 files changed
+113
-17
No files matched your search
+70
-8
@@ -1,16 +1,34 @@
|
||||
use std::ops::Add;
|
||||
use std::ops::Sub;
|
||||
use std::ops::Mul;
|
||||
use std::ops::Div;
|
||||
use std::ops::Neg;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
use super::gcd;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Frac{
|
||||
num: u32,
|
||||
den: u32,
|
||||
num: i32,
|
||||
den: i32,
|
||||
}
|
||||
|
||||
impl Frac {
|
||||
pub fn new(n: u32, d: u32) -> Self {
|
||||
pub fn new(n: i32, d: i32) -> Self {
|
||||
let common = gcd(n, d);
|
||||
|
||||
if common == 0 {
|
||||
return Frac::zero();
|
||||
}
|
||||
|
||||
Frac {
|
||||
num: n,
|
||||
den: d,
|
||||
num: n/common,
|
||||
den: d/common,
|
||||
}
|
||||
}
|
||||
pub fn zero() -> Self {
|
||||
Self {
|
||||
num: 0,
|
||||
den: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,9 +38,53 @@ impl Add for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Frac::new(
|
||||
self.num * other.den + other.num * self.den,
|
||||
self.den * other.den
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: Self) -> Self::Output {
|
||||
Frac::new(
|
||||
self.num * other.den + -other.num * self.den,
|
||||
self.den * other.den
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self::Output {
|
||||
Frac::new(
|
||||
self.num * other.num,
|
||||
self.den * other.den
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Div for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self::Output {
|
||||
Frac::new(
|
||||
self.num * other.den,
|
||||
self.den * other.num
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Frac {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Self {
|
||||
num: self.num * other.den + other.num * self.den,
|
||||
den: self.den * other.den,
|
||||
num: -self.num,
|
||||
den: self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-4
@@ -1,12 +1,43 @@
|
||||
pub fn gcd(mut a: u64, mut b: u64) -> u64 {
|
||||
/*pub fn gcd(a: u32, b: u32) -> u32 {
|
||||
if a == 0 { return b }
|
||||
if b == 0 { return a }
|
||||
|
||||
let shift = (a | b).trailing_zeros();
|
||||
|
||||
let a = a >> a.trailing_zeros();
|
||||
|
||||
gcd_loop(a, b, shift)
|
||||
}
|
||||
|
||||
fn gcd_loop(a: u32, mut b: u32, shift: u32) -> u32 {
|
||||
if b == 0 {
|
||||
return a << shift;
|
||||
}
|
||||
|
||||
b >>= b.trailing_zeros();
|
||||
|
||||
if a > b {
|
||||
gcd_loop(b, a - b, shift)
|
||||
} else {
|
||||
gcd_loop(a, b - a, shift)
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn gcd(a: i32, b: i32) -> i32 {
|
||||
let mut a = a.abs();
|
||||
let mut b = b.abs();
|
||||
|
||||
if a == 0 { return b; }
|
||||
if b == 0 { return a; }
|
||||
|
||||
let shift = (a | b).trailing_zeros();
|
||||
|
||||
a >>= a.trailing_zeros();
|
||||
|
||||
while b != 0 {
|
||||
loop {
|
||||
b >>= b.trailing_zeros();
|
||||
|
||||
if a > b {
|
||||
@@ -14,7 +45,9 @@ pub fn gcd(mut a: u64, mut b: u64) -> u64 {
|
||||
}
|
||||
|
||||
b -= a;
|
||||
}
|
||||
|
||||
a << shift
|
||||
if b == 0 {
|
||||
return a << shift;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user