« Introduction au Lua/Fonctions Math » : différence entre les versions

Contenu supprimé Contenu ajouté
Youni Verciti (discussion | contributions)
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.
{{nobots}}
Un module Lua-Scribunto pour Mediawiki, est une page de l'espace de nom "Module" qui utilise une table comme variable locale pour stocker ses fonctions et variables mais aussi pour renvoyer la réponse à la fin du processus.<ref>http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual/fr</ref>
Le chapitre bibliothèques Lua, étudie les fonctions intégrées au language et communément regroupées en librairies ou bibliothèques.
Cette leçon vous apprendra comment utiliser les [[mw:Extension:Scribunto/Lua_reference_manual/fr#Math_library|fonctions mathématiques]] Lua, dans vos scipts.
__TOC__
== Prérequis ==
Cette leçon suppose que vous ayez assimilé la leçon [[../Objet Frame | Objet Frame]].
 
== Prerequisites ==
== Créer un script Lua avec... ==
This lesson assumes you have already completed the [[Lua/Frame Object| Frame Object]] lesson.
# Accéder au [[Module:Sandbox]].
 
# Supprimer le code existant.
== Create a Lua Script that Uses the Math Library ==
# Ajouter le code suivant et enregistrer la page:
To create a Lua script that uses the Math library:
<source lang="lua">
# Accéder auNavigate to [[Module:Sandbox]].
# 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(os.time())\n:math.random(' .. m .. ', ' .. n .. ') is ' .. math.random(m, n) .. '\n'
end
Ligne 173 :
return p
</pre></blockquote>
</source>
 
== TesterTest votreYour nouveauLua scriptScript ==
To test your Lua script:
# Rendez-vous sur "votre page de test".
# Navigate to either the [[Module_talk:Sandbox]] page, the [[Wikiversity:Sandbox]] page, or your own user or sandbox page.
# Ajouter le code suivant et enregistrer la 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:
=== Le résultat doit correspondre à ceci: ===
<blockquote>
;abs
Ligne 221 ⟶ 222 :
;atan
:math.atan(1) is 0.78539816339745
;atan2
;acos
:math.atan2(1, 1) is 0.78539816339745
;ceil
Ligne 235 ⟶ 236 :
;floor
:math.floor(1.5) is 1
;fmod
;ldexp
:math.fmod(5, 3) is 402
;frexp
:math.frexp(1) is 0.5
Ligne 242 ⟶ 243 :
:math.huge is inf
;ldexp
:math.fmodldexp(1, 2) is 4
;log
:math.log( 2.718281828459) is 0.99999999999998
Ligne 256 ⟶ 257 :
:math.pi is 3.1415926535898
;pow
:math.pow(10, 2) is 2100
;rad
:math.rad(45) is 0.78539816339745
;random
:math.random(1, 6) is 12
;randomseed
:math.random(1, 6) is 51
;sin
:math.sin(0.78539816339745) is 0.70710678118655
Ligne 275 ⟶ 276 :
</blockquote>
 
== ComprendreUnderstand leYour nouveauLua scriptScript ==
To understand your Lua script:
 
# <code>math.abs(x)</code> retournereturns lathe valeurabsolute absoluevalue deof <code>x</code> returns .
# <code>math.acos(x)</code> retournereturns the l'arc cosinuscosine selonof <code>x</code> (donnégiven enin radians).
# <code>math.asin(x)</code> retournereturns the l'arc sinussine selonof <code>x</code> (donnégiven enin radians).
# <code>math.atan(x)</code> retournereturns the l'arc tangentetangent selonof <code>x</code> (donnégiven enin radians).
# <code>math.atan2(y, x)</code> retournereturns the l'arc tangentetangent selonof <code>y/x</code> (donnégiven enin radians), using the signs of both parameters to find the quadrant of the result...
# <code>math.ceil(x)</code> retournereturns lethe plussmallest petitinteger nombrelarger entierthan quior soitequal supérieur ou égal àto <code>x</code>.
# <code>math.cos(x)</code> retournereturns lethe cosinuscosine deof <code>x</code> (donnégiven enin radians).
# <code>math.cosh(x)</code> retournereturns lethe cosinushyperbolic hyperboliquecosine deof <code>x</code>.
# <code>math.deg(x)</code> retournereturns lathe valeur en degrés de l'angle <code>x</code> (donnégiven enin radians) in degrees.
# <code>math.exp(x)</code> retournereturns lathe valeur e (la base des logarithmes normaux) augmenté à une puissance donnéevalue <code>e^x</code>.
# <code>math.floor(x)</code> retournereturns lethe pluslargest grandinteger nombresmaller entierthan quior soitequal inférieur ou égal à oto <code>x</code>.
# <code>math.fmod(x, y)</code> retournereturns lethe resteremainder deof lathe division deof <code>x</code> parby <code>y</code> ...that rounds the quotient towards zero...
# <code>math.frexp(x)</code> retournereturns deuxtwo valeursvalues m etand e telsuch quethat <code>x</code> = m times 2^e, e estis unan entierinteger, etand lathe valeurabsolute absoluevalue deof m estis entrein the range [0.5, 1)... (La fonction est employée pour couper la valeur de nombre en fraction normale et exposant. Deux valeurs sont retournées : le premier est une valeur toujours dans la gamme 1/2 (incluse) à 1 (exclusivité) et la seconde est un exposant.)
# <code>math.huge</code> retournereturns lathe valeurvalue représentantrepresenting l'infini positifpositive infinity; supérieurlarger than or ouequal égalto àany touteother valeurnumerical numériquevalue.
# <code>math.ldexp(m, e)</code> retournereturns <code>m</code> times <code>2^e</code> (<code>e</code> devraitshould êtrebe unan entierinteger)...(La fonction prend un nombre normalisé et renvoie la représentation de virgule flottante. C'est la valeur multipliée par 2 à la puissance de l'exposant.)
# <code>math.log(x)</code> retournereturns lethe logarithmenatural naturellogarithm deof <code>x</code>.
# <code>math.log10(x)</code> retournereturns lethe logarithme décimal (base-10 10)logarithm deof <code>x</code>.
# <code>math.max(x, y)</code> retournereturns lathe valeurma<code>x</code>imum maximumvalue parmiamong cesits arguments.
# <code>math.min(x, y)</code> retournereturns la valeurthe minimum parmivalue among cesits arguments.
# <code>math.modf(x)</code> retournereturns deuxtwo valeursnumbers, lathe partieintegral entièrepart deof <code>x</code> et la partie décimaleand (the fractional part) deof <code>x</code>.
# <code>math.pi</code> retournereturns lathe valeurvalue deof pi.
# <code>math.pow(x, y)</code> retourne x exposant y équivaut à l’opérateur binaire returns <code>x^y</code>.
# <code>math.rad(x)</code> retournereturns lathe valeur en radians de l'angle <code>x</code> (donnégiven enin degrésdegrees) in radians.
# <code>math.random(m, n)</code> retournereturns un nombre entiera pseudo-aléatoirerandom integer in compristhe entrerange <code>[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... La fonction utilise une base pour le générateur pseudo-aléatoire, on utilise communément <code>os.time()</code> pour générer une base aléatoire.
# <code>math.sin(x)</code> retournereturns lethe sinussine deof <code>x</code> (donnégiven enin radians).
# <code>math.sinh(x)</code> retournereturns lethe sinushyperbolic hyperboliquesine deof <code>x</code>.
# <code>math.sqrt(x)</code> retournereturns lathe racinesquare carréeroot deof <code>x</code>.
# <code>math.tan(x)</code> retournereturns lathe tangentetangent deof <code>x</code> (donnégiven enin radians).
# <code>math.tanh(x)</code> retournereturns lathe tangentehyperbolic hyperboliquetangent deof <code>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.
Félicitation! Vous êtes capable de créer, tester et comprendre un script Lua qui utilise la librairie de fonctions mathématiques. Retournez à la page principale de la leçon [[Informatique/Programmation/Lua|Lua]], pour étudier une autre bibliothèque de fonctions.
 
== See Also ==
* [[..Lua/Objet Frame Object | ObjetLua Frame Object]]
 
== Voir aussiReferences ==
{{reflist}}
* [[../Objet Frame | Objet Frame]]
 
{{CourseCat}}
== Références ==
[[Category: Lessons]]
[[v:en:Lua| Lua for Wikiversity (en)]]
[[Category: Completed resources]]
 
[[fr:Initiation au Lua avec Scribunto/Fonctions mathématiques]]
[[Catégorie : Informatique | Programmation | Lua]]