« Introduction au Lua/Fonctions Math » : différence entre les versions
Contenu supprimé Contenu ajouté
Correcting fmod and ldexp examples. |
|||
Ligne 1 :
{{:{{BASEPAGENAME}}/Sidebar}}
Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.<ref>http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual</ref> This lesson will show you how to use the Lua [[mw:Extension:Scribunto/Lua_reference_manual#Math_library|Math library]] in your scripts.
__TOC__
== Prerequisites ==
This lesson assumes you have already completed the [[Lua/Frame Object| Frame Object]] lesson.
# Accéder au [[Module:Sandbox]].▼
== Create a Lua Script that Uses the Math Library ==
To create a Lua script that uses the Math library:
# Clear all existing code.
#: It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
# Add the following code and save the page:
<blockquote><pre>
local p = {}
Ligne 144 :
local n = frame.args[2]
math.randomseed(os.time())
return ';randomseed
end
Ligne 173 :
return p
</pre></blockquote>
==
To test your Lua script:
# Navigate to either the [[Module_talk:Sandbox]] page, the [[Wikiversity:Sandbox]] page, or your own user or sandbox page.
# Add the following code and save the page:
<blockquote><pre>
{{#invoke:Sandbox|abs|-1}}
Ligne 190 ⟶ 191 :
{{#invoke:Sandbox|exp|1}}
{{#invoke:Sandbox|floor|1.5}}
{{#invoke:Sandbox|fmod|5|3}}
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
Ligne 211 ⟶ 212 :
</pre></blockquote>
The result should be similar to:
<blockquote>
;abs
Ligne 221 ⟶ 222 :
;atan
:math.atan(1) is 0.78539816339745
;atan2
:math.atan2(1, 1) is 0.78539816339745
;ceil
Ligne 235 ⟶ 236 :
;floor
:math.floor(1.5) is 1
;fmod
:math.fmod(5, 3) is
;frexp
:math.frexp(1) is 0.5
Ligne 242 ⟶ 243 :
:math.huge is inf
;ldexp
:math.
;log
:math.log( 2.718281828459) is 0.99999999999998
Ligne 256 ⟶ 257 :
:math.pi is 3.1415926535898
;pow
:math.pow(10, 2) is
;rad
:math.rad(45) is 0.78539816339745
;random
:math.random(1, 6) is
;randomseed
:math.random(1, 6) is
;sin
:math.sin(0.78539816339745) is 0.70710678118655
Ligne 275 ⟶ 276 :
</blockquote>
==
To understand your Lua script:
# <code>math.abs(x)</code>
# <code>math.acos(x)</code>
# <code>math.asin(x)</code>
# <code>math.atan(x)</code>
# <code>math.atan2(y, x)</code>
# <code>math.ceil(x)</code>
# <code>math.cos(x)</code>
# <code>math.cosh(x)</code>
# <code>math.deg(x)</code>
# <code>math.exp(x)</code>
# <code>math.floor(x)</code>
# <code>math.fmod(x, y)</code>
# <code>math.frexp(x)</code>
# <code>math.huge</code>
# <code>math.ldexp(m, e)</code>
# <code>math.log(x)</code>
# <code>math.log10(x)</code>
# <code>math.max(x, y)</code>
# <code>math.min(x, y)</code>
# <code>math.modf(x)</code>
# <code>math.pi</code>
# <code>math.pow(x, y)</code>
# <code>math.rad(x)</code>
# <code>math.random(m, n)</code>
#:Note that unless randomseed is called first, the random number sequence will be the same every time, meaning not random
# <code>math.randomseed(os.time())</code> seeds the random number generator with the current server operating system elapsed time in seconds
# <code>math.sin(x)</code>
# <code>math.sinh(x)</code>
# <code>math.sqrt(x)</code>
# <code>math.tan(x)</code>
# <code>math.tanh(x)</code>
== Conclusion ==
Congratulations! You've now created, tested, and understood a Lua script that uses the Math library. Return to the main [[Lua]] page to learn about other Lua code libraries.
== See Also ==
==
{{reflist}}
▲* [[../Objet Frame | Objet Frame]]
{{CourseCat}}
[[Category: Lessons]]
[[Category: Completed resources]]
[[fr:Initiation au Lua avec Scribunto/Fonctions mathématiques]]
|