Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ rounds up my decimals

Reply
Thread Tools

C++ rounds up my decimals

 
 
eli m
Guest
Posts: n/a
 
      04-10-2013
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-10-2013
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
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      04-10-2013
On Apr 10, 3:40*am, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      04-10-2013
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.
 
Reply With Quote
 
Bill Gill
Guest
Posts: n/a
 
      04-11-2013
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
 
Reply With Quote
 
Bill Gill
Guest
Posts: n/a
 
      04-11-2013
On 4/11/2013 3:30 AM, Juha Nieminen wrote:
> Bill Gill <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed) ---
>

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
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      04-11-2013
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

 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      04-11-2013
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.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-11-2013
Öö Tiib <(E-Mail Removed)> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-11-2013
On 4/11/2013 12:51 PM, Stefan Ram wrote:
> Öö Tiib <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recently patched exploit making rounds online Au79 Computer Support 1 06-26-2007 08:00 PM
psycopg2 rounds unix time Luis P. Mendes Python 2 02-06-2006 01:06 PM
latest virus going the rounds Jim Watt Computer Security 6 02-28-2005 08:39 PM
Is this another Worm doing the rounds? pinkbikini@myhome.net NZ Computing 16 10-11-2003 04:06 AM
I gather this has been doing the rounds, but thought some might like it.. anthonyberet Computer Support 2 09-02-2003 01:59 AM



Advertisments