Skip to content

Commit

Permalink
Length conversion (#830)
Browse files Browse the repository at this point in the history
* feat: length_conversion implemented

* link: added to DIRECTORY.md

* test: zero_to_zero test added

* test: length_of_one_meter added

* fix: apply @vil02 suggestions

* docs: remove redundant comments

---------

Co-authored-by: Piotr Idzik <[email protected]>
  • Loading branch information
ali77gh and vil02 authored Oct 27, 2024
1 parent 1af4efa commit 1b07a48
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs)
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
* [Length Conversions](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/length_conversion.rs)
* Data Structures
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
Expand Down
94 changes: 94 additions & 0 deletions src/conversions/length_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// Author : https://github.com/ali77gh
/// Conversion of length units.
///
/// Available Units:
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Meter
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Inch
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Foot
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Yard
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Mile

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum LengthUnit {
Millimeter,
Centimeter,
Meter,
Kilometer,
Inch,
Foot,
Yard,
Mile,
}

fn unit_to_meter_multiplier(from: LengthUnit) -> f64 {
match from {
LengthUnit::Millimeter => 0.001,
LengthUnit::Centimeter => 0.01,
LengthUnit::Meter => 1.0,
LengthUnit::Kilometer => 1000.0,
LengthUnit::Inch => 0.0254,
LengthUnit::Foot => 0.3048,
LengthUnit::Yard => 0.9144,
LengthUnit::Mile => 1609.34,
}
}

fn unit_to_meter(input: f64, from: LengthUnit) -> f64 {
input * unit_to_meter_multiplier(from)
}

fn meter_to_unit(input: f64, to: LengthUnit) -> f64 {
input / unit_to_meter_multiplier(to)
}

/// This function will convert a value in unit of [from] to value in unit of [to]
/// by first converting it to meter and than convert it to destination unit
pub fn length_conversion(input: f64, from: LengthUnit, to: LengthUnit) -> f64 {
meter_to_unit(unit_to_meter(input, from), to)
}

#[cfg(test)]
mod length_conversion_tests {
use std::collections::HashMap;

use super::LengthUnit::*;
use super::*;

#[test]
fn zero_to_zero() {
let units = vec![
Millimeter, Centimeter, Meter, Kilometer, Inch, Foot, Yard, Mile,
];

for u1 in units.clone() {
for u2 in units.clone() {
assert_eq!(length_conversion(0f64, u1, u2), 0f64);
}
}
}

#[test]
fn length_of_one_meter() {
let meter_in_different_units = HashMap::from([
(Millimeter, 1000f64),
(Centimeter, 100f64),
(Kilometer, 0.001f64),
(Inch, 39.37007874015748f64),
(Foot, 3.280839895013123f64),
(Yard, 1.0936132983377078f64),
(Mile, 0.0006213727366498068f64),
]);
for (input_unit, input_value) in &meter_in_different_units {
for (target_unit, target_value) in &meter_in_different_units {
assert!(
num_traits::abs(
length_conversion(*input_value, *input_unit, *target_unit) - *target_value
) < 0.0000001
);
}
}
}
}
2 changes: 2 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod decimal_to_binary;
mod decimal_to_hexadecimal;
mod hexadecimal_to_binary;
mod hexadecimal_to_decimal;
mod length_conversion;
mod octal_to_binary;
mod octal_to_decimal;
pub use self::binary_to_decimal::binary_to_decimal;
Expand All @@ -12,5 +13,6 @@ pub use self::decimal_to_binary::decimal_to_binary;
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
pub use self::length_conversion::length_conversion;
pub use self::octal_to_binary::octal_to_binary;
pub use self::octal_to_decimal::octal_to_decimal;

0 comments on commit 1b07a48

Please sign in to comment.