github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/mathlib/lua/mathlib.lua (about)

     1  local function checknumarg(f)
     2      local ok = not pcall(f) and not pcall(f, {})
     3      if ok then
     4          print "ok"
     5      else
     6          print "bad"
     7      end
     8  end
     9  
    10  do
    11      checknumarg(math.abs)
    12      --> =ok
    13  
    14      print(math.abs(12))
    15      --> =12
    16  
    17      print(math.abs(-12))
    18      --> =12
    19  
    20      print(math.abs(-0.5))
    21      --> =0.5
    22  
    23      print(math.abs("-123"))
    24      --> =123
    25  
    26      print(pcall(math.abs))
    27      --> ~false\t.*value needed
    28  
    29      print(pcall(math.abs, {}))
    30      --> ~false\t.*must be a number
    31  end
    32  
    33  do
    34      checknumarg(math.ceil)
    35      --> =ok
    36  
    37      print(math.ceil(123))
    38      --> =123
    39  
    40      print(math.ceil(12.7))
    41      --> =13
    42  
    43      print(math.ceil(-8.2))
    44      --> =-8
    45  
    46      print(math.ceil("888.5673"))
    47      --> =889
    48  
    49      print(math.type(math.floor(3.5)))
    50      --> =integer
    51  end
    52  
    53  do
    54      checknumarg(math.exp)
    55      --> =ok
    56  
    57      print(math.exp(0))
    58      --> =1
    59      
    60  end
    61  
    62  do
    63      checknumarg(math.floor)
    64      --> =ok
    65  
    66      print(math.floor(123))
    67      --> =123
    68  
    69      print(math.floor(12.7))
    70      --> =12
    71  
    72      print(math.floor(-8.2))
    73      --> =-9
    74  
    75      print(math.floor("888.5673"))
    76      --> =888
    77  
    78      print(math.type(math.floor(3.5)))
    79      --> =integer
    80  end
    81  
    82  do
    83      checknumarg(math.log)
    84      --> =ok
    85  
    86      print(math.log(1))
    87      --> =0
    88  
    89      print(math.log(8, 2))
    90      --> =3
    91  
    92      print(pcall(math.log, 3, {}))
    93      --> ~false\t.*#2 must be a number
    94  end
    95  
    96  do
    97      print(pcall(math.min))
    98      --> ~false
    99  
   100      print(pcall(math.max))
   101      --> ~false
   102  
   103      print(math.max(3, 5, 2, 1))
   104      --> =5
   105  
   106      print(math.min(3, 5, 2, 1))
   107      --> =1
   108  
   109      print(pcall(math.min, 1, true))
   110      --> ~false
   111  
   112      print(pcall(math.max, 1, "true"))
   113      --> ~false
   114  
   115  end
   116  
   117  do
   118      checknumarg(math.modf)
   119      --> =ok
   120  
   121      print(math.modf(23))
   122      --> =23	0
   123  
   124      print(math.modf(1.5))
   125      --> =1	0.5
   126  
   127      print(math.modf(-1.5))
   128      --> =-1	-0.5
   129  
   130      print(math.modf(1/0))
   131      --> =+Inf	0
   132  
   133      print(math.modf(-1/0))
   134      --> =-Inf	0
   135  
   136  end
   137  
   138  do
   139      checknumarg(math.rad)
   140      --> =ok
   141  
   142      checknumarg(math.deg)
   143      --> =ok
   144  
   145      print(math.rad(90) == math.pi / 2)
   146      --> =true
   147  
   148      print(math.deg(math.pi) == 180)
   149      --> =true
   150  end
   151  
   152  do
   153      math.randomseed()
   154      local r1 = math.random()
   155      math.randomseed()
   156      local r2 = math.random()
   157      print(r1 == r2)
   158      --> =false
   159  
   160      local s1, s2 = math.randomseed()
   161      r1 = math.random()
   162      math.randomseed(s1, s2)
   163      r2 = math.random()
   164      print(r1 == r2)
   165      --> =true
   166  
   167      print(not pcall(math.randomseed, "hi"))
   168      --> =true
   169  
   170      print(not pcall(math.randomseed, 1, {}))
   171      --> =true
   172  
   173      print(not pcall(math.randomseed, 1.1, 2))
   174      --> =true
   175  
   176      print(not pcall(rand))
   177      --> =true
   178  
   179      local r = math.random()
   180      print(r >= 0 and r <= 1)
   181      --> =true
   182  
   183      local rands = {}
   184      math.randomseed(5)
   185      for i = 1, 20 do
   186          rands[i] = math.random(i, 2*i)
   187      end
   188  
   189      math.randomseed(5)
   190      local out, diff = 0, 0
   191      for i = 1, 20 do
   192          r = math.random(i, 2*i)
   193          if r < i or r > 2*i then
   194              out = out + 1
   195          end
   196          if rands[i] ~= r then
   197              diff = diff + 1
   198          end
   199      end
   200      print(out, diff)
   201      --> =0	0
   202  
   203      print(math.random(5, 5))
   204      --> =5
   205  
   206      local t={}
   207      for i = 1, 100 do
   208          t[math.random(-2,-1)] = true
   209      end
   210      print(t[-2] and t[-1])
   211      --> =true
   212  
   213      print((pcall(math.random, 2, 1)))
   214      --> =false
   215  
   216      print(math.random(0, math.maxinteger) >= 0)
   217      --> =true
   218  
   219      -- New in Lua 5.4
   220      print(math.random(0) == math.random(0))
   221      --> =false
   222  
   223      -- Any interval works
   224      print(math.random(-1, math.maxinteger) >= -1)
   225      --> =true
   226  end
   227  
   228  do
   229      checknumarg(math.sin)
   230      --> =ok
   231  
   232      checknumarg(math.cos)
   233      --> =ok
   234  
   235      checknumarg(math.tan)
   236      --> =ok
   237  
   238      local function toz(f)
   239          return function(x)
   240              local y = f(x)
   241              if math.abs(y) < 1e-15 then
   242                  return 0
   243              elseif math.abs(y) > 1e15 then
   244                  return 1/0
   245              end
   246              return y
   247          end
   248      end
   249      local pi = math.pi
   250      local sin = toz(math.sin)
   251      local cos = toz(math.cos)
   252      local tan = toz(math.tan)
   253  
   254      print(sin(0), sin(pi/2), sin(pi), sin(3*pi/2))
   255      --> =0	1	0	-1
   256  
   257      print(cos(0), cos(pi/2), cos(pi), cos(3*pi/2))
   258      --> =1	0	-1	0
   259  
   260      print(tan(0), tan(pi/2), tan(pi), tan(3*pi/2))
   261      --> =0	+Inf	0	+Inf
   262  end
   263  
   264  do
   265      checknumarg(math.asin)
   266      --> =ok
   267  
   268      checknumarg(math.acos)
   269      --> =ok
   270  
   271      checknumarg(math.atan)
   272      --> =ok
   273  
   274      local function round(num) 
   275          if num >= 0 then return math.floor(num+.5) 
   276          else return math.ceil(num-.5) end
   277      end
   278  
   279      local function top(f)
   280          return function(x)
   281              local y = 4*f(x)/math.pi
   282              local ry = round(y)
   283              if math.abs(y - ry) > 1e-15 then
   284                  return ry
   285              end
   286              return y
   287          end
   288      end
   289  
   290      local acos = top(math.acos)
   291      local asin = top(math.asin)
   292      local atan = top(math.atan)
   293  
   294      print(acos(-1), acos(0), acos(1))
   295      --> =4	2	0
   296  
   297      print(asin(-1), asin(0), asin(1))
   298      --> =-2	0	2
   299  
   300      print(atan(-1), atan(0), atan(1))
   301      --> =-1	0	1
   302  
   303      print(math.atan(1, 0) == math.pi/2)
   304      --> =true
   305  end
   306  
   307  do
   308      checknumarg(math.sqrt)
   309      --> =ok
   310  
   311      for i = 1, 5 do
   312          print(math.floor(1000*math.sqrt(i)))
   313      end
   314      --> =1000
   315      --> =1414
   316      --> =1732
   317      --> =2000
   318      --> =2236
   319  end
   320  
   321  do
   322      print(math.tointeger(56.0))
   323      --> =56
   324  
   325      print(math.tointeger("-123"))
   326      --> =-123
   327  
   328      print(math.tointeger({}))
   329      --> =nil
   330  
   331      print(math.tointeger(true))
   332      --> =nil
   333  end
   334  
   335  do
   336      print(math.type("123"))
   337      --> =nil
   338  
   339      print(math.type(123))
   340      --> =integer
   341  
   342      print(math.type(123.0))
   343      --> =float
   344  
   345      print(math.type(nil))
   346      --> =nil
   347  end
   348  
   349  do
   350      print(math.ult(1, 2))
   351      --> =true
   352  
   353      print(math.ult(-1, -2))
   354      --> =false
   355  
   356      print(math.ult(-1, 2))
   357      --> =false
   358  end
   359  
   360  do
   361      print(math.fmod(5, 2))
   362      --> =1
   363  
   364      print(math.fmod(-6, -6) == 0, math.fmod(-6.0, -6) == 0)
   365      --> =true	true
   366  
   367      print(pcall(math.fmod))
   368      --> ~false\t.*2 arguments needed
   369  
   370      print(pcall(math.fmod,"2", "a"))
   371      --> ~false\t.*expected numeric arguments
   372  
   373      print(pcall(math.fmod, 3, 0))
   374      --> ~false\t.*attempt to perform 'n%0'
   375  
   376      -- TODO: fix implementation
   377      -- print(math.fmod(-5, 2))
   378      -- --> =-1
   379  end