« Introduction au Lua/Fonctions Math » : différence entre les versions
Contenu supprimé Contenu ajouté
m Correcting label on atan2 |
|||
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 74 :
local x = frame.args[1]
local y = frame.args[2]
return ';fmod\n:math.fmod(' .. x .. ', ' .. y .. ') is ' .. math.
end
Ligne 86 :
end
function p.
local m = frame.args[1]
local e = frame.args[2]
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 193 ⟶ 194 :
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
{{#invoke:Sandbox|
{{#invoke:Sandbox|log| 2.718281828459}}
{{#invoke:Sandbox|log10|100}}
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 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.
# <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]]
|