math.lua 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. -- $Id: testes/math.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing numbers and math lib")
  4. local minint <const> = math.mininteger
  5. local maxint <const> = math.maxinteger
  6. local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1
  7. assert((1 << intbits) == 0)
  8. assert(minint == 1 << (intbits - 1))
  9. assert(maxint == minint - 1)
  10. -- number of bits in the mantissa of a floating-point number
  11. local floatbits = 24
  12. do
  13. local p = 2.0^floatbits
  14. while p < p + 1.0 do
  15. p = p * 2.0
  16. floatbits = floatbits + 1
  17. end
  18. end
  19. local function isNaN (x)
  20. return (x ~= x)
  21. end
  22. assert(isNaN(0/0))
  23. assert(not isNaN(1/0))
  24. do
  25. local x = 2.0^floatbits
  26. assert(x > x - 1.0 and x == x + 1.0)
  27. print(string.format("%d-bit integers, %d-bit (mantissa) floats",
  28. intbits, floatbits))
  29. end
  30. assert(math.type(0) == "integer" and math.type(0.0) == "float"
  31. and not math.type("10"))
  32. local function checkerror (msg, f, ...)
  33. local s, err = pcall(f, ...)
  34. assert(not s and string.find(err, msg))
  35. end
  36. local msgf2i = "number.* has no integer representation"
  37. -- float equality
  38. local function eq (a,b,limit)
  39. if not limit then
  40. if floatbits >= 50 then limit = 1E-11
  41. else limit = 1E-5
  42. end
  43. end
  44. -- a == b needed for +inf/-inf
  45. return a == b or math.abs(a-b) <= limit
  46. end
  47. -- equality with types
  48. local function eqT (a,b)
  49. return a == b and math.type(a) == math.type(b)
  50. end
  51. -- basic float notation
  52. assert(0e12 == 0 and .0 == 0 and 0. == 0 and .2e2 == 20 and 2.E-1 == 0.2)
  53. do
  54. local a,b,c = "2", " 3e0 ", " 10 "
  55. assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0)
  56. assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string')
  57. assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ")
  58. assert(c%a == 0 and a^b == 08)
  59. a = 0
  60. assert(a == -a and 0 == -0)
  61. end
  62. do
  63. local x = -1
  64. local mz = 0/x -- minus zero
  65. local t = {[0] = 10, 20, 30, 40, 50}
  66. assert(t[mz] == t[0] and t[-0] == t[0])
  67. end
  68. do -- tests for 'modf'
  69. local a,b = math.modf(3.5)
  70. assert(a == 3.0 and b == 0.5)
  71. a,b = math.modf(-2.5)
  72. assert(a == -2.0 and b == -0.5)
  73. a,b = math.modf(-3e23)
  74. assert(a == -3e23 and b == 0.0)
  75. a,b = math.modf(3e35)
  76. assert(a == 3e35 and b == 0.0)
  77. a,b = math.modf(-1/0) -- -inf
  78. assert(a == -1/0 and b == 0.0)
  79. a,b = math.modf(1/0) -- inf
  80. assert(a == 1/0 and b == 0.0)
  81. a,b = math.modf(0/0) -- NaN
  82. assert(isNaN(a) and isNaN(b))
  83. a,b = math.modf(3) -- integer argument
  84. assert(eqT(a, 3) and eqT(b, 0.0))
  85. a,b = math.modf(minint)
  86. assert(eqT(a, minint) and eqT(b, 0.0))
  87. end
  88. assert(math.huge > 10e30)
  89. assert(-math.huge < -10e30)
  90. -- integer arithmetic
  91. assert(minint < minint + 1)
  92. assert(maxint - 1 < maxint)
  93. assert(0 - minint == minint)
  94. assert(minint * minint == 0)
  95. assert(maxint * maxint * maxint == maxint)
  96. -- testing floor division and conversions
  97. for _, i in pairs{-16, -15, -3, -2, -1, 0, 1, 2, 3, 15} do
  98. for _, j in pairs{-16, -15, -3, -2, -1, 1, 2, 3, 15} do
  99. for _, ti in pairs{0, 0.0} do -- try 'i' as integer and as float
  100. for _, tj in pairs{0, 0.0} do -- try 'j' as integer and as float
  101. local x = i + ti
  102. local y = j + tj
  103. assert(i//j == math.floor(i/j))
  104. end
  105. end
  106. end
  107. end
  108. assert(1//0.0 == 1/0)
  109. assert(-1 // 0.0 == -1/0)
  110. assert(eqT(3.5 // 1.5, 2.0))
  111. assert(eqT(3.5 // -1.5, -3.0))
  112. do -- tests for different kinds of opcodes
  113. local x, y
  114. x = 1; assert(x // 0.0 == 1/0)
  115. x = 1.0; assert(x // 0 == 1/0)
  116. x = 3.5; assert(eqT(x // 1, 3.0))
  117. assert(eqT(x // -1, -4.0))
  118. x = 3.5; y = 1.5; assert(eqT(x // y, 2.0))
  119. x = 3.5; y = -1.5; assert(eqT(x // y, -3.0))
  120. end
  121. assert(maxint // maxint == 1)
  122. assert(maxint // 1 == maxint)
  123. assert((maxint - 1) // maxint == 0)
  124. assert(maxint // (maxint - 1) == 1)
  125. assert(minint // minint == 1)
  126. assert(minint // minint == 1)
  127. assert((minint + 1) // minint == 0)
  128. assert(minint // (minint + 1) == 1)
  129. assert(minint // 1 == minint)
  130. assert(minint // -1 == -minint)
  131. assert(minint // -2 == 2^(intbits - 2))
  132. assert(maxint // -1 == -maxint)
  133. -- negative exponents
  134. do
  135. assert(2^-3 == 1 / 2^3)
  136. assert(eq((-3)^-3, 1 / (-3)^3))
  137. for i = -3, 3 do -- variables avoid constant folding
  138. for j = -3, 3 do
  139. -- domain errors (0^(-n)) are not portable
  140. if not _port or i ~= 0 or j > 0 then
  141. assert(eq(i^j, 1 / i^(-j)))
  142. end
  143. end
  144. end
  145. end
  146. -- comparison between floats and integers (border cases)
  147. if floatbits < intbits then
  148. assert(2.0^floatbits == (1 << floatbits))
  149. assert(2.0^floatbits - 1.0 == (1 << floatbits) - 1.0)
  150. assert(2.0^floatbits - 1.0 ~= (1 << floatbits))
  151. -- float is rounded, int is not
  152. assert(2.0^floatbits + 1.0 ~= (1 << floatbits) + 1)
  153. else -- floats can express all integers with full accuracy
  154. assert(maxint == maxint + 0.0)
  155. assert(maxint - 1 == maxint - 1.0)
  156. assert(minint + 1 == minint + 1.0)
  157. assert(maxint ~= maxint - 1.0)
  158. end
  159. assert(maxint + 0.0 == 2.0^(intbits - 1) - 1.0)
  160. assert(minint + 0.0 == minint)
  161. assert(minint + 0.0 == -2.0^(intbits - 1))
  162. -- order between floats and integers
  163. assert(1 < 1.1); assert(not (1 < 0.9))
  164. assert(1 <= 1.1); assert(not (1 <= 0.9))
  165. assert(-1 < -0.9); assert(not (-1 < -1.1))
  166. assert(1 <= 1.1); assert(not (-1 <= -1.1))
  167. assert(-1 < -0.9); assert(not (-1 < -1.1))
  168. assert(-1 <= -0.9); assert(not (-1 <= -1.1))
  169. assert(minint <= minint + 0.0)
  170. assert(minint + 0.0 <= minint)
  171. assert(not (minint < minint + 0.0))
  172. assert(not (minint + 0.0 < minint))
  173. assert(maxint < minint * -1.0)
  174. assert(maxint <= minint * -1.0)
  175. do
  176. local fmaxi1 = 2^(intbits - 1)
  177. assert(maxint < fmaxi1)
  178. assert(maxint <= fmaxi1)
  179. assert(not (fmaxi1 <= maxint))
  180. assert(minint <= -2^(intbits - 1))
  181. assert(-2^(intbits - 1) <= minint)
  182. end
  183. if floatbits < intbits then
  184. print("testing order (floats cannot represent all integers)")
  185. local fmax = 2^floatbits
  186. local ifmax = fmax | 0
  187. assert(fmax < ifmax + 1)
  188. assert(fmax - 1 < ifmax)
  189. assert(-(fmax - 1) > -ifmax)
  190. assert(not (fmax <= ifmax - 1))
  191. assert(-fmax > -(ifmax + 1))
  192. assert(not (-fmax >= -(ifmax - 1)))
  193. assert(fmax/2 - 0.5 < ifmax//2)
  194. assert(-(fmax/2 - 0.5) > -ifmax//2)
  195. assert(maxint < 2^intbits)
  196. assert(minint > -2^intbits)
  197. assert(maxint <= 2^intbits)
  198. assert(minint >= -2^intbits)
  199. else
  200. print("testing order (floats can represent all integers)")
  201. assert(maxint < maxint + 1.0)
  202. assert(maxint < maxint + 0.5)
  203. assert(maxint - 1.0 < maxint)
  204. assert(maxint - 0.5 < maxint)
  205. assert(not (maxint + 0.0 < maxint))
  206. assert(maxint + 0.0 <= maxint)
  207. assert(not (maxint < maxint + 0.0))
  208. assert(maxint + 0.0 <= maxint)
  209. assert(maxint <= maxint + 0.0)
  210. assert(not (maxint + 1.0 <= maxint))
  211. assert(not (maxint + 0.5 <= maxint))
  212. assert(not (maxint <= maxint - 1.0))
  213. assert(not (maxint <= maxint - 0.5))
  214. assert(minint < minint + 1.0)
  215. assert(minint < minint + 0.5)
  216. assert(minint <= minint + 0.5)
  217. assert(minint - 1.0 < minint)
  218. assert(minint - 1.0 <= minint)
  219. assert(not (minint + 0.0 < minint))
  220. assert(not (minint + 0.5 < minint))
  221. assert(not (minint < minint + 0.0))
  222. assert(minint + 0.0 <= minint)
  223. assert(minint <= minint + 0.0)
  224. assert(not (minint + 1.0 <= minint))
  225. assert(not (minint + 0.5 <= minint))
  226. assert(not (minint <= minint - 1.0))
  227. end
  228. do
  229. local NaN <const> = 0/0
  230. assert(not (NaN < 0))
  231. assert(not (NaN > minint))
  232. assert(not (NaN <= -9))
  233. assert(not (NaN <= maxint))
  234. assert(not (NaN < maxint))
  235. assert(not (minint <= NaN))
  236. assert(not (minint < NaN))
  237. assert(not (4 <= NaN))
  238. assert(not (4 < NaN))
  239. end
  240. -- avoiding errors at compile time
  241. local function checkcompt (msg, code)
  242. checkerror(msg, assert(load(code)))
  243. end
  244. checkcompt("divide by zero", "return 2 // 0")
  245. checkcompt(msgf2i, "return 2.3 >> 0")
  246. checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1))
  247. checkcompt("field 'huge'", "return math.huge << 1")
  248. checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1))
  249. checkcompt(msgf2i, "return 2.3 ~ 0.0")
  250. -- testing overflow errors when converting from float to integer (runtime)
  251. local function f2i (x) return x | x end
  252. checkerror(msgf2i, f2i, math.huge) -- +inf
  253. checkerror(msgf2i, f2i, -math.huge) -- -inf
  254. checkerror(msgf2i, f2i, 0/0) -- NaN
  255. if floatbits < intbits then
  256. -- conversion tests when float cannot represent all integers
  257. assert(maxint + 1.0 == maxint + 0.0)
  258. assert(minint - 1.0 == minint + 0.0)
  259. checkerror(msgf2i, f2i, maxint + 0.0)
  260. assert(f2i(2.0^(intbits - 2)) == 1 << (intbits - 2))
  261. assert(f2i(-2.0^(intbits - 2)) == -(1 << (intbits - 2)))
  262. assert((2.0^(floatbits - 1) + 1.0) // 1 == (1 << (floatbits - 1)) + 1)
  263. -- maximum integer representable as a float
  264. local mf = maxint - (1 << (floatbits - intbits)) + 1
  265. assert(f2i(mf + 0.0) == mf) -- OK up to here
  266. mf = mf + 1
  267. assert(f2i(mf + 0.0) ~= mf) -- no more representable
  268. else
  269. -- conversion tests when float can represent all integers
  270. assert(maxint + 1.0 > maxint)
  271. assert(minint - 1.0 < minint)
  272. assert(f2i(maxint + 0.0) == maxint)
  273. checkerror("no integer rep", f2i, maxint + 1.0)
  274. checkerror("no integer rep", f2i, minint - 1.0)
  275. end
  276. -- 'minint' should be representable as a float no matter the precision
  277. assert(f2i(minint + 0.0) == minint)
  278. -- testing numeric strings
  279. assert("2" + 1 == 3)
  280. assert("2 " + 1 == 3)
  281. assert(" -2 " + 1 == -1)
  282. assert(" -0xa " + 1 == -9)
  283. -- Literal integer Overflows (new behavior in 5.3.3)
  284. do
  285. -- no overflows
  286. assert(eqT(tonumber(tostring(maxint)), maxint))
  287. assert(eqT(tonumber(tostring(minint)), minint))
  288. -- add 1 to last digit as a string (it cannot be 9...)
  289. local function incd (n)
  290. local s = string.format("%d", n)
  291. s = string.gsub(s, "%d$", function (d)
  292. assert(d ~= '9')
  293. return string.char(string.byte(d) + 1)
  294. end)
  295. return s
  296. end
  297. -- 'tonumber' with overflow by 1
  298. assert(eqT(tonumber(incd(maxint)), maxint + 1.0))
  299. assert(eqT(tonumber(incd(minint)), minint - 1.0))
  300. -- large numbers
  301. assert(eqT(tonumber("1"..string.rep("0", 30)), 1e30))
  302. assert(eqT(tonumber("-1"..string.rep("0", 30)), -1e30))
  303. -- hexa format still wraps around
  304. assert(eqT(tonumber("0x1"..string.rep("0", 30)), 0))
  305. -- lexer in the limits
  306. assert(minint == load("return " .. minint)())
  307. assert(eqT(maxint, load("return " .. maxint)()))
  308. assert(eqT(10000000000000000000000.0, 10000000000000000000000))
  309. assert(eqT(-10000000000000000000000.0, -10000000000000000000000))
  310. end
  311. -- testing 'tonumber'
  312. -- 'tonumber' with numbers
  313. assert(tonumber(3.4) == 3.4)
  314. assert(eqT(tonumber(3), 3))
  315. assert(eqT(tonumber(maxint), maxint) and eqT(tonumber(minint), minint))
  316. assert(tonumber(1/0) == 1/0)
  317. -- 'tonumber' with strings
  318. assert(tonumber("0") == 0)
  319. assert(not tonumber(""))
  320. assert(not tonumber(" "))
  321. assert(not tonumber("-"))
  322. assert(not tonumber(" -0x "))
  323. assert(not tonumber{})
  324. assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
  325. tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
  326. tonumber'+1.' == 1)
  327. assert(not tonumber'+ 0.01' and not tonumber'+.e1' and
  328. not tonumber'1e' and not tonumber'1.0e+' and
  329. not tonumber'.')
  330. assert(tonumber('-012') == -010-2)
  331. assert(tonumber('-1.2e2') == - - -120)
  332. assert(tonumber("0xffffffffffff") == (1 << (4*12)) - 1)
  333. assert(tonumber("0x"..string.rep("f", (intbits//4))) == -1)
  334. assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1)
  335. -- testing 'tonumber' with base
  336. assert(tonumber(' 001010 ', 2) == 10)
  337. assert(tonumber(' 001010 ', 10) == 001010)
  338. assert(tonumber(' -1010 ', 2) == -10)
  339. assert(tonumber('10', 36) == 36)
  340. assert(tonumber(' -10 ', 36) == -36)
  341. assert(tonumber(' +1Z ', 36) == 36 + 35)
  342. assert(tonumber(' -1z ', 36) == -36 + -35)
  343. assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15)))))))
  344. assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2^(intbits - 2))
  345. assert(tonumber('ffffFFFF', 16)+1 == (1 << 32))
  346. assert(tonumber('0ffffFFFF', 16)+1 == (1 << 32))
  347. assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40))
  348. for i = 2,36 do
  349. local i2 = i * i
  350. local i10 = i2 * i2 * i2 * i2 * i2 -- i^10
  351. assert(tonumber('\t10000000000\t', i) == i10)
  352. end
  353. if not _soft then
  354. -- tests with very long numerals
  355. assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1)
  356. assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1)
  357. assert(tonumber("0x"..string.rep("f", 300)..".0") == 2.0^(4*300) - 1)
  358. assert(tonumber("0x"..string.rep("f", 500)..".0") == 2.0^(4*500) - 1)
  359. assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3)
  360. assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10)
  361. assert(tonumber('0x0.' .. string.rep('0', 13).."1") == 2.0^(-4*14))
  362. assert(tonumber('0x0.' .. string.rep('0', 150).."1") == 2.0^(-4*151))
  363. assert(tonumber('0x0.' .. string.rep('0', 300).."1") == 2.0^(-4*301))
  364. assert(tonumber('0x0.' .. string.rep('0', 500).."1") == 2.0^(-4*501))
  365. assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0)
  366. assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 0x7.4)
  367. end
  368. -- testing 'tonumber' for invalid formats
  369. local function f (...)
  370. if select('#', ...) == 1 then
  371. return (...)
  372. else
  373. return "***"
  374. end
  375. end
  376. assert(not f(tonumber('fFfa', 15)))
  377. assert(not f(tonumber('099', 8)))
  378. assert(not f(tonumber('1\0', 2)))
  379. assert(not f(tonumber('', 8)))
  380. assert(not f(tonumber(' ', 9)))
  381. assert(not f(tonumber(' ', 9)))
  382. assert(not f(tonumber('0xf', 10)))
  383. assert(not f(tonumber('inf')))
  384. assert(not f(tonumber(' INF ')))
  385. assert(not f(tonumber('Nan')))
  386. assert(not f(tonumber('nan')))
  387. assert(not f(tonumber(' ')))
  388. assert(not f(tonumber('')))
  389. assert(not f(tonumber('1 a')))
  390. assert(not f(tonumber('1 a', 2)))
  391. assert(not f(tonumber('1\0')))
  392. assert(not f(tonumber('1 \0')))
  393. assert(not f(tonumber('1\0 ')))
  394. assert(not f(tonumber('e1')))
  395. assert(not f(tonumber('e 1')))
  396. assert(not f(tonumber(' 3.4.5 ')))
  397. -- testing 'tonumber' for invalid hexadecimal formats
  398. assert(not tonumber('0x'))
  399. assert(not tonumber('x'))
  400. assert(not tonumber('x3'))
  401. assert(not tonumber('0x3.3.3')) -- two decimal points
  402. assert(not tonumber('00x2'))
  403. assert(not tonumber('0x 2'))
  404. assert(not tonumber('0 x2'))
  405. assert(not tonumber('23x'))
  406. assert(not tonumber('- 0xaa'))
  407. assert(not tonumber('-0xaaP ')) -- no exponent
  408. assert(not tonumber('0x0.51p'))
  409. assert(not tonumber('0x5p+-2'))
  410. -- testing hexadecimal numerals
  411. assert(0x10 == 16 and 0xfff == 2^12 - 1 and 0XFB == 251)
  412. assert(0x0p12 == 0 and 0x.0p-3 == 0)
  413. assert(0xFFFFFFFF == (1 << 32) - 1)
  414. assert(tonumber('+0x2') == 2)
  415. assert(tonumber('-0xaA') == -170)
  416. assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1)
  417. -- possible confusion with decimal exponent
  418. assert(0E+1 == 0 and 0xE+1 == 15 and 0xe-1 == 13)
  419. -- floating hexas
  420. assert(tonumber(' 0x2.5 ') == 0x25/16)
  421. assert(tonumber(' -0x2.5 ') == -0x25/16)
  422. assert(tonumber(' +0x0.51p+8 ') == 0x51)
  423. assert(0x.FfffFFFF == 1 - '0x.00000001')
  424. assert('0xA.a' + 0 == 10 + 10/16)
  425. assert(0xa.aP4 == 0XAA)
  426. assert(0x4P-2 == 1)
  427. assert(0x1.1 == '0x1.' + '+0x.1')
  428. assert(0Xabcdef.0 == 0x.ABCDEFp+24)
  429. assert(1.1 == 1.+.1)
  430. assert(100.0 == 1E2 and .01 == 1e-2)
  431. assert(1111111111 - 1111111110 == 1000.00e-03)
  432. assert(1.1 == '1.'+'.1')
  433. assert(tonumber'1111111111' - tonumber'1111111110' ==
  434. tonumber" +0.001e+3 \n\t")
  435. assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31)
  436. assert(0.123456 > 0.123455)
  437. assert(tonumber('+1.23E18') == 1.23*10.0^18)
  438. -- testing order operators
  439. assert(not(1<1) and (1<2) and not(2<1))
  440. assert(not('a'<'a') and ('a'<'b') and not('b'<'a'))
  441. assert((1<=1) and (1<=2) and not(2<=1))
  442. assert(('a'<='a') and ('a'<='b') and not('b'<='a'))
  443. assert(not(1>1) and not(1>2) and (2>1))
  444. assert(not('a'>'a') and not('a'>'b') and ('b'>'a'))
  445. assert((1>=1) and not(1>=2) and (2>=1))
  446. assert(('a'>='a') and not('a'>='b') and ('b'>='a'))
  447. assert(1.3 < 1.4 and 1.3 <= 1.4 and not (1.3 < 1.3) and 1.3 <= 1.3)
  448. -- testing mod operator
  449. assert(eqT(-4 % 3, 2))
  450. assert(eqT(4 % -3, -2))
  451. assert(eqT(-4.0 % 3, 2.0))
  452. assert(eqT(4 % -3.0, -2.0))
  453. assert(eqT(4 % -5, -1))
  454. assert(eqT(4 % -5.0, -1.0))
  455. assert(eqT(4 % 5, 4))
  456. assert(eqT(4 % 5.0, 4.0))
  457. assert(eqT(-4 % -5, -4))
  458. assert(eqT(-4 % -5.0, -4.0))
  459. assert(eqT(-4 % 5, 1))
  460. assert(eqT(-4 % 5.0, 1.0))
  461. assert(eqT(4.25 % 4, 0.25))
  462. assert(eqT(10.0 % 2, 0.0))
  463. assert(eqT(-10.0 % 2, 0.0))
  464. assert(eqT(-10.0 % -2, 0.0))
  465. assert(math.pi - math.pi % 1 == 3)
  466. assert(math.pi - math.pi % 0.001 == 3.141)
  467. do -- very small numbers
  468. local i, j = 0, 20000
  469. while i < j do
  470. local m = (i + j) // 2
  471. if 10^-m > 0 then
  472. i = m + 1
  473. else
  474. j = m
  475. end
  476. end
  477. -- 'i' is the smallest possible ten-exponent
  478. local b = 10^-(i - (i // 10)) -- a very small number
  479. assert(b > 0 and b * b == 0)
  480. local delta = b / 1000
  481. assert(eq((2.1 * b) % (2 * b), (0.1 * b), delta))
  482. assert(eq((-2.1 * b) % (2 * b), (2 * b) - (0.1 * b), delta))
  483. assert(eq((2.1 * b) % (-2 * b), (0.1 * b) - (2 * b), delta))
  484. assert(eq((-2.1 * b) % (-2 * b), (-0.1 * b), delta))
  485. end
  486. -- basic consistency between integer modulo and float modulo
  487. for i = -10, 10 do
  488. for j = -10, 10 do
  489. if j ~= 0 then
  490. assert((i + 0.0) % j == i % j)
  491. end
  492. end
  493. end
  494. for i = 0, 10 do
  495. for j = -10, 10 do
  496. if j ~= 0 then
  497. assert((2^i) % j == (1 << i) % j)
  498. end
  499. end
  500. end
  501. do -- precision of module for large numbers
  502. local i = 10
  503. while (1 << i) > 0 do
  504. assert((1 << i) % 3 == i % 2 + 1)
  505. i = i + 1
  506. end
  507. i = 10
  508. while 2^i < math.huge do
  509. assert(2^i % 3 == i % 2 + 1)
  510. i = i + 1
  511. end
  512. end
  513. assert(eqT(minint % minint, 0))
  514. assert(eqT(maxint % maxint, 0))
  515. assert((minint + 1) % minint == minint + 1)
  516. assert((maxint - 1) % maxint == maxint - 1)
  517. assert(minint % maxint == maxint - 1)
  518. assert(minint % -1 == 0)
  519. assert(minint % -2 == 0)
  520. assert(maxint % -2 == -1)
  521. -- non-portable tests because Windows C library cannot compute
  522. -- fmod(1, huge) correctly
  523. if not _port then
  524. local function anan (x) assert(isNaN(x)) end -- assert Not a Number
  525. anan(0.0 % 0)
  526. anan(1.3 % 0)
  527. anan(math.huge % 1)
  528. anan(math.huge % 1e30)
  529. anan(-math.huge % 1e30)
  530. anan(-math.huge % -1e30)
  531. assert(1 % math.huge == 1)
  532. assert(1e30 % math.huge == 1e30)
  533. assert(1e30 % -math.huge == -math.huge)
  534. assert(-1 % math.huge == math.huge)
  535. assert(-1 % -math.huge == -1)
  536. end
  537. -- testing unsigned comparisons
  538. assert(math.ult(3, 4))
  539. assert(not math.ult(4, 4))
  540. assert(math.ult(-2, -1))
  541. assert(math.ult(2, -1))
  542. assert(not math.ult(-2, -2))
  543. assert(math.ult(maxint, minint))
  544. assert(not math.ult(minint, maxint))
  545. assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1))
  546. assert(eq(math.tan(math.pi/4), 1))
  547. assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0))
  548. assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and
  549. eq(math.asin(1), math.pi/2))
  550. assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2))
  551. assert(math.abs(-10.43) == 10.43)
  552. assert(eqT(math.abs(minint), minint))
  553. assert(eqT(math.abs(maxint), maxint))
  554. assert(eqT(math.abs(-maxint), maxint))
  555. assert(eq(math.atan(1,0), math.pi/2))
  556. assert(math.fmod(10,3) == 1)
  557. assert(eq(math.sqrt(10)^2, 10))
  558. assert(eq(math.log(2, 10), math.log(2)/math.log(10)))
  559. assert(eq(math.log(2, 2), 1))
  560. assert(eq(math.log(9, 3), 2))
  561. assert(eq(math.exp(0), 1))
  562. assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
  563. assert(tonumber(' 1.3e-2 ') == 1.3e-2)
  564. assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
  565. -- testing constant limits
  566. -- 2^23 = 8388608
  567. assert(8388609 + -8388609 == 0)
  568. assert(8388608 + -8388608 == 0)
  569. assert(8388607 + -8388607 == 0)
  570. do -- testing floor & ceil
  571. assert(eqT(math.floor(3.4), 3))
  572. assert(eqT(math.ceil(3.4), 4))
  573. assert(eqT(math.floor(-3.4), -4))
  574. assert(eqT(math.ceil(-3.4), -3))
  575. assert(eqT(math.floor(maxint), maxint))
  576. assert(eqT(math.ceil(maxint), maxint))
  577. assert(eqT(math.floor(minint), minint))
  578. assert(eqT(math.floor(minint + 0.0), minint))
  579. assert(eqT(math.ceil(minint), minint))
  580. assert(eqT(math.ceil(minint + 0.0), minint))
  581. assert(math.floor(1e50) == 1e50)
  582. assert(math.ceil(1e50) == 1e50)
  583. assert(math.floor(-1e50) == -1e50)
  584. assert(math.ceil(-1e50) == -1e50)
  585. for _, p in pairs{31,32,63,64} do
  586. assert(math.floor(2^p) == 2^p)
  587. assert(math.floor(2^p + 0.5) == 2^p)
  588. assert(math.ceil(2^p) == 2^p)
  589. assert(math.ceil(2^p - 0.5) == 2^p)
  590. end
  591. checkerror("number expected", math.floor, {})
  592. checkerror("number expected", math.ceil, print)
  593. assert(eqT(math.tointeger(minint), minint))
  594. assert(eqT(math.tointeger(minint .. ""), minint))
  595. assert(eqT(math.tointeger(maxint), maxint))
  596. assert(eqT(math.tointeger(maxint .. ""), maxint))
  597. assert(eqT(math.tointeger(minint + 0.0), minint))
  598. assert(not math.tointeger(0.0 - minint))
  599. assert(not math.tointeger(math.pi))
  600. assert(not math.tointeger(-math.pi))
  601. assert(math.floor(math.huge) == math.huge)
  602. assert(math.ceil(math.huge) == math.huge)
  603. assert(not math.tointeger(math.huge))
  604. assert(math.floor(-math.huge) == -math.huge)
  605. assert(math.ceil(-math.huge) == -math.huge)
  606. assert(not math.tointeger(-math.huge))
  607. assert(math.tointeger("34.0") == 34)
  608. assert(not math.tointeger("34.3"))
  609. assert(not math.tointeger({}))
  610. assert(not math.tointeger(0/0)) -- NaN
  611. end
  612. -- testing fmod for integers
  613. for i = -6, 6 do
  614. for j = -6, 6 do
  615. if j ~= 0 then
  616. local mi = math.fmod(i, j)
  617. local mf = math.fmod(i + 0.0, j)
  618. assert(mi == mf)
  619. assert(math.type(mi) == 'integer' and math.type(mf) == 'float')
  620. if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then
  621. assert(eqT(mi, i % j))
  622. end
  623. end
  624. end
  625. end
  626. assert(eqT(math.fmod(minint, minint), 0))
  627. assert(eqT(math.fmod(maxint, maxint), 0))
  628. assert(eqT(math.fmod(minint + 1, minint), minint + 1))
  629. assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1))
  630. checkerror("zero", math.fmod, 3, 0)
  631. do -- testing max/min
  632. checkerror("value expected", math.max)
  633. checkerror("value expected", math.min)
  634. assert(eqT(math.max(3), 3))
  635. assert(eqT(math.max(3, 5, 9, 1), 9))
  636. assert(math.max(maxint, 10e60) == 10e60)
  637. assert(eqT(math.max(minint, minint + 1), minint + 1))
  638. assert(eqT(math.min(3), 3))
  639. assert(eqT(math.min(3, 5, 9, 1), 1))
  640. assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2)
  641. assert(math.min(1.9, 1.7, 1.72) == 1.7)
  642. assert(math.min(-10e60, minint) == -10e60)
  643. assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
  644. assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
  645. end
  646. -- testing implicit conversions
  647. local a,b = '10', '20'
  648. assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
  649. assert(a == '10' and b == '20')
  650. do
  651. print("testing -0 and NaN")
  652. local mz <const> = -0.0
  653. local z <const> = 0.0
  654. assert(mz == z)
  655. assert(1/mz < 0 and 0 < 1/z)
  656. local a = {[mz] = 1}
  657. assert(a[z] == 1 and a[mz] == 1)
  658. a[z] = 2
  659. assert(a[z] == 2 and a[mz] == 2)
  660. local inf = math.huge * 2 + 1
  661. local mz <const> = -1/inf
  662. local z <const> = 1/inf
  663. assert(mz == z)
  664. assert(1/mz < 0 and 0 < 1/z)
  665. local NaN <const> = inf - inf
  666. assert(NaN ~= NaN)
  667. assert(not (NaN < NaN))
  668. assert(not (NaN <= NaN))
  669. assert(not (NaN > NaN))
  670. assert(not (NaN >= NaN))
  671. assert(not (0 < NaN) and not (NaN < 0))
  672. local NaN1 <const> = 0/0
  673. assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
  674. local a = {}
  675. assert(not pcall(rawset, a, NaN, 1))
  676. assert(a[NaN] == undef)
  677. a[1] = 1
  678. assert(not pcall(rawset, a, NaN, 1))
  679. assert(a[NaN] == undef)
  680. -- strings with same binary representation as 0.0 (might create problems
  681. -- for constant manipulation in the pre-compiler)
  682. local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0"
  683. assert(a1 == a2 and a2 == a4 and a1 ~= a3)
  684. assert(a3 == a5)
  685. end
  686. print("testing 'math.random'")
  687. local random, max, min = math.random, math.max, math.min
  688. local function testnear (val, ref, tol)
  689. return (math.abs(val - ref) < ref * tol)
  690. end
  691. -- low-level!! For the current implementation of random in Lua,
  692. -- the first call after seed 1007 should return 0x7a7040a5a323c9d6
  693. do
  694. -- all computations should work with 32-bit integers
  695. local h <const> = 0x7a7040a5 -- higher half
  696. local l <const> = 0xa323c9d6 -- lower half
  697. math.randomseed(1007)
  698. -- get the low 'intbits' of the 64-bit expected result
  699. local res = (h << 32 | l) & ~(~0 << intbits)
  700. assert(random(0) == res)
  701. math.randomseed(1007, 0)
  702. -- using higher bits to generate random floats; (the '% 2^32' converts
  703. -- 32-bit integers to floats as unsigned)
  704. local res
  705. if floatbits <= 32 then
  706. -- get all bits from the higher half
  707. res = (h >> (32 - floatbits)) % 2^32
  708. else
  709. -- get 32 bits from the higher half and the rest from the lower half
  710. res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32)
  711. end
  712. local rand = random()
  713. assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits))
  714. assert(rand * 2^floatbits == res)
  715. end
  716. do
  717. -- testing return of 'randomseed'
  718. local x, y = math.randomseed()
  719. local res = math.random(0)
  720. x, y = math.randomseed(x, y) -- should repeat the state
  721. assert(math.random(0) == res)
  722. math.randomseed(x, y) -- again should repeat the state
  723. assert(math.random(0) == res)
  724. -- keep the random seed for following tests
  725. print(string.format("random seeds: %d, %d", x, y))
  726. end
  727. do -- test random for floats
  728. local randbits = math.min(floatbits, 64) -- at most 64 random bits
  729. local mult = 2^randbits -- to make random float into an integral
  730. local counts = {} -- counts for bits
  731. for i = 1, randbits do counts[i] = 0 end
  732. local up = -math.huge
  733. local low = math.huge
  734. local rounds = 100 * randbits -- 100 times for each bit
  735. local totalrounds = 0
  736. ::doagain:: -- will repeat test until we get good statistics
  737. for i = 0, rounds do
  738. local t = random()
  739. assert(0 <= t and t < 1)
  740. up = max(up, t)
  741. low = min(low, t)
  742. assert(t * mult % 1 == 0) -- no extra bits
  743. local bit = i % randbits -- bit to be tested
  744. if (t * 2^bit) % 1 >= 0.5 then -- is bit set?
  745. counts[bit + 1] = counts[bit + 1] + 1 -- increment its count
  746. end
  747. end
  748. totalrounds = totalrounds + rounds
  749. if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then
  750. goto doagain
  751. end
  752. -- all bit counts should be near 50%
  753. local expected = (totalrounds / randbits / 2)
  754. for i = 1, randbits do
  755. if not testnear(counts[i], expected, 0.10) then
  756. goto doagain
  757. end
  758. end
  759. print(string.format("float random range in %d calls: [%f, %f]",
  760. totalrounds, low, up))
  761. end
  762. do -- test random for full integers
  763. local up = 0
  764. local low = 0
  765. local counts = {} -- counts for bits
  766. for i = 1, intbits do counts[i] = 0 end
  767. local rounds = 100 * intbits -- 100 times for each bit
  768. local totalrounds = 0
  769. ::doagain:: -- will repeat test until we get good statistics
  770. for i = 0, rounds do
  771. local t = random(0)
  772. up = max(up, t)
  773. low = min(low, t)
  774. local bit = i % intbits -- bit to be tested
  775. -- increment its count if it is set
  776. counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1)
  777. end
  778. totalrounds = totalrounds + rounds
  779. local lim = maxint >> 10
  780. if not (maxint - up < lim and low - minint < lim) then
  781. goto doagain
  782. end
  783. -- all bit counts should be near 50%
  784. local expected = (totalrounds / intbits / 2)
  785. for i = 1, intbits do
  786. if not testnear(counts[i], expected, 0.10) then
  787. goto doagain
  788. end
  789. end
  790. print(string.format(
  791. "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]",
  792. totalrounds, (minint - low) / minint * 1e6,
  793. (maxint - up) / maxint * 1e6))
  794. end
  795. do
  796. -- test distribution for a dice
  797. local count = {0, 0, 0, 0, 0, 0}
  798. local rep = 200
  799. local totalrep = 0
  800. ::doagain::
  801. for i = 1, rep * 6 do
  802. local r = random(6)
  803. count[r] = count[r] + 1
  804. end
  805. totalrep = totalrep + rep
  806. for i = 1, 6 do
  807. if not testnear(count[i], totalrep, 0.05) then
  808. goto doagain
  809. end
  810. end
  811. end
  812. do
  813. local function aux (x1, x2) -- test random for small intervals
  814. local mark = {}; local count = 0 -- to check that all values appeared
  815. while true do
  816. local t = random(x1, x2)
  817. assert(x1 <= t and t <= x2)
  818. if not mark[t] then -- new value
  819. mark[t] = true
  820. count = count + 1
  821. if count == x2 - x1 + 1 then -- all values appeared; OK
  822. goto ok
  823. end
  824. end
  825. end
  826. ::ok::
  827. end
  828. aux(-10,0)
  829. aux(1, 6)
  830. aux(1, 2)
  831. aux(1, 13)
  832. aux(1, 31)
  833. aux(1, 32)
  834. aux(1, 33)
  835. aux(-10, 10)
  836. aux(-10,-10) -- unit set
  837. aux(minint, minint) -- unit set
  838. aux(maxint, maxint) -- unit set
  839. aux(minint, minint + 9)
  840. aux(maxint - 3, maxint)
  841. end
  842. do
  843. local function aux(p1, p2) -- test random for large intervals
  844. local max = minint
  845. local min = maxint
  846. local n = 100
  847. local mark = {}; local count = 0 -- to count how many different values
  848. ::doagain::
  849. for _ = 1, n do
  850. local t = random(p1, p2)
  851. if not mark[t] then -- new value
  852. assert(p1 <= t and t <= p2)
  853. max = math.max(max, t)
  854. min = math.min(min, t)
  855. mark[t] = true
  856. count = count + 1
  857. end
  858. end
  859. -- at least 80% of values are different
  860. if not (count >= n * 0.8) then
  861. goto doagain
  862. end
  863. -- min and max not too far from formal min and max
  864. local diff = (p2 - p1) >> 4
  865. if not (min < p1 + diff and max > p2 - diff) then
  866. goto doagain
  867. end
  868. end
  869. aux(0, maxint)
  870. aux(1, maxint)
  871. aux(3, maxint // 3)
  872. aux(minint, -1)
  873. aux(minint // 2, maxint // 2)
  874. aux(minint, maxint)
  875. aux(minint + 1, maxint)
  876. aux(minint, maxint - 1)
  877. aux(0, 1 << (intbits - 5))
  878. end
  879. assert(not pcall(random, 1, 2, 3)) -- too many arguments
  880. -- empty interval
  881. assert(not pcall(random, minint + 1, minint))
  882. assert(not pcall(random, maxint, maxint - 1))
  883. assert(not pcall(random, maxint, minint))
  884. print('OK')