Python math.e() Examples

The following are 30 code examples of math.e(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module math , or try the search function .
Example #1
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_delete():
    e = Evaluator()
    e.symbol_table['a'] = 0
    e.symbol_table['b'] = 0
    e.symbol_table['c'] = 0
    e.run('del a, b, c')
    assert 'a' not in e.symbol_table
    assert 'b' not in e.symbol_table
    assert 'c' not in e.symbol_table
    e.symbol_table['l'] = [1, 2, 3, 4]
    e.run('del l[0]')
    assert e.symbol_table['l'] == [2, 3, 4]

    err = 'This delete method is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('del l[2:3]')

    e.symbol_table['dt'] = datetime.now()
    err = 'This delete method is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('del dt.year') 
Example #2
Source File: test_quaternion.py    From quaternion with MIT License 6 votes vote down vote up
def test_isclose():
    from quaternion import x, y

    assert np.array_equal(quaternion.isclose([1e10*x, 1e-7*y], [1.00001e10*x, 1e-8*y], rtol=1.e-5, atol=2.e-8),
                          np.array([True, False]))
    assert np.array_equal(quaternion.isclose([1e10*x, 1e-8*y], [1.00001e10*x, 1e-9*y], rtol=1.e-5, atol=2.e-8),
                          np.array([True, True]))
    assert np.array_equal(quaternion.isclose([1e10*x, 1e-8*y], [1.0001e10*x, 1e-9*y], rtol=1.e-5, atol=2.e-8),
                          np.array([False, True]))
    assert np.array_equal(quaternion.isclose([x, np.nan*y], [x, np.nan*y]),
                          np.array([True, False]))
    assert np.array_equal(quaternion.isclose([x, np.nan*y], [x, np.nan*y], equal_nan=True),
                          np.array([True, True]))

    np.random.seed(1234)
    a = quaternion.as_quat_array(np.random.random((3, 5, 4)))
    assert quaternion.allclose(1e10 * a, 1.00001e10 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
    assert quaternion.allclose(1e-7 * a, 1e-8 * a, rtol=1.e-5, atol=2.e-8) == False
    assert quaternion.allclose(1e10 * a, 1.00001e10 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
    assert quaternion.allclose(1e-8 * a, 1e-9 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
    assert quaternion.allclose(1e10 * a, 1.0001e10 * a, rtol=1.e-5, atol=2.e-8) == False
    assert quaternion.allclose(1e-8 * a, 1e-9 * a, rtol=1.e-5, atol=2.e-8, verbose=True) == True
    assert quaternion.allclose(np.nan * a, np.nan * a) == False
    assert quaternion.allclose(np.nan * a, np.nan * a, equal_nan=True, verbose=True) == True 
Example #3
Source File: programs.py    From raveberry with GNU Lesser General Public License v3.0 6 votes vote down vote up
def stop(self) -> None:
        try:
            os.close(self.cava_fifo)
        except OSError as e:
            logging.info("fifo already closed: %s", e)
        except TypeError as e:
            logging.info("fifo does not exist: %s", e)

        if self.cava_process:
            self.cava_process.terminate()

        try:
            os.remove(self.cava_fifo_path)
        except FileNotFoundError as e:
            # the file was already deleted
            logging.info("%s not found while deleting: %s", self.cava_fifo_path, e) 
Example #4
Source File: test_quaternion.py    From quaternion with MIT License 6 votes vote down vote up
def test_allclose(Qs):
    for q in Qs[Qs_nonnan]:
        assert quaternion.allclose(q, q, rtol=0.0, atol=0.0)
    assert quaternion.allclose(Qs[Qs_nonnan], Qs[Qs_nonnan], rtol=0.0, atol=0.0)

    for q in Qs[Qs_finitenonzero]:
        assert quaternion.allclose(q, q*(1+1e-13), rtol=1.1e-13, atol=0.0)
        assert ~quaternion.allclose(q, q*(1+1e-13), rtol=0.9e-13, atol=0.0)
        for e in [quaternion.one, quaternion.x, quaternion.y, quaternion.z]:
            assert quaternion.allclose(q, q+(1e-13*e), rtol=0.0, atol=1.1e-13)
            assert ~quaternion.allclose(q, q+(1e-13*e), rtol=0.0, atol=0.9e-13)
    assert quaternion.allclose(Qs[Qs_finitenonzero], Qs[Qs_finitenonzero]*(1+1e-13), rtol=1.1e-13, atol=0.0)
    assert ~quaternion.allclose(Qs[Qs_finitenonzero], Qs[Qs_finitenonzero]*(1+1e-13), rtol=0.9e-13, atol=0.0)
    for e in [quaternion.one, quaternion.x, quaternion.y, quaternion.z]:
        assert quaternion.allclose(Qs[Qs_finite], Qs[Qs_finite]+(1e-13*e), rtol=0.0, atol=1.1e-13)
        assert ~quaternion.allclose(Qs[Qs_finite], Qs[Qs_finite]+(1e-13*e), rtol=0.0, atol=0.9e-13)
    assert quaternion.allclose(Qs[Qs_zero], Qs[Qs_zero]*2, rtol=0.0, atol=1.1e-13)

    for qnan in Qs[Qs_nan]:
        assert ~quaternion.allclose(qnan, qnan, rtol=1.0, atol=1.0)
        for q in Qs:
            assert ~quaternion.allclose(q, qnan, rtol=1.0, atol=1.0) 
Example #5
Source File: test_quaternion.py    From quaternion with MIT License 6 votes vote down vote up
def test_from_spherical_coords():
    np.random.seed(1843)
    random_angles = [[np.random.uniform(-np.pi, np.pi), np.random.uniform(-np.pi, np.pi)]
                     for i in range(5000)]
    for vartheta, varphi in random_angles:
        q = quaternion.from_spherical_coords(vartheta, varphi)
        assert abs((np.quaternion(0, 0, 0, varphi / 2.).exp() * np.quaternion(0, 0, vartheta / 2., 0).exp())
                   - q) < 1.e-15
        xprime = q * quaternion.x * q.inverse()
        yprime = q * quaternion.y * q.inverse()
        zprime = q * quaternion.z * q.inverse()
        nhat = np.quaternion(0.0, math.sin(vartheta)*math.cos(varphi), math.sin(vartheta)*math.sin(varphi),
                             math.cos(vartheta))
        thetahat = np.quaternion(0.0, math.cos(vartheta)*math.cos(varphi), math.cos(vartheta)*math.sin(varphi),
                                 -math.sin(vartheta))
        phihat = np.quaternion(0.0, -math.sin(varphi), math.cos(varphi), 0.0)
        assert abs(xprime - thetahat) < 1.e-15
        assert abs(yprime - phihat) < 1.e-15
        assert abs(zprime - nhat) < 1.e-15
    assert np.max(np.abs(quaternion.from_spherical_coords(random_angles)
                         - np.array([quaternion.from_spherical_coords(vartheta, varphi)
                                     for vartheta, varphi in random_angles]))) < 1.e-15 
Example #6
Source File: test_callbacks.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_callback_register_double(self):
        # Issue #8275: buggy handling of callback args under Win64
        # NOTE: should be run on release builds as well
        dll = CDLL(_ctypes_test.__file__)
        CALLBACK = CFUNCTYPE(c_double, c_double, c_double, c_double,
                             c_double, c_double)
        # All this function does is call the callback with its args squared
        func = dll._testfunc_cbk_reg_double
        func.argtypes = (c_double, c_double, c_double,
                         c_double, c_double, CALLBACK)
        func.restype = c_double

        def callback(a, b, c, d, e):
            return a + b + c + d + e

        result = func(1.1, 2.2, 3.3, 4.4, 5.5, CALLBACK(callback))
        self.assertEqual(result,
                         callback(1.1*1.1, 2.2*2.2, 3.3*3.3, 4.4*4.4, 5.5*5.5)) 
Example #7
Source File: SimpleCalc.py    From phpsploit with GNU General Public License v3.0 6 votes vote down vote up
def evaluateStack( s ):
    op = s.pop()
    if op == 'unary -':
        return -evaluateStack( s )
    if op in "+-*/^":
        op2 = evaluateStack( s )
        op1 = evaluateStack( s )
        return opn[op]( op1, op2 )
    elif op == "PI":
        return math.pi # 3.1415926535
    elif op == "E":
        return math.e  # 2.718281828
    elif op in fn:
        return fn[op]( evaluateStack( s ) )
    elif op[0].isalpha():
        if op in variables:
            return variables[op]
        raise Exception("invalid identifier '%s'" % op)
    else:
        return float( op ) 
Example #8
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_attribute():
    e = Evaluator()
    e.symbol_table['dt'] = datetime.now()
    e.run('x = dt.year')
    assert e.symbol_table['x'] == e.symbol_table['dt'].year

    err = 'You can not access `test_test_test` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('y = dt.test_test_test')

    assert 'y' not in e.symbol_table

    err = 'You can not access `asdf` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('z = x.asdf')

    e.symbol_table['math'] = math
    err = 'You can not access `__module__` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('math.__module__')

    e.symbol_table['datetime'] = datetime
    err = 'You can not access `test_test` attribute'
    with pytest.raises(BadSyntax, match=err):
        e.run('datetime.test_test') 
Example #9
Source File: test_quaternion.py    From quaternion with MIT License 6 votes vote down vote up
def test_as_euler_angles():
    np.random.seed(1843)
    random_angles = [[np.random.uniform(-np.pi, np.pi),
                      np.random.uniform(-np.pi, np.pi),
                      np.random.uniform(-np.pi, np.pi)]
                     for i in range(5000)]
    for alpha, beta, gamma in random_angles:
        R1 = quaternion.from_euler_angles(alpha, beta, gamma)
        R2 = quaternion.from_euler_angles(*list(quaternion.as_euler_angles(R1)))
        d = quaternion.rotation_intrinsic_distance(R1, R2)
        assert d < 6e3*eps, ((alpha, beta, gamma), R1, R2, d)  # Can't use allclose here; we don't care about rotor sign
    q0 = quaternion.quaternion(0, 0.6, 0.8, 0)
    assert q0.norm() == 1.0
    assert abs(q0 - quaternion.from_euler_angles(*list(quaternion.as_euler_angles(q0)))) < 1.e-15


# Unary bool returners 
Example #10
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_dictcomp():
    e = Evaluator()
    assert e.run('{k+1: v**2 for k, v in {1: 1, 2: 11, 3: 111}.items()}') == {
        2: 1,
        3: 121,
        4: 12321,
    }
    assert 'k' not in e.symbol_table
    assert 'v' not in e.symbol_table
    e.run('a = {k+1: v**2 for k, v in {1: 1, 2: 11, 3: 111}.items()}')
    assert e.symbol_table['a'] == {
        2: 1,
        3: 121,
        4: 12321,
    }
    assert 'k' not in e.symbol_table
    assert 'v' not in e.symbol_table 
Example #11
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_binop():
    e = Evaluator()
    assert e.run('1 + 2') == 1 + 2
    assert e.run('3 & 2') == 3 & 2
    assert e.run('1 | 2') == 1 | 2
    assert e.run('3 ^ 2') == 3 ^ 2
    assert e.run('3 / 2') == 3 / 2
    assert e.run('3 // 2') == 3 // 2
    assert e.run('3 << 2') == 3 << 2
    with pytest.raises(TypeError):
        e.run('2 @ 3')
    assert e.run('3 * 2') == 3 * 2
    assert e.run('33 % 4') == 33 % 4
    assert e.run('3 ** 2') == 3 ** 2
    assert e.run('100 >> 2') == 100 >> 2
    assert e.run('3 - 1') == 3 - 1 
Example #12
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_augassign():
    e = Evaluator()
    e.symbol_table['a'] = 0
    e.run('a += 1')
    assert e.symbol_table['a'] == 1
    e.symbol_table['l'] = [1, 2, 3, 4]
    e.run('l[0] -= 1')
    assert e.symbol_table['l'] == [0, 2, 3, 4]

    err = 'This assign method is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('l[2:3] += 20')

    e.symbol_table['dt'] = datetime.now()
    err = 'This assign method is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('dt.year += 2000') 
Example #13
Source File: fourFn.py    From phpsploit with GNU General Public License v3.0 6 votes vote down vote up
def evaluateStack( s ):
    op = s.pop()
    if op == 'unary -':
        return -evaluateStack( s )
    if op in "+-*/^":
        op2 = evaluateStack( s )
        op1 = evaluateStack( s )
        return opn[op]( op1, op2 )
    elif op == "PI":
        return math.pi # 3.1415926535
    elif op == "E":
        return math.e  # 2.718281828
    elif op in fn:
        return fn[op]( evaluateStack( s ) )
    elif op[0].isalpha():
        raise Exception("invalid identifier '%s'" % op)
    else:
        return float( op ) 
Example #14
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_global():
    e = Evaluator()
    err = 'You can not use `global` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run('global x') 
Example #15
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_try():
    e = Evaluator()
    err = 'You can not use `try` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run(
            '''
try:
    x = 1
except:
    pass
'''
        )
    assert 'x' not in e.symbol_table 
Example #16
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_str():
    e = Evaluator()
    assert e.run('"asdf"') == 'asdf'
    e.run('a = "asdf"')
    assert e.symbol_table['a'] == 'asdf' 
Example #17
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_slice():
    e = Evaluator()
    e.symbol_table['obj'] = GetItemSpy()
    e.run('obj[10:20:3]')
    s = e.symbol_table['obj'].queue.pop()
    assert isinstance(s, slice)
    assert s.start == 10
    assert s.stop == 20
    assert s.step == 3 
Example #18
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_index():
    e = Evaluator()
    e.symbol_table['obj'] = GetItemSpy()
    e.run('obj[10]')
    index = e.symbol_table['obj'].queue.pop()
    assert index == 10
    e.run('obj["asdf"]')
    index = e.symbol_table['obj'].queue.pop()
    assert index == 'asdf' 
Example #19
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_importfrom():
    e = Evaluator()
    err = 'You can not import anything'
    with pytest.raises(BadSyntax, match=err):
        e.run('from os import path')
    assert 'path' not in e.symbol_table 
Example #20
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_import():
    e = Evaluator()
    err = 'You can not import anything'
    with pytest.raises(BadSyntax, match=err):
        e.run('import sys')
    assert 'sys' not in e.symbol_table 
Example #21
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_ifexp():
    e = Evaluator()
    assert e.run('100 if 1 == 1 else 200') == 100
    assert e.run('100 if 1 == 2 else 200') == 200 
Example #22
Source File: test_quaternion.py    From quaternion with MIT License 5 votes vote down vote up
def test_from_euler_angles():
    np.random.seed(1843)
    random_angles = [[np.random.uniform(-np.pi, np.pi),
                      np.random.uniform(-np.pi, np.pi),
                      np.random.uniform(-np.pi, np.pi)]
                     for i in range(5000)]
    for alpha, beta, gamma in random_angles:
        assert abs((np.quaternion(0, 0, 0, alpha / 2.).exp()
                    * np.quaternion(0, 0, beta / 2., 0).exp()
                    * np.quaternion(0, 0, 0, gamma / 2.).exp()
                   )
                   - quaternion.from_euler_angles(alpha, beta, gamma)) < 1.e-15
    assert np.max(np.abs(quaternion.from_euler_angles(random_angles)
                         - np.array([quaternion.from_euler_angles(alpha, beta, gamma)
                                     for alpha, beta, gamma in random_angles]))) < 1.e-15 
Example #23
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_generator_exp():
    e = Evaluator()
    e.symbol_table['r'] = [1, 2, 3]
    err = 'Defining new generator expression is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run('x = (i ** 2 for i in r)')
    assert 'x' not in e.symbol_table 
Example #24
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_formattedvalue():
    e = Evaluator()
    e.symbol_table['before'] = 123456
    e.run('after = f"change {before} to {before:,}!"')
    assert e.symbol_table['after'] == 'change 123456 to 123,456!' 
Example #25
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_call():
    e = Evaluator()
    e.symbol_table['date'] = date
    e.run('x = date(2019, 10, day=7)')
    assert e.symbol_table['x'] == date(2019, 10, day=7)

    e.symbol_table['math'] = math
    e.run('y = math.sqrt(121)')
    assert e.symbol_table['y'] == math.sqrt(121)

    e.symbol_table['datetime'] = datetime
    e.run('z = datetime.now().date()')
    assert e.symbol_table['z'] == datetime.now().date() 
Example #26
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with():
    e = Evaluator()
    err = 'You can not use `with` syntax'
    with pytest.raises(BadSyntax, match=err):
        e.run(
            '''
with some:
    x = 1
'''
        )
    assert 'x' not in e.symbol_table 
Example #27
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_functiondef():
    e = Evaluator()
    err = 'Defining new function via def syntax is not allowed'
    with pytest.raises(BadSyntax, match=err):
        e.run(
            '''
def abc():
    pass

'''
        )
    assert 'abc' not in e.symbol_table 
Example #28
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_dict():
    e = Evaluator()
    assert e.run('{1: 111, 2: 222}') == {1: 111, 2: 222}
    e.run('a = {1: 111, 2: 222}')
    assert e.symbol_table['a'] == {1: 111, 2: 222} 
Example #29
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_expr():
    e = Evaluator()
    assert e.run('True') is True
    assert e.run('False') is False
    assert e.run('None') is None
    assert e.run('123') == 123
    assert e.run('"abc"') == 'abc'
    assert e.run('[1, 2, 3]') == [1, 2, 3]
    assert e.run('(1, 2, 3, 3)') == (1, 2, 3, 3)
    assert e.run('{1, 2, 3, 3}') == {1, 2, 3}
    assert e.run('{1: 111, 2: 222}') == {1: 111, 2: 222} 
Example #30
Source File: calc_test.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_ellipsis():
    e = Evaluator()
    assert e.run('...') == Ellipsis