新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

  x5 w1 m7 ~' O! Y" U【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
4 [8 S) @2 ^0 p. K+ L

( i! S& [/ A) ^9 y" @' H3 o9 X. g方法1
4 l& [6 r  Q. u) x- c
  1. >>> import math
    0 B/ G7 Q( A! n7 T
  2. >>> math.sqrt(9)
    ; `! d. Z  E& E- X; ^' ?' a9 f
  3. 3.0
复制代码
方法2' X; a0 Y4 r. U( [' K( d( k9 v3 w, S
  1. >>> from math import sqrt! F. d! k2 t5 J- G; N- W0 S
  2. >>> sqrt(9)
    1 K: i% R  Y, v
  3. 3.0
复制代码
) ~  f4 t3 [8 r5 n  s


2 x* _$ I8 s* B. u) ~# ^# T0 Z
math.e  表示一个常量: g7 D- Z5 h0 \0 o9 J* N* d
  1. #表示一个常量6 D1 i$ ]+ _9 j9 o8 \$ t
  2. >>> math.e/ d( X1 O5 |+ _3 ]( U
  3. 2.718281828459045
复制代码
  o+ \* T5 e# z, L' l+ A* C
math.pi  
数字常量,圆周率

, Z* |  N) U. g
  1. #数字常量,圆周率
    # Y/ X" n! c* B/ S+ d4 T' X: l
  2. >>> print(math.pi)
    0 O* r+ X8 k5 t* u5 o
  3. 3.141592653589793
复制代码
: t3 e. e  Z3 |" F  a2 B& f$ [
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

% X$ E8 A; F2 w% `! u
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, b0 k  V9 W1 i# z4 d0 l( r2 I
  2. ceil(x)$ X3 c. l8 f' i# A' p* [
  3. Return the ceiling of x as an int.
    $ l8 ^  o2 D% _& t
  4. This is the smallest integral value >= x.
    # {+ G% g* m+ W1 ~
  5. 4 `' _. A" r" |# Z4 A: [
  6. >>> math.ceil(4.01)
    6 f- l* p  o7 f; w  J* P5 c" |9 n
  7. 5: s# X- x# n$ j
  8. >>> math.ceil(4.99)
    4 G2 {8 ]8 {* a% |0 \5 l
  9. 5, C/ T# h" e+ {, v/ i1 z
  10. >>> math.ceil(-3.99)  A' c$ R& P' _6 @- Z/ ~2 V. m
  11. -3
    ( V; h$ g% x- i
  12. >>> math.ceil(-3.01)9 ~* ^% \! A' t/ f. Q- t
  13. -3
复制代码

5 f( t9 @0 j2 P, dmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ N: P) x2 N6 e, ]
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    % M/ [. y- `3 A7 m! I( k6 Z
  2. floor(x)
    1 P8 @& G* I. H
  3. Return the floor of x as an int.
    8 X* ?, T0 ~7 a0 A6 ?
  4. This is the largest integral value <= x.
    ) q- a2 r( p/ v- O4 k8 ~% t- h( u
  5. >>> math.floor(4.1)
    , e' y! }) t& O5 k+ e
  6. 4
    4 Q" q2 W! O+ @: w  g+ a
  7. >>> math.floor(4.999)
    & J( f  Z: b! c7 E# \- v0 z
  8. 4  C/ O9 ?# [! O
  9. >>> math.floor(-4.999), X7 ]0 U+ {  @
  10. -5' p1 w8 c' j( Q
  11. >>> math.floor(-4.01)) |! n! ~; a9 m6 k' V2 i7 s
  12. -5
复制代码
4 z3 x& i7 v7 ?# E( P
math.pow(x,y)  返回x的y次方,即x**y) x" H$ j! c4 p: [8 Z" B. g1 l
  1. #返回x的y次方,即x**y
    ' i! O0 b& a0 N. H8 l
  2. pow(x, y)5 S0 ~3 e* N1 j/ }
  3. Return x**y (x to the power of y).0 x/ b. w! N8 m. H$ o
  4. >>> math.pow(3,4)
      z) @& i; ^! r' l7 ]( {: t
  5. 81.0
    4 C1 ?  U/ x  T1 n' r* I
  6. >>> ) X+ z7 e. L' @' x
  7. >>> math.pow(2,7)' I% y  }2 B$ i/ s' u% f7 b
  8. 128.0
复制代码

3 Q. X3 d$ V" F: B% ]6 g! Nmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: e& Y/ ~" ?8 }# ~/ G7 K
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! V3 Q# N  y) e! Q. H) d
  2. log(x[, base]): a- \9 M# A1 M
  3. Return the logarithm of x to the given base.
    : s7 f$ [+ D" C2 `* k% Q+ }* C( e" Q. J) n
  4. If the base not specified, returns the natural logarithm (base e) of x.
    3 R. F* y. a/ m) m" w! G
  5. >>> math.log(10): f) W4 o) C0 z" i. `7 X
  6. 2.302585092994046
    7 Q3 d6 k. B/ K! S
  7. >>> math.log(11), R/ y4 R$ p# C- S
  8. 2.3978952727983707- {, R/ L! r, \
  9. >>> math.log(20)
    0 ^, O* [% W8 a9 G
  10. 2.995732273553991
复制代码

1 e8 i7 ^( U* q' ]math.sin(x)  求x(x为弧度)的正弦值
+ I/ a9 z& A; Y, d1 v- v; ?
  1. #求x(x为弧度)的正弦值
    8 A" p: g7 V" W, D8 s; B, r
  2. sin(x)
    ' c; D/ _3 y" F5 g. T$ R+ x6 p4 D
  3. Return the sine of x (measured in radians).( w; D4 K/ l9 S2 a& @2 M( ~
  4. >>> math.sin(math.pi/4)
    * L5 }+ u1 a: O5 ~, z: \% O8 L
  5. 0.7071067811865475
    * A' d8 Y% }3 d& Z0 a% q2 {
  6. >>> math.sin(math.pi/2)
      k: m7 T) l0 z* R7 Y
  7. 1.0
      }+ I7 \0 ~9 o& A
  8. >>> math.sin(math.pi/3)* ~0 W  x! I7 e! [/ {
  9. 0.8660254037844386
复制代码

' E4 A5 M: c) vmath.cos(x)  求x的余弦,x必须是弧度1 E" b. t4 Q/ T: @
  1. #求x的余弦,x必须是弧度
    7 l: p) }$ T3 D4 g6 x# @
  2. cos(x)* N, g$ K' W1 l0 r+ T5 ]# d
  3. Return the cosine of x (measured in radians).
    6 E3 Q/ B/ C1 Y# h5 i
  4. #math.pi/4表示弧度,转换成角度为45度' I: O/ h- Y, Y
  5. >>> math.cos(math.pi/4)% ^7 h' y+ {8 Q# h% l" T
  6. 0.7071067811865476
    ! F" W6 \9 o& m: @) P; H: n$ F& C
  7. math.pi/3表示弧度,转换成角度为60度+ k7 ~7 y6 n# b' c
  8. >>> math.cos(math.pi/3)" _4 r8 d( ~9 x
  9. 0.5000000000000001$ w% X4 V, T8 t3 k% J
  10. math.pi/6表示弧度,转换成角度为30度4 t  c9 A- \$ S- ^/ L
  11. >>> math.cos(math.pi/6)# c/ J2 ~% c' z5 L! a- a$ W
  12. 0.8660254037844387
复制代码
3 P" D- H; H  [4 N. |. g
math.tan(x)  返回x(x为弧度)的正切值
4 `1 @, @! \4 F8 h' ]
  1. #返回x(x为弧度)的正切值
    - M* B" L5 g3 `- [& Q* E; L; F
  2. tan(x)
    3 j7 G( n8 `/ b
  3. Return the tangent of x (measured in radians).. c2 @0 m; L" K7 L" `
  4. >>> math.tan(math.pi/4)
    ' ~! r$ z/ i) T8 U% n7 |& N: D
  5. 0.9999999999999999
    + @5 r& n' \1 }$ O8 X/ r
  6. >>> math.tan(math.pi/6)& _& M% m; h/ a7 k% w
  7. 0.5773502691896257& }. `! ?3 G  E. M$ E# E( S
  8. >>> math.tan(math.pi/3)
    & e/ q4 b, Q! p4 s( T4 _) y/ ?8 p
  9. 1.7320508075688767
复制代码
0 U% _8 L) D0 Q$ i/ P' }$ p
math.degrees(x)  把x从弧度转换成角度! ~& v! G+ Q2 s6 r5 f; G
  1. #把x从弧度转换成角度# l* I, N% ~# x5 a- D+ z
  2. degrees(x)! r/ d# v6 z7 P# J" G  p
  3. Convert angle x from radians to degrees.
    ! ?) G/ L, G: K- s1 [6 R
  4. 1 o- z3 F% a! [' `2 Y) r
  5. >>> math.degrees(math.pi/4)/ K1 z* v6 K0 E  X
  6. 45.0
    ) V8 }5 e7 b. h6 l: ?; L3 a6 |
  7. >>> math.degrees(math.pi)
    8 d( @0 u" [; ]5 z' Q" Z: @
  8. 180.0
    " {: c6 w# f6 x" k. e
  9. >>> math.degrees(math.pi/6)6 B6 s& x) `0 k0 ~
  10. 29.999999999999996- _2 `- W1 v) ~6 [; Q
  11. >>> math.degrees(math.pi/3)
    # v6 R, T% r( B8 a9 }
  12. 59.99999999999999
复制代码

8 k8 ~2 w# d6 jmath.radians(x)  把角度x转换成弧度
" ]( ?7 ^' v4 I' G) X
  1. #把角度x转换成弧度/ s8 W- e- E8 v0 a! P$ i' A6 N
  2. radians(x)- a, b* n. H* a3 M) |6 B
  3. Convert angle x from degrees to radians.6 T: ?* M' t! q7 P. M; Z) P4 h4 P
  4. >>> math.radians(45)
    9 m" Y9 @; ^) r! j! P
  5. 0.78539816339744834 R  F" S" V9 g) n
  6. >>> math.radians(60)" g. L! h1 o; H# o, }+ P
  7. 1.0471975511965976
复制代码
9 ?) s3 {$ Q2 ~+ c- S$ i0 Y
math.copysign(x,y)  把y的正负号加到x前面,可以使用08 ]  B) c' H  \* @  w7 ~
  1. #把y的正负号加到x前面,可以使用0
    5 Z3 ^0 X0 K" T0 N. ]
  2. copysign(x, y)/ @6 X" I2 |! p! ]
  3. Return a float with the magnitude (absolute value) of x but the sign + a/ P# s# B- N: M
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ; Y% h- |. J2 l$ l6 B3 B
  5. returns -1.0.
    3 J* J7 Q+ W( f& k- o$ ]

  6. ) b, ~/ l3 v. e6 z( Y
  7. >>> math.copysign(2,3)
    9 Y  K8 L6 r1 _/ i
  8. 2.06 U3 m$ S3 g: K: S6 E( D/ o
  9. >>> math.copysign(2,-3). f/ v$ x; i& l% Q, W
  10. -2.0/ v- R/ T; U: I
  11. >>> math.copysign(3,8)6 ?" x6 o7 _7 E0 ]4 h3 q1 R
  12. 3.0* n; p! o! e+ W! d, d: b
  13. >>> math.copysign(3,-8)/ L& m2 I; H* l& Q4 i
  14. -3.0
复制代码
& [* w/ ~- `2 Q
math.exp(x)  返回math.e,也就是2.71828的x次方" u# {4 c" T) j1 D5 M8 a4 E
  1. #返回math.e,也就是2.71828的x次方: v8 g7 Y5 @9 B5 d) L  n
  2. exp(x)7 ?0 N$ A! Z- f- q4 @; H
  3. Return e raised to the power of x.& o: L0 T$ h3 |, R1 \) H

  4. : z/ Y! T8 y- K' o2 g7 ^8 g
  5. >>> math.exp(1)& B1 ^. w# d& {6 q8 P
  6. 2.718281828459045
    * r6 n; j7 S- p5 z2 X& \
  7. >>> math.exp(2)# J. r4 H9 h/ }
  8. 7.389056098930650 l# q# c- k+ }6 r5 B" Z
  9. >>> math.exp(3)
    # r0 @7 H" C* F& S& C7 b3 Y9 k" C
  10. 20.085536923187668
复制代码

0 {' K! ?" O: |% t, tmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
% q  }: M; D: Y# Q& @2 y. }
  1. #返回math.e的x(其值为2.71828)次方的值减15 c: r" `( N9 t: ?! Z1 E
  2. expm1(x)
    , S1 b3 G) A; ~" l
  3. Return exp(x)-1.
    6 |. u% ?* i! Q6 y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ; D2 i) @: ~8 r' V, q1 L
  5. 9 {3 U0 u* K7 _
  6. >>> math.expm1(1)
    2 s& a8 z% ]8 u# W! r9 Y3 u7 R
  7. 1.718281828459045% t6 R4 j2 l% c9 H$ Q& K" C
  8. >>> math.expm1(2)
    : Y( L! D& ]5 _- O" {% G
  9. 6.38905609893065
    , l4 x' k2 y$ ]" {! ?$ a: ^
  10. >>> math.expm1(3)
    % A6 i8 ]- ~5 d
  11. 19.085536923187668
复制代码

  H" g. U3 L1 U! @math.fabs(x)  返回x的绝对值
; R9 M% n- m8 K! U$ b& Y4 w
  1. #返回x的绝对值
    - F3 c  X3 q1 b
  2. fabs(x)3 s  t. A3 r5 u; J! J9 Q
  3. Return the absolute value of the float x.: o7 A1 Y) g+ [0 D) A
  4. 8 H/ ^. z! J( l
  5. >>> math.fabs(-0.003)6 O9 ?3 u5 b+ p- r+ W* w, }1 E
  6. 0.003
    + R3 H+ l' Y8 u3 A6 v& r, g
  7. >>> math.fabs(-110)
    9 E  t3 g: P$ P, \9 z6 z; c: B
  8. 110.0$ d2 z3 h) d# z
  9. >>> math.fabs(100)
    6 b- b- j$ x6 L; t
  10. 100.0
复制代码

1 o. b& v4 F0 N" h& Omath.factorial(x)  取x的阶乘的值5 I5 I) A! p: C' d( V
  1. #取x的阶乘的值/ h2 ~2 t" T* z, l
  2. factorial(x) -> Integral6 S" i6 ?7 I# }* r* ^# d: q: |
  3. Find x!. Raise a ValueError if x is negative or non-integral.
      \3 X; V; @1 z2 H9 d  D! l2 ?4 f
  4. >>> math.factorial(1)- p) q8 ?+ W4 ?; a
  5. 1
    * @, ?# u% Z1 ^( w" z0 M. z
  6. >>> math.factorial(2)
    5 `0 i' i& X2 r
  7. 2
    # D+ H4 A( n+ Q5 z5 j6 W$ _- S
  8. >>> math.factorial(3)
    + A) {/ u! w: f0 t6 K. ^3 ?9 J2 V
  9. 6( ~, d4 {2 X4 q" [/ ~
  10. >>> math.factorial(5)
    % O9 S/ Z. @# q$ |' _  O6 v
  11. 1205 B! i/ g  Q5 b2 r% V8 d7 j
  12. >>> math.factorial(10)
    9 Y  u: W, x7 u8 O
  13. 3628800
复制代码
; {5 {6 Q6 o6 V* r' D. o9 ?' c
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
4 Q" P0 o' O% w) {0 x
  1. #得到x/y的余数,其值是一个浮点数7 g- R" V+ [. \
  2. fmod(x, y)2 g9 z) i. v3 T1 e3 i' w
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ( j( o& V' Y3 _  a) D  S, E
  4. >>> math.fmod(20,3)
    ) \1 S! o$ J- N5 j8 Y. @
  5. 2.0
    + R  F5 x: @- c; s3 M, G2 K+ H
  6. >>> math.fmod(20,7)
    4 T: c" h/ Q) r; O8 k
  7. 6.0
复制代码
" ~% E+ ^5 x2 C4 n
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 x, L3 t1 }5 p
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    2 q7 Z5 r3 g: P; E( ?- ?" r: e
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值  S2 w# @5 N3 |: K- M8 C7 n# X- a
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 n& q) N& G9 j& G. n+ q
  4. frexp(x)
    % H) B9 w* I1 p. E
  5. Return the mantissa and exponent of x, as pair (m, e).! J: q' y4 W0 j* g. J. w- O3 o( h
  6. m is a float and e is an int, such that x = m * 2.**e.
    0 S0 E( s, @) }* ]  `
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    " H$ T  A( n( H4 E
  8. >>> math.frexp(10)
    4 D- r# R& K$ W1 _! |6 D; Z
  9. (0.625, 4). t  \$ z! |" w3 d0 E( P
  10. >>> math.frexp(75)
    8 V$ h% A8 Q0 T" l& G8 K- K8 u
  11. (0.5859375, 7)
    , t! d5 s( l6 D5 h
  12. >>> math.frexp(-40)( a: j3 |, h3 a: s; p; D- w! p
  13. (-0.625, 6)
    ; |. M2 \- l& a' l
  14. >>> math.frexp(-100)
    : p- j! z8 t. S6 ]# {5 _9 N
  15. (-0.78125, 7)
    & u* g4 |9 ?9 r- i1 @2 L/ H
  16. >>> math.frexp(100)3 [8 e& D1 N% ^' z
  17. (0.78125, 7)
复制代码
; n; q* {# ~/ K2 N. ]
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列0 W& F+ c, e7 ^; P
  1. #对迭代器里的每个元素进行求和操作
    $ X  f8 p  Z( R/ M
  2. fsum(iterable)
    * @& W# x. o' J$ X- P
  3. Return an accurate floating point sum of values in the iterable.
    3 S( q9 E, J# B( [- E% `
  4. Assumes IEEE-754 floating point arithmetic.
    ! ~2 q. @0 f( ^
  5. >>> math.fsum([1,2,3,4])
    2 n( e2 G1 R" N
  6. 10.0* M" }5 W/ z( ]
  7. >>> math.fsum((1,2,3,4))
    ; `, O, Y$ c7 _+ z, y2 k. w
  8. 10.0
    3 ]5 j) i) W8 r3 C  C: o- X' S
  9. >>> math.fsum((-1,-2,-3,-4))# \/ F: p8 L0 M* e8 _- E! B
  10. -10.0/ }4 v7 ^0 o3 K5 ?) S4 y4 f. X
  11. >>> math.fsum([-1,-2,-3,-4])& q# J4 m7 X4 U  f1 Q9 V% f
  12. -10.0
复制代码
9 _- ?! d: ~& ?$ G5 S) h
math.gcd(x,y)  返回x和y的最大公约数
9 Z6 P: J: ^8 r. |8 N. f& a
  1. #返回x和y的最大公约数" w3 ?* R% ^- }
  2. gcd(x, y) -> int  ~4 p0 z4 v) u7 _. W# d
  3. greatest common divisor of x and y( f5 z# ?" ~" t& ^" X& Q9 Q. H2 V
  4. >>> math.gcd(8,6)
    / ]* G8 P! C: [# v8 }4 D7 E
  5. 29 J, j6 l, _$ C7 P$ W- m& n5 J
  6. >>> math.gcd(40,20)
    & z( f8 e; e6 Y6 S7 Z' t
  7. 208 B  z- H* x9 D1 V8 |8 v
  8. >>> math.gcd(8,12)  p: N1 ?& k8 |( H* [
  9. 4
复制代码

6 c3 ?7 s5 h$ ?9 o1 w: cmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
+ {- g% R. j  X: Z
  1. #得到(x**2+y**2),平方的值  p- w$ J, t5 y! l7 A0 v
  2. hypot(x, y)
    % T5 c, q4 v& C" @, `9 K$ m
  3. Return the Euclidean distance, sqrt(x*x + y*y).  g( N" a9 g7 n
  4. >>> math.hypot(3,4)9 t" r% q3 e2 s' |
  5. 5.0
    , m( c5 ^1 Q) e/ Y$ R0 v8 G
  6. >>> math.hypot(6,8)7 G4 p8 u$ {1 r
  7. 10.0
复制代码
4 ]1 ?# w4 p* b7 q8 v
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False5 w0 U! ?( o8 y; W  s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    3 h7 i, h; ~3 g0 T
  2. isfinite(x) -> bool
    & Q5 k: g3 s1 ]: V0 y
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.$ d- ^" Y. M. n' ^
  4. >>> math.isfinite(100)
    4 j. |, `/ }  T$ {- c
  5. True/ m% v6 ^# l9 E0 W6 C8 i$ d  E3 z
  6. >>> math.isfinite(0)
    3 b  g/ R0 @/ ^" U: W0 R. D
  7. True
    5 R$ M4 K  t( w' y% \5 e9 X! e/ c
  8. >>> math.isfinite(0.1)
    ' [% u2 j8 O2 v  z% u8 r; X1 i
  9. True5 t2 y5 u3 H  {5 ~+ O
  10. >>> math.isfinite("a")) o# g* _" Q# A9 i
  11. >>> math.isfinite(0.0001)
    4 |& U3 ^! h: U3 ^# Z
  12. True
复制代码
8 q  [7 @  \, `
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
7 u3 C3 [$ ~3 k9 f$ [, r
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False& z) A; a) U" ^7 e4 ?
  2. isinf(x) -> bool
    4 E6 {8 `8 Y+ o
  3. Return True if x is a positive or negative infinity, and False otherwise.
    2 D& ~8 V! C% y* r/ d
  4. >>> math.isinf(234)
    ) i! M  C* s8 l7 K0 Q7 [
  5. False$ o0 P/ M' y: r2 X
  6. >>> math.isinf(0.1)" q/ [/ I5 n0 p& q" F
  7. False
复制代码
0 s$ J6 d/ q) b
math.isnan(x)  如果x不是数字True,否则返回False2 X: |; X3 J2 I
  1. #如果x不是数字True,否则返回False
    ! \" ~; o6 i4 k& f0 h: V! C
  2. isnan(x) -> bool; k! J" D+ P8 ?6 B6 z
  3. Return True if x is a NaN (not a number), and False otherwise.$ i! m( J( i+ c$ N
  4. >>> math.isnan(23)3 W6 m* S% o+ B/ W, [, j
  5. False
    7 u3 C  n' j& @  _2 G
  6. >>> math.isnan(0.01)
    9 h  B( J* l! d
  7. False
复制代码

- M9 o, R" E, N8 w' D4 r  V8 Y1 tmath.ldexp(x,i)  返回x*(2**i)的值
# V$ o! P0 [' X; `1 \
  1. #返回x*(2**i)的值5 m. P+ |8 }. \. R! Z+ p' Y
  2. ldexp(x, i); g. q+ h8 j2 f1 b
  3. Return x * (2**i).- {$ E, Z. \* w% J' D/ R
  4. >>> math.ldexp(5,5), Z2 K: z$ H  @) l  i
  5. 160.09 v& b- @( j8 v4 T
  6. >>> math.ldexp(3,5)5 x$ M! U, K4 z
  7. 96.0
复制代码

. O, A7 h* d6 C2 X2 ?math.log10(x)  返回x的以10为底的对数
; k# X6 W/ G% _) B. S
  1. #返回x的以10为底的对数
    " B: J/ F6 \! y" r7 i. w
  2. log10(x)
    3 a- n1 ]: H9 V# B3 m. k  G' E
  3. Return the base 10 logarithm of x.# r) t2 P5 v; A" U4 J
  4. >>> math.log10(10)
    / N- F  h3 h- p" K
  5. 1.0+ S! E/ z/ ?9 [7 ~. h
  6. >>> math.log10(100)
    ; t# d5 |6 L" q, H1 w2 `5 z
  7. 2.04 i) i1 b6 J) E' @/ U0 F
  8. #即10的1.3次方的结果为20
    7 H1 A: i9 m3 q8 m( ~6 Q
  9. >>> math.log10(20)" d5 Q$ k+ m1 R( V
  10. 1.3010299956639813
复制代码

7 w3 Y: m5 J+ @. b7 F( C9 ~4 A' }math.log1p(x)  返回x+1的自然对数(基数为e)的值0 ^/ N' R/ _; N0 C
  1. #返回x+1的自然对数(基数为e)的值) m! M( ^( `2 W! Q7 [# g* f& }
  2. log1p(x)6 ]6 g/ L, [1 z; h' e2 G5 y1 X& A
  3. Return the natural logarithm of 1+x (base e).
    + Q3 c! q4 m0 V. n, r
  4. The result is computed in a way which is accurate for x near zero.+ j) G5 G  @3 l1 J; D+ X) a
  5. >>> math.log(10)4 `* B6 G0 V' L+ ~/ h% O
  6. 2.302585092994046, l+ d; {5 z1 e
  7. >>> math.log1p(10): Y1 I2 U% V) z8 o
  8. 2.3978952727983707
    $ w1 ^$ y( V7 h
  9. >>> math.log(11)- ~& z0 ]5 _0 _, o$ `9 J
  10. 2.3978952727983707
复制代码
$ O9 r2 P1 @# E2 ?) q
math.log2(x)  返回x的基2对数
0 i# E8 i! ~) E7 i
  1. #返回x的基2对数
    , s) S* f4 }1 w$ L
  2. log2(x)
    6 P# ^  b8 ]: B) \6 V% m) Y7 M
  3. Return the base 2 logarithm of x.( a" `8 }, Q8 S4 C3 K& R4 N2 a
  4. >>> math.log2(32)9 D% C2 h& W) F8 {' O
  5. 5.0
    ( H8 I- ^9 E) s, @# f1 n. }5 Y/ p
  6. >>> math.log2(20)/ V+ N0 e- U! g' \" \
  7. 4.321928094887363
    7 M+ n! z; ^$ n1 N5 V
  8. >>> math.log2(16)
    0 Z) t# p4 n  X0 P0 k% r
  9. 4.0
复制代码
" K' ^6 j" I' G0 p9 O& A" \0 a. \3 L
math.modf(x)  返回由x的小数部分和整数部分组成的元组
$ l! n' u9 o: C8 W: O( |
  1. #返回由x的小数部分和整数部分组成的元组2 @+ v$ ]: Y8 W6 N
  2. modf(x)
    ( J& [2 u$ c+ F, ^; ~: P6 m9 B
  3. Return the fractional and integer parts of x.  Both results carry the sign
    % |* E! ^* c  `1 ], _
  4. of x and are floats.
    8 N4 {9 i% i2 ~" C
  5. >>> math.modf(math.pi)* k  j7 q  t9 p5 G) u
  6. (0.14159265358979312, 3.0), l0 m9 {$ O8 P8 l) f
  7. >>> math.modf(12.34)! P) P$ @) _( L% c9 q% J
  8. (0.33999999999999986, 12.0)
复制代码
: d. _  Y( v$ h% W9 A; }' T. T
math.sqrt(x)  求x的平方根
# l- V  b: I3 d; v
  1. #求x的平方根3 Z- H7 x. Q: L5 h
  2. sqrt(x)
    % S' W8 \& e1 y8 y' k
  3. Return the square root of x.
    " X$ C9 o* R' g; E8 v! X$ [
  4. >>> math.sqrt(100)0 r/ u7 l  [0 G' M. k8 c  L" T
  5. 10.0
    1 p" W: _: z2 h; u1 X) M8 L
  6. >>> math.sqrt(16)* n" ~- y# f* A7 D9 c0 V
  7. 4.0
    1 M: D4 o" M5 @4 R* r
  8. >>> math.sqrt(20)
    ( ^* ^% q8 f$ S7 w+ l; A' h
  9. 4.47213595499958
复制代码
0 Y% f8 n8 \/ z, G* e  J+ }$ A
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    3 W* H5 K# f* W' c0 \" R2 v
  2. trunc(x:Real) -> Integral9 @9 A, j4 e! ^7 \
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    2 q$ I; _/ \* q/ o7 c
  4. >>> math.trunc(6.789)
    1 z+ u8 }+ D, m1 t
  5. 6
    " X- w! ]" |9 w, u2 m8 z1 W5 O. X
  6. >>> math.trunc(math.pi)
    4 Z2 a" X+ I7 Q
  7. 38 A# G% C4 {* p  \5 L
  8. >>> math.trunc(2.567)# z  x& X( T0 w
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-20 07:41 , Processed in 0.086825 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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