# Hmm. A Quandary. Rounding/adding problem

• 10-06-2005
Sennet
I'm working on a program that rounds and adds two real numbers provided by the user. All is going well, except that one of my sample runs is producing an undesired result, and I can't figure out how to fix it for the life of me. The code itself is rather simple, but one problem just. Won't. Work.

1.674 + 1.322 produces:

1.67
1.32
---------
3.00

It should be producing 2.99! D: Please, any advice on how to get it to produce the desired output would be very, very much appreciated.

Code:

```#include <iostream> #include <iomanip> using namespace std; int main() { double a, b, sum; cout << "Enter two real numbers to be rounded and added: "; cin >> a >> b; sum = a + b; cout << endl << setw(30) << setiosflags(ios::fixed) << setprecision(2) << a << endl << setw(30) << b << endl << setw(30) << "---------" << setw(30) << endl << sum << endl; return 0; }```
• 10-06-2005
Daved
First, you are computing the sum before you do any rounding, whereas you should be rounding first, then doing the sum. Second, you aren't rounding at all, you are just changing the way the number is displayed. It is still stored the same way in memory.

You need to come up with a method to round a number and apply it to a and b before you compute the sum.
• 10-06-2005
Sennet
Quote:

Originally Posted by Daved
First, you are computing the sum before you do any rounding, whereas you should be rounding first, then doing the sum. Second, you aren't rounding at all, you are just changing the way the number is displayed. It is still stored the same way in memory.

You need to come up with a method to round a number and apply it to a and b before you compute the sum.

Our compiler rounds the manipulated numbers instead of truncating the extra decimal places. Your mileage may vary.

We're supposed to be practicing the use of setw() and setprecision(). We haven't gotten deep enough into our material to round otherwise, I think!

If anyone can be a bit more helpful with this new information, PLEASE. :(
• 10-06-2005
Enahs
The precision function will automatically round.

So in your case you are getting 2.996, which then the last decimal place gets rounded.
If you did some math and got 2.994, then your answer would result in 2.99.

That is just how precision works to my knowledge.
• 10-06-2005
Decrypt
setprecision() , AFAIK, does not actually change the value of a or b, it just sets the output precision to whatever you set it to. That said, there is no way (I can think of) to round the numbers prior to adding them using only setprecision(). You need a way to round by actually changing the values of a and b before the addition if you want the answer to be 2.99. I could be wrong - if so, I apologize. I'll look into it more after I get home...

Decrypt
• 10-06-2005
ChaosEngine
as others have said, setprecision only affects the display of the variables. Probably your best bet is to look into fixed point math.
• 10-06-2005
Enahs
In VC++ 6 setprecision not only rounds, depending on how you use it and call it, it might include things in the precision on the left of the decimal place.
• 10-06-2005
Sennet
Why do I get the sneaking suspicion that my prof has given us ANOTHER project that is impossible for us to do at the moment? Blar. Anybody know how to round?
• 10-06-2005
sam_8917
I actually have a similiar problem, I am trying to create a program that in the end calculates income. The problem is that i want only two decimal places since I am working with currency. I have been trying to use the round() function but it is not working. If anyone could explain how it works that would be really cool, thanks.
• 10-06-2005
Enahs
This will do what you want. In this specific case.
It will vary though depending on how many digits are to the left of the decimal (but you should be able to easily figure out how to solve that).

You would probably want to put it in a single function so you can just call it and it does it.

But I will point out, that this way you are still rounding (down) and 2.99 is further away from the actual value than 3.00.

3.00 is a more correct value in this specific case.

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {         double test = 1.674;         double test2 = 1.322;         test = (test * 100); //*100 get everything you want to keep to the left of decimal         test2 = (test2 * 100);         test = floor(test); //rounds to nearest integer (floor for down, ceil for up)         test2 = floor(test2);         test = (test / 100); // (/100) to get it back to proper decimal places         test2 = (test2 / 100);         cout << test + test2;         return 0; }```
Results = 2.99
• 10-07-2005
Sennet
Quote:

Originally Posted by Enahs
But I will point out, that this way you are still rounding (down) and 2.99 is further away from the actual value than 3.00.

3.00 is a more correct value in this specific case.

Yes, but it has to be 2.99 for the program to be accepted. No, I'm not entirely sure WHY it has to be such. I'll fiddle around with your idea and see what comes up.
• 10-08-2005
Salem
I've said this many times before, floats are approximations and dealing with that takes a lot of care and attention.

Some code and output to munch on
Code:

```#include <iostream> #include <iomanip> using namespace std; void example1(void) {   double a, b, sum;   cout << "Enter two real numbers to be rounded and added: ";   cin >> a >> b;   sum = a + b;   cout << endl << setw(30) << setiosflags(ios::fixed) << setprecision(2) << a << endl <<       setw(30) << b << endl << setw(30) << "---------" << setw(30) << endl << sum << endl; } double roundto2dp ( double a ) {   int temp = (int)( a * 100.0 );   return temp / 100.0; } void example2(void) {   double a, b, sum;   cout << "Enter two real numbers to be rounded and added: ";   cin >> a >> b;   cout << "Actual values " << setprecision(20) << a << " " << b << endl;   a = roundto2dp(a);   b = roundto2dp(b);   sum = a + b;   cout << "Actual values " << setprecision(20) << a << " " << b << " " << sum << endl;   cout << endl << setw(30) << setiosflags(ios::fixed) << setprecision(2) << a << endl <<       setw(30) << b << endl << setw(30) << "---------" << setw(30) << endl << sum << endl; } int main ( ) {   example1();   example2();   return 0; } \$ g++ foo.cpp \$ ./a.out Enter two real numbers to be rounded and added: 1.674 1.322                           1.67                           1.32                     ---------                           3.00 Enter two real numbers to be rounded and added: 1.674 1.322 Actual values 1.67399999999999993250 1.32200000000000006395 Actual values 1.66999999999999992895 1.32000000000000006217 2.99000000000000021316                           1.67                           1.32                     ---------                           2.99```
Doubles have about 15 decimal digits of precision, so the last 5 or so digits in each of the actual results are just noise.