lundi 22 janvier 2018

Possible rounding error? C++

I'm still very new to c++, so forgive me if this should is an ignorant question. The purpose of this short code is to calculate a monthly car payment based on 6 user inputs.

Using these inputs, 623.72 is output, however I was expecting 626.81 Inputs, in order: 20000, 0.06, 1000, 100, 0.07, 36.

Can anyone shed any light on why my answer is slightly off? Am I running into a rounding error? Am I rounding in the wrong place or using a wrong variable type? Thanks!

The formula for monthly payment is broken down into steps which made it easier to write. It's based off this formula:

monthly_payment = Final_price_minus_downpayment * ( (monthly_rate * (1 + monthly_rate)^num_months / (1 + monthly_rate)^num_months - 1 )

Code Screenshot

Aucun commentaire:

Enregistrer un commentaire