test_float_func.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. " test float functions
  2. source check.vim
  3. CheckFeature float
  4. source vim9.vim
  5. func Test_abs()
  6. call assert_equal('1.23', string(abs(1.23)))
  7. call assert_equal('1.23', string(abs(-1.23)))
  8. eval -1.23->abs()->string()->assert_equal('1.23')
  9. call assert_equal('0.0', string(abs(0.0)))
  10. call assert_equal('0.0', string(abs(1.0/(1.0/0.0))))
  11. call assert_equal('0.0', string(abs(-1.0/(1.0/0.0))))
  12. call assert_equal("str2float('inf')", string(abs(1.0/0.0)))
  13. call assert_equal("str2float('inf')", string(abs(-1.0/0.0)))
  14. call assert_equal("str2float('nan')", string(abs(0.0/0.0)))
  15. call assert_equal('12', string(abs('12abc')))
  16. call assert_equal('12', string(abs('-12abc')))
  17. call assert_fails("call abs([])", 'E745:')
  18. call assert_fails("call abs({})", 'E728:')
  19. call assert_fails("call abs(function('string'))", 'E703:')
  20. endfunc
  21. func Test_sqrt()
  22. call assert_equal('0.0', string(sqrt(0.0)))
  23. call assert_equal('1.414214', string(sqrt(2.0)))
  24. eval 2.0->sqrt()->string()->assert_equal('1.414214')
  25. call assert_equal("str2float('inf')", string(sqrt(1.0/0.0)))
  26. call assert_equal("str2float('nan')", string(sqrt(-1.0)))
  27. call assert_equal("str2float('nan')", string(sqrt(0.0/0.0)))
  28. call assert_fails('call sqrt("")', 'E808:')
  29. endfunc
  30. func Test_log()
  31. call assert_equal('0.0', string(log(1.0)))
  32. call assert_equal('-0.693147', string(log(0.5)))
  33. eval 0.5->log()->string()->assert_equal('-0.693147')
  34. call assert_equal("-str2float('inf')", string(log(0.0)))
  35. call assert_equal("str2float('nan')", string(log(-1.0)))
  36. call assert_equal("str2float('inf')", string(log(1.0/0.0)))
  37. call assert_equal("str2float('nan')", string(log(0.0/0.0)))
  38. call assert_fails('call log("")', 'E808:')
  39. endfunc
  40. func Test_log10()
  41. call assert_equal('0.0', string(log10(1.0)))
  42. call assert_equal('2.0', string(log10(100.0)))
  43. call assert_equal('2.079181', string(log10(120.0)))
  44. eval 120.0->log10()->string()->assert_equal('2.079181')
  45. call assert_equal("-str2float('inf')", string(log10(0.0)))
  46. call assert_equal("str2float('nan')", string(log10(-1.0)))
  47. call assert_equal("str2float('inf')", string(log10(1.0/0.0)))
  48. call assert_equal("str2float('nan')", string(log10(0.0/0.0)))
  49. call assert_fails('call log10("")', 'E808:')
  50. endfunc
  51. func Test_exp()
  52. call assert_equal('1.0', string(exp(0.0)))
  53. call assert_equal('7.389056', string(exp(2.0)))
  54. call assert_equal('0.367879', string(exp(-1.0)))
  55. eval -1.0->exp()->string()->assert_equal('0.367879')
  56. call assert_equal("str2float('inf')", string(exp(1.0/0.0)))
  57. call assert_equal('0.0', string(exp(-1.0/0.0)))
  58. call assert_equal("str2float('nan')", string(exp(0.0/0.0)))
  59. call assert_fails('call exp("")', 'E808:')
  60. endfunc
  61. func Test_sin()
  62. call assert_equal('0.0', string(sin(0.0)))
  63. call assert_equal('0.841471', string(sin(1.0)))
  64. call assert_equal('-0.479426', string(sin(-0.5)))
  65. eval -0.5->sin()->string()->assert_equal('-0.479426')
  66. call assert_equal("str2float('nan')", string(sin(0.0/0.0)))
  67. call assert_equal("str2float('nan')", string(sin(1.0/0.0)))
  68. call assert_equal('0.0', string(sin(1.0/(1.0/0.0))))
  69. call assert_equal('-0.0', string(sin(-1.0/(1.0/0.0))))
  70. call assert_fails('call sin("")', 'E808:')
  71. endfunc
  72. func Test_asin()
  73. call assert_equal('0.0', string(asin(0.0)))
  74. call assert_equal('1.570796', string(asin(1.0)))
  75. eval 1.0->asin()->string()->assert_equal('1.570796')
  76. call assert_equal('-0.523599', string(asin(-0.5)))
  77. call assert_equal("str2float('nan')", string(asin(1.1)))
  78. call assert_equal("str2float('nan')", string(asin(1.0/0.0)))
  79. call assert_equal("str2float('nan')", string(asin(0.0/0.0)))
  80. call assert_fails('call asin("")', 'E808:')
  81. endfunc
  82. func Test_sinh()
  83. call assert_equal('0.0', string(sinh(0.0)))
  84. call assert_equal('0.521095', string(sinh(0.5)))
  85. call assert_equal('-1.026517', string(sinh(-0.9)))
  86. eval -0.9->sinh()->string()->assert_equal('-1.026517')
  87. call assert_equal("str2float('inf')", string(sinh(1.0/0.0)))
  88. call assert_equal("-str2float('inf')", string(sinh(-1.0/0.0)))
  89. call assert_equal("str2float('nan')", string(sinh(0.0/0.0)))
  90. call assert_fails('call sinh("")', 'E808:')
  91. endfunc
  92. func Test_cos()
  93. call assert_equal('1.0', string(cos(0.0)))
  94. call assert_equal('0.540302', string(cos(1.0)))
  95. call assert_equal('0.877583', string(cos(-0.5)))
  96. eval -0.5->cos()->string()->assert_equal('0.877583')
  97. call assert_equal("str2float('nan')", string(cos(0.0/0.0)))
  98. call assert_equal("str2float('nan')", string(cos(1.0/0.0)))
  99. call assert_fails('call cos("")', 'E808:')
  100. endfunc
  101. func Test_acos()
  102. call assert_equal('1.570796', string(acos(0.0)))
  103. call assert_equal('0.0', string(acos(1.0)))
  104. call assert_equal('3.141593', string(acos(-1.0)))
  105. eval -1.0->acos()->string()->assert_equal('3.141593')
  106. call assert_equal('2.094395', string(acos(-0.5)))
  107. call assert_equal("str2float('nan')", string(acos(1.1)))
  108. call assert_equal("str2float('nan')", string(acos(1.0/0.0)))
  109. call assert_equal("str2float('nan')", string(acos(0.0/0.0)))
  110. call assert_fails('call acos("")', 'E808:')
  111. endfunc
  112. func Test_cosh()
  113. call assert_equal('1.0', string(cosh(0.0)))
  114. call assert_equal('1.127626', string(cosh(0.5)))
  115. eval 0.5->cosh()->string()->assert_equal('1.127626')
  116. call assert_equal("str2float('inf')", string(cosh(1.0/0.0)))
  117. call assert_equal("str2float('inf')", string(cosh(-1.0/0.0)))
  118. call assert_equal("str2float('nan')", string(cosh(0.0/0.0)))
  119. call assert_fails('call cosh("")', 'E808:')
  120. endfunc
  121. func Test_tan()
  122. call assert_equal('0.0', string(tan(0.0)))
  123. call assert_equal('0.546302', string(tan(0.5)))
  124. call assert_equal('-0.546302', string(tan(-0.5)))
  125. eval -0.5->tan()->string()->assert_equal('-0.546302')
  126. call assert_equal("str2float('nan')", string(tan(1.0/0.0)))
  127. call assert_equal("str2float('nan')", string(cos(0.0/0.0)))
  128. call assert_equal('0.0', string(tan(1.0/(1.0/0.0))))
  129. call assert_equal('-0.0', string(tan(-1.0/(1.0/0.0))))
  130. call assert_fails('call tan("")', 'E808:')
  131. endfunc
  132. func Test_atan()
  133. call assert_equal('0.0', string(atan(0.0)))
  134. call assert_equal('0.463648', string(atan(0.5)))
  135. call assert_equal('-0.785398', string(atan(-1.0)))
  136. eval -1.0->atan()->string()->assert_equal('-0.785398')
  137. call assert_equal('1.570796', string(atan(1.0/0.0)))
  138. call assert_equal('-1.570796', string(atan(-1.0/0.0)))
  139. call assert_equal("str2float('nan')", string(atan(0.0/0.0)))
  140. call assert_fails('call atan("")', 'E808:')
  141. endfunc
  142. func Test_atan2()
  143. call assert_equal('-2.356194', string(atan2(-1, -1)))
  144. call assert_equal('2.356194', string(atan2(1, -1)))
  145. call assert_equal('0.0', string(atan2(1.0, 1.0/0.0)))
  146. eval 1.0->atan2(1.0/0.0)->string()->assert_equal('0.0')
  147. call assert_equal('1.570796', string(atan2(1.0/0.0, 1.0)))
  148. call assert_equal("str2float('nan')", string(atan2(0.0/0.0, 1.0)))
  149. call assert_fails('call atan2("", -1)', 'E808:')
  150. call assert_fails('call atan2(-1, "")', 'E808:')
  151. endfunc
  152. func Test_tanh()
  153. call assert_equal('0.0', string(tanh(0.0)))
  154. call assert_equal('0.462117', string(tanh(0.5)))
  155. call assert_equal('-0.761594', string(tanh(-1.0)))
  156. eval -1.0->tanh()->string()->assert_equal('-0.761594')
  157. call assert_equal('1.0', string(tanh(1.0/0.0)))
  158. call assert_equal('-1.0', string(tanh(-1.0/0.0)))
  159. call assert_equal("str2float('nan')", string(tanh(0.0/0.0)))
  160. call assert_fails('call tanh("")', 'E808:')
  161. endfunc
  162. func Test_fmod()
  163. call assert_equal('0.13', string(fmod(12.33, 1.22)))
  164. call assert_equal('-0.13', string(fmod(-12.33, 1.22)))
  165. call assert_equal("str2float('nan')", string(fmod(1.0/0.0, 1.0)))
  166. eval (1.0/0.0)->fmod(1.0)->string()->assert_equal("str2float('nan')")
  167. " On Windows we get "nan" instead of 1.0, accept both.
  168. let res = string(fmod(1.0, 1.0/0.0))
  169. if res != "str2float('nan')"
  170. call assert_equal('1.0', res)
  171. endif
  172. call assert_equal("str2float('nan')", string(fmod(1.0, 0.0)))
  173. call assert_fails("call fmod('', 1.22)", 'E808:')
  174. call assert_fails("call fmod(12.33, '')", 'E808:')
  175. endfunc
  176. func Test_pow()
  177. call assert_equal('1.0', string(pow(0.0, 0.0)))
  178. call assert_equal('8.0', string(pow(2.0, 3.0)))
  179. eval 2.0->pow(3.0)->string()->assert_equal('8.0')
  180. call assert_equal("str2float('nan')", string(pow(2.0, 0.0/0.0)))
  181. call assert_equal("str2float('nan')", string(pow(0.0/0.0, 3.0)))
  182. call assert_equal("str2float('nan')", string(pow(0.0/0.0, 3.0)))
  183. call assert_equal("str2float('inf')", string(pow(2.0, 1.0/0.0)))
  184. call assert_equal("str2float('inf')", string(pow(1.0/0.0, 3.0)))
  185. call assert_fails("call pow('', 2.0)", 'E808:')
  186. call assert_fails("call pow(2.0, '')", 'E808:')
  187. endfunc
  188. func Test_str2float()
  189. call assert_equal('1.0', string(str2float('1')))
  190. call assert_equal('1.0', string(str2float(' 1 ')))
  191. call assert_equal('1.0', string(str2float(' 1.0 ')))
  192. call assert_equal('1.23', string(str2float('1.23')))
  193. call assert_equal('1.23', string(str2float('1.23abc')))
  194. eval '1.23abc'->str2float()->string()->assert_equal('1.23')
  195. call assert_equal('1.0e40', string(str2float('1e40')))
  196. call assert_equal('-1.23', string(str2float('-1.23')))
  197. call assert_equal('1.23', string(str2float(' + 1.23 ')))
  198. call assert_equal('1.0', string(str2float('+1')))
  199. call assert_equal('1.0', string(str2float('+1')))
  200. call assert_equal('1.0', string(str2float(' +1 ')))
  201. call assert_equal('1.0', string(str2float(' + 1 ')))
  202. call assert_equal('-1.0', string(str2float('-1')))
  203. call assert_equal('-1.0', string(str2float('-1')))
  204. call assert_equal('-1.0', string(str2float(' -1 ')))
  205. call assert_equal('-1.0', string(str2float(' - 1 ')))
  206. call assert_equal('0.0', string(str2float('+0.0')))
  207. call assert_equal('-0.0', string(str2float('-0.0')))
  208. call assert_equal("str2float('inf')", string(str2float('1e1000')))
  209. call assert_equal("str2float('inf')", string(str2float('inf')))
  210. call assert_equal("-str2float('inf')", string(str2float('-inf')))
  211. call assert_equal("str2float('inf')", string(str2float('+inf')))
  212. call assert_equal("str2float('inf')", string(str2float('Inf')))
  213. call assert_equal("str2float('inf')", string(str2float(' +inf ')))
  214. call assert_equal("str2float('nan')", string(str2float('nan')))
  215. call assert_equal("str2float('nan')", string(str2float('NaN')))
  216. call assert_equal("str2float('nan')", string(str2float(' nan ')))
  217. call assert_equal(1.2, str2float(1.2))
  218. call CheckDefExecFailure(['str2float(1.2)'], 'E1013:')
  219. call CheckScriptFailure(['vim9script', 'str2float(1.2)'], 'E806:')
  220. call assert_fails("call str2float([])", 'E730:')
  221. call assert_fails("call str2float({})", 'E731:')
  222. call assert_fails("call str2float(function('string'))", 'E729:')
  223. endfunc
  224. func Test_float2nr()
  225. call assert_equal(1, float2nr(1.234))
  226. call assert_equal(123, float2nr(1.234e2))
  227. call assert_equal(12, float2nr(123.4e-1))
  228. eval 123.4e-1->float2nr()->assert_equal(12)
  229. let max_number = 1/0
  230. let min_number = -max_number
  231. call assert_equal(max_number/2+1, float2nr(pow(2, 62)))
  232. call assert_equal(max_number, float2nr(pow(2, 63)))
  233. call assert_equal(max_number, float2nr(pow(2, 64)))
  234. call assert_equal(min_number/2-1, float2nr(-pow(2, 62)))
  235. call assert_equal(min_number, float2nr(-pow(2, 63)))
  236. call assert_equal(min_number, float2nr(-pow(2, 64)))
  237. endfunc
  238. func Test_floor()
  239. call assert_equal('2.0', string(floor(2.0)))
  240. call assert_equal('2.0', string(floor(2.11)))
  241. call assert_equal('2.0', string(floor(2.99)))
  242. eval 2.99->floor()->string()->assert_equal('2.0')
  243. call assert_equal('-3.0', string(floor(-2.11)))
  244. call assert_equal('-3.0', string(floor(-2.99)))
  245. call assert_equal("str2float('nan')", string(floor(0.0/0.0)))
  246. call assert_equal("str2float('inf')", string(floor(1.0/0.0)))
  247. call assert_equal("-str2float('inf')", string(floor(-1.0/0.0)))
  248. call assert_fails("call floor('')", 'E808:')
  249. endfunc
  250. func Test_ceil()
  251. call assert_equal('2.0', string(ceil(2.0)))
  252. call assert_equal('3.0', string(ceil(2.11)))
  253. call assert_equal('3.0', string(ceil(2.99)))
  254. call assert_equal('-2.0', string(ceil(-2.11)))
  255. eval -2.11->ceil()->string()->assert_equal('-2.0')
  256. call assert_equal('-2.0', string(ceil(-2.99)))
  257. call assert_equal("str2float('nan')", string(ceil(0.0/0.0)))
  258. call assert_equal("str2float('inf')", string(ceil(1.0/0.0)))
  259. call assert_equal("-str2float('inf')", string(ceil(-1.0/0.0)))
  260. call assert_fails("call ceil('')", 'E808:')
  261. endfunc
  262. func Test_round()
  263. call assert_equal('2.0', string(round(2.1)))
  264. call assert_equal('3.0', string(round(2.5)))
  265. call assert_equal('3.0', string(round(2.9)))
  266. eval 2.9->round()->string()->assert_equal('3.0')
  267. call assert_equal('-2.0', string(round(-2.1)))
  268. call assert_equal('-3.0', string(round(-2.5)))
  269. call assert_equal('-3.0', string(round(-2.9)))
  270. call assert_equal("str2float('nan')", string(round(0.0/0.0)))
  271. call assert_equal("str2float('inf')", string(round(1.0/0.0)))
  272. call assert_equal("-str2float('inf')", string(round(-1.0/0.0)))
  273. call assert_fails("call round('')", 'E808:')
  274. endfunc
  275. func Test_trunc()
  276. call assert_equal('2.0', string(trunc(2.1)))
  277. call assert_equal('2.0', string(trunc(2.5)))
  278. call assert_equal('2.0', string(trunc(2.9)))
  279. eval 2.9->trunc()->string()->assert_equal('2.0')
  280. call assert_equal('-2.0', string(trunc(-2.1)))
  281. call assert_equal('-2.0', string(trunc(-2.5)))
  282. call assert_equal('-2.0', string(trunc(-2.9)))
  283. call assert_equal("str2float('nan')", string(trunc(0.0/0.0)))
  284. call assert_equal("str2float('inf')", string(trunc(1.0/0.0)))
  285. call assert_equal("-str2float('inf')", string(trunc(-1.0/0.0)))
  286. call assert_fails("call trunc('')", 'E808:')
  287. endfunc
  288. func Test_isinf()
  289. call assert_equal(1, isinf(1.0/0.0))
  290. call assert_equal(-1, isinf(-1.0/0.0))
  291. eval (-1.0/0.0)->isinf()->assert_equal(-1)
  292. call assert_false(isinf(1.0))
  293. call assert_false(isinf(0.0/0.0))
  294. call assert_false(isinf('a'))
  295. call assert_false(isinf([]))
  296. call assert_false(isinf({}))
  297. endfunc
  298. func Test_isnan()
  299. call assert_true(isnan(0.0/0.0))
  300. call assert_false(isnan(1.0))
  301. call assert_false(isnan(1.0/0.0))
  302. eval (1.0/0.0)->isnan()->assert_false()
  303. call assert_false(isnan(-1.0/0.0))
  304. call assert_false(isnan('a'))
  305. call assert_false(isnan([]))
  306. call assert_false(isnan({}))
  307. endfunc
  308. " This was converted from test65
  309. func Test_float_misc()
  310. call assert_equal('123.456000', printf('%f', 123.456))
  311. call assert_equal('1.234560e+02', printf('%e', 123.456))
  312. call assert_equal('123.456', printf('%g', 123.456))
  313. " +=
  314. let v = 1.234
  315. let v += 6.543
  316. call assert_equal('7.777', printf('%g', v))
  317. let v = 1.234
  318. let v += 5
  319. call assert_equal('6.234', printf('%g', v))
  320. let v = 5
  321. let v += 3.333
  322. call assert_equal('8.333', string(v))
  323. " ==
  324. let v = 1.234
  325. call assert_true(v == 1.234)
  326. call assert_false(v == 1.2341)
  327. " add-subtract
  328. call assert_equal('5.234', printf('%g', 4 + 1.234))
  329. call assert_equal('-6.766', printf('%g', 1.234 - 8))
  330. " mult-div
  331. call assert_equal('4.936', printf('%g', 4 * 1.234))
  332. call assert_equal('0.003241', printf('%g', 4.0 / 1234))
  333. " dict
  334. call assert_equal("{'x': 1.234, 'y': -2.0e20}", string({'x': 1.234, 'y': -2.0e20}))
  335. " list
  336. call assert_equal('[-123.4, 2.0e-20]', string([-123.4, 2.0e-20]))
  337. endfunc
  338. " vim: shiftwidth=2 sts=2 expandtab