Skip to content
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

create financial/price_plus_tax function #829

Merged
merged 11 commits into from
Nov 4, 2024
5 changes: 5 additions & 0 deletions src/financial/README.md
vil02 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Financial Algorithms
### [Present-Value Algorithm](./present_value.rs)
From [Wikipedia][present-value-wiki]: 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. The present value is usually less than the future value because money has interest-earning potential, a characteristic referred to as the time value of money, except during times of negative interest rates, when the present value will be equal or more than the future value

[present-value-wiki]: https://en.wikipedia.org/wiki/Present_value
4 changes: 4 additions & 0 deletions src/financial/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod present_value;
mod price_plus_tax;
pub use present_value::present_value;
pub use price_plus_tax::price_plus_tax;
69 changes: 69 additions & 0 deletions src/financial/present_value.rs
vil02 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#[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((present_value * 100.0).round() / 100.0)
vil02 marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_present_value() {
assert_eq!(
4.69,
present_value(0.13, vec![10.0, 20.70, -293.0, 297.0]).unwrap()
);

assert_eq!(
-42739.63,
present_value(0.07, vec![-109129.39, 30923.23, 15098.93, 29734.0, 39.0]).unwrap()
);

assert_eq!(
175519.15,
present_value(0.07, vec![109129.39, 30923.23, 15098.93, 29734.0, 39.0]).unwrap()
);
}

#[test]
fn test_present_value_negative_discount_rate() {
assert_eq!(
PresentValueError::NegetiveDiscount,
present_value(-1.0, vec![10.0, 20.70, -293.0, 297.0]).unwrap_err()
);
}

#[test]
fn test_present_value_empty_cash_flow() {
assert_eq!(
PresentValueError::EmptyCashFlow,
present_value(1.0, vec![]).unwrap_err()
);
}

#[test]
fn test_present_value_zero_discount_rate() {
assert_eq!(
184924.55,
present_value(0.0, vec![109129.39, 30923.23, 15098.93, 29734.0, 39.0]).unwrap()
);
}
vil02 marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 14 additions & 0 deletions src/financial/price_plus_tax.rs
vil02 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn price_plus_tax(price: f64, tax_rate: f64) -> f64 {
price * (1_f64 + tax_rate)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_price_plus_tax() {
assert_eq!(131.775, price_plus_tax(125.50, 0.05));
assert_eq!(125.0, price_plus_tax(100.0, 0.25));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod compression;
pub mod conversions;
pub mod data_structures;
pub mod dynamic_programming;
pub mod financial;
pub mod general;
pub mod geometry;
pub mod graph;
Expand Down