Howdy, Stranger!

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

Categories

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

  • Geeth AsokanGeeth Asokan Member Posts: 127
    : 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'.

  • Shawn CarterShawn Carter Member Posts: 0

    _______ \ http://forcoder.org \ free ebooks and video tutorials about / PHP C Objective-C Perl Java Visual Basic C++ Python JavaScript Delphi Visual Basic .NET Scratch Assembly MATLAB R Ruby Go C# PL/SQL Swift Scheme Julia Hack Clojure Rust Fortran VBScript COBOL Alice ML Crystal F# Prolog Lua Scala Ada LabVIEW SAS Bash D Logo Apex Kotlin Erlang ABAP Awk Lisp Dart Transact-SQL FoxPro / _

Sign In or Register to comment.