UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » int64_t multiplication problem

#1 2011-01-04 07:51 AM

pankajdev
Member
Registered: 2009-07-15
Posts: 14

int64_t multiplication problem

I have following code

int64_t t1 =  999999999989999LL;
int temp = 10000;
int64_t t2 = (t1 * temp);
cout << "t2 = " << t2 << endl;

Result of this multiplication comes out to be negative. I am running on redhat linux 5,  gcc 4.1.1.

Result t2 = -8446744073809561616

can you please tell me solution for this problem.
Thanks in advance for your help

Offline

#2 2011-01-04 01:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,768
Website

Re: int64_t multiplication problem

Problem?  You're exceding the scope of a signed 64-bit int and overflowing into the sign bit...  This is normal, expected behavior...

I'm not sure what you're trying to accomplish, exactly...  But, why not use an unsigned variable instead?  ("uint64_t", or "unsigned long long"...  And, use "ULL" as the literal suffix...)  That will suffice for this narrow case you're dealing with now, at least...  But, bump it up much more than that, and you'll end up overflowing the unsigned 64-bit int, as well...  You're just getting to the limits of values you can deal with as native ints, so if you really need to deal with such huge values that won't fit in a 64-bit int, then you're going to have to move on to something like a bignum library instead of native ints...

Offline

  • Index
  • » C++
  • » int64_t multiplication problem

Board footer

Powered by FluxBB