pub fn rate<P, F, C>(
periods: u32,
present_value: P,
future_value: F,
compounding: C,
) -> FinanceResult<f64>Expand description
Returns the periodic rate of an investment given the number of periods along with the present and future values.
See the rate module page for the formulas.
Related functions:
- To calculate a periodic rate and return a struct that shows the formula and optionally
produces the the period-by-period values use
rate_solution.
§Arguments
periods- The number of periods such as quarters or periods. Often appears asnort.present_value- The starting value of the investment. May appear aspvin formulas, orCfor cash flow orPfor principal.future_value- The final value of the investment.continuous_compounding- True for continuous compounding, false for simple compounding.
If present_value and future_value are both zero then any rate will work so the function returns zero.
§Errors
The call returns FinanceError if the present value is zero and the future value is nonzero or vice versa.
It also returns an error if the number of periods is zero and the present value is not equal to the
future value. In both cases this is because there’s no periodic rate that could make that work.
§Examples
use finance_solution::*;
// The interest will compound for 365 days.
let periods = 365;
// The starting value is $10,000.
let present_value = -10_000.00;
// The ending value is $11,000.
let future_value = 11_000.00;
let continuous_compounding = false;
// Calculate the periodic rate needed.
let rate = rate(periods, present_value, future_value, continuous_compounding).unwrap();
dbg!(&rate);
// The rate is 0.0261% per day.
assert_rounded_6(0.000261, rate);§Errors
Returns FinanceError::SameSignValues, FinanceError::Unsolvable, or
FinanceError::NonFinite when inputs cannot determine a rate.
§Examples
use finance_solution::{rate, FinanceError};
let r = rate(365, -10_000.0, 11_000.0, false).unwrap();
assert!(r > 0.0);
match rate(12, 1_000.0, 1_100.0, false) {
Err(FinanceError::SameSignValues { .. }) => {}
other => panic!("expected SameSignValues, got {other:?}"),
}