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

some math functions

Started by H4X0RZ, 22 April 2013 - 09:09 AM
H4X0RZ #1
Posted 22 April 2013 - 11:09 AM
Hi all,
I've found some math functions and I need some help with them :)/>/>

the functions:

math.floor()
math.randomseed()
math.ceil()
math.ldexp() (I read that this function normalizes the number )
math.exp()

what I want to know:

-What they do
-Where can I use them

I hope anybody helps me :)/>/>

thx for reading
-Freack100-
theoriginalbit #2
Posted 22 April 2013 - 11:17 AM
Google and Developer Documentation are your friends … http://lua-users.org...LibraryTutorial … I do suggest reading the PIL


What they do: math.floor rounds the number down so 3.1 is 3 and 3.9 is also 3
Where you can use: use this when you need to use a whole number somewhere and you feel that it may not be a whole number after calculations

What they do: math.ceil rounds the number up so 3.9 is 4 and 3.1 is 4
Where you can use: use this when you need to use a whole number somewhere and you feel that it may not be a whole number after calculations

What they do: math.randomseed sets a seed for the math.random function… there is no such thing as truly random, anything random is calculated one way or another…
Where you can use: not many places tbh

What they do: math.ldexp won't work in Lua, it will always error
Where you can use: never

What they do: math.exp
math.exp(myval) returns e (the base of natural logarithms) raised to the power myval. math.log() returns the inverse of this. math.exp(1) returns e.
Where you can use: not sure.
H4X0RZ #3
Posted 22 April 2013 - 11:20 AM
Google and Developer Documentation are your friends … http://lua-users.org...LibraryTutorial … I do suggest reading the PIL


What they do: math.floor rounds the number down so 3.1 is 3 and 3.9 is also 3
Where you can use: use this when you need to use a whole number somewhere and you feel that it may not be a whole number after calculations

What they do: math.ceil rounds the number up so 3.9 is 4 and 3.1 is 4
Where you can use: use this when you need to use a whole number somewhere and you feel that it may not be a whole number after calculations

What they do: math.randomseed sets a seed for the math.random function… there is no such thing as truly random, anything random is calculated one way or another…
Where you can use: not many places tbh

What they do: math.ldexp won't work in Lua, it will always error
Where you can use: never

What they do: math.exp
math.exp(myval) returns e (the base of natural logarithms) raised to the power myval. math.log() returns the inverse of this. math.exp(1) returns e.
Where you can use: not sure.
Thx…
But now I have to learn what an Logarithmus is ^_^/>
theoriginalbit #4
Posted 22 April 2013 - 11:24 AM
But now I have to learn what an Logarithmus is ^_^/>
Only if you REALLY want to… I don't see you ever needing to use math.exp and math.log they aren't common functions used in most programs let alone CC programs… I remember learning logarithms when I was ~14, and I have only used the knowledge once in the years since, which in the end just googled for the code for the function and got it….
H4X0RZ #5
Posted 22 April 2013 - 11:34 AM
But now I have to learn what an Logarithmus is ^_^/>/>
Only if you REALLY want to… I don't see you ever needing to use math.exp and math.log they aren't common functions used in most programs let alone CC programs… I remember learning logarithms when I was ~14, and I have only used the knowledge once in the years since, which in the end just googled for the code for the function and got it….
A few moments ago I read something about the logarithmus: "It's the reversal of a potentiation", right?
So, when 10^3=30 then is the logarithmus (with the base ten) = 3?
theoriginalbit #6
Posted 22 April 2013 - 11:40 AM
So, when 10^3=30 then is the logarithmus (with the base ten) = 3?
Well 10^3 is 1000 not 30 … 10*3 is 30 … but yes, the log base-10 of 10^3 is 3… so log base-4 of 4^16 is 16…
and that is the only use I have ever had for it, making cc colours from the binary scale (0-32768) to hex (0-15) which I used in a colour compression method.
H4X0RZ #7
Posted 22 April 2013 - 12:08 PM
Yea, em… I had a blackout :D/>
3^10 = 10*10*10 = 1000
remiX #8
Posted 22 April 2013 - 07:12 PM
Anything to the log of a number to the base of the same number is 1
aaa #9
Posted 17 July 2013 - 05:35 PM
Yea, em… I had a blackout :D/>/>/>/>/> 3^10 = 10*10*10 = 1000

Ahem…
3^10 = 3*3*3*3*3*3*3*3*3*3 = 59049
In math, we say that the exponentiation is not commutative.

What they do: math.ldexp won't work in Lua, it will always error
Where you can use: never

What they do: math.exp
math.exp(myval) returns e (the base of natural logarithms) raised to the power myval. math.log() returns the inverse of this. math.exp(1) returns e.
Where you can use: not sure.

Though I have not yet either saw any of them in CC user program, nor used them myself in lua, ldexp might be more usefull as exp, and is surely more understandable.
Imagine you made/download a self replicating program, that, on a mining Turtle, let him replicate every 24 hours. If you run it, after one day, you will have 2 turtles. The second day, they will both replicate, so you will have 4 turtles. Then 8, 16 and so on.
If you began with 3 turtles, after a month, you will have 3 * 2^30 turtles, which is a lot…
And 3 * 2^30 is math.ldexp ( 3, 30)

Computers think binary, that's why I think it can be more useful than the natural exponential, which rather is for mathematicians. I think one reason for it does not exist in Computercraft is that it does exactly the same thing as bit.blshift and bit.blogic_rshift. Can by used for encoding/decoding data.

back to my dumbness :
SpoilerIf we take on account the lag, ours turtles will quick replicate very slower, and be limited by the number of (real) operations needed to replicate, so will grow linearly.
Suppose now that our (real) computer is infinitely fast, we will be limited by memory.
Suppose we have infinite memory, we will suffer of quadratic growth, due to the 2D nature of minecraft (well, it's thick, but quite thin considering its length).
It would be interesting to know whether we reach this limit before limitation of minecraft itself. Indeed, the world is finite (2*30.000.000 by 2*30.000.000 by 256), but it would take 34 days to an human to reach its edges, and a turtle is quite slower, and suffers from 1-Norm : a human takes sqrt(2) more times to go to the corner than to go to the edges, a turtle takes twice that time.
If we suppose exponential growth, the whole world would have been filled with turtles in less than 60 days (log2(2*30.000.000 *2*30.000.000 *256)), and less than 37 days if you consider there are 6 diamonds a chunk, ignoring the time for travelling…