-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Length conversion #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Length conversion #830
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9bbee41
feat: length_conversion implemented
ali77gh da6b3e3
link: added to DIRECTORY.md
ali77gh ef7fe07
test: zero_to_zero test added
ali77gh b523197
test: length_of_one_meter added
ali77gh ea60fed
fix: apply @vil02 suggestions
ali77gh e8c5821
docs: remove redundant comments
vil02 967f112
Merge branch 'master' into length_conversion
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /// 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 | ||
|
|
||
| /// Universal Units on Length | ||
| #[derive(Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub enum LengthUnit { | ||
| Millimeter, | ||
| Centimeter, | ||
| Meter, | ||
| Kilometer, | ||
| Inch, | ||
| Foot, | ||
| Yard, | ||
| Mile, | ||
| } | ||
|
|
||
| /// Private methods | ||
| /// This is a n*n problem | ||
| /// It's gonna be 56 functions or 56 cases in match statement | ||
| /// It's hard to write code for every unit to unit so I solve this problem | ||
| /// by converting input to meter and than convert it to output | ||
| /// This function give you a number (let's call it n) | ||
| /// So if you multiple a value in this unit to n you will get meters | ||
| /// | ||
| /// m * in-unit = meter | ||
vil02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[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 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.