This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
クデル's profile picture

Bit Shifting and Squaring Pi

Started by クデル, 06 July 2015 - 06:03 AM
クデル #1
Posted 06 July 2015 - 08:03 AM
I was playing around with ComputerCraft today, I am curious as to why the output in the first example is greater than the output of the second. Even though the Lua interpreter also claims both statements are false. Surely the output should be the same, or atleast a larger output from the second example.

Statements

math.pi^2 > math.pi^math.pi
2 > math.pi

Example 1

bit.blshift(97, math.pi^2) = 49664

Example 2

bit.blshift(97, math.pi^math.pi) = 1552
Edited on 06 July 2015 - 06:04 AM
Lyqyd #2
Posted 06 July 2015 - 08:32 AM
Both statements are false.

2 is less than pi, and pi squared is less than pi raised to the power of pi. And I've no idea what you're trying to do with your two examples, but perhaps they're confusing you because you don't understand that pi is larger than two?
クデル #3
Posted 06 July 2015 - 09:11 AM
Both statements are false.

2 is less than pi, and pi squared is less than pi raised to the power of pi. And I've no idea what you're trying to do with your two examples, but perhaps they're confusing you because you don't understand that pi is larger than two?

Statements were just trying to prove the authenticity of the examples, i understand that pi is larger than two, but why would example one result in a larger output when example two is squaring pi by a larger number?
Edited on 06 July 2015 - 07:28 AM
MKlegoman357 #4
Posted 06 July 2015 - 10:59 AM
(These are not facts, I'm no expert in binary and I may be wrong about this all)

Lua integers are 32-bit, thus you can only shift by that many bits. The number of bits to shift when passed to bit.blshift is changed a bit before the number is being shifted:


function bit.blshift (num, n)
  n = math.floor(n) % 32

  return leftShiftNumber(num, n)
end

pi ^ pi = ~36.4, thus:


n = math.floor(36.4) = 36
n = 36 % 32 = 4

So the number is actually being shifted by only 4 bits, rather than 36.

Now I don't know if this is the way bit shifting should work, but I do know that this is the way bit.blshift works. This might also be the case with other bit library functions.
クデル #5
Posted 06 July 2015 - 11:53 AM
Ohh, I didn't know being 32 bit had anything to do with it, thanks for explaining!