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?
integer overflow
-
- Filter Inserter
- Posts: 502
- Joined: Tue Jun 26, 2018 10:14 am
- Contact:
Re: integer overflow
Let's try smaller numbers: 127 x 127 = 16129 = 0011 1111 0000 00001
This must be some magic
This must be some magic
Last edited by coppercoil on Sat Mar 14, 2020 7:11 pm, edited 2 times in total.
Re: integer overflow
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
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
2147483647² = 4611686014132420609 = 0011111111111111111111111111111100000000000000000000000000000001 in binary 2's compl. or am i missing something?
Re: integer overflow
(2 ^ 32 - 1) ^ 2 = 18446744065119617025
according wolframalpha that is 1111111111111111111111111111111000000000000000000000000000000001
https://www.wolframalpha.com/input/?i=% ... 5E2+binary
according wolframalpha that is 1111111111111111111111111111111000000000000000000000000000000001
https://www.wolframalpha.com/input/?i=% ... 5E2+binary
My Mods: mods.factorio.com
Re: integer overflow
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
127 = 0111 1111
127 x 127 = 16129 = 0011 1111 0000 0001
Re: integer overflow
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.
With 16bits even 255^2 produces enough zeroes to leave only 1 as result.
My Mods: mods.factorio.com
Re: integer overflow
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!
Many thanks for all the quality answers!