« Introduction au Lua/Fonctions Math » : différence entre les versions
Contenu supprimé Contenu ajouté
Aucun résumé des modifications |
m 18 révisions importées depuis en:Lua/Math_Library : Pour traduction en français |
||
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__
==
This lesson assumes you have already completed the [[Lua/Frame Object| Frame Object]] lesson.
== 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.
▲# Accéder au [[Module:Sandbox]].
# Add the following code and save the page:
<blockquote><pre>
local p = {}
Ligne 149 ⟶ 144 :
local n = frame.args[2]
math.randomseed(os.time())
return ';randomseed
end
Ligne 178 ⟶ 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 195 ⟶ 191 :
{{#invoke:Sandbox|exp|1}}
{{#invoke:Sandbox|floor|1.5}}
{{#invoke:Sandbox|fmod|5|3}}
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
Ligne 216 ⟶ 212 :
</pre></blockquote>
The result should be similar to:
<blockquote>
;abs
:
;acos
:
;asin
:
;atan
:
;atan2
:
;ceil
:
;cos
:
;cosh
:
;deg
:
;exp
:
;floor
:
;fmod
:
;frexp
:
;huge
:
;ldexp
:
;log
:
;log10
:
;max
:
;min
:
;modf
:
;pi
:
;pow
:
;rad
:
;random
:
;randomseed
:
;sin
:
;sinh
:
;sqrt
:
;tan
:
;tanh
:
</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>
# <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]]
|