Python的math库常用函数介绍
发布于 2024-10-26
1208
版权声明
我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。
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学习杂记的其他文章
一文搞懂什么是向量数据库:为什么RAG离不开它
你问AI;我昨天上传的合同里,甲方是谁来着?AI能准确回答。背后的技术流程是:...
pulp解决混合整数规划问题
pulp是用来求解线性规划、整数规划等的开源包。从官网介绍来看,其也能调用常用的求解工具来解决实际问题。
决策树基本实现原理介绍
决策树是一种常用的机器学习算法,具有直观、易于理解和解释的特点。本文将介绍决策树的基本原理、实现过程。
flopt,融合了多种启发式算法的Python求解器
flopt是国外一小哥自己开发的一个求解器,里面底层结构是pulp,在pulp求解框架基础上增加了多种启发式算法。
分享一些Python的学习资料
之前有一些朋友留言想要分享一些python学习的资料,我在这里分享之前我学习python收集到的部分资料,
加入社区微信群
与行业大咖零距离交流学习
PMO实践白皮书
白皮书上线
白皮书上线