新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
8 A5 o  N- J+ q$ g# e+ X' O  u1 ?% d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。  C3 b$ i. \& j# Q( I: C/ y

8 h) `7 m/ w: g0 D方法10 R3 R& P* K& {6 X
  1. >>> import math
    3 ]1 T9 N5 C; k* e  y
  2. >>> math.sqrt(9)
    % M2 C6 Z: y$ R% `0 B
  3. 3.0
复制代码
方法2- t! X) L' I  J/ [! ?: @
  1. >>> from math import sqrt
    * I9 y; |" C* B1 D. ^, g$ T: c
  2. >>> sqrt(9)
    + x9 A6 b5 i/ T! Q
  3. 3.0
复制代码
( o1 O: v* H% L, y% O% k( G$ p& O6 r

2 O! ?1 P6 ]- l( u$ A( y% i
math.e  表示一个常量$ L1 M$ S9 B, w- }& ?+ Y9 d
  1. #表示一个常量
    6 H. V2 O% W- C; l& s
  2. >>> math.e! G" }: u! L3 D. I4 w& m$ J( E
  3. 2.718281828459045
复制代码

7 z5 z+ k7 C7 a: Tmath.pi  
数字常量,圆周率

5 J! }# ~; m+ i/ z2 p3 w: I% W
  1. #数字常量,圆周率
    3 p) U: Y9 q  ^9 U5 S
  2. >>> print(math.pi)
    & l( q' R9 z' t* |8 R& N
  3. 3.141592653589793
复制代码
& V6 ~0 n) f. V' Z) V% A  L9 s/ S
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 ]! Q  V8 U+ |; Q( h" Z$ F
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x- y+ Q* {" E3 g* Z- Y" k
  2. ceil(x)
      Z2 D# h7 U3 K' b5 v+ P3 J
  3. Return the ceiling of x as an int.) ~0 W  {1 B1 b
  4. This is the smallest integral value >= x.
    ( h" M( G- z& B2 ]$ }% S
  5. ) f5 ~3 r- ~: [! e
  6. >>> math.ceil(4.01)
    % k; X7 y0 Y4 h" ^
  7. 5: H. T7 b5 `8 e
  8. >>> math.ceil(4.99)" Z7 i6 I; ]- K" j
  9. 5
    3 B) X$ N" ]% F! K
  10. >>> math.ceil(-3.99)
    : [% s% s5 a* a( A2 ^2 ^! _6 m
  11. -3+ D. Q1 @' r% }: J
  12. >>> math.ceil(-3.01)8 o- B; a4 N8 a- z
  13. -3
复制代码

2 L5 e% i5 w* G0 o" @" B# B* lmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 t$ T, H0 D) b
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ; p) a* ]4 e4 R' E  u' u7 u6 v
  2. floor(x)9 w: w$ z9 d* y5 ^) N" b7 g' ^/ n" x- U0 o
  3. Return the floor of x as an int.
    . s, X2 B7 d7 v' y) P* b
  4. This is the largest integral value <= x.
    0 B2 u2 _' @8 z
  5. >>> math.floor(4.1)
    9 ?( T$ V8 P" W' X3 R
  6. 4
    / M" q/ I0 E: N2 G8 j0 U
  7. >>> math.floor(4.999)
    ) E: ~( v, p; D/ V5 ^6 T
  8. 4
    ! d9 s, C; ^; b  C6 R5 o: N- P; f
  9. >>> math.floor(-4.999)7 C/ u( \% V" d2 Y" I
  10. -5# C1 A0 r5 R# W
  11. >>> math.floor(-4.01)3 p& g3 O4 p! w8 H  f7 d
  12. -5
复制代码

7 F' k+ y+ e1 b% N, O% U9 gmath.pow(x,y)  返回x的y次方,即x**y6 V% B: {: [! ^2 J  C: A4 H( O- x
  1. #返回x的y次方,即x**y
    4 w) J- ~! s5 T) P
  2. pow(x, y)' i* v. e2 k$ V  w! K) |
  3. Return x**y (x to the power of y).
    8 j' O' K; f! {) W, ^; |
  4. >>> math.pow(3,4)
    * @; z7 G/ R* g0 G8 S; a7 L
  5. 81.00 l7 [* N( Q' Z0 i: b, w! F
  6. >>>
    ; p# l  f/ \1 \& p
  7. >>> math.pow(2,7)
    : H" N1 G: _* Q; d
  8. 128.0
复制代码
0 v/ Y+ @; X9 f, P
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' m( b, N* \5 z0 z% ^5 G
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    $ [% u/ l2 V( X& V+ g; F
  2. log(x[, base])& i# H4 t& v  v/ `8 r
  3. Return the logarithm of x to the given base.
    . I4 i, A3 `) n. T  |
  4. If the base not specified, returns the natural logarithm (base e) of x.
      u3 A  F5 e# x8 P2 y. r9 P4 n
  5. >>> math.log(10)
    - z" s, n' E  G) s, J
  6. 2.302585092994046
    - _$ V+ O  }9 S- B2 X5 h
  7. >>> math.log(11)- ^- M& H; D2 V: X" e4 O! v
  8. 2.3978952727983707
    5 S: ~# Z* N6 U7 n# ]" y
  9. >>> math.log(20)7 K8 Y( S+ X- h& i/ b' X
  10. 2.995732273553991
复制代码
0 y: j' X, v% D+ h6 _
math.sin(x)  求x(x为弧度)的正弦值, d" X! z# J0 a: r5 h) t/ U
  1. #求x(x为弧度)的正弦值7 o- \4 b0 g2 b( b5 z* h& p7 G4 k9 M3 h
  2. sin(x)
      g+ m. t# ^: |1 E) S* y
  3. Return the sine of x (measured in radians).  Y+ @( {/ l% a9 V
  4. >>> math.sin(math.pi/4)
    9 I4 B; }! Z' g2 o, F( A9 U
  5. 0.7071067811865475+ I+ l- R; X1 @* {  n4 I) f
  6. >>> math.sin(math.pi/2)
    ( z+ ~' V. O" [* _/ T
  7. 1.0# ^' O, t/ P# q& m- F
  8. >>> math.sin(math.pi/3)
    ( k* e9 c: g3 Y: c$ ]: }- {" h
  9. 0.8660254037844386
复制代码

# G% ]& X+ c: K4 s0 V; ^1 |math.cos(x)  求x的余弦,x必须是弧度+ B  C8 H) d: v( \1 k
  1. #求x的余弦,x必须是弧度' \( J, W% X& S  _
  2. cos(x)* b# z' q: v" z/ _/ a
  3. Return the cosine of x (measured in radians).
    1 F4 H+ v6 u7 F
  4. #math.pi/4表示弧度,转换成角度为45度
    " g: r0 s/ F1 \2 p: g+ N" X
  5. >>> math.cos(math.pi/4)
    . ?# z* `! G& Y: c7 I- u
  6. 0.70710678118654766 r7 Z" B- n9 d3 Y
  7. math.pi/3表示弧度,转换成角度为60度
    , L' M2 G' i' C/ y+ v7 {. K, A% {  P
  8. >>> math.cos(math.pi/3)
    0 e1 h" L5 t0 D% P/ i1 M  L
  9. 0.5000000000000001
    % w/ C$ i1 D+ ]& l: X
  10. math.pi/6表示弧度,转换成角度为30度8 C6 l8 F2 E% K" B  k/ A
  11. >>> math.cos(math.pi/6)
    1 z0 n0 ^7 K+ ~
  12. 0.8660254037844387
复制代码
/ X  ^% c4 b& g" V. k
math.tan(x)  返回x(x为弧度)的正切值2 k' P% W8 l% @- y% M5 f
  1. #返回x(x为弧度)的正切值; |# t7 @/ s) i" Y
  2. tan(x)
    " Z; S. s2 K# o7 D% V+ S, `
  3. Return the tangent of x (measured in radians).7 U2 h, [9 J  A7 x' J  a
  4. >>> math.tan(math.pi/4); O5 J" Z. E% u" i8 S$ ^$ F
  5. 0.9999999999999999
    # t3 z8 m, N+ S- h: r
  6. >>> math.tan(math.pi/6)' p4 d7 R* W  n5 R8 o
  7. 0.5773502691896257: ]- Z0 B! N4 p/ e6 a
  8. >>> math.tan(math.pi/3)
    % ^. o: o. t4 P& m: B6 M5 X! e
  9. 1.7320508075688767
复制代码

$ G; w$ K8 a/ B2 g0 Y9 Kmath.degrees(x)  把x从弧度转换成角度$ C! E1 F/ C0 ?+ r- q  i
  1. #把x从弧度转换成角度
    , k% k9 Y' M! w* p4 g
  2. degrees(x)
    8 B9 [, f* i8 T0 w0 ~
  3. Convert angle x from radians to degrees.
    . t- M* A4 G8 j1 t7 F9 \
  4. , p& X4 ]; K5 ?- d3 {
  5. >>> math.degrees(math.pi/4)
    2 I2 ]' e# _! N5 t5 m  X/ d# r
  6. 45.00 ], N: ~) _5 {. e+ W. ~5 ^8 o
  7. >>> math.degrees(math.pi)1 ~, D( n( D' R: [
  8. 180.0
    ) S0 z) z; f7 y4 R7 K; }9 d8 [
  9. >>> math.degrees(math.pi/6)
    3 S! b- t0 `$ x, d
  10. 29.999999999999996
    + R9 H! }8 w* Q$ c2 d
  11. >>> math.degrees(math.pi/3)0 O% E( {' V  A
  12. 59.99999999999999
复制代码

" A, x# G* ~/ n+ H/ `3 qmath.radians(x)  把角度x转换成弧度
5 |8 z+ k; i# ]! D
  1. #把角度x转换成弧度0 t+ u- _7 I& o' m. ~0 S0 b
  2. radians(x)! R) v) }. k3 ^. Y/ |
  3. Convert angle x from degrees to radians./ g' z+ V( F6 y8 p' R, ^1 @
  4. >>> math.radians(45)
    8 b7 f5 t, u4 U, n; d
  5. 0.7853981633974483
    $ F! L; l1 }/ w
  6. >>> math.radians(60)
    3 @( E( y1 V4 @' Y/ Z0 T
  7. 1.0471975511965976
复制代码

6 ~. X, n5 d) e# I  Kmath.copysign(x,y)  把y的正负号加到x前面,可以使用0( X$ c) j  w5 K" A% F" e+ G) A
  1. #把y的正负号加到x前面,可以使用0
    ; \! X& o1 G% q
  2. copysign(x, y)
    2 V# s  O" P/ @% a7 ?) U
  3. Return a float with the magnitude (absolute value) of x but the sign ; ?6 ^8 H/ a2 y% T
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 8 K0 s  ?8 q- [+ X; D
  5. returns -1.0.
    # A9 m# z' e1 c5 y8 r

  6. 5 |- ^4 O0 ]" |2 C9 D8 r
  7. >>> math.copysign(2,3)
    6 t# D3 P7 ~% E, ^  ~
  8. 2.03 d( ?7 I* o+ u/ u* Z" o7 r( o
  9. >>> math.copysign(2,-3)9 v) M1 W6 k7 ^, a. N
  10. -2.0* T+ Q* g; g' E9 @8 p4 b
  11. >>> math.copysign(3,8)+ T) I1 L* M6 C& e! p" b
  12. 3.0
      @8 A* P  j/ c" I/ e: g  e
  13. >>> math.copysign(3,-8)
    2 s' r8 O! K4 W; D9 C7 m
  14. -3.0
复制代码
$ q1 g! i0 I& W
math.exp(x)  返回math.e,也就是2.71828的x次方/ u; [( w4 e8 z. }9 r( h
  1. #返回math.e,也就是2.71828的x次方
    7 |2 t% I3 B+ B* h
  2. exp(x)# [% O, R' y5 a# m* y: k3 W
  3. Return e raised to the power of x.
    0 S5 ]/ H* i6 A" N# e9 o
  4. 4 f- ^$ v$ F3 @
  5. >>> math.exp(1)( u% ~$ V" }& {9 ^
  6. 2.718281828459045
    % W4 Y8 c5 ~6 s+ |& Z7 I: O
  7. >>> math.exp(2)
    ( L0 }* _% F4 F3 ~
  8. 7.38905609893065
    0 Q" }. K5 ]7 W) E
  9. >>> math.exp(3)/ _& x( s+ {8 o+ {4 y) t
  10. 20.085536923187668
复制代码
8 C1 f( r( b8 o' u- K/ O( K5 e: l; A; t
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1% l& F1 q  d  A  n, Q3 U( N
  1. #返回math.e的x(其值为2.71828)次方的值减1/ w! M+ Q. B1 R6 ^$ [& q
  2. expm1(x); D+ @) t& B" ]! D
  3. Return exp(x)-1.
    # f9 }1 k# Q0 D0 m5 [# }1 T
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ' A# _% {7 X8 B$ U' F+ }
  5. - H: b- |; k  b3 P4 A9 m9 |9 c: [
  6. >>> math.expm1(1)
    3 r) W5 f& R, u- ^6 t4 A$ |
  7. 1.718281828459045
    6 a$ z6 c6 A% Z; _+ [8 }
  8. >>> math.expm1(2)
    9 D1 q/ d; r$ g8 f  o
  9. 6.38905609893065
    , ^1 U% n8 q% f5 a5 J) t
  10. >>> math.expm1(3)! g/ I  r. |0 b, Z
  11. 19.085536923187668
复制代码

6 A+ w# x! ~# Y5 W0 Z) Ymath.fabs(x)  返回x的绝对值% s7 J! `, o% i, K/ M
  1. #返回x的绝对值
    ) a, P6 O$ A3 S0 R% R  o
  2. fabs(x)
    : m; V' u* C2 ~
  3. Return the absolute value of the float x.
    * Z  M& N1 K) ~; o5 w
  4. ! T( ~6 f# o* [7 c7 d1 D
  5. >>> math.fabs(-0.003)' H& [( G: V6 _; k& t( r
  6. 0.003
    7 @$ @$ J" \  C, _0 h
  7. >>> math.fabs(-110)( _( q2 Y* l. @- N' ?* K0 f, ]
  8. 110.00 a8 e* p% U) f* a; m9 n/ ?6 R
  9. >>> math.fabs(100)
    $ l4 q5 q& N/ ~, ~2 Q
  10. 100.0
复制代码
( W: \4 i3 P7 h/ m
math.factorial(x)  取x的阶乘的值6 U2 F. N- L$ s8 d0 D& U
  1. #取x的阶乘的值
    - \+ \# a2 D* k3 X. s
  2. factorial(x) -> Integral
    / b& t3 d0 F. E) U* ~0 v
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ! t. O# F9 s4 l3 I& u2 ?* r
  4. >>> math.factorial(1)
    ( q8 s( V6 [3 q" T& Y
  5. 18 N1 r1 c, U% P7 b0 H2 n* T
  6. >>> math.factorial(2)( S" {( c% t2 \, c$ J) v' U' q
  7. 2
    3 R4 ?1 w/ H. O$ `2 y! h, A" O- J9 m
  8. >>> math.factorial(3)' B% J0 G: b' B* }- a8 L% I
  9. 6
    6 u2 K+ j" y% G+ F9 w! r8 {
  10. >>> math.factorial(5)
    3 a$ f! X2 @7 S  i/ Y4 Y
  11. 120+ @: B; Y/ `7 u7 K* \0 L. a2 \
  12. >>> math.factorial(10)
    ) c6 i2 O7 E- l* _
  13. 3628800
复制代码

2 J3 L; W% F. O  |/ b: b/ {math.fmod(x,y)  得到x/y的余数,其值是一个浮点数, L& K8 N1 M& S- Z
  1. #得到x/y的余数,其值是一个浮点数
    4 D* c$ H8 M' B+ {+ o7 a
  2. fmod(x, y). O! }6 I  R0 C# s8 r! y+ v1 {7 O
  3. Return fmod(x, y), according to platform C.  x % y may differ.7 u, }! G1 L" A- ^4 O
  4. >>> math.fmod(20,3)
    - H4 ?# v0 `' O9 G! A
  5. 2.0
    6 p) z( I* B! D
  6. >>> math.fmod(20,7)
    5 \1 T3 h$ j: C  o6 F0 ^
  7. 6.0
复制代码
. B, h( P$ R3 R2 H1 `* h7 t/ s! v
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 P7 A6 b5 O1 \$ ~; d
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ) |1 r! X- o  a# }7 j' `4 n
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    1 `8 L7 d3 A: w
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 a1 m+ c' z' K: m$ A7 T- t
  4. frexp(x)
    8 H- v( x+ M) p
  5. Return the mantissa and exponent of x, as pair (m, e).0 J  V7 K/ |8 n
  6. m is a float and e is an int, such that x = m * 2.**e./ v% l6 W9 j7 A
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    % r8 T% u! b7 S) t% ?9 ^; K' N6 {) f
  8. >>> math.frexp(10)
    ; b* p% C/ t! n# r/ p; \
  9. (0.625, 4)" j. Q0 D( x6 W! a" ^( t) F; E
  10. >>> math.frexp(75). `+ U! C- M" l" I% w; ^' x: E
  11. (0.5859375, 7)
    ; z  [& m2 K, Z2 V/ k/ S+ {# ]
  12. >>> math.frexp(-40)
    * w( h( ?: E; ~; s
  13. (-0.625, 6)( G7 I0 R/ [1 R( z) x5 e" e* |
  14. >>> math.frexp(-100)$ X: j- ?. K7 _. V
  15. (-0.78125, 7)
    6 L% f; t) ^" {9 v
  16. >>> math.frexp(100)6 @0 v* q) c: V  q
  17. (0.78125, 7)
复制代码
0 }! Z) `/ |) `1 i+ V. G
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列% b4 X5 o8 [. D
  1. #对迭代器里的每个元素进行求和操作4 o4 `& k' F8 O* L9 \3 I& U8 _
  2. fsum(iterable)' ]4 o. N: Y8 |1 ~! J
  3. Return an accurate floating point sum of values in the iterable.7 q1 s- F( P$ o. a1 `7 d! w; c
  4. Assumes IEEE-754 floating point arithmetic.
    ! }7 j8 H* z5 n6 v. X+ `0 S( s1 a
  5. >>> math.fsum([1,2,3,4]): v/ ]* G! N9 Q. J2 M
  6. 10.0
    ' s0 |4 d3 Y' K1 Z" j$ l; b5 R! A
  7. >>> math.fsum((1,2,3,4))
    2 a) Y3 K# K: e) X
  8. 10.0, q. J( t8 y; O4 v
  9. >>> math.fsum((-1,-2,-3,-4))
    + u" H: u2 O6 t. l. l9 }; w
  10. -10.05 c: @, h! ~- C5 y0 `3 z
  11. >>> math.fsum([-1,-2,-3,-4])
    % H( e% Z3 u) f3 b; ^: S. `" D. t5 `: D
  12. -10.0
复制代码
% r7 e, `. G5 E  [0 h! c
math.gcd(x,y)  返回x和y的最大公约数0 K2 N) `) B) C8 z
  1. #返回x和y的最大公约数
    ) x$ o: G$ j2 b& q, t
  2. gcd(x, y) -> int$ z7 ?9 ~5 D  S9 e# p9 |( _5 X
  3. greatest common divisor of x and y
    ' L" E% T. P0 U  q# C! ^( U5 \
  4. >>> math.gcd(8,6). o2 c- [9 H. j8 w1 l# W
  5. 2
    ; k) j) |0 \: T; K
  6. >>> math.gcd(40,20)
    % J9 `' w, b3 ?: j# i( Y
  7. 20' h; ~' b5 P+ T7 Q& Q
  8. >>> math.gcd(8,12)/ V. p7 W, [, I; r  c
  9. 4
复制代码

) ]" R' D; j3 F5 X* k4 kmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
3 C# I. r, C/ ?0 w' {$ }. d
  1. #得到(x**2+y**2),平方的值# x, I/ t( E" r" B- G2 E
  2. hypot(x, y)
    ; X0 Y, o5 _, `
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    % y& V) f+ K# L; A& D5 u, J
  4. >>> math.hypot(3,4)' R8 }2 L* @8 F% Q7 H
  5. 5.0; A% _* t) a" R) j5 ~- y
  6. >>> math.hypot(6,8)) D: f0 W$ f+ R$ u  B9 m* Y  n
  7. 10.0
复制代码
# E) i5 v# t1 F: R+ c
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 f/ y! G& C8 {7 u; d5 e. k
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    - X* A+ W' V( d: Y! @
  2. isfinite(x) -> bool$ @/ _5 x0 _  P. a$ ]% z/ K
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    - w4 N4 C: h8 r2 m0 Z0 A3 f
  4. >>> math.isfinite(100)0 D  z! M" B5 m9 W
  5. True# a9 G1 W) F5 j
  6. >>> math.isfinite(0), G% [5 }. l, X. t
  7. True
    , N; {3 W# w: W6 Y/ B! P, z( |
  8. >>> math.isfinite(0.1)
    - Y( m3 h! w# O# X! ]/ k: u
  9. True" B( F+ ^" ]0 t9 A4 c5 x
  10. >>> math.isfinite("a")
    $ p8 J! C8 `9 }( ^1 i
  11. >>> math.isfinite(0.0001)
    % M+ d2 W. j, O) z7 d" A
  12. True
复制代码

0 O; b9 z# \! B- b2 ]# Omath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False: E3 b" b6 k( R& p: Z% V( {1 [
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False, @6 Z4 s# r+ V# Q9 C: z
  2. isinf(x) -> bool
    + U" W" C' v) i, i* g
  3. Return True if x is a positive or negative infinity, and False otherwise.# e5 `  P& @3 [% H
  4. >>> math.isinf(234)- K# X- E8 T: A9 T/ c6 {8 j
  5. False
    $ P/ G8 o- P9 f, I
  6. >>> math.isinf(0.1)
    $ _2 ?8 E) g& q
  7. False
复制代码
. f2 h; Q) A4 N: x/ j' c
math.isnan(x)  如果x不是数字True,否则返回False) H0 l; U& }# u8 H  Z& x
  1. #如果x不是数字True,否则返回False
    8 ?" D+ Q, S) q" Y3 u
  2. isnan(x) -> bool
    # S# G8 A1 p& f
  3. Return True if x is a NaN (not a number), and False otherwise./ R9 h! u: U0 A+ \
  4. >>> math.isnan(23)2 o# W; f, [$ M" g
  5. False
    9 C% _6 m0 @1 d# s! \
  6. >>> math.isnan(0.01)% @; X# P8 f  i4 W& T( K( _; T4 {
  7. False
复制代码

+ F  @5 q+ d2 F: W. y2 Tmath.ldexp(x,i)  返回x*(2**i)的值
  o2 z) o' t0 D/ _
  1. #返回x*(2**i)的值
    ; H$ Z2 b. i; w+ W9 K3 s
  2. ldexp(x, i)
    ) g- g/ {4 u* h6 j$ Z5 H/ I
  3. Return x * (2**i).) z) s( ^8 @, f0 K0 P
  4. >>> math.ldexp(5,5)8 H7 U6 Q3 P+ O0 w
  5. 160.0- Z3 j9 v8 z& |  E( B
  6. >>> math.ldexp(3,5)/ \4 f) ]* d( K6 Y+ u+ O
  7. 96.0
复制代码

; h9 S' x% w2 Z4 d! U( ymath.log10(x)  返回x的以10为底的对数6 p0 U- P/ K& [6 k% a
  1. #返回x的以10为底的对数
    4 Q; n4 |* r0 n& q
  2. log10(x)' r1 q9 G. a* N  x5 _
  3. Return the base 10 logarithm of x.1 a4 [4 N4 G( }2 b5 C9 ^' O
  4. >>> math.log10(10)
    0 a# X7 v4 D5 T
  5. 1.0( e7 d# C  _2 g. z* w
  6. >>> math.log10(100)  N$ X7 O# H) k' J
  7. 2.0
    ( F; M1 B: C! x4 L8 C6 T  d
  8. #即10的1.3次方的结果为20
    % [7 C2 K! L' C0 z  @
  9. >>> math.log10(20)1 w2 |2 J+ u% P" v/ J) ^' u
  10. 1.3010299956639813
复制代码

/ h0 ]0 M0 w, f! Y# S$ g; t- q; mmath.log1p(x)  返回x+1的自然对数(基数为e)的值
3 P- r# y( g2 _6 a: V) L' z
  1. #返回x+1的自然对数(基数为e)的值; e2 h5 a3 Y1 v5 W. I9 k+ y. ~
  2. log1p(x)
    ! E% J7 i5 x0 T$ \, T0 L
  3. Return the natural logarithm of 1+x (base e).
    # j3 _+ u$ I) |% r, y
  4. The result is computed in a way which is accurate for x near zero.9 p( p( F1 s4 ?$ t7 x, L( x
  5. >>> math.log(10)
    . x) P7 B2 Q# p) i, _; W
  6. 2.302585092994046+ s/ D: c3 }# |" o1 k
  7. >>> math.log1p(10)/ t( }. K* @) m3 B& R8 P" [, ]
  8. 2.39789527279837072 U1 N8 M$ E- B8 {5 b: |
  9. >>> math.log(11)
    4 \+ r6 e: v  D8 h
  10. 2.3978952727983707
复制代码

7 ]  e" B& i4 g8 n$ Hmath.log2(x)  返回x的基2对数
" p3 p7 E4 n9 |) y2 T5 \$ D# B  ~
  1. #返回x的基2对数
    1 T" O4 \. l. P/ i/ L- K
  2. log2(x)
    1 i' U8 P; D0 m& b. v; ^
  3. Return the base 2 logarithm of x.% V* y( j6 O! A2 @' i
  4. >>> math.log2(32)2 ^4 g) J+ D& \9 w
  5. 5.03 `% T* l0 w5 k  l8 g8 g1 q
  6. >>> math.log2(20)$ F! k; t+ m0 I4 Z
  7. 4.3219280948873637 ^% I6 s, A: f; _  N8 g; h. P9 \! p
  8. >>> math.log2(16)
    / D$ U- y  j$ m1 D( E% y4 K
  9. 4.0
复制代码

5 o; ~* [' @4 a* ~5 T( _% s# W9 cmath.modf(x)  返回由x的小数部分和整数部分组成的元组) d' q/ A- V! A$ k
  1. #返回由x的小数部分和整数部分组成的元组
    7 ?& f0 m, o( ]
  2. modf(x)) f: x2 y( W: Z2 p9 }
  3. Return the fractional and integer parts of x.  Both results carry the sign
    3 M7 A) f7 l9 O
  4. of x and are floats.
      x- a  D3 k6 ~7 X4 E3 F
  5. >>> math.modf(math.pi). o+ [; [+ U; f. E5 a
  6. (0.14159265358979312, 3.0)1 F6 h6 a# n- K6 Y+ |. @) S
  7. >>> math.modf(12.34)9 z2 Z! E5 X5 |# R% ~0 X, V+ H
  8. (0.33999999999999986, 12.0)
复制代码

2 a8 O1 Y8 v# k$ b7 v1 nmath.sqrt(x)  求x的平方根
+ Y/ l& h1 Z# e" _0 N1 p
  1. #求x的平方根: S% v+ s8 P! Y$ C+ s8 q- r9 e9 N
  2. sqrt(x)
    1 f* s1 {$ E* h8 S( }1 l1 b2 l3 R
  3. Return the square root of x.
    9 A3 U/ V+ J6 A; y6 m+ @
  4. >>> math.sqrt(100)- o& P0 Z* t4 D
  5. 10.0
    , q* d7 L$ H9 @: g
  6. >>> math.sqrt(16)6 H/ @8 q0 ~9 N4 @9 |# o
  7. 4.00 V, c6 i9 g8 Y- |& [1 n
  8. >>> math.sqrt(20)
    " P( x1 ~: i/ e
  9. 4.47213595499958
复制代码

1 K* G% W% _. S% V+ C8 |2 Y  wmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    # _- ?6 J; H7 g- v
  2. trunc(x:Real) -> Integral
    ! ~9 @/ C( s3 g% x( q/ r% y& o
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: B, D( O  v7 K* D1 B0 ~/ a
  4. >>> math.trunc(6.789)1 Z( y  U: x9 ], @6 V5 Y6 f
  5. 6' d$ R0 L2 F, B& |+ P4 q& F  x. x$ B+ _
  6. >>> math.trunc(math.pi)
    ) z4 V" j  r( S  k' {4 h
  7. 3' n; F' F% a$ l7 x
  8. >>> math.trunc(2.567)
    & Z2 l& y5 Z2 H: j
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-13 14:53 , Processed in 0.088537 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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