math.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. print("testing numbers and math lib")
  2. do
  3. local a,b,c = "2", " 3e0 ", " 10 "
  4. assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0)
  5. assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string')
  6. assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ")
  7. assert(c%a == 0 and a^b == 8)
  8. end
  9. do
  10. local a,b = math.modf(3.5)
  11. assert(a == 3 and b == 0.5)
  12. assert(math.huge > 10e30)
  13. assert(-math.huge < -10e30)
  14. end
  15. function f(...)
  16. if select('#', ...) == 1 then
  17. return (...)
  18. else
  19. return "***"
  20. end
  21. end
  22. assert(tonumber{} == nil)
  23. assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
  24. tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
  25. tonumber'+1.' == 1)
  26. assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
  27. tonumber'1e' == nil and tonumber'1.0e+' == nil and
  28. tonumber'.' == nil)
  29. assert(tonumber('-12') == -10-2)
  30. assert(tonumber('-1.2e2') == - - -120)
  31. assert(f(tonumber('1 a')) == nil)
  32. assert(f(tonumber('e1')) == nil)
  33. assert(f(tonumber('e 1')) == nil)
  34. assert(f(tonumber(' 3.4.5 ')) == nil)
  35. assert(f(tonumber('')) == nil)
  36. assert(f(tonumber('', 8)) == nil)
  37. assert(f(tonumber(' ')) == nil)
  38. assert(f(tonumber(' ', 9)) == nil)
  39. assert(f(tonumber('99', 8)) == nil)
  40. assert(tonumber(' 1010 ', 2) == 10)
  41. assert(tonumber('10', 36) == 36)
  42. assert(tonumber('\n -10 \n', 36) == -36)
  43. assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15)))))))
  44. assert(tonumber('fFfa', 15) == nil)
  45. assert(tonumber(string.rep('1', 42), 2) + 1 == 2^42)
  46. assert(tonumber(string.rep('1', 32), 2) + 1 == 2^32)
  47. assert(tonumber('-fffffFFFFF', 16)-1 == -2^40)
  48. assert(tonumber('ffffFFFF', 16)+1 == 2^32)
  49. assert(tonumber('0xF') == 15)
  50. assert(1.1 == 1.+.1)
  51. print(100.0, 1E2, .01)
  52. assert(100.0 == 1E2 and .01 == 1e-2)
  53. assert(1111111111111111-1111111111111110== 1000.00e-03)
  54. -- 1234567890123456
  55. assert(1.1 == '1.'+'.1')
  56. assert('1111111111111111'-'1111111111111110' == tonumber" +0.001e+3 \n\t")
  57. function eq (a,b,limit)
  58. if not limit then limit = 10E-10 end
  59. return math.abs(a-b) <= limit
  60. end
  61. assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31)
  62. assert(0.123456 > 0.123455)
  63. assert(tonumber('+1.23E30') == 1.23*10^30)
  64. -- testing order operators
  65. assert(not(1<1) and (1<2) and not(2<1))
  66. assert(not('a'<'a') and ('a'<'b') and not('b'<'a'))
  67. assert((1<=1) and (1<=2) and not(2<=1))
  68. assert(('a'<='a') and ('a'<='b') and not('b'<='a'))
  69. assert(not(1>1) and not(1>2) and (2>1))
  70. assert(not('a'>'a') and not('a'>'b') and ('b'>'a'))
  71. assert((1>=1) and not(1>=2) and (2>=1))
  72. assert(('a'>='a') and not('a'>='b') and ('b'>='a'))
  73. -- testing mod operator
  74. assert(-4%3 == 2)
  75. assert(4%-3 == -2)
  76. assert(math.pi - math.pi % 1 == 3)
  77. assert(math.pi - math.pi % 0.001 == 3.141)
  78. local function testbit(a, n)
  79. return a/2^n % 2 >= 1
  80. end
  81. assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1))
  82. assert(eq(math.tan(math.pi/4), 1))
  83. assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0))
  84. assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and
  85. eq(math.asin(1), math.pi/2))
  86. assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2))
  87. assert(math.abs(-10) == 10)
  88. assert(eq(math.atan2(1,0), math.pi/2))
  89. assert(math.ceil(4.5) == 5.0)
  90. assert(math.floor(4.5) == 4.0)
  91. assert(math.mod(10,3) == 1)
  92. assert(eq(math.sqrt(10)^2, 10))
  93. assert(eq(math.log10(2), math.log(2)/math.log(10)))
  94. assert(eq(math.exp(0), 1))
  95. assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
  96. local v,e = math.frexp(math.pi)
  97. assert(eq(math.ldexp(v,e), math.pi))
  98. assert(eq(math.tanh(3.5), math.sinh(3.5)/math.cosh(3.5)))
  99. assert(tonumber(' 1.3e-2 ') == 1.3e-2)
  100. assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
  101. -- testing constant limits
  102. -- 2^23 = 8388608
  103. assert(8388609 + -8388609 == 0)
  104. assert(8388608 + -8388608 == 0)
  105. assert(8388607 + -8388607 == 0)
  106. if rawget(_G, "_soft") then return end
  107. f = io.tmpfile()
  108. assert(f)
  109. f:write("a = {")
  110. i = 1
  111. repeat
  112. f:write("{", math.sin(i), ", ", math.cos(i), ", ", i/3, "},\n")
  113. i=i+1
  114. until i > 1000
  115. f:write("}")
  116. f:seek("set", 0)
  117. assert(loadstring(f:read('*a')))()
  118. assert(f:close())
  119. assert(eq(a[300][1], math.sin(300)))
  120. assert(eq(a[600][1], math.sin(600)))
  121. assert(eq(a[500][2], math.cos(500)))
  122. assert(eq(a[800][2], math.cos(800)))
  123. assert(eq(a[200][3], 200/3))
  124. assert(eq(a[1000][3], 1000/3, 0.001))
  125. print('+')
  126. do -- testing NaN
  127. local NaN = 10e500 - 10e400
  128. assert(NaN ~= NaN)
  129. assert(not (NaN < NaN))
  130. assert(not (NaN <= NaN))
  131. assert(not (NaN > NaN))
  132. assert(not (NaN >= NaN))
  133. assert(not (0 < NaN))
  134. assert(not (NaN < 0))
  135. local a = {}
  136. assert(not pcall(function () a[NaN] = 1 end))
  137. assert(a[NaN] == nil)
  138. a[1] = 1
  139. assert(not pcall(function () a[NaN] = 1 end))
  140. assert(a[NaN] == nil)
  141. end
  142. require "checktable"
  143. stat(a)
  144. a = nil
  145. -- testing implicit convertions
  146. local a,b = '10', '20'
  147. assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
  148. assert(a == '10' and b == '20')
  149. math.randomseed(0)
  150. local i = 0
  151. local Max = 0
  152. local Min = 2
  153. repeat
  154. local t = math.random()
  155. Max = math.max(Max, t)
  156. Min = math.min(Min, t)
  157. i=i+1
  158. flag = eq(Max, 1, 0.001) and eq(Min, 0, 0.001)
  159. until flag or i>10000
  160. assert(0 <= Min and Max<1)
  161. assert(flag);
  162. for i=1,10 do
  163. local t = math.random(5)
  164. assert(1 <= t and t <= 5)
  165. end
  166. i = 0
  167. Max = -200
  168. Min = 200
  169. repeat
  170. local t = math.random(-10,0)
  171. Max = math.max(Max, t)
  172. Min = math.min(Min, t)
  173. i=i+1
  174. flag = (Max == 0 and Min == -10)
  175. until flag or i>10000
  176. assert(-10 <= Min and Max<=0)
  177. assert(flag);
  178. print('OK')