Skip to main content

ipmt

Function ipmt 

Source
pub fn ipmt<P, F, T>(
    rate: f64,
    period: u32,
    periods: u32,
    present_value: P,
    future_value: F,
    timing: T,
) -> FinanceResult<f64>
where P: Into<f64> + Copy, F: Into<f64> + Copy, T: Into<PaymentTiming>,
Expand description

Interest portion of the payment for a single period (Excel IPMT).

§Arguments

  • rate – periodic rate
  • period – 1-based period index (1..=periods)
  • periods – total number of periods
  • present_value – principal / PV
  • future_value – residual value at end (often 0)
  • timing – [PaymentTiming] or bool (false = end of period / Excel type=0)

§Errors

Propagates construction errors from amortization_solution and FinanceError::InvalidPeriod when period is out of range.

§Examples

use finance_solution::*;
let interest = ipmt(0.01, 1, 12, 10_000.0, 0.0, false).unwrap();
assert!(interest < 0.0); // opposite sign of positive principal
assert_rounded_2!(interest, -100.0);

For schedules and tables, prefer amortization_solution:

use finance_solution::*;
let s = amortization_solution(0.01, 12, 10_000.0, 0.0, false).unwrap();
assert_approx_equal!(
    s.ipmt(1).unwrap(),
    ipmt(0.01, 1, 12, 10_000.0, 0.0, false).unwrap()
);

Out-of-range period:

use finance_solution::{ipmt, FinanceError};

match ipmt(0.01, 1, 12, 10_000.0, 0.0, false) {
    Ok(interest) => assert!(interest < 0.0),
    Err(FinanceError::InvalidPeriod { period, periods, .. }) => {
        panic!("period {period} not in 1..={periods}");
    }
    Err(e) => panic!("{e}"),
}

assert!(matches!(
    ipmt(0.01, 0, 12, 10_000.0, 0.0, false),
    Err(FinanceError::InvalidPeriod { .. })
));