新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

5 H# S) G5 d- u0 T4 k5 v【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 U* I* Z2 o' G/ J

9 l0 i& n6 ^5 C( W% a方法1
% ?* f3 ~+ }( P/ V2 E1 u
  1. >>> import math
    ! C. r  ?" e) c0 g7 z. P/ a; m% e9 c
  2. >>> math.sqrt(9); n" F. i* W9 x
  3. 3.0
复制代码
方法24 F8 `: e. F: s
  1. >>> from math import sqrt
    9 @6 S) ?! f8 }) C' `$ P
  2. >>> sqrt(9)
    ! G8 L5 @. R2 v$ d/ s$ ]
  3. 3.0
复制代码
% `* Y1 [& `' u3 f0 t$ c" f  L

9 ^6 ]; J7 {/ K+ \+ }+ N. w1 ]
math.e  表示一个常量) Q" x# p9 k; |: z
  1. #表示一个常量
    3 }4 ~! B; e8 B3 u6 O! X; j+ ~
  2. >>> math.e
    + S- T+ H, }# ]1 [5 k! k" k- h1 x7 R) \
  3. 2.718281828459045
复制代码
  {$ Z; p+ ~! Y9 E' b' a* ~
math.pi  
数字常量,圆周率
4 i6 g: a: F; L4 i5 {0 t8 h8 {; C$ U
  1. #数字常量,圆周率
    9 w% m* x9 a: O" z% b
  2. >>> print(math.pi)& `: L4 s; A5 A/ C2 ~% [
  3. 3.141592653589793
复制代码

, M- R4 i; X/ Q' S) Gmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

. J* Y. H( g6 k2 W' X% a9 C0 s& B) \
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x" ^7 z1 `" \( E1 s8 ^3 a; x
  2. ceil(x)2 R3 K: @$ Y6 X& ^
  3. Return the ceiling of x as an int.
    8 Q# N( Q5 o9 E% E) F, ^* C
  4. This is the smallest integral value >= x." i! t0 ~1 E: w( |1 L% O
  5. 4 {0 `; K' @+ P! z" ?+ C
  6. >>> math.ceil(4.01), i5 E2 J3 y" @: v9 W8 C  [
  7. 5
    1 u# ]# o9 C: B  z
  8. >>> math.ceil(4.99)% F0 e4 C% r% ^$ m
  9. 5, S3 N1 r9 O& A" s/ x8 Q  V& g
  10. >>> math.ceil(-3.99)2 {# r1 V2 E$ E* `. s
  11. -3/ Z9 T5 w9 `: ?3 o& K9 Q/ k
  12. >>> math.ceil(-3.01)
      Q9 w) u3 G4 k3 d9 ^
  13. -3
复制代码
0 b% z9 c. m0 F( a
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
- M. N$ L2 [" U6 k
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 t" R% o3 T; K. R
  2. floor(x)4 v$ o( W7 e/ y. [" z/ ^; ]
  3. Return the floor of x as an int.
    # }$ x5 S9 K, Q
  4. This is the largest integral value <= x.
    : C2 E# O2 C- a9 l5 j9 h
  5. >>> math.floor(4.1)' J0 o% K; c: g3 j( R) M
  6. 4  }; }& y) ^- q. U; a- g/ ~
  7. >>> math.floor(4.999)
    ) C2 o; B8 v, P! F: e* H
  8. 4
    2 L& I7 [6 g8 c2 x6 B' O* A+ i
  9. >>> math.floor(-4.999). f2 a+ a; H" o  y
  10. -5  z* l& z. Z" o# H, l
  11. >>> math.floor(-4.01)
    : d% p5 \* N1 J- ]: u$ m
  12. -5
复制代码

1 R: l7 Q4 K( Q: j0 g* `5 pmath.pow(x,y)  返回x的y次方,即x**y8 `$ z. G+ Q" ~
  1. #返回x的y次方,即x**y
    : X0 A9 J/ r% T) }- e0 f; C
  2. pow(x, y)! _  S- D4 V, |2 d" Z
  3. Return x**y (x to the power of y).
    # m- Z8 n9 Z4 W
  4. >>> math.pow(3,4)0 C+ e$ B% q6 N% t9 I2 S# L8 J/ _
  5. 81.0
    ( s1 H3 c% }, I# J8 i( b
  6. >>> . K' q) [6 k& u8 M. L
  7. >>> math.pow(2,7)  B- h" n! Q* }0 O. f
  8. 128.0
复制代码

6 T8 y! T: j/ _' k% ?5 mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 V7 Q% J& b7 n, C6 j, {$ ~8 b# U
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- Z8 m: A/ P( s
  2. log(x[, base])( Z- m! ^0 w! L$ ?
  3. Return the logarithm of x to the given base.
    1 f2 M! h8 K1 u& d* L
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ r; Z0 W; T9 {! H$ c
  5. >>> math.log(10)& t9 D5 D3 Q- a' p2 @0 J
  6. 2.3025850929940461 T! i+ H: o3 N0 s- ^# O
  7. >>> math.log(11)7 a. l3 l# f4 Q. l, R
  8. 2.3978952727983707
    ' v: @" H/ A& o- p
  9. >>> math.log(20)
    8 M$ I. n  o  h. J/ J9 M* e
  10. 2.995732273553991
复制代码

+ n* d9 I" _4 }4 z0 L  Dmath.sin(x)  求x(x为弧度)的正弦值
6 b4 e, k  q' a- E( Y
  1. #求x(x为弧度)的正弦值8 q: m4 ?- P( p; \: L9 z9 R
  2. sin(x)
    6 T: r# n0 b4 \" S
  3. Return the sine of x (measured in radians).! C: [$ L, v1 n+ C( C  w/ o* y. n
  4. >>> math.sin(math.pi/4)  E/ @6 M- z7 S4 A& g3 Z
  5. 0.7071067811865475, S' a/ y5 {) m8 z
  6. >>> math.sin(math.pi/2)
    9 Z* m) I" _, |
  7. 1.0
    & ?4 A8 Z$ k$ F" l* C: G; X2 `
  8. >>> math.sin(math.pi/3)# T: ]/ {5 |- y5 N$ u! e
  9. 0.8660254037844386
复制代码

# ?  L+ `' w0 t! P% w9 ]: ?- y5 L  Umath.cos(x)  求x的余弦,x必须是弧度
0 t, `! w' d+ m4 A. S! `7 f
  1. #求x的余弦,x必须是弧度  s) H# I, C1 w6 s
  2. cos(x)" ~: t7 @) X* U$ _. ?
  3. Return the cosine of x (measured in radians).
    6 ?& H) z6 B0 O# {, I; j! y
  4. #math.pi/4表示弧度,转换成角度为45度
    % F7 Y* ~, K" j9 ]4 ]- N" U
  5. >>> math.cos(math.pi/4)
    3 I. {9 S5 m" h9 H3 T+ {! I& _1 G
  6. 0.7071067811865476
    * e) F9 N. V3 I5 c$ V5 y0 e
  7. math.pi/3表示弧度,转换成角度为60度
    / |+ K8 Q* E; r. a$ T% [( f
  8. >>> math.cos(math.pi/3)
    $ P9 {3 k7 H! L; Y( i
  9. 0.5000000000000001% H7 C) H: d6 M* q  o
  10. math.pi/6表示弧度,转换成角度为30度$ P! c, J: j8 @( I
  11. >>> math.cos(math.pi/6)
    $ ?& P$ U2 _! J/ q  M+ f
  12. 0.8660254037844387
复制代码
6 Z8 R; ?) E8 E: U* J  ^0 }
math.tan(x)  返回x(x为弧度)的正切值7 O. n  i6 v+ H5 _' R) v! y' s
  1. #返回x(x为弧度)的正切值
    4 d3 H  z1 n* Q
  2. tan(x)- J+ w7 a) q2 x0 ]
  3. Return the tangent of x (measured in radians).
    2 E! z% W" m8 T$ e$ ~! m
  4. >>> math.tan(math.pi/4)% n9 @: S; }, x5 I- {" e2 \
  5. 0.99999999999999997 E% H- Q/ B  h) K. X, o
  6. >>> math.tan(math.pi/6): n4 G1 r2 Y, }( D
  7. 0.57735026918962573 _+ U; |, o; ~' W; H" d
  8. >>> math.tan(math.pi/3)
    0 s( X6 a+ X3 G7 Q: Y- L
  9. 1.7320508075688767
复制代码

2 ?* F+ w* Y5 n- x# M; N. ^math.degrees(x)  把x从弧度转换成角度+ H5 ^( j) b1 H
  1. #把x从弧度转换成角度
    2 a, V0 J/ `/ ]+ m
  2. degrees(x)
    # F" a9 N1 P2 i: x7 J
  3. Convert angle x from radians to degrees.  m7 D7 p/ }* O: I, _% u% _+ ^4 [

  4. ! ^; \: Q: u2 R* J0 B% r9 q
  5. >>> math.degrees(math.pi/4)# f; G% E' f. f$ x4 @
  6. 45.0/ A; K( r. X) u2 X% e) |
  7. >>> math.degrees(math.pi)
    0 e/ @- L/ z- \
  8. 180.0
    8 H" `+ Q, C6 F2 ^/ `
  9. >>> math.degrees(math.pi/6)
    & ?7 \# X/ ^- b8 J" S1 ?) Y
  10. 29.999999999999996
    7 J; y) t  e& u* B& k$ d* U
  11. >>> math.degrees(math.pi/3)
    : l& y! {+ I* P' o# _
  12. 59.99999999999999
复制代码
2 B" t. n( Q4 i; i2 \1 F
math.radians(x)  把角度x转换成弧度( e9 ]7 u8 U, @! j( j
  1. #把角度x转换成弧度
    9 W8 w& Z1 N- D* ~  Y9 U
  2. radians(x)
    ) {0 k7 m* I! N' i6 F. }
  3. Convert angle x from degrees to radians.
    1 `# U& g- I/ j4 k3 ^" ?) w
  4. >>> math.radians(45)
    & Y+ c" g# D/ b% Y: m# ^
  5. 0.7853981633974483; x0 w9 t! X* [( ~0 \7 G% z
  6. >>> math.radians(60)
    ) B' t0 t1 z. y5 S" ]
  7. 1.0471975511965976
复制代码
: I( t$ y0 _# S4 r! V
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
# e7 T+ b' V' o( K/ r0 z8 P
  1. #把y的正负号加到x前面,可以使用0
    . P0 W0 S7 ~3 {0 z. p0 B4 y% S
  2. copysign(x, y)
    ' Q; y, ]3 ]0 z* A
  3. Return a float with the magnitude (absolute value) of x but the sign ; D0 g. z3 D/ `" y% g
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) h8 V5 ]8 w* w9 o
  5. returns -1.0.
    9 {8 F' n; R6 W, \8 K" S4 p& H
  6. ! x" @( B! s, @9 G$ @
  7. >>> math.copysign(2,3)
    ; g" o3 P- {8 _& I" N4 d
  8. 2.0% S3 E% T: j- c) q; i
  9. >>> math.copysign(2,-3)$ Q3 e& i) V. _; a
  10. -2.0' {& ^- ], e# {7 @/ ]
  11. >>> math.copysign(3,8)
    ; P. l; W  _$ O
  12. 3.0; Y$ k' u0 w# T# N8 i- ?' h
  13. >>> math.copysign(3,-8)
    4 q8 Z/ x3 c* k$ E
  14. -3.0
复制代码

9 f9 Q7 ~; |( L" @! [, f2 o1 Pmath.exp(x)  返回math.e,也就是2.71828的x次方$ \( X- s) R; `+ @  _+ }1 \0 G. R
  1. #返回math.e,也就是2.71828的x次方) P; T- W2 V% L) }3 r! B
  2. exp(x)
    ; o3 o% }7 M3 ]
  3. Return e raised to the power of x.
      f5 B9 Z8 v5 \8 u8 |" q! z$ S
  4. ; R* E2 f. d! D/ L: g% y: {/ d
  5. >>> math.exp(1)5 d1 p: F, Y3 A+ P
  6. 2.718281828459045
    # p! F2 d" Q- s8 a
  7. >>> math.exp(2)1 ?, x1 ?% u/ q* B+ C
  8. 7.38905609893065
    9 a3 F- X4 a& x7 ^. I/ z7 [& U
  9. >>> math.exp(3)7 g% r# L, Q. M9 F
  10. 20.085536923187668
复制代码

: v4 `# I% u3 @' Mmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
* g1 J7 i; c# ]: g& ^1 G
  1. #返回math.e的x(其值为2.71828)次方的值减1
    6 D. P/ a% ~8 B5 _9 ^6 G3 G6 d
  2. expm1(x)& j: B$ G' T) v: v2 ~
  3. Return exp(x)-1.
    # x# c0 z1 K3 e7 ~1 j# p
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.% d3 |( c  I1 Y
  5. . U7 S) x2 r3 _- X0 |
  6. >>> math.expm1(1)
    6 H3 H7 B( b9 N# }: t/ ]* q
  7. 1.718281828459045' K9 d4 d6 N% Y( L) L% ^
  8. >>> math.expm1(2)7 x2 T& h% J0 j4 t8 s
  9. 6.389056098930657 j0 S0 ^5 w8 y6 L& k1 ?( i* b
  10. >>> math.expm1(3)
    + M0 i9 h1 \! X2 @; {3 {6 T* h& g
  11. 19.085536923187668
复制代码

3 k$ T4 X2 F! C7 T& W1 G) cmath.fabs(x)  返回x的绝对值
/ o6 k9 n+ Z4 u/ p3 l
  1. #返回x的绝对值: h' [$ q3 k2 u) L; R
  2. fabs(x)8 e& E. a6 d, B) I6 S4 {
  3. Return the absolute value of the float x.
    7 R- \" C! `5 o5 N
  4. ! }3 I; A: Y; c7 e% C3 T) K
  5. >>> math.fabs(-0.003)
    : C/ ~! G2 J5 l; j! H$ E6 k/ f
  6. 0.003
    4 V( E* C! ]* k' b! y4 Z+ q
  7. >>> math.fabs(-110)5 }3 K* D; W: [! w
  8. 110.0
    ' \. S; z( Z. I# q  H- z5 x
  9. >>> math.fabs(100)' }" q( y# G8 O
  10. 100.0
复制代码

4 z/ ]4 e8 b' `- L( \5 Pmath.factorial(x)  取x的阶乘的值
1 ]- S) H( n9 {: T5 _- X3 a
  1. #取x的阶乘的值
      U9 X. P+ ~* H; j% M! R
  2. factorial(x) -> Integral& v& r/ u- y3 w
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    $ Z/ k0 ]" P: O
  4. >>> math.factorial(1)4 W2 d6 j- L$ o2 r, B" A) f
  5. 1
    0 Z; I# X$ E9 U0 _/ f5 b
  6. >>> math.factorial(2)
    5 v) v- ]% {# Y
  7. 2$ a  _: B! M  S0 l' R
  8. >>> math.factorial(3)( i5 G8 H8 Y. j$ q4 b8 @5 W
  9. 6! g3 [: r- r4 Y, \9 n" v; A; F: Z
  10. >>> math.factorial(5)0 Q9 G9 \6 f7 t; V& U1 n
  11. 120
    4 Z. v1 q, B  s1 V0 h
  12. >>> math.factorial(10), ^$ ~$ |& _( @
  13. 3628800
复制代码
9 b: R2 @' a5 M7 q5 B. b9 K7 E
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
4 H( y5 z$ p) Q  Z2 c6 f5 }
  1. #得到x/y的余数,其值是一个浮点数
    , d* \& u8 G5 I
  2. fmod(x, y)6 C$ Q* i* G, d1 u2 j1 @9 Q
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ; C, c. M2 M( z$ e4 l  L
  4. >>> math.fmod(20,3)
    ( D+ @, `7 G% x  g
  5. 2.0- n  r' N3 W- t+ b0 ?7 y. x
  6. >>> math.fmod(20,7)1 }) z. j0 r$ N3 J2 y+ ~
  7. 6.0
复制代码
* ?' e4 e6 r. |
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) ^1 `2 R0 p$ ~5 ?
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,0 E: m4 Z3 ~  X1 |4 I% ~: q! ^
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 Y  U! M) B! X  c# b/ }
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    1 L5 l/ F7 b, L- Y3 g4 ~, u, C
  4. frexp(x)
    / w4 C- e6 @4 q, m; V7 v" _9 ~/ |
  5. Return the mantissa and exponent of x, as pair (m, e).% w6 w; f' }9 J- F" M9 Z; r  X
  6. m is a float and e is an int, such that x = m * 2.**e.8 U* B" N+ N" M4 ?
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    # r* l6 z$ p# U8 b1 `! o# w! T/ l
  8. >>> math.frexp(10)8 x7 E. t4 m; E4 f& ~7 G9 B
  9. (0.625, 4), [( _# v: {# c
  10. >>> math.frexp(75)4 R/ M5 I1 L& Q; i. @& j  W% r; q
  11. (0.5859375, 7)( y! w, H9 A; K  V4 H+ T2 y
  12. >>> math.frexp(-40)9 m8 l" _% e( Y2 `
  13. (-0.625, 6)
    ) v2 `2 G; o- A. y. u
  14. >>> math.frexp(-100)$ w& p4 B" M. ?; m* \7 v2 S
  15. (-0.78125, 7)
    % l- _( P8 P. [% T! ]- z
  16. >>> math.frexp(100)
    ' n! ]. k* G/ q5 @: y: V
  17. (0.78125, 7)
复制代码

  T2 n7 J7 k0 c/ n& Q* q, \$ q9 [math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列1 X4 D% G& I& i
  1. #对迭代器里的每个元素进行求和操作1 M# W7 W" j* B# x* Y
  2. fsum(iterable)* ?: @/ Y$ Q" \# L% z
  3. Return an accurate floating point sum of values in the iterable.4 h2 P; O$ q5 {" S) M
  4. Assumes IEEE-754 floating point arithmetic.4 a9 x1 p: u) J" n
  5. >>> math.fsum([1,2,3,4])
    6 A, a1 T: ?) @' y! u
  6. 10.0
    ) J8 {2 [: V( T- O" J
  7. >>> math.fsum((1,2,3,4))
    4 S. F4 f* c* l! P2 [
  8. 10.0( p9 B4 `9 h0 V7 ~' r
  9. >>> math.fsum((-1,-2,-3,-4)); c* R, q( Y: }% M( y, Z
  10. -10.03 R7 k; ?+ i  V2 A
  11. >>> math.fsum([-1,-2,-3,-4])8 R+ L$ T: ]+ t+ l4 f% Q: H) ?2 w
  12. -10.0
复制代码

) q" k9 b3 U) M; jmath.gcd(x,y)  返回x和y的最大公约数
4 G- B7 h( S7 ?1 W
  1. #返回x和y的最大公约数* y8 u( Q9 x! f6 m
  2. gcd(x, y) -> int# {& P( Q: T/ R$ J  ~
  3. greatest common divisor of x and y9 a3 t5 C* H3 p
  4. >>> math.gcd(8,6)3 ~* R0 i) g9 ?
  5. 29 q, h8 E; {; i  i6 ?+ q
  6. >>> math.gcd(40,20)
    , ~) D6 l+ C' \7 s2 p! Q
  7. 20
    % u" F8 w* X  W
  8. >>> math.gcd(8,12)
    3 R* _  [1 D& E/ u- k, x  M
  9. 4
复制代码
1 g! j, O5 C- E3 y! ^& [
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
, H" k$ s( Y7 ]( t( P8 D; q8 M
  1. #得到(x**2+y**2),平方的值" p: u& B+ L; {) ^7 V) K
  2. hypot(x, y)2 ?3 A0 T1 D1 ^. a% S
  3. Return the Euclidean distance, sqrt(x*x + y*y).+ {2 @/ T2 \7 T
  4. >>> math.hypot(3,4)& v8 V5 a, _  b; y$ M
  5. 5.0# A$ q2 o) R& m6 f3 w! a
  6. >>> math.hypot(6,8)  ^" X9 M, q$ i) F; j
  7. 10.0
复制代码

0 y; }$ B7 O$ @math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False, U9 |, r( C5 ?# k9 @
  1. #如果x是不是无穷大的数字,则返回True,否则返回False) e1 t3 p3 ~/ b, C+ l
  2. isfinite(x) -> bool
    4 R+ T# s& f  Z: W* P+ q
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    + _% r5 {* Z* t7 j; Z' o' V
  4. >>> math.isfinite(100)! v" g$ p* ]: |! o: f: ^6 v+ w: y* C/ o
  5. True$ z1 k. s( c2 Q+ Y# S; ^, S
  6. >>> math.isfinite(0)" J& V; @. j( u, ^: U) f- a
  7. True
    ) _! G0 m, @, s/ t
  8. >>> math.isfinite(0.1)
    & C' D) W# U% r+ d' `4 A, t
  9. True# ]3 v7 P$ ~& ~2 V% P
  10. >>> math.isfinite("a")% [* l" F  g8 N- v+ s
  11. >>> math.isfinite(0.0001)! w: e. ~% W) |. q. `
  12. True
复制代码
: g! X/ h" ?/ c/ G4 L- G7 U& ?
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
9 @  Q3 w' y: |# ?0 I! h
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False" n8 m  D: L( E* ?2 n: S0 s
  2. isinf(x) -> bool
    ' J& U' B  P5 M! ^$ v% z7 W5 Z
  3. Return True if x is a positive or negative infinity, and False otherwise.1 y- \- K& \, P. \4 \
  4. >>> math.isinf(234)% D: l) V6 x5 D
  5. False0 \# Y/ e# f0 m' _( j
  6. >>> math.isinf(0.1)1 e: _3 @: ^7 n# `8 S
  7. False
复制代码

  ^  y. `' q- R2 D4 }math.isnan(x)  如果x不是数字True,否则返回False
) t. M* d: L2 T1 S( t) R
  1. #如果x不是数字True,否则返回False8 }0 t( a: W; H+ P  h
  2. isnan(x) -> bool
    0 @3 l+ J& ?, e6 z) U
  3. Return True if x is a NaN (not a number), and False otherwise.: s1 G% w9 c2 G  g# m* u
  4. >>> math.isnan(23)
    * s, }7 ?+ P- V+ T6 r: l5 u
  5. False
    # U4 @( [# [! i) d* S9 M- o
  6. >>> math.isnan(0.01)
    2 G- `3 L, i/ M! x' A8 k$ U
  7. False
复制代码

  H5 F& W2 R  V4 z9 M- Z2 Lmath.ldexp(x,i)  返回x*(2**i)的值
' G2 E4 W; V# a$ V0 p6 U1 `
  1. #返回x*(2**i)的值* \4 _7 n3 D8 C& q% E6 M
  2. ldexp(x, i)# {3 S1 _7 @$ g5 J2 j3 Y; Q% G
  3. Return x * (2**i).5 U) r" m! Q3 x5 o* }
  4. >>> math.ldexp(5,5)4 e! Z; k6 ^  u: {, O3 y
  5. 160.0) R# F% [/ C, l8 C4 C
  6. >>> math.ldexp(3,5)! Y( R$ c% u1 Z/ a4 c' q2 i: Y
  7. 96.0
复制代码
: {0 w: q3 i0 I  `3 N1 u
math.log10(x)  返回x的以10为底的对数' R$ q) Z1 ^8 \
  1. #返回x的以10为底的对数; R: M& s) S& Z* Q" a) J2 Z
  2. log10(x)# S( p1 n; o! w! L7 ^# f  x3 m' Q6 n
  3. Return the base 10 logarithm of x." t, e1 T% b6 }. Q0 u+ s
  4. >>> math.log10(10)7 ^  r& o& f0 s  }
  5. 1.0
    ) \6 d. z) A) `  G3 V/ `" x3 n: {
  6. >>> math.log10(100)
    . ]/ o+ D' K$ a' q' l) ^2 m
  7. 2.0
    / P$ w  K' ]- D
  8. #即10的1.3次方的结果为20& O1 s* V( ?6 q: ^
  9. >>> math.log10(20)$ l: M- L8 f5 ?& U/ d/ J; g
  10. 1.3010299956639813
复制代码

. L; z# x- K$ f2 _0 e3 {2 t; n/ Bmath.log1p(x)  返回x+1的自然对数(基数为e)的值+ c. M/ H1 ]$ o( G8 O
  1. #返回x+1的自然对数(基数为e)的值
    4 l5 W! n, a0 m2 K2 M7 Z% n2 x
  2. log1p(x)" e& L! J0 ^! w0 t" g" q
  3. Return the natural logarithm of 1+x (base e).
    8 Y2 D; E) m2 Q4 Q" j) k! Y, T
  4. The result is computed in a way which is accurate for x near zero./ D  Z' m/ n5 [% J1 i6 T
  5. >>> math.log(10)4 ~6 b( G5 _6 l! E
  6. 2.302585092994046
    " w- |4 W, U% z6 t/ ~9 x
  7. >>> math.log1p(10)
    : m  o8 N, O0 O
  8. 2.3978952727983707
    - a4 c* K4 a; a3 M4 J1 a
  9. >>> math.log(11)
    4 U' Z. p" Y% o! w. y' U
  10. 2.3978952727983707
复制代码
$ ]) o  z/ t& h
math.log2(x)  返回x的基2对数" S6 I; L- O, m* S! P+ H0 x
  1. #返回x的基2对数
    % L  v. I' X- Q$ O$ X0 s( \9 G
  2. log2(x)
    , W- e; e* S) P! k
  3. Return the base 2 logarithm of x.
    ) Q6 f, A# m7 _! j. \+ S: E% S
  4. >>> math.log2(32)) T+ s" l$ G7 D9 p% W
  5. 5.0
    " Q9 D. N/ \# W1 s! M
  6. >>> math.log2(20)# H3 g* y3 Q* K: n* w
  7. 4.3219280948873632 |  P$ A+ I5 _5 _( i
  8. >>> math.log2(16)
    ) I* C- t4 u: l  v" \
  9. 4.0
复制代码

/ I6 v3 T: u) Z/ n: pmath.modf(x)  返回由x的小数部分和整数部分组成的元组
# b& C# F# \) X4 v
  1. #返回由x的小数部分和整数部分组成的元组
    1 n& G  [) r! H& D9 J
  2. modf(x)% E. k" y' i+ i! Q2 B& s. m5 @; C. _
  3. Return the fractional and integer parts of x.  Both results carry the sign6 d3 V! S. s, {
  4. of x and are floats.
    , O9 J" [! |6 b3 G7 ?- V; m
  5. >>> math.modf(math.pi); \7 ~' y& E4 |6 m/ F7 C
  6. (0.14159265358979312, 3.0)% j- Y6 w7 T+ {$ S
  7. >>> math.modf(12.34)  j1 i4 c2 S, E7 j5 v# \" }
  8. (0.33999999999999986, 12.0)
复制代码

% O# w8 H8 D0 B3 [4 F3 \- u  x6 U( ^- Wmath.sqrt(x)  求x的平方根
$ c5 e. Y" A7 l6 B) |
  1. #求x的平方根! f7 b8 _0 ?; ?5 t2 V+ j
  2. sqrt(x)
    ( w: U- Q% y% Q8 r8 i0 H! y' P
  3. Return the square root of x.7 p* Z+ C5 r2 z4 c9 Z& `$ q' U
  4. >>> math.sqrt(100)
    1 P, x. Q1 }/ p! R; `
  5. 10.05 ?3 \. O4 O0 ~- X8 g2 l
  6. >>> math.sqrt(16)+ H7 c& F1 W4 F( C( t( Y* a) }
  7. 4.0
    8 a3 b/ O5 b" B6 X3 Q: \
  8. >>> math.sqrt(20)
      e6 t+ L$ i5 M% q
  9. 4.47213595499958
复制代码
& ^) X3 H- Q2 P. @. d! o1 O8 `
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    + q2 X" [4 y  R. r+ u
  2. trunc(x:Real) -> Integral
    % j7 w/ `! y9 u: u. n8 Q
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    3 K  r4 Q  F) [* j3 h
  4. >>> math.trunc(6.789)" m  B  C% o' x3 E* T8 d: @
  5. 6
    + K) }# E) z+ W, Z( l& Q( E. {
  6. >>> math.trunc(math.pi)
    * g2 `6 g6 R6 h* {, |
  7. 3
    * ]6 X5 J' ]  c& T/ J
  8. >>> math.trunc(2.567)
    * X9 `+ A0 [8 n6 h
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-4 06:34 , Processed in 0.074720 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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