新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

: i  |$ A0 l4 d) t+ r$ _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。, b8 l  w6 k: Y3 `; i3 ?

4 F. \9 n4 N; v$ }' t方法1
) }! M0 h1 Z& \' L" S
  1. >>> import math6 u2 u& j7 r: ]# S/ B
  2. >>> math.sqrt(9); y, k5 A9 {6 S! J1 u
  3. 3.0
复制代码
方法22 t' u5 l( u# l" d
  1. >>> from math import sqrt
    " l2 E/ f# w% i
  2. >>> sqrt(9)
    - P6 p2 @) R0 w; O! V* x9 J
  3. 3.0
复制代码
$ ~, W& M; w2 k! R6 F0 {7 J, w+ e


% ~: r+ ?) _& }& F) e" R6 @
math.e  表示一个常量
) W$ }* B* s/ f  s8 m* P8 T
  1. #表示一个常量
    " k" B' v2 ^1 {, K' `+ R
  2. >>> math.e
    " {9 P8 f% `4 M( n" F
  3. 2.718281828459045
复制代码
& z) @  ^2 D) n3 b
math.pi  
数字常量,圆周率
* o; p0 p8 q4 u- G3 Z
  1. #数字常量,圆周率4 U7 A5 d5 R+ ^) x; M1 t$ W' `
  2. >>> print(math.pi)
    . z% _) f$ K# E7 P" d" c+ R+ r
  3. 3.141592653589793
复制代码
+ h. A( L7 [' [, R: K: x4 _9 V$ a
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
# B5 {" U) A1 W
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    4 Z  ^: t$ L" N  Q% s1 \5 d
  2. ceil(x)( {+ x& e( ], V3 a9 I( n
  3. Return the ceiling of x as an int.
    ! N3 D; Y0 f- X0 d9 R
  4. This is the smallest integral value >= x.
    , A  `2 X6 e# ?5 q( J, [7 C! J9 F+ g

  5. * U& _% ~3 I, J. U' C2 ?* y; i' f3 U
  6. >>> math.ceil(4.01)
    6 }5 _5 T; p4 A* x6 D9 u  o7 ?
  7. 5
    2 s8 A5 \$ }0 \. I4 |/ k  n& V' v
  8. >>> math.ceil(4.99)
    9 ~# t8 O9 u$ a
  9. 5
    * k( p; P; F( E- |* y
  10. >>> math.ceil(-3.99)
    - M- e2 @. Q$ A) Z6 [+ j, ~, ?2 ^
  11. -3
    7 N+ s. _" ]/ l. A
  12. >>> math.ceil(-3.01)& W8 H$ \/ B: K$ d4 t; |- ~
  13. -3
复制代码

# ]$ \: K' W: a- xmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 A0 d8 c+ |  g* k; m& f5 ?9 I
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 W/ q, w# `, G* U6 B" [
  2. floor(x)9 t; |% f3 s) ^$ R5 e" E( t
  3. Return the floor of x as an int.& [1 P7 v- b4 J6 v. f
  4. This is the largest integral value <= x.
      k: @! j5 Z, C3 r9 d: K5 i& h* ^
  5. >>> math.floor(4.1)
    ) h0 M+ v0 P6 N+ K9 n' F. j
  6. 4/ t0 s  e9 Z( B  ?; q
  7. >>> math.floor(4.999)
    - m0 _& l3 x6 u" n1 q" ?; a
  8. 4
    : f, H- [, |" J
  9. >>> math.floor(-4.999)
    3 Z( [9 I. M; D) H* c5 |6 P2 T, @
  10. -58 x5 D1 W! o- R+ o. O+ o, j% r0 c
  11. >>> math.floor(-4.01)
    2 G* M. B9 q% a) C+ i& E: T
  12. -5
复制代码
! ]" K9 `$ Y9 ]. r6 Z& f7 C- F. f4 d
math.pow(x,y)  返回x的y次方,即x**y
) s( Y" A4 P: Y5 ~( Y
  1. #返回x的y次方,即x**y
    3 x/ [, W7 R( j5 R1 Z/ [
  2. pow(x, y)* l: F' q" C1 J5 B
  3. Return x**y (x to the power of y).9 Y' M. s" l! G  Y
  4. >>> math.pow(3,4)1 k% \' m( Q4 K" p
  5. 81.00 M5 W* \+ k* }7 O* y5 L
  6. >>>
    # Z* v0 G7 O# j- z
  7. >>> math.pow(2,7)7 b) H0 ~4 n6 v2 W' B, T
  8. 128.0
复制代码
( u$ h+ i6 `: I
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: X( o& L! p) n& S1 G
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 \+ }- v! t$ n  C
  2. log(x[, base])1 a( ^1 B) @; f: ?- P& A. }3 u0 [
  3. Return the logarithm of x to the given base.
    ! U( w- m1 H" E" g  s* e3 a
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ ]+ K5 N+ ~1 s1 i! n& x
  5. >>> math.log(10)' h6 s& g/ [5 t4 I/ M: C
  6. 2.3025850929940465 E5 Z5 D1 |0 ^5 D& b9 c
  7. >>> math.log(11)
    + n, s0 T6 s; r1 v. n
  8. 2.3978952727983707
    ) h: u4 N3 h" N; V& x
  9. >>> math.log(20), a1 r1 q$ k% y$ W& d" x
  10. 2.995732273553991
复制代码

2 S9 s1 U! b- ~8 rmath.sin(x)  求x(x为弧度)的正弦值
2 b' W+ s: C5 p) N5 D
  1. #求x(x为弧度)的正弦值
    " e% l" z8 G/ ?9 t9 s0 r
  2. sin(x)! U3 g* `# h; `8 L
  3. Return the sine of x (measured in radians).
    2 t( T% {* W0 r6 q3 X* i
  4. >>> math.sin(math.pi/4), @7 V9 o! j& G
  5. 0.7071067811865475, L: h( V% L# J% c7 y
  6. >>> math.sin(math.pi/2); a- u1 z4 z9 s
  7. 1.0. x+ B2 U7 Y; e3 ^
  8. >>> math.sin(math.pi/3)
    - k  ?  u  K- l, ^, p0 _1 t$ E' a
  9. 0.8660254037844386
复制代码

+ _9 C9 m) p$ x5 ?7 E: ymath.cos(x)  求x的余弦,x必须是弧度
: v, M; r3 O( W% g0 W2 ~
  1. #求x的余弦,x必须是弧度$ G$ P& w1 |( L. c
  2. cos(x)
    5 N; H1 g3 F4 Q7 t
  3. Return the cosine of x (measured in radians).' b+ a& p: e0 Q2 z
  4. #math.pi/4表示弧度,转换成角度为45度
    3 c4 w0 _0 P" H) @: C5 W
  5. >>> math.cos(math.pi/4)  u1 ~4 }8 K( h2 r
  6. 0.7071067811865476$ y+ b8 |! E& Y4 H! ^8 z
  7. math.pi/3表示弧度,转换成角度为60度
    6 J' |7 ?) J7 h* S% e4 R- M
  8. >>> math.cos(math.pi/3)3 a3 d9 f2 C; z
  9. 0.5000000000000001  R/ Q; j( S9 U8 L
  10. math.pi/6表示弧度,转换成角度为30度" ^: _3 ?* I- c, w+ m. k7 A! T
  11. >>> math.cos(math.pi/6), U* w: U$ S- \  d
  12. 0.8660254037844387
复制代码
" e2 ?& j6 ?" ]' R
math.tan(x)  返回x(x为弧度)的正切值
4 Y/ C, c9 i% u) S0 M; V  z
  1. #返回x(x为弧度)的正切值
    ! a. ^+ Y, Q( |/ `; ?! l: N6 m
  2. tan(x). D. b6 n' t9 l# z2 C; e2 f) A
  3. Return the tangent of x (measured in radians).! }% ~# ~9 ?( P( J% a7 [7 Y& f
  4. >>> math.tan(math.pi/4)3 ^5 @! A7 T8 @6 v
  5. 0.99999999999999994 Q3 r/ F; Q3 j) \3 X/ d% u; c
  6. >>> math.tan(math.pi/6)
    % X" v* S& W% W& Z1 f% _3 k
  7. 0.5773502691896257
    ! N' Z/ I  a: [2 m
  8. >>> math.tan(math.pi/3)
    ) Q+ B, _6 S- p  t4 R0 c
  9. 1.7320508075688767
复制代码
& r; F! F1 D, X* z% v- ~4 F
math.degrees(x)  把x从弧度转换成角度# w! d% z7 m" s1 X4 d- {; q
  1. #把x从弧度转换成角度; g% R( ?% `- s: {7 J& p
  2. degrees(x)" O1 Z; |5 ]/ o" h
  3. Convert angle x from radians to degrees., w7 `5 r, z; z. {3 c

  4.   m8 h, C6 O3 @  S8 `7 X( o
  5. >>> math.degrees(math.pi/4): _% G! W2 _  Q0 D1 ^9 m; b( k
  6. 45.03 Y# E, @* N! N! B
  7. >>> math.degrees(math.pi)4 \3 E% Q* n9 U& p
  8. 180.0
    " @- p; X% ^3 L; m
  9. >>> math.degrees(math.pi/6)0 L* ?  u7 D) N7 G+ t) j
  10. 29.999999999999996' r! i0 H; _2 M9 r; H' t
  11. >>> math.degrees(math.pi/3)/ D# a9 P4 k9 X: N6 l  [
  12. 59.99999999999999
复制代码

) u5 N, o$ M0 s$ O( n$ Pmath.radians(x)  把角度x转换成弧度7 l% \1 [* q! o5 \; _2 U! N
  1. #把角度x转换成弧度- [' j- I8 `- K. ]6 j
  2. radians(x)
    ! W5 M& x% z+ W: g3 W( t
  3. Convert angle x from degrees to radians.
    # O. \8 H- G4 y1 ~1 x
  4. >>> math.radians(45)
    , z! i6 B2 I8 _$ }
  5. 0.7853981633974483
    + w, O$ X* F4 S/ B, W& C
  6. >>> math.radians(60)
    * [9 p* h. ?/ s4 ^$ @  n- u7 k
  7. 1.0471975511965976
复制代码

2 q7 N" l1 s' F+ x; ^math.copysign(x,y)  把y的正负号加到x前面,可以使用0
) u) f+ @, y* m: j: f& ]
  1. #把y的正负号加到x前面,可以使用0
    . [5 V) X! p9 J
  2. copysign(x, y)0 S1 f: h0 E" `6 s5 ^
  3. Return a float with the magnitude (absolute value) of x but the sign 8 S; H! @4 \& o% W
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    5 }5 Y! f5 |! V8 t( G. W' a
  5. returns -1.0.$ |; B( O1 a) z# E

  6. ; j( e0 K: u- s5 _
  7. >>> math.copysign(2,3)
    1 I% \$ `: `$ [" L! s( u) L% w# ~) ^
  8. 2.0
    % k9 m/ ]# W/ b/ _: t% e' U
  9. >>> math.copysign(2,-3)9 E% h1 b2 G  v
  10. -2.0
    " y' k( v- O/ ]2 l
  11. >>> math.copysign(3,8)
    : x0 D5 j4 D8 M) A: s/ S; v0 b! U3 h+ W) E
  12. 3.0* V( m9 J/ p- G' p" b; o( J/ z2 A  ]
  13. >>> math.copysign(3,-8)
    4 j* O& E% U0 z% [; @
  14. -3.0
复制代码
( `& ~' m" g' r: z* O
math.exp(x)  返回math.e,也就是2.71828的x次方
+ U( q" C3 {0 U6 M( y
  1. #返回math.e,也就是2.71828的x次方
    8 P, ~7 k# G" g  _2 }7 k$ u( D
  2. exp(x)
    4 G1 n/ \/ S: D$ K! x6 \& q9 W* `
  3. Return e raised to the power of x.3 p) V4 N2 V: u  e0 o

  4. ( U/ z2 C% B0 ?  _5 L! |
  5. >>> math.exp(1); b/ X0 _" w/ m7 ?# ^
  6. 2.718281828459045- ^+ Y# [3 J% n
  7. >>> math.exp(2)
    + s' |/ V" X$ p- M$ w  j6 t9 J
  8. 7.38905609893065, S; L! g6 O/ k- |/ ^& F
  9. >>> math.exp(3)+ ]  K8 H( Z0 @9 r  `5 X
  10. 20.085536923187668
复制代码

5 U6 K& S1 i: c6 c/ {1 x" b6 Ymath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
' a, r' I1 ~2 o" G8 S
  1. #返回math.e的x(其值为2.71828)次方的值减1
    / C4 B4 Q. q( q: ?8 j* a
  2. expm1(x)5 R# p" j0 X! A* Z  F9 a
  3. Return exp(x)-1.
    ; c0 n- M& o. a
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ' B  |9 L3 z5 G: O
  5. ! E! Q$ k: k+ u' l7 h
  6. >>> math.expm1(1)
    6 \2 Y6 `5 y2 e. N# [
  7. 1.718281828459045
    " o" [3 |4 g7 ?& X! ~
  8. >>> math.expm1(2)
    / I# g% I! ~; A# Y
  9. 6.38905609893065& a  Z3 j" l/ r: P7 R$ g  R
  10. >>> math.expm1(3)
    / i& H! K, C# v3 {* W8 ~. v+ p/ o  R+ d
  11. 19.085536923187668
复制代码

* A+ ~) n. i$ p/ P4 v  C  y5 Bmath.fabs(x)  返回x的绝对值
# \# F: u/ t1 u; T
  1. #返回x的绝对值
    ! c1 a0 x4 n9 p+ X( l4 l3 F
  2. fabs(x)
    - i5 @/ q3 ?  Z/ N! d) z
  3. Return the absolute value of the float x.
    ; N6 v, W$ r& ^. o5 y
  4. 8 X) X4 k1 K/ M( k' y3 N
  5. >>> math.fabs(-0.003)
    . d9 q' R  c$ C; [
  6. 0.003" W& ]7 P4 W  o8 d- R7 F6 R. ]
  7. >>> math.fabs(-110). z! t0 W. J% E9 f# S$ ^
  8. 110.0
    1 M4 W5 v. J9 e3 B
  9. >>> math.fabs(100)
    1 \* U* d! w, }( G
  10. 100.0
复制代码
- m  Z  G/ f. v& `
math.factorial(x)  取x的阶乘的值
* L0 }8 R5 T. k/ ~. z. M8 M
  1. #取x的阶乘的值/ @9 u# h* L! B  [7 X' U' Z
  2. factorial(x) -> Integral
    5 I6 X1 E9 I3 b1 Z
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    3 Y! h! G3 A  E- I3 {+ d
  4. >>> math.factorial(1)
    0 R1 H, i$ n  N# o; R" w, e
  5. 11 m. l0 ^) R7 w9 d$ T$ P
  6. >>> math.factorial(2)& F5 H7 {4 A; r1 n
  7. 27 t3 ^+ v9 `$ A0 y) U
  8. >>> math.factorial(3)
    & D) {1 v& ^. j) {
  9. 69 M" o- j3 y- q3 {* S" Z
  10. >>> math.factorial(5), m# E' U( i3 x  P# u
  11. 120
    : U+ l" s' T* J- g0 J' G" d5 w' @/ `
  12. >>> math.factorial(10)
    ! _0 l7 ~5 A1 \9 h4 E
  13. 3628800
复制代码

9 c0 z/ z, m1 H1 j& }math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
3 g0 M# ]2 P0 o7 @# {8 h0 v
  1. #得到x/y的余数,其值是一个浮点数$ p' X! T* p- p5 @$ Q; c
  2. fmod(x, y): `$ G2 d' p2 y3 n/ m9 S% f: k
  3. Return fmod(x, y), according to platform C.  x % y may differ.% @5 `# @* U1 j8 M, s  f6 Q- P4 r4 v
  4. >>> math.fmod(20,3)1 E& _4 i2 w/ Q' Y, }
  5. 2.0
    : A. ^4 {0 p7 K# a! L& ^
  6. >>> math.fmod(20,7)
    ! q1 }4 f, n# e8 g5 W/ c8 B$ u5 S
  7. 6.0
复制代码

0 h' T0 O# Z8 N/ f! |3 Smath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! Z4 Z% N( A, n( R$ r6 a* J
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 b8 S# L! h! R: P7 }- B
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: o$ L+ h* O/ e# u  \. v
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; ?9 V; l. W! q" n1 C1 v' h
  4. frexp(x)
    ) }" b! U) x. z. K. \0 L
  5. Return the mantissa and exponent of x, as pair (m, e).
    7 z7 a( a- i: P
  6. m is a float and e is an int, such that x = m * 2.**e.
    8 E% d' V: B" o$ b. Y  t
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    & ]+ @! U0 V0 x# P
  8. >>> math.frexp(10)
    : p1 t& C  M) v: e, U
  9. (0.625, 4)
    2 @  {: S- n2 k- W3 ^5 @, P3 D6 F
  10. >>> math.frexp(75). B9 g' M9 G5 [; q
  11. (0.5859375, 7)" _5 O9 S1 H( U& n- |" z1 Y" C
  12. >>> math.frexp(-40)) q& f( U! A) x4 O9 Q) @2 z; y
  13. (-0.625, 6)8 D. @: N) B9 t6 S
  14. >>> math.frexp(-100)
    ; }( t" `% ]7 A& |3 |* V
  15. (-0.78125, 7)& B3 h4 j( W. X2 j3 @6 m+ a
  16. >>> math.frexp(100)
    3 o  H, s: n) f. S
  17. (0.78125, 7)
复制代码
8 Z3 d- \) x& ~: h* S& N. o2 r
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
: i; E1 w$ B. d. _; P) |
  1. #对迭代器里的每个元素进行求和操作; j$ E; t) _7 o8 W  I3 d. e
  2. fsum(iterable)
    2 j3 i0 y3 d0 D/ E' _
  3. Return an accurate floating point sum of values in the iterable.( C8 z5 N: I! x
  4. Assumes IEEE-754 floating point arithmetic.
    $ h% \( g( V2 {8 G1 q/ o- ?
  5. >>> math.fsum([1,2,3,4])
      j: c7 O; ?: {
  6. 10.0, U# j9 c( L2 a. [  [; u
  7. >>> math.fsum((1,2,3,4))% A. ^) S  x+ _  i8 ?3 H
  8. 10.0
    $ _4 D  l9 v$ `! w7 f/ a
  9. >>> math.fsum((-1,-2,-3,-4))$ y/ i6 z+ y5 f
  10. -10.0
    , \. y, F: ~/ t
  11. >>> math.fsum([-1,-2,-3,-4])
    8 a- C/ q8 h' A' @$ s
  12. -10.0
复制代码
1 x: V' C8 m( T9 P
math.gcd(x,y)  返回x和y的最大公约数8 ~( s% Q- h7 O8 @7 k2 f
  1. #返回x和y的最大公约数
    # v. k3 c0 c, i. Y% N# p6 Y
  2. gcd(x, y) -> int
    9 Q4 X3 g2 r) I. C% N1 J
  3. greatest common divisor of x and y
    6 v# O$ ]" Z9 R4 R
  4. >>> math.gcd(8,6)% j: T: _: ?% `) F0 p
  5. 21 ?5 k. O0 D8 I3 e! `6 A
  6. >>> math.gcd(40,20)( Q: z  x# Q: N1 Y/ e
  7. 20
    - V0 ^4 k; n# Z0 t! y8 D4 ^
  8. >>> math.gcd(8,12)0 y3 A! K8 W& ?6 Z) r
  9. 4
复制代码
2 U  h* K/ w/ M
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False' d0 d' S3 z6 u" ~5 h5 O" @
  1. #得到(x**2+y**2),平方的值
    % T, f/ S" E& z. ^7 ~. J- M9 ^
  2. hypot(x, y)
    9 \  C2 T& I$ U
  3. Return the Euclidean distance, sqrt(x*x + y*y).3 n1 f$ P2 t5 V8 c# i
  4. >>> math.hypot(3,4), d& {: z( i* g
  5. 5.04 t" G: u1 z0 O
  6. >>> math.hypot(6,8)+ a% ~* a' P. F! z1 C% q" ~
  7. 10.0
复制代码
1 i+ P8 y  J5 N! v+ P+ \$ ^
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False6 s. R) C0 [9 s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False# a" _/ C6 ^3 h8 ^$ S' I8 P! k. j$ Z
  2. isfinite(x) -> bool5 j5 ?2 }# E% A. m8 U
  3. Return True if x is neither an infinity nor a NaN, and False otherwise., _. f: m* F* |+ d% F; y
  4. >>> math.isfinite(100)
    , \: ?4 v& p& x' O0 _5 H
  5. True# K/ F4 @" q5 v7 j
  6. >>> math.isfinite(0)
    + X. s6 \1 H1 e' t
  7. True$ ^+ J& Z+ w! u0 C) F0 B
  8. >>> math.isfinite(0.1)  `! Y3 W$ w+ U3 ^5 ]
  9. True! X/ J1 q' c- K3 A+ ~/ D8 c# f% I9 G
  10. >>> math.isfinite("a")6 x+ E( m+ t8 [4 J' x) B
  11. >>> math.isfinite(0.0001)
    8 \* S  u1 q9 ^, f4 K
  12. True
复制代码

  c) s6 b% ~1 I9 g0 F& u# Emath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False+ o8 a, X5 W" T5 q) q% K
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 i; t7 i: w4 d2 O
  2. isinf(x) -> bool
    ( u2 t# W. K6 @0 K/ o2 g% U( F
  3. Return True if x is a positive or negative infinity, and False otherwise.9 ^( v! x1 `+ g
  4. >>> math.isinf(234)
    * Z9 o$ H: F* m8 |- L2 S
  5. False
    % }# j1 v& }/ a- s% N' P8 I
  6. >>> math.isinf(0.1)# z8 [: t5 G; p+ h3 x$ E4 t2 x
  7. False
复制代码
$ F. O: H$ ~; U. K" W, v+ p! @
math.isnan(x)  如果x不是数字True,否则返回False% b$ C: }% q4 j+ J
  1. #如果x不是数字True,否则返回False7 ]% S, \9 _; b$ b% u+ b; ?2 k
  2. isnan(x) -> bool5 e! s7 S5 E6 ]
  3. Return True if x is a NaN (not a number), and False otherwise.
    5 c3 g" `( z' `
  4. >>> math.isnan(23)
    8 L& e( M% I# D' a
  5. False
    / F/ S2 W' f8 f( m0 V- J7 B7 G! W5 ]1 H; `
  6. >>> math.isnan(0.01)
    3 k' M9 p- r3 U8 P+ k
  7. False
复制代码
+ y( |4 J" @, D: \( V5 q
math.ldexp(x,i)  返回x*(2**i)的值
# ^: L1 s6 G0 m! V! }  @
  1. #返回x*(2**i)的值' z3 x% M/ O8 I- A! w# E& Y% s! ^7 A
  2. ldexp(x, i)
    , s" A6 A( l8 d& M' ?
  3. Return x * (2**i).7 [5 e( O2 G; Y& h" q
  4. >>> math.ldexp(5,5)
    . t6 |2 F, q( X2 P; q2 }8 |
  5. 160.0
    , R6 ~% K- c) M! i3 m8 U# e- I
  6. >>> math.ldexp(3,5)
    7 m3 R' I  }- }$ Z+ r; j# I& e+ N
  7. 96.0
复制代码
; Y7 o# N  C' r/ H7 C
math.log10(x)  返回x的以10为底的对数. ?) ^3 L8 z+ H: N0 Z1 C
  1. #返回x的以10为底的对数5 Q: V" Z6 Y+ f; U3 v2 L* M
  2. log10(x)! U# x; D, O! f- x% _' Z
  3. Return the base 10 logarithm of x.
    : D3 ?: O* `, Q' ?+ e
  4. >>> math.log10(10)# _  X4 B, \2 ^9 i0 G5 W
  5. 1.0
    & i+ i. b  _5 X! g
  6. >>> math.log10(100). x) p5 A* ~2 f- K- ?$ ~  K' h  L
  7. 2.06 U& r; ^. x9 h( N; u, n$ {+ I
  8. #即10的1.3次方的结果为20
    / I3 L* F; L* K( S- A% b  N
  9. >>> math.log10(20)) d1 p& L" m( {/ h4 a
  10. 1.3010299956639813
复制代码

+ b8 i( s9 E( k: Z4 C6 Umath.log1p(x)  返回x+1的自然对数(基数为e)的值+ z% q" P! f! {; c7 z  g" `
  1. #返回x+1的自然对数(基数为e)的值
    5 L. E9 z- g! X/ q' _/ X& p$ }  r
  2. log1p(x)
    ! Y+ Z4 [9 G) A& ^
  3. Return the natural logarithm of 1+x (base e).# N" t+ R4 k" C# F" f( ^7 N
  4. The result is computed in a way which is accurate for x near zero.8 o. }  t5 Q7 s6 j$ p* m: b& E4 \
  5. >>> math.log(10)2 D. V  @1 p& c2 D/ t: e
  6. 2.302585092994046
    ( n# F' N. I% S# K! F/ ~; P
  7. >>> math.log1p(10)1 v& K4 T% j$ S! C3 `
  8. 2.3978952727983707% F* G0 W$ @" ?" r* W2 A
  9. >>> math.log(11)9 h* o  C# p" C+ P+ Q* y/ m0 X; U1 I
  10. 2.3978952727983707
复制代码

" Q- k- G( U3 X. i, d6 hmath.log2(x)  返回x的基2对数
( A$ `1 ~6 _3 h. u9 b* \9 T: ]
  1. #返回x的基2对数! p4 P( D. u: r
  2. log2(x)
    9 w8 T- W) J3 E0 Y8 L/ S) w2 I5 d6 ?
  3. Return the base 2 logarithm of x.
    + z; I. _; _4 n
  4. >>> math.log2(32)
    ' T* M$ N1 y, }, H9 N$ d
  5. 5.0; x! |) V! f* U
  6. >>> math.log2(20)
    5 D! C( v) m6 `) E5 C: ^
  7. 4.3219280948873635 N& }0 q! F! O5 |" X6 f# ~
  8. >>> math.log2(16)
    2 B7 v, X" r- o' {8 z3 }4 G) \% z) ]
  9. 4.0
复制代码
7 M- }* |+ |% k  g* h5 U( q
math.modf(x)  返回由x的小数部分和整数部分组成的元组
  X2 J' b6 ~! f% K2 R
  1. #返回由x的小数部分和整数部分组成的元组
    7 `( H5 k$ r. ~/ e: x  a
  2. modf(x)
    ( M& y$ C& N0 C! L5 I/ g
  3. Return the fractional and integer parts of x.  Both results carry the sign% }8 _. ]. S  B& p! @  E2 L
  4. of x and are floats.
    & y$ D2 S2 B0 t
  5. >>> math.modf(math.pi)# Q' ?1 b9 H4 I( U# r! u
  6. (0.14159265358979312, 3.0)
    8 a. i/ {& |6 {; ]4 }6 Q0 m3 D
  7. >>> math.modf(12.34)1 m* I  K6 {- F( V+ y0 V
  8. (0.33999999999999986, 12.0)
复制代码

( x9 I0 K1 W0 I& u, C& O' Emath.sqrt(x)  求x的平方根' w- P% ?0 i/ U. b0 d
  1. #求x的平方根
    / R) d+ O2 ]# H' O
  2. sqrt(x)( ~- a4 ^$ C- X( k
  3. Return the square root of x." i+ }3 h& @; w" G
  4. >>> math.sqrt(100), d% B* \/ c% ~0 g( e
  5. 10.0. y8 A% h$ F  ?5 J; \( q" H' d
  6. >>> math.sqrt(16)
    9 L* R! G; e6 G: p* p
  7. 4.0
    " U/ t0 |" h5 L% d9 }* s
  8. >>> math.sqrt(20)
    " R( Z6 i" c4 |/ G3 l
  9. 4.47213595499958
复制代码

( {2 e! [" k9 S3 c7 S9 r3 omath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 U. @( O- v3 o+ l3 j9 {! H# U+ `7 \% h! h
  2. trunc(x:Real) -> Integral
    3 f! v) E% M" ]
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    - I- u" @* E- H; `- B3 w$ ]- \
  4. >>> math.trunc(6.789)
    2 Q6 {4 q& O; u& D
  5. 65 T' A' D% g( C: e1 ~! c6 B& O
  6. >>> math.trunc(math.pi)
    % K5 X& s, e& z7 g7 L4 ]
  7. 39 n  l( s  E' @- i9 A* T: d
  8. >>> math.trunc(2.567)" l! t% p! ^( p( K7 d
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-22 07:21 , Processed in 0.083824 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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