Skip to content
Permalink
Browse files

Merge pull request #504 from cptobvious/math

Nearly complete Math module
  • Loading branch information
alex committed Mar 15, 2013
2 parents b2295eb + c12b071 commit f862db1d256edea037dad1704f0fbba401dfbed2
@@ -1,10 +1,3 @@
fails:Math.acos returns a float
fails:Math.acos returns the arccosine of the argument
fails:Math.acos raises an Errno::EDOM if the argument is greater than 1.0
fails:Math.acos raises an Errno::EDOM if the argument is less than -1.0
fails:Math.acos raises a TypeError if the string argument cannot be coerced with Float()
fails:Math.acos returns NaN given NaN
fails:Math.acos raises an TypeError if the argument cannot be coerced with Float()
fails:Math.acos raises a TypeError if the argument is nil
fails:Math.acos accepts any argument that can be coerced with Float()
fails:Math#acos is accessible as a private instance method
@@ -1,8 +1,2 @@
fails:Math.acosh returns a float
fails:Math.acosh returns the principle value of the inverse hyperbolic cosine of the argument
fails:Math.acosh it raises Errno::EDOM if the passed argument is less than -1.0 or greater than 1.0
fails:Math.acosh raises a TypeError if the argument cannot be coerced with Float()
fails:Math.acosh returns NaN given NaN
fails:Math.acosh raises a TypeError if the argument is nil
fails:Math.acosh accepts any argument that can be coerced with Float()
fails:Math#acosh is accessible as a private instance method
@@ -1,9 +1,3 @@
fails:Math.asin return a float
fails:Math.asin returns the arcsine of the argument
fails:Math.asin raises an Errno::EDOM if the argument is greater than 1.0
fails:Math.asin raises an Errno::EDOM if the argument is less than -1.0
fails:Math.asin raises a TypeError if the argument cannot be coerced with Float()
fails:Math.asin returns NaN given NaN
fails:Math.asin raises a TypeError if the argument is nil
fails:Math.asin accepts any argument that can be coerced with Float()
fails:Math#asin is accessible as a private instance method
@@ -1,7 +1 @@
fails:Math.asinh returns a float
fails:Math.asinh returns the inverse hyperbolic sin of the argument
fails:Math.asinh raises a TypeError if the argument cannot be coerced with Float()
fails:Math.asinh returns NaN given NaN
fails:Math.asinh raises a TypeError if the argument is nil
fails:Math.asinh accepts any argument that can be coerced with Float()
fails:Math#asinh is accessible as a private instance method
@@ -1,10 +1 @@
fails:Math.atan2 returns a float
fails:Math.atan2 returns the arc tangent of y, x
fails:Math.atan2 raises an TypeError if the argument cannot be coerced with Float()
fails:Math.atan2 raises a TypeError if the argument is nil
fails:Math.atan2 accepts any argument that can be coerced with Float()
fails:Math.atan2 returns positive zero when passed 0.0, 0.0
fails:Math.atan2 returns negative zero when passed -0.0, 0.0
fails:Math.atan2 returns Pi when passed 0.0, -0.0
fails:Math.atan2 returns -Pi when passed -0.0, -0.0
fails:Math#atan2 is accessible as a private instance method
@@ -1,7 +1 @@
fails:Math.atan returns a float
fails:Math.atan return the arctangent of the argument
fails:Math.atan raises a TypeError if the argument cannot be coerced with Float()
fails:Math.atan returns NaN given NaN
fails:Math.atan raises a TypeError if the argument is nil
fails:Math.atan accepts any argument that can be coerced with Float()
fails:Math#atan is accessible as a private instance method
@@ -1,9 +1,5 @@
fails:Math.atanh returns a float
fails:Math.atanh returns the inverse hyperbolic tangent of the argument
fails:Math.atanh raises an Math::DomainError for arguments greater than 1.0
fails:Math.atanh raises an Math::DomainError for arguments less than -1.0
fails:Math#atanh is a private instance method
fails:Math#atanh returns a float
fails:Math#atanh returns the inverse hyperbolic tangent of the argument
fails:Math#atanh raises an Math::DomainError for arguments greater than 1.0
fails:Math#atanh raises an Math::DomainError for arguments less than -1.0
@@ -1,7 +1 @@
fails:Math.cos returns a float
fails:Math.cos returns the cosine of the argument expressed in radians
fails:Math.cos raises a TypeError unless the argument is Numeric and has #to_f
fails:Math.cos returns NaN given NaN
fails:Math.cos raises a TypeError if the argument is nil
fails:Math.cos coerces its argument with #to_f
fails:Math#cos is accessible as a private instance method
@@ -1,7 +1 @@
fails:Math.cosh returns a float
fails:Math.cosh returns the hyperbolic cosine of the argument
fails:Math.cosh raises a TypeError if the argument cannot be coerced with Float()
fails:Math.cosh returns NaN given NaN
fails:Math.cosh raises a TypeError if the argument is nil
fails:Math.cosh accepts any argument that can be coerced with Float()
fails:Math#cosh is accessible as a private instance method
@@ -1,2 +1 @@
fails:Math.exp raises a TypeError if the argument cannot be coerced with Float()
fails:Math.exp returns NaN given NaN
@@ -1,6 +1 @@
fails:Math.frexp returns the normalized fraction and exponent
fails:Math.frexp raises a TypeError if the argument cannot be coerced with Float()
fails:Math.frexp returns NaN given NaN
fails:Math.frexp raises a TypeError if the argument is nil
fails:Math.frexp accepts any argument that can be coerced with Float()
fails:Math#frexp is accessible as a private instance method
@@ -1,7 +1 @@
fails:Math.hypot returns a float
fails:Math.hypot returns the length of the hypotenuse of a right triangle with legs given by the arguments
fails:Math.hypot raises a TypeError if the argument cannot be coerced with Float()
fails:Math.hypot returns NaN given NaN
fails:Math.hypot raises a ArgumentError if the argument is nil
fails:Math.hypot accepts any argument that can be coerced with Float()
fails:Math#hypot is accessible as a private instance method
@@ -1,11 +1,2 @@
fails:Math.ldexp returns a float
fails:Math.ldexp returns the argument multiplied by 2**n
fails:Math.ldexp raises a TypeError if the first argument cannot be coerced with Float()
fails:Math.ldexp returns NaN given NaN
fails:Math.ldexp raises RangeError if NaN is given as the second arg
fails:Math.ldexp raises an TypeError if the second argument cannot be coerced with Integer()
fails:Math.ldexp raises a TypeError if the first argument is nil
fails:Math.ldexp raises a TypeError if the second argument is nil
fails:Math.ldexp accepts any first argument that can be coerced with Float()
fails:Math.ldexp accepts any second argument that can be coerced with Integer()
fails:Math#ldexp is accessible as a private instance method
@@ -1,8 +1,2 @@
fails:Math.log10 returns a float
fails:Math.log10 return the base-10 logarithm of the argument
fails:Math.log10 raises an Errno::EDOM if the argument is less than 0
fails:Math.log10 raises a TypeError if the argument cannot be coerced with Float()
fails:Math.log10 returns NaN given NaN
fails:Math.log10 raises a TypeError if the argument is nil
fails:Math.log10 accepts any argument that can be coerced with Float()
fails:Math#log10 is accessible as a private instance method
@@ -1,8 +1,2 @@
fails:Math.log2 returns a float
fails:Math.log2 returns the natural logarithm of the argument
fails:Math.log2 raises an Errno::EDOM if the argument is less than 0
fails:Math.log2 raises an TypeError if the argument cannot be coerced with Float()
fails:Math.log2 raises an TypeError if passed a numerical argument as a string
fails:Math.log2 returns NaN given NaN
fails:Math.log2 raises a TypeError if the argument is nil
fails:Math.log2 accepts any argument that can be coerced with Float()
@@ -2,4 +2,3 @@ fails:Math.log raises an Errno::EDOM if the argument is less than 0
fails:Math.log raises a TypeError if the argument cannot be coerced with Float()
fails:Math.log raises a TypeError for numerical values passed as string
fails:Math.log raises a TypeError when the numerical base cannot be coerced to a float
fails:Math.log returns NaN given NaN
@@ -1,2 +1 @@
fails:Math.sin raises a TypeError if the argument cannot be coerced with Float()
fails:Math.sin returns NaN given NaN
@@ -1,7 +1 @@
fails:Math.sinh returns a float
fails:Math.sinh returns the hyperbolic sin of the argument
fails:Math.sinh raises a TypeError if the argument cannot be coerced with Float()
fails:Math.sinh returns NaN given NaN
fails:Math.sinh raises a TypeError if the argument is nil
fails:Math.sinh accepts any argument that can be coerced with Float()
fails:Math#sinh is accessible as a private instance method
@@ -1,2 +1 @@
fails:Math.sqrt raises a TypeError if the argument cannot be coerced with Float()
fails:Math.sqrt returns NaN given NaN
@@ -1,8 +1 @@
fails:Math.tan returns a float
fails:Math.tan returns the tangent of the argument
fails:Math.tan returns NaN if called with +-Infinitty
fails:Math.tan raises a TypeError if the argument cannot be coerced with Float()
fails:Math.tan returns NaN given NaN
fails:Math.tan raises a TypeError if the argument is nil
fails:Math.tan accepts any argument that can be coerced with Float()
fails:Math#tan is accessible as a private instance method
@@ -1,7 +1 @@
fails:Math.tanh returns a float
fails:Math.tanh returns the hyperbolic tangent of the argument
fails:Math.tanh raises an TypeError if the argument cannot be coerced with Float()
fails:Math.tanh returns NaN given NaN
fails:Math.tanh raises a TypeError if the argument is nil
fails:Math.tanh accepts any argument that can be coerced with Float()
fails:Math#tanh is accessible as a private instance method
@@ -14,27 +14,69 @@ def test_pi(self, space):
w_res = space.execute("return Math::PI")
assert space.float_w(w_res) == math.pi

def test_exp(self, space):
w_res = space.execute("return [Math.exp(0.0), Math.exp(1)]")
assert self.unwrap(space, w_res) == [1, math.exp(1)]

def test_sqrt(self, space):
w_res = space.execute("return [Math.sqrt(4), Math.sqrt(28)]")
assert self.unwrap(space, w_res) == [2, math.sqrt(28)]

def test_e(self, space):
w_res = space.execute("return Math::E")
assert space.float_w(w_res) == math.e

def test_log(self, space):
w_res = space.execute("return Math.log(4, 10)")
self.assert_float_equal(space.float_w(w_res), math.log(4, 10))
def test_acos(self, space):
w_res = space.execute("return [Math.acos(0), Math.acos(1)]")
assert self.unwrap(space, w_res) == [math.acos(0), 0]

w_res = space.execute("return Math.log(28)")
self.assert_float_equal(space.float_w(w_res), math.log(28))
def test_acosh(self, space):
w_res = space.execute("return [Math.acosh(1), Math.acosh(2)]")
assert self.unwrap(space, w_res) == [0, math.acosh(2)]

w_res = space.execute("return Math.log(3, 4)")
self.assert_float_equal(space.float_w(w_res), math.log(3, 4))
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "acosh"'):
space.execute("Math.acosh(0)")

def test_asin(self, space):
w_res = space.execute("return [Math.asin(0), Math.asin(1)]")
assert self.unwrap(space, w_res) == [0, math.asin(1)]

def test_asinh(self, space):
w_res = space.execute("return [Math.asinh(0), Math.asinh(1)]")
assert self.unwrap(space, w_res) == [math.asinh(0), math.asinh(1)]

def test_atan(self, space):
w_res = space.execute("return [Math.atan(0), Math.atan(1)]")
assert self.unwrap(space, w_res) == [0, math.atan(1)]

def test_atan2(self, space):
w_res = space.execute("return [Math.atan2(-0.0, -1.0), Math.atan2(-1, -1.0)]")
assert self.unwrap(space, w_res) == [math.atan2(-0.0, -1.0), math.atan2(-1.0, -1)]

def test_atanh(self, space):
w_res = space.execute("return [Math.atanh(1), Math.atanh(-1), Math.atanh(0), Math.atanh(0.5)]")
assert self.unwrap(space, w_res) == [float("inf"), float("-inf"), 0, math.atanh(0.5)]

with self.raises(space, "DomainError", 'Numerical argument is out of domain - "atanh"'):
space.execute("Math.atanh(2)")

def test_cbrt(self, space):
w_res = space.execute("return [Math.cbrt(-8), Math.cbrt(-1), Math.cbrt(0)]")
assert self.unwrap(space, w_res) == [-2.0, -1.0, 0]

w_res = space.execute("return Math.cbrt(8)")
self.assert_float_equal(space.float_w(w_res), 2.0)

w_res = space.execute("return Math.cbrt(64)")
self.assert_float_equal(space.float_w(w_res), 4.0)

def test_cos(self, space):
w_res = space.execute("return [Math.cos(0), Math.cos(1)]")
assert self.unwrap(space, w_res) == [1, math.cos(1)]

def test_cosh(self, space):
w_res = space.execute("return [Math.cosh(0), Math.cosh(1), Math.cosh(123123)]")
assert self.unwrap(space, w_res) == [1, math.cosh(1), float("inf")]

def test_exp(self, space):
w_res = space.execute("return [Math.exp(0.0), Math.exp(1)]")
assert self.unwrap(space, w_res) == [1, math.exp(1)]

def test_frexp(self, space):
w_res = space.execute("return Math.frexp(1234)")
assert self.unwrap(space, w_res) == [math.frexp(1234)[0], 11]

def test_gamma(self, space):
w_res = space.execute("return Math.gamma(5.0)")
@@ -65,3 +107,82 @@ def test_gamma(self, space):

w_res = space.execute("return Math.gamma(Float::NAN)")
assert math.isnan(space.float_w(w_res))

def test_hypot(self, space):
w_res = space.execute("return Math.hypot(3, 4)")
assert self.unwrap(space, w_res) == 5

def test_ldexp(self, space):
w_res = space.execute("return Math.ldexp(Math.frexp(1234)[0], 11)")
assert self.unwrap(space, w_res) == 1234

def test_log(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log"'):
space.execute("Math.log(-1)")

w_res = space.execute("return Math.log(0)")
assert space.float_w(w_res) == float("-inf")

w_res = space.execute("return Math.log(4, 10)")
self.assert_float_equal(space.float_w(w_res), math.log(4, 10))

w_res = space.execute("return Math.log(28)")
self.assert_float_equal(space.float_w(w_res), math.log(28))

w_res = space.execute("return Math.log(3, 4)")
self.assert_float_equal(space.float_w(w_res), math.log(3, 4))

def test_log10(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log10"'):
space.execute("Math.log10(-1)")

w_res = space.execute("return Math.log10(0)")
assert space.float_w(w_res) == float("-inf")

w_res = space.execute("return Math.log10(1)")
assert space.float_w(w_res) == 0.0

w_res = space.execute("return Math.log10(10)")
assert space.float_w(w_res) == 1.0

def test_log2(self, space):
with self.raises(space, "DomainError", 'Numerical argument is out of domain - "log2"'):
space.execute("Math.log2(-1)")

w_res = space.execute("return Math.log2(0)")
assert space.float_w(w_res) == float("-inf")

w_res = space.execute("return Math.log2(1)")
assert space.float_w(w_res) == 0.0

w_res = space.execute("return Math.log2(2)")
assert space.float_w(w_res) == 1.0

w_res = space.execute("return Math.log2(32768)")
assert space.float_w(w_res) == 15.0

w_res = space.execute("return Math.log2(65536)")
assert space.float_w(w_res) == 16.0

def test_sin(self, space):
w_res = space.execute("return [Math.sin(0), Math.sin(1)]")
assert self.unwrap(space, w_res) == [0, math.sin(1)]

def test_sinh(self, space):
w_res = space.execute("return [Math.sinh(0), Math.sinh(2), Math.sinh(1234)]")
assert self.unwrap(space, w_res) == [0, math.sinh(2), float("inf")]

def test_sqrt(self, space):
w_res = space.execute("return [Math.sqrt(4), Math.sqrt(28)]")
assert self.unwrap(space, w_res) == [2, math.sqrt(28)]

def test_tan(self, space):
w_res = space.execute("return Math.tan(Float::INFINITY)")
assert math.isnan(space.float_w(w_res))

w_res = space.execute("return [Math.tan(0), Math.tan(1)]")
assert self.unwrap(space, w_res) == [0, math.tan(1)]

def test_tanh(self, space):
w_res = space.execute("return [Math.tanh(0), Math.tanh(1), Math.tanh(1234)]")
assert self.unwrap(space, w_res) == [0, math.tanh(1), 1.0]

0 comments on commit f862db1

Please sign in to comment.
You can’t perform that action at this time.