扫码阅读
手机扫码阅读

Python的math库常用函数介绍

32 2024-10-26

我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。

查看原文:Python的math库常用函数介绍
文章来源:
Python学习杂记
扫码关注公众号
Python Math Library Summary

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) yields 5.0.
  • Ceiling: math.ceil(num) rounds a number up to the nearest integer. Example: math.ceil(4.7) results in 5.
  • Square Root: math.sqrt(num) calculates the square root of a number, such as math.sqrt(9) producing 3.0.
  • Floor: math.floor(num) rounds a number down to the nearest integer, as seen with math.floor(4.7) which gives 4.
  • Power: math.pow(base, exponent) performs exponentiation. For example, math.pow(2, 3) equals 8.0.
  • Logarithm: math.log(num) calculates the natural logarithm of a number. math.log(10) returns 2.302585092994046.
  • Factorial: math.factorial(num) computes the factorial of an integer, with math.factorial(5) equating to 120.
  • Sine: math.sin(radians) calculates the sine of an angle in radians. For example, math.sin(math.pi / 2) is 1.0.
  • Cosine: math.cos(radians) calculates the cosine, and math.cos(math.pi / 2) is approximately 0.
  • Tangent: math.tan(radians) returns the tangent of an angle. math.tan(math.pi / 4) equals 1.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的math库常用函数介绍
文章来源:
Python学习杂记
扫码关注公众号