Here's a good example, from this page. It computes a 25 year investment based on a particular interest rate which is given as the yearly interest rate but actually computed and added to the balance each day.
IN COBOL:
program-id. Compare.
working-storage section.
01 yield-exact pic 9(9)v99 comp.
01 yield-float pic 9(9)v99 comp.
01 money pic 9(9).99.
01 yield-info.
03 start-balance pic 9(9)v9(2) comp.
03 yield pic 9(9)v9(2) comp.
03 interest pic v9(7) comp.
03 days pic 9(9) comp.
03 years pic 9(2) comp.
procedure division.
move 0.0625 to interest
move 9132 to days
move 25 to years
perform varying start-balance from 1000000 by 0.01
until start-balance = 1000100
call "get-yield-exact" using yield-info
move yield to yield-exact
call "get-yield-float" using yield-info
move yield to yield-float
if yield-float not = yield-exact
move start-balance to money
display "For start balance: " money
move yield-exact to money
display " - At 25 years exact balance is " money
move yield-float to money
display " - But floating point is " money
display " "
end-if
end-perform
display "All Done"
goback.
end program Compare.
* Calculate the yield using exact mathematics
program-id. get-yield-exact.
working-storage section.
01 current-balance pic 9(9)v9(18) comp.
01 interest-c pic 9(9)v9(18) comp.
01 intermediate-7 pic 9(9)v9(7) comp.
01 calc-day pic 9(9) comp.
linkage section.
01 yield-info.
03 start-balance pic 9(9)v9(2) comp.
03 yield pic 9(9)v9(2) comp.
03 interest pic v9(7) comp.
03 days pic 9(9) comp.
03 years pic 9(2) comp.
procedure division using yield-info.
move start-balance to current-balance
move interest to interest-c
compute interest-c rounded = (interest-c * years) / days
compute intermediate-7 rounded = interest-c
move intermediate-7 to interest-c
move days to calc-day
perform varying calc-day from 1 by 1 until calc-day greater days
compute intermediate-7 rounded
= current-balance + (current-balance * interest-c)
move intermediate-7 to current-balance
end-perform
move current-balance to yield
end program get-yield-exact.
* Calculate the yield using floating point mathematics
program-id. get-yield-float.
working-storage section.
01 current-balance float-long.
01 interest-c float-long.
01 intermediate-7 pic 9(9)v9(7) comp.
01 calc-day pic 9(9) comp.
linkage section.
01 yield-info.
03 start-balance pic 9(9)v9(2) comp.
03 yield pic 9(9)v9(2) comp.
03 interest pic v9(7) comp.
03 days pic 9(9) comp.
03 years pic 9(2) comp.
procedure division using yield-info.
move start-balance to current-balance
move interest to interest-c
compute interest-c rounded = (interest-c * years) / days
compute intermediate-7 rounded = interest-c
move intermediate-7 to interest-c
move days to calc-day
perform varying calc-day from 1 by 1 until calc-day greater days
compute intermediate-7 rounded
= current-balance + (current-balance * interest-c)
move intermediate-7 to current-balance
end-perform
move current-balance to yield
end program get-yield-float.
In Java:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class GetYield
{
public static BigDecimal computeYield
(
BigDecimal startBalance,
BigDecimal interest,
int days,
int years
)
{
int large=18;
int small=7;
RoundingMode rm=RoundingMode.HALF_UP;
BigDecimal currentBalance=startBalance.setScale(large,rm);
BigDecimal interestC=interest.setScale(large,rm);
interestC=
(interestC.multiply(BigDecimal.valueOf(years,large)))
.divide(BigDecimal.valueOf(days,large),rm)
.setScale(small, rm)
.setScale(large, rm);
System.out.println(interestC);
for(int calcDay=1;calcDay<=days;++calcDay)
{
currentBalance=
(currentBalance.add(currentBalance.multiply(interestC).setScale(large, rm))).
setScale(small, rm).
setScale(large, rm);
}
return currentBalance.setScale(2, rm);
}
public static void main(String[] args)
{
BigDecimal yield=computeYield(BigDecimal.valueOf(1000095.20),BigDecimal.valueOf(0.0625),9132,25);
System.out.println(yield);
}
}