Function Dump/Mathematical Functions: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
cleaned up some examples
>NecroBumpist
Complete request on the ROBLOX Wiki Group wall to clear up the synonyms "arc" and "inverse" trigonometric functions.
 
(98 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{CatUp|Function Dump}}
{{Map|Function Dump}}
 
 
 
==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)'''


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


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


{{Example|
{{code and output|code=
<pre>
print( math.abs(-5) )  
for i = -10, 10, .1 do
|output=5}}
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,50+math.abs(i),0))
wait()
end
</pre>
}}


'''math.acos (x)'''
==math.acos(x)==
Returns the arc[[#Trigonometric Names|*]] cosine of x (in radians).


{{code and output|code=
print( math.acos(-1) )
|output=3.1415926535898}}


Returns the arc cosine of x (in radians).
==math.asin(x)==
Returns the arc[[#Trigonometric Names|*]] sine of x (in radians).


{{Example|
{{code and output|code=
<pre>
print( math.asin(0) )  
for i = -1, 1, .01 do
|output=0}}
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.acos(i)*10,0))
wait()
end
</pre>
}}
 
'''math.asin (x)'''


==math.atan(x)==
Returns the arc[[#Trigonometric Names|*]] tangent of x (in radians).


Returns the arc sine of x (in radians).
{{code and output|code=
 
print( math.atan(1) )  
{{Example|
|output = 0.78539816339745 (45 degrees)
<pre>
for i = -1, 1, .01 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.asin(i)*10,0))
wait()
end
</pre>
}}
}}


'''math.atan (x)'''
==math.atan2(y, x)==
Returns the arc[[#Trigonometric Names|*]] 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 a number rounded up (towards positive infinity) to the nearest integer.


Returns the arc tangent of x (in radians).
{{code and output|code=
 
print( math.ceil(4.2) )  
{{Example|
|output=5}}
<pre>
{{code and output|code=
for i = -5, 5, .1 do
print( math.ceil(-4.2) )
local p = Instance.new("Part")
|output=-4}}
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.atan(i)*10,0))
wait()
end
</pre>
}}
 
'''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.)
 
{{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)'''
 
 
Returns the smallest integer larger than or equal to x.
 
{{Example|
<pre>
for i = 0, 50, .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,math.ceil(i),0))
wait()
end
</pre>
}}
 
'''math.cos (x)'''
 


See also: [[#math.floor(x)|math.floor]].


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


{{Example|
{{code and output|code=
<pre>
print( math.cos(1) )  
for i = 0, 50, .1 do
|output=0.54030230586814}}
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(2*math.cos(i),i,0))
wait()
end
</pre>
}}
 
'''math.cosh (x)'''
 


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


{{Example|
{{code and output|code=
<pre>
print( math.cosh(1) )  
for i = 0, 5, .1 do
|output=1.5430806348152}}
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,2*math.cosh(i),0))
wait()
end
</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|
{{code and output|code=
<pre>
print( math.deg(math.pi / 2) )  
a=math.deg (1.5707963267948966192313216916398)
|output=90}}
print(a)


Will result in:
==math.exp(x)==
90
Returns the the value e^x.
</pre>
}}
 
'''math.exp (x)'''


{{code and output|code=
print( math.exp(1) )
|output=2.718281828459}}


Returns the the value e^x.
==math.floor(x)==
Returns a number rounded down (towards negative infinity) to the nearest integer.


{{Example|
{{code and output|code=
<pre>
print( math.floor(4.8) )  
for i = -5, 5, .1 do
|output=4
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,50+math.exp(i),0))
wait()
end
</pre>
}}
}}
 
{{code and output|code=
'''math.floor (x)'''
print( math.floor(-4.8) )  
 
|output=-5
 
Returns the largest integer smaller than or equal to x.
 
{{Example|
<pre>
for i = 0, 50, .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,math.floor(i),0))
wait()
end
</pre>
}}
}}


'''math.fmod (x, y)'''
See also: [[#math.ceil(x)|math.ceil]].
 


==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|
{{code and output|code=
<pre>
print( math.fmod(10, 3) )  
for i = -10, 10, 1 do
|output=1}}
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,50+math.fmod(i,2),0))
wait()
end
</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).


{{Example|
{{code and output|code=
<pre>
print( math.frexp(0) )  
print(math.frexp (0))
|output = 0 0}}
 
Will result in:
0 0
</pre>
<pre>
print(math.frexp (4))


Will result in:
{{code and output|code=
0.5 3 -- (2^3/2=4)
print( math.frexp(4) )
</pre>
|output = 0.5 3 (2^3/2 == 4)}}
}}


'''math.huge'''
==math.huge==
 
A representation of infinity, 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.
 
{{Example|
<pre>
print(math.huge)
 
Will result in:
1.#INF
</pre>
}}
 
'''math.ldexp (m, e)'''


{{code and output|code=
print( math.huge )
|output=1.#INF}}


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


{{Example|
{{code and output|code=
<pre>
print( math.ldexp(2, 6) )  
for i = -10, 10, 1 do
|output=128 (i.e., 2*(2^6))}}
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,50+math.ldexp (i, 1),0))
wait()
end
</pre>
}}
 
 
'''math.log (x)'''




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


{{Example|
{{code and output|code=
<pre>
print( math.log(2.718281828459045) )  
for i = 0, 30, 1 do
|output=1}}
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,50+5*math.log (i),0))
wait()
end
</pre>
}}
 
'''math.log10 (x)'''
 


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


{{Example|
{{code and output|code=
<pre>
print( math.log10(100) )  
for i = 0, 30, 1 do
|output=2}}
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,50+5*math.log10 (i),0))
wait()
end
</pre>
}}
 
'''math.max (x, ···)'''
 


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


{{Example|
{{code and output|code=
<pre>
print( math.max(1, 2, 3, 4, 5, 6, 7) )  
print(math.max (1, 2, 3, 4, 5, 6, 7))
|output=7}}
Will result in:
7
</pre>
}}
 
'''math.min (x, ···)'''
 


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


{{Example|
{{code and output|code=
<pre>
print( math.min(1, 2, 3, 4, 5, 6, 7) )  
print(math.min (1, 2, 3, 4, 5, 6, 7))
|output=1}}


Will result in:
==math.modf(x)==
1
Returns two numbers, the integral part of x and the fractional part of x.
</pre>
}}


'''math.modf (x)'''
{{code and output|code=
print( math.modf(2.5) )  
|output=2 0.5}}


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


Returns two numbers, the integral part of x and the fractional part of x.
{{code and output|code=
print( math.pi )
|output=3.1415926535898}}


{{Example|
==math.pow(x, y)==
<pre>
Returns x^y. (You can also use the expression x^y to compute this value.)
print(math.modf (2.5))
 
Will result in:
2 0.5
</pre>
}}


'''math.pi'''
{{code and output|code=
print( math.pow(4, 2) )
|output=16}}


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


The value of pi.
{{code and output|code=
print( math.rad(90) )
|output=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|
{{Example|
<pre>
<pre>
a=(math.pi^2)
local str = ""
print(math.sqrt(a))
for i = 1, 10 do
 
  local num = math.random(33, 126)
Will result in:
  str = str .. string.char(num)
3.1415926535898
end
print(str)  -- random string length 10
</pre>
</pre>
}}
}}
 
math.random() -- returns a [[Number]] value 0-1
'''math.pow (x, y)'''
 
 
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
local color = Color3.new(math.random(), math.random(), math.random())
local p = Instance.new("Part")
print(color) -- random color3
p.Parent = game.Workspace
p.Size = Vector3.new(1,1,1)
p.Anchored = true
p.CFrame=(CFrame.fromEulerAnglesXYZ(0,0,0)+Vector3.new(i+50,math.pow(i,2),0))
wait()
end
</pre>
</pre>
}}
}}
 
math.random(''max'') -- returns an [[Integer]] 1-''max''
'''math.rad (x)'''
 
 
Returns the angle x (given in degrees) in radians.
 
{{Example|
{{Example|
<pre>
<pre>
print(math.rad (90))
local list = Workspace:GetChildren()
 
print( list[math.random(#list)] ) -- random item from list
Will result in:
1.5707963267949 (Which is pi/2)
</pre>
</pre>
}}
'''math.random ([m [, n]])'''
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.)
When called without arguments, returns a pseudo-random real number in the range [0,1). When called with a number m, math.random returns a pseudo-random integer in the range [1, m]. When called with two numbers m and n, math.random returns a pseudo-random integer in the range [m, n].
{{Example|
<pre>
<pre>
local str = ""
local a = math.random(5)
for i = 1,10 do
local num = math.random(33,126)
str = str .. string.char(num)
end
 
print(str)
</pre>
</pre>
The first example returns a random item from list, while the second returns an integer between one and five.
}}
}}


'''math.randomseed (x)'''
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>


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.
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|
{{code and output|code=
<pre>
print( math.sin(math.pi / 2) )  
for i = 0, 20, .1 do
|output=1}}
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(3*math.sin(i),3*i,3*math.cos(i)))
wait()
end
</pre>
}}
 
'''math.sinh (x)'''
 


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


{{Example|
{{code and output|code=
<pre>
print( math.sinh(0) )  
for i = -2, 2, .1 do
|output=0}}
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(2*math.sinh(i),5*i+50,2*math.cosh(i)))
wait()
end
</pre>
}}
 
'''math.sqrt (x)'''
 
 
Returns the square root of x. (You can also use the expression x^0.5 to compute this value.)
 
{{Example|
<pre>
for i = 0, 30, 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,50+5*math.sqrt (i),0))
wait()
end
</pre>
}}
 
'''math.tan (x)'''


==math.sqrt(x)==
Returns the square root of x. You can also use the expression x^0.5 to compute this value, but it is less efficient.


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


{{Example|
{{code and output|code=
<pre>
print( math.tan(1) )  
for i = 0, 50, .1 do
|output=1.5574077246549}}
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,50+2*math.tan(i),0))
wait()
end
</pre>
}}


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


{{code and output|code=
print( math.tanh(1) )
|output=0.76159415595576}}


Returns the hyperbolic tangent of x.
== Trigonometric Names ==
 
The inverse trigonometric functions (i.e. [[#math.acos(x)|math.acos()]], [[#math.asin(x)|math.asin()]], [[#math.atan(x)|math.atan()]], and [[#math.atan2(y, x)|math.atan2()]]) can be referred to as the "arc" or "inverse" version of their corresponding original function.
{{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,50+2*math.tanh(i),0))
wait()
end
</pre>
}}


----
This means if someone says "inverse cosine", they are referring to the [[#math.acos(x)|arc cosine]] function, and vice-versa.

Latest revision as of 15:23, 14 April 2012

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

math.abs(x)

Returns the absolute value of x.

print( math.abs(-5) )
5

math.acos(x)

Returns the arc* cosine of x (in radians).

print( math.acos(-1) )
3.1415926535898

math.asin(x)

Returns the arc* sine of x (in radians).

print( math.asin(0) )
0

math.atan(x)

Returns the arc* tangent of x (in radians).

print( math.atan(1) )
0.78539816339745 (45 degrees)

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 a number rounded up (towards positive infinity) to the nearest integer.

print( math.ceil(4.2) )
5
print( math.ceil(-4.2) )
-4

See also: math.floor.

math.cos(x)

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

print( math.cos(1) )
0.54030230586814

math.cosh(x)

Returns the hyperbolic cosine of x.

print( math.cosh(1) )
1.5430806348152

math.deg(x)

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

print( math.deg(math.pi / 2) )
90

math.exp(x)

Returns the the value e^x.

print( math.exp(1) )
2.718281828459

math.floor(x)

Returns a number rounded down (towards negative infinity) to the nearest integer.

print( math.floor(4.8) )
4
print( math.floor(-4.8) )
-5

See also: math.ceil.

math.fmod(x, y)

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

print( math.fmod(10, 3) )
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).

print( math.frexp(0) )
0 0
print( math.frexp(4) )
0.5 3 (2^3/2 == 4)

math.huge

A representation of infinity, a value larger than or equal to any other numerical value.

print( math.huge )
1.#INF

math.ldexp(m, e)

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

print( math.ldexp(2, 6) )
128 (i.e., 2*(2^6))


math.log(x)

Returns the natural logarithm of x.

print( math.log(2.718281828459045) )
1

math.log10(x)

Returns the base-10 logarithm of x.

print( math.log10(100) )
2

math.max(x, ···)

Returns the maximum value among its arguments.

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

math.min(x, ···)

Returns the minimum value among its arguments.

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

math.modf(x)

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

print( math.modf(2.5) )
2 0.5

math.pi

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

print( math.pi )
3.1415926535898

math.pow(x, y)

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

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

math.rad(x)

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

print( math.rad(90) )
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
local a = math.random(5)

The first example returns a random item from list, while the second returns an integer between one and five.


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

print( math.sin(math.pi / 2) )
1

math.sinh(x)

Returns the hyperbolic sine of x.

print( math.sinh(0) )
0

math.sqrt(x)

Returns the square root of x. You can also use the expression x^0.5 to compute this value, but it is less efficient.

math.tan(x)

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

print( math.tan(1) )
1.5574077246549

math.tanh(x)

Returns the hyperbolic tangent of x.

print( math.tanh(1) )
0.76159415595576

Trigonometric Names

The inverse trigonometric functions (i.e. math.acos(), math.asin(), math.atan(), and math.atan2()) can be referred to as the "arc" or "inverse" version of their corresponding original function.

This means if someone says "inverse cosine", they are referring to the arc cosine function, and vice-versa.