[−][src]Function finance_solution::periods
pub fn periods<P, F>(
rate: f64,
present_value: P,
future_value: F,
continuous_compounding: bool
) -> f64 where
P: Into<f64> + Copy,
F: Into<f64> + Copy,
Returns the number of periods given a periodic rate along with the present and future values, using simple compounding.
Note that the returned number of periods will be a floating point number representing fractional periods.
See the periods module page for the formulas.
Related functions:
- To calculate the periods using simple compounding and return a struct that shows the formula and can be used to produce the the period-by-period values use periods_solution.
- To calculate the periods using continuous compounding use periods_continuous or periods_continuous_solution.
Arguments
rate
- The rate at which the investment grows or shrinks per period, expressed as a floating point number. For instance 0.05 would mean 5% growth. Often appears asr
ori
in formulas.present_value
- The starting value of the investment. May appear aspv
in formulas, orC
for cash flow orP
for principal.future_value
- The final value of the investment.continuous_compounding
- True for continuous compounding, false for simple compounding.
Panics
The call will fail if the rate, the present value, or the future value is infinite or not a number (NaN).
The call will also fail in any of the follwing cases because there is no number of periods that would make the calculation work:
- The periodic rate is less than -1.0.
- The present value is zero and the future value is nonzero.
- The present value is nonzero and the future value is zero, unless the rate is exactly -1.0%.
- The present value is negative and the future value is positive or vice versa.
- The present value and future value are both negative, the future value is less than the present value, and the periodic rate is zero or negative.
- The present value and future value are both negative, the future value is greater than the present value, and the periodic rate is zero or positive.
- The present value and future value are both positive, the future value is greater than the present value, and the periodic rate is zero or negative.
- The present value and future value are both positive, the future value is less than the present value, and the periodic rate is zero or positive.
Examples
use finance_solution::*; // The interest rate is 8% per year. let rate = 0.08; // The starting value is $5,000.00. let present_value = -5_000.00; // The ending value is $7,000.00. let future_value = 7_000.00; let continuous_compounding = false; // Calculate the number of years required. let fractional_periods = periods(rate, present_value, future_value, false); dbg!(&fractional_periods); assert_rounded_2(4.37, fractional_periods); // Round up to get a whole number of years. let periods = fractional_periods.ceil() as u32; dbg!(&periods); assert_eq!(5, periods);