cosmicabyss.com.lib

## Class Finance

• java.lang.Object
• cosmicabyss.com.lib.Finance

• ```public class Finance
extends java.lang.Object```
This class provides static financial methods.

These methods are based on the compound interest formula:

```         fv = pv * ((1 + i) ^ n)

where
i = r / m
and
pv   Present Value (principle)
fv   Future Value at end of n periods
n    Number of compounding periods
m    number of compounding periods per year
i    Interest rate per compounding period
r    annual interest Rate (nominal rate)

also indirectly derivable from this formula are the following:

er   effective annual interest Rate (effective rate)
When interest is compounded at a frequency of any other than
1 compounding period per year, the stated annual rate is
called a "nominal rate". The simple interest rate that would
produce the same interest using a compounding period of 1
time per year is called the "effective rate". The effective
rate is used to compare various types of investments.
```

Example usage:
```                  =========================================================================================
What is the annual interest rate (r) on an investment of \$120.00 (pv)
that is worth \$131.68 (fv) after 60 compounding periods (n) where
there are 12 compounding periods per year (m) -- that is, 5 years
compounded monthly.
-----------------------------------------------------------------------------------------
double r = r(120, 131.68, 60, 12);
System.out.println("r   =   " + UMath.toPct(r) + "%");

=========================================================================================
whats the er
-----------------------------------------------------------------------------------------
double er = er(120, 131.68, 60, 12);
System.out.println("er  =   " + UMath.toPct(er) + "%");

=========================================================================================
Check the above computation by finding what an investment of \$120.00
(pv) is worth (fv) after 60 compounding periods (n) where there are
12 compounding periods per year (m) -- that is, 5 years compounded
monthly -- and the annual interest rate is the rate computed above
(r). The computation of r above is verified if the fv calculated
here is \$131.68.
-----------------------------------------------------------------------------------------
System.out.println("fv  =  \$" + fv(120, 60, 12, r));

=========================================================================================
Check using the er
-----------------------------------------------------------------------------------------
System.out.println("fv  =  \$" + fv(120, 5, 1, er));
```
View Source:
Finance.java
Author:
Allen Baker
• ### Constructor Summary

Constructors
Constructor and Description
`Finance()`
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static double` ```er(double pv, double fv, double n, double m)```
This method computes er from pv, fv, n, and m.
`static double` ```fv(double pv, double n, double m, double r)```
This method computes fv from pv, n, m, and r.
`static double` ```i(double m, double r)```
This method computes I from m and r.
`static double` ```i(double pv, double fv, double n)```
This method computes I from pv, fv, and n.
`static void` `main(java.lang.String[] pArgs)`
This method allows the Finance class file to be unit tested as a standalone application.
`static double` ```n(double pv, double fv, double m, double r)```
This method computes n from pv, fv, m, and r.
`static double` ```pv(double fv, double n, double m, double r)```
This method computes pv from fv, n, m, and r.
`static double` ```r(double pv, double fv, double n, double m)```
This method computes r from pv, fv, n, and m.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### Finance

`public Finance()`
• ### Method Detail

• #### fv

```public static double fv(double pv,
double n,
double m,
double r)```
This method computes fv from pv, n, m, and r.

This method is based on solving the compound interest formula for fv:

```            fv = pv * ((1 + i) ^ n)
fv = pv * ((1 + (r/m)) ^ n)
```

Implementation: View source

Parameters:
`Pv` - Present Value (principle)
`N` - Number of copounding periods
`M` - number of compounding periods per year
`R` - annual interest Rate
Returns:
Fv - Future Value at end of n periods
• #### pv

```public static double pv(double fv,
double n,
double m,
double r)```
This method computes pv from fv, n, m, and r.

This method is based on solving the compound interest formula for pv:

```            fv                      =  pv * ((1 + i) ^ n)
fv / ((1 + i) ^ n)      =  pv
fv / ((1 + (r/m)) ^ n)  =  pv
```

Implementation: View source

Parameters:
`Fv` - Future Value at end of n periods
`N` - Number of copounding periods
`M` - number of compounding periods per year
`R` - annual interest Rate
Returns:
Pv - Present Value (principle)
• #### i

```public static double i(double pv,
double fv,
double n)```
This method computes I from pv, fv, and n.

This method is based on solving the compound interest formula for i:

```                          fv  =  pv * ((1 + i) ^ n)
(fv / pv)  =  ((1 + i) ^ n)
(fv / pv) ^ (1 / n)  =  (1 + i)
((fv / pv) ^ (1 / n)) - 1  =  i
```

Implementation: View source

Parameters:
`Pv` - Present Value (principle)
`Fv` - Future Value at end of n periods
`N` - Number of copounding periods
Returns:
I - Interest rate per compounding period
• #### i

```public static double i(double m,
double r)```
This method computes I from m and r.
```            i = (r / m)
```

Implementation: View source

Parameters:
`M` - number of compounding periods per year
`R` - annual interest Rate
Returns:
I - Interest rate per compounding period
• #### r

```public static double r(double pv,
double fv,
double n,
double m)```
This method computes r from pv, fv, n, and m.

This method is based on solving the compound interest formula for r:

```                                fv  =  pv * ((1 + i) ^ n)
(fv / pv)  =  ((1 + i) ^ n)
(fv / pv) ^ (1 / n)  =  (1 + i)
((fv / pv) ^ (1 / n)) - 1  =  i
((fv / pv) ^ (1 / n)) - 1  =  (r / m)
(((fv / pv) ^ (1 / n)) - 1) * m  =  r
```

Implementation: View source

Parameters:
`Pv` - Present Value (principle)
`Fv` - Future Value at end of n periods
`N` - Number of copounding periods
`M` - number of compounding periods per year
Returns:
R - annual interest Rate
• #### er

```public static double er(double pv,
double fv,
double n,
double m)```
This method computes er from pv, fv, n, and m.

Implementation: View source

Parameters:
`Pv` - Present Value (principle)
`Fv` - Future Value at end of n periods
`N` - Number of copounding periods
`M` - number of compounding periods per year
Returns:
Er - effective annual interest Rate (effective rate)
• #### n

```public static double n(double pv,
double fv,
double m,
double r)```
This method computes n from pv, fv, m, and r.

This method is based on solving the compound interest formula for n:

```            using these facts about logarithms:

x = log[b](y) <--> y = b^x            (see: http://www.math.utah.edu/~alfeld/math/log.html)

log[a](b) = log(b) / log(a)   (see: http://en.wikipedia.org/wiki/Logarithm#Change_of_base)

Solve for n:

fv  =  pv * ((1 + i) ^ n)
(fv / pv)  =  ((1 + i) ^ n)
n  =  log[1 + i](fv / pv)
n  =  log(fv / pv) / log(1 + i)
n  =  log(fv / pv) / log(1 + (r / m))
```

Implementation: View source

Parameters:
`Pv` - Present Value (principle)
`Fv` - Future Value at end of n periods
`M` - number of compounding periods per year
`R` - annual interest Rate
Returns:
N - Number of copounding periods
• #### main

```public static void main(java.lang.String[] pArgs)
throws java.lang.Exception```
This method allows the Finance class file to be unit tested as a standalone application. It's the method that's called when the class is invoked from the command line by using the java application launcher - "java". Main() is not a required method, but the practice of putting one in each class and wrapping class test code within it allows easy unit testing of the class; and main does not need to be removed when testing is complete.

Command line usage:
Java cosmicabyss.com.lib.Finance

Implementation: View source

Parameters:
`pArgs` - contains the command line arguments with which the class was invoked as an application.
Throws:
`java.lang.Exception`
```
var preElements = document.getElementsByTagName('pre');

for (var i = 0; i < preElements.length; i++)
{
if (preElements[i].id == 'unindent')
{
var content = preElements[i].innerHTML;

var spacesToRemove = '';
while (content.indexOf(' ') == '0')
{
spacesToRemove += ' ';
content = content.substring(1);
}

var re = new RegExp('\n' + spacesToRemove, 'g');
content = content.replace(re, '\n');
preElements[i].outerHTML = '<pre>' + content + '</pre>';
}
}

```