mathlib.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package lua
  2. import (
  3. "math"
  4. "math/rand"
  5. )
  6. func OpenMath(L *LState) int {
  7. mod := L.RegisterModule(MathLibName, mathFuncs).(*LTable)
  8. mod.RawSetString("pi", LNumber(math.Pi))
  9. mod.RawSetString("huge", LNumber(math.MaxFloat64))
  10. L.Push(mod)
  11. return 1
  12. }
  13. var mathFuncs = map[string]LGFunction{
  14. "abs": mathAbs,
  15. "acos": mathAcos,
  16. "asin": mathAsin,
  17. "atan": mathAtan,
  18. "atan2": mathAtan2,
  19. "ceil": mathCeil,
  20. "cos": mathCos,
  21. "cosh": mathCosh,
  22. "deg": mathDeg,
  23. "exp": mathExp,
  24. "floor": mathFloor,
  25. "fmod": mathFmod,
  26. "frexp": mathFrexp,
  27. "ldexp": mathLdexp,
  28. "log": mathLog,
  29. "log10": mathLog10,
  30. "max": mathMax,
  31. "min": mathMin,
  32. "mod": mathMod,
  33. "modf": mathModf,
  34. "pow": mathPow,
  35. "rad": mathRad,
  36. "random": mathRandom,
  37. "randomseed": mathRandomseed,
  38. "sin": mathSin,
  39. "sinh": mathSinh,
  40. "sqrt": mathSqrt,
  41. "tan": mathTan,
  42. "tanh": mathTanh,
  43. }
  44. func mathAbs(L *LState) int {
  45. L.Push(LNumber(math.Abs(float64(L.CheckNumber(1)))))
  46. return 1
  47. }
  48. func mathAcos(L *LState) int {
  49. L.Push(LNumber(math.Acos(float64(L.CheckNumber(1)))))
  50. return 1
  51. }
  52. func mathAsin(L *LState) int {
  53. L.Push(LNumber(math.Asin(float64(L.CheckNumber(1)))))
  54. return 1
  55. }
  56. func mathAtan(L *LState) int {
  57. L.Push(LNumber(math.Atan(float64(L.CheckNumber(1)))))
  58. return 1
  59. }
  60. func mathAtan2(L *LState) int {
  61. L.Push(LNumber(math.Atan2(float64(L.CheckNumber(1)), float64(L.CheckNumber(2)))))
  62. return 1
  63. }
  64. func mathCeil(L *LState) int {
  65. L.Push(LNumber(math.Ceil(float64(L.CheckNumber(1)))))
  66. return 1
  67. }
  68. func mathCos(L *LState) int {
  69. L.Push(LNumber(math.Cos(float64(L.CheckNumber(1)))))
  70. return 1
  71. }
  72. func mathCosh(L *LState) int {
  73. L.Push(LNumber(math.Cosh(float64(L.CheckNumber(1)))))
  74. return 1
  75. }
  76. func mathDeg(L *LState) int {
  77. L.Push(LNumber(float64(L.CheckNumber(1)) * 180 / math.Pi))
  78. return 1
  79. }
  80. func mathExp(L *LState) int {
  81. L.Push(LNumber(math.Exp(float64(L.CheckNumber(1)))))
  82. return 1
  83. }
  84. func mathFloor(L *LState) int {
  85. L.Push(LNumber(math.Floor(float64(L.CheckNumber(1)))))
  86. return 1
  87. }
  88. func mathFmod(L *LState) int {
  89. L.Push(LNumber(math.Mod(float64(L.CheckNumber(1)), float64(L.CheckNumber(2)))))
  90. return 1
  91. }
  92. func mathFrexp(L *LState) int {
  93. v1, v2 := math.Frexp(float64(L.CheckNumber(1)))
  94. L.Push(LNumber(v1))
  95. L.Push(LNumber(v2))
  96. return 2
  97. }
  98. func mathLdexp(L *LState) int {
  99. L.Push(LNumber(math.Ldexp(float64(L.CheckNumber(1)), L.CheckInt(2))))
  100. return 1
  101. }
  102. func mathLog(L *LState) int {
  103. L.Push(LNumber(math.Log(float64(L.CheckNumber(1)))))
  104. return 1
  105. }
  106. func mathLog10(L *LState) int {
  107. L.Push(LNumber(math.Log10(float64(L.CheckNumber(1)))))
  108. return 1
  109. }
  110. func mathMax(L *LState) int {
  111. if L.GetTop() == 0 {
  112. L.RaiseError("wrong number of arguments")
  113. }
  114. max := L.CheckNumber(1)
  115. top := L.GetTop()
  116. for i := 2; i <= top; i++ {
  117. v := L.CheckNumber(i)
  118. if v > max {
  119. max = v
  120. }
  121. }
  122. L.Push(max)
  123. return 1
  124. }
  125. func mathMin(L *LState) int {
  126. if L.GetTop() == 0 {
  127. L.RaiseError("wrong number of arguments")
  128. }
  129. min := L.CheckNumber(1)
  130. top := L.GetTop()
  131. for i := 2; i <= top; i++ {
  132. v := L.CheckNumber(i)
  133. if v < min {
  134. min = v
  135. }
  136. }
  137. L.Push(min)
  138. return 1
  139. }
  140. func mathMod(L *LState) int {
  141. lhs := L.CheckNumber(1)
  142. rhs := L.CheckNumber(2)
  143. L.Push(luaModulo(lhs, rhs))
  144. return 1
  145. }
  146. func mathModf(L *LState) int {
  147. v1, v2 := math.Modf(float64(L.CheckNumber(1)))
  148. L.Push(LNumber(v1))
  149. L.Push(LNumber(v2))
  150. return 2
  151. }
  152. func mathPow(L *LState) int {
  153. L.Push(LNumber(math.Pow(float64(L.CheckNumber(1)), float64(L.CheckNumber(2)))))
  154. return 1
  155. }
  156. func mathRad(L *LState) int {
  157. L.Push(LNumber(float64(L.CheckNumber(1)) * math.Pi / 180))
  158. return 1
  159. }
  160. func mathRandom(L *LState) int {
  161. switch L.GetTop() {
  162. case 0:
  163. L.Push(LNumber(rand.Float64()))
  164. case 1:
  165. n := L.CheckInt(1)
  166. L.Push(LNumber(rand.Intn(n) + 1))
  167. default:
  168. min := L.CheckInt(1)
  169. max := L.CheckInt(2) + 1
  170. L.Push(LNumber(rand.Intn(max-min) + min))
  171. }
  172. return 1
  173. }
  174. func mathRandomseed(L *LState) int {
  175. rand.Seed(L.CheckInt64(1))
  176. return 0
  177. }
  178. func mathSin(L *LState) int {
  179. L.Push(LNumber(math.Sin(float64(L.CheckNumber(1)))))
  180. return 1
  181. }
  182. func mathSinh(L *LState) int {
  183. L.Push(LNumber(math.Sinh(float64(L.CheckNumber(1)))))
  184. return 1
  185. }
  186. func mathSqrt(L *LState) int {
  187. L.Push(LNumber(math.Sqrt(float64(L.CheckNumber(1)))))
  188. return 1
  189. }
  190. func mathTan(L *LState) int {
  191. L.Push(LNumber(math.Tan(float64(L.CheckNumber(1)))))
  192. return 1
  193. }
  194. func mathTanh(L *LState) int {
  195. L.Push(LNumber(math.Tanh(float64(L.CheckNumber(1)))))
  196. return 1
  197. }
  198. //