floating point problem

ok, i had to write a loan amortization program for my c++ class which, for those of you who don't know, displays a monthly chart of loan information. anyway i have it running a loop and each time the loop runs it assigns the current balance to the previous balance minus how much was paid off. like this kinda:
balance = balance - prin_pay (prin_pay is the amount paid off)

all of these variables are floats. my problem is that each time the loop runs, the balance gets a few cents higher than it should. this problem gets very large as the loop progresses. if i do the above math by hand it comes out correct, but for some reason the program screws it up. is it rounding and then adding the rounded values or something?? anyways, how do i fix this because the program is due tomorrow?

Comments

  • : ok, i had to write a loan amortization program for my c++ class which, for those of you who don't know, displays a monthly chart of loan information. anyway i have it running a loop and each time the loop runs it assigns the current balance to the previous balance minus how much was paid off. like this kinda:
    : balance = balance - prin_pay (prin_pay is the amount paid off)
    :
    : all of these variables are floats. my problem is that each time the loop runs, the balance gets a few cents higher than it should. this problem gets very large as the loop progresses. if i do the above math by hand it comes out correct, but for some reason the program screws it up. is it rounding and then adding the rounded values or something?? anyways, how do i fix this because the program is due tomorrow?
    :

    Perhaps you should change your integer types to [b]long[/b]. I think the problem is due to 'Hidden round off errors'.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion