def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))
    
    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info[:2] >= (2, 6):
    
        # factorial() must not be "damaged" by the umath module, so as 
        # to help make it a drop-in replacement for math (even though 
        # factorial() does not work on numbers with uncertainties 
        # because it is restricted to integers, as for 
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3
示例#2
0
def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))

    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info[:2] >= (2, 6):

        # factorial() must not be "damaged" by the umath module, so as
        # to help make it a drop-in replacement for math (even though
        # factorial() does not work on numbers with uncertainties
        # because it is restricted to integers, as for
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3
示例#3
0
def test_math_module():
    "Operations with the math module"

    x = ufloat(-1.5, 0.1)
    
    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info >= (2, 6):
    
        # factorial() must not be "damaged" by the umath module, so as 
        # to help make it a drop-in replacement for math (even though 
        # factorial() does not work on numbers with uncertainties 
        # because it is restricted to integers, as for 
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    if sys.version_info < (2, 6):
            
        try:
            math.log(0)
        except OverflowError, err_math:  # "as", for Python 2.6+
            pass
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(0)
        except OverflowError, err_ufloat:  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
示例#4
0
def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))

    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info >= (2, 6):

        # factorial() must not be "damaged" by the umath module, so as
        # to help make it a drop-in replacement for math (even though
        # factorial() does not work on numbers with uncertainties
        # because it is restricted to integers, as for
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    if sys.version_info < (2, 6):

        try:
            math.log(0)
        except OverflowError(err_math):  # "as", for Python 2.6+
            pass
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(0)
        except OverflowError(err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 0)))
        except OverflowError( err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 1)))
        except OverflowError( err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')

    elif sys.version_info < (3,):

        try:
            math.log(0)
        except ValueError( err_math):
            pass
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(0)
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 0)))
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 1)))
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')

    else:  # Python 3+

        # !!! The tests should be made to work with Python 3 too!
        pass