Jump to content




some math functions


  • You cannot reply to this topic
8 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 10: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-

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 April 2013 - 10: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

Quote

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.

#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 10:20 AM

View Posttheoriginalbit, on 22 April 2013 - 10:17 AM, said:

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

Quote

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 ^_^

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 April 2013 - 10:24 AM

View PostFreack100, on 22 April 2013 - 10:20 AM, said:

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....

#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 10:34 AM

View Posttheoriginalbit, on 22 April 2013 - 10:24 AM, said:

View PostFreack100, on 22 April 2013 - 10:20 AM, said:

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?

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 April 2013 - 10:40 AM

View PostFreack100, on 22 April 2013 - 10:34 AM, said:

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.

#7 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 11:08 AM

Yea, em... I had a blackout :D
3^10 = 10*10*10 = 1000

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 22 April 2013 - 06:12 PM

Anything to the log of a number to the base of the same number is 1

#9 aaa

  • Members
  • 53 posts

Posted 17 July 2013 - 04:35 PM

View PostFreack100, on 22 April 2013 - 11:08 AM, said:

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.

View Posttheoriginalbit, on 22 April 2013 - 10:17 AM, said:

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

What they do: math.exp

Quote

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 :
Spoiler






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users