0

I was making a Pascal's triangle in AS3.

And in the results I find that, the addition of 1037158320 and 1166803110 yields -2091005866, a negative integer!

This is weird. I first thought that the magnitudes were too great for an int to hold, but the magnitude of the sum is even greater than the two addends.

And plus, as a number approaches the center of a line of a Pascal's triangle, it should increase, but the one next to -2091005866 was -1961361076, which is closer to the centerm but the magnitude decreases!

Any possible solutions?

devnull
  • 118,548
  • 33
  • 236
  • 227
hello all
  • 252
  • 2
  • 17

1 Answers1

4

Quoting ActionScript 3.0 Reference:

The int class lets you work with the data type representing a 32-bit signed integer. The range of values represented by the int class is -2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1).

1037158320 + 1166803110 = 2203961430 which is greater than 2147483647.

Consider using uint or Number.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • +1 for Number. In AS3 `Number` is capable of containing 64-bit integer precision, as well as a double-precision floating-point value. – Vesper Apr 19 '13 at 05:35
  • You should avoid uint when using such large numbers here as well, since uint has a max value of 2^32-1 (4.2 billion) which isn't a whole lot when dealing with numbers that large. By comparison, Number has a max value of 1.79e+308 – Josh Apr 19 '13 at 05:45
  • @Vesper, "In AS3 Number is capable of containing 64-bit integer precision" it's not full correct, Number is IEEE754 Double precision 64-bit so it keeps only 52 bit for the mantisa, so the maximum integer without loss in precision is 0xFFFFFFFFFFFFF = 4503599627370495 – fsbmain Apr 19 '13 at 07:17
  • 1
    I checks the docs to be sure and the actual maximum integer is even 53 bit (0x1FFFFFFFFFFFFF = 9007199254740991) because of way the system store Double there is 1 additional bit ) – fsbmain Apr 19 '13 at 07:33