新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

《新大榭》- 创大榭地方网络社区先锋品牌 新大榭始终专注于地方网络社区平台的建设 关于我们- [大记事]- 留言建议- [新手报道]

发布 .新大榭软件管家(Excel版) V6.0版 财务/仓库/生产/销售/采购/行政/人事/校园 .公告 - 客户 - 打赏 - 职场 - Excel - Python.

新大榭镜像-音乐-法律-图书-高中课堂-实验 广告是为了能更好的发展 [欢迎商家支持本站互利共赢] 广告位招租.首页黄金广告位等您来!联系 13566035181

新大榭论坛 门户 查看主题

7442 - Python库 AP085【math】数学模块常用方法

发布者: admin | 发布时间: 2021-7-24 10:21| 查看数: 1995| 评论数: 0|帖子模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!

您需要 登录 才可以下载或查看,没有账号?注册

x
7 a; O2 C+ E8 Z& A9 W
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 K: L- N' _: C# W

- ^7 v- c- x6 ?2 u# C1 ~% t方法1+ ?5 F# v' v! P- A3 \% Y. T/ G
  1. >>> import math1 X2 s9 l/ @+ v6 d
  2. >>> math.sqrt(9)2 M& w5 Y3 @* L- z4 \$ p$ n
  3. 3.0
复制代码
方法25 ~% I: N& X9 ]* b' C& ~+ r% o9 R/ |
  1. >>> from math import sqrt
    / u: V% ^( @8 _/ e6 a
  2. >>> sqrt(9)) Q& }. O$ P/ g' L( ~* {# u: {3 v' X
  3. 3.0
复制代码
" N' Z! l  [# q( L1 n


* F- v8 {! ^: Z/ W7 ~! }
math.e  表示一个常量
3 a0 c- C* T0 K  t/ c1 ]5 |9 Y2 c0 Q
  1. #表示一个常量
    6 @" t; g. o# |) g1 T$ }) D
  2. >>> math.e7 V0 W: f7 g1 i) h" }
  3. 2.718281828459045
复制代码

4 L  G6 ^9 k# i9 Z$ p6 r( ]math.pi  
数字常量,圆周率

& }/ ]6 P* ]! m" w7 c; ^
  1. #数字常量,圆周率
    ; N0 D4 c8 Y+ z( ]% g' W3 s, O+ `
  2. >>> print(math.pi)
    1 n" Q5 P0 O& y2 V& h
  3. 3.141592653589793
复制代码
: i% O6 _0 S6 ]- g/ ^' `
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

. D  w# `9 K- V6 b9 i
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 ^( d7 F; b5 u  y7 `1 ?7 y
  2. ceil(x)
    0 y+ H  _2 N1 n7 G( B
  3. Return the ceiling of x as an int.
    2 C' z1 e$ ]' R3 r1 V8 ]# h- w
  4. This is the smallest integral value >= x.
    , Y/ k$ m, S3 Y# r( }

  5. : r& x6 T5 H8 S; z  V0 y
  6. >>> math.ceil(4.01)+ |) n7 ?- U/ W- o) O
  7. 5: R. E* x) Z  Z5 Z/ [+ L5 k' A
  8. >>> math.ceil(4.99)7 d! O7 T2 Y- s# w5 N8 x" K
  9. 5
    * K+ x% e1 l. \. `! m3 j- o
  10. >>> math.ceil(-3.99)
    9 N( k$ K. j2 Y8 }
  11. -3
    $ z- ~/ q) k7 _. ^0 Z' ]; b
  12. >>> math.ceil(-3.01)
    ! H% x, [. M% ^, V
  13. -3
复制代码

. B! j* P1 ]( s( Q1 }2 }0 B" @math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! O" [0 y6 R! _9 }- q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 `# [& O) Q, l5 ]
  2. floor(x)$ L4 i; U% ?, @4 q) ^6 ~  P/ \  A7 u
  3. Return the floor of x as an int.
    ! A" |$ i6 T0 n$ l! E
  4. This is the largest integral value <= x.1 }1 g+ e! R# {! O: K0 T$ `
  5. >>> math.floor(4.1)
    $ ^6 F  Y" B/ X9 v( {& p; `! T3 T( M
  6. 43 P" Y/ G4 u: Q$ h0 P
  7. >>> math.floor(4.999)* t& x- D; G/ C3 i/ F
  8. 4
    $ F' N3 o( U/ f$ u. \& Z1 W) W
  9. >>> math.floor(-4.999)
    6 B+ [0 z) I9 C1 g  M+ ]
  10. -5+ D! i4 N5 e9 X" Y( ]
  11. >>> math.floor(-4.01)
    1 k3 z7 G- U! O' L, F3 p9 [: j) d; c. O
  12. -5
复制代码
8 i5 K' u9 X# u$ j9 M
math.pow(x,y)  返回x的y次方,即x**y  e! F/ U5 J8 S+ b/ {# Z% b
  1. #返回x的y次方,即x**y4 N7 q' N( ]8 Y6 a9 c6 u) {4 |
  2. pow(x, y)3 U$ U# ?2 J) d" k& i
  3. Return x**y (x to the power of y).! E  `, s- G  C$ `
  4. >>> math.pow(3,4)0 w- b, p) B, m& W
  5. 81.0
    # |; D5 `6 R$ h  U" Q
  6. >>>
    % S" M, H2 V* Z' ]$ E
  7. >>> math.pow(2,7)
    : C2 A5 F/ ^  u/ n& o' `
  8. 128.0
复制代码

9 e# [  `3 K& nmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' r8 l5 g; F0 A& `  E3 G7 I
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  u1 Y* H+ G& \& a
  2. log(x[, base])
      z0 Z! p+ s- e* T. M
  3. Return the logarithm of x to the given base.
    " D& E$ _" s+ d+ K6 [2 ?: n4 ?
  4. If the base not specified, returns the natural logarithm (base e) of x.  p( _- C& O- g+ R8 G
  5. >>> math.log(10)
    ( T- K- e- c4 U& }8 ]+ j+ y/ _
  6. 2.302585092994046$ q" W  ~' L- r3 b0 L; }
  7. >>> math.log(11)0 m2 y& {* L! q6 v5 ^
  8. 2.3978952727983707
    0 N& e' G; _( \, x) S  _' U0 m
  9. >>> math.log(20)& Z( T" A  Q. o  i' S) K& _9 X
  10. 2.995732273553991
复制代码
# q# x5 m, d8 q% g" m1 v. B
math.sin(x)  求x(x为弧度)的正弦值
6 h6 L  B# D' B/ }  Q7 w+ Q* w
  1. #求x(x为弧度)的正弦值
    6 r! V9 Q) z' t2 h
  2. sin(x)
    + d1 N) a/ h) J) Q& z
  3. Return the sine of x (measured in radians).
    * h$ h- t3 V8 o
  4. >>> math.sin(math.pi/4)
    . `) S7 [/ ]8 W) m& @& `% s/ f
  5. 0.70710678118654756 {' v/ m$ n# `; O2 K. m4 G- Y
  6. >>> math.sin(math.pi/2)2 J, Q5 W! C! @, }6 E* j' p+ O* I! k& o
  7. 1.0
    , @/ h0 y) o$ C2 y
  8. >>> math.sin(math.pi/3)
    ) R4 L8 V7 q! U6 ?4 P
  9. 0.8660254037844386
复制代码

1 w$ |7 q/ p. ^# d, L! Mmath.cos(x)  求x的余弦,x必须是弧度
% o# Y! T1 T2 L0 i- W+ H- C
  1. #求x的余弦,x必须是弧度
      a7 b9 ^/ z' ~, N( A/ X# K1 s
  2. cos(x): E3 R  R$ i) Y, G  N
  3. Return the cosine of x (measured in radians)./ x8 x! @: i' M" t5 N- I
  4. #math.pi/4表示弧度,转换成角度为45度
    ; r: t6 _8 f* N
  5. >>> math.cos(math.pi/4)
    " W+ e) n: p& h& N) V4 @
  6. 0.7071067811865476
    / E% q/ \3 M, t0 i9 ]" e
  7. math.pi/3表示弧度,转换成角度为60度
    * j+ W1 }2 J5 b  K+ @
  8. >>> math.cos(math.pi/3): L' I( x) Z) a! z; H  u
  9. 0.5000000000000001
    " ?! H  J0 @6 ?2 e: L- K& _' A
  10. math.pi/6表示弧度,转换成角度为30度1 a' @# x/ |8 e; ?( Q& P
  11. >>> math.cos(math.pi/6)( Q8 C1 l, M- R8 `$ B  h8 O$ O
  12. 0.8660254037844387
复制代码
! {% S% H8 y9 G& c2 C  E- j
math.tan(x)  返回x(x为弧度)的正切值" _( e: q( C: o% B% G# n
  1. #返回x(x为弧度)的正切值
    / L, q5 S6 h7 _, ^& Z
  2. tan(x)
    0 L6 T6 D! N( K" g/ d1 v+ n& i6 K6 y
  3. Return the tangent of x (measured in radians).1 u# |! l6 c3 v! J9 }4 a& M7 `
  4. >>> math.tan(math.pi/4)9 }6 ]# C1 G& g' q$ X/ S+ Q) S
  5. 0.9999999999999999
    ' I, E% R+ w3 ?, _1 d
  6. >>> math.tan(math.pi/6)6 |7 B. p9 A4 `# J
  7. 0.5773502691896257' e2 \0 J1 ?8 D% T: L3 f' p
  8. >>> math.tan(math.pi/3)
    : d& w6 ~2 l, j2 K
  9. 1.7320508075688767
复制代码
: Z& r; g: D8 ~3 D
math.degrees(x)  把x从弧度转换成角度
5 \5 B" o7 ^! X+ |0 i
  1. #把x从弧度转换成角度
    2 E4 H" `1 {  h, ~: ?; g
  2. degrees(x)
    8 i' X( m: `9 ^( c" b3 ~
  3. Convert angle x from radians to degrees.
    9 ?7 B1 e/ U- A: ^
  4. + W7 P2 `' b. W  J' l. K+ |
  5. >>> math.degrees(math.pi/4)
    % C: o5 m. v+ Y( N& y
  6. 45.08 |! q/ X6 u) b6 K+ @7 e
  7. >>> math.degrees(math.pi)* Z" R3 f4 p  a, P: ]7 w
  8. 180.0
    % p1 `$ l! Y; s0 t  R: F; b
  9. >>> math.degrees(math.pi/6)
    0 M) m; B( _% r+ D
  10. 29.999999999999996" f4 Y1 U3 W7 Y% h% S1 `
  11. >>> math.degrees(math.pi/3): n' Y8 m5 V: L  {- d
  12. 59.99999999999999
复制代码

5 F# X% l6 w5 O0 D0 Lmath.radians(x)  把角度x转换成弧度8 g0 h% V9 C2 I2 j! ?5 p. a
  1. #把角度x转换成弧度
    ; E0 {0 S$ S% @
  2. radians(x). ?+ m9 g7 a# }& A/ \9 ?
  3. Convert angle x from degrees to radians.
    8 U% Y! i' u4 x* k5 q9 `
  4. >>> math.radians(45)3 T3 e+ h, a9 {
  5. 0.7853981633974483
    ; B4 O/ }' G' v9 e3 h" h
  6. >>> math.radians(60)' u- d0 u+ i8 J  t- C0 h( ]
  7. 1.0471975511965976
复制代码

. k( W4 P( e) Bmath.copysign(x,y)  把y的正负号加到x前面,可以使用0' R" R* \9 t6 u) v% x5 k
  1. #把y的正负号加到x前面,可以使用0
    9 v! Y: |( ^, I8 s# n
  2. copysign(x, y)
    . _3 L5 v* D7 t$ {
  3. Return a float with the magnitude (absolute value) of x but the sign
    0 S. E* D* d3 S9 h
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)   S) h* k/ z: i* G$ W* b/ ^
  5. returns -1.0.
    $ w" L" X  z: w! m* x. g

  6. ) y1 b* W( ~3 W
  7. >>> math.copysign(2,3), P9 z+ K/ h- D+ R7 c$ c
  8. 2.0
    - T1 |5 T8 Z6 a4 ?, t+ m8 Q- G
  9. >>> math.copysign(2,-3)
    . m! H/ w& e; W- r" n+ G, i- p
  10. -2.0
    6 G5 A% m. X: A) r3 [: }) J7 ^+ e, ^+ m
  11. >>> math.copysign(3,8)
    ; p9 Y% J; U( b; H( M2 D0 s' Q3 t
  12. 3.0- f+ g$ S5 i9 K5 ^' @
  13. >>> math.copysign(3,-8)
    : M% |8 ^% I7 g8 H
  14. -3.0
复制代码
4 g+ b8 G+ m) Z, p' `
math.exp(x)  返回math.e,也就是2.71828的x次方
) h. d  j: o$ h  s4 s# O
  1. #返回math.e,也就是2.71828的x次方
    2 `6 S: ]9 `  D2 A- a  `" f# W
  2. exp(x)7 g. ^* w% ~$ }. A' ~
  3. Return e raised to the power of x.
    7 f) v0 J0 Q* [0 a1 S* c! R

  4. ) y: z1 N8 Q2 E: \
  5. >>> math.exp(1), t) b# E5 X$ c7 L+ `* F2 {5 A( a
  6. 2.718281828459045
    9 g/ D$ q9 u- x: l
  7. >>> math.exp(2)
    6 \9 [+ n4 J9 k
  8. 7.38905609893065: w: l/ o2 ?! w3 S$ o
  9. >>> math.exp(3)7 T1 b8 O: b# k! I  z0 O
  10. 20.085536923187668
复制代码

- @  N3 x7 I3 @: m, kmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
2 }5 L4 u& M/ W
  1. #返回math.e的x(其值为2.71828)次方的值减18 p6 {; z4 p  k( o0 ]  t
  2. expm1(x)
    ) h- V, ]1 u, _% i
  3. Return exp(x)-1.! y% q& [' x9 z% @) f  c, ~  L
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 P0 v6 R5 `0 K4 p/ y4 V9 Z

  5. 5 R$ W; a/ g! A3 r
  6. >>> math.expm1(1)2 Q+ M: h8 Y8 Z4 m1 T; k- z; @
  7. 1.718281828459045( S' ]7 r% g! _, r5 C: Y
  8. >>> math.expm1(2)
    4 ?9 \! n$ q& ?, B: m+ M9 J$ t
  9. 6.389056098930653 o) d3 h+ \$ h! V# S# M9 e4 o
  10. >>> math.expm1(3)
    6 n, D& k, W& {5 d7 l' k
  11. 19.085536923187668
复制代码
1 K' ~6 |7 u" k# o1 |6 c( y1 y
math.fabs(x)  返回x的绝对值
/ B' A! O% [! H
  1. #返回x的绝对值
    : ]% Z0 o( N) y- E6 W
  2. fabs(x)
    0 s5 p8 O; }* D: y: y$ c- ~; r
  3. Return the absolute value of the float x.
    " O- D9 j+ v( U7 v/ i

  4. ' @2 {; z: A8 @+ v
  5. >>> math.fabs(-0.003)
    8 {; Q( Z) m; s& ]6 ?
  6. 0.003
    " j; M# z6 G# ^; P
  7. >>> math.fabs(-110)
    0 d, _% e$ f3 V/ ^
  8. 110.0
    5 O% B; [* X, e& g; \6 c. U. _1 L
  9. >>> math.fabs(100)' g; H, z# i# o6 z% ?8 }
  10. 100.0
复制代码

8 n3 B8 U+ z( F+ @# g. omath.factorial(x)  取x的阶乘的值5 h. S4 C8 a* k5 V# y
  1. #取x的阶乘的值& n7 N/ r# A9 ?1 A8 T8 h
  2. factorial(x) -> Integral2 v+ A! r) N, G% h! S) I
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    , n: v5 u/ |8 H1 m! B
  4. >>> math.factorial(1)" U0 Y+ a7 H2 c% |" ^3 @# C4 j( ~+ n* F
  5. 1
    1 q# e  |% L/ j% |7 g2 S
  6. >>> math.factorial(2)" {% |9 L7 f6 @! v9 c; O$ y
  7. 20 W; d. \) D$ ?4 x
  8. >>> math.factorial(3); R. ]) S5 y+ z
  9. 69 B4 C/ x8 A' T; s% i8 j" C
  10. >>> math.factorial(5)
    * d" D+ T, \! J4 q
  11. 120
    % }; x! p0 {8 t
  12. >>> math.factorial(10)
    5 F) c* T+ }. c" O# K
  13. 3628800
复制代码

5 w! n9 ^5 d9 R4 c8 R$ pmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
; y" `! H" x. t, D! u0 F' L9 p. s8 ~; d
  1. #得到x/y的余数,其值是一个浮点数
    $ w* E* f" Q7 |, r0 a5 V
  2. fmod(x, y)$ J. N: b7 f  d9 w9 O  ?" t
  3. Return fmod(x, y), according to platform C.  x % y may differ.8 M: u% j( Q6 I+ E* R/ U% V
  4. >>> math.fmod(20,3)
    ( ^8 A: T+ n+ ]) ]8 d+ M# S( S
  5. 2.0
    ) G( Z4 r1 g) q6 N
  6. >>> math.fmod(20,7)
    + |$ I7 f- t0 o* K
  7. 6.0
复制代码

3 I" X' N" n& P. ]math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
, M( r  a4 x/ S9 [
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 q. y: T- N; \, x4 @5 U+ Q* Z6 F  [
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    : Z  Y9 [& x" m/ t- V) M
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) V7 O) @# u+ {9 L$ @2 m
  4. frexp(x)
    3 X+ P! ^. @" t+ q7 [" o
  5. Return the mantissa and exponent of x, as pair (m, e).
    9 d1 A+ t& e" N( j2 T
  6. m is a float and e is an int, such that x = m * 2.**e.
    * F; P. N2 F1 ~+ [. c, n
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.3 h& D7 @4 _2 v  B+ f* \: `& }
  8. >>> math.frexp(10)0 a- U. d6 N/ Y
  9. (0.625, 4)
      M9 [2 @& {8 J0 f% Q
  10. >>> math.frexp(75)* c0 X* Z, m6 ?8 Q! P2 M# V! P
  11. (0.5859375, 7)
    5 w- Y! [6 E/ D9 e! O- H0 [0 [
  12. >>> math.frexp(-40)
    $ z% o  ?- v! h: h
  13. (-0.625, 6)* J$ \* E8 G) W: x
  14. >>> math.frexp(-100)
    ; a5 M' F! \' d# Z: u: [" A
  15. (-0.78125, 7)
    , i0 O7 [# z6 t  W: T
  16. >>> math.frexp(100)
    ) Q* s7 F2 \: u; H% d) @. P
  17. (0.78125, 7)
复制代码

" r; k& P( n* v. ymath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列) k& Y7 m8 e" H
  1. #对迭代器里的每个元素进行求和操作
    ) R  o- S% [. }5 L/ ?
  2. fsum(iterable)& F6 x! f$ T  W: A
  3. Return an accurate floating point sum of values in the iterable.
    ! J/ A4 o  Y4 N! \4 Z, O' `5 v
  4. Assumes IEEE-754 floating point arithmetic.
    6 C- J# n" j5 D
  5. >>> math.fsum([1,2,3,4])
    1 U! v7 E' |, J0 V6 U  F. I* [
  6. 10.0
    + f! Z% d) o+ ^' U# i" ^3 {
  7. >>> math.fsum((1,2,3,4))& j, b4 p3 c- k! ?1 _: {
  8. 10.0$ y2 g  J  c) D& }& \
  9. >>> math.fsum((-1,-2,-3,-4))2 T% @6 i8 d9 `4 h
  10. -10.0* B; m9 k# M: ]8 a5 e
  11. >>> math.fsum([-1,-2,-3,-4])% d; u( k* s/ A4 N6 E/ g
  12. -10.0
复制代码
0 ^, e/ [% m2 R0 i" Q, r
math.gcd(x,y)  返回x和y的最大公约数9 F* J1 T, ?) F* B
  1. #返回x和y的最大公约数
      h1 [* d( {8 s! y
  2. gcd(x, y) -> int4 r1 c8 D& P2 |( i3 U' d
  3. greatest common divisor of x and y4 n3 B2 K. Y( [4 R  V& K
  4. >>> math.gcd(8,6)! _$ z4 Z. Q$ \: B. X, H
  5. 20 c) h% J1 Y; }7 m% S. J, g
  6. >>> math.gcd(40,20)) z8 X# |* K7 J0 o6 c6 ]' p$ Q. b
  7. 20% |* Q) K/ w4 |& U( K: W
  8. >>> math.gcd(8,12)
    4 l' {( W5 _" \5 m8 g# e3 }
  9. 4
复制代码
! C8 Q" X% {, m7 g7 X4 @3 u7 T  w
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
" c0 z( F/ ~  v0 l' @" `
  1. #得到(x**2+y**2),平方的值6 j, N% b9 H* E" x/ ~& r1 D
  2. hypot(x, y)  M, [: _# Y- l1 q& @
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    # i7 J9 {3 F+ P) [* N5 w# Q, j+ B6 {
  4. >>> math.hypot(3,4)
    , r7 I8 v* g& M* H5 K
  5. 5.00 p; E' T. Y5 F  B$ B, W+ K
  6. >>> math.hypot(6,8)
    - J0 u  ~7 W2 z  J* y/ H
  7. 10.0
复制代码
1 z& O9 x2 ?) ^
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False( i8 ?( j& q+ b  U
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" M! \7 q: Y! q4 |: x5 {. z6 f" ^4 S
  2. isfinite(x) -> bool) z( B( S$ u! \6 r& B: O: X
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.; x! I/ W  p; j4 G, x
  4. >>> math.isfinite(100)
    & Z, L: @! z/ G/ t+ |0 L
  5. True
    6 O4 M. m3 a5 c6 b
  6. >>> math.isfinite(0)' |6 a2 I5 H9 f, s5 A, k
  7. True. L/ z# s; N% t4 v- o! }1 w/ d
  8. >>> math.isfinite(0.1)
    3 n, |& ?. Q2 y0 R
  9. True1 h/ Z+ \5 k0 U1 ~
  10. >>> math.isfinite("a")% r2 v% s2 m; J! z. z7 j* E
  11. >>> math.isfinite(0.0001)
    - ~/ N* F1 e5 d" Q4 K
  12. True
复制代码
! i7 }0 l7 ?. i% b0 ]
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
' @$ G& ^& |4 _+ f6 h
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    $ A" f; b. \7 z/ M
  2. isinf(x) -> bool
    ; p  g/ @1 b$ L; h) s6 I( P/ e* z
  3. Return True if x is a positive or negative infinity, and False otherwise.
    4 J8 Z$ }# z/ \1 D
  4. >>> math.isinf(234)
    8 H, M$ ]4 T! s6 [! _
  5. False% Z% W& c& p1 @4 l) v! w
  6. >>> math.isinf(0.1)! A  Z6 O, [+ e8 L5 ]; K' T
  7. False
复制代码

, \5 E4 q: y% ~1 a' y4 I! Qmath.isnan(x)  如果x不是数字True,否则返回False
+ H6 m! a3 B+ x0 U4 K2 n
  1. #如果x不是数字True,否则返回False4 N7 O0 H: n4 h. O6 C4 D+ c3 G* ]
  2. isnan(x) -> bool" {1 \$ ?5 X4 c8 ?
  3. Return True if x is a NaN (not a number), and False otherwise.; P7 b5 B& y0 `
  4. >>> math.isnan(23)
      O' E+ G( Z. `$ ^
  5. False2 j1 M  q* U- r4 U1 A1 a
  6. >>> math.isnan(0.01)" Y+ \: V0 J% A% L( |  @
  7. False
复制代码

, g) g9 O: a. S! {, a0 }math.ldexp(x,i)  返回x*(2**i)的值
/ f3 j. h  n" l* l- ^7 X7 H- `
  1. #返回x*(2**i)的值8 U1 Z* v) B* \! w- y5 I# Z
  2. ldexp(x, i)5 g- P/ A5 q+ n/ |% {
  3. Return x * (2**i).
    ) p! e7 r6 T4 P
  4. >>> math.ldexp(5,5)7 l. c4 [2 W5 J; |1 V! c
  5. 160.0
      G* S( n5 D2 s' B! H7 _3 b
  6. >>> math.ldexp(3,5)4 J- {9 ~( j+ J  f: x7 R
  7. 96.0
复制代码

+ l. L/ g6 ^- I. s$ xmath.log10(x)  返回x的以10为底的对数2 X5 U1 i' `( G& F4 ~
  1. #返回x的以10为底的对数
    ; W- r9 X6 F' u4 l
  2. log10(x)
    + X  B, e% ?' g1 W0 l9 @
  3. Return the base 10 logarithm of x.
    4 o. G# G2 j% `$ i5 M6 H$ l
  4. >>> math.log10(10)2 n. b: f, y' Z: z; z0 i
  5. 1.0
    7 M& s* O2 o3 a2 W/ W
  6. >>> math.log10(100)
    & o- B$ |+ I0 j# L6 B
  7. 2.0$ r% x# c4 R0 T2 j) x
  8. #即10的1.3次方的结果为20
    & Q5 i7 d0 t4 o2 O! Y' P
  9. >>> math.log10(20)
    4 M5 c, y  Z7 k6 c6 P
  10. 1.3010299956639813
复制代码

' t" n" X# j  Z0 N2 `' {7 Nmath.log1p(x)  返回x+1的自然对数(基数为e)的值0 ?4 y3 }/ W  e' G( v" W6 M
  1. #返回x+1的自然对数(基数为e)的值
    4 v+ |# ]+ p0 G
  2. log1p(x)
    9 f8 a( B' w4 l5 o% z8 }
  3. Return the natural logarithm of 1+x (base e)./ a- A9 k6 Y$ X* Z( o6 M
  4. The result is computed in a way which is accurate for x near zero.
    : @) k$ R' t: y) T. T8 @: _, E* Z
  5. >>> math.log(10)+ u& u' \8 h: b4 l- K& A
  6. 2.302585092994046
    ( H, s$ ?8 A% M9 p! n; U
  7. >>> math.log1p(10)
    % T% m' Q1 j( r+ t; L4 i) M
  8. 2.3978952727983707
    2 c" W# n; q; A6 _* f
  9. >>> math.log(11)
    1 W0 Q7 U# @1 G: S
  10. 2.3978952727983707
复制代码

0 n  j: t) h9 [2 Cmath.log2(x)  返回x的基2对数
" |* g) M: g+ Z) N+ b
  1. #返回x的基2对数- F; j- ]+ {# T* z, G1 M/ I
  2. log2(x). \$ K1 _9 U* o  b6 t
  3. Return the base 2 logarithm of x.
    ; h. ~  l- K# g7 T' B5 x; P. q
  4. >>> math.log2(32)
    + q# O/ a& H5 g: o
  5. 5.0: q8 _" W9 I4 n
  6. >>> math.log2(20)- R* @1 X3 E" j$ T- ?
  7. 4.321928094887363
    6 u( _7 }# z3 x. p+ D1 U' l
  8. >>> math.log2(16)* O- J3 C1 Y, a: d' H5 I
  9. 4.0
复制代码

1 d& ]; ^0 L/ D6 ^$ z: v# smath.modf(x)  返回由x的小数部分和整数部分组成的元组
( ^# c3 q) l4 y; O& m0 }. d3 x+ P# [8 N
  1. #返回由x的小数部分和整数部分组成的元组
    1 q+ X+ P/ J% M1 N. Y
  2. modf(x)( v% l. B: n; @, R5 N
  3. Return the fractional and integer parts of x.  Both results carry the sign# D& f$ r. `, v! B$ c
  4. of x and are floats.
    4 U0 O3 m8 n: }6 ~) E
  5. >>> math.modf(math.pi)
    . c% z4 b1 [/ l! h* h/ ~
  6. (0.14159265358979312, 3.0)9 m. u, ]$ {# L! M. c2 D
  7. >>> math.modf(12.34)
    " [& h) ]; S" o$ d' \
  8. (0.33999999999999986, 12.0)
复制代码
: w5 R3 [0 a$ F2 |
math.sqrt(x)  求x的平方根& S5 }' @4 i* T- C/ D/ F
  1. #求x的平方根
    ; M/ n3 W& M/ ~4 E) ^/ x
  2. sqrt(x)9 T, R; F  u7 T% k, ^; v
  3. Return the square root of x.
    0 L; X% a" _! _' T1 j# t; }" j
  4. >>> math.sqrt(100)
    2 c1 S+ o0 p* g6 e) i0 F" A& z* |# [4 E
  5. 10.0
    ; T, S  }: ^. [  K& l) X1 [4 v
  6. >>> math.sqrt(16)4 l1 W+ m+ V) F3 j- l9 K7 t
  7. 4.0: h+ N3 r2 G: @2 k4 }, Z- b) @
  8. >>> math.sqrt(20)7 B' ]9 ?# Q. V: P$ o4 g+ r+ v
  9. 4.47213595499958
复制代码

% ]6 g& q4 b3 e. l$ _math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    , T" u, D4 `3 d3 T
  2. trunc(x:Real) -> Integral
    ; ~8 [. e7 w; U; S7 E! s7 s
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ' l: j8 t$ q) b  g
  4. >>> math.trunc(6.789)% c% {: M8 A$ t) T: F
  5. 6' J- E% q. M' B
  6. >>> math.trunc(math.pi)1 X* v0 ?- r$ |. n+ X
  7. 3
    : I2 O! U" _$ w* X
  8. >>> math.trunc(2.567)
    " V2 |' I: I' z4 h
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

文字版|小黑屋|新大榭 ( 浙ICP备16018253号-1 )|点击这里给站长发消息|

GMT+8, 2026-5-5 23:27 , Processed in 0.084192 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表