Velocity Reviews > C++ > Multiplication of big numbers always says zero

# Multiplication of big numbers always says zero

eli m
Guest
Posts: n/a

 03-17-2013
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
Guest
Posts: n/a

 03-17-2013
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
Guest
Posts: n/a

 03-17-2013
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;
}

SG
Guest
Posts: n/a

 03-17-2013
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

eli m
Guest
Posts: n/a

 03-17-2013
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++.

SG
Guest
Posts: n/a

 03-17-2013
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

osmium
Guest
Posts: n/a

 03-17-2013
"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
Guest
Posts: n/a

 03-17-2013
"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.

eli m
Guest
Posts: n/a

 03-17-2013
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.

osmium
Guest
Posts: n/a

 03-18-2013
"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.