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

Contenu supprimé Contenu ajouté
m Correcting label on atan2
Youni Verciti (discussion | contributions)
Ligne 1 :
{{:{{BASEPAGENAME}}/Sidebar}}
{{nobots}}
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.
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]].
 
== Créer un script Lua avec... ==
== Prerequisites ==
# Accéder au [[Module:Sandbox]].
This lesson assumes you have already completed the [[Lua/Frame Object| Frame Object]] lesson.
# Supprimer le code existant.
 
# Ajouter le code suivant et enregistrer la page:
== Create a Lua Script that Uses the Math Library ==
<source lang="lua">
To create a Lua script that uses the Math library:
# Navigate 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 74 :
local x = frame.args[1]
local y = frame.args[2]
return ';fmod\n:math.fmod(' .. x .. ', ' .. y .. ') is ' .. math.fmod2fmod(x, y) .. '\n'
end
Ligne 86 :
end
function p.fmodldexp(frame)
local m = frame.args[1]
local e = frame.args[2]
return ';ldexp\n:math.fmodldexp(' .. m .. ', ' .. e .. ') is ' .. math.ldexp(m, e) .. '\n'
end
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
</source>
</pre></blockquote>
 
== TestTester Yourvotre Luanouveau Scriptscript ==
# Rendez-vous sur "votre page de test".
To test your Lua script:
# Ajouter le code suivant et enregistrer la page:
# 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 194 ⟶ 193 :
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
{{#invoke:Sandbox|fmodldexp|1|2}}
{{#invoke:Sandbox|log| 2.718281828459}}
{{#invoke:Sandbox|log10|100}}
Ligne 212 ⟶ 211 :
</pre></blockquote>
 
=== Le résultat doit correspondre à ceci: ===
The result should be similar to:
<blockquote>
;abs
Ligne 222 ⟶ 221 :
;atan
:math.atan(1) is 0.78539816339745
;acos
;atan2
:math.atan2(1, 1) is 0.78539816339745
;ceil
Ligne 276 ⟶ 275 :
</blockquote>
 
== UnderstandComprendre Yourle Luanouveau Scriptscript ==
To understand your Lua script:
# <code>math.abs(x)</code> returns the absolute value of <code>x</code> returns .
# <code>math.acos(x)</code> returns the arc cosine of <code>x</code> (given in radians).
# <code>math.asin(x)</code> returns the arc sine of <code>x</code> (given in radians).
# <code>math.atan(x)</code> returns the arc tangent of <code>x</code> (given in radians).
# <code>math.atan2(y, x)</code> returns the arc tangent of <code>y/x</code> (given in radians), using the signs of both parameters to find the quadrant of the result.
# <code>math.ceil(x)</code> returns the smallest integer larger than or equal to <code>x</code>.
# <code>math.cos(x)</code> returns the cosine of <code>x</code> (given in radians).
# <code>math.cosh(x)</code> returns the hyperbolic cosine of <code>x</code>.
# <code>math.deg(x)</code> returns the angle <code>x</code> (given in radians) in degrees.
# <code>math.exp(x)</code> returns the value <code>e^x</code>.
# <code>math.floor(x)</code> returns the largest integer smaller than or equal to <code>x</code>.
# <code>math.fmod2(x, y)</code> returns the remainder of the division of <code>x</code> by <code>y</code> that rounds the quotient towards zero.
# <code>math.frexp(x)</code> returns two values m and e such that <code>x</code> = m times 2^e, e is an integer, and the absolute value of m is in the range [0.5, 1)
# <code>math.huge</code> returns the value representing positive infinity; larger than or equal to any other numerical value.
# <code>math.ldexp(m, e)</code> returns <code>m</code> times <code>2^e</code> (<code>e</code> should be an integer).
# <code>math.log(x)</code> returns the natural logarithm of <code>x</code>.
# <code>math.log10(x)</code> returns the base-10 logarithm of <code>x</code>.
# <code>math.max(x, y)</code> returns the ma<code>x</code>imum value among its arguments.
# <code>math.min(x, y)</code> returns the minimum value among its arguments.
# <code>math.modf(x)</code> returns two numbers, the integral part of <code>x</code> and the fractional part of <code>x</code>.
# <code>math.pi</code> returns the value of pi.
# <code>math.pow(x, y)</code> returns <code>x^y</code>.
# <code>math.rad(x)</code> returns the angle <code>x</code> (given in degrees) in radians.
# <code>math.random(m, n)</code> returns a pseudo-random integer in the range <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.
# <code>math.sin(x)</code> returns the sine of <code>x</code> (given in radians).
# <code>math.sinh(x)</code> returns the hyperbolic sine of <code>x</code>.
# <code>math.sqrt(x)</code> returns the square root of <code>x</code>.
# <code>math.tan(x)</code> returns the tangent of <code>x</code> (given in radians).
# <code>math.tanh(x)</code> returns the hyperbolic tangent of <code>x</code>.
 
# <code>math.abs(x)</code> retourne la valeur absolue de <code>x</code>.
== Conclusion ==
# <code>math.acos(x)</code> retourne l'arc cosinus selon <code>x</code> (donné en radians).
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.
# <code>math.asin(x)</code> retourne l'arc sinus selon <code>x</code> (donné en radians).
# <code>math.atan(x)</code> retourne l'arc tangente selon <code>x</code> (donné en radians).
# <code>math.atan2(y, x)</code> retourne l'arc tangente selon <code>y/x</code> (donné en radians), using the signs of both parameters to find the quadrant of the result...
# <code>math.ceil(x)</code> retourne le plus petit nombre entier qui soit supérieur ou égal à <code>x</code>.
# <code>math.cos(x)</code> retourne le cosinus de <code>x</code> (donné en radians).
# <code>math.cosh(x)</code> retourne le cosinus hyperbolique de <code>x</code>.
# <code>math.deg(x)</code> retourne la valeur en degrés de l'angle <code>x</code> (donné en radians).
# <code>math.exp(x)</code> retourne la valeur e (la base des logarithmes normaux) augmenté à une puissance donnée <code>e^x</code>.
# <code>math.floor(x)</code> retourne le plus grand nombre entier qui soit inférieur ou égal à o <code>x</code>.
# <code>math.fmod(x, y)</code> retourne le reste de la division de <code>x</code> par <code>y</code> ...that rounds the quotient towards zero...
# <code>math.frexp(x)</code> retourne deux valeurs m et e tel que <code>x</code> = m times 2^e, e est un entier, et la valeur absolue de m est entre [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> retourne la valeur représentant l'infini positif ; supérieur ou égal à toute valeur numérique.
# <code>math.ldexp(m, e)</code> retourne <code>m</code> times <code>2^e</code> (<code>e</code> devrait être un entier)...(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> retourne le logarithme naturel de <code>x</code>.
# <code>math.log10(x)</code> retourne le logarithme décimal (base 10) de <code>x</code>.
# <code>math.max(x, y)</code> retourne la valeur maximum parmi ces arguments.
# <code>math.min(x, y)</code> retourne la valeur minimum parmi ces arguments.
# <code>math.modf(x)</code> retourne deux valeurs, la partie entière de <code>x</code> et la partie décimale (the fractional part) de <code>x</code>.
# <code>math.pi</code> retourne la valeur de pi.
# <code>math.pow(x, y)</code> retourne x exposant y équivaut à l’opérateur binaire <code>x^y</code>.
# <code>math.rad(x)</code> retourne la valeur en radians de l'angle <code>x</code> (donné en degrés).
# <code>math.random(m, n)</code> retourne un nombre entier pseudo-aléatoire compris entre <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> retourne le sinus de <code>x</code> (donné en radians).
# <code>math.sinh(x)</code> retourne le sinus hyperbolique de <code>x</code>.
# <code>math.sqrt(x)</code> retourne la racine carrée de <code>x</code>.
# <code>math.tan(x)</code> retourne la tangente de <code>x</code> (donné en radians).
# <code>math.tanh(x)</code> retourne la tangente hyperbolique de <code>x</code>.
 
== See AlsoConclusion ==
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.
* [[Lua/Frame Object | Lua Frame Object]]
 
== ReferencesVoir aussi ==
* [[../Objet Frame | Objet Frame]]
{{reflist}}
 
== Références ==
{{CourseCat}}
[[v:en:Lua| Lua for Wikiversity (en)]]
[[Category: Lessons]]
[[Category: Completed resources]]
 
[[Catégorie : Informatique | Programmation | Lua]]
[[fr:Initiation au Lua avec Scribunto/Fonctions mathématiques]]