Last active
September 10, 2021 01:48
-
-
Save edermanoel94/8cf609b86fd5c85ee4a7e567d2ff0e4e to your computer and use it in GitHub Desktop.
Algoritmo para criar tabela PRICE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
"strconv" | |
) | |
type Loan struct { | |
installments float64 | |
payment float64 | |
amortization float64 | |
interest float64 | |
balanceDue float64 | |
} | |
func main() { | |
} | |
func must(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func calculateInterest(presentValue, interest float64) float64 { | |
return presentValue * interest | |
} | |
func toFixed(value float64) float64 { | |
newValue, err := strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64) | |
must(err) | |
return newValue | |
} | |
func NewLoan(installments, payment, am, interest, balanceDue float64) Loan { | |
return Loan{ | |
installments: toFixed(installments), | |
payment: toFixed(payment), | |
amortization: toFixed(am), | |
interest: toFixed(interest), | |
balanceDue: toFixed(balanceDue), | |
} | |
} | |
func price(presentValue, installments, interest float64) float64 { | |
a := math.Pow(1+interest, installments) | |
return presentValue / ((a - 1) / (a * interest)) | |
} | |
func tablePrice(presentValue, installments, interestRate float64) []Loan { | |
loans := make([]Loan, 0) | |
for i := 0; i <= int(installments); i++ { | |
payment := price(presentValue, installments, interestRate) | |
if i == 0 { | |
loans = append(loans, NewLoan(float64(i), payment, 0, 0, presentValue)) | |
continue | |
} | |
previousLoan := loans[i-1] | |
interest := calculateInterest(previousLoan.balanceDue, interestRate) | |
amortization := payment - interest | |
loan := NewLoan(float64(i), payment, amortization, interest, previousLoan.balanceDue-amortization) | |
loans = append(loans, loan) | |
} | |
return loans | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestPrice(t *testing.T) { | |
payment := price(26000, 3, 0.04) | |
paymentFixed, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", payment), 64) | |
if paymentFixed != 9369.06 { | |
t.Fail() | |
} | |
} | |
func TestTablePrice(t *testing.T) { | |
results := tablePrice(26000, 3, 0.04) | |
expected := []Loan{ | |
{installments: 0, payment: 9369.06, amortization: 0, interest: 0, balanceDue: 26000}, | |
{installments: 1, payment: 9369.06, amortization: 8329.06, interest: 1040.00, balanceDue: 17670.94}, | |
{installments: 2, payment: 9369.06, amortization: 8662.22, interest: 706.84, balanceDue: 9008.72}, | |
{installments: 3, payment: 9369.06, amortization: 9008.71, interest: 360.35, balanceDue: 0.01}, | |
} | |
assert.Equal(t, expected, results) | |
} | |
func benchmarkTablePrice(pv, n, i float64, b *testing.B) { | |
for y := 0; y < b.N; y++ { | |
tablePrice(pv, n, i) | |
} | |
} | |
func BenchmarkTablePrice1(b *testing.B) { benchmarkTablePrice(26000, 3, 0.04, b) } | |
func BenchmarkTablePrice2(b *testing.B) { benchmarkTablePrice(50000, 6, 0.05, b) } | |
func BenchmarkTablePrice3(b *testing.B) { benchmarkTablePrice(100000, 24, 0.06, b) } | |
func BenchmarkTablePrice4(b *testing.B) { benchmarkTablePrice(200000, 48, 0.02, b) } | |
func BenchmarkTablePrice5(b *testing.B) { benchmarkTablePrice(400000, 64, 0.011, b) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment