math.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. -- $Id: testes/math.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing numbers and math lib")
  4. local minint = math.mininteger
  5. local maxint = math.maxinteger
  6. local intbits = 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 math.type("10") == nil)
  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. 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. 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. 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 = 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(tonumber("") == nil)
  320. assert(tonumber(" ") == nil)
  321. assert(tonumber("-") == nil)
  322. assert(tonumber(" -0x ") == nil)
  323. assert(tonumber{} == nil)
  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(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
  328. tonumber'1e' == nil and tonumber'1.0e+' == nil and
  329. tonumber'.' == nil)
  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(f(tonumber('fFfa', 15)) == nil)
  377. assert(f(tonumber('099', 8)) == nil)
  378. assert(f(tonumber('1\0', 2)) == nil)
  379. assert(f(tonumber('', 8)) == nil)
  380. assert(f(tonumber(' ', 9)) == nil)
  381. assert(f(tonumber(' ', 9)) == nil)
  382. assert(f(tonumber('0xf', 10)) == nil)
  383. assert(f(tonumber('inf')) == nil)
  384. assert(f(tonumber(' INF ')) == nil)
  385. assert(f(tonumber('Nan')) == nil)
  386. assert(f(tonumber('nan')) == nil)
  387. assert(f(tonumber(' ')) == nil)
  388. assert(f(tonumber('')) == nil)
  389. assert(f(tonumber('1 a')) == nil)
  390. assert(f(tonumber('1 a', 2)) == nil)
  391. assert(f(tonumber('1\0')) == nil)
  392. assert(f(tonumber('1 \0')) == nil)
  393. assert(f(tonumber('1\0 ')) == nil)
  394. assert(f(tonumber('e1')) == nil)
  395. assert(f(tonumber('e 1')) == nil)
  396. assert(f(tonumber(' 3.4.5 ')) == nil)
  397. -- testing 'tonumber' for invalid hexadecimal formats
  398. assert(tonumber('0x') == nil)
  399. assert(tonumber('x') == nil)
  400. assert(tonumber('x3') == nil)
  401. assert(tonumber('0x3.3.3') == nil) -- two decimal points
  402. assert(tonumber('00x2') == nil)
  403. assert(tonumber('0x 2') == nil)
  404. assert(tonumber('0 x2') == nil)
  405. assert(tonumber('23x') == nil)
  406. assert(tonumber('- 0xaa') == nil)
  407. assert(tonumber('-0xaaP ') == nil) -- no exponent
  408. assert(tonumber('0x0.51p') == nil)
  409. assert(tonumber('0x5p+-2') == nil)
  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(math.pi - math.pi % 1 == 3)
  454. assert(math.pi - math.pi % 0.001 == 3.141)
  455. assert(eqT(minint % minint, 0))
  456. assert(eqT(maxint % maxint, 0))
  457. assert((minint + 1) % minint == minint + 1)
  458. assert((maxint - 1) % maxint == maxint - 1)
  459. assert(minint % maxint == maxint - 1)
  460. assert(minint % -1 == 0)
  461. assert(minint % -2 == 0)
  462. assert(maxint % -2 == -1)
  463. -- non-portable tests because Windows C library cannot compute
  464. -- fmod(1, huge) correctly
  465. if not _port then
  466. local function anan (x) assert(isNaN(x)) end -- assert Not a Number
  467. anan(0.0 % 0)
  468. anan(1.3 % 0)
  469. anan(math.huge % 1)
  470. anan(math.huge % 1e30)
  471. anan(-math.huge % 1e30)
  472. anan(-math.huge % -1e30)
  473. assert(1 % math.huge == 1)
  474. assert(1e30 % math.huge == 1e30)
  475. assert(1e30 % -math.huge == -math.huge)
  476. assert(-1 % math.huge == math.huge)
  477. assert(-1 % -math.huge == -1)
  478. end
  479. -- testing unsigned comparisons
  480. assert(math.ult(3, 4))
  481. assert(not math.ult(4, 4))
  482. assert(math.ult(-2, -1))
  483. assert(math.ult(2, -1))
  484. assert(not math.ult(-2, -2))
  485. assert(math.ult(maxint, minint))
  486. assert(not math.ult(minint, maxint))
  487. assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1))
  488. assert(eq(math.tan(math.pi/4), 1))
  489. assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0))
  490. assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and
  491. eq(math.asin(1), math.pi/2))
  492. assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2))
  493. assert(math.abs(-10.43) == 10.43)
  494. assert(eqT(math.abs(minint), minint))
  495. assert(eqT(math.abs(maxint), maxint))
  496. assert(eqT(math.abs(-maxint), maxint))
  497. assert(eq(math.atan(1,0), math.pi/2))
  498. assert(math.fmod(10,3) == 1)
  499. assert(eq(math.sqrt(10)^2, 10))
  500. assert(eq(math.log(2, 10), math.log(2)/math.log(10)))
  501. assert(eq(math.log(2, 2), 1))
  502. assert(eq(math.log(9, 3), 2))
  503. assert(eq(math.exp(0), 1))
  504. assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
  505. assert(tonumber(' 1.3e-2 ') == 1.3e-2)
  506. assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
  507. -- testing constant limits
  508. -- 2^23 = 8388608
  509. assert(8388609 + -8388609 == 0)
  510. assert(8388608 + -8388608 == 0)
  511. assert(8388607 + -8388607 == 0)
  512. do -- testing floor & ceil
  513. assert(eqT(math.floor(3.4), 3))
  514. assert(eqT(math.ceil(3.4), 4))
  515. assert(eqT(math.floor(-3.4), -4))
  516. assert(eqT(math.ceil(-3.4), -3))
  517. assert(eqT(math.floor(maxint), maxint))
  518. assert(eqT(math.ceil(maxint), maxint))
  519. assert(eqT(math.floor(minint), minint))
  520. assert(eqT(math.floor(minint + 0.0), minint))
  521. assert(eqT(math.ceil(minint), minint))
  522. assert(eqT(math.ceil(minint + 0.0), minint))
  523. assert(math.floor(1e50) == 1e50)
  524. assert(math.ceil(1e50) == 1e50)
  525. assert(math.floor(-1e50) == -1e50)
  526. assert(math.ceil(-1e50) == -1e50)
  527. for _, p in pairs{31,32,63,64} do
  528. assert(math.floor(2^p) == 2^p)
  529. assert(math.floor(2^p + 0.5) == 2^p)
  530. assert(math.ceil(2^p) == 2^p)
  531. assert(math.ceil(2^p - 0.5) == 2^p)
  532. end
  533. checkerror("number expected", math.floor, {})
  534. checkerror("number expected", math.ceil, print)
  535. assert(eqT(math.tointeger(minint), minint))
  536. assert(eqT(math.tointeger(minint .. ""), minint))
  537. assert(eqT(math.tointeger(maxint), maxint))
  538. assert(eqT(math.tointeger(maxint .. ""), maxint))
  539. assert(eqT(math.tointeger(minint + 0.0), minint))
  540. assert(math.tointeger(0.0 - minint) == nil)
  541. assert(math.tointeger(math.pi) == nil)
  542. assert(math.tointeger(-math.pi) == nil)
  543. assert(math.floor(math.huge) == math.huge)
  544. assert(math.ceil(math.huge) == math.huge)
  545. assert(math.tointeger(math.huge) == nil)
  546. assert(math.floor(-math.huge) == -math.huge)
  547. assert(math.ceil(-math.huge) == -math.huge)
  548. assert(math.tointeger(-math.huge) == nil)
  549. assert(math.tointeger("34.0") == 34)
  550. assert(math.tointeger("34.3") == nil)
  551. assert(math.tointeger({}) == nil)
  552. assert(math.tointeger(0/0) == nil) -- NaN
  553. end
  554. -- testing fmod for integers
  555. for i = -6, 6 do
  556. for j = -6, 6 do
  557. if j ~= 0 then
  558. local mi = math.fmod(i, j)
  559. local mf = math.fmod(i + 0.0, j)
  560. assert(mi == mf)
  561. assert(math.type(mi) == 'integer' and math.type(mf) == 'float')
  562. if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then
  563. assert(eqT(mi, i % j))
  564. end
  565. end
  566. end
  567. end
  568. assert(eqT(math.fmod(minint, minint), 0))
  569. assert(eqT(math.fmod(maxint, maxint), 0))
  570. assert(eqT(math.fmod(minint + 1, minint), minint + 1))
  571. assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1))
  572. checkerror("zero", math.fmod, 3, 0)
  573. do -- testing max/min
  574. checkerror("value expected", math.max)
  575. checkerror("value expected", math.min)
  576. assert(eqT(math.max(3), 3))
  577. assert(eqT(math.max(3, 5, 9, 1), 9))
  578. assert(math.max(maxint, 10e60) == 10e60)
  579. assert(eqT(math.max(minint, minint + 1), minint + 1))
  580. assert(eqT(math.min(3), 3))
  581. assert(eqT(math.min(3, 5, 9, 1), 1))
  582. assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2)
  583. assert(math.min(1.9, 1.7, 1.72) == 1.7)
  584. assert(math.min(-10e60, minint) == -10e60)
  585. assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
  586. assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
  587. end
  588. -- testing implicit convertions
  589. local a,b = '10', '20'
  590. assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
  591. assert(a == '10' and b == '20')
  592. do
  593. print("testing -0 and NaN")
  594. local mz, z = -0.0, 0.0
  595. assert(mz == z)
  596. assert(1/mz < 0 and 0 < 1/z)
  597. local a = {[mz] = 1}
  598. assert(a[z] == 1 and a[mz] == 1)
  599. a[z] = 2
  600. assert(a[z] == 2 and a[mz] == 2)
  601. local inf = math.huge * 2 + 1
  602. mz, z = -1/inf, 1/inf
  603. assert(mz == z)
  604. assert(1/mz < 0 and 0 < 1/z)
  605. local NaN = inf - inf
  606. assert(NaN ~= NaN)
  607. assert(not (NaN < NaN))
  608. assert(not (NaN <= NaN))
  609. assert(not (NaN > NaN))
  610. assert(not (NaN >= NaN))
  611. assert(not (0 < NaN) and not (NaN < 0))
  612. local NaN1 = 0/0
  613. assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
  614. local a = {}
  615. assert(not pcall(rawset, a, NaN, 1))
  616. assert(a[NaN] == undef)
  617. a[1] = 1
  618. assert(not pcall(rawset, a, NaN, 1))
  619. assert(a[NaN] == undef)
  620. -- strings with same binary representation as 0.0 (might create problems
  621. -- for constant manipulation in the pre-compiler)
  622. 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"
  623. assert(a1 == a2 and a2 == a4 and a1 ~= a3)
  624. assert(a3 == a5)
  625. end
  626. print("testing 'math.random'")
  627. local random, max, min = math.random, math.max, math.min
  628. local function testnear (val, ref, tol)
  629. return (math.abs(val - ref) < ref * tol)
  630. end
  631. -- low-level!! For the current implementation of random in Lua,
  632. -- the first call after seed 1007 should return 0x7a7040a5a323c9d6
  633. do
  634. -- all computations assume at most 32-bit integers
  635. local h = 0x7a7040a5 -- higher half
  636. local l = 0xa323c9d6 -- lower half
  637. math.randomseed(1007)
  638. -- get the low 'intbits' of the 64-bit expected result
  639. local res = (h << 32 | l) & ~(~0 << intbits)
  640. assert(random(0) == res)
  641. math.randomseed(1007, 0)
  642. -- using lower bits to generate random floats; (the '% 2^32' converts
  643. -- 32-bit integers to floats as unsigned)
  644. local res
  645. if floatbits <= 32 then
  646. -- get all bits from the lower half
  647. res = (l & ~(~0 << floatbits)) % 2^32
  648. else
  649. -- get 32 bits from the lower half and the rest from the higher half
  650. res = ((h & ~(~0 << (floatbits - 32))) % 2^32) * 2^32 + (l % 2^32)
  651. end
  652. assert(random() * 2^floatbits == res)
  653. end
  654. math.randomseed(0, os.time())
  655. do -- test random for floats
  656. local randbits = math.min(floatbits, 64) -- at most 64 random bits
  657. local mult = 2^randbits -- to make random float into an integral
  658. local counts = {} -- counts for bits
  659. for i = 1, randbits do counts[i] = 0 end
  660. local up = -math.huge
  661. local low = math.huge
  662. local rounds = 100 * randbits -- 100 times for each bit
  663. local totalrounds = 0
  664. ::doagain:: -- will repeat test until we get good statistics
  665. for i = 0, rounds do
  666. local t = random()
  667. assert(0 <= t and t < 1)
  668. up = max(up, t)
  669. low = min(low, t)
  670. assert(t * mult % 1 == 0) -- no extra bits
  671. local bit = i % randbits -- bit to be tested
  672. if (t * 2^bit) % 1 >= 0.5 then -- is bit set?
  673. counts[bit + 1] = counts[bit + 1] + 1 -- increment its count
  674. end
  675. end
  676. totalrounds = totalrounds + rounds
  677. if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then
  678. goto doagain
  679. end
  680. -- all bit counts should be near 50%
  681. local expected = (totalrounds / randbits / 2)
  682. for i = 1, randbits do
  683. if not testnear(counts[i], expected, 0.10) then
  684. goto doagain
  685. end
  686. end
  687. print(string.format("float random range in %d calls: [%f, %f]",
  688. totalrounds, low, up))
  689. end
  690. do -- test random for full integers
  691. local up = 0
  692. local low = 0
  693. local counts = {} -- counts for bits
  694. for i = 1, intbits do counts[i] = 0 end
  695. local rounds = 100 * intbits -- 100 times for each bit
  696. local totalrounds = 0
  697. ::doagain:: -- will repeat test until we get good statistics
  698. for i = 0, rounds do
  699. local t = random(0)
  700. up = max(up, t)
  701. low = min(low, t)
  702. local bit = i % intbits -- bit to be tested
  703. -- increment its count if it is set
  704. counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1)
  705. end
  706. totalrounds = totalrounds + rounds
  707. local lim = maxint >> 10
  708. if not (maxint - up < lim and low - minint < lim) then
  709. goto doagain
  710. end
  711. -- all bit counts should be near 50%
  712. local expected = (totalrounds / intbits / 2)
  713. for i = 1, intbits do
  714. if not testnear(counts[i], expected, 0.10) then
  715. goto doagain
  716. end
  717. end
  718. print(string.format(
  719. "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]",
  720. totalrounds, (minint - low) / minint * 1e6,
  721. (maxint - up) / maxint * 1e6))
  722. end
  723. do
  724. -- test distribution for a dice
  725. local count = {0, 0, 0, 0, 0, 0}
  726. local rep = 200
  727. local totalrep = 0
  728. ::doagain::
  729. for i = 1, rep * 6 do
  730. local r = random(6)
  731. count[r] = count[r] + 1
  732. end
  733. totalrep = totalrep + rep
  734. for i = 1, 6 do
  735. if not testnear(count[i], totalrep, 0.05) then
  736. goto doagain
  737. end
  738. end
  739. end
  740. do
  741. local function aux (x1, x2) -- test random for small intervals
  742. local mark = {}; local count = 0 -- to check that all values appeared
  743. while true do
  744. local t = random(x1, x2)
  745. assert(x1 <= t and t <= x2)
  746. if not mark[t] then -- new value
  747. mark[t] = true
  748. count = count + 1
  749. if count == x2 - x1 + 1 then -- all values appeared; OK
  750. goto ok
  751. end
  752. end
  753. end
  754. ::ok::
  755. end
  756. aux(-10,0)
  757. aux(1, 6)
  758. aux(1, 2)
  759. aux(1, 32)
  760. aux(-10, 10)
  761. aux(-10,-10) -- unit set
  762. aux(minint, minint) -- unit set
  763. aux(maxint, maxint) -- unit set
  764. aux(minint, minint + 9)
  765. aux(maxint - 3, maxint)
  766. end
  767. do
  768. local function aux(p1, p2) -- test random for large intervals
  769. local max = minint
  770. local min = maxint
  771. local n = 100
  772. local mark = {}; local count = 0 -- to count how many different values
  773. ::doagain::
  774. for _ = 1, n do
  775. local t = random(p1, p2)
  776. if not mark[t] then -- new value
  777. assert(p1 <= t and t <= p2)
  778. max = math.max(max, t)
  779. min = math.min(min, t)
  780. mark[t] = true
  781. count = count + 1
  782. end
  783. end
  784. -- at least 80% of values are different
  785. if not (count >= n * 0.8) then
  786. goto doagain
  787. end
  788. -- min and max not too far from formal min and max
  789. local diff = (p2 - p1) >> 4
  790. if not (min < p1 + diff and max > p2 - diff) then
  791. goto doagain
  792. end
  793. end
  794. aux(0, maxint)
  795. aux(1, maxint)
  796. aux(minint, -1)
  797. aux(minint // 2, maxint // 2)
  798. aux(minint, maxint)
  799. aux(minint + 1, maxint)
  800. aux(minint, maxint - 1)
  801. aux(0, 1 << (intbits - 5))
  802. end
  803. assert(not pcall(random, 1, 2, 3)) -- too many arguments
  804. -- empty interval
  805. assert(not pcall(random, minint + 1, minint))
  806. assert(not pcall(random, maxint, maxint - 1))
  807. assert(not pcall(random, maxint, minint))
  808. print('OK')