Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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?
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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;
}
 
Reply With Quote
 
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
 
Reply With Quote
 
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++.
 
Reply With Quote
 
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++.


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

 
Reply With Quote
 
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.


 
Reply With Quote
 
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.


 
Reply With Quote
 
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.
 
Reply With Quote
 
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.


 
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
Source of term "multiplication" in matrix multiplication William Hughes C Programming 13 03-15-2010 02:04 PM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net 0 12-26-2008 09:29 AM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net Web Controls 0 12-26-2008 06:11 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Python 0 12-24-2008 07:35 AM
Multiplication with zero logiclips@yahoo.com Java 6 02-07-2007 12:21 PM



Advertisments