Skip to main content

present_value

Function present_value 

Source
pub fn present_value<T, C>(
    rate: f64,
    periods: u32,
    future_value: T,
    compounding: C,
) -> FinanceResult<f64>
where T: Into<f64> + Copy, C: Into<Compounding>,
Expand description

Returns the current value of a future amount using a fixed rate.

Related functions:

See the present_value module page for the formulas.

§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 as r or i in formulas.
  • periods - The number of periods such as quarters or years. Often appears as n or t.
  • future_value - The final value of the investment.
  • continuous_compounding - True for continuous compounding, false for simple compounding.

§Errors

The call returns FinanceError if rate is less than -1.0 as this would mean the investment is losing more than its full value every period. It returns an error also if the future value is zero as in this case there’s no way to determine the present value.

§Examples

Investment that grows month by month.

use finance_solution::*;

// The investment will grow by 1.1% per month.
let rate = 0.011;

// The investment will grow for 12 months.
let periods = 12;

// The final value will be $50,000.
let future_value = 50_000;

let continuous_compounding = false;

// Find the current value.
let present_value = present_value(rate, periods, future_value as f64, continuous_compounding).unwrap();
dbg!(&present_value);

// Confirm that the present value is correct to four decimal places (one hundredth of a cent).
assert_rounded_4(-43_848.6409, present_value);

Error case: rate less than −100% per period is outside the domain.

let rate = -1.05;
let periods = 6;
let future_value = -10_000.75;
let err = present_value(rate, periods, future_value, false).unwrap_err();
assert!(matches!(err, FinanceError::InvalidRate { .. }));

§Errors

Returns FinanceError::InvalidRate if rate < -1.0, FinanceError::ZeroValue if future_value is zero/subnormal, or FinanceError::NonFinite for non-finite values.

§Examples

use finance_solution::{present_value, FinanceError, FinanceResult};

assert!(present_value(0.05, 10, 1000.0, false).is_ok());

match present_value(-1.5, 10, 1000.0, false) {
    Err(FinanceError::InvalidRate { rate }) => assert_eq!(rate, -1.5),
    other => panic!("expected InvalidRate, got {other:?}"),
}

match present_value(0.05, 10, 0.0, false) {
    Err(FinanceError::ZeroValue { field }) => assert_eq!(field, "future_value"),
    other => panic!("expected ZeroValue, got {other:?}"),
}

fn discount(fv: f64) -> FinanceResult<f64> {
    present_value(0.06, 8, fv, false)
}
match discount(50_000.0) {
    Ok(pv) => assert!(pv < 0.0),
    Err(e) => panic!("{e}"),
}