### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: CAGR Formula  (Read 3281 times)

#### tMinuszero

• New member
• Posts: 5
##### CAGR Formula
« 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

#### Bart

• Hero Member
• Posts: 3940
##### Re: CAGR Formula
« Reply #1 on: September 16, 2016, 10:27:23 pm »
Take a look at the Power() function (unit math).

Bart

#### tMinuszero

• New member
• Posts: 5
##### Re: CAGR Formula
« Reply #2 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.

#### lainz

• Hero Member
• Posts: 3763
• Leandro Diaz
##### Re: CAGR Formula
« Reply #3 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);
11.   readln();
12. end.
13.

#### JD

• Hero Member
• Posts: 1796
##### Re: CAGR Formula
« Reply #4 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
« Last Edit: September 16, 2016, 11:20:15 pm by JD »
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

#### tMinuszero

• New member
• Posts: 5
##### Re: CAGR Formula
« Reply #5 on: September 16, 2016, 11:36:48 pm »
Thanks a lot guys................

Appreciated.