zoukankan      html  css  js  c++  java
  • Python3-math常用模块

    代码:

    import math
    from decimal import Decimal
    from decimal import getcontext
    from fractions import Fraction
    
    def func_test(num_list, num_list2):
        '''''
        math模块常用函数
        '''
        for one_num in num_list:
            print('向上取整:', one_num, math.ceil(one_num))
            print('向下取整:', one_num, math.floor(one_num))
            print('取绝对值:', one_num, math.fabs(one_num))
            print('截断整数部分:', one_num, math.trunc(one_num))
            print('是否是数字:', one_num, math.isnan(one_num))
        for one_num in num_list2:
            print('开方:', one_num, math.sqrt(one_num))
            print('阶乘:', one_num, math.factorial(one_num))
    
        x, y = 12, 3
        print('x,y乘积:', math.sqrt(x * x + y * y))
        print('x,y乘积:', math.hypot(x, y))
        print('幂指数计算:', math.pow(x, y))
        print('浮点数计算:')
        getcontext().prec = 4  # 设置全局精度
        print(Decimal('0.1') / Decimal('0.3'))
        print('分数简化:', Fraction(16, -10))  # 分子分母
        print('圆周率:', math.pi)
        print('取余操作:', math.fmod(10, 3))
        print('对数运算:', math.log(x, y))
        print('对数运算:', math.log10(x))
        print('对数运算:', math.log1p(x))
        print('角度弧度转化:', math.radians(30))
        print('角度弧度转化:', math.degrees(math.pi))
        print('三角函数使用——x的正弦、余弦:', math.sin(x), math.cos(x))
        print('三角函数使用——x的双曲正弦、余弦', math.sinh(x), math.cosh(x))
        print('三角函数使用——x的正切、双曲正切', math.tan(x), math.tanh(x))
        # print 'x的反余弦', math.acos(x)
        # print 'x的反双曲余弦', math.acosh(x)
        # print 'x的反正弦', math.asin(x)
        # print 'x的反双曲正弦', math.asinh(x)
        print('Pi、e', math.pi, math.e)
        print('e的幂指数', math.exp(y))
    
    if __name__ == '__main__':
        num_list = [3, 4, 5, -7.9, 6.4]
        num_list2 = [6, 12, 25, 9]
        func_test(num_list, num_list2)
    View Code

    运行结果:

    向上取整: 3 3
    向下取整: 3 3
    取绝对值: 3 3.0
    截断整数部分: 3 3
    是否是数字: 3 False
    向上取整: 4 4
    向下取整: 4 4
    取绝对值: 4 4.0
    截断整数部分: 4 4
    是否是数字: 4 False
    向上取整: 5 5
    向下取整: 5 5
    取绝对值: 5 5.0
    截断整数部分: 5 5
    是否是数字: 5 False
    向上取整: -7.9 -7
    向下取整: -7.9 -8
    取绝对值: -7.9 7.9
    截断整数部分: -7.9 -7
    是否是数字: -7.9 False
    向上取整: 6.4 7
    向下取整: 6.4 6
    取绝对值: 6.4 6.4
    截断整数部分: 6.4 6
    是否是数字: 6.4 False
    开方: 6 2.449489742783178
    阶乘: 6 720
    开方: 12 3.4641016151377544
    阶乘: 12 479001600
    开方: 25 5.0
    阶乘: 25 15511210043330985984000000
    开方: 9 3.0
    阶乘: 9 362880
    x,y乘积: 12.36931687685298
    x,y乘积: 12.36931687685298
    幂指数计算: 1728.0
    浮点数计算:
    0.3333
    分数简化: -8/5
    圆周率: 3.141592653589793
    取余操作: 1.0
    对数运算: 2.2618595071429146
    对数运算: 1.0791812460476249
    对数运算: 2.5649493574615367
    角度弧度转化: 0.5235987755982988
    角度弧度转化: 180.0
    三角函数使用——x的正弦、余弦: -0.5365729180004349 0.8438539587324921
    三角函数使用——x的双曲正弦、余弦 81377.39570642986 81377.39571257407
    三角函数使用——x的正切、双曲正切 -0.6358599286615808 0.9999999999244973
    Pi、e 3.141592653589793 2.718281828459045
    e的幂指数 20.085536923187668
    View Code
  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/bashliuhe/p/12540136.html
Copyright © 2011-2022 走看看