Tutorial - Math Library

This tutorial is aiming to teach you the function of the Math Library used by Sprite Basic Compiler. Those functions are provided through the math object, available at any time during program life-cycle.

math.PI() : number

get the value of PI

number n=math.PI() // = 3.14159 ...

math.abs(number n) : number

gives the absolute value

number n=math.abs(-2) // 2

math.acos(number n) : number

arccosinus

number n=math.acos(0) // Math.PI/2

math.asin(number n) : number

arcsinus

number n=math.asin(0) // 0

math.atan(number n) : number

arcsinus

number n=math.atan(1) // Math.PI/4

math.atan2(number y, number x) : number

arctangent quotient. get the angle between point 0,0 and point x,y Beware to know the angle for a point x,y , use Math.atan2(y,x) and not (x,y)

number angle=math.atan2(1,0) // Math.PI

math.ceil(number n) : number

gives the higher integer value

number n=math.ceil(1.7) // 2

math.cos(number n) : number

cosinus

number n=math.cos(0) // 1

math.exp(number n) : number

euler function

number n=math.exp(1) // 2.718

math.floor(number n) : number

gives the lower integer value

number n=math.floor(1.7) // 1

math.loga(number n) : number

natural logarythm

number n=math.loga(2) // 0.693

math.max(number y, number x) : number

find maximum value

number n=math.max(3,5) // = 5

math.min(number y, number x) : number

find minimum value

number n=math.min(3,5) // = 3

math.parseFloat(string str) : number

give the float value

number n=math.parseInt("2.5") // = 2.5

math.parseInt(string str) : number

give the integer value

number n=math.parseInt("2") // = 2

math.pow(number y, number x) : number

get the value of x to the power of y

number n=math.pow(2,3) // = 8

math.round(number n) : number

gives the nearest integer value

number n=math.round(1.7) // 2

math.sin(number n) : number

sinus

number n=math.sin(0) // 0

math.sqrt(number n) : number

square root

number n=math.sqrt(9) // 3

math.tan(number n) : number

tangent

number n=math.tan(Math.PI/4) // 1