math.lua 32 KB

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