HVM Integer Arithmetic Question
Posted: Wed Sep 07, 2011 3:47 pm
I'm not sure if I understand the signed integer arithmetic of the HVM correctly. Take for example this code which is a bit lengthy, but mathematically correct:
(EDIT: Added line breaks to make the example look less ugly. hackvm.py seems to allow them when interpreting the code)
It computes 2^0 + 2^1 + 2^2 + ... + 2^28 + 2^29 + 2^30, which is the same as 2^31-1. The printed result is, correctly so, 2147483647. This is the largest positive number which can be stored in a signed 32-bit integer, which is what the HVM description claims is being used.
Now, what happens if I add 1 to that? The result printed is 2147483648. I don't know how this would be possible, since this number cannot be held in a signed 32-bit integer. I would either expect an error message ("integer overflow", this is what happens in the JavaScript implementation), or a "wrap around" to the number -2147483648, but neither of that happens using the official implementation (hackvm.py).
So, is my understanding of things wrong here, or is this an error in the Python implementation of the HVM? Or is the HVM working correctly, and it is rather the documentation which is erroneous?
(EDIT: Added line breaks to make the example look less ugly. hackvm.py seems to allow them when interpreting the code)
Code: Select all
12+
4+
8+
82*+
84*+
88*+
88*2*+
88*4*+
88*8*+
88*8*2*+
88*8*4*+
88*8*8*+
88*8*8*2*+
88*8*8*4*+
88*8*8*8*+
88*8*8*8*2*+
88*8*8*8*4*+
88*8*8*8*8*+
88*8*8*8*8*2*+
88*8*8*8*8*4*+
88*8*8*8*8*8*+
88*8*8*8*8*8*2*+
88*8*8*8*8*8*4*+
88*8*8*8*8*8*8*+
88*8*8*8*8*8*8*2*+
88*8*8*8*8*8*8*4*+
88*8*8*8*8*8*8*8*+
88*8*8*8*8*8*8*8*2*+
88*8*8*8*8*8*8*8*4*+
88*8*8*8*8*8*8*8*8*+p!
Now, what happens if I add 1 to that? The result printed is 2147483648. I don't know how this would be possible, since this number cannot be held in a signed 32-bit integer. I would either expect an error message ("integer overflow", this is what happens in the JavaScript implementation), or a "wrap around" to the number -2147483648, but neither of that happens using the official implementation (hackvm.py).
So, is my understanding of things wrong here, or is this an error in the Python implementation of the HVM? Or is the HVM working correctly, and it is rather the documentation which is erroneous?