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

Contenu supprimé Contenu ajouté
m 18 révisions importées depuis en:Lua/Math_Library : Pour traduction en français
m Lydie Noria a déplacé la page Informatique/Programmation/Lua/Fonctions Math vers Lua/Math Library sans laisser de redirection
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.
 
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#Fonctions mathématiques|fonctions mathématiques]] Lua, dans vos scripts.
 
__TOC__
 
== PrerequisitesPrérequis ==
This lesson assumes you have already completed the [[Lua/Frame Object| Frame Object]] lesson.
 
Cette leçon suppose que vous ayez assimilé la leçon [[../Objet Frame | Objet Frame]].
== Create a Lua Script that Uses the Math Library ==
 
To create a Lua script that uses the Math library:
== Créer un script Lua avec... ==
# Navigate to [[Module:Sandbox]].
 
# Clear all existing code.
# Accéder au [[Module:Sandbox]].
#: 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.
# Supprimer le code existant.
# Add the following code and save the page:
# Ajouter le code suivant et enregistrer la page:
<blockquote><pre>
<source lang="lua">
local p = {}
Ligne 144 ⟶ 149 :
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 ⟶ 178 :
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 191 ⟶ 195 :
{{#invoke:Sandbox|exp|1}}
{{#invoke:Sandbox|floor|1.5}}
{{#invoke:Sandbox|fmod|5|3}}
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
Ligne 212 ⟶ 216 :
</pre></blockquote>
 
=== Le résultat doit correspondre à ceci: ===
The result should be similar to:
<blockquote>
;abs
: math.abs(-1) is 1
 
;acos
: math.acos(1) is 0
;asin
: math.asin(1) is 1.5707963267949
;atan
: math.atan(1) is 0.78539816339745
;atan2
: math.atan2(1, 1) is 0.78539816339745
;ceil
: math.ceil(1.5) is 2
;cos
: math.cos(0.78539816339745) is 0.70710678118655
;cosh
: math.cosh(0.78539816339745) is 1.324609089252
;deg
: math.deg(0.78539816339745) is 45
;exp
: math.exp(1) is 2.718281828459
;floor
: math.floor(1.5) is 1
;fmod
: math.fmod(5, 3) is 2
;frexp
: math.frexp(1) is 0.5
;huge
: math.huge is inf
;ldexp
: math.ldexp(1, 2) is 4
;log
: math.log( 2.718281828459) is 0.99999999999998
;log10
: math.log10(100) is 2
;max
: math.max(1, 2) is 2
;min
: math.min(1, 2) is 1
;modf
: math.modf(1.5) is 1
;pi
: math.pi is 3.1415926535898
;pow
: math.pow(10, 2) is 100
;rad
: math.rad(45) is 0.78539816339745
;random
: math.random(1, 6) is 2
;randomseed(os.time())
: math.random(1, 6) is 1
;sin
: math.sin(0.78539816339745) is 0.70710678118655
;sinh
: math.sinh(0.78539816339745) is 0.86867096148601
;sqrt
: math.sqrt(100) is 10
;tan
: math.tan(0.78539816339745) is 1
;tanh
: math.tanh(0.78539816339745) is 0.65579420263267
</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.fmod(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>.
#:Si le choix est aléatoire, l’algorithme reste le même, randomseed() permet de modifier la base du générateur pseudo-aléatoire.
# <code>math.randomseed(os.time())</code> 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]]
[[fr:Initiation au Lua avec Scribunto/Fonctions mathématiques]]
[[Catégorie:Programmation informatique]]
[[Catégorie:Lua]]