Function Dump/Mathematical Functions: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
→‎math.tanh (x): lrn2CFrame
>Anaminus
reverting examples
Line 1: Line 1:
{{CatUp|Function Dump}}
{{CatUp|Function Dump}}


=Mathematical Functions=
==Mathematical Functions==


This library is an interface to the standard C math library. It provides all its functions inside the table math.
This library is an interface to the standard C math library. It provides all its functions inside the table math.


===math.abs (x)===
===math.abs (x)===
Returns the absolute value of x.
Returns the absolute value of x.


{{Example|1=<!--why does it require this???-->
{{Example|
<span style="color:blue">''Try me with [[Edit#Getting_into_Edit_Mode|Edit Mode]]!''</span>
<pre>
<pre>
for i = -10, 10, .1 do
print(math.abs(-5))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
5
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+math.abs(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.acos (x)===
===math.acos (x)===
Returns the arc cosine of x (in radians).
Returns the arc cosine of x (in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = -1, 1, .01 do
print(math.acos(-1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
3.1415926535898
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i*10,50+math.acos(i)*10,0))
wait()
end
</pre>
</pre>
}}
}}


===math.asin (x)===
===math.asin (x)===
Returns the arc sine of x (in radians).
Returns the arc sine of x (in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = -1, 1, .01 do
print(math.asin(0))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
0
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i*10,50+math.asin(i)*10,0))
wait()
end
</pre>
</pre>
}}
}}


===math.atan (x)===
===math.atan (x)===
Returns the arc tangent of x (in radians).
Returns the arc tangent of x (in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = -5, 5, .1 do
print(math.atan(math.pi))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1.2626272556789
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i*10,50+math.atan(i)*10,0))
wait()
end
</pre>
</pre>
}}
}}


===math.atan2 (y, x)===
===math.atan2 (y, x)===
Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)
Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)
{{Example|
<pre>
for i = -5, 5, .1 do
local p = Instance.new("Part")
p.Parent = game.Workspace
p.Size = Vector3.new(1,1,1)
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i*10,50+math.atan2(1,i)*10,0))
wait()
end
</pre>
}}


===math.ceil (x)===
===math.ceil (x)===
Returns the smallest integer larger than or equal to x. Essentially, rounds a number to the next highest value
Returns the smallest integer larger than or equal to x. Essentially, rounds a number to the next highest value


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 50, .1 do
z=math.ceil (4.2)
local p = Instance.new("Part")
print(z)
p.Parent = game.Workspace
 
p.Size = Vector3.new(1,1,1)
Will result in:
p.Anchored = true
5
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,math.ceil(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.cos (x)===
===math.cos (x)===
Returns the cosine of x (assumed to be in radians).
Returns the cosine of x (assumed to be in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 50, .1 do
print(math.cos (1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
0.54030230586814
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(2*math.cos(i),i,0))
wait()
end
</pre>
</pre>
}}
}}


===math.cosh (x)===
===math.cosh (x)===
Returns the hyperbolic cosine of x.
Returns the hyperbolic cosine of x.


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 5, .1 do
print(math.cosh (1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1.5430806348152
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,2*math.cosh(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.deg (x)===
===math.deg (x)===
Returns the angle x (given in radians) in degrees.
Returns the angle x (given in radians) in degrees.


{{Example|
{{Example|
<pre>
<pre>
a=math.deg (1.5707963267948966192313216916398)
print(math.deg (1.5707963267948966192313216916398))
print(a)


Will result in:
Will result in:
Line 169: Line 106:


===math.exp (x)===
===math.exp (x)===
Returns the the value e^x.
Returns the the value e^x.


{{Example|
{{Example|
<pre>
<pre>
for i = -5, 5, .1 do
print(math.exp (1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
2.718281828459
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+math.exp(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.floor (x)===
===math.floor (x)===
Returns the largest integer smaller than or equal to x.
Returns the largest integer smaller than or equal to x.


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 50, .1 do
z=math.ceil (4.2)
local p = Instance.new("Part")
print(z)
p.Parent = game.Workspace
 
p.Size = Vector3.new(1,1,1)
Will result in:
p.Anchored = true
4
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,math.floor(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.fmod (x, y)===
===math.fmod (x, y)===
Returns the remainder of the division of x by y that rounds the quotient towards zero.
Returns the remainder of the division of x by y that rounds the quotient towards zero.


{{Example|
{{Example|
<pre>
<pre>
for i = -10, 10, 1 do
print(math.fmod (10, 3))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+math.fmod(i,2),0))
wait()
end
</pre>
</pre>
}}
}}


===math.frexp (x)===
===math.frexp (x)===
Returns m and e such that x = m*2^e, e is an integer and the absolute value of m is in the range [0.5, 1) (or zero when x is zero).
Returns m and e such that x = m*2^e, e is an integer and the absolute value of m is in the range [0.5, 1) (or zero when x is zero).


Line 243: Line 161:


===math.huge===
===math.huge===
The value HUGE_VAL, a value larger than or equal to any other numerical value.
The value HUGE_VAL, a value larger than or equal to any other numerical value.


Line 257: Line 173:


===math.ldexp (m, e)===
===math.ldexp (m, e)===
Returns m*2^e (e should be an integer).
Returns m*2^e (e should be an integer).


{{Example|
{{Example|
<pre>
<pre>
for i = -10, 10, 1 do
print(math.ldexp (2, 6))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
128 (i.e., (2*(2^6))
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+math.ldexp (i, 1),0))
wait()
end
</pre>
</pre>
}}
}}
Line 276: Line 186:


===math.log (x)===
===math.log (x)===
Returns the natural logarithm of x.
Returns the natural logarithm of x.


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 30, 1 do
print(math.log (2.71828182845904523536))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+5*math.log (i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.log10 (x)===
===math.log10 (x)===
Returns the base-10 logarithm of x.
Returns the base-10 logarithm of x.


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 30, 1 do
print(math.log10 (100))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
2
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+5*math.log10 (i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.max (x, ···)===
===math.max (x, ···)===
Returns the maximum value among its arguments.
Returns the maximum value among its arguments.


Line 325: Line 221:


===math.min (x, ···)===
===math.min (x, ···)===
Returns the minimum value among its arguments.
Returns the minimum value among its arguments.


Line 339: Line 233:


===math.modf (x)===
===math.modf (x)===
Returns two numbers, the integral part of x and the fractional part of x.
Returns two numbers, the integral part of x and the fractional part of x.


Line 353: Line 245:


===math.pi===
===math.pi===
The value of pi. Pi is a mathematics term (not the baked good) that represents a very specific number.
The value of pi. Pi is a mathematics term (not the baked good) that represents a very specific number.


{{Example|
{{Example|
<pre>
<pre>
a=(math.pi^2)
print(math.pi)
print(math.sqrt(a))


Will result in:
Will result in:
Line 368: Line 257:


===math.pow (x, y)===
===math.pow (x, y)===
Returns x^y. (You can also use the expression x^y to compute this value.)
Returns x^y. (You can also use the expression x^y to compute this value.)


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 10, .1 do
print(math.pow (4, 2))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
16
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i+50,math.pow(i,2),0))
wait()
end
</pre>
</pre>
}}
}}


===math.rad (x)===
===math.rad (x)===
Returns the angle x (given in degrees) in radians.
Returns the angle x (given in degrees) in radians.


Line 400: Line 281:


===math.random ([m [, n]])===
===math.random ([m [, n]])===
This function can be called 3 ways:
This function can be called 3 ways:
  math.random(''min'',''max'') -- returns an [[Integer]] ''min''-''max''
  math.random(''min'',''max'') -- returns an [[Integer]] ''min''-''max''
Line 428: Line 308:
}}
}}


If second number is less than first (or only number is less than 1), you'll get:
If the second number is less than first (or only number is less than 1), you'll get:
  <font style="color:red">bad argument #n to 'random' (interval is empty)</font>
  <font style="color:red">bad argument #n to 'random' (interval is empty)</font>


Line 434: Line 314:


===math.randomseed (x)===
===math.randomseed (x)===
Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.
Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.


===math.sin (x)===
===math.sin (x)===
Returns the sine of x (assumed to be in radians).
Returns the sine of x (assumed to be in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 20, .1 do
print(math.sin (1.5707963267948966192313216916398))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(3*math.sin(i),3*i,3*math.cos(i)))
wait()
end
</pre>
</pre>
}}
}}


===math.sinh (x)===
===math.sinh (x)===
Returns the hyperbolic sine of x.
Returns the hyperbolic sine of x.


{{Example|
{{Example|
<pre>
<pre>
for i = -2, 2, .1 do
print(math.sinh (0))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
0
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(2*math.sinh(i),5*i+50,2*math.cosh(i)))
wait()
end
</pre>
</pre>
}}
}}


===math.sqrt (x)===
===math.sqrt (x)===
Returns the square root of x. (You can also use the expression x^0.5 to compute this value.)
Returns the square root of x. (You can also use the expression x^0.5 to compute this value.)


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 30, 1 do
z=math.sqrt (16)
local p = Instance.new("Part")
print(z)
p.Parent = game.Workspace
 
p.Size = Vector3.new(1,1,1)
Will result in:
p.Anchored = true
4
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+5*math.sqrt (i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.tan (x)===
===math.tan (x)===
Returns the tangent of x (assumed to be in radians).
Returns the tangent of x (assumed to be in radians).


{{Example|
{{Example|
<pre>
<pre>
for i = 0, 50, .1 do
print(math.tan (1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
1.5574077246549
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i,50+2*math.tan(i),0))
wait()
end
</pre>
</pre>
}}
}}


===math.tanh (x)===
===math.tanh (x)===
Returns the hyperbolic tangent of x.  
Returns the hyperbolic tangent of x.  


{{Example|
{{Example|
<pre>
<pre>
for i = -5, 5, .1 do
print(math.tanh (1))
local p = Instance.new("Part")
 
p.Parent = game.Workspace
Will result in:
p.Size = Vector3.new(1,1,1)
0.76159415595576
p.Anchored = true
p.CFrame = CFrame.new(i, 50 + 2*math.tanh(i), 0)
wait()
end
</pre>
</pre>
}}
}}

Revision as of 10:58, 2 April 2011

Mathematical Functions

This library is an interface to the standard C math library. It provides all its functions inside the table math.

math.abs (x)

Returns the absolute value of x.

Example
print(math.abs(-5))

Will result in:
5


math.acos (x)

Returns the arc cosine of x (in radians).

Example
print(math.acos(-1))

Will result in:
3.1415926535898


math.asin (x)

Returns the arc sine of x (in radians).

Example
print(math.asin(0))

Will result in:
0


math.atan (x)

Returns the arc tangent of x (in radians).

Example
print(math.atan(math.pi))

Will result in:
1.2626272556789


math.atan2 (y, x)

Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)

math.ceil (x)

Returns the smallest integer larger than or equal to x. Essentially, rounds a number to the next highest value

Example
z=math.ceil (4.2)
print(z)

Will result in:
5


math.cos (x)

Returns the cosine of x (assumed to be in radians).

Example
print(math.cos (1))

Will result in:
0.54030230586814


math.cosh (x)

Returns the hyperbolic cosine of x.

Example
print(math.cosh (1))

Will result in:
1.5430806348152


math.deg (x)

Returns the angle x (given in radians) in degrees.

Example
print(math.deg (1.5707963267948966192313216916398))

Will result in:
90


math.exp (x)

Returns the the value e^x.

Example
print(math.exp (1))

Will result in:
2.718281828459


math.floor (x)

Returns the largest integer smaller than or equal to x.

Example
z=math.ceil (4.2)
print(z)

Will result in:
4


math.fmod (x, y)

Returns the remainder of the division of x by y that rounds the quotient towards zero.

Example
print(math.fmod (10, 3))

Will result in:
1


math.frexp (x)

Returns m and e such that x = m*2^e, e is an integer and the absolute value of m is in the range [0.5, 1) (or zero when x is zero).

Example
print(math.frexp (0))

Will result in:
0 0
print(math.frexp (4))

Will result in:
0.5 3 -- (2^3/2=4)


math.huge

The value HUGE_VAL, a value larger than or equal to any other numerical value.

Example
print(math.huge)

Will result in:
1.#INF


math.ldexp (m, e)

Returns m*2^e (e should be an integer).

Example
print(math.ldexp (2, 6))

Will result in:
128 (i.e., (2*(2^6))


math.log (x)

Returns the natural logarithm of x.

Example
print(math.log (2.71828182845904523536))

Will result in:
1


math.log10 (x)

Returns the base-10 logarithm of x.

Example
print(math.log10 (100))

Will result in:
2


math.max (x, ···)

Returns the maximum value among its arguments.

Example
print(math.max (1, 2, 3, 4, 5, 6, 7))
Will result in:
7


math.min (x, ···)

Returns the minimum value among its arguments.

Example
print(math.min (1, 2, 3, 4, 5, 6, 7))

Will result in:
1


math.modf (x)

Returns two numbers, the integral part of x and the fractional part of x.

Example
print(math.modf (2.5))

Will result in:
2 0.5


math.pi

The value of pi. Pi is a mathematics term (not the baked good) that represents a very specific number.

Example
print(math.pi)

Will result in:
3.1415926535898


math.pow (x, y)

Returns x^y. (You can also use the expression x^y to compute this value.)

Example
print(math.pow (4, 2))

Will result in:
16


math.rad (x)

Returns the angle x (given in degrees) in radians.

Example
print(math.rad (90))

Will result in:
1.5707963267949 (Which is pi/2)


math.random ([m [, n]])

This function can be called 3 ways:

math.random(min,max) -- returns an Integer min-max
Example
local str = ""
for i = 1,10 do
  local num = math.random(33,126)
  str = str .. string.char(num)
end
print(str) -- random string length 10
math.random() -- returns a Number value 0-1
Example
local color = Color3.new( math.random(), math.random(), math.random() )
print(color) -- random color3
math.random(max) -- returns an Integer 1-max
Example
local list = Workspace:GetChildren()
print( list[math.random(#list)] ) -- random item from list


If the second number is less than first (or only number is less than 1), you'll get:

bad argument #n to 'random' (interval is empty)

This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.)

math.randomseed (x)

Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.

math.sin (x)

Returns the sine of x (assumed to be in radians).

Example
print(math.sin (1.5707963267948966192313216916398))

Will result in:
1


math.sinh (x)

Returns the hyperbolic sine of x.

Example
print(math.sinh (0))

Will result in:
0


math.sqrt (x)

Returns the square root of x. (You can also use the expression x^0.5 to compute this value.)

Example
z=math.sqrt (16)
print(z)

Will result in:
4


math.tan (x)

Returns the tangent of x (assumed to be in radians).

Example
print(math.tan (1))

Will result in:
1.5574077246549


math.tanh (x)

Returns the hyperbolic tangent of x.

Example
print(math.tanh (1))

Will result in:
0.76159415595576