C++ rounds up my decimals

Discussion in 'C++' started by eli m, Apr 10, 2013.

  1. eli m

    eli m Guest

    Hi I made this code:

    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main() {
    int getIntx()
    unsigned int ans;
    double mfirstnum, msecondnum;
    cout << "Type in your first number:";
    mfirstnum = getIntx();
    cout << "Multiply your first number by:";
    msecondnum = getIntx();
    ans = mfirstnum * msecondnum;
    cout << ans << "\n"; }
    //Get int x
    int getIntx() {
    double x = 0;

    while(!(cin >> x)) {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    cout << "Invalid Input. Try again:";
    }
    return x;
    }

    So, whenever I type in a decimal it rounds it up. Why is this happening?
    eli m, Apr 10, 2013
    #1
    1. Advertising

  2. eli m

    Ian Collins Guest

    eli m wrote:
    > Hi I made this code:
    >
    > #include <stdlib.h>
    > #include <iostream>
    > using namespace std;
    > int main() {
    > int getIntx()
    > unsigned int ans;
    > double mfirstnum, msecondnum;
    > cout << "Type in your first number:";
    > mfirstnum = getIntx();
    > cout << "Multiply your first number by:";
    > msecondnum = getIntx();
    > ans = mfirstnum * msecondnum;
    > cout << ans << "\n"; }
    > //Get int x


    White-space comes at no extra cost.

    > int getIntx() {
    > double x = 0;
    >
    > while(!(cin >> x)) {
    > cin.clear();
    > cin.ignore(numeric_limits<streamsize>::max(),'\n');
    > cout << "Invalid Input. Try again:";
    > }
    > return x;
    > }
    >
    > So, whenever I type in a decimal it rounds it up. Why is this happening?


    Because your input function returns an int?

    --
    Ian Collins
    Ian Collins, Apr 10, 2013
    #2
    1. Advertising

  3. eli m

    Paul N Guest

    On Apr 10, 3:40 am, Ian Collins <> wrote:
    > eli m wrote:
    > > Hi I made this code:

    >
    > > #include <stdlib.h>
    > > #include <iostream>
    > > using namespace std;
    > > int main() {
    > > int getIntx()
    > > unsigned int ans;
    > > double mfirstnum, msecondnum;
    > > cout << "Type in your first number:";
    > > mfirstnum = getIntx();
    > > cout << "Multiply your first number by:";
    > > msecondnum = getIntx();
    > > ans = mfirstnum * msecondnum;
    > > cout << ans << "\n"; }
    > > //Get int x

    >
    > White-space comes at no extra cost.
    >
    > > int getIntx() {
    > >            double x = 0;

    >
    > >            while(!(cin >> x)) {
    > >                    cin.clear();
    > >                    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    > >                    cout << "Invalid Input. Try again:";
    > >            }
    > >            return x;
    > >    }

    >
    > > So, whenever I type in a decimal it rounds it up. Why is this happening?

    >
    > Because your input function returns an int?


    I thought C++ tended to round things down rather than up?

    Still, I'm more interested in how he got

    > > int getIntx()
    > > unsigned int ans;


    to compile.
    Paul N, Apr 10, 2013
    #3
  4. eli m

    Öö Tiib Guest

    On Wednesday, 10 April 2013 23:44:23 UTC+3, Paul N wrote:
    > I thought C++ tended to round things down rather than up?


    No. Conversion from double to int will truncate toward zero.
    Rounding down is done with floor().

    OP was either testing with negative values so saw round up
    effect or was also puzzled with terminology.

    > Still, I'm more interested in how he got
    >
    > > > int getIntx()
    > > > unsigned int ans;

    >
    > to compile.


    Hmm. But that is elementary, Watson.
    Öö Tiib, Apr 10, 2013
    #4
  5. eli m

    Bill Gill Guest

    On 4/10/2013 4:23 PM, Öö Tiib wrote:
    > On Wednesday, 10 April 2013 23:44:23 UTC+3, Paul N wrote:
    >> I thought C++ tended to round things down rather than up?

    >
    > No. Conversion from double to int will truncate toward zero.
    > Rounding down is done with floor().
    >
    >

    That came as a bit of surprise to me. I was always taught that
    it truncated by dropping everything after the decimal point. Or
    at least that was what I understood. If I had tried to convert
    a negative double to int I would have had problems just like the
    OP. I will have to remember that in the future.

    Bill
    Bill Gill, Apr 11, 2013
    #5
  6. eli m

    Bill Gill Guest

    On 4/11/2013 3:30 AM, Juha Nieminen wrote:
    > Bill Gill <> wrote:
    >> On 4/10/2013 4:23 PM, ?? Tiib wrote:
    >>> On Wednesday, 10 April 2013 23:44:23 UTC+3, Paul N wrote:
    >>>> I thought C++ tended to round things down rather than up?
    >>>
    >>> No. Conversion from double to int will truncate toward zero.
    >>> Rounding down is done with floor().
    >>>

    >> That came as a bit of surprise to me. I was always taught that
    >> it truncated by dropping everything after the decimal point.

    >
    > And the difference with "truncates towards zero" is what, exactly?
    >
    > --- news://freenews.netfront.net/ - complaints: ---
    >

    Ok, I see. Sometimes I get carried away with my own smarts.

    I think I got confused because I never heard (or saw) it stated that
    way before.

    Bill
    Bill Gill, Apr 11, 2013
    #6
  7. eli m

    Stuart Guest

    On 10 April 2013, Paul N wrote:
    >> Still, I'm more interested in how he got
    >>
    >>>> int getIntx()
    >>>> unsigned int ans;

    >>
    >> to compile.


    On 04/10/13, Öö Tiib wrote:
    > Hmm. But that is elementary, Watson.


    Err, would you mind giving an explanation to the not so enlightened, Mr.
    Holmes? My compiler says "Expected ';' at end of declaration."

    I'm pretty positive that this is either some funky compiler extension or
    some left-over from C. Hopefully the first.

    Thanks,
    Stuart
    Stuart, Apr 11, 2013
    #7
  8. eli m

    Öö Tiib Guest

    On Thursday, 11 April 2013 18:13:28 UTC+3, Stuart wrote:
    > On 10 April 2013, Paul N wrote:
    > >> Still, I'm more interested in how he got
    > >>
    > >>>> int getIntx()
    > >>>> unsigned int ans;
    > >>
    > >> to compile.

    >
    > On 04/10/13, Öö Tiib wrote:
    >
    > > Hmm. But that is elementary, Watson.

    >
    > Err, would you mind giving an explanation to the not so enlightened, Mr.
    > Holmes? My compiler says "Expected ';' at end of declaration."


    Excellent! So what does a developer do when compiler tells him that ';' is
    missing?

    > I'm pretty positive that this is either some funky compiler extension or
    > some left-over from C. Hopefully the first.


    I think that he never compiled posted code. He pasted compiling code into
    post and then edited it so a semicolon got accidentally removed.
    Öö Tiib, Apr 11, 2013
    #8
  9. eli m

    Stefan Ram Guest

    Öö Tiib <> writes:
    >Excellent! So what does a developer do when compiler tells him that ';' is
    >missing?


    That does not explain how he got

    int getIntx()
    unsigned int ans;

    (without that semicolon!) to compile. He claimed

    »whenever I type in a decimal it rounds it up.«,

    which implied that that code did compile as posted.
    Stefan Ram, Apr 11, 2013
    #9
  10. On 4/11/2013 12:51 PM, Stefan Ram wrote:
    > Öö Tiib <> writes:
    >> Excellent! So what does a developer do when compiler tells him that ';' is
    >> missing?

    >
    > That does not explain how he got
    >
    > int getIntx()
    > unsigned int ans;
    >
    > (without that semicolon!) to compile. He claimed
    >
    > »whenever I type in a decimal it rounds it up.«,
    >
    > which implied that that code did compile as posted.
    >


    It could be a severe case of "I type my code into newsreader instead of
    copy-pasting" disorder...

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 11, 2013
    #10
  11. eli m

    Stuart Guest

    >> On 10 April 2013, Paul N wrote:
    >>>> Still, I'm more interested in how he got
    >>>>
    >>>>>> int getIntx()
    >>>>>> unsigned int ans;
    >>>>
    >>>> to compile.


    On 04/10/13, Öö Tiib wrote:
    >>> Hmm. But that is elementary, Watson.


    On 4/11/2013, Stuart wrote:
    >> Err, would you mind giving an explanation to the not so enlightened, Mr.
    >> Holmes? My compiler says "Expected ';' at end of declaration."


    On 04/11/2013, Öö Tiib wrote:

    > Excellent! So what does a developer do when compiler tells him that ';' is
    > missing?
    >
    >> I'm pretty positive that this is either some funky compiler extension or
    >> some left-over from C. Hopefully the first.

    >
    > I think that he never compiled posted code. He pasted compiling code into
    > post and then edited it so a semicolon got accidentally removed.
    >


    Oh dear me. I was hoping that this would turn out to be the perfect ...
    err ... crime? Well, posting non-workable examples should certainly be
    considered a crime.

    Back to Baker Street, then!

    Thanks,
    Stuart (a.k.a. Watson in this thread ;-)
    Stuart, Apr 11, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri

    Formatting Decimals

    Kerri, Aug 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. Kevin Spencer

    Re: Formatting Decimals

    Kevin Spencer, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    396
    Kevin Spencer
    Aug 26, 2003
  3. Brent Burkart

    asp.net text box for decimals

    Brent Burkart, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    497
    Chris Jackson
    Nov 12, 2003
  4. hansiman
    Replies:
    1
    Views:
    19,390
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
    Oct 8, 2004
  5. Luis P. Mendes

    psycopg2 rounds unix time

    Luis P. Mendes, Feb 6, 2006, in forum: Python
    Replies:
    2
    Views:
    361
    Luis P. Mendes
    Feb 6, 2006
Loading...

Share This Page