0

My C++ programme may create a value until 10^16 during the run time, I tried to use "long long int" for it but it didn't work. Which data type correspond to 10^16?

Thanks;

eponymous
  • 1,880
  • 5
  • 21
  • 26
  • 5
    Can you be more specific than "it didn't work" please? – Benjamin Lindley Mar 23 '13 at 18:45
  • there is no answer in my programme for 6 minutes, when i finished my programme i saw overflow error. – eponymous Mar 23 '13 at 18:54
  • @Selen That sounds more like a stack overflow than an integer overflow. What makes you think the `10^16` value is the problem? – Joseph Mansfield Mar 23 '13 at 18:55
  • That doesn't tell me much, since I don't know what your program consists of. Try this: `long long x = 10000000000000000; std::cout << x;` -- If that works, then your problem is elsewhere. If it doesn't work, then your compiler is broken. – Benjamin Lindley Mar 23 '13 at 18:57
  • my programme should support L(n)=(12+12)(12+22)(12+32) ..(12+n2) (22+22)(22+32) .. (22+n2) (32+32)(32+42)...(32+n2) .....(n2+n2) fonction for three input. One of them is n and it's range should be like this 1≤ n ≤10^8. when i write long long x = 1000000000000000 , it works but when i run my programme for n = 100000000 , i get an error that is 'process terminated with status -1073741510' – eponymous Mar 23 '13 at 19:12
  • 2
    Clearly your problem then is not that `long long int` does not hold 10^16. It is somewhere in the logic of your program, which we can't see. So this question is answered. You should accept an answer and ask a new question about your code and what is wrong with it. – Benjamin Lindley Mar 23 '13 at 19:28
  • 1
    If you are multiplying this sized long long values, they will overflow. – brian beuning Mar 23 '13 at 20:25
  • I will rewiew my code again. I think there is stg wrong. Thanks for all your anwers. – eponymous Mar 23 '13 at 20:15

4 Answers4

6

The minimum guaranteed maximum of long long int is 2^63 - 1, which is approximately 10^19. It should be fine. To be sure, use the std::int_least64_t type from <cstdint>. It is guaranteed to have at least 64 bits.

Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
1

less than 64bit so.. long long should be good..

Alon
  • 1,736
  • 11
  • 29
1

Try int64_t. That should be long enough. But be sure to enter an integer literal:

int64_t n = 10000000000000000;

If you say 1E16, that's a double literal, and the conversion to integer may produce unexpected results.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 2
    10000000000000000LL – s3rius Mar 23 '13 at 19:10
  • Unless you're running your gcc in pre-C99 mode, which seems you are by default, (or if it's simply some C89/90 compiler) and the compiler truncates what would've been a normal long long constant in C99 to the longest integer type supported by C89, which is long (or unsigned long). Surprise. – Alexey Frunze Mar 24 '13 at 08:51
-2

Unsigned long long Int works up-to 10^18!

Varad
  • 1
  • Welcome to Stack Overflow! The question is not on-topic for Stack Overflow as defined in the [help]. Please don't answer such questions; instead, you should flag them for attention and they will be closed or migrated appropriately. – Toby Speight Oct 19 '16 at 14:23