math.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. -- $Id: testes/math.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing numbers and math lib")
  4. local <const> minint = math.mininteger
  5. local <const> maxint = math.maxinteger
  6. local <const> 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(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(math.tointeger(0.0 - minint) == nil)
  599. assert(math.tointeger(math.pi) == nil)
  600. assert(math.tointeger(-math.pi) == nil)
  601. assert(math.floor(math.huge) == math.huge)
  602. assert(math.ceil(math.huge) == math.huge)
  603. assert(math.tointeger(math.huge) == nil)
  604. assert(math.floor(-math.huge) == -math.huge)
  605. assert(math.ceil(-math.huge) == -math.huge)
  606. assert(math.tointeger(-math.huge) == nil)
  607. assert(math.tointeger("34.0") == 34)
  608. assert(math.tointeger("34.3") == nil)
  609. assert(math.tointeger({}) == nil)
  610. assert(math.tointeger(0/0) == nil) -- 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 convertions
  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, z = -0.0, 0.0
  653. assert(mz == z)
  654. assert(1/mz < 0 and 0 < 1/z)
  655. local a = {[mz] = 1}
  656. assert(a[z] == 1 and a[mz] == 1)
  657. a[z] = 2
  658. assert(a[z] == 2 and a[mz] == 2)
  659. local inf = math.huge * 2 + 1
  660. mz, z = -1/inf, 1/inf
  661. assert(mz == z)
  662. assert(1/mz < 0 and 0 < 1/z)
  663. local NaN = inf - inf
  664. assert(NaN ~= NaN)
  665. assert(not (NaN < NaN))
  666. assert(not (NaN <= NaN))
  667. assert(not (NaN > NaN))
  668. assert(not (NaN >= NaN))
  669. assert(not (0 < NaN) and not (NaN < 0))
  670. local NaN1 = 0/0
  671. assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
  672. local a = {}
  673. assert(not pcall(rawset, a, NaN, 1))
  674. assert(a[NaN] == undef)
  675. a[1] = 1
  676. assert(not pcall(rawset, a, NaN, 1))
  677. assert(a[NaN] == undef)
  678. -- strings with same binary representation as 0.0 (might create problems
  679. -- for constant manipulation in the pre-compiler)
  680. 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"
  681. assert(a1 == a2 and a2 == a4 and a1 ~= a3)
  682. assert(a3 == a5)
  683. end
  684. print("testing 'math.random'")
  685. local random, max, min = math.random, math.max, math.min
  686. local function testnear (val, ref, tol)
  687. return (math.abs(val - ref) < ref * tol)
  688. end
  689. -- low-level!! For the current implementation of random in Lua,
  690. -- the first call after seed 1007 should return 0x7a7040a5a323c9d6
  691. do
  692. -- all computations assume at most 32-bit integers
  693. local h = 0x7a7040a5 -- higher half
  694. local l = 0xa323c9d6 -- lower half
  695. math.randomseed(1007)
  696. -- get the low 'intbits' of the 64-bit expected result
  697. local res = (h << 32 | l) & ~(~0 << intbits)
  698. assert(random(0) == res)
  699. math.randomseed(1007, 0)
  700. -- using higher bits to generate random floats; (the '% 2^32' converts
  701. -- 32-bit integers to floats as unsigned)
  702. local res
  703. if floatbits <= 32 then
  704. -- get all bits from the higher half
  705. res = (h >> (32 - floatbits)) % 2^32
  706. else
  707. -- get 32 bits from the higher half and the rest from the lower half
  708. res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32)
  709. end
  710. local rand = random()
  711. assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits))
  712. assert(rand * 2^floatbits == res)
  713. end
  714. math.randomseed()
  715. do -- test random for floats
  716. local randbits = math.min(floatbits, 64) -- at most 64 random bits
  717. local mult = 2^randbits -- to make random float into an integral
  718. local counts = {} -- counts for bits
  719. for i = 1, randbits do counts[i] = 0 end
  720. local up = -math.huge
  721. local low = math.huge
  722. local rounds = 100 * randbits -- 100 times for each bit
  723. local totalrounds = 0
  724. ::doagain:: -- will repeat test until we get good statistics
  725. for i = 0, rounds do
  726. local t = random()
  727. assert(0 <= t and t < 1)
  728. up = max(up, t)
  729. low = min(low, t)
  730. assert(t * mult % 1 == 0) -- no extra bits
  731. local bit = i % randbits -- bit to be tested
  732. if (t * 2^bit) % 1 >= 0.5 then -- is bit set?
  733. counts[bit + 1] = counts[bit + 1] + 1 -- increment its count
  734. end
  735. end
  736. totalrounds = totalrounds + rounds
  737. if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then
  738. goto doagain
  739. end
  740. -- all bit counts should be near 50%
  741. local expected = (totalrounds / randbits / 2)
  742. for i = 1, randbits do
  743. if not testnear(counts[i], expected, 0.10) then
  744. goto doagain
  745. end
  746. end
  747. print(string.format("float random range in %d calls: [%f, %f]",
  748. totalrounds, low, up))
  749. end
  750. do -- test random for full integers
  751. local up = 0
  752. local low = 0
  753. local counts = {} -- counts for bits
  754. for i = 1, intbits do counts[i] = 0 end
  755. local rounds = 100 * intbits -- 100 times for each bit
  756. local totalrounds = 0
  757. ::doagain:: -- will repeat test until we get good statistics
  758. for i = 0, rounds do
  759. local t = random(0)
  760. up = max(up, t)
  761. low = min(low, t)
  762. local bit = i % intbits -- bit to be tested
  763. -- increment its count if it is set
  764. counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1)
  765. end
  766. totalrounds = totalrounds + rounds
  767. local lim = maxint >> 10
  768. if not (maxint - up < lim and low - minint < lim) then
  769. goto doagain
  770. end
  771. -- all bit counts should be near 50%
  772. local expected = (totalrounds / intbits / 2)
  773. for i = 1, intbits do
  774. if not testnear(counts[i], expected, 0.10) then
  775. goto doagain
  776. end
  777. end
  778. print(string.format(
  779. "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]",
  780. totalrounds, (minint - low) / minint * 1e6,
  781. (maxint - up) / maxint * 1e6))
  782. end
  783. do
  784. -- test distribution for a dice
  785. local count = {0, 0, 0, 0, 0, 0}
  786. local rep = 200
  787. local totalrep = 0
  788. ::doagain::
  789. for i = 1, rep * 6 do
  790. local r = random(6)
  791. count[r] = count[r] + 1
  792. end
  793. totalrep = totalrep + rep
  794. for i = 1, 6 do
  795. if not testnear(count[i], totalrep, 0.05) then
  796. goto doagain
  797. end
  798. end
  799. end
  800. do
  801. local function aux (x1, x2) -- test random for small intervals
  802. local mark = {}; local count = 0 -- to check that all values appeared
  803. while true do
  804. local t = random(x1, x2)
  805. assert(x1 <= t and t <= x2)
  806. if not mark[t] then -- new value
  807. mark[t] = true
  808. count = count + 1
  809. if count == x2 - x1 + 1 then -- all values appeared; OK
  810. goto ok
  811. end
  812. end
  813. end
  814. ::ok::
  815. end
  816. aux(-10,0)
  817. aux(1, 6)
  818. aux(1, 2)
  819. aux(1, 32)
  820. aux(-10, 10)
  821. aux(-10,-10) -- unit set
  822. aux(minint, minint) -- unit set
  823. aux(maxint, maxint) -- unit set
  824. aux(minint, minint + 9)
  825. aux(maxint - 3, maxint)
  826. end
  827. do
  828. local function aux(p1, p2) -- test random for large intervals
  829. local max = minint
  830. local min = maxint
  831. local n = 100
  832. local mark = {}; local count = 0 -- to count how many different values
  833. ::doagain::
  834. for _ = 1, n do
  835. local t = random(p1, p2)
  836. if not mark[t] then -- new value
  837. assert(p1 <= t and t <= p2)
  838. max = math.max(max, t)
  839. min = math.min(min, t)
  840. mark[t] = true
  841. count = count + 1
  842. end
  843. end
  844. -- at least 80% of values are different
  845. if not (count >= n * 0.8) then
  846. goto doagain
  847. end
  848. -- min and max not too far from formal min and max
  849. local diff = (p2 - p1) >> 4
  850. if not (min < p1 + diff and max > p2 - diff) then
  851. goto doagain
  852. end
  853. end
  854. aux(0, maxint)
  855. aux(1, maxint)
  856. aux(minint, -1)
  857. aux(minint // 2, maxint // 2)
  858. aux(minint, maxint)
  859. aux(minint + 1, maxint)
  860. aux(minint, maxint - 1)
  861. aux(0, 1 << (intbits - 5))
  862. end
  863. assert(not pcall(random, 1, 2, 3)) -- too many arguments
  864. -- empty interval
  865. assert(not pcall(random, minint + 1, minint))
  866. assert(not pcall(random, maxint, maxint - 1))
  867. assert(not pcall(random, maxint, minint))
  868. print('OK')