math.lua 32 KB

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