integer overflow

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Oscar
Burner Inserter
Burner Inserter
Posts: 18
Joined: Sat Mar 14, 2020 5:10 pm
Contact:

integer overflow

Post 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?

coppercoil
Filter Inserter
Filter Inserter
Posts: 472
Joined: Tue Jun 26, 2018 10:14 am
Contact:

Re: integer overflow

Post by coppercoil »

Let's try smaller numbers: 127 x 127 = 16129 = 0011 1111 0000 00001
This must be some magic :mrgreen:
Last edited by coppercoil on Sat Mar 14, 2020 7:11 pm, edited 2 times in total.

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: integer overflow

Post 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.

Oscar
Burner Inserter
Burner Inserter
Posts: 18
Joined: Sat Mar 14, 2020 5:10 pm
Contact:

Re: integer overflow

Post 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!

Loewchen
Global Moderator
Global Moderator
Posts: 8301
Joined: Wed Jan 07, 2015 5:53 pm
Contact:

Re: integer overflow

Post by Loewchen »

2147483647² = 4611686014132420609 = 0011111111111111111111111111111100000000000000000000000000000001 in binary 2's compl. or am i missing something?

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: integer overflow

Post by Optera »

(2 ^ 32 - 1) ^ 2 = 18446744065119617025
according wolframalpha that is 1111111111111111111111111111111000000000000000000000000000000001
https://www.wolframalpha.com/input/?i=% ... 5E2+binary

DerGraue
Fast Inserter
Fast Inserter
Posts: 151
Joined: Mon May 30, 2016 12:12 pm
Contact:

Re: integer overflow

Post 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

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2915
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: integer overflow

Post 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.

Oscar
Burner Inserter
Burner Inserter
Posts: 18
Joined: Sat Mar 14, 2020 5:10 pm
Contact:

Re: integer overflow

Post 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:

Post Reply

Return to “Gameplay Help”