# Lazarus

## Free Pascal => General => Topic started by: tMinuszero on September 16, 2016, 10:24:59 pm

Title: CAGR Formula
Post by: tMinuszero on September 16, 2016, 10:24:59 pm
Hi,

Could somebody explain how to implement the following formula in object pascal code please? Sticky point is implementing (^) power bit.

y = a(1 + r)^x

where:
y = future compounded value
a = initial value
r = growth rate
x = number of periods
Title: Re: CAGR Formula
Post by: Bart on September 16, 2016, 10:27:23 pm
Take a look at the Power() function (unit math).

Bart
Title: Re: CAGR Formula
Post by: tMinuszero on September 16, 2016, 11:03:45 pm
Thanks Bart..

I don't know enough coding to use IDE yet.

Just wanted to know sample code implementing the above formula.
Title: Re: CAGR Formula
Post by: lainz on September 16, 2016, 11:12:43 pm
Go to Lazarus and do Project > New Project > select Simple Program and copy this (hope the formula is OK typed, else fix it yourself):

program Project1;

Code: Pascal  [Select][+][-]
1. uses
2.   Math;
3.
4. function CAGR(initial_value, grouth_rate, number_of_periods: double): double;
5. begin
6.   result := initial_value * Power(( 1 + grouth_rate), number_of_periods);
7. end;
8.
9. begin
10.   writeln(CAGR(5.1, 10.4, 2.2):0:2);
12. end.
13.
Title: Re: CAGR Formula
Post by: JD on September 16, 2016, 11:15:02 pm
@tMinusZero

Look in the Math unit. It has some basic financial functions that can help you with capital budgeting calculations. You can use them to put together more complicated equations.

Code: Pascal  [Select][+][-]
1. { Financial functions }
2.
3. function FutureValue(ARate: Float; NPeriods: Integer;
4.   APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
5.
6. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
7.   APaymentTime: TPaymentTime): Float;
8.
9. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
10.   APaymentTime: TPaymentTime): Float;
11.
12. function Payment(ARate: Float; NPeriods: Integer;
13.   APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
14.
15. function PresentValue(ARate: Float; NPeriods: Integer;
16.   APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
17.

You are looking for the future value of y so use the FutureValue function OR the solution lainz proposed above.

JD
Title: Re: CAGR Formula
Post by: tMinuszero on September 16, 2016, 11:36:48 pm
Thanks a lot guys................

Appreciated.