Skip to content

Commit

Permalink
add from<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Mar 21, 2024
1 parent 1e7d799 commit a3e0f15
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[package]
edition = "2021"
name = "fractions-rs"
version = "0.1.2"
version = "0.1.3"
authors = ["Wai-Shing Luk"]
exclude = [
"/bors.toml",
Expand Down
18 changes: 14 additions & 4 deletions src/fractions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<T: Integer + Zero + Neg<Output = T> + Ord + Copy> Fraction<T> {
}
}

impl<T: Integer + One> Fraction<T> {
impl<T: Integer + One> From<T> for Fraction<T> {
/// The `from` function in Rust creates a `Fraction` struct from an integer.
///
/// Arguments:
Expand All @@ -399,11 +399,11 @@ impl<T: Integer + One> Fraction<T> {
///
/// ```rust
/// use fractions::Fraction;
/// let mut f = Fraction::from(3);
/// assert_eq!(f, Fraction::new(3, 1));
/// let mut f = Fraction::<i32>::from(3);
/// assert_eq!(f, Fraction::<i32>::new(3, 1));
/// ```
#[inline]
pub fn from(numer: T) -> Self {
fn from(numer: T) -> Self {
Fraction {
numer,
denom: One::one(),
Expand Down Expand Up @@ -435,6 +435,16 @@ impl<T: Integer + One + Zero> Default for Fraction<T> {
}
}

// From integer
// impl<T> From<T> for Fraction<T>
// where
// T: Clone + Integer,
// {
// fn from(x: T) -> Fraction<T> {
// Fraction::new_raw(x, One::one())
// }
// }

impl<T: Integer + Copy> Fraction<T> {
/// The `cross` function calculates the cross product of two values.
///
Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ pub mod fractions;
pub use crate::fractions::Fraction;
pub use crate::fractions::{const_abs, const_gcd};

use core::ops::{Add, Mul, Sub};

/// The function `archimedes` calculates the area of a triangle using Archimedes' formula with the
/// lengths of the three sides provided as `Fraction<i64>` values.
///
/// Arguments:
///
/// * `q_1`: Represents the length of the first side of the triangle.
/// * `q_2`: The parameters `q_1`, `q_2`, and `q_3` represent the lengths of the sides of a triangle. In
/// the context of Archimedes' formula for the area of a triangle, `q_1`, `q_2`, and `q_3`
/// * `q_3`: The parameter `q_3` represents the length of the third side of the triangle.
///
/// Returns:
///
/// The function `archimedes` returns the area of a triangle computed using Archimedes' formula, given
/// the lengths of the 3 sides.
///
#[inline]
pub fn archimedes<T>(q_1: &T, q_2: &T, q_3: &T) -> T
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + From<i32>,
{
let temp = *q_1 + *q_2 - *q_3;
T::from(4) * *q_1 * *q_2 - temp * temp
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -200,4 +226,12 @@ mod tests {
let q = Fraction::new(n2 as i128, 2000000009_i128);
p == (p + q) - q
}

#[test]
fn test_archimedes4() {
let q_1 = Fraction::<i32>::new(1, 2);
let q_2 = Fraction::<i32>::new(1, 4);
let q_3 = Fraction::<i32>::new(1, 6);
assert_eq!(archimedes(&q_1, &q_2, &q_3), Fraction::<i32>::new(23, 144));
}
}

0 comments on commit a3e0f15

Please sign in to comment.