Introduction au Lua/Fonctions Math

Début de la boite de navigation du chapitre


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.

Fonctions Math
Icône de la faculté
Chapitre no 12
Leçon : Introduction au Lua
Chap. préc. :Erreurs
Chap. suiv. :Fonctions OS
fin de la boite de navigation du chapitre
En raison de limitations techniques, la typographie souhaitable du titre, « Introduction au Lua : Fonctions Math
Introduction au Lua/Fonctions Math
 », n'a pu être restituée correctement ci-dessus.

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 fonctions mathématiques Lua, dans vos scripts.

Prérequis modifier

Cette leçon suppose que vous ayez assimilé la leçon Scribunto objet Frame.

Créer un script Lua avec des fonctions mathématiques modifier

  1. Accéder au Module:Sandbox.
  2. Supprimer le code existant.
  3. Ajouter le code suivant et enregistrer la page:
local p = {}
 
function p.abs(frame)
    local x = frame.args[1]
    return ';abs\n:math.abs(' .. x .. ') is ' .. math.abs(x) .. '\n'
end
 
function p.acos(frame)
    local x = frame.args[1]
    return ';acos\n:math.acos(' .. x .. ') is ' .. math.acos(x) .. '\n'
end
 
function p.asin(frame)
    local x = frame.args[1]
    return ';asin\n:math.asin(' .. x .. ') is ' .. math.asin(x) .. '\n'
end
 
function p.atan(frame)
    local x = frame.args[1]
    return ';atan\n:math.atan(' .. x .. ') is ' .. math.atan(x) .. '\n'
end
 
function p.atan2(frame)
    local y = frame.args[1]
    local x = frame.args[2]
    return ';atan2\n:math.atan2(' .. y .. ', ' .. x .. ') is ' .. math.atan2(y, x) .. '\n'
end
 
function p.ceil(frame)
    local x = frame.args[1]
    return ';ceil\n:math.ceil(' .. x .. ') is ' .. math.ceil(x) .. '\n'
end
 
function p.cos(frame)
    local x = frame.args[1]
    return ';cos\n:math.cos(' .. x .. ') is ' .. math.cos(x) .. '\n'
end
 
function p.cosh(frame)
    local x = frame.args[1]
    return ';cosh\n:math.cosh(' .. x .. ') is ' .. math.cosh(x) .. '\n'
end
 
function p.deg(frame)
    local x = frame.args[1]
    return ';deg\n:math.deg(' .. x .. ') is ' .. math.deg(x) .. '\n'
end
 
function p.exp(frame)
    local x = frame.args[1]
    return ';exp\n:math.exp(' .. x .. ') is ' .. math.exp(x) .. '\n'
end
 
function p.floor(frame)
    local x = frame.args[1]
    return ';floor\n:math.floor(' .. x .. ') is ' .. math.floor(x) .. '\n'
end
 
function p.fmod(frame)
    local x = frame.args[1]
    local y = frame.args[2]
    return ';fmod\n:math.fmod(' .. x .. ', ' .. y .. ') is ' .. math.fmod(x, y) .. '\n'
end
 
function p.frexp(frame)
    local x = frame.args[1]
    return ';frexp\n:math.frexp(' .. x .. ') is ' .. math.frexp(x) .. '\n'
end
 
function p.huge()
    return ';huge\n:math.huge is ' .. math.huge .. '\n'
end
 
function p.ldexp(frame)
    local m = frame.args[1]
    local e = frame.args[2]
    return ';ldexp\n:math.ldexp(' .. m .. ', ' .. e .. ') is ' .. math.ldexp(m, e) .. '\n'
end
 
function p.log(frame)
    local x = frame.args[1]
    return ';log\n:math.log(' .. x .. ') is ' .. math.log(x) .. '\n'
end
 
function p.log10(frame)
    local x = frame.args[1]
    return ';log10\n:math.log10(' .. x .. ') is ' .. math.log10(x) .. '\n'
end
 
function p.max(frame)
    local x = frame.args[1]
    local y = frame.args[2]
    return ';max\n:math.max(' .. x .. ', ' .. y .. ') is ' .. math.max(x, y) .. '\n'
end
 
function p.min(frame)
    local x = frame.args[1]
    local y = frame.args[2]
    return ';min\n:math.min(' .. x .. ', ' .. y .. ') is ' .. math.min(x, y) .. '\n'
end
 
function p.modf(frame)
    local x = frame.args[1]
    return ';modf\n:math.modf(' .. x .. ') is ' .. math.modf(x) .. '\n'
end
 
function p.pi()
    return ';pi\n:math.pi is ' .. math.pi .. '\n'
end
 
function p.pow(frame)
    local x = frame.args[1]
    local y = frame.args[2]
    return ';pow\n:math.pow(' .. x .. ', ' .. y .. ') is ' .. math.pow(x, y) .. '\n'
end
 
function p.rad(frame)
    local x = frame.args[1]
    return ';rad\n:math.rad(' .. x .. ') is ' .. math.rad(x) .. '\n'
end
 
function p.random(frame)
    local m = frame.args[1]
    local n = frame.args[2]
    return ';random\n:math.random(' .. m .. ', ' .. n .. ') is ' .. math.random(m, n) .. '\n'
end
 
function p.randomseed(frame)
    local m = frame.args[1]
    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
 
function p.sin(frame)
    local x = frame.args[1]
    return ';sin\n:math.sin(' .. x .. ') is ' .. math.sin(x) .. '\n'
end
 
function p.sinh(frame)
    local x = frame.args[1]
    return ';sinh\n:math.sinh(' .. x .. ') is ' .. math.sinh(x) .. '\n'
end
 
function p.sqrt(frame)
    local x = frame.args[1]
    return ';sqrt\n:math.sqrt(' .. x .. ') is ' .. math.sqrt(x) .. '\n'
end
 
function p.tan(frame)
    local x = frame.args[1]
    return ';tan\n:math.tan(' .. x .. ') is ' .. math.tan(x) .. '\n'
end
 
function p.tanh(frame)
    local x = frame.args[1]
    return ';tanh\n:math.tanh(' .. x .. ') is ' .. math.tanh(x) .. '\n'
end
 
return p

Tester votre nouveau script modifier

  1. Rendez-vous sur "votre page de test".
  2. Ajouter le code suivant et enregistrer la page:
{{#invoke:Sandbox|abs|-1}}
{{#invoke:Sandbox|acos|1}}
{{#invoke:Sandbox|asin|1}}
{{#invoke:Sandbox|atan|1}}
{{#invoke:Sandbox|atan2|1|1}}
{{#invoke:Sandbox|ceil|1.5}}
{{#invoke:Sandbox|cos|0.78539816339745}}
{{#invoke:Sandbox|cosh|0.78539816339745}}
{{#invoke:Sandbox|deg|0.78539816339745}}
{{#invoke:Sandbox|exp|1}}
{{#invoke:Sandbox|floor|1.5}}
{{#invoke:Sandbox|fmod|5|3}} 
{{#invoke:Sandbox|frexp|1}}
{{#invoke:Sandbox|huge}}
{{#invoke:Sandbox|ldexp|1|2}}
{{#invoke:Sandbox|log| 2.718281828459}}
{{#invoke:Sandbox|log10|100}}
{{#invoke:Sandbox|max|1|2}}
{{#invoke:Sandbox|min|1|2}}
{{#invoke:Sandbox|modf|1.5}}
{{#invoke:Sandbox|pi}}
{{#invoke:Sandbox|pow|10|2}}
{{#invoke:Sandbox|rad|45}}
{{#invoke:Sandbox|random|1|6}}
{{#invoke:Sandbox|randomseed|1|6}}
{{#invoke:Sandbox|sin|0.78539816339745}}
{{#invoke:Sandbox|sinh|0.78539816339745}}
{{#invoke:Sandbox|sqrt|100}}
{{#invoke:Sandbox|tan|0.78539816339745}}
{{#invoke:Sandbox|tanh|0.78539816339745}}

Le résultat doit correspondre à ceci: modifier

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

Comprendre le nouveau script modifier

  1. math.abs(x) retourne la valeur absolue de x.
  2. math.acos(x) retourne l'arc cosinus selon x (donné en radians).
  3. math.asin(x) retourne l'arc sinus selon x (donné en radians).
  4. math.atan(x) retourne l'arc tangente selon x (donné en radians).
  5. math.atan2(y, x) retourne l'arc tangente selon y/x (donné en radians), using the signs of both parameters to find the quadrant of the result...
  6. math.ceil(x) retourne le plus petit nombre entier qui soit supérieur ou égal à x.
  7. math.cos(x) retourne le cosinus de x (donné en radians).
  8. math.cosh(x) retourne le cosinus hyperbolique de x.
  9. math.deg(x) retourne la valeur en degrés de l'angle x (donné en radians).
  10. math.exp(x) retourne la valeur e (la base des logarithmes normaux) augmenté à une puissance donnée e^x.
  11. math.floor(x) retourne le plus grand nombre entier qui soit inférieur ou égal à o x.
  12. math.fmod(x, y) retourne le reste de la division de x par y ...that rounds the quotient towards zero...
  13. math.frexp(x) retourne deux valeurs m et e tel que x = 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.)
  14. math.huge retourne la valeur représentant l'infini positif ; supérieur ou égal à toute valeur numérique.
  15. math.ldexp(m, e) retourne m times 2^e (e 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.)
  16. math.log(x) retourne le logarithme naturel de x.
  17. math.log10(x) retourne le logarithme décimal (base 10) de x.
  18. math.max(x, y) retourne la valeur maximum parmi ces arguments.
  19. math.min(x, y) retourne la valeur minimum parmi ces arguments.
  20. math.modf(x) retourne deux valeurs, la partie entière de x et la partie décimale (the fractional part) de x.
  21. math.pi retourne la valeur de pi.
  22. math.pow(x, y) retourne x exposant y équivaut à l’opérateur binaire x^y.
  23. math.rad(x) retourne la valeur en radians de l'angle x (donné en degrés).
  24. math.random(m, n) retourne un nombre entier pseudo-aléatoire compris entre [m,n].
    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.
  25. math.randomseed(os.time()) La fonction utilise une base pour le générateur pseudo-aléatoire, on utilise communément os.time() pour générer une base aléatoire.
  26. math.sin(x) retourne le sinus de x (donné en radians).
  27. math.sinh(x) retourne le sinus hyperbolique de x.
  28. math.sqrt(x) retourne la racine carrée de x.
  29. math.tan(x) retourne la tangente de x (donné en radians).
  30. math.tanh(x) retourne la tangente hyperbolique de x.

Conclusion modifier

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 Introduction au Lua, pour étudier une autre bibliothèque de fonctions.

Voir aussi modifier

Références modifier

Lua for Wikiversity (en)