fractions/
lib.rs

1//! Fraction numbers
2//!
3//! ## Compatibility
4//!
5//! The `fractions-rs` crate is tested for rustc 1.31 and greater.
6
7// #![no_std]
8// Fraction ops often use other "suspicious" ops
9#![allow(clippy::suspicious_arithmetic_impl)]
10#![allow(clippy::suspicious_op_assign_impl)]
11
12pub mod fractions;
13pub use crate::fractions::Fraction;
14pub use crate::fractions::{const_abs, const_gcd};
15
16use core::ops::{Add, Mul, Sub};
17
18/// The function `archimedes` calculates the area of a triangle using Archimedes' formula with the
19/// lengths of the three sides provided as `Fraction<i64>` values.
20///
21/// Arguments:
22///
23/// * `q_1`: Represents the length of the first side of the triangle.
24/// * `q_2`: The parameters `q_1`, `q_2`, and `q_3` represent the lengths of the sides of a triangle. In
25///          the context of Archimedes' formula for the area of a triangle, `q_1`, `q_2`, and `q_3`
26/// * `q_3`: The parameter `q_3` represents the length of the third side of the triangle.
27///
28/// Returns:
29///
30/// The function `archimedes` returns the area of a triangle computed using Archimedes' formula, given
31/// the lengths of the 3 sides.
32///
33#[inline]
34pub fn archimedes<T>(q_1: &T, q_2: &T, q_3: &T) -> T
35where
36    T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + From<i32>,
37{
38    let temp = *q_1 + *q_2 - *q_3;
39    T::from(4) * *q_1 * *q_2 - temp * temp
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_archimedes4() {
48        let q_1 = Fraction::<i32>::new(1, 2);
49        let q_2 = Fraction::<i32>::new(1, 4);
50        let q_3 = Fraction::<i32>::new(1, 6);
51        assert_eq!(archimedes(&q_1, &q_2, &q_3), Fraction::<i32>::new(23, 144));
52    }
53
54    #[test]
55    fn test_archimedes5() {
56        let q_1 = Fraction::<i64>::new(1, 2);
57        let q_2 = Fraction::<i64>::new(1, 4);
58        let q_3 = Fraction::<i64>::new(1, 6);
59        assert_eq!(archimedes(&q_1, &q_2, &q_3), Fraction::<i64>::new(23, 144));
60    }
61}
62
63#[cfg(test)]
64mod more_tests;