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
|
2004-04-01 06:32:17 -05:00
|
|
|
|
|
|
|
def test_symmetry_bignum # [ruby-bugs-ja:118]
|
|
|
|
a = 100000000000000000000000
|
|
|
|
b = 100000000000000000000000.0
|
|
|
|
assert_equal(a == b, b == a)
|
|
|
|
end
|
2004-05-09 10:38:11 -04:00
|
|
|
|
|
|
|
def test_strtod
|
|
|
|
a = Float("0")
|
|
|
|
assert(a.abs < Float::EPSILON)
|
|
|
|
a = Float("0.0")
|
|
|
|
assert(a.abs < Float::EPSILON)
|
|
|
|
a = Float("+0.0")
|
|
|
|
assert(a.abs < Float::EPSILON)
|
|
|
|
a = Float("-0.0")
|
|
|
|
assert(a.abs < Float::EPSILON)
|
2004-05-14 00:34:07 -04:00
|
|
|
a = Float("0." + "00" * Float::DIG + "1")
|
|
|
|
assert(a != 0.0)
|
2004-05-15 04:54:23 -04:00
|
|
|
a = Float("+0." + "00" * Float::DIG + "1")
|
|
|
|
assert(a != 0.0)
|
|
|
|
a = Float("-0." + "00" * Float::DIG + "1")
|
|
|
|
assert(a != 0.0)
|
2004-05-09 10:38:11 -04:00
|
|
|
# add expected behaviour here.
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|