-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
create financial/price_plus_tax function #829
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
910ecce
create financial/price_plus_tax function
alirezasariri78 3901d8b
run cargo fmt
alirezasariri78 603db05
add readme.md in financial tax
alirezasariri78 8e9dbdb
create present value module
alirezasariri78 e639253
run fmt command
alirezasariri78 14491c7
seperate round function in present_value module
alirezasariri78 b9919e5
remove price_plus_tx
alirezasariri78 b38be35
use fmt and clippy
alirezasariri78 90a244f
Merge branch 'TheAlgorithms:master' into master
alirezasariri78 7f5b659
add document to present_value
alirezasariri78 fb7ae17
Merge branch 'master' into master
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod present_value; | ||
pub use present_value::present_value; |
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,91 @@ | ||
/// In economics and finance, present value (PV), also known as present discounted value, | ||
/// is the value of an expected income stream determined as of the date of valuation. | ||
/// | ||
/// -> Uncyclopedia reference: https://en.wikipedia.org/wiki/Present_value | ||
|
||
#[derive(PartialEq, Eq, Debug)] | ||
pub enum PresentValueError { | ||
NegetiveDiscount, | ||
EmptyCashFlow, | ||
} | ||
|
||
pub fn present_value(discount_rate: f64, cash_flows: Vec<f64>) -> Result<f64, PresentValueError> { | ||
if discount_rate < 0.0 { | ||
return Err(PresentValueError::NegetiveDiscount); | ||
} | ||
if cash_flows.is_empty() { | ||
return Err(PresentValueError::EmptyCashFlow); | ||
} | ||
|
||
let present_value = cash_flows | ||
.iter() | ||
.enumerate() | ||
.map(|(i, &cash_flow)| cash_flow / (1.0 + discount_rate).powi(i as i32)) | ||
.sum::<f64>(); | ||
|
||
Ok(round(present_value)) | ||
} | ||
|
||
fn round(value: f64) -> f64 { | ||
(value * 100.0).round() / 100.0 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
macro_rules! test_present_value { | ||
($($name:ident: $inputs:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $name() { | ||
let ((discount_rate,cash_flows), expected) = $inputs; | ||
assert_eq!(present_value(discount_rate,cash_flows).unwrap(), expected); | ||
} | ||
)* | ||
} | ||
} | ||
|
||
macro_rules! test_present_value_Err { | ||
($($name:ident: $inputs:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $name() { | ||
let ((discount_rate,cash_flows), expected) = $inputs; | ||
assert_eq!(present_value(discount_rate,cash_flows).unwrap_err(), expected); | ||
} | ||
)* | ||
} | ||
} | ||
|
||
macro_rules! test_round { | ||
($($name:ident: $inputs:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $name() { | ||
let (input, expected) = $inputs; | ||
assert_eq!(round(input), expected); | ||
} | ||
)* | ||
} | ||
} | ||
|
||
test_present_value! { | ||
general_inputs1:((0.13, vec![10.0, 20.70, -293.0, 297.0]),4.69), | ||
general_inputs2:((0.07, vec![-109129.39, 30923.23, 15098.93, 29734.0, 39.0]),-42739.63), | ||
general_inputs3:((0.07, vec![109129.39, 30923.23, 15098.93, 29734.0, 39.0]), 175519.15), | ||
zero_input:((0.0, vec![109129.39, 30923.23, 15098.93, 29734.0, 39.0]), 184924.55), | ||
|
||
} | ||
|
||
test_present_value_Err! { | ||
negative_discount_rate:((-1.0, vec![10.0, 20.70, -293.0, 297.0]), PresentValueError::NegetiveDiscount), | ||
empty_cash_flow:((1.0, vec![]), PresentValueError::EmptyCashFlow), | ||
|
||
} | ||
test_round! { | ||
test1:(0.55434, 0.55), | ||
test2:(10.453, 10.45), | ||
test3:(1111_f64, 1111_f64), | ||
} | ||
} |
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.