Hmm. A Quandary. Rounding/adding problem

This is a discussion on Hmm. A Quandary. Rounding/adding problem within the C++ Programming forums, part of the General Programming Boards category; I'm working on a program that rounds and adds two real numbers provided by the user. All is going well, ...

  1. #1
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20

    Hmm. A Quandary. Rounding/adding problem

    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.

    Here's the code, for your referrence. Sorry about any formatting issues.

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  3. #3
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    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.

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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
    There is a difference between tedious and difficult.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    as others have said, setprecision only affects the display of the variables. Probably your best bet is to look into fixed point math.

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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.

  8. #8
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    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?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    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. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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

  11. #11
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    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.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hmm... (iterator declaration problem)
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2008, 06:02 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. hmm....terrain wont display
    By EvBladeRunnervE in forum Game Programming
    Replies: 4
    Last Post: 05-26-2003, 11:26 AM
  4. hmm, easy problem from by book.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 01-07-2003, 11:14 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21