PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/test_float.rb

http://github.com/ruby/ruby
Ruby | 980 lines | 859 code | 117 blank | 4 comment | 8 complexity | 308d631e328c08b0b4ed72989e880272 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class TestFloat < Test::Unit::TestCase
  4. include EnvUtil
  5. def test_float
  6. assert_equal(2, 2.6.floor)
  7. assert_equal(-3, (-2.6).floor)
  8. assert_equal(3, 2.6.ceil)
  9. assert_equal(-2, (-2.6).ceil)
  10. assert_equal(2, 2.6.truncate)
  11. assert_equal(-2, (-2.6).truncate)
  12. assert_equal(3, 2.6.round)
  13. assert_equal(-2, (-2.4).truncate)
  14. assert_in_delta(13.4 % 1, 0.4, 0.0001)
  15. assert_equal(36893488147419111424,
  16. 36893488147419107329.0.to_i)
  17. assert_equal(1185151044158398820374743613440,
  18. 1.1851510441583988e+30.to_i)
  19. end
  20. def nan_test(x,y)
  21. extend Test::Unit::Assertions
  22. assert_operator(x, :!=, y)
  23. assert_not_operator(x, :<, y)
  24. assert_not_operator(x, :>, y)
  25. assert_not_operator(x, :<=, y)
  26. assert_not_operator(x, :>=, y)
  27. end
  28. def test_nan
  29. nan = Float::NAN
  30. nan_test(nan, nan)
  31. nan_test(nan, 0)
  32. nan_test(nan, 1)
  33. nan_test(nan, -1)
  34. nan_test(nan, 1000)
  35. nan_test(nan, -1000)
  36. nan_test(nan, 1_000_000_000_000)
  37. nan_test(nan, -1_000_000_000_000)
  38. nan_test(nan, 100.0);
  39. nan_test(nan, -100.0);
  40. nan_test(nan, 0.001);
  41. nan_test(nan, -0.001);
  42. nan_test(nan, 1.0/0);
  43. nan_test(nan, -1.0/0);
  44. end
  45. def test_precision
  46. u = 3.7517675036461267e+17
  47. v = sprintf("%.16e", u).to_f
  48. assert_in_delta(u, v, u.abs * Float::EPSILON)
  49. assert_in_delta(u, v, v.abs * Float::EPSILON)
  50. end
  51. def test_symmetry_bignum # [ruby-bugs-ja:118]
  52. a = 100000000000000000000000
  53. b = 100000000000000000000000.0
  54. assert_equal(a == b, b == a)
  55. end
  56. def test_cmp_int
  57. 100.times {|i|
  58. int0 = 1 << i
  59. [int0, -int0].each {|int|
  60. flt = int.to_f
  61. bigger = int + 1
  62. smaller = int - 1
  63. assert_operator(flt, :==, int)
  64. assert_operator(flt, :>, smaller)
  65. assert_operator(flt, :>=, smaller)
  66. assert_operator(flt, :<, bigger)
  67. assert_operator(flt, :<=, bigger)
  68. assert_equal(0, flt <=> int)
  69. assert_equal(-1, flt <=> bigger)
  70. assert_equal(1, flt <=> smaller)
  71. assert_operator(int, :==, flt)
  72. assert_operator(bigger, :>, flt)
  73. assert_operator(bigger, :>=, flt)
  74. assert_operator(smaller, :<, flt)
  75. assert_operator(smaller, :<=, flt)
  76. assert_equal(0, int <=> flt)
  77. assert_equal(-1, smaller <=> flt)
  78. assert_equal(1, bigger <=> flt)
  79. [
  80. [int, flt + 0.5, bigger],
  81. [smaller, flt - 0.5, int]
  82. ].each {|smaller2, flt2, bigger2|
  83. next if flt2 == flt2.round
  84. assert_operator(flt2, :!=, smaller2)
  85. assert_operator(flt2, :!=, bigger2)
  86. assert_operator(flt2, :>, smaller2)
  87. assert_operator(flt2, :>=, smaller2)
  88. assert_operator(flt2, :<, bigger2)
  89. assert_operator(flt2, :<=, bigger2)
  90. assert_equal(-1, flt2 <=> bigger2)
  91. assert_equal(1, flt2 <=> smaller2)
  92. assert_operator(smaller2, :!=, flt2)
  93. assert_operator(bigger2, :!=, flt2)
  94. assert_operator(bigger2, :>, flt2)
  95. assert_operator(bigger2, :>=, flt2)
  96. assert_operator(smaller2, :<, flt2)
  97. assert_operator(smaller2, :<=, flt2)
  98. assert_equal(-1, smaller2 <=> flt2)
  99. assert_equal(1, bigger2 <=> flt2)
  100. }
  101. }
  102. }
  103. end
  104. def test_strtod
  105. a = Float("0")
  106. assert_in_delta(a, 0, Float::EPSILON)
  107. a = Float("0.0")
  108. assert_in_delta(a, 0, Float::EPSILON)
  109. a = Float("+0.0")
  110. assert_in_delta(a, 0, Float::EPSILON)
  111. a = Float("-0.0")
  112. assert_in_delta(a, 0, Float::EPSILON)
  113. a = Float("0.0000000000000000001")
  114. assert_not_equal(0.0, a)
  115. a = Float("+0.0000000000000000001")
  116. assert_not_equal(0.0, a)
  117. a = Float("-0.0000000000000000001")
  118. assert_not_equal(0.0, a)
  119. a = Float(".0")
  120. assert_in_delta(a, 0, Float::EPSILON)
  121. a = Float("+.0")
  122. assert_in_delta(a, 0, Float::EPSILON)
  123. a = Float("-.0")
  124. assert_in_delta(a, 0, Float::EPSILON)
  125. assert_raise(ArgumentError){Float("0.")}
  126. assert_raise(ArgumentError){Float("+0.")}
  127. assert_raise(ArgumentError){Float("-0.")}
  128. assert_raise(ArgumentError){Float(".")}
  129. assert_raise(ArgumentError){Float("+")}
  130. assert_raise(ArgumentError){Float("+.")}
  131. assert_raise(ArgumentError){Float("-")}
  132. assert_raise(ArgumentError){Float("-.")}
  133. assert_raise(ArgumentError){Float("1e")}
  134. assert_raise(ArgumentError){Float("1__1")}
  135. assert_raise(ArgumentError){Float("1.")}
  136. assert_raise(ArgumentError){Float("1.e+00")}
  137. assert_raise(ArgumentError){Float("0x1.p+0")}
  138. # add expected behaviour here.
  139. assert_equal(10, Float("1_0"))
  140. assert_equal([ 0.0].pack('G'), [Float(" 0x0p+0").to_f].pack('G'))
  141. assert_equal([-0.0].pack('G'), [Float("-0x0p+0").to_f].pack('G'))
  142. assert_equal(255.0, Float("0Xff"))
  143. assert_equal(1024.0, Float("0x1p10"))
  144. assert_equal(1024.0, Float("0x1p+10"))
  145. assert_equal(0.0009765625, Float("0x1p-10"))
  146. assert_equal(2.6881171418161356e+43, Float("0x1.3494a9b171bf5p+144"))
  147. assert_equal(-3.720075976020836e-44, Float("-0x1.a8c1f14e2af5dp-145"))
  148. assert_equal(31.0*2**1019, Float("0x0."+("0"*268)+"1fp2099"))
  149. assert_equal(31.0*2**1019, Float("0x0."+("0"*600)+"1fp3427"))
  150. assert_equal(-31.0*2**1019, Float("-0x0."+("0"*268)+"1fp2099"))
  151. assert_equal(-31.0*2**1019, Float("-0x0."+("0"*600)+"1fp3427"))
  152. suppress_warning do
  153. assert_equal(31.0*2**-1027, Float("0x1f"+("0"*268)+".0p-2099"))
  154. assert_equal(31.0*2**-1027, Float("0x1f"+("0"*600)+".0p-3427"))
  155. assert_equal(-31.0*2**-1027, Float("-0x1f"+("0"*268)+".0p-2099"))
  156. assert_equal(-31.0*2**-1027, Float("-0x1f"+("0"*600)+".0p-3427"))
  157. end
  158. assert_equal(1.0e10, Float("1.0_"+"00000"*Float::DIG+"e10"))
  159. z = "0" * (Float::DIG * 4 + 10)
  160. all_assertions_foreach("long invalid string", "1.0", "1.0e", "1.0e-", "1.0e+") do |n|
  161. assert_raise(ArgumentError, n += z + "A") {Float(n)}
  162. assert_raise(ArgumentError, n += z + ".0") {Float(n)}
  163. end
  164. end
  165. def test_divmod
  166. assert_equal([2, 3.5], 11.5.divmod(4))
  167. assert_equal([-3, -0.5], 11.5.divmod(-4))
  168. assert_equal([-3, 0.5], (-11.5).divmod(4))
  169. assert_equal([2, -3.5], (-11.5).divmod(-4))
  170. assert_raise(FloatDomainError) { Float::NAN.divmod(2) }
  171. assert_raise(FloatDomainError) { Float::INFINITY.divmod(2) }
  172. end
  173. def test_div
  174. assert_equal(2, 11.5.div(4))
  175. assert_equal(-3, 11.5.div(-4))
  176. assert_equal(-3, (-11.5).div(4))
  177. assert_equal(2, (-11.5).div(-4))
  178. assert_raise(FloatDomainError) { 11.5.div(Float::NAN).nan? }
  179. assert_raise(FloatDomainError) { Float::NAN.div(2).nan? }
  180. assert_raise(FloatDomainError) { Float::NAN.div(11.5).nan? }
  181. end
  182. def test_modulo
  183. assert_equal(3.5, 11.5.modulo(4))
  184. assert_equal(-0.5, 11.5.modulo(-4))
  185. assert_equal(0.5, (-11.5).modulo(4))
  186. assert_equal(-3.5, (-11.5).modulo(-4))
  187. end
  188. def test_remainder
  189. assert_equal(3.5, 11.5.remainder(4))
  190. assert_equal(3.5, 11.5.remainder(-4))
  191. assert_equal(-3.5, (-11.5).remainder(4))
  192. assert_equal(-3.5, (-11.5).remainder(-4))
  193. assert_predicate(Float::NAN.remainder(4), :nan?)
  194. assert_predicate(4.remainder(Float::NAN), :nan?)
  195. end
  196. def test_to_s
  197. inf = Float::INFINITY
  198. assert_equal("Infinity", inf.to_s)
  199. assert_equal("-Infinity", (-inf).to_s)
  200. assert_equal("NaN", (inf / inf).to_s)
  201. assert_equal("1.0e+18", 1000_00000_00000_00000.0.to_s)
  202. bug3273 = '[ruby-core:30145]'
  203. [0.21611564636388508, 0.56].each do |f|
  204. s = f.to_s
  205. assert_equal(f, s.to_f, bug3273)
  206. assert_not_equal(f, s.chop.to_f, bug3273)
  207. end
  208. end
  209. def test_coerce
  210. assert_equal(Float, 1.0.coerce(1).first.class)
  211. end
  212. def test_plus
  213. assert_equal(4.0, 2.0.send(:+, 2))
  214. assert_equal(4.0, 2.0.send(:+, (2**32).coerce(2).first))
  215. assert_equal(4.0, 2.0.send(:+, 2.0))
  216. assert_equal(Float::INFINITY, 2.0.send(:+, Float::INFINITY))
  217. assert_predicate(2.0.send(:+, Float::NAN), :nan?)
  218. assert_raise(TypeError) { 2.0.send(:+, nil) }
  219. end
  220. def test_minus
  221. assert_equal(0.0, 2.0.send(:-, 2))
  222. assert_equal(0.0, 2.0.send(:-, (2**32).coerce(2).first))
  223. assert_equal(0.0, 2.0.send(:-, 2.0))
  224. assert_equal(-Float::INFINITY, 2.0.send(:-, Float::INFINITY))
  225. assert_predicate(2.0.send(:-, Float::NAN), :nan?)
  226. assert_raise(TypeError) { 2.0.send(:-, nil) }
  227. end
  228. def test_mul
  229. assert_equal(4.0, 2.0.send(:*, 2))
  230. assert_equal(4.0, 2.0.send(:*, (2**32).coerce(2).first))
  231. assert_equal(4.0, 2.0.send(:*, 2.0))
  232. assert_equal(Float::INFINITY, 2.0.send(:*, Float::INFINITY))
  233. assert_raise(TypeError) { 2.0.send(:*, nil) }
  234. end
  235. def test_div2
  236. assert_equal(1.0, 2.0.send(:/, 2))
  237. assert_equal(1.0, 2.0.send(:/, (2**32).coerce(2).first))
  238. assert_equal(1.0, 2.0.send(:/, 2.0))
  239. assert_equal(0.0, 2.0.send(:/, Float::INFINITY))
  240. assert_raise(TypeError) { 2.0.send(:/, nil) }
  241. end
  242. def test_modulo2
  243. assert_equal(0.0, 2.0.send(:%, 2))
  244. assert_equal(0.0, 2.0.send(:%, (2**32).coerce(2).first))
  245. assert_equal(0.0, 2.0.send(:%, 2.0))
  246. assert_raise(TypeError) { 2.0.send(:%, nil) }
  247. end
  248. def test_modulo3
  249. bug6048 = '[ruby-core:42726]'
  250. assert_equal(4.2, 4.2.send(:%, Float::INFINITY), bug6048)
  251. assert_equal(4.2, 4.2 % Float::INFINITY, bug6048)
  252. assert_is_minus_zero(-0.0 % 4.2)
  253. assert_is_minus_zero(-0.0.send :%, 4.2)
  254. assert_raise(ZeroDivisionError, bug6048) { 4.2.send(:%, 0.0) }
  255. assert_raise(ZeroDivisionError, bug6048) { 4.2 % 0.0 }
  256. assert_raise(ZeroDivisionError, bug6048) { 42.send(:%, 0) }
  257. assert_raise(ZeroDivisionError, bug6048) { 42 % 0 }
  258. end
  259. def test_modulo4
  260. assert_predicate((0.0).modulo(Float::NAN), :nan?)
  261. assert_predicate((1.0).modulo(Float::NAN), :nan?)
  262. assert_predicate(Float::INFINITY.modulo(1), :nan?)
  263. end
  264. def test_divmod2
  265. assert_equal([1.0, 0.0], 2.0.divmod(2))
  266. assert_equal([1.0, 0.0], 2.0.divmod((2**32).coerce(2).first))
  267. assert_equal([1.0, 0.0], 2.0.divmod(2.0))
  268. assert_raise(TypeError) { 2.0.divmod(nil) }
  269. inf = Float::INFINITY
  270. assert_raise(ZeroDivisionError) {inf.divmod(0)}
  271. a, b = (2.0**32).divmod(1.0)
  272. assert_equal(2**32, a)
  273. assert_equal(0, b)
  274. end
  275. def test_pow
  276. assert_equal(1.0, 1.0 ** (2**32))
  277. assert_equal(1.0, 1.0 ** 1.0)
  278. assert_raise(TypeError) { 1.0 ** nil }
  279. assert_equal(9.0, 3.0 ** 2)
  280. end
  281. def test_eql
  282. inf = Float::INFINITY
  283. nan = Float::NAN
  284. assert_operator(1.0, :eql?, 1.0)
  285. assert_operator(inf, :eql?, inf)
  286. assert_not_operator(nan, :eql?, nan)
  287. assert_not_operator(1.0, :eql?, nil)
  288. assert_equal(1.0, 1)
  289. assert_not_equal(1.0, 2**32)
  290. assert_not_equal(1.0, nan)
  291. assert_not_equal(1.0, nil)
  292. end
  293. def test_cmp
  294. inf = Float::INFINITY
  295. nan = Float::NAN
  296. assert_equal(0, 1.0 <=> 1.0)
  297. assert_equal(1, 1.0 <=> 0.0)
  298. assert_equal(-1, 1.0 <=> 2.0)
  299. assert_nil(1.0 <=> nil)
  300. assert_nil(1.0 <=> nan)
  301. assert_nil(nan <=> 1.0)
  302. assert_equal(0, 1.0 <=> 1)
  303. assert_equal(1, 1.0 <=> 0)
  304. assert_equal(-1, 1.0 <=> 2)
  305. assert_equal(-1, 1.0 <=> 2**32)
  306. assert_equal(1, inf <=> (Float::MAX.to_i*2))
  307. assert_equal(-1, -inf <=> (-Float::MAX.to_i*2))
  308. assert_equal(-1, (Float::MAX.to_i*2) <=> inf)
  309. assert_equal(1, (-Float::MAX.to_i*2) <=> -inf)
  310. bug3609 = '[ruby-core:31470]'
  311. def (pinf = Object.new).infinite?; +1 end
  312. def (ninf = Object.new).infinite?; -1 end
  313. def (fin = Object.new).infinite?; nil end
  314. nonum = Object.new
  315. assert_equal(0, inf <=> pinf, bug3609)
  316. assert_equal(1, inf <=> fin, bug3609)
  317. assert_equal(1, inf <=> ninf, bug3609)
  318. assert_nil(inf <=> nonum, bug3609)
  319. assert_equal(-1, -inf <=> pinf, bug3609)
  320. assert_equal(-1, -inf <=> fin, bug3609)
  321. assert_equal(0, -inf <=> ninf, bug3609)
  322. assert_nil(-inf <=> nonum, bug3609)
  323. assert_raise(ArgumentError) { 1.0 > nil }
  324. assert_raise(ArgumentError) { 1.0 >= nil }
  325. assert_raise(ArgumentError) { 1.0 < nil }
  326. assert_raise(ArgumentError) { 1.0 <= nil }
  327. end
  328. def test_zero_p
  329. assert_predicate(0.0, :zero?)
  330. assert_not_predicate(1.0, :zero?)
  331. end
  332. def test_positive_p
  333. assert_predicate(+1.0, :positive?)
  334. assert_not_predicate(+0.0, :positive?)
  335. assert_not_predicate(-0.0, :positive?)
  336. assert_not_predicate(-1.0, :positive?)
  337. assert_predicate(+(0.0.next_float), :positive?)
  338. assert_not_predicate(-(0.0.next_float), :positive?)
  339. assert_predicate(Float::INFINITY, :positive?)
  340. assert_not_predicate(-Float::INFINITY, :positive?)
  341. assert_not_predicate(Float::NAN, :positive?)
  342. end
  343. def test_negative_p
  344. assert_predicate(-1.0, :negative?)
  345. assert_not_predicate(-0.0, :negative?)
  346. assert_not_predicate(+0.0, :negative?)
  347. assert_not_predicate(+1.0, :negative?)
  348. assert_predicate(-(0.0.next_float), :negative?)
  349. assert_not_predicate(+(0.0.next_float), :negative?)
  350. assert_predicate(-Float::INFINITY, :negative?)
  351. assert_not_predicate(Float::INFINITY, :negative?)
  352. assert_not_predicate(Float::NAN, :negative?)
  353. end
  354. def test_infinite_p
  355. inf = Float::INFINITY
  356. assert_equal(1, inf.infinite?)
  357. assert_equal(-1, (-inf).infinite?)
  358. assert_nil(1.0.infinite?)
  359. end
  360. def test_finite_p
  361. inf = Float::INFINITY
  362. assert_not_predicate(inf, :finite?)
  363. assert_not_predicate(-inf, :finite?)
  364. assert_predicate(1.0, :finite?)
  365. end
  366. def test_floor_ceil_round_truncate
  367. assert_equal(1, 1.5.floor)
  368. assert_equal(2, 1.5.ceil)
  369. assert_equal(2, 1.5.round)
  370. assert_equal(1, 1.5.truncate)
  371. assert_equal(2, 2.0.floor)
  372. assert_equal(2, 2.0.ceil)
  373. assert_equal(2, 2.0.round)
  374. assert_equal(2, 2.0.truncate)
  375. assert_equal(-2, (-1.5).floor)
  376. assert_equal(-1, (-1.5).ceil)
  377. assert_equal(-2, (-1.5).round)
  378. assert_equal(-1, (-1.5).truncate)
  379. assert_equal(-2, (-2.0).floor)
  380. assert_equal(-2, (-2.0).ceil)
  381. assert_equal(-2, (-2.0).round)
  382. assert_equal(-2, (-2.0).truncate)
  383. inf = Float::INFINITY
  384. assert_raise(FloatDomainError) { inf.floor }
  385. assert_raise(FloatDomainError) { inf.ceil }
  386. assert_raise(FloatDomainError) { inf.round }
  387. assert_raise(FloatDomainError) { inf.truncate }
  388. end
  389. def test_round_with_precision
  390. assert_equal(1.100, 1.111.round(1))
  391. assert_equal(1.110, 1.111.round(2))
  392. assert_equal(11110.0, 11111.1.round(-1))
  393. assert_equal(11100.0, 11111.1.round(-2))
  394. assert_equal(-1.100, -1.111.round(1))
  395. assert_equal(-1.110, -1.111.round(2))
  396. assert_equal(-11110.0, -11111.1.round(-1))
  397. assert_equal(-11100.0, -11111.1.round(-2))
  398. assert_equal(0, 11111.1.round(-5))
  399. assert_equal(10**300, 1.1e300.round(-300))
  400. assert_equal(-10**300, -1.1e300.round(-300))
  401. assert_equal(1.0e-300, 1.1e-300.round(300))
  402. assert_equal(-1.0e-300, -1.1e-300.round(300))
  403. bug5227 = '[ruby-core:39093]'
  404. assert_equal(42.0, 42.0.round(308), bug5227)
  405. assert_equal(1.0e307, 1.0e307.round(2), bug5227)
  406. assert_raise(TypeError) {1.0.round("4")}
  407. assert_raise(TypeError) {1.0.round(nil)}
  408. def (prec = Object.new).to_int; 2; end
  409. assert_equal(1.0, 0.998.round(prec))
  410. assert_equal(+5.02, +5.015.round(2))
  411. assert_equal(-5.02, -5.015.round(2))
  412. assert_equal(+1.26, +1.255.round(2))
  413. assert_equal(-1.26, -1.255.round(2))
  414. end
  415. def test_floor_with_precision
  416. assert_equal(+0.0, +0.001.floor(1))
  417. assert_equal(-0.1, -0.001.floor(1))
  418. assert_equal(1.100, 1.111.floor(1))
  419. assert_equal(1.110, 1.111.floor(2))
  420. assert_equal(11110, 11119.9.floor(-1))
  421. assert_equal(11100, 11100.0.floor(-2))
  422. assert_equal(11100, 11199.9.floor(-2))
  423. assert_equal(-1.200, -1.111.floor(1))
  424. assert_equal(-1.120, -1.111.floor(2))
  425. assert_equal(-11120, -11119.9.floor(-1))
  426. assert_equal(-11100, -11100.0.floor(-2))
  427. assert_equal(-11200, -11199.9.floor(-2))
  428. assert_equal(0, 11111.1.floor(-5))
  429. assert_equal(10**300, 1.1e300.floor(-300))
  430. assert_equal(-2*10**300, -1.1e300.floor(-300))
  431. assert_equal(1.0e-300, 1.1e-300.floor(300))
  432. assert_equal(-2.0e-300, -1.1e-300.floor(300))
  433. assert_equal(42.0, 42.0.floor(308))
  434. assert_equal(1.0e307, 1.0e307.floor(2))
  435. assert_raise(TypeError) {1.0.floor("4")}
  436. assert_raise(TypeError) {1.0.floor(nil)}
  437. def (prec = Object.new).to_int; 2; end
  438. assert_equal(0.99, 0.998.floor(prec))
  439. end
  440. def test_ceil_with_precision
  441. assert_equal(+0.1, +0.001.ceil(1))
  442. assert_equal(-0.0, -0.001.ceil(1))
  443. assert_equal(1.200, 1.111.ceil(1))
  444. assert_equal(1.120, 1.111.ceil(2))
  445. assert_equal(11120, 11111.1.ceil(-1))
  446. assert_equal(11200, 11111.1.ceil(-2))
  447. assert_equal(-1.100, -1.111.ceil(1))
  448. assert_equal(-1.110, -1.111.ceil(2))
  449. assert_equal(-11110, -11111.1.ceil(-1))
  450. assert_equal(-11100, -11111.1.ceil(-2))
  451. assert_equal(100000, 11111.1.ceil(-5))
  452. assert_equal(2*10**300, 1.1e300.ceil(-300))
  453. assert_equal(-10**300, -1.1e300.ceil(-300))
  454. assert_equal(2.0e-300, 1.1e-300.ceil(300))
  455. assert_equal(-1.0e-300, -1.1e-300.ceil(300))
  456. assert_equal(42.0, 42.0.ceil(308))
  457. assert_equal(1.0e307, 1.0e307.ceil(2))
  458. assert_raise(TypeError) {1.0.ceil("4")}
  459. assert_raise(TypeError) {1.0.ceil(nil)}
  460. def (prec = Object.new).to_int; 2; end
  461. assert_equal(0.99, 0.981.ceil(prec))
  462. end
  463. def test_truncate_with_precision
  464. assert_equal(1.100, 1.111.truncate(1))
  465. assert_equal(1.110, 1.111.truncate(2))
  466. assert_equal(11110, 11119.9.truncate(-1))
  467. assert_equal(11100, 11100.0.truncate(-2))
  468. assert_equal(11100, 11199.9.truncate(-2))
  469. assert_equal(-1.100, -1.111.truncate(1))
  470. assert_equal(-1.110, -1.111.truncate(2))
  471. assert_equal(-11110, -11111.1.truncate(-1))
  472. assert_equal(-11100, -11111.1.truncate(-2))
  473. assert_equal(0, 11111.1.truncate(-5))
  474. assert_equal(10**300, 1.1e300.truncate(-300))
  475. assert_equal(-10**300, -1.1e300.truncate(-300))
  476. assert_equal(1.0e-300, 1.1e-300.truncate(300))
  477. assert_equal(-1.0e-300, -1.1e-300.truncate(300))
  478. assert_equal(42.0, 42.0.truncate(308))
  479. assert_equal(1.0e307, 1.0e307.truncate(2))
  480. assert_raise(TypeError) {1.0.truncate("4")}
  481. assert_raise(TypeError) {1.0.truncate(nil)}
  482. def (prec = Object.new).to_int; 2; end
  483. assert_equal(0.99, 0.998.truncate(prec))
  484. end
  485. VS = [
  486. 18446744073709551617.0,
  487. 18446744073709551616.0,
  488. 18446744073709551615.8,
  489. 18446744073709551615.5,
  490. 18446744073709551615.2,
  491. 18446744073709551615.0,
  492. 18446744073709551614.0,
  493. 4611686018427387905.0,
  494. 4611686018427387904.0,
  495. 4611686018427387903.8,
  496. 4611686018427387903.5,
  497. 4611686018427387903.2,
  498. 4611686018427387903.0,
  499. 4611686018427387902.0,
  500. 4294967297.0,
  501. 4294967296.0,
  502. 4294967295.8,
  503. 4294967295.5,
  504. 4294967295.2,
  505. 4294967295.0,
  506. 4294967294.0,
  507. 1073741825.0,
  508. 1073741824.0,
  509. 1073741823.8,
  510. 1073741823.5,
  511. 1073741823.2,
  512. 1073741823.0,
  513. 1073741822.0,
  514. -1073741823.0,
  515. -1073741824.0,
  516. -1073741824.2,
  517. -1073741824.5,
  518. -1073741824.8,
  519. -1073741825.0,
  520. -1073741826.0,
  521. -4294967295.0,
  522. -4294967296.0,
  523. -4294967296.2,
  524. -4294967296.5,
  525. -4294967296.8,
  526. -4294967297.0,
  527. -4294967298.0,
  528. -4611686018427387903.0,
  529. -4611686018427387904.0,
  530. -4611686018427387904.2,
  531. -4611686018427387904.5,
  532. -4611686018427387904.8,
  533. -4611686018427387905.0,
  534. -4611686018427387906.0,
  535. -18446744073709551615.0,
  536. -18446744073709551616.0,
  537. -18446744073709551616.2,
  538. -18446744073709551616.5,
  539. -18446744073709551616.8,
  540. -18446744073709551617.0,
  541. -18446744073709551618.0,
  542. ]
  543. def test_truncate
  544. VS.each {|f|
  545. i = f.truncate
  546. assert_equal(i, f.to_i)
  547. if f < 0
  548. assert_operator(i, :<, 0)
  549. else
  550. assert_operator(i, :>, 0)
  551. end
  552. assert_operator(i.abs, :<=, f.abs)
  553. d = f.abs - i.abs
  554. assert_operator(0, :<=, d)
  555. assert_operator(d, :<, 1)
  556. }
  557. end
  558. def test_ceil
  559. VS.each {|f|
  560. i = f.ceil
  561. if f < 0
  562. assert_operator(i, :<, 0)
  563. else
  564. assert_operator(i, :>, 0)
  565. end
  566. assert_operator(i, :>=, f)
  567. d = f - i
  568. assert_operator(-1, :<, d)
  569. assert_operator(d, :<=, 0)
  570. }
  571. end
  572. def test_floor
  573. VS.each {|f|
  574. i = f.floor
  575. if f < 0
  576. assert_operator(i, :<, 0)
  577. else
  578. assert_operator(i, :>, 0)
  579. end
  580. assert_operator(i, :<=, f)
  581. d = f - i
  582. assert_operator(0, :<=, d)
  583. assert_operator(d, :<, 1)
  584. }
  585. end
  586. def test_round
  587. VS.each {|f|
  588. msg = "round(#{f})"
  589. i = f.round
  590. if f < 0
  591. assert_operator(i, :<, 0, msg)
  592. else
  593. assert_operator(i, :>, 0, msg)
  594. end
  595. d = f - i
  596. assert_operator(-0.5, :<=, d, msg)
  597. assert_operator(d, :<=, 0.5, msg)
  598. }
  599. end
  600. def test_round_half_even
  601. assert_equal(12.0, 12.5.round(half: :even))
  602. assert_equal(14.0, 13.5.round(half: :even))
  603. assert_equal(2.2, 2.15.round(1, half: :even))
  604. assert_equal(2.2, 2.25.round(1, half: :even))
  605. assert_equal(2.4, 2.35.round(1, half: :even))
  606. assert_equal(-2.2, -2.15.round(1, half: :even))
  607. assert_equal(-2.2, -2.25.round(1, half: :even))
  608. assert_equal(-2.4, -2.35.round(1, half: :even))
  609. assert_equal(7.1364, 7.13645.round(4, half: :even))
  610. assert_equal(7.1365, 7.1364501.round(4, half: :even))
  611. assert_equal(7.1364, 7.1364499.round(4, half: :even))
  612. assert_equal(-7.1364, -7.13645.round(4, half: :even))
  613. assert_equal(-7.1365, -7.1364501.round(4, half: :even))
  614. assert_equal(-7.1364, -7.1364499.round(4, half: :even))
  615. end
  616. def test_round_half_up
  617. assert_equal(13.0, 12.5.round(half: :up))
  618. assert_equal(14.0, 13.5.round(half: :up))
  619. assert_equal(2.2, 2.15.round(1, half: :up))
  620. assert_equal(2.3, 2.25.round(1, half: :up))
  621. assert_equal(2.4, 2.35.round(1, half: :up))
  622. assert_equal(-2.2, -2.15.round(1, half: :up))
  623. assert_equal(-2.3, -2.25.round(1, half: :up))
  624. assert_equal(-2.4, -2.35.round(1, half: :up))
  625. assert_equal(7.1365, 7.13645.round(4, half: :up))
  626. assert_equal(7.1365, 7.1364501.round(4, half: :up))
  627. assert_equal(7.1364, 7.1364499.round(4, half: :up))
  628. assert_equal(-7.1365, -7.13645.round(4, half: :up))
  629. assert_equal(-7.1365, -7.1364501.round(4, half: :up))
  630. assert_equal(-7.1364, -7.1364499.round(4, half: :up))
  631. end
  632. def test_round_half_down
  633. assert_equal(12.0, 12.5.round(half: :down))
  634. assert_equal(13.0, 13.5.round(half: :down))
  635. assert_equal(2.1, 2.15.round(1, half: :down))
  636. assert_equal(2.2, 2.25.round(1, half: :down))
  637. assert_equal(2.3, 2.35.round(1, half: :down))
  638. assert_equal(-2.1, -2.15.round(1, half: :down))
  639. assert_equal(-2.2, -2.25.round(1, half: :down))
  640. assert_equal(-2.3, -2.35.round(1, half: :down))
  641. assert_equal(7.1364, 7.13645.round(4, half: :down))
  642. assert_equal(7.1365, 7.1364501.round(4, half: :down))
  643. assert_equal(7.1364, 7.1364499.round(4, half: :down))
  644. assert_equal(-7.1364, -7.13645.round(4, half: :down))
  645. assert_equal(-7.1365, -7.1364501.round(4, half: :down))
  646. assert_equal(-7.1364, -7.1364499.round(4, half: :down))
  647. end
  648. def test_round_half_nil
  649. assert_equal(13.0, 12.5.round(half: nil))
  650. assert_equal(14.0, 13.5.round(half: nil))
  651. assert_equal(2.2, 2.15.round(1, half: nil))
  652. assert_equal(2.3, 2.25.round(1, half: nil))
  653. assert_equal(2.4, 2.35.round(1, half: nil))
  654. assert_equal(-2.2, -2.15.round(1, half: nil))
  655. assert_equal(-2.3, -2.25.round(1, half: nil))
  656. assert_equal(-2.4, -2.35.round(1, half: nil))
  657. assert_equal(7.1365, 7.13645.round(4, half: nil))
  658. assert_equal(7.1365, 7.1364501.round(4, half: nil))
  659. assert_equal(7.1364, 7.1364499.round(4, half: nil))
  660. assert_equal(-7.1365, -7.13645.round(4, half: nil))
  661. assert_equal(-7.1365, -7.1364501.round(4, half: nil))
  662. assert_equal(-7.1364, -7.1364499.round(4, half: nil))
  663. end
  664. def test_round_half_invalid
  665. assert_raise_with_message(ArgumentError, /Object/) {
  666. 1.0.round(half: Object)
  667. }
  668. assert_raise_with_message(ArgumentError, /xxx/) {
  669. 1.0.round(half: "\0xxx")
  670. }
  671. assert_raise_with_message(Encoding::CompatibilityError, /ASCII incompatible/) {
  672. 1.0.round(half: "up".force_encoding("utf-16be"))
  673. }
  674. end
  675. def test_Float
  676. assert_in_delta(0.125, Float("0.1_2_5"), 0.00001)
  677. assert_in_delta(0.125, "0.1_2_5__".to_f, 0.00001)
  678. assert_in_delta(0.0, "0_.125".to_f, 0.00001)
  679. assert_in_delta(0.0, "0._125".to_f, 0.00001)
  680. assert_in_delta(0.1, "0.1__2_5".to_f, 0.00001)
  681. assert_in_delta(0.1, "0.1_e10".to_f, 0.00001)
  682. assert_in_delta(0.1, "0.1e_10".to_f, 0.00001)
  683. assert_in_delta(1.0, "0.1e1__0".to_f, 0.00001)
  684. assert_equal(1, suppress_warning {Float(([1] * 10000).join)}.infinite?)
  685. assert_not_predicate(Float(([1] * 10000).join("_")), :infinite?) # is it really OK?
  686. assert_raise(ArgumentError) { Float("1.0\x001") }
  687. assert_equal(15.9375, Float('0xf.fp0'))
  688. assert_raise(ArgumentError) { Float('0x') }
  689. assert_equal(15, Float('0xf'))
  690. assert_equal(15, Float('0xfp0'))
  691. assert_raise(ArgumentError) { Float('0xfp') }
  692. assert_raise(ArgumentError) { Float('0xf.') }
  693. assert_raise(ArgumentError) { Float('0xf.p') }
  694. assert_raise(ArgumentError) { Float('0xf.p0') }
  695. assert_raise(ArgumentError) { Float('0xf.f') }
  696. assert_raise(ArgumentError) { Float('0xf.fp') }
  697. begin
  698. verbose_bak, $VERBOSE = $VERBOSE, nil
  699. assert_equal(Float::INFINITY, Float('0xf.fp1000000000000000'))
  700. ensure
  701. $VERBOSE = verbose_bak
  702. end
  703. assert_equal(1, suppress_warning {Float("1e10_00")}.infinite?)
  704. assert_raise(TypeError) { Float(nil) }
  705. assert_raise(TypeError) { Float(:test) }
  706. o = Object.new
  707. def o.to_f; inf = Float::INFINITY; inf/inf; end
  708. assert_predicate(Float(o), :nan?)
  709. end
  710. def test_invalid_str
  711. bug4310 = '[ruby-core:34820]'
  712. assert_raise(ArgumentError, bug4310) {under_gc_stress {Float('a'*10000)}}
  713. end
  714. def test_Float_with_invalid_exception
  715. assert_raise(ArgumentError) {
  716. Float("0", exception: 1)
  717. }
  718. end
  719. def test_Float_with_exception_keyword
  720. assert_raise(ArgumentError) {
  721. Float(".", exception: true)
  722. }
  723. assert_nothing_raised(ArgumentError) {
  724. assert_equal(nil, Float(".", exception: false))
  725. }
  726. assert_raise(RangeError) {
  727. Float(1i, exception: true)
  728. }
  729. assert_nothing_raised(RangeError) {
  730. assert_equal(nil, Float(1i, exception: false))
  731. }
  732. assert_raise(TypeError) {
  733. Float(nil, exception: true)
  734. }
  735. assert_nothing_raised(TypeError) {
  736. assert_equal(nil, Float(nil, exception: false))
  737. }
  738. assert_nothing_raised(TypeError) {
  739. assert_equal(nil, Float(:test, exception: false))
  740. }
  741. assert_nothing_raised(TypeError) {
  742. assert_equal(nil, Float(Object.new, exception: false))
  743. }
  744. assert_nothing_raised(TypeError) {
  745. o = Object.new
  746. def o.to_f; 3.14; end
  747. assert_equal(3.14, Float(o, exception: false))
  748. }
  749. assert_nothing_raised(RuntimeError) {
  750. o = Object.new
  751. def o.to_f; raise; end
  752. assert_equal(nil, Float(o, exception: false))
  753. }
  754. end
  755. def test_num2dbl
  756. assert_raise(ArgumentError, "comparison of String with 0 failed") do
  757. 1.0.step(2.0, "0.5") {}
  758. end
  759. assert_raise(TypeError) do
  760. 1.0.step(2.0, nil) {}
  761. end
  762. end
  763. def test_sleep_with_Float
  764. assert_nothing_raised("[ruby-core:23282]") do
  765. sleep(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)
  766. end
  767. end
  768. def test_step
  769. 1000.times do
  770. a = rand
  771. b = a+rand*1000
  772. s = (b - a) / 10
  773. assert_equal(11, (a..b).step(s).to_a.length)
  774. end
  775. (1.0..12.7).step(1.3).each do |n|
  776. assert_operator(n, :<=, 12.7)
  777. end
  778. assert_equal([5.0, 4.0, 3.0, 2.0], 5.0.step(1.5, -1).to_a)
  779. end
  780. def test_step2
  781. assert_equal([0.0], 0.0.step(1.0, Float::INFINITY).to_a)
  782. end
  783. def test_step_excl
  784. 1000.times do
  785. a = rand
  786. b = a+rand*1000
  787. s = (b - a) / 10
  788. seq = (a...b).step(s)
  789. assert_equal(10, seq.to_a.length, seq.inspect)
  790. end
  791. assert_equal([1.0, 2.9, 4.8, 6.699999999999999], (1.0...6.8).step(1.9).to_a)
  792. e = 1+1E-12
  793. (1.0 ... e).step(1E-16) do |n|
  794. assert_operator(n, :<=, e)
  795. end
  796. end
  797. def test_singleton_method
  798. # flonum on 64bit platform
  799. assert_raise(TypeError) { a = 1.0; def a.foo; end }
  800. # always not flonum
  801. assert_raise(TypeError) { a = Float::INFINITY; def a.foo; end }
  802. end
  803. def test_long_string
  804. assert_separately([], <<-'end;')
  805. assert_in_epsilon(10.0, ("1."+"1"*300000).to_f*9)
  806. end;
  807. end
  808. def test_next_float
  809. smallest = 0.0.next_float
  810. assert_equal(-Float::MAX, (-Float::INFINITY).next_float)
  811. assert_operator(-Float::MAX, :<, (-Float::MAX).next_float)
  812. assert_equal(Float::EPSILON/2, (-1.0).next_float + 1.0)
  813. assert_operator(0.0, :<, smallest)
  814. assert_operator([0.0, smallest], :include?, smallest/2)
  815. assert_equal(Float::EPSILON, 1.0.next_float - 1.0)
  816. assert_equal(Float::INFINITY, Float::MAX.next_float)
  817. assert_equal(Float::INFINITY, Float::INFINITY.next_float)
  818. assert_predicate(Float::NAN.next_float, :nan?)
  819. end
  820. def test_prev_float
  821. smallest = 0.0.next_float
  822. assert_equal(-Float::INFINITY, (-Float::INFINITY).prev_float)
  823. assert_equal(-Float::INFINITY, (-Float::MAX).prev_float)
  824. assert_equal(-Float::EPSILON, (-1.0).prev_float + 1.0)
  825. assert_equal(-smallest, 0.0.prev_float)
  826. assert_operator([0.0, 0.0.prev_float], :include?, 0.0.prev_float/2)
  827. assert_equal(-Float::EPSILON/2, 1.0.prev_float - 1.0)
  828. assert_operator(Float::MAX, :>, Float::MAX.prev_float)
  829. assert_equal(Float::MAX, Float::INFINITY.prev_float)
  830. assert_predicate(Float::NAN.prev_float, :nan?)
  831. end
  832. def test_next_prev_float_zero
  833. z = 0.0.next_float.prev_float
  834. assert_equal(0.0, z)
  835. assert_equal(Float::INFINITY, 1.0/z)
  836. z = 0.0.prev_float.next_float
  837. assert_equal(0.0, z)
  838. assert_equal(-Float::INFINITY, 1.0/z)
  839. end
  840. def test_hash_0
  841. bug10979 = '[ruby-core:68541] [Bug #10979]'
  842. assert_equal(+0.0.hash, -0.0.hash)
  843. assert_operator(+0.0, :eql?, -0.0)
  844. h = {0.0 => bug10979}
  845. assert_equal(bug10979, h[-0.0])
  846. end
  847. def test_aliased_quo_recursion
  848. assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
  849. begin;
  850. class Float
  851. $VERBOSE = nil
  852. alias / quo
  853. end
  854. assert_raise(NameError) do
  855. begin
  856. 1.0/2.0
  857. rescue SystemStackError => e
  858. raise SystemStackError, e.message
  859. end
  860. end
  861. end;
  862. end
  863. end