Thursday, May 3, 2012

math library 사용예

   
 do  
      print(); print("***** MIN *****")  
   
      require("math")  
      local minVal = math.min(30, 20, 50, 40, 10)  
      print(minVal)  
   
      local maxVal = math.max(30, 20, 50, 40, 10)  
      print(maxVal)  
   
 end  
   
 do  
      print(); print("***** floor, ceil, sin, cos, pow *****")  
   
      local a = 10.8  
      local a_floor = math.floor(a)  
      local a_ceil = math.ceil(a)  
      local sin30 = math.sin(math.pi / 6)  
      local cos30 = math.cos(math.pi / 6)  
      print("floor 10.8 = " .. a_floor)  
      print("ceil 10.8 = " .. a_ceil)  
      print("sin 30 = " .. sin30)  
      print("cos 30 = " .. cos30)  
        
      local c = sin30^2 + cos30^2  
      print("sin30^2 + cos30^2 = " .. c)  
        
      local x = math.pow(2, 10)  
      print("2^10 = " .. x)  
 end  
        
 do  
      print(); print("***** modf, sqrt *****")  
   
      local x, y, z;  
        
      x, y = math.modf(3.14)  
      print("3.14 -> " .. x .. " & " .. y)  
        
      x, y = math.modf(-3.14)  
      print("3.14 -> " .. x .. " & " .. y)  
        
      x, y = math.modf(3)  
      print("3.14 -> " .. x .. " & " .. y)  
        
      z = math.sqrt(3)  
      print("sqrt(" .. 3 .. ") = " .. z)  
 end  
   
 do  
      print(); print("***** MATH.HUGE *****")  
      print("math.huge = " .. math.huge)  
      print("-math.huge = " .. -math.huge)  
      print("math.huge / 2 = " .. math.huge / 2)  
      print("math.huge / math.huge = " .. math.huge / math.huge)  
      print("math.huge * 0 = " .. math.huge * 0)  
      print("1 / 0 = " .. 1 / 0)  
      print(math.huge == math.huge)  
      print(1 / 0 == math.huge)  
 end  
   

No comments:

Post a Comment