Page 1 of 1

integer overflow

Posted: Sat Mar 14, 2020 6:35 pm
by Oscar
Hi all,

For a while I thought I understood how integer overflow works inside combinators (see this old discussion: viewtopic.php?t=20118). But then I started running into outcomes I couldn't fully explain. For instance, while I fully understand why 2,147,483,647 * 2 = -2, I couldn't figure why 2,147,483,647 ^ 2 = 1, shouldn't it be zero? What did I miss?

Re: integer overflow

Posted: Sat Mar 14, 2020 7:02 pm
by coppercoil
Let's try smaller numbers: 127 x 127 = 16129 = 0011 1111 0000 00001
This must be some magic :mrgreen:

Re: integer overflow

Posted: Sat Mar 14, 2020 7:05 pm
by ptx0
at least one case of this was fixed in .18 where mining drills would return a huge negative number when they overflow the 32bit space.

Re: integer overflow

Posted: Sat Mar 14, 2020 8:25 pm
by Oscar
ptx0 wrote: Sat Mar 14, 2020 7:05 pm at least one case of this was fixed in .18 where mining drills would return a huge negative number when they overflow the 32bit space.
I don't think the situation I illustrated is because of a bug. This overflow mechanism is intentional. For instance adding 1 to 2,147,483,647 will give you -2,147,483,648 simply by way of how numbers are stored inside combinators: signed 32 bits integers using two's complement to store negative values.

Therefore I am so confused why 2,147,483,647 ^ 2 gives a 1, since by the above logic 2,147,483,647 ^ 2 in binary format should be 111111 11111111 11111111 11111111 00000000 00000000 00000000 00000000 (62 bits), which means after accounting for overflow you should be left with 00000000 00000000 00000000 00000000 (32 bits), which should equal zero.

The above example is only one of the instances I encountered so far where I could not understand why combinators output different result than I would expected based on my current understanding of overflow. Therefore I think there is still something that escaped my attention.

Anyone anymore ideas? Thanks!

Re: integer overflow

Posted: Sat Mar 14, 2020 8:34 pm
by Loewchen
2147483647² = 4611686014132420609 = 0011111111111111111111111111111100000000000000000000000000000001 in binary 2's compl. or am i missing something?

Re: integer overflow

Posted: Sat Mar 14, 2020 8:42 pm
by Optera
(2 ^ 32 - 1) ^ 2 = 18446744065119617025
according wolframalpha that is 1111111111111111111111111111111000000000000000000000000000000001
https://www.wolframalpha.com/input/?i=% ... 5E2+binary

Re: integer overflow

Posted: Sat Mar 14, 2020 8:45 pm
by DerGraue
Also coppercoil showcased the magic already quite well (although a 0 snuck in where it don't belong)

127 = 0111 1111
127 x 127 = 16129 = 0011 1111 0000 0001

Re: integer overflow

Posted: Sat Mar 14, 2020 8:56 pm
by Optera
Yes, any (2^n-1)^2 in binary has 1 as lsb followed by varying amounts of 0.
With 16bits even 255^2 produces enough zeroes to leave only 1 as result.

Re: integer overflow

Posted: Sat Mar 14, 2020 9:16 pm
by Oscar
I see now where the problem is. I was printing the answer for 2147483647^2 in R, and it gave 4611686014132420608 (I was using: sprintf("%.10f",2147483647^2)). With full precision, the answer is indeed 4611686014132420609 and hence the return value zero.

Many thanks for all the quality answers! :mrgreen: