Skip to content

Commit

Permalink
Make Ratio Copy and Clone (#1)
Browse files Browse the repository at this point in the history
* make ratio `Copy` and `Clone`
* version 0.1.1
  • Loading branch information
aarkegz authored Dec 20, 2024
1 parent d7238b7 commit c753d34
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "int_ratio"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Yuekai Jia <[email protected]>"]
description = "The type of ratios represented by two integers."
Expand Down
30 changes: 27 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::{cmp::PartialEq, fmt};
/// improve precision.
///
/// Currently, it only supports `u32` as the numerator and denominator.
#[derive(Clone, Copy)]
pub struct Ratio {
numerator: u32,
denominator: u32,
Expand All @@ -19,6 +20,20 @@ pub struct Ratio {

impl Ratio {
/// The zero ratio.
///
/// It is a ratio of `0/0``, and behaves like a zero value in calculation. It
/// differs from other `0/x` ratios in that it does not panic when getting
/// the inverse ratio. Instead, it returns another zero ratio.
///
/// # Examples
///
/// ```
/// use int_ratio::Ratio;
///
/// let zero = Ratio::zero();
/// assert_eq!(zero.mul_trunc(123), 0);
/// assert_eq!(zero.inverse(), Ratio::zero());
/// ```
pub const fn zero() -> Self {
Self {
numerator: 0,
Expand All @@ -29,6 +44,10 @@ impl Ratio {
}

/// Creates a new ratio `numerator / denominator`.
///
/// # Panics
///
/// Panics if `denominator` is zero and `numerator` is not zero.
pub const fn new(numerator: u32, denominator: u32) -> Self {
assert!(!(denominator == 0 && numerator != 0));
if numerator == 0 {
Expand Down Expand Up @@ -73,8 +92,11 @@ impl Ratio {
///
/// let ratio = Ratio::new(1, 2);
/// assert_eq!(ratio.inverse(), Ratio::new(2, 1));
///
/// let zero = Ratio::zero();
/// assert_eq!(zero.inverse(), Ratio::zero());
/// ```
pub const fn inverse(&self) -> Self {
pub const fn inverse(self) -> Self {
Self::new(self.denominator, self.numerator)
}

Expand All @@ -89,7 +111,7 @@ impl Ratio {
/// assert_eq!(ratio.mul_trunc(99), 66); // 99 * 2 / 3 = 66
/// assert_eq!(ratio.mul_trunc(100), 66); // trunc(100 * 2 / 3) = trunc(66.66...) = 66
/// ```
pub const fn mul_trunc(&self, value: u64) -> u64 {
pub const fn mul_trunc(self, value: u64) -> u64 {
((value as u128 * self.mult as u128) >> self.shift) as u64
}

Expand All @@ -105,7 +127,7 @@ impl Ratio {
/// assert_eq!(ratio.mul_round(99), 66); // 99 * 2 / 3 = 66
/// assert_eq!(ratio.mul_round(100), 67); // round(100 * 2 / 3) = round(66.66...) = 67
/// ```
pub const fn mul_round(&self, value: u64) -> u64 {
pub const fn mul_round(self, value: u64) -> u64 {
((value as u128 * self.mult as u128 + (1 << self.shift >> 1)) >> self.shift) as u64
}
}
Expand Down Expand Up @@ -154,6 +176,8 @@ mod tests {
assert_eq!(c.shift, 0);
assert_eq!(c.mul_trunc(u32::MAX as _), u32::MAX as _);

assert_eq!(b.inverse(), d);

println!("{:?}", a);
println!("{:?}", b);
println!("{:?}", c);
Expand Down

0 comments on commit c753d34

Please sign in to comment.