I'm trying to make the Excel formula VF (future value) in PHP, however the values are not matched, ie in Excel is a value for the column and in the PHP calculation others leave values but almost identical values, from R $ 1.50 to R $ 3.00.
index.php
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-sac">
<style type="text/css">
.table-sac thead tr th,
tbody tr td {
text-align: center;
}
</style>
<thead>
<tr>
<th>Mes</th>
<th>Se for investir</th>
<th>Juros no finan.</th>
<th>Amortização</th>
<th>VT</th>
<th>Valor futuro</th>
</tr>
</thead>
<tbody>
<?php require_once('calcs.php'); ?>
<?php for($i = 0; $i <= $mes; $i++): ?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo number_format($sac['G'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['H'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['I'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['J'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo $sac['K']['vf'][$i]; ?>
</td>
</tr>
<?php endfor; ?>
</tbody>
</table>
</div>
</div>
<!-- row -->
</div>
<!-- container -->
calcs.php
<?php
// F = P. (1+i)° + M.[(1+i)° - 1]/i
function vf($taxa,$n,$pgto,$vlr_presente=0, $iterador)
{
if($iterador > 0):
// $first = bcmul($GLOBALS['ul_vf'], $taxa);
// $second = bcadd($GLOBALS['ul_pgto'], $first);
// $vf = bcadd($GLOBALS['ul_vf'], $second);
// $vf = ($GLOBALS['ul_vf']) + ($GLOBALS['ul_vf'] * $taxa + $GLOBALS['ul_pgto']);
$first = bcadd(1, $taxa);
$second = bcpow($first, $n);
$third = bcmul($GLOBALS['ul_vf'], $second);
$fourth = bcsub($second, 1);
$fifth = bcdiv($fourth, $taxa);
$sixth = bcmul($fifth, $GLOBALS['ul_pgto']);
$seventh = bcadd($third, $sixth);
$vf = $seventh;
$GLOBALS['ul_vf'] = $vf;
$GLOBALS['ul_pgto'] = $pgto;
else:
$vf = $vlr_presente;
$GLOBALS['ul_vf'] = $vf;
$GLOBALS['ul_pgto'] = $pgto;
endif;
return $vf;
}
$vlr_imovel = 144000; //var_dump($vlr_imovel);
$vlr_aluguel = 400.00; //var_dump($vlr_imovel);
$vlr_entrada = 43200.00; //var_dump($vlr_imovel);
$vlr_encargos = 500.00; //var_dump($vlr_imovel);
$mes = 360;
$taxa = (0.54/100);
$se_for_investir = $vlr_encargos + $vlr_entrada;
for($i = 0; $i <= $mes; $i++):
$juros_no_finan = (($vlr_imovel - $vlr_entrada) - ((($vlr_imovel - $vlr_entrada)/$mes)*$i))*0.007;
$amortizacao = ($vlr_imovel - $vlr_entrada)/$mes;
$vlr_total = $amortizacao + $juros_no_finan;
//VALOR FUTURO -------------------------
// Como: FV = PV x ( 1 + i ) ^ n
$n = 1;
$pgto = $vlr_total - $vlr_aluguel;
$vlr_presente = $vlr_entrada + $vlr_encargos;
$tipo = 0;
$one = 0.96;
$two = 0.42;
$taxa2 = 0.54/100;
$vf = vf($taxa2, $n, $pgto, $vlr_presente, $i);
//VALOR FUTURO -------------------------
$sac['G'][] = $se_for_investir;
$sac['H'][] = $juros_no_finan;
$sac['I'][] = $amortizacao;
$sac['J'][] = $vlr_total;
$sac['K']['pgto'][] = $pgto;
$sac['K']['vf'][] = $vf;
endfor;
Explanation of the code.
1 - I loop in months, 360 months altogether.
2 - In the first loop (0) the function FV
is called, FV
receives the parameters:
($taxa, $parcelas, $pagamento, $vlr_presente, $iterador_atual)
These parameters
are of the current loop.
3 - In FV (function) we have conditionals that check that the $iterador
(which was passed by the parameter) is > 1, example:
if(iterador > 1):
//aqui eu faço a formula com as variaveis globais no else abaixo
//e insiro variaveis globais para ser aproveitadas no proximo loop
else:
//aqui é o primeiro loop, ou seja a primeira linha do excel(o mês 0)
//variavel global desse valor passado(será aproveitada no proximo loop)
//variavel global do pagamento passado(será aproveitada no proximo loop)
endif;
return $valorFuturo;
4 - Now in Calcs.php we have the future value that will be passed to the view;
The error here is that the values are not identical, almost, but not identical. I thought it might be the floats that were messing up but it is not.
I've placed the excel file on google drive.