Solved

Floating Decimal Points

Posted on 2000-03-31
5
303 Views
Last Modified: 2010-04-02
int item number;
float price; float numsold;
float income; float discount;
float saleprice;

My program reads from an input file and output is #sold - price -discount - saleprice - income
my calculations for saleprice returns:
Ex.- 5.0575
setw(2) returns 5.06 which is correct.

When the program calculates income-
saleprice * numsold the program returns:
65.747498
setw(2) = 65.75 which is incorrect.
The correct answer is 65.78
RAM is using (65.747498 * 13)=65.747498
How do I correct this problem w/ floating point decimals?
Am I overlooking something simple?
I have tried everything I know of.
Help!!!!!!! Thanks    David A Davis
 Nash Community College Rocky Mt NC
0
Comment
Question by:DavidAshley
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2673576
The quick fix is to round all the values to 2 decimal places (round to cents) after you do the calcualtions (also within the calculations, after and multiply or divide steps)..  at the moment you are rounding to 2 decimals only when you print, but not after each calculations.  So the rounding errors that are occuring in your calculations are "componding" and are becoming noticable.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2673593
You can round a number to an integer value by adding .5 to and then using the floor function.  To round to 2 decimals, you multiply it by 100, then round to an intger, then divide by 100.  For example

double RoundToCents(double D)
{
   return floor( (100*D)+ 0.5) / 100;
};

So use this function to round numbers that you create as a result of a calculation before the number is used in a 2nd calculation.

Let me know if you have any questions.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2674387
It is often more dependable to work in exact integer cents instead of approximate floating dollars.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2674532
Yes, I had actually meant to mention this point, that is why I called it a "quick fix" then forgot to mention it at the end.

Floating point numbers are always approximate (although there are certian guarantees that you are given) and thus are really  not appropriate for accounting type calculations where you have to be accurate to the exact penny.  The rounding sugggestion I made will usually be enough to guarantee good results, but occasionally you might still be off a penny.  a better idea would be to work with a number that guarantees perfect results to a specified level of precission.  You can use integers to represent amounts expressed in cents.   i.e $12.34 would be specified as 1234.  Another option would be to use BCD numbers to represent the values.  The advantage of BCD is that it can have a much greater range than integers and it can represent decimal values too.  However intergers are smaller and more efficient.
0
 

Author Comment

by:DavidAshley
ID: 2678559
Thanks a bunch.  You have saved me lots of time, blood, sweat and tears.  Plus I will get an A on this program. David
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 117
C++ to C# code conversion issue 4 100
Unresolved External Symbols 3 68
C++ error cannot convert from std::string to const char * 6 130
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now