mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Finish all Float ISO Tests
This commit is contained in:
@@ -65,3 +65,16 @@ def report()
|
||||
print("\n")
|
||||
end
|
||||
|
||||
##
|
||||
# Performs fuzzy check for equality on methods returning floats
|
||||
# on the basis of the Math::TOLERANCE constant.
|
||||
def check_float(a, b)
|
||||
a = a.to_f
|
||||
b = b.to_f
|
||||
if a.finite? and b.finite?
|
||||
(a-b).abs < Math::TOLERANCE
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
##
|
||||
# Float ISO Test
|
||||
|
||||
assert('Float', '15.2.9') do
|
||||
Float.class == Class
|
||||
end
|
||||
|
||||
assert('Float#+', '15.2.9.3.1') do
|
||||
a = 3.123456788 + 0.000000001
|
||||
b = 3.123456789 + 1
|
||||
|
||||
check_float(a, 3.123456789) and
|
||||
check_float(b, 4.123456789)
|
||||
end
|
||||
|
||||
assert('Float#-', '15.2.9.3.2') do
|
||||
a = 3.123456790 - 0.000000001
|
||||
b = 5.123456789 - 1
|
||||
|
||||
check_float(a, 3.123456789) and
|
||||
check_float(b, 4.123456789)
|
||||
end
|
||||
|
||||
assert('Float#*', '15.2.9.3.3') do
|
||||
a = 3.123456789 * 3.123456789
|
||||
b = 3.123456789 * 1
|
||||
|
||||
check_float(a, 9.75598231275019) and
|
||||
check_float(b, 3.123456789)
|
||||
end
|
||||
|
||||
assert('Float#/', '15.2.9.3.4') do
|
||||
a = 3.123456789 / 3.123456789
|
||||
b = 3.123456789 / 1
|
||||
|
||||
check_float(a, 1.0) and
|
||||
check_float(b, 3.123456789)
|
||||
end
|
||||
|
||||
assert('Float#%', '15.2.9.3.5') do
|
||||
a = 3.123456789 % 3.123456789
|
||||
b = 3.123456789 % 1
|
||||
|
||||
check_float(a, 0.0) and
|
||||
check_float(b, 0.123456789)
|
||||
end
|
||||
|
||||
assert('Float#<=>', '15.2.9.3.6') do
|
||||
a = 3.123456789 <=> 3.123456788
|
||||
b = 3.123456789 <=> 3.123456789
|
||||
c = 3.123456789 <=> 3.123456790
|
||||
a2 = 3.123456789 <=> 3
|
||||
c2 = 3.123456789 <=> 4
|
||||
|
||||
a == 1 and b == 0 and c == -1 and
|
||||
a2 == 1 and c2 == -1
|
||||
end
|
||||
|
||||
assert('Float#==', '15.2.9.3.7') do
|
||||
3.1 == 3.1 and not (3.1 == 3.2)
|
||||
end
|
||||
|
||||
assert('Float#ceil', '15.2.9.3.8') do
|
||||
3.123456789.ceil == 4
|
||||
end
|
||||
|
||||
assert('Float#finite?', '15.2.9.3.9') do
|
||||
3.123456789.finite? and
|
||||
not (1.0 / 0.0).finite?
|
||||
end
|
||||
|
||||
assert('Float#floor', '15.2.9.3.10') do
|
||||
3.123456789.floor == 3
|
||||
end
|
||||
|
||||
assert('Float#infinite?', '15.2.9.3.11') do
|
||||
not 3.123456789.infinite? and
|
||||
(1.0 / 0.0).infinite?
|
||||
end
|
||||
|
||||
assert('Float#round', '15.2.9.3.12') do
|
||||
a = 3.123456789.round
|
||||
b = 3.5.round
|
||||
c = 3.499999999.round
|
||||
|
||||
a == 3 and b == 4 and c == 3
|
||||
end
|
||||
|
||||
assert('Float#to_f', '15.2.9.3.13') do
|
||||
a = 3.123456789
|
||||
|
||||
check_float(a.to_f, a)
|
||||
end
|
||||
|
||||
assert('Float#to_i', '15.2.9.3.14') do
|
||||
3.123456789.to_i == 3
|
||||
end
|
||||
|
||||
assert('Float#truncate', '15.2.9.3.15') do
|
||||
3.123456789.truncate == 3
|
||||
end
|
||||
|
||||
+24
-37
@@ -1,25 +1,12 @@
|
||||
##
|
||||
# Math Test
|
||||
|
||||
##
|
||||
# Performs fuzzy check for equality on methods returning floats
|
||||
# on the basis of the Math::TOLERANCE constant.
|
||||
def check(a,b)
|
||||
a = a.to_f
|
||||
b = b.to_f
|
||||
if a.finite? and b.finite?
|
||||
(a-b).abs < Math::TOLERANCE
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
assert('Math.sin 0') do
|
||||
check(Math.sin(0), 0)
|
||||
check_float(Math.sin(0), 0)
|
||||
end
|
||||
|
||||
assert('Math.sin PI/2') do
|
||||
check(Math.sin(Math::PI / 2), 1)
|
||||
check_float(Math.sin(Math::PI / 2), 1)
|
||||
end
|
||||
|
||||
|
||||
@@ -32,61 +19,61 @@ assert('Fundamental trig identities') do
|
||||
s = Math.sin(a)
|
||||
c = Math.cos(a)
|
||||
t = Math.tan(a)
|
||||
result &= check(s, Math.cos(ca))
|
||||
result &= check(t, 1 / Math.tan(ca))
|
||||
result &= check(s ** 2 + c ** 2, 1)
|
||||
result &= check(t ** 2 + 1, (1/c) ** 2)
|
||||
result &= check((1/t) ** 2 + 1, (1/s) ** 2)
|
||||
result &= check_float(s, Math.cos(ca))
|
||||
result &= check_float(t, 1 / Math.tan(ca))
|
||||
result &= check_float(s ** 2 + c ** 2, 1)
|
||||
result &= check_float(t ** 2 + 1, (1/c) ** 2)
|
||||
result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
assert('Math.erf 0') do
|
||||
check(Math.erf(0), 0)
|
||||
check_float(Math.erf(0), 0)
|
||||
end
|
||||
|
||||
assert('Math.exp 0') do
|
||||
check(Math.exp(0), 1.0)
|
||||
check_float(Math.exp(0), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.exp 1') do
|
||||
check(Math.exp(1), 2.718281828459045)
|
||||
check_float(Math.exp(1), 2.718281828459045)
|
||||
end
|
||||
|
||||
assert('Math.exp 1.5') do
|
||||
check(Math.exp(1.5), 4.4816890703380645)
|
||||
check_float(Math.exp(1.5), 4.4816890703380645)
|
||||
end
|
||||
|
||||
assert('Math.log 1') do
|
||||
check(Math.log(1), 0)
|
||||
check_float(Math.log(1), 0)
|
||||
end
|
||||
|
||||
assert('Math.log E') do
|
||||
check(Math.log(Math::E), 1.0)
|
||||
check_float(Math.log(Math::E), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log E**3') do
|
||||
check(Math.log(Math::E**3), 3.0)
|
||||
check_float(Math.log(Math::E**3), 3.0)
|
||||
end
|
||||
|
||||
assert('Math.log2 1') do
|
||||
check(Math.log2(1), 0.0)
|
||||
check_float(Math.log2(1), 0.0)
|
||||
end
|
||||
|
||||
assert('Math.log2 2') do
|
||||
check(Math.log2(2), 1.0)
|
||||
check_float(Math.log2(2), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 1') do
|
||||
check(Math.log10(1), 0.0)
|
||||
check_float(Math.log10(1), 0.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 10') do
|
||||
check(Math.log10(10), 1.0)
|
||||
check_float(Math.log10(10), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 10**100') do
|
||||
check(Math.log10(10**100), 100.0)
|
||||
check_float(Math.log10(10**100), 100.0)
|
||||
end
|
||||
|
||||
assert('Math.cbrt') do
|
||||
@@ -94,25 +81,25 @@ assert('Math.cbrt') do
|
||||
cub = [-8, -1, 0, 1, 8]
|
||||
result = true
|
||||
cub.each_with_index do |v,i|
|
||||
result &= check(Math.cbrt(v), num[i])
|
||||
result &= check_float(Math.cbrt(v), num[i])
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
assert('Math.hypot') do
|
||||
check(Math.hypot(3, 4), 5.0)
|
||||
check_float(Math.hypot(3, 4), 5.0)
|
||||
end
|
||||
|
||||
assert('Math.frexp 1234') do
|
||||
n = 1234
|
||||
fraction, exponent = Math.frexp(n)
|
||||
check(Math.ldexp(fraction, exponent), n)
|
||||
check_float(Math.ldexp(fraction, exponent), n)
|
||||
end
|
||||
|
||||
assert('Math.erf 1') do
|
||||
check(Math.erf(1), 0.842700792949715)
|
||||
check_float(Math.erf(1), 0.842700792949715)
|
||||
end
|
||||
|
||||
assert('Math.erfc 1') do
|
||||
check(Math.erfc(1), 0.157299207050285)
|
||||
check_float(Math.erfc(1), 0.157299207050285)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user