2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestFloat < Test::Unit::TestCase
|
|
|
|
def test_float
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(2, 2.6.floor)
|
|
|
|
assert_equal(-3, (-2.6).floor)
|
|
|
|
assert_equal(3, 2.6.ceil)
|
|
|
|
assert_equal(-2, (-2.6).ceil)
|
|
|
|
assert_equal(2, 2.6.truncate)
|
|
|
|
assert_equal(-2, (-2.6).truncate)
|
|
|
|
assert_equal(3, 2.6.round)
|
|
|
|
assert_equal(-2, (-2.4).truncate)
|
2003-09-04 12:18:59 -04:00
|
|
|
assert((13.4 % 1 - 0.4).abs < 0.0001)
|
2003-10-07 02:27:11 -04:00
|
|
|
end
|
|
|
|
|
2003-12-01 08:16:09 -05:00
|
|
|
def nan_test(x,y)
|
|
|
|
extend Test::Unit::Assertions
|
|
|
|
assert(x != y)
|
|
|
|
assert_equal(false, (x < y))
|
|
|
|
assert_equal(false, (x > y))
|
|
|
|
assert_equal(false, (x <= y))
|
|
|
|
assert_equal(false, (x >= y))
|
|
|
|
end
|
2003-10-07 02:27:11 -04:00
|
|
|
def test_nan
|
2003-09-04 12:18:59 -04:00
|
|
|
nan = 0.0/0
|
2003-12-01 08:16:09 -05:00
|
|
|
nan_test(nan, nan)
|
|
|
|
nan_test(nan, 0)
|
|
|
|
nan_test(nan, 1)
|
|
|
|
nan_test(nan, -1)
|
|
|
|
nan_test(nan, 1000)
|
|
|
|
nan_test(nan, -1000)
|
|
|
|
nan_test(nan, 1_000_000_000_000)
|
|
|
|
nan_test(nan, -1_000_000_000_000)
|
|
|
|
nan_test(nan, 100.0);
|
|
|
|
nan_test(nan, -100.0);
|
|
|
|
nan_test(nan, 0.001);
|
|
|
|
nan_test(nan, -0.001);
|
|
|
|
nan_test(nan, 1.0/0);
|
|
|
|
nan_test(nan, -1.0/0);
|
2003-10-07 02:27:11 -04:00
|
|
|
end
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-10-07 02:27:11 -04:00
|
|
|
def test_precision
|
2003-09-04 12:18:59 -04:00
|
|
|
#s = "3.7517675036461267e+17"
|
|
|
|
#assert(s == sprintf("%.16e", s.to_f))
|
|
|
|
f = 3.7517675036461267e+17
|
2003-09-05 07:28:27 -04:00
|
|
|
assert_equal(f, sprintf("%.16e", f).to_f)
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|