Jump to content




Bit Shifting and Squaring Pi


4 replies to this topic

#1 クデル

  • Members
  • 349 posts

Posted 06 July 2015 - 07: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 by Ice Cream, 06 July 2015 - 07:04 AM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 06 July 2015 - 07: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 クデル

  • Members
  • 349 posts

Posted 06 July 2015 - 08:11 AM

View PostLyqyd, on 06 July 2015 - 07:32 AM, said:

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 by Ice Cream, 06 July 2015 - 08:28 AM.


#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 06 July 2015 - 09: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 クデル

  • Members
  • 349 posts

Posted 06 July 2015 - 10:53 AM

Ohh, I didn't know being 32 bit had anything to do with it, thanks for explaining!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users