its not talkin long long a=111111111111(12 1's)

Nov 5, 2009 at 12:28am
well in one code if i assign a=12 1's its showing tht "integer constant is too large for ‘long’ type"
but if i go through loop
for(;a<x;a=a*10+1);
where x is long long.it can take up a=111111111111(same as above).n shows no error.
since both are same values its showing error if i assign it directly in d beginning,but its fine if it eventually computed tht in a loop.
hope i am clear enof.
Nov 5, 2009 at 1:02am
A 32-bit signed integer's upper limit is 2,147,483,647. You're trying to put 11,111,111,111 in it.
Nov 5, 2009 at 1:21pm
Integral literals in C/C++ are interpreted as type "int" by default unless a type override is specified.

Try

 
long long x = 111111111111LL;  // LL means treat literal as long long 


Nov 5, 2009 at 11:11pm
or try double x

int can only hold so many numbers, there are other ways to expand it, long long x like jsmith said,
theres long x, double, int, can't remeber anymore if there are any.

signed = only a positive number
unsigned = only negative number

not sure if thats right, but they allow you to use more numbers. For a number with 12 1's it is probably best to use double.

just put double x instead of int x. HTH, also feel free to correct me if i'm wrong and as helios said , int can hold at the max: 2,147,483,647 anymore and u have to change data type (int, char, double, float etc...)
Nov 6, 2009 at 12:56pm
@rej3kt:

?

signed means the value has sign, meaning it can hold positive and negative numbers.
unsigned means the value has no sign (meaning it can hold only non-negative numbers).

But the solution to "my integer can't store a number large enough" is rarely ever to switch
to floating point arithmetic; floating point has a whole other set of issues that integer
arithmetic does not have. If C++ does not have an integral POD-type large enough, then
OP should consider a bigint library such as GMP (and there are others).

Nov 6, 2009 at 5:38pm
thanks for correcting me I was obviously wrong lol
Nov 15, 2009 at 6:06pm
but my problem is not about storing large number in int.its about how that big number is gettin stored in int if i use that loop but it shows error if i assign tht value directly during starting of my programme
Nov 15, 2009 at 8:01pm
Post your code.
Nov 15, 2009 at 8:30pm
Unless your number just had to be an integer, I would just set your number as a float and divide it by 1000 to make it 11111111.111 which float can hold. then if you need to do something with it in your program, just multiply it by 1000 and its 11 billion again.

Nov 15, 2009 at 9:17pm
set your number as a float and divide it by 1000 to make it 11111111.111 which float can hold. then if you need to do something with it in your program, just multiply it by 1000 and its 11 billion again.
It's almost like magic! 32-bit floating point variables can hold more information than 32-bit integral variables!
Last edited on Nov 15, 2009 at 9:17pm
Topic archived. No new replies allowed.