def test_int(self): self.assertEqual(0, int(bigfloat.Number(""))) self.assertEqual(-7, int(bigfloat.Number("-7"))) self.assertEqual(-7, int(bigfloat.Number("-7.5"))) self.assertEqual(13423412341234, int(bigfloat.Number("13423412341234"))) self.assertEqual(0, int(bigfloat.Number("0.000000000001431")))
def test_s_to_bf_to_s(self): self.assertEqual(bigfloat.Number("0"), bigfloat.Number(" -0")) self.assertEqual(bigfloat.Number("0"), bigfloat.Number(" +0")) self.assertEqual(bigfloat.Number("0"), bigfloat.Number("0.00000")) self.assertEqual(bigfloat.Number("1"), bigfloat.Number("1.00000")) self.assertEqual( bigfloat.Number("0").get_sign(), bigfloat.Number("-0").get_sign())
def test_cmp(self): a = bigfloat.Number("0") b = bigfloat.Number("-0") self.assertTrue(a == b) a = bigfloat.Number("0.001") b = bigfloat.Number("0") self.assertTrue(a > b) self.assertTrue(b < a)
def atom(token): "数は数にし、それ以外のトークンはシンボルにする。" try: if token[0] in [str(i) for i in range(10)] or (token[0] in ["+","-"] and token[1] in [str(i) for i in range(10)]): return bigfloat.Number(token) except: pass return Symbol(token)
def test_float(self): self.assertEqual(0.0, float(bigfloat.Number(""))) self.assertEqual(-7, float(bigfloat.Number("-7"))) self.assertEqual(13423412341234, float(bigfloat.Number("13423412341234"))) self.assertEqual(0.0000001431, float(bigfloat.Number("0.0000001431")))
def test_div_helper(ans, a, b): u = bigfloat.Number(ans) v = bigfloat.Number(a) w = bigfloat.Number(b) self.assertEqual(u, v / w)
def test_rand(self): bigfloat.rand_seed(123) self.assertGreater(bigfloat.rand(), bigfloat.Number()) self.assertLess(bigfloat.rand(), bigfloat.Number("1")) self.assertNotEqual(bigfloat.rand(), bigfloat.rand())
def test_ln_helper(a1, a2, a3, b, bound): a = bigfloat.ln(bigfloat.Number(a1)) + bigfloat.ln( bigfloat.Number(a2)) + bigfloat.ln(bigfloat.Number(a3)) b = bigfloat.ln(bigfloat.Number(b)) test_almost_equal(a, b, bound)
def test_almost_equal(a, b, bound): err = a - b self.assertGreaterEqual(bigfloat.Number(bound), err) self.assertLessEqual(bigfloat.Number("-" + bound), err)
def test_ln(self): def test_almost_equal(a, b, bound): err = a - b self.assertGreaterEqual(bigfloat.Number(bound), err) self.assertLessEqual(bigfloat.Number("-" + bound), err) test_almost_equal(bigfloat.Number("0"), bigfloat.ln(bigfloat.Number("1")), "0.00001") test_almost_equal( bigfloat.Number("3") * bigfloat.ln(bigfloat.Number("1")), bigfloat.ln(bigfloat.Number("1")), "0.00001") test_almost_equal( bigfloat.Number("3") * bigfloat.ln(bigfloat.Number("8")), bigfloat.ln(bigfloat.Number("512")), "0.00001") test_almost_equal( bigfloat.Number("3") * bigfloat.ln(bigfloat.Number("0.3")), bigfloat.ln(bigfloat.Number("0.027")), "0.00001") test_almost_equal(bigfloat.Number("1"), bigfloat.ln(bigfloat.Number("2.71828 18284")), "0.00001") def test_ln_helper(a1, a2, a3, b, bound): a = bigfloat.ln(bigfloat.Number(a1)) + bigfloat.ln( bigfloat.Number(a2)) + bigfloat.ln(bigfloat.Number(a3)) b = bigfloat.ln(bigfloat.Number(b)) test_almost_equal(a, b, bound) test_ln_helper("100", "10", "1", "1000", "0.00001") test_ln_helper("2", "3", "7", "42", "0.00001") test_ln_helper("0.2", "0.3", "0.9", "0.054", "0.00001")
def test_exp_helper(a1, a2, a3, b, bound): a = bigfloat.exp(bigfloat.Number(a1)) * bigfloat.exp( bigfloat.Number(a2)) * bigfloat.exp(bigfloat.Number(a3)) b = bigfloat.exp(bigfloat.Number(b)) test_almost_equal(a, b, bound)
def test_exp(self): self.assertEqual(bigfloat.Number("1"), bigfloat.exp(bigfloat.Number("0"))) self.assertEqual( bigfloat.Number("1") / bigfloat.exp(bigfloat.Number("1")), bigfloat.exp(bigfloat.Number("-1"))) self.assertEqual( bigfloat.Number("1") / bigfloat.exp(bigfloat.Number("123")), bigfloat.exp(bigfloat.Number("-123"))) self.assertEqual( bigfloat.Number("1") / bigfloat.exp(bigfloat.Number("0.3")), bigfloat.exp(bigfloat.Number("-0.3"))) def test_almost_equal(a, b, bound): err = a - b self.assertGreaterEqual(bigfloat.Number(bound), err) self.assertLessEqual(bigfloat.Number("-" + bound), err) test_almost_equal(bigfloat.exp(bigfloat.Number("1")), bigfloat.Number("2.71828 18284"), "0.00001") def test_exp_helper(a1, a2, a3, b, bound): a = bigfloat.exp(bigfloat.Number(a1)) * bigfloat.exp( bigfloat.Number(a2)) * bigfloat.exp(bigfloat.Number(a3)) b = bigfloat.exp(bigfloat.Number(b)) test_almost_equal(a, b, bound) test_exp_helper("1", "1", "1", "3", "0.0000001") test_exp_helper("2", "3", "7", "12", "0.0000001") test_exp_helper("0.2", "0.3", "0.9", "1.4", "0.0000001")
def test_sqrt_helper(ans, a): u = bigfloat.Number(ans) v = bigfloat.Number(a) self.assertEqual(u, bigfloat.sqrt(v))