Multiplication of big numbers always says zero

E

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?
 
E

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?

And when i also multiply decimals it says zero.
 
E

eli m

"eli m" write:






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

SG

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
 
E

eli m

Am 17.03.2013 19:20, schrieb eli m:






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++.
 
S

SG

Am 17.03.2013 20:13, schrieb eli m:
I don't have a book on c++.

Then you should do something about this, seriously!
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
 
O

osmium

"eli m" write:
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.
 
O

osmium

eli m said:
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.
 
E

eli m

:


















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.
 
O

osmium

eli m said:
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.
 
G

Geoff

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
implement your own projects.

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
discipline and your problem space.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top