Multiplication of big numbers always says zero

Discussion in 'C++' started by eli m, Mar 17, 2013.

1. eli mGuest

Whenever i try to multiply big numbers in c++ (for example: 384723987432324 * 23042348372947233240) it always says zero for the answer. Why does it do this?
eli m, Mar 17, 2013

2. eli mGuest

On Sunday, March 17, 2013 11:20:58 AM UTC-7, eli m wrote:
> Whenever i try to multiply big numbers in c++ (for example: 384723987432324 * 23042348372947233240) it always says zero for the answer. Why does it do this?

And when i also multiply decimals it says zero.
eli m, Mar 17, 2013

3. eli mGuest

On Sunday, March 17, 2013 11:54:01 AM UTC-7, osmium wrote:
> "eli m" write:
>
>
>
> > On Sunday, March 17, 2013 11:20:58 AM UTC-7, eli m wrote:

>
> >> Whenever i try to multiply big numbers in c++ (for example:

>
> >> 384723987432324 * 23042348372947233240) it always says zero for the

>
> >> answer. Why does it do this?

>
> >

>
> > And when i also multiply decimals it says zero.

>
>
>
> How about posting the code for the decimal multiply? This sounds more
>
> mysterious to me than your first question.

int main() {
int mfirstnum, msecondnum;
usigned float ans;
cout << "Type in your first number:";
mfirstnum = getIntx();
cout << "Multiply your first number by:";
msecondnum = getIntx();
ans = mfirstnum * msecondnum;
cout << ans << "\n"; }
int getIntx() {
double x = 0;

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

Am 17.03.2013 19:20, schrieb eli m:
> Whenever i try to multiply big numbers in c++ (for example:
> 384723987432324 * 23042348372947233240) it always says zero for the
> answer. Why does it do this?

What does your C++ book say on
- the type(s) of integer literals?
- the values different integer types can represent?
- on arithmetic overflows?

SG
SG, Mar 17, 2013
5. eli mGuest

On Sunday, March 17, 2013 12:12:54 PM UTC-7, SG wrote:
> Am 17.03.2013 19:20, schrieb eli m:
>
> > Whenever i try to multiply big numbers in c++ (for example:

>
> > 384723987432324 * 23042348372947233240) it always says zero for the

>
> > answer. Why does it do this?

>
>
>
> What does your C++ book say on
>
> - the type(s) of integer literals?
>
> - the values different integer types can represent?
>
> - on arithmetic overflows?
>
>
>
> SG

I don't have a book on c++.
eli m, Mar 17, 2013
6. SGGuest

Am 17.03.2013 20:13, schrieb eli m:
> On Sunday, March 17, 2013 12:12:54 PM UTC-7, SG wrote:
>> Am 17.03.2013 19:20, schrieb eli m:
>>> Whenever i try to multiply big numbers in c++ (for example:
>>> 384723987432324 * 23042348372947233240) it always says zero for the
>>> answer. Why does it do this?

>>
>> What does your C++ book say on
>> - the type(s) of integer literals?
>> - the values different integer types can represent?
>> - on arithmetic overflows?
>>
>> SG

>
> I don't have a book on c++.

See
http://stackoverflow.com/questions/388242/
for a nice selection.

And since you posted some of the code
here are two more homework questions for you:

What does the C++ book you will be ordering say

- about the type "unsigned float"?

- about conversions between different arithmetic
types with respect to whether or not the
values will be preserved and if not, whether
or not there is some specific expected behaviour
of the program?

SG
SG, Mar 17, 2013
7. osmiumGuest

"eli m" write:

> On Sunday, March 17, 2013 11:20:58 AM UTC-7, eli m wrote:
>> Whenever i try to multiply big numbers in c++ (for example:
>> 384723987432324 * 23042348372947233240) it always says zero for the
>> answer. Why does it do this?

>
> And when i also multiply decimals it says zero.

How about posting the code for the decimal multiply? This sounds more
mysterious to me than your first question.
osmium, Mar 17, 2013
8. osmiumGuest

"eli m" wrote:

>> > And when i also multiply decimals it says zero.

>>
>>
>>
>> How about posting the code for the decimal multiply? This sounds more
>>
>> mysterious to me than your first question.

> int main() {
> int mfirstnum, msecondnum;
> usigned float ans;
> cout << "Type in your first number:";
> mfirstnum = getIntx();
> cout << "Multiply your first number by:";
> msecondnum = getIntx();
> ans = mfirstnum * msecondnum;
> cout << ans << "\n"; }
> int getIntx() {
> double x = 0;
>
> while(!(cin >> x)) {
> cin.clear();
> cin.ignore(numeric_limits<streamsize>::max(),'\n');
> cout << "Invalid Input. Try again:";
> }
> return x;
> }

That is a fragment, not a program.

I don't know why any compiler, of any vintage, would compile that.
Certainly a modern compiler would reject it. I can't think of any good
reason to use whatever it is you have. Decent compilers are free, search
the archives. Get a compiler. Get a book. Read the first part of the
book. Then write a program. Don't use the tab key. Cut and paste; don't
retype when posting.

DevC is an adequate compiler for the kind of thing you want to do if you are
using Windows.

WRT your first question, your numbers are likely too large, the compiler can
do strange things then, possibly including printing a zero. The answer you
want may be hidden someplace in a huge specification I refuse to look at.
Life is too short for that kind of thing.
osmium, Mar 17, 2013
9. eli mGuest

On Sunday, March 17, 2013 12:59:46 PM UTC-7, osmium wrote:
> "eli m" wrote:
>
>
>
> >> > And when i also multiply decimals it says zero.

>
> >>

>
> >>

>
> >>

>
> >> How about posting the code for the decimal multiply? This sounds more

>
> >>

>
> >> mysterious to me than your first question.

>
> > int main() {

>
> > int mfirstnum, msecondnum;

>
> > usigned float ans;

>
> > cout << "Type in your first number:";

>
> > mfirstnum = getIntx();

>
> > cout << "Multiply your first number by:";

>
> > msecondnum = getIntx();

>
> > ans = mfirstnum * msecondnum;

>
> > cout << ans << "\n"; }

>
> > int getIntx() {

>
> > double x = 0;

>
> >

>
> > while(!(cin >> x)) {

>
> > cin.clear();

>
> > cin.ignore(numeric_limits<streamsize>::max(),'\n');

>
> > cout << "Invalid Input. Try again:";

>
> > }

>
> > return x;

>
> > }

>
>
>
> That is a fragment, not a program.
>
>
>
> I don't know why any compiler, of any vintage, would compile that.
>
> Certainly a modern compiler would reject it. I can't think of any good
>
> reason to use whatever it is you have. Decent compilers are free, search
>
> the archives. Get a compiler. Get a book. Read the first part of the
>
> book. Then write a program. Don't use the tab key. Cut and paste; don't
>
> retype when posting.
>
>
>
> DevC is an adequate compiler for the kind of thing you want to do if you are
>
> using Windows.
>
>
>
> WRT your first question, your numbers are likely too large, the compiler can
>
> do strange things then, possibly including printing a zero. The answer you
>
> want may be hidden someplace in a huge specification I refuse to look at.
>
> Life is too short for that kind of thing.

I am using visual studio 2010.
eli m, Mar 17, 2013
10. osmiumGuest

"eli m" wrote:

> Whenever i try to multiply big numbers in c++ (for example:
> 384723987432324 * 23042348372947233240) it always says zero for the
> answer. Why does it do this?

The product is about 10^32 so it takes about 96 bits to represent it.
Visual Studio doesn't have any integer type that large. People usually
handle this by using a big number library of some sort. I suggest you work
on something easier and more rewarding.
osmium, Mar 18, 2013
11. GeoffGuest

On Sun, 17 Mar 2013 16:43:11 -0700 (PDT), eli m
<> wrote:

>I am using visual studio 2010.

Your sample code won't compile in Visual Studio 2010 as written.

I suggest you pay attention to the warnings and errors your compiler
is generating and deal with them carefully, one at a time.

Learn to set breakpoints in the IDE and to execute your code with the
single-step keys, F10 and F11 in Visual Studio.

Others have recommended you get a book on C++, you should do this
immediately and start at the beginning and work out the examples in
that book. You need to learn the language before you can plan and

You have three learning curves to overcome:
1. Learning the language.
2. Learning how to use the IDE you have chosen.
3. Learning how to think clearly and systematically about programming