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

here's why math is fun... to mess with.

Started by Waitdev_, 07 August 2015 - 11:47 PM
Waitdev_ #1
Posted 08 August 2015 - 01:47 AM
messing with java and computercraft is fun. for example, try these things

>lua
welcum to lua
now u do magic with lua
lua>print(tostring(2^15))
yeah, that is normal.
lua>print(tostring(2^127))
that would be a big number. now for the fun part.
lua>print(tostring(2^128))
what does it return?
Infinity0
yeah, that's something fun to do. now for something confusing.

a = 1
for i = 1,127 do
a = a/2
print(a)
sleep(0.1)
end
print(a)
that's a fun one. now try this.

a = 1
while not a==0 do
a = a/2
print(a)
sleep(0.1)
end
print(a)
why does that return 1?
just how fun it is.
got any cool things to try? leave a reply with what it is.
H4X0RZ #2
Posted 08 August 2015 - 03:37 AM
The first one is "normal" behaviour. The number you tostring is just so big that is comes out as infinite and the 0 is added because of the way print and tostring works. It looks like tostring returns 2 things. A string and a number (i have no idea what the number does). Print concatenates every argument you give it with "" so the 0 (second argument) is sticked to the end of the string (first argument). :P/>
Waitdev_ #3
Posted 08 August 2015 - 03:56 AM
The first one is "normal" behaviour. The number you tostring is just so big that is comes out as infinite and the 0 is added because of the way print and tostring works. It looks like tostring returns 2 things. A string and a number (i have no idea what the number does). Print concatenates every argument you give it with "" so the 0 (second argument) is sticked to the end of the string (first argument). :P/>
nice explaination, and i also have a theory.
Java uses tones of number variable types, and if they are really big (don't say that's what she said) then it can break the code.
the biggest i know (oh, don't even think about it) is a long, which is 2^63.
it is like 128, but the max is always 2^(num-1) so the max is 2^127.
this is why when you show all 16 bit colors you do 2^1 - 2^15

yeah, that totally made sense.
nitrogenfingers #4
Posted 19 August 2015 - 08:12 AM
It's returning 1 on your third test because you're failing to account for operator precedence. not evaluates before ==, so your statement is checking to see if false == 0, which of course it isn't. A better way to write that statement is:

while a ~= 0 do
Mathematically speaking the loop should be infinite because the series of rational numbers is theoretically infinite, but the loop will terminate (at around 1/2^1075 in my tests) because computers have finite precision.This is also why you should never compare floating point numbers for equality in this manner.

Here's a bit of code to demonstrate why it's bad practice:

a = 11111111113
b = -11111111111
c = 7.51111111111
print(a + (b + c))
print((a + b ) + c)
if (a + b ) + c == a + (b + c) then
  print("FP arithmetic is associative")
else
  print("FP arithmetic is not associative")
  print(math.abs(((a + b ) + c) - (a + (b + c))))
end

Also, while longs and doubles are the largest primitive number types, remember Java also has BigInteger and BigDecimal, which are arbitrary precision.

Edit: Don't capitalize the B's. I have no idea why the forum editor is doing that.
Edited on 19 August 2015 - 06:18 AM
Bomb Bloke #5
Posted 19 August 2015 - 08:32 AM
Edit: Don't capitalize the B's. I have no idea why the forum editor is doing that.

Because you're using the code for emoticons. Disabling them via the full editor sorts it out.
Exerro #6
Posted 19 August 2015 - 08:40 AM

a = 11111111113
b = -11111111111
c = 7.51111111111
print(a + (b + c))
print((a + b ) + c)
if (a + b ) + c == a + (b + c) then
  print("FP arithmetic is associative")
else
  print("FP arithmetic is not associative")
  print(math.abs(((a + b ) + c) - (a + (b + c))))
end

That's pretty interesting (spoiler: it prints out "not associative"). I've been flummoxed before when my various math related things were evaluating things incorrectly, even when I was printing out the values beforehand, doing the math myself, and getting a correct answer. I've always defaulted to using a function like this to try and avoid the issue:

function roughlyEqualTo( a, b )
     return math.abs( a - b ) < .0001
end

…but I never thought it could be a difference between the order in which things were added! That's crazy.