Author Topic: Math Functions  (Read 2067 times)


  • Guest
Math Functions
« on: October 22, 2015, 03:38:35 pm »
 I did a short study of the Lua math functions and have the results here.
 I left out a few that were too hard to bother with.

                PulsarLua Math Functions

abs       ceil      cos        sin       
exp       floor     sqrt       rnd       round           
math.fmod     math.modf     math.log           math.log10
math.pi          math.pow      math.random    math.randomseed   
math.min       math.max      math.deg          math.tan          math.rad

 Here's how to use them in a program:

Code: [Select]

 ------------ START OF PROGRAM ---------------
 win = openwindow ("Math Functions",-1,-1,1000,700)
       setactivewindow (win)
       backcolor (0,0,0,255)
 color (0,0,255,255)       
a = math.pi                  -- returns 3.14....
b = abs(-2)                   -- converts to positive number
c = ceil(a)                   -- rounds a float up to the next integer
d = floor(a)                  -- rounds a float down to the next integer
e = math.max( 1,2,3,4 )      -- returns highest number in list: 4
f = math.min(1,2,3,4)        -- returns lowest number in list: 1
g = math.modf(a)             -- returns the integer part of a float 
h = math.fmod(10,4)          -- returns the integer part of a division 10/4
i = sqrt(81)                  -- Square Root
j = math.rad(60)             -- convert angles to radians
k = math.pow(10,6)           -- same as 10^6
l = math.log10(1000000)      -- base 10 logarithm
m = math.log(1000000)         -- base e logarithm
n = exp(2)                    -- returns e^x
    math.randomseed(10)            -- set up random number generator
o = math.random(10)                -- get random number from 1 to 10
p = math.random(10,100)            -- get random number from 10 to 100
q = sin(j)                          -- main trig functions
r = cos(j)
s = math.tan(j)
t = rnd(20)                          -- returns random number from 0 to 1
u = round(a)                        -- returns integer part of decimal fraction
v = math.deg( a/3)                  -- converts radians to degrees

drawtext( a,0,0)
drawtext( b,0,20)
drawtext( c,0,40)
drawtext( d,0,60)
drawtext( e,0,80)
drawtext( f,0,100)
drawtext( g,0,120)
drawtext( h,0,140)
drawtext( i,0,160)
drawtext( j,0,180)
drawtext( k,0,200)
drawtext( l,0,220)
drawtext( m,0,240)
drawtext( n,0,260)
drawtext( o,0,280)
drawtext( p,0,300)
drawtext( q,0,320)
drawtext( r,0,340)
drawtext( s,0,360)
drawtext( t,0,380)
drawtext( u,0,400)
drawtext( v,0,420)
drawtext( w,0,440)
drawtext( x,0,460)
drawtext( y,0,480)
drawtext( z,0,500)

closewindow(win)             -- This ends the program.


  • Guest
Re: Math Functions
« Reply #1 on: October 26, 2015, 10:46:09 am »
With Lua 5.2, math.log10(x) becomes math.log(x, 10). They have kept math.log10() for compatibility, but it should be deprecated soon...
Same for math.atan2(y,x) which will become math.atan(y, |x|) (with x optional) in Lua 5.3, but it seems this does not concern Pulsar2D at present.

PS: math.random() without argument is in principle equivalent to rnd()
« Last Edit: October 26, 2015, 10:50:26 am by Bereb »