math.lua 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. do print("testing ldexp/frexp")
  577. global ipairs
  578. for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do
  579. local m, p = math.frexp(x)
  580. assert(math.ldexp(m, p) == x)
  581. local am = math.abs(m)
  582. assert(m == x or (0.5 <= am and am < 1))
  583. end
  584. end
  585. assert(tonumber(' 1.3e-2 ') == 1.3e-2)
  586. assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
  587. -- testing constant limits
  588. -- 2^23 = 8388608
  589. assert(8388609 + -8388609 == 0)
  590. assert(8388608 + -8388608 == 0)
  591. assert(8388607 + -8388607 == 0)
  592. do -- testing floor & ceil
  593. assert(eqT(math.floor(3.4), 3))
  594. assert(eqT(math.ceil(3.4), 4))
  595. assert(eqT(math.floor(-3.4), -4))
  596. assert(eqT(math.ceil(-3.4), -3))
  597. assert(eqT(math.floor(maxint), maxint))
  598. assert(eqT(math.ceil(maxint), maxint))
  599. assert(eqT(math.floor(minint), minint))
  600. assert(eqT(math.floor(minint + 0.0), minint))
  601. assert(eqT(math.ceil(minint), minint))
  602. assert(eqT(math.ceil(minint + 0.0), minint))
  603. assert(math.floor(1e50) == 1e50)
  604. assert(math.ceil(1e50) == 1e50)
  605. assert(math.floor(-1e50) == -1e50)
  606. assert(math.ceil(-1e50) == -1e50)
  607. for _, p in pairs{31,32,63,64} do
  608. assert(math.floor(2^p) == 2^p)
  609. assert(math.floor(2^p + 0.5) == 2^p)
  610. assert(math.ceil(2^p) == 2^p)
  611. assert(math.ceil(2^p - 0.5) == 2^p)
  612. end
  613. checkerror("number expected", math.floor, {})
  614. checkerror("number expected", math.ceil, print)
  615. assert(eqT(math.tointeger(minint), minint))
  616. assert(eqT(math.tointeger(minint .. ""), minint))
  617. assert(eqT(math.tointeger(maxint), maxint))
  618. assert(eqT(math.tointeger(maxint .. ""), maxint))
  619. assert(eqT(math.tointeger(minint + 0.0), minint))
  620. assert(not math.tointeger(0.0 - minint))
  621. assert(not math.tointeger(math.pi))
  622. assert(not math.tointeger(-math.pi))
  623. assert(math.floor(math.huge) == math.huge)
  624. assert(math.ceil(math.huge) == math.huge)
  625. assert(not math.tointeger(math.huge))
  626. assert(math.floor(-math.huge) == -math.huge)
  627. assert(math.ceil(-math.huge) == -math.huge)
  628. assert(not math.tointeger(-math.huge))
  629. assert(math.tointeger("34.0") == 34)
  630. assert(not math.tointeger("34.3"))
  631. assert(not math.tointeger({}))
  632. assert(not math.tointeger(0/0)) -- NaN
  633. end
  634. -- testing fmod for integers
  635. for i = -6, 6 do
  636. for j = -6, 6 do
  637. if j ~= 0 then
  638. local mi = math.fmod(i, j)
  639. local mf = math.fmod(i + 0.0, j)
  640. assert(mi == mf)
  641. assert(math.type(mi) == 'integer' and math.type(mf) == 'float')
  642. if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then
  643. assert(eqT(mi, i % j))
  644. end
  645. end
  646. end
  647. end
  648. assert(eqT(math.fmod(minint, minint), 0))
  649. assert(eqT(math.fmod(maxint, maxint), 0))
  650. assert(eqT(math.fmod(minint + 1, minint), minint + 1))
  651. assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1))
  652. checkerror("zero", math.fmod, 3, 0)
  653. do -- testing max/min
  654. checkerror("value expected", math.max)
  655. checkerror("value expected", math.min)
  656. assert(eqT(math.max(3), 3))
  657. assert(eqT(math.max(3, 5, 9, 1), 9))
  658. assert(math.max(maxint, 10e60) == 10e60)
  659. assert(eqT(math.max(minint, minint + 1), minint + 1))
  660. assert(eqT(math.min(3), 3))
  661. assert(eqT(math.min(3, 5, 9, 1), 1))
  662. assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2)
  663. assert(math.min(1.9, 1.7, 1.72) == 1.7)
  664. assert(math.min(-10e60, minint) == -10e60)
  665. assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
  666. assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
  667. end
  668. -- testing implicit conversions
  669. local a,b = '10', '20'
  670. assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
  671. assert(a == '10' and b == '20')
  672. do
  673. print("testing -0 and NaN")
  674. global rawset, undef
  675. local mz <const> = -0.0
  676. local z <const> = 0.0
  677. assert(mz == z)
  678. assert(1/mz < 0 and 0 < 1/z)
  679. local a = {[mz] = 1}
  680. assert(a[z] == 1 and a[mz] == 1)
  681. a[z] = 2
  682. assert(a[z] == 2 and a[mz] == 2)
  683. local inf = math.huge * 2 + 1
  684. local mz <const> = -1/inf
  685. local z <const> = 1/inf
  686. assert(mz == z)
  687. assert(1/mz < 0 and 0 < 1/z)
  688. local NaN <const> = inf - inf
  689. assert(NaN ~= NaN)
  690. assert(not (NaN < NaN))
  691. assert(not (NaN <= NaN))
  692. assert(not (NaN > NaN))
  693. assert(not (NaN >= NaN))
  694. assert(not (0 < NaN) and not (NaN < 0))
  695. local NaN1 <const> = 0/0
  696. assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
  697. local a = {}
  698. assert(not pcall(rawset, a, NaN, 1))
  699. assert(a[NaN] == undef)
  700. a[1] = 1
  701. assert(not pcall(rawset, a, NaN, 1))
  702. assert(a[NaN] == undef)
  703. -- strings with same binary representation as 0.0 (might create problems
  704. -- for constant manipulation in the pre-compiler)
  705. 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"
  706. assert(a1 == a2 and a2 == a4 and a1 ~= a3)
  707. assert(a3 == a5)
  708. end
  709. --
  710. -- [[==================================================================
  711. print("testing 'math.random'")
  712. -- -===================================================================
  713. --
  714. local random, max, min = math.random, math.max, math.min
  715. local function testnear (val, ref, tol)
  716. return (math.abs(val - ref) < ref * tol)
  717. end
  718. -- low-level!! For the current implementation of random in Lua,
  719. -- the first call after seed 1007 should return 0x7a7040a5a323c9d6
  720. do
  721. -- all computations should work with 32-bit integers
  722. local h <const> = 0x7a7040a5 -- higher half
  723. local l <const> = 0xa323c9d6 -- lower half
  724. math.randomseed(1007)
  725. -- get the low 'intbits' of the 64-bit expected result
  726. local res = (h << 32 | l) & ~(~0 << intbits)
  727. assert(random(0) == res)
  728. math.randomseed(1007, 0)
  729. -- using higher bits to generate random floats; (the '% 2^32' converts
  730. -- 32-bit integers to floats as unsigned)
  731. local res
  732. if floatbits <= 32 then
  733. -- get all bits from the higher half
  734. res = (h >> (32 - floatbits)) % 2^32
  735. else
  736. -- get 32 bits from the higher half and the rest from the lower half
  737. res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32)
  738. end
  739. local rand = random()
  740. assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits))
  741. assert(rand * 2^floatbits == res)
  742. end
  743. do
  744. -- testing return of 'randomseed'
  745. local x, y = math.randomseed()
  746. local res = math.random(0)
  747. x, y = math.randomseed(x, y) -- should repeat the state
  748. assert(math.random(0) == res)
  749. math.randomseed(x, y) -- again should repeat the state
  750. assert(math.random(0) == res)
  751. -- keep the random seed for following tests
  752. print(string.format("random seeds: %d, %d", x, y))
  753. end
  754. do -- test random for floats
  755. local randbits = math.min(floatbits, 64) -- at most 64 random bits
  756. local mult = 2^randbits -- to make random float into an integral
  757. local counts = {} -- counts for bits
  758. for i = 1, randbits do counts[i] = 0 end
  759. local up = -math.huge
  760. local low = math.huge
  761. local rounds = 100 * randbits -- 100 times for each bit
  762. local totalrounds = 0
  763. ::doagain:: -- will repeat test until we get good statistics
  764. for i = 0, rounds do
  765. local t = random()
  766. assert(0 <= t and t < 1)
  767. up = max(up, t)
  768. low = min(low, t)
  769. assert(t * mult % 1 == 0) -- no extra bits
  770. local bit = i % randbits -- bit to be tested
  771. if (t * 2^bit) % 1 >= 0.5 then -- is bit set?
  772. counts[bit + 1] = counts[bit + 1] + 1 -- increment its count
  773. end
  774. end
  775. totalrounds = totalrounds + rounds
  776. if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then
  777. goto doagain
  778. end
  779. -- all bit counts should be near 50%
  780. local expected = (totalrounds / randbits / 2)
  781. for i = 1, randbits do
  782. if not testnear(counts[i], expected, 0.10) then
  783. goto doagain
  784. end
  785. end
  786. print(string.format("float random range in %d calls: [%f, %f]",
  787. totalrounds, low, up))
  788. end
  789. do -- test random for full integers
  790. local up = 0
  791. local low = 0
  792. local counts = {} -- counts for bits
  793. for i = 1, intbits do counts[i] = 0 end
  794. local rounds = 100 * intbits -- 100 times for each bit
  795. local totalrounds = 0
  796. ::doagain:: -- will repeat test until we get good statistics
  797. for i = 0, rounds do
  798. local t = random(0)
  799. up = max(up, t)
  800. low = min(low, t)
  801. local bit = i % intbits -- bit to be tested
  802. -- increment its count if it is set
  803. counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1)
  804. end
  805. totalrounds = totalrounds + rounds
  806. local lim = maxint >> 10
  807. if not (maxint - up < lim and low - minint < lim) then
  808. goto doagain
  809. end
  810. -- all bit counts should be near 50%
  811. local expected = (totalrounds / intbits / 2)
  812. for i = 1, intbits do
  813. if not testnear(counts[i], expected, 0.10) then
  814. goto doagain
  815. end
  816. end
  817. print(string.format(
  818. "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]",
  819. totalrounds, (minint - low) / minint * 1e6,
  820. (maxint - up) / maxint * 1e6))
  821. end
  822. do
  823. -- test distribution for a dice
  824. local count = {0, 0, 0, 0, 0, 0}
  825. local rep = 200
  826. local totalrep = 0
  827. ::doagain::
  828. for i = 1, rep * 6 do
  829. local r = random(6)
  830. count[r] = count[r] + 1
  831. end
  832. totalrep = totalrep + rep
  833. for i = 1, 6 do
  834. if not testnear(count[i], totalrep, 0.05) then
  835. goto doagain
  836. end
  837. end
  838. end
  839. do
  840. local function aux (x1, x2) -- test random for small intervals
  841. local mark = {}; local count = 0 -- to check that all values appeared
  842. while true do
  843. local t = random(x1, x2)
  844. assert(x1 <= t and t <= x2)
  845. if not mark[t] then -- new value
  846. mark[t] = true
  847. count = count + 1
  848. if count == x2 - x1 + 1 then -- all values appeared; OK
  849. goto ok
  850. end
  851. end
  852. end
  853. ::ok::
  854. end
  855. aux(-10,0)
  856. aux(1, 6)
  857. aux(1, 2)
  858. aux(1, 13)
  859. aux(1, 31)
  860. aux(1, 32)
  861. aux(1, 33)
  862. aux(-10, 10)
  863. aux(-10,-10) -- unit set
  864. aux(minint, minint) -- unit set
  865. aux(maxint, maxint) -- unit set
  866. aux(minint, minint + 9)
  867. aux(maxint - 3, maxint)
  868. end
  869. do
  870. local function aux(p1, p2) -- test random for large intervals
  871. local max = minint
  872. local min = maxint
  873. local n = 100
  874. local mark = {}; local count = 0 -- to count how many different values
  875. ::doagain::
  876. for _ = 1, n do
  877. local t = random(p1, p2)
  878. if not mark[t] then -- new value
  879. assert(p1 <= t and t <= p2)
  880. max = math.max(max, t)
  881. min = math.min(min, t)
  882. mark[t] = true
  883. count = count + 1
  884. end
  885. end
  886. -- at least 80% of values are different
  887. if not (count >= n * 0.8) then
  888. goto doagain
  889. end
  890. -- min and max not too far from formal min and max
  891. local diff = (p2 - p1) >> 4
  892. if not (min < p1 + diff and max > p2 - diff) then
  893. goto doagain
  894. end
  895. end
  896. aux(0, maxint)
  897. aux(1, maxint)
  898. aux(3, maxint // 3)
  899. aux(minint, -1)
  900. aux(minint // 2, maxint // 2)
  901. aux(minint, maxint)
  902. aux(minint + 1, maxint)
  903. aux(minint, maxint - 1)
  904. aux(0, 1 << (intbits - 5))
  905. end
  906. assert(not pcall(random, 1, 2, 3)) -- too many arguments
  907. -- empty interval
  908. assert(not pcall(random, minint + 1, minint))
  909. assert(not pcall(random, maxint, maxint - 1))
  910. assert(not pcall(random, maxint, minint))
  911. -- ]]==================================================================
  912. --
  913. -- [[==================================================================
  914. print("testing precision of 'tostring'")
  915. -- -===================================================================
  916. --
  917. -- number of decimal digits supported by float precision
  918. local decdig = math.floor(floatbits * math.log(2, 10))
  919. print(string.format(" %d-digit float numbers with full precision",
  920. decdig))
  921. -- number of decimal digits supported by integer precision
  922. local Idecdig = math.floor(math.log(maxint, 10))
  923. print(string.format(" %d-digit integer numbers with full precision",
  924. Idecdig))
  925. do
  926. -- Any number should print so that reading it back gives itself:
  927. -- tonumber(tostring(x)) == x
  928. -- Mersenne fractions
  929. local p = 1.0
  930. for i = 1, maxexp do
  931. p = p + p
  932. local x = 1 / (p - 1)
  933. assert(x == tonumber(tostring(x)))
  934. end
  935. -- some random numbers in [0,1)
  936. for i = 1, 100 do
  937. local x = math.random()
  938. assert(x == tonumber(tostring(x)))
  939. end
  940. -- different numbers should print differently.
  941. -- check pairs of floats with minimum detectable difference
  942. local p = floatbits - 1
  943. global ipairs
  944. for i = 1, maxexp - 1 do
  945. for _, i in ipairs{-i, i} do
  946. local x = 2^i
  947. local diff = 2^(i - p) -- least significant bit for 'x'
  948. local y = x + diff
  949. local fy = tostring(y)
  950. assert(x ~= y and tostring(x) ~= fy)
  951. assert(tonumber(fy) == y)
  952. end
  953. end
  954. -- "reasonable" numerals should be printed like themselves
  955. -- create random float numerals with 5 digits, with a decimal point
  956. -- inserted in all places. (With more than 5, things like "0.00001"
  957. -- reformats like "1e-5".)
  958. for i = 1, 1000 do
  959. -- random numeral with 5 digits
  960. local x = string.format("%.5d", math.random(0, 99999))
  961. for i = 2, #x do
  962. -- insert decimal point at position 'i'
  963. local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1)
  964. y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros
  965. assert(y == tostring(tonumber(y)))
  966. end
  967. end
  968. -- all-random floats
  969. local Fsz = string.packsize("n") -- size of floats in bytes
  970. for i = 1, 400 do
  971. local s = string.pack("j", math.random(0)) -- a random string of bits
  972. while #s < Fsz do -- make 's' long enough
  973. s = s .. string.pack("j", math.random(0))
  974. end
  975. local n = string.unpack("n", s) -- read 's' as a float
  976. s = tostring(n)
  977. if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf
  978. assert(tonumber(s) == n)
  979. end
  980. end
  981. end
  982. -- ]]==================================================================
  983. print('OK')