新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

, u& ^3 ?; Y/ n  ]2 g* O. e' U1 L2 g【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% v3 e+ i+ R5 S: r# m# d% O3 i- N

5 |+ l; t# p, f( q% X方法1- h, U; U3 U3 O
  1. >>> import math, w( y2 Q: y1 z, o0 q- F% `
  2. >>> math.sqrt(9)) f. h3 H: l! q* K/ u( j
  3. 3.0
复制代码
方法2
  j. Q  Q' f. W! |* x6 o0 _1 a
  1. >>> from math import sqrt% Y, H* Z8 V; c
  2. >>> sqrt(9); P3 ^' @3 B0 }4 P  K, P
  3. 3.0
复制代码
! m8 F/ |2 ?. c* r$ m7 j! U. ~


5 C$ f2 U$ d, ]
math.e  表示一个常量
/ Q+ x: X" @! k7 v2 P3 ^) j
  1. #表示一个常量7 m% t- l. o0 K& ?" v7 l
  2. >>> math.e  x# Q" o" |! ]& i8 m3 o+ W
  3. 2.718281828459045
复制代码
3 m2 C  J. [# {3 y
math.pi  
数字常量,圆周率

' U$ j5 b" q6 ]. Q" z: x+ v4 z
  1. #数字常量,圆周率" U8 ^  e( y! Z/ _/ N
  2. >>> print(math.pi)& D$ Y* }: t% h7 D8 K* ?5 N% h$ r
  3. 3.141592653589793
复制代码
$ p8 U8 N' D4 Y0 B7 h
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
- A/ G2 X5 E' a  h' U, t
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x$ r0 U. C& w! R. q! |
  2. ceil(x)6 f/ ~3 s3 R; m. N0 s2 b
  3. Return the ceiling of x as an int.
    * B* ?$ I7 a5 `% j2 Q
  4. This is the smallest integral value >= x.9 E7 B. [+ q: Y2 h9 n, w( n
  5. ) H0 J! \5 {9 S- ^9 b
  6. >>> math.ceil(4.01)2 Y' t; ^4 |; M: V: v4 L7 r
  7. 5, \& N9 @2 [5 E5 d" [
  8. >>> math.ceil(4.99)% H4 _. P% F* c; [0 s7 ^
  9. 5# o" d0 x- a. S4 C! K9 a5 t; c
  10. >>> math.ceil(-3.99)
    5 `9 Y  g3 e9 V  Q! l' i
  11. -3# Z% O# t0 ]) @/ Y# R
  12. >>> math.ceil(-3.01): t4 Q; Z2 C. h9 k
  13. -3
复制代码
# n2 H( m2 m( ]3 V. |. P
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 Z9 |/ t2 X; Y% S. {( i/ t% y
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    % z/ f9 U( c) E5 [+ Z6 B7 b
  2. floor(x)
    / X; C; P% L4 |. v6 @+ J
  3. Return the floor of x as an int.
    / ~1 W8 r5 w! r: Z; g2 j" l
  4. This is the largest integral value <= x.
    : E, H7 c% w' y! p, m4 i8 ^2 d
  5. >>> math.floor(4.1)2 f% h4 r5 Y2 W0 a. }
  6. 4
    & U: C  h& w6 v
  7. >>> math.floor(4.999)
    ( m0 j2 S5 f; `* g% g/ U
  8. 4
    ! o  W% ?0 s4 a/ E0 P
  9. >>> math.floor(-4.999)  S' E- |) l4 D8 o7 t' b: p, V
  10. -5
    : k" G) t0 ~  L6 g* _  m0 L
  11. >>> math.floor(-4.01)+ V* d& Y, e  O. j
  12. -5
复制代码

! v6 O1 y6 B- H- Z+ X, r8 }math.pow(x,y)  返回x的y次方,即x**y
8 L5 j9 J( f8 c4 y- g. E
  1. #返回x的y次方,即x**y
    ; y+ N6 x- K, s, D+ M
  2. pow(x, y)7 v% Q: D( D6 x3 L4 Z
  3. Return x**y (x to the power of y).7 O9 M. ], k* g
  4. >>> math.pow(3,4)4 P$ W" |" W! v8 `* P' M- d
  5. 81.0
    0 C; y) m. q: p% Q5 Z5 E
  6. >>>
    0 J) S+ C8 j2 ~" M$ ~1 e4 n% }& q1 r2 b
  7. >>> math.pow(2,7)1 |$ b! b5 q+ C8 F3 ^- O7 r/ Y! i
  8. 128.0
复制代码
; G: W+ e* @5 q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 L: d* \  {1 [# {3 k* o8 v) }
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    8 R# b6 t1 ]$ A
  2. log(x[, base])
    : C9 h+ G9 k$ d& Q* D; R' e: C
  3. Return the logarithm of x to the given base.
    4 z7 [9 N- E& [4 `
  4. If the base not specified, returns the natural logarithm (base e) of x.1 ^1 a4 c4 K$ [3 ~
  5. >>> math.log(10)  U' C/ }4 O' C; n
  6. 2.3025850929940466 |6 t3 O; A: I! [
  7. >>> math.log(11)
    1 g% S8 {7 }5 q1 T- d4 ^# `
  8. 2.3978952727983707
    & d) N4 C5 |" D* a3 u
  9. >>> math.log(20)
    ' w$ Q6 L) v( W" b
  10. 2.995732273553991
复制代码

- G* g4 J5 w0 }. m6 Rmath.sin(x)  求x(x为弧度)的正弦值2 B: f- Z5 Y3 `/ \& O8 s6 R
  1. #求x(x为弧度)的正弦值  \' d& K, ^  X1 P+ d" h
  2. sin(x)9 y8 C; }6 a  N: t: c. o3 _* |
  3. Return the sine of x (measured in radians).
    0 p  c( U; [, J( `  i) I
  4. >>> math.sin(math.pi/4)
    - t# B) V5 `6 w
  5. 0.7071067811865475
    & a+ Z; O  T" J3 d/ p% ^* F
  6. >>> math.sin(math.pi/2)" Q. f( w& @4 p( d
  7. 1.05 [$ I/ G- K6 B0 E
  8. >>> math.sin(math.pi/3)& n. h6 U& ^* G. g- _
  9. 0.8660254037844386
复制代码

" K+ j+ x4 D% U/ h, ?/ y" Amath.cos(x)  求x的余弦,x必须是弧度
& P+ B) Z/ }3 Q/ o; W8 d8 X* n8 Q( G
  1. #求x的余弦,x必须是弧度/ f% n! q, B, P6 `8 C- Z- v# }* ^7 Q# g
  2. cos(x)
    ; o7 X/ S7 _/ c1 X9 ~6 {
  3. Return the cosine of x (measured in radians).. a# @& b- j$ D/ O
  4. #math.pi/4表示弧度,转换成角度为45度
    1 E: }5 y  l3 n' z
  5. >>> math.cos(math.pi/4)' q! }; z% J: Q* d$ _* K& O+ O+ J
  6. 0.7071067811865476
    2 g- i8 Z' h% v& ?# k
  7. math.pi/3表示弧度,转换成角度为60度
    * y* o. Q' R0 y
  8. >>> math.cos(math.pi/3)
    4 \/ X& s5 `  r. \7 E
  9. 0.5000000000000001
    + l" ]+ z, F5 h
  10. math.pi/6表示弧度,转换成角度为30度: G/ Z! R* [. e/ d: A- `
  11. >>> math.cos(math.pi/6): |* G$ Z7 g8 J0 _' h* Y  w: L
  12. 0.8660254037844387
复制代码

+ N: }& {- e6 t! l4 |+ ymath.tan(x)  返回x(x为弧度)的正切值
* {9 G! B3 s  o8 m1 ?- O
  1. #返回x(x为弧度)的正切值- o, ~: f7 C" k) U
  2. tan(x)0 x3 o1 n: [$ x) {* n  q
  3. Return the tangent of x (measured in radians).+ y: h  s/ E  W1 |2 F
  4. >>> math.tan(math.pi/4)
    2 a  m6 F. E3 C2 J% X
  5. 0.9999999999999999( F. S, n9 P7 P& f6 i
  6. >>> math.tan(math.pi/6)+ [3 f0 H$ {0 W: ^2 G  A
  7. 0.5773502691896257
    + g7 N; p" m  ?6 C/ I( y+ A, q
  8. >>> math.tan(math.pi/3)
    9 t; L0 l0 b, V% E
  9. 1.7320508075688767
复制代码
( `6 A* K7 w3 ]2 b' c
math.degrees(x)  把x从弧度转换成角度4 N4 R8 Z* E+ x( e
  1. #把x从弧度转换成角度. t9 J3 D- B& `# f7 W9 N* C( O4 P
  2. degrees(x)
    . t7 W3 |4 N2 S: m# X
  3. Convert angle x from radians to degrees.* z6 q. r8 ]( y# A
  4.   ?* a  J' Q( b1 y7 W
  5. >>> math.degrees(math.pi/4)
    - n' G5 k1 ]) Z" |
  6. 45.0
    7 [9 v# B9 W; g; a1 `) X2 x
  7. >>> math.degrees(math.pi)
    ; `. P7 T/ d/ |& u) N# J7 q1 T
  8. 180.0  d% o( a; |, S. q% j  N
  9. >>> math.degrees(math.pi/6); }  f. W: e5 }
  10. 29.999999999999996
    2 S) @" m4 i7 G& U
  11. >>> math.degrees(math.pi/3)
    % v! M' [) ~6 K
  12. 59.99999999999999
复制代码

5 b- W! k* |: o# @math.radians(x)  把角度x转换成弧度
4 b$ l$ d+ j5 J# U6 e1 u
  1. #把角度x转换成弧度: f/ \& @0 {" M! }" e2 `0 t$ s
  2. radians(x)( ~* |* G' i. S# H7 a, T
  3. Convert angle x from degrees to radians.7 g8 t7 e0 r$ Y: J; `# t* ^5 \
  4. >>> math.radians(45)
    1 _; u3 r2 v- H6 G" }4 B5 H
  5. 0.7853981633974483  g+ g! j2 Y9 l; d& c8 N
  6. >>> math.radians(60)! B9 o" X: N7 e. U5 ^
  7. 1.0471975511965976
复制代码

" G/ u" T# W4 d% |: x( [3 Vmath.copysign(x,y)  把y的正负号加到x前面,可以使用03 p4 `' [6 ]1 C1 l
  1. #把y的正负号加到x前面,可以使用0' b* B6 ^) b5 ]$ C( _
  2. copysign(x, y)
    % }1 Z! R) Q, L( i* ^
  3. Return a float with the magnitude (absolute value) of x but the sign
    / [  S& v! K3 c4 f7 j4 E
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    " x: ~/ C8 q/ W/ b
  5. returns -1.0.
    3 [! Y& `5 w% ]' P- t; P* D7 \+ H8 ^

  6. ' \1 ]" `- M" P1 q
  7. >>> math.copysign(2,3)6 {' B2 y1 M* v8 T1 O
  8. 2.0- D5 Q4 X$ P) h- `$ o+ j
  9. >>> math.copysign(2,-3)$ v1 \2 @, i4 l
  10. -2.0- a0 T6 w( v! U! ^
  11. >>> math.copysign(3,8)
    : ~( v; n. n# T$ i* {
  12. 3.0
    8 ]6 y" P5 i) L8 d8 F
  13. >>> math.copysign(3,-8)1 [5 v! G2 h: E: k
  14. -3.0
复制代码

3 m/ t( v& u3 ~) A  smath.exp(x)  返回math.e,也就是2.71828的x次方
0 w, K8 \5 F: j: F- V6 G
  1. #返回math.e,也就是2.71828的x次方/ \' C; d8 [  @1 Y. Y! y
  2. exp(x)( `; g2 W( E  L4 n. u$ G# l! I
  3. Return e raised to the power of x.! \! N! |+ j& @, ~* d
  4. 5 l# @/ x0 x# N
  5. >>> math.exp(1)! H" ~7 d; G' Q( @' l1 k6 x2 h9 ]
  6. 2.718281828459045
    5 [& j8 v' }  V5 P% a
  7. >>> math.exp(2). T0 B' K) r+ u6 Y. ]
  8. 7.38905609893065/ l' q  s' {0 n, O
  9. >>> math.exp(3)' q% X8 |$ L7 T# ~; p3 L
  10. 20.085536923187668
复制代码
8 ^% a5 j4 G; n1 V# @# e4 s; Y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
' `/ b3 d) I% v; h
  1. #返回math.e的x(其值为2.71828)次方的值减16 l; o7 u% _. @7 J: X
  2. expm1(x)- t5 h6 _8 s- E1 J# y  y- h
  3. Return exp(x)-1.& M9 f8 m6 M8 b) T
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 l; s- r  Q+ `+ q) E

  5. $ }3 C3 g8 d; q# o% \
  6. >>> math.expm1(1)
    : Y# }7 @& U1 i! g
  7. 1.718281828459045
    ) k- ~# H% e  G# g
  8. >>> math.expm1(2)* Q2 X( J1 ?0 \* b2 j" d0 S* C
  9. 6.38905609893065
    . j) k- c; O/ {: n) Q, V9 I
  10. >>> math.expm1(3)
    2 _: u  o, c" }9 s7 W5 N
  11. 19.085536923187668
复制代码
# |& K- A* B; c* H: ^8 U
math.fabs(x)  返回x的绝对值
3 q0 W& p4 |; K. `, X; h, D
  1. #返回x的绝对值
    1 b' m  [, u, r3 H
  2. fabs(x)7 A& f& e5 O0 x: w, Y7 O+ E
  3. Return the absolute value of the float x.8 \; t9 O0 u/ P, }/ N7 E9 x
  4. * O& }5 G  F; `1 F
  5. >>> math.fabs(-0.003)
    $ i/ Q) K. d: q/ D: j
  6. 0.003; q* r1 r$ a. [9 w$ @
  7. >>> math.fabs(-110)
    7 I, \( g' O7 ^) {$ d# d  P
  8. 110.0+ O  L, L2 P# `& m; X0 h) ?# f7 }
  9. >>> math.fabs(100)
    ; O0 E4 e2 k" v6 J, x' K1 T- }
  10. 100.0
复制代码

, F2 Z9 x/ P! ^" q& n7 fmath.factorial(x)  取x的阶乘的值
7 j5 a8 B. y- {+ A0 b% a
  1. #取x的阶乘的值! M! J) N4 k) S, @. M7 f7 v
  2. factorial(x) -> Integral$ e7 h  s! P, P- L
  3. Find x!. Raise a ValueError if x is negative or non-integral.5 a5 \& Y1 _) F& n! Y
  4. >>> math.factorial(1)
    # v$ K3 G7 b. o  c( ?
  5. 1' \. I0 K& |. D) S) A
  6. >>> math.factorial(2)( i2 A5 ~6 @: k/ P
  7. 2
    ; ^* {8 w' K( P2 }$ Q7 m
  8. >>> math.factorial(3)+ X: Y5 I! L2 r4 C* C
  9. 6; p* q7 o' [: n+ B1 i' _
  10. >>> math.factorial(5)* I. |5 y7 M! i( v: R( h' K
  11. 120" D; `- T1 U# f
  12. >>> math.factorial(10)- f; @# Q8 z, z/ ?8 _) }  X8 V
  13. 3628800
复制代码
) X2 ~: C8 i$ n5 ~; H
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
9 g! P- I$ {2 U, q" `
  1. #得到x/y的余数,其值是一个浮点数- U, D+ J5 @6 `1 ?+ j
  2. fmod(x, y)  `9 O9 H$ o1 F+ A
  3. Return fmod(x, y), according to platform C.  x % y may differ.; ?( e# E2 c9 T2 }6 I0 ^
  4. >>> math.fmod(20,3)
    7 y, d! i7 {" Q7 Y$ E0 F& v0 ?& K
  5. 2.0
    7 S" h4 k7 `) {/ P; a$ [5 h/ x& L
  6. >>> math.fmod(20,7)
    - m  u( @+ ^: H6 r% y2 m3 e
  7. 6.0
复制代码

4 ^# [& A7 L2 v5 C! Gmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
7 q, O2 r$ K$ a. Q5 u8 h5 `9 k
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 V3 c% M+ i* G" t4 G
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" Z% ~' e( i5 e  K, s0 G
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ; x" k! W# i8 B- ?7 j
  4. frexp(x)# R+ N- n7 ]; j3 o& _* y
  5. Return the mantissa and exponent of x, as pair (m, e).
    ! O' g/ m8 U  \/ ~
  6. m is a float and e is an int, such that x = m * 2.**e.4 M$ h) U. a  p. |+ \
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 r  [# u- m$ i6 T3 W+ P% N8 V
  8. >>> math.frexp(10)- S. ]2 ~/ ~" @% \, }
  9. (0.625, 4)) u5 H. ]1 }1 d$ H3 ^0 s8 A
  10. >>> math.frexp(75)0 p% z0 B1 [( n+ q3 E" V
  11. (0.5859375, 7)% J% M% ~9 ^5 v: J
  12. >>> math.frexp(-40)5 m# x! i( Y$ k: R" }
  13. (-0.625, 6)
    . Z4 R6 B9 y3 M3 e1 [7 q
  14. >>> math.frexp(-100)
    ( Q4 Y+ R6 t& W9 |7 \
  15. (-0.78125, 7)
    8 S% d$ L" D  l7 `3 e! [$ a* y
  16. >>> math.frexp(100)  E1 P3 V" o$ c! E+ e) v+ C. v
  17. (0.78125, 7)
复制代码

, R* F: q) ?- I8 k1 Emath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列8 U0 X% F" U7 o7 w; I/ E) R5 h3 x
  1. #对迭代器里的每个元素进行求和操作
    ' j! B' \: s! K( @. T8 X+ A$ N
  2. fsum(iterable)
    ( }! H& l3 e9 h1 l
  3. Return an accurate floating point sum of values in the iterable.
      u6 p) [8 {9 D7 O" g' X( U# s
  4. Assumes IEEE-754 floating point arithmetic.
    5 M  _( Q$ m7 K; H6 c
  5. >>> math.fsum([1,2,3,4]), l8 w# u. c2 d$ Z9 n) j  L
  6. 10.0
    ) h' y. F  p9 |: Z* G
  7. >>> math.fsum((1,2,3,4))' Q" f3 f* q- ]* x
  8. 10.02 M- N+ a4 a* q  P
  9. >>> math.fsum((-1,-2,-3,-4))  {" P3 C& r$ x8 k! B% m
  10. -10.0
    3 N' A6 h1 s$ n# z2 P
  11. >>> math.fsum([-1,-2,-3,-4])
    2 t: ~8 u% l7 I+ O9 E
  12. -10.0
复制代码
6 L0 J3 B& T9 u1 p  K/ p  h1 ?
math.gcd(x,y)  返回x和y的最大公约数% R. J0 x# X% f: [$ [; z
  1. #返回x和y的最大公约数& p+ ?! h3 a* _- o
  2. gcd(x, y) -> int
    + E% d  A/ f1 {; R" @# B4 |
  3. greatest common divisor of x and y" H/ ^! v+ `4 m7 X: ?8 X0 @# \
  4. >>> math.gcd(8,6): O$ P# ^0 V/ S- S; ]
  5. 2% x3 V  ^2 {# u
  6. >>> math.gcd(40,20)& s5 H0 {8 b0 ?
  7. 209 Z# B' J6 x4 s! J3 h
  8. >>> math.gcd(8,12)' @4 b* s: [% `7 m' w0 P
  9. 4
复制代码

- Z& X4 N+ y% A/ m% ?math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
) p% o, v# X3 |% ~# Q5 s
  1. #得到(x**2+y**2),平方的值$ j5 N2 z, ?$ j. ]3 n9 P3 {' b1 M
  2. hypot(x, y)
    7 i. L  o9 I, M. Z3 A9 h+ x  F
  3. Return the Euclidean distance, sqrt(x*x + y*y).7 e; t4 w4 z: R& z8 N5 ~
  4. >>> math.hypot(3,4)
    ! M7 I- X& X* `. s1 h
  5. 5.0: l7 Y. x; V" y$ X0 @, A- o& u
  6. >>> math.hypot(6,8)  x9 G5 B3 n6 R  x
  7. 10.0
复制代码
. p: H* ~+ N6 \
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False# V) L) G7 C: O: a3 z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    7 Z7 y5 h2 M9 s
  2. isfinite(x) -> bool
    ) a; G* b# b- [
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    + N% D  Q% n# H# X) R
  4. >>> math.isfinite(100)
      w7 w; D9 }. {3 T$ t' \
  5. True8 w+ B8 ^( m( \  ^# \
  6. >>> math.isfinite(0). c& u; t( }5 V+ T- @5 q# _; x
  7. True' c- p! {9 S( O7 ]) {9 M# k
  8. >>> math.isfinite(0.1)5 p0 R# l8 r9 ~
  9. True* W* L0 H7 w7 O8 m2 e8 W) M7 b
  10. >>> math.isfinite("a")
    2 I* @* x! Q; q3 @! ?3 b4 j1 n1 G0 Y
  11. >>> math.isfinite(0.0001)2 {0 q  _7 i) ?8 W9 }& j+ q- J
  12. True
复制代码

. K1 r8 C  t  v4 s2 Bmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False5 c" W4 T* \4 ?7 ^6 x0 W: {. w
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False1 M3 G& R2 X4 Q, w9 ~" {
  2. isinf(x) -> bool
    % a; q: G0 _7 N. ]9 O
  3. Return True if x is a positive or negative infinity, and False otherwise.# H3 x  l9 w2 E
  4. >>> math.isinf(234)
    # J0 `2 [  d; r) k" N9 ]" k: A
  5. False3 \' a0 L, a3 _- I/ U& H, v* \
  6. >>> math.isinf(0.1)
    4 N% U! W8 g+ i: Y
  7. False
复制代码

1 I+ I  ?7 M7 I- h7 k- }( amath.isnan(x)  如果x不是数字True,否则返回False" C; L, Z( m& ?3 Q, H
  1. #如果x不是数字True,否则返回False; @7 O0 E# }5 M* J/ m. |, d& x, j
  2. isnan(x) -> bool1 B/ r6 A8 N+ P  A$ ~
  3. Return True if x is a NaN (not a number), and False otherwise.
    & R* I  S+ d( q3 ~
  4. >>> math.isnan(23)
    ( W& i; B% g, `+ U7 ~4 F
  5. False$ J; i2 o. g# h
  6. >>> math.isnan(0.01)
    1 j% r! C$ [# b7 d( f/ ]- ]) B* _
  7. False
复制代码

! N1 b. [9 ?" i) X2 W: }; nmath.ldexp(x,i)  返回x*(2**i)的值
& D! R0 \  S% \. N; ~
  1. #返回x*(2**i)的值0 L" @- V. q( X+ Q# ~
  2. ldexp(x, i)7 b: K' b& v2 U1 _% U/ V$ V
  3. Return x * (2**i).
    ' ?' G6 e: V- ~* j( n. U% C; J  s
  4. >>> math.ldexp(5,5)
    ! ?! s9 e; B% E# L" M
  5. 160.0
    # a( d7 n/ b* T$ K, D+ n
  6. >>> math.ldexp(3,5)
    3 q/ \( ~/ w$ A# t0 T$ ?- r# f8 ]
  7. 96.0
复制代码
! c& c/ O6 ?1 Z! h4 M
math.log10(x)  返回x的以10为底的对数
3 n8 J! K& |+ I$ W# r
  1. #返回x的以10为底的对数# O3 f9 b8 o2 V9 m5 O1 F( |
  2. log10(x)! T+ x! d! v: @( y7 D# k$ X
  3. Return the base 10 logarithm of x.
    & I. A1 V3 }) h+ l
  4. >>> math.log10(10)
    5 ?% O; S3 D  s. C7 |+ p7 i: @
  5. 1.00 a; ^- }8 H" q* P3 D2 X) Z2 w
  6. >>> math.log10(100)
    ) C* H/ V1 g) @- E6 R4 q; Q" m
  7. 2.0; o2 q% K. v6 _- U
  8. #即10的1.3次方的结果为20
    : r  V+ ]. N/ |" O/ C
  9. >>> math.log10(20)
    ' j7 \7 A; I4 M3 c6 I4 Q; y8 C
  10. 1.3010299956639813
复制代码

% Z4 c/ l& a& m0 O  `( b3 gmath.log1p(x)  返回x+1的自然对数(基数为e)的值  u7 E  R. \+ m$ e0 D' c" P
  1. #返回x+1的自然对数(基数为e)的值
    # b: {/ x' K/ M! q' f
  2. log1p(x)0 t8 D# B+ C/ z# l. z6 N
  3. Return the natural logarithm of 1+x (base e).' L$ G0 C/ W! J. j5 J- H3 `0 G
  4. The result is computed in a way which is accurate for x near zero.
    * c$ ?8 A* c  R: T2 g1 |- O
  5. >>> math.log(10)7 q; A' ^  R  I: `  Z/ y! ]: Q
  6. 2.302585092994046
    : q! d. B2 y# S( j8 E2 |
  7. >>> math.log1p(10)# U  c. V! J: P/ _
  8. 2.39789527279837071 C; y! I& ?+ B( X2 G$ x
  9. >>> math.log(11)
    & }! G) C8 W6 o* F8 f. e
  10. 2.3978952727983707
复制代码

$ P( J( V( ^3 y" p( Q- ~: w) F0 Amath.log2(x)  返回x的基2对数% ?5 O, |+ h% k1 I
  1. #返回x的基2对数; O5 x# i$ s1 R
  2. log2(x)% ]/ T  i1 y3 L) @
  3. Return the base 2 logarithm of x.
    % M6 W4 k" c5 p! W1 N+ j4 A$ M* A/ s
  4. >>> math.log2(32)
    - a- g  y) y5 q
  5. 5.09 q: D* U+ n" h) a
  6. >>> math.log2(20)" S, w8 w! j3 W7 n
  7. 4.321928094887363
    ' L* S8 _# i+ q
  8. >>> math.log2(16). x6 Q3 P0 @1 p& z% O
  9. 4.0
复制代码

, l! _$ ?, D% W1 wmath.modf(x)  返回由x的小数部分和整数部分组成的元组; a- U/ ~, C! Z( X# X
  1. #返回由x的小数部分和整数部分组成的元组
    6 h; G) v; U+ O6 @& w9 r
  2. modf(x)
      ?: s6 y3 U* F6 K- w; X$ ~. N
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ! \) a8 ]$ r; D. p( D
  4. of x and are floats.8 ]$ `( @# B  A' h/ E" E% e
  5. >>> math.modf(math.pi)
    * l& t4 L! A3 E* T) q6 G# y, ~
  6. (0.14159265358979312, 3.0)9 c0 [+ y3 q3 S- d
  7. >>> math.modf(12.34)
    $ f: q- G/ e8 `" X6 P* n
  8. (0.33999999999999986, 12.0)
复制代码
( P8 X  x* I' V4 }
math.sqrt(x)  求x的平方根" ~( r) K# \0 H4 w
  1. #求x的平方根, u0 U4 f* `- r$ j/ G; d
  2. sqrt(x)- @3 o) j8 _  Q2 s5 K  [! N/ r$ A
  3. Return the square root of x.( Y7 Z$ z' ]& K% J
  4. >>> math.sqrt(100)/ F1 J- W* t- D0 X+ Q% Y
  5. 10.0  E; {/ n& s# ]* I5 c
  6. >>> math.sqrt(16)
    9 ?% ]7 o& L( ?% }# ?2 F; U
  7. 4.0
    ; H- \. [4 E* m
  8. >>> math.sqrt(20)
    0 L. l" a9 g- W/ K
  9. 4.47213595499958
复制代码

& K7 d6 H7 L( s- Emath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 h4 H4 e: G" b. w% Z
  2. trunc(x:Real) -> Integral  ~/ ^( V- e4 E- x; L8 d2 I1 |
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    4 u$ s/ p* q* b, |* x( }
  4. >>> math.trunc(6.789)' n4 b! A& ?. b( S. y" h
  5. 6
    ' v1 T" r7 K* D+ Y* s3 t
  6. >>> math.trunc(math.pi)
    ' n$ F8 [& V6 P! K8 G
  7. 32 v% ^' O0 O7 m+ O' f+ ^- W
  8. >>> math.trunc(2.567)8 s+ w8 ]& Q4 r/ Z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-20 15:01 , Processed in 0.096096 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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