3

I want to calculate the number of inversions for a very big array, something like 200,000 ints, and the number I get is quite big. So big it can't be stored in an int value.

The answer I get is something like -8,353,514,212, while for simple cases it works, so I think that the problem is the type of the variable I use to store the number of inversions.

I also tried with long int and the output is the same, but if I try with double 4.0755e+009 is the output. I don't know what the problem is.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
exilonX
  • 1,712
  • 3
  • 26
  • 50

2 Answers2

3
  1. use an unsigned data type
  2. use unsigned long (usually 2^32-1) or unsigned long long (usually 2^64-1)

For full reference see this article.

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
0

If the native types of the compiler aren't fit to hold the result of your computation, you could consider using a bignum library.

A quick search revealed these two:

http://www.ttmath.org/

http://gmplib.org/

I've no experience with either, but gmp seems to be the more popular choice around SO, so maybe that's what you should try first

Radu Chivu
  • 1,057
  • 7
  • 17