Python的math库常用函数介绍
发布于 2024-10-26
1566
版权声明
我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。
Python学习杂记
扫码关注公众号
扫码阅读
手机扫码阅读
Python Math Library Overview
The math library in Python contains a variety of mathematical functions designed to solve different mathematical problems. This article presents commonly used functions from the math library, accompanied by example cases for educational purposes.
Mathematical Functions and Examples
-
Absolute Value:
math.fabs(num)returns the absolute value of a number. For instance,math.fabs(-5)yields5.0. -
Ceiling:
math.ceil(num)rounds a number up to the nearest integer. Example:math.ceil(4.7)results in5. -
Square Root:
math.sqrt(num)calculates the square root of a number, such asmath.sqrt(9)producing3.0. -
Floor:
math.floor(num)rounds a number down to the nearest integer, as seen withmath.floor(4.7)which gives4. -
Power:
math.pow(base, exponent)performs exponentiation. For example,math.pow(2, 3)equals8.0. -
Logarithm:
math.log(num)calculates the natural logarithm of a number.math.log(10)returns2.302585092994046. -
Factorial:
math.factorial(num)computes the factorial of an integer, withmath.factorial(5)equating to120. -
Sine:
math.sin(radians)calculates the sine of an angle in radians. For example,math.sin(math.pi / 2)is1.0. -
Cosine:
math.cos(radians)calculates the cosine, andmath.cos(math.pi / 2)is approximately0. -
Tangent:
math.tan(radians)returns the tangent of an angle.math.tan(math.pi / 4)equals1.0. -
Modulo:
math.fmod(a, b)provides the remainder of a division. -
Greatest Common Divisor:
math.gcd(a, b)finds the largest number that divides both integers without leaving a remainder. -
Exponential:
math.exp(x)calculates e raised to the power of x, where e is the base of natural logarithms. -
Fractional and Integer Parts:
math.modf(value)breaks a number into its fractional and integer parts.
Mastering these common functions from the math library can significantly enhance programming efficiency.
Python学习杂记
Python学习杂记
扫码关注公众号
还在用多套工具管项目?
一个平台搞定产品、项目、质量与效能,告别整合之苦,实现全流程闭环。
查看方案
Python学习杂记的其他文章
启发式算法库scikit-opt使用介绍
今天给大家介绍一个非常实用的Python优化算法库——scikit-opt。它集成了许多经典的智能优化算法,
ortools求解非线性问题
最近抽空研究了谷歌运筹优化库ortools底层封装的函数,发现其自带的许多函数能解决很多复杂的非线性规划问题。
WPS AI试用(与GPT、Claude参照对比)
金山办公的WPS AI已经开放申请了,申请网站https://ai.wps.cn/。大概一到两天就可以申请成功。
图片转文字识别方法
工作中,我们经常需要直接获取图片中的文字,我在网上找了一些方法。1.pytesseract ,2.EasyOCR
选址问题(一)-精确重心法和遗传算法
选址问题是运筹学中经典的问题之一。选址问题在生产生活、物流、甚至军事中都有着非常广泛的应用。
加入社区微信群
与行业大咖零距离交流学习
PMO实践白皮书
白皮书上线
白皮书上线