예제 #1
0
def test_addition():
    x = Const(1.23478755)
    y = Const(356.1)
    ans = x + y
    assert ans.v == 1.23478755 + 356.1

    ans = Float("10.0") + Float("103")
    assert ans.v == 10.0 + 103

    with pytest.raises(TypeError):
        Const("1.23") + "abc"

    with pytest.raises(TypeError):
        "abc" + Const(1.23)

    with pytest.raises(TypeError):
        Const("1.23") + 5

    with pytest.raises(TypeError):
        5 + Const("1.23")

    with pytest.raises(TypeError):
        Const("1.23") + 5.1

    with pytest.raises(TypeError):
        5.1 + Const("1.23")
예제 #2
0
def test_addition():
    x = Float(1.23478755, 3)
    y = Float(356.1, 4)
    ans = x + y
    assert ans.str == "3.573E+02"

    ans = Float("10.0") + Float("103")
    assert ans.str == "1.13E+02"

    with pytest.raises(TypeError):
        Float("1.23") + "abc"

    with pytest.raises(TypeError):
        "abc" + Float(1.23, num_sig_figs=3)

    with pytest.raises(TypeError):
        Float("1.23") + 5

    with pytest.raises(TypeError):
        5 + Float("1.23")

    with pytest.raises(TypeError):
        Float("1.23") + 5.1

    with pytest.raises(TypeError):
        5.1 + Float("1.23")
예제 #3
0
def test_pow():
    ans = Const(10.0)**(Float("5.484"))
    assert isinstance(ans, Float)
    assert ans.str == "3.05E+05"

    ans = Const(10.0)**(Float("5.483"))
    assert isinstance(ans, Float)
    assert ans.str == "3.04E+05"

    ans = Const(10.0)**(Float("5.485"))
    assert isinstance(ans, Float)
    assert ans.str == "3.05E+05"
예제 #4
0
def test_log():
    with pytest.raises(TypeError):
        func.log("a")
    with pytest.raises(TypeError):
        func.log(10.0)

    assert func.log(Float("10.0")).str == "2.30E+00"
예제 #5
0
def test_initialize():
    x = Float(1.23478755, 3)
    assert x.v == 1.23478755
    assert x.sf == 3
    assert x.lsd == -2
    assert x.str == "1.23E+00"

    x = Float(1.23478755, num_sig_figs=3)
    assert x.v == 1.23478755
    assert x.sf == 3
    assert x.lsd == -2
    assert x.str == "1.23E+00"

    x = Float("1.23478755", num_sig_figs=3)
    assert x.v == 1.23478755
    assert x.sf == 3
    assert x.lsd == -2
    assert x.str == "1.23E+00"

    x = Float(10000, 2)
    assert x.str == "1.0E+04"

    x = Float(10000.0, 2)
    assert x.str == "1.0E+04"

    x = Float("40.1234567")
    assert x.sf == 9
    assert x.str == "4.01234567E+01"

    x = Float("10.0")
    assert x.sf == 3

    x = Float("100")
    assert x.sf == 3

    with pytest.raises(ValueError):
        Float("abc")

    with pytest.raises(OverflowError):
        Float("1E+400")
예제 #6
0
def test_multiply():
    x = Float(1.23478755, 3)
    y = Float(356.1, 4)
    ans = x * y
    assert ans.str == "4.40E+02"

    with pytest.raises(TypeError):
        Float("1.23") * "abc"

    with pytest.raises(TypeError):
        "abc" * Float(1.23, num_sig_figs=3)

    with pytest.raises(TypeError):
        Float("1.23") * 5

    with pytest.raises(TypeError):
        5 * Float("1.23")

    with pytest.raises(TypeError):
        Float("1.23") * 5.1

    with pytest.raises(TypeError):
        5.1 * Float("1.23")
예제 #7
0
def test_divide():
    x = Float(1.23478755, 3)
    y = Float(356.1, 4)
    ans = x / y
    assert ans.str == "3.47E-03"

    with pytest.raises(TypeError):
        Float("1.23") / "abc"

    with pytest.raises(TypeError):
        "abc" / Float(1.23, num_sig_figs=3)

    with pytest.raises(TypeError):
        Float("1.23") / 5

    with pytest.raises(TypeError):
        5 / Float("1.23")

    with pytest.raises(TypeError):
        Float("1.23") / 5.1

    with pytest.raises(TypeError):
        5.1 / Float("1.23")
예제 #8
0
def test_subtract():
    x = Float(1.23478755, 3)
    y = Float(356.1, 4)
    ans = x - y
    assert ans.str == "-3.549E+02"

    with pytest.raises(TypeError):
        Float("1.23") - "abc"

    with pytest.raises(TypeError):
        "abc" - Float(1.23, num_sig_figs=3)

    with pytest.raises(TypeError):
        Float("1.23") - 5

    with pytest.raises(TypeError):
        5 - Float("1.23")

    with pytest.raises(TypeError):
        Float("1.23") - 5.1

    with pytest.raises(TypeError):
        5.1 - Float("1.23")
예제 #9
0
def test_pow():
    x = Float(1.23, 3)**3
    assert x.v == 1.23**3
    assert x.sf == 3

    x = Float(1.23, 3)**Float(3.0, 2)
    assert x.v == 1.23**3.0
    assert x.sf == 3

    x = Float(1.23, 3)**Const(3.0)
    assert x.v == 1.23**3.0
    assert x.sf == 3

    with pytest.raises(TypeError):
        Float("1.23")**"abc"

    with pytest.raises(TypeError):
        "abc"**Float(1.23, num_sig_figs=3)

    with pytest.raises(TypeError):
        Float("1.23")**5.1

    with pytest.raises(TypeError):
        5.1**Float("1.23")
예제 #10
0
def test_exp10():
    with pytest.raises(TypeError):
        func.exp10("a")
    with pytest.raises(TypeError):
        func.exp10(10.0)
    assert func.exp10(Float("1.234")).str == "1.71E+01"
예제 #11
0
def test_log10():
    with pytest.raises(TypeError):
        func.log10("a")
    with pytest.raises(TypeError):
        func.log10(10.0)

    # examples of the rule that log10 of 3 sig figs == number with 3 sig places after the decimal
    assert func.log10(Float("4.85E-6"), basic_rule=True).str == "-5.314E+00"
    assert func.log10(Float("4.85E-6"), basic_rule=False).str == "-5.314E+00"
    assert func.log10(Float("2.73E-05"), basic_rule=True) == Float("-4.564")
    assert func.log10(Float("2.73E-05"), basic_rule=False) == Float("-4.564")
    assert func.log10(Float("9.1E6"), basic_rule=True) == Float("6.96")
    assert func.log10(Float("9.1E6"), basic_rule=False) == Float("6.96")
    # example of breaking the basic rule
    assert func.log10(Float("1E-12"), basic_rule=True) == Float("-12.0")
    assert func.log10(Float("1E-12"), basic_rule=False) == Float("-12")
예제 #12
0
def test_combined():
    ans = (Float("1.23145E4") + Float("2.11379E2")) / (Float("1.503E-2") -
                                                       Float("4.6E-3"))
    assert ans.str == "1.20E+06"
예제 #13
0
def test_invert():
    x = Float(1.23478755, 3)
    ans = ~x
    assert ans.str == "8.10E-01"
예제 #14
0
def test_negative():
    x = Float(1.23478755, 3)
    ans = -x
    assert ans.str == "-1.23E+00"
예제 #15
0
def test_homework1():
    # https://www.saddleback.edu/faculty/jzoval/worksheets_tutorials/ch1worksheets/worksheet_Sig_Fig_9_11_08.pdf
    assert Float("246.32").sf == 5
    assert Float("107.854").sf == 6
    assert Float("100.3").sf == 4
    assert Float("0.678").sf == 3
    assert Float("1.008").sf == 4
    assert Float("0.00340").sf == 3
    assert Float("14.600").sf == 5
    assert Float("0.0001").sf == 1
    assert Float("700000").sf == 6  # package not capable
    assert Float("7E+05").sf == 1
    assert Float("350.670").sf == 6
    assert Float("1.0000").sf == 5
    assert Float("320001").sf == 6

    assert Float("32.567") + Float("135.0") + Float("1.4567") == Float("169.0")
    assert Float("246.24") + Float("238.278") + Float("98.3") == Float("582.8")
    assert Float("658.0") + Float("23.5478") + Float("1345.29") == Float("2026.8")

    assert Float("23.7") * Float("3.8") == Float("90.")
    assert Float("45.76") * Float("0.25") == Float("11")
    assert Float("81.04") * Float("0.010") == Float("0.81")
    assert Float("6.47") * Float("64.5") == Float("417")
    assert Float("43.678") * Float("64.1") == Float("2.80E+03")
    assert Float("1.678") / Float("0.42") == Float("4.0")
    assert Float("28.367") / Float("3.74") == Float("7.58")
    assert Float("4278") / Float("1.006") == Float("4252")
예제 #16
0
def test_exp2():
    with pytest.raises(TypeError):
        func.exp2("a")
    with pytest.raises(TypeError):
        func.exp2(10.0)
    assert func.exp2(Float("1.234")).str == "2.352E+00"
예제 #17
0
def test_log2():
    with pytest.raises(TypeError):
        func.log2("a")
    with pytest.raises(TypeError):
        func.log2(10.0)
    assert func.log2(Float("10.0")).str == "3.32E+00"
예제 #18
0
def test_exp():
    with pytest.raises(TypeError):
        func.exp("a")
    with pytest.raises(TypeError):
        func.exp(10.0)
    assert func.exp(Float("1.234")).str == "3.43E+00"
예제 #19
0
def test_sin():
    with pytest.raises(TypeError):
        func.sin("a")
    with pytest.raises(TypeError):
        func.sin(10.0)

    assert func.sin(Float(0, 3)) == Float("0.00E+00")
    assert func.sin(Float(np.pi / 2.0, 2)).v == 1.0
    assert func.sin(Float(np.pi / 2.0, 2)).sf == 18
    assert func.sin(Float(np.pi / 2.0, 5)).sf == 21

    print("\n")
    # TODO:  Don't like this.  These should all return the same sig figs
    print("sin(pi/2) {0}".format(func.sin(Float(np.pi / 2.0 - 0.01, 3))))
    print("sin(pi/2) {0}".format(func.sin(Float(np.pi / 2.0 - 0.0001, 3))))
    print("sin(pi/2) {0}".format(func.sin(Float(np.pi / 2.0, 3))))
    print("sin(pi/2) {0}".format(func.sin(Float(np.pi / 2.0 + 0.0001, 3))))
    print("sin(pi/2) {0}".format(func.sin(Float(np.pi / 2.0 + 0.01, 3))))
    print("\n")
    print("sin(pi) {0}".format(func.sin(Float(np.pi - 0.000000000001, 3))))
    print("sin(pi) {0}".format(func.sin(Float(np.pi, 3))))
    print("sin(pi) {0}".format(func.sin(Float(np.pi + 0.000000000001, 3))))
    print("\n")
    print("sin(3 pi / 2) {0}".format(func.sin(Float(3.0 * np.pi / 2.0, 3))))
    print("sin(2 pi) {0}".format(func.sin(Float(2.0 * np.pi, 3))))
예제 #20
0
def test_homework2():
    # http://www.chemistry.wustl.edu/~coursedev/Online%20tutorials/Plink/sigfigkey.htm
    assert Float("34.6209").sf == 6
    assert Float("0.003408").sf == 4
    assert Float("5010.0").sf == 5
    assert Float("4032.090").sf == 7

    assert Float("34.683") + Float("58.930") + Float("68.35112") == Float("161.964")
    x = Float("34.683") + Float("58.930") + Float("68.35112")
    assert x.v == pytest.approx(34.683 + 58.930 + 68.35112)
    assert Float("45001") - Float("56.355") - Float("78.44") == Float("44866")
    assert Float("0.003") + Float("3.5198") + Float("0.0118") == Float("3.535")
    assert Float("36.01") - Float("0.4") - Float("15") == Float("21")

    assert Float("98.1") * Float("0.03") == Float("3")
    assert Float("57") * Float("7.368") == Float("4.2E+02")
    assert Float("8.578") / Float("4.33821") == Float("1.977")
    assert Float("6.90") / Float("2.8952") == Float("2.38")