新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
$ E7 E7 @9 Y" P" C' O% s. G2 q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 J  x% L4 W* p, o& j; R! u* p
' f3 l: f* j# l' }/ q1 v
方法1
$ b& o9 S' l/ m* `6 @! t3 Y
  1. >>> import math$ j5 m8 |5 w  F) L. k
  2. >>> math.sqrt(9)- a; M/ k& Q/ |. E9 J' ]. G
  3. 3.0
复制代码
方法2# r9 c; P) d* n% z
  1. >>> from math import sqrt
    ! g; j9 L$ |$ {/ W
  2. >>> sqrt(9)3 n; r8 P! a- x" s' g
  3. 3.0
复制代码
) r8 Y4 D, `! Y, S  M7 q4 R4 @9 y3 Z


) b  k. W& e2 @$ h) h: X: G
math.e  表示一个常量9 ]# L# ~2 g7 J
  1. #表示一个常量, J$ u" _1 z( W) _6 V& e; y" B
  2. >>> math.e6 u) O! \4 a: X# A- Q! N' O' ]. h
  3. 2.718281828459045
复制代码
# d3 X& b$ L& s+ j" r
math.pi  
数字常量,圆周率
$ B1 T4 y$ ~5 M6 S7 k7 a- h
  1. #数字常量,圆周率
    , F- W% k) |& _% g. ?: p
  2. >>> print(math.pi)
    * x% |+ ^9 s9 P3 v' n
  3. 3.141592653589793
复制代码
( V  R4 G  r# k5 Y& s
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
% X9 C/ n$ Q4 A& Z" _
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, \3 B3 A' v# f) P
  2. ceil(x)2 i  k& U9 v* `) [/ C
  3. Return the ceiling of x as an int.
    3 [  l+ m5 \0 x# n" E
  4. This is the smallest integral value >= x.4 U) u9 m/ D6 T- q% v8 `

  5. 5 [) K7 n! t; j5 m5 F
  6. >>> math.ceil(4.01)( e" ~8 f& U( _
  7. 55 n" Y: _% J9 H6 @# {* R# h# m
  8. >>> math.ceil(4.99)
    - h: _7 w& Q2 b& I% H
  9. 5
    ! c: c0 J$ ?4 m1 Q6 ^6 v
  10. >>> math.ceil(-3.99)2 x0 o2 Y! [2 c3 y' |4 K& o
  11. -3
    2 G. E% c6 f- o# L- ]" K5 ?3 Z
  12. >>> math.ceil(-3.01): f; J) L( {4 ?1 Z
  13. -3
复制代码
4 F' @( D- W. w
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 N/ o# B9 ^  o5 }' D, f6 u8 q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ' X! l# O8 L9 k- b* a# s
  2. floor(x)
    8 h" {; ]0 z3 J' P
  3. Return the floor of x as an int.9 n+ v& A2 e. M3 J3 u
  4. This is the largest integral value <= x.$ M( U5 J8 g) b
  5. >>> math.floor(4.1)
    3 T7 w* f9 V* P
  6. 4
    5 `" d: M$ Z$ _$ S
  7. >>> math.floor(4.999). O& j6 h+ ~. g' `0 s+ x) a
  8. 4
    ; Q7 {+ z" ?6 s! F0 w6 |
  9. >>> math.floor(-4.999)8 U- |6 V, I8 |; M
  10. -5
    % f! q" V0 b2 ~
  11. >>> math.floor(-4.01)
    0 w, b0 y6 w7 w/ o8 {. \4 u; q
  12. -5
复制代码
: G- x; Z) V) s" N; N  a: V
math.pow(x,y)  返回x的y次方,即x**y+ K/ v. U( ?1 G0 R. V; K! Q
  1. #返回x的y次方,即x**y
    8 |: t' H8 R5 U/ W* _  Q: j
  2. pow(x, y)
    6 y& k1 I  |( |& z1 i
  3. Return x**y (x to the power of y).2 F  P) H4 o3 h! P6 r" }
  4. >>> math.pow(3,4)0 P, B2 g0 G1 ~' c0 W
  5. 81.0
    + r2 X; r, [. I$ U, \, Y
  6. >>> & ^3 |. Q$ I5 K3 a- {! i6 n
  7. >>> math.pow(2,7)  R' a! G' o5 c1 T8 v3 J9 E
  8. 128.0
复制代码
8 h4 E2 `1 I' y8 K
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# U. L; z8 G" }$ Q
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)1 F8 X9 |3 Y7 j' i
  2. log(x[, base])* T$ U$ i; ?. y
  3. Return the logarithm of x to the given base.  a$ ?3 |6 V7 `( A
  4. If the base not specified, returns the natural logarithm (base e) of x.
      N  H: N2 ~; R
  5. >>> math.log(10)% K  N' W: x4 [4 g, o; k$ r! s7 ?
  6. 2.3025850929940463 g% y# s& i$ R: o3 w0 A" |, Y
  7. >>> math.log(11)
    ) ?8 t) C1 o: V6 ~: {- n
  8. 2.3978952727983707
    1 _$ g/ ?; c: k6 P% U+ y
  9. >>> math.log(20)
      ]  G% |* U$ d# r) M0 r
  10. 2.995732273553991
复制代码

$ k/ [( E7 s1 z" L8 Smath.sin(x)  求x(x为弧度)的正弦值
! ~0 u  [' K+ o6 g/ a, ~9 @
  1. #求x(x为弧度)的正弦值% v0 p' ]" V! Q( U
  2. sin(x)) g& g3 A. V9 P6 R
  3. Return the sine of x (measured in radians).& G* f$ a; t6 Z' H! b2 U
  4. >>> math.sin(math.pi/4)
    ( y2 i) ^: z0 G3 f" g
  5. 0.7071067811865475
    + u3 t9 L) `) P
  6. >>> math.sin(math.pi/2)+ G8 B0 z0 `6 a9 W# q; P( S
  7. 1.0
    ) m  J  W3 W/ Q2 y* S' ]" r5 r4 F' I
  8. >>> math.sin(math.pi/3)
    " I! n" c- n4 e
  9. 0.8660254037844386
复制代码

# f3 b: ?% b0 c, {  Tmath.cos(x)  求x的余弦,x必须是弧度
" N; }. D" g% S6 Z7 _
  1. #求x的余弦,x必须是弧度
    6 u/ }4 F2 ?  @5 P- f5 ]' I
  2. cos(x)  R' o' S  W5 C
  3. Return the cosine of x (measured in radians).0 L/ K! K0 [1 v
  4. #math.pi/4表示弧度,转换成角度为45度6 s  q6 c, s7 f
  5. >>> math.cos(math.pi/4)+ v# S! E& Y; n* ]2 E
  6. 0.7071067811865476
    , H; U: @  x$ P# X9 S0 V* K
  7. math.pi/3表示弧度,转换成角度为60度
    ) L- I. j- B+ b9 n' w- j
  8. >>> math.cos(math.pi/3)5 c1 z6 S0 A: w
  9. 0.50000000000000018 S: o) v; i3 i
  10. math.pi/6表示弧度,转换成角度为30度4 K% Z2 p- \& R! ~. c0 A4 {- Q, ^
  11. >>> math.cos(math.pi/6)) @/ e8 u- B& m
  12. 0.8660254037844387
复制代码

0 J. Y' ^2 \" P+ b; E! @" emath.tan(x)  返回x(x为弧度)的正切值- e1 g6 [8 W2 Z) f
  1. #返回x(x为弧度)的正切值
    $ U* _, B$ \" _9 x
  2. tan(x); E$ }* ~) L' H3 {
  3. Return the tangent of x (measured in radians)., t4 N3 @+ O4 l6 Q  P2 m0 s
  4. >>> math.tan(math.pi/4)
    5 [' |, |2 g* \
  5. 0.9999999999999999  q( e* v- Z6 o$ U6 H
  6. >>> math.tan(math.pi/6)
    + ~2 D, @  ^9 B
  7. 0.5773502691896257
    * b4 I$ x+ c& m4 W- i. j8 q1 b4 J
  8. >>> math.tan(math.pi/3)
      ^: ~0 ^# N! X+ ~" }
  9. 1.7320508075688767
复制代码

0 t2 y5 ~  f; }7 nmath.degrees(x)  把x从弧度转换成角度
% t! X# B+ x0 m. _) [3 v$ D
  1. #把x从弧度转换成角度
    : D9 S# X+ Z* F/ z
  2. degrees(x)
    1 Y3 u& E6 o1 j2 P- z1 ~) V6 i
  3. Convert angle x from radians to degrees.
    ( y" r# D1 E$ ]/ ]4 M" k7 N5 P
  4. 3 n  {2 N* [9 x2 y' s3 R' ^
  5. >>> math.degrees(math.pi/4)
    6 ]; I% T, l6 w8 V
  6. 45.0: E. b" V& \" V2 f$ |
  7. >>> math.degrees(math.pi)! V& h# N7 A8 `6 a& G
  8. 180.0
    3 r$ f$ d' n0 @' ]; z' [7 k+ m
  9. >>> math.degrees(math.pi/6)& g; x/ J- z/ |. U9 J/ A: N
  10. 29.999999999999996
    , s# V/ \, u) f$ u# G
  11. >>> math.degrees(math.pi/3)2 s) c8 v+ q* D4 k" s0 Y
  12. 59.99999999999999
复制代码

5 g' I6 q  o) t8 ~* f' G! Pmath.radians(x)  把角度x转换成弧度
, B+ m' M& L  g) ?! f
  1. #把角度x转换成弧度
    6 |& ]* j% }) `4 F" W# C, n) d8 r1 z: {
  2. radians(x)# g& X3 n. E( B2 L* o0 m
  3. Convert angle x from degrees to radians.
    . g( A) [" F* {  \2 j/ D) z2 `
  4. >>> math.radians(45)
    ' ]# C9 ?$ l6 `8 U
  5. 0.7853981633974483/ r6 c3 n* Z" w% s; d: Q1 \
  6. >>> math.radians(60)% ]% [( R; a. q" d  O0 m- p' f! U6 d
  7. 1.0471975511965976
复制代码

/ j" q7 ?, h9 t3 q3 s7 ]/ qmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
8 a6 P/ s0 W$ O2 |
  1. #把y的正负号加到x前面,可以使用0/ F. Y5 N4 p" b  P2 K2 |: c
  2. copysign(x, y)- w4 t7 z, E8 ~
  3. Return a float with the magnitude (absolute value) of x but the sign
    / H% n) ^/ w" p/ @
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    : i6 l+ O4 v+ E
  5. returns -1.0.
    + [7 U( k$ F7 b4 g
  6. - \% H  s( z. w9 g
  7. >>> math.copysign(2,3)
    ' W+ x: ~* S% |; @% J1 _1 _
  8. 2.0
    ( U* l  ~( k/ W  |
  9. >>> math.copysign(2,-3)
    6 L  R8 z+ o4 b! D
  10. -2.06 r6 ~4 G5 l7 y% l6 {
  11. >>> math.copysign(3,8)
    3 Z- ?" M- T& c3 S
  12. 3.0
    2 C0 D% |" B" r: {
  13. >>> math.copysign(3,-8)
    & f- \* G: ~9 w4 U- ?6 I  g" \
  14. -3.0
复制代码
$ N0 o6 }9 z+ d3 L& J8 {
math.exp(x)  返回math.e,也就是2.71828的x次方
% \! {9 p& b' N; Z( c
  1. #返回math.e,也就是2.71828的x次方
    $ M! b! Z5 M% U9 v3 P+ ?+ R/ Y, U! A; ^
  2. exp(x)6 t2 e& I  l0 N4 x/ Z' U! ~
  3. Return e raised to the power of x.. N0 n$ m0 f" T! l. a

  4. + C1 ^& T$ T! S* Y' V5 C9 T
  5. >>> math.exp(1)7 x! F' J8 [, c1 V; a* F! b
  6. 2.718281828459045
    * ~3 R* S! E  \# }+ y, E
  7. >>> math.exp(2)
    + S, |2 a3 M! T- T  Y; s$ Y
  8. 7.38905609893065
    1 I- T0 e! s7 R  {: }7 J
  9. >>> math.exp(3)
    % s. l. b) f2 k9 W6 x1 c
  10. 20.085536923187668
复制代码

2 i. B( |* n& @8 e" x9 Y8 Emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减17 j& h$ ^% p# G% P7 K
  1. #返回math.e的x(其值为2.71828)次方的值减17 P5 |' t: F* C% C6 m
  2. expm1(x)5 C% _9 f+ j9 q! U4 t' p4 e
  3. Return exp(x)-1.1 ?% L  k! a. F9 f3 j4 w% h
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 x8 {- X% H. w" L  M4 q
  5. % L  d+ E0 F9 `6 o
  6. >>> math.expm1(1)
    ( g  c6 c' W% `
  7. 1.718281828459045
      |  p( B6 `* J) l" k/ z2 m0 L
  8. >>> math.expm1(2)
    + w/ K4 F$ D' e" \) J/ `
  9. 6.389056098930650 O7 l* @* N: ?/ R5 V+ q1 T9 o) ^
  10. >>> math.expm1(3)
    - X# M; j: Z. e
  11. 19.085536923187668
复制代码
5 _8 y- |* X) P' Q
math.fabs(x)  返回x的绝对值
* h: j; A' G% d4 R
  1. #返回x的绝对值9 J1 P1 W1 y( O6 p
  2. fabs(x)3 V8 C! I' _3 O
  3. Return the absolute value of the float x.
    2 w; G* F$ P  X# p/ ^
  4. . q5 y1 D! v/ t8 M5 @
  5. >>> math.fabs(-0.003)1 \; {  ^5 Q9 h2 |% v- p0 u: G4 i: W3 J2 C
  6. 0.0030 X9 N* a9 y- I! f1 P3 g
  7. >>> math.fabs(-110)
    3 @# Q& m/ g! n5 ?$ f
  8. 110.0
    4 S1 ^6 k; s" p; x4 b' _- t" M
  9. >>> math.fabs(100)
      a# o; H& {) q7 d0 E
  10. 100.0
复制代码
$ P8 c( V! D7 F0 u; C4 Q$ Y
math.factorial(x)  取x的阶乘的值$ b( j* Z2 T2 S% {4 Z6 h- P
  1. #取x的阶乘的值! i1 s1 n% Y8 ]4 Y7 O" M3 h3 v
  2. factorial(x) -> Integral
    ' q1 a7 w5 e5 R3 z& S% a0 ^
  3. Find x!. Raise a ValueError if x is negative or non-integral./ f3 b! y& D. C/ C0 f$ e' h
  4. >>> math.factorial(1)) L- T) R$ W  p0 y* F! a" V
  5. 1
    , v( \% ^/ K' }9 e* j
  6. >>> math.factorial(2)
    " {2 o+ \2 e; E/ F8 u
  7. 2( Q1 n" v( Y2 t" z
  8. >>> math.factorial(3)
    1 d& p( U7 l3 k8 Z& l
  9. 6
    0 ~; |. Z1 V0 w0 d9 H
  10. >>> math.factorial(5)
    ; v: }2 t- ?7 D5 x( H3 o+ @" b
  11. 120: i! a: Z: H; [. {
  12. >>> math.factorial(10)
    4 a3 `! t- s$ Q+ c0 w# R7 a
  13. 3628800
复制代码

# s  \1 M" q* b! F. Imath.fmod(x,y)  得到x/y的余数,其值是一个浮点数- U' J; I- ]0 Y
  1. #得到x/y的余数,其值是一个浮点数  D& q4 |  z) \% s5 I% G
  2. fmod(x, y)
    ! z0 i. v% B; [3 d
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    % j( n' q2 H& |
  4. >>> math.fmod(20,3)
    0 O) v/ r2 |6 C* \5 C
  5. 2.0/ q, [, U5 S0 y  V7 a
  6. >>> math.fmod(20,7)3 V; q5 p1 g: x& }; f* \2 A
  7. 6.0
复制代码
/ x! r$ x0 U! o9 E+ F. x" t# |2 x- g( }
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 c: O$ u, ^8 ?8 g7 y2 S5 O* N/ t
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. D( O. _8 L" p4 R6 M$ `
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
      M, `) p  x/ ]  v4 W; O& y
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    $ G- H9 V0 m- t* e0 x
  4. frexp(x)( n6 s# R+ H0 u, X. C: @+ `8 n
  5. Return the mantissa and exponent of x, as pair (m, e).
    + t8 ~* B1 G% o4 [& [2 j
  6. m is a float and e is an int, such that x = m * 2.**e.
    . s/ g+ X7 X- \* N! z( p9 s' N; d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    8 }5 v4 M4 U7 Q- V2 ~3 Q
  8. >>> math.frexp(10)
    9 R  Z6 L0 `! O- V3 L/ g
  9. (0.625, 4)
      B3 |, ~/ n1 J2 B: {0 u
  10. >>> math.frexp(75)6 S* U" x& R6 @; n! L
  11. (0.5859375, 7)  j5 o" @8 h% L2 C! P/ U
  12. >>> math.frexp(-40)3 s, i" E/ E! p0 z7 C& p
  13. (-0.625, 6)
    + k5 r. [' Y& p. E7 x' P  F0 S+ q# C
  14. >>> math.frexp(-100)
    . f3 \: h' \0 i8 t5 p) \
  15. (-0.78125, 7)) V- ]' ^& d& Y6 @3 Y; G. O
  16. >>> math.frexp(100)( N% ^' O8 M5 Y; n+ g
  17. (0.78125, 7)
复制代码
/ M  J+ \" r0 U8 u. k; T) a
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列) @) E1 E2 ~2 \/ m
  1. #对迭代器里的每个元素进行求和操作
    . d1 b. x, q- {6 {3 b/ L
  2. fsum(iterable)  F/ G  r) _# w. @+ ?7 T/ R2 D4 e
  3. Return an accurate floating point sum of values in the iterable.
    ! F7 e& n! X! L) p0 X+ F( A
  4. Assumes IEEE-754 floating point arithmetic." [' g4 K+ h9 p3 Q
  5. >>> math.fsum([1,2,3,4])+ V& \4 ~' m! v4 j) _' ?- E
  6. 10.0- ]* M. h0 b8 `$ n" L2 N6 L
  7. >>> math.fsum((1,2,3,4))
    7 N6 X$ F, d4 _- ]3 L9 E
  8. 10.05 H: Q% {& n! b( f" y9 \
  9. >>> math.fsum((-1,-2,-3,-4))
    3 \% Z2 A7 W7 ^+ h2 q5 I( f4 F% q
  10. -10.0
    5 z0 C& I& f' Y8 l3 `$ C
  11. >>> math.fsum([-1,-2,-3,-4])
    + Q: Y, n+ x$ N
  12. -10.0
复制代码

& x: B3 `- y" R, h: qmath.gcd(x,y)  返回x和y的最大公约数" P% r+ p' M, T; L* ^4 I2 F# z5 u7 d
  1. #返回x和y的最大公约数) C( m! f/ D/ |
  2. gcd(x, y) -> int+ V5 ]+ ~5 _( t& U# L
  3. greatest common divisor of x and y
    2 O% m9 h4 g6 x9 q& S. O. x
  4. >>> math.gcd(8,6)" c( _) t* T9 ]; L- P
  5. 2- m5 `( y5 z  p8 C( D. W
  6. >>> math.gcd(40,20)
    3 V& @, ^6 x+ i7 L3 B% N
  7. 20  z- M8 N$ _  l% F0 ^  @4 h$ S& F2 k
  8. >>> math.gcd(8,12)
    - T, C# `2 L9 Y: j, n- P& N( E; U* `6 U
  9. 4
复制代码

& I- `5 Y9 |9 v& N, j/ G; Wmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False% w9 I; l" c! b/ e
  1. #得到(x**2+y**2),平方的值
    " o) ?( v: X2 v6 C" J- w
  2. hypot(x, y)
    7 U% O0 H" ?' K2 ?
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    / k7 S. ^, r5 B6 w) b
  4. >>> math.hypot(3,4)# T+ q/ P- u6 }5 [* g. w1 i
  5. 5.0
    0 Y9 v' \! `- v& N, \5 j; D8 o, B
  6. >>> math.hypot(6,8)
    , N9 K! H  D$ r' ]4 S2 Z
  7. 10.0
复制代码

/ ^3 C! E1 L" }math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 E# B  F$ h9 O* @2 v* Q8 @, o/ G
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" @2 Z4 C; A! E: f( M6 `
  2. isfinite(x) -> bool
    " q) [" l4 f( A
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.: O( p, ?) o0 K. H0 i5 p
  4. >>> math.isfinite(100)
    . }( M) r9 c. o" \# _' @
  5. True
    8 j: a) K) [" A
  6. >>> math.isfinite(0)5 {' G, X: |/ c. X8 j( L5 g  j
  7. True6 M# N5 C+ l$ [3 u# x
  8. >>> math.isfinite(0.1)
    & V8 O' C) M, B9 j. k4 d- E
  9. True
    : x5 L* f& A) q8 E
  10. >>> math.isfinite("a")6 t7 u4 v5 @, b, s! g8 L( s1 I( v
  11. >>> math.isfinite(0.0001)
    1 `$ W5 B4 F) S9 J) B0 H
  12. True
复制代码

+ N! ]7 Z  Z" Vmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False$ a+ D" X& l7 o, n" P& M. I' ~0 M
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False4 e. ^, e* c# j) `
  2. isinf(x) -> bool
    # i+ @( r% W! {' @0 s+ |6 C& s% N6 ?+ G
  3. Return True if x is a positive or negative infinity, and False otherwise.
    1 O8 y$ O- K5 Q( t$ m) j
  4. >>> math.isinf(234)
    5 v1 r$ V4 q1 o+ f) h: v# r
  5. False
    & S6 m: S& p- ~1 h- [" q+ i% d% R1 a/ l
  6. >>> math.isinf(0.1)
      v# y8 s2 x; I! M, _
  7. False
复制代码
& Q9 J, D2 c* {7 d( z
math.isnan(x)  如果x不是数字True,否则返回False
7 J, a' A* ?: o+ J. S) O# _
  1. #如果x不是数字True,否则返回False
    9 q" T2 ]0 q% O, y( E# E* r
  2. isnan(x) -> bool* j0 Q/ J3 m3 O3 a
  3. Return True if x is a NaN (not a number), and False otherwise.2 \2 c9 G6 k4 h/ k; j- }
  4. >>> math.isnan(23)# D9 `1 z8 l$ c1 X  @' `
  5. False0 z0 d! l+ c/ j& @; M& R9 a' y1 S! n
  6. >>> math.isnan(0.01)
    6 ?- @) h1 c, ^3 F6 E
  7. False
复制代码
$ W! W9 B& g& h9 [3 r4 s# F1 \/ v+ G/ Z( p
math.ldexp(x,i)  返回x*(2**i)的值# c9 m- a. {" J7 A3 R, o
  1. #返回x*(2**i)的值
    + R$ [2 U5 ^$ H5 S. _" D
  2. ldexp(x, i)
    % i  D( s( b' d* A- q
  3. Return x * (2**i).- X2 A9 a' ^/ p! o6 l1 ~
  4. >>> math.ldexp(5,5)
    - D% y; E  c0 o% S% q
  5. 160.0
    ; F+ t: ^& v* m6 ^! [5 h
  6. >>> math.ldexp(3,5)
    3 h* w+ [. _9 R+ N( {: M6 H9 Y
  7. 96.0
复制代码

* o7 Q+ c  I3 w7 C5 d9 r2 Y) k+ gmath.log10(x)  返回x的以10为底的对数
" P, `9 N0 v- m1 B! ^4 [: [
  1. #返回x的以10为底的对数, z1 A. W* Y% t  P* [
  2. log10(x)
    $ |% d; {. r9 R. L$ b  ~; X- ^& e
  3. Return the base 10 logarithm of x.
    0 A: M2 b  r- c# U' i, y  ]
  4. >>> math.log10(10)
    7 |- D0 t7 n* G/ O* v' q
  5. 1.0
    0 @, ]. O# q* _5 s4 A0 _
  6. >>> math.log10(100)# N, f" u( @! ^" E" t
  7. 2.0
    ( K* y, ~* Q0 \% m6 c, a1 }8 B+ R
  8. #即10的1.3次方的结果为200 [7 u! _0 b9 _( ^1 h+ q
  9. >>> math.log10(20)
    + i& i1 O$ {6 G
  10. 1.3010299956639813
复制代码
' c. b/ G& O  ]5 d: P) T! G6 q
math.log1p(x)  返回x+1的自然对数(基数为e)的值5 \/ j  [  A7 B
  1. #返回x+1的自然对数(基数为e)的值
    3 a% K+ ]4 `6 _( ^3 a5 K
  2. log1p(x)
    2 _0 _+ c! }' G" Z# n3 T/ R
  3. Return the natural logarithm of 1+x (base e).' \( x  q( h+ c+ L; _+ }
  4. The result is computed in a way which is accurate for x near zero.
    ' \7 L5 D4 Q- R
  5. >>> math.log(10), e9 H4 z2 B) s, l+ h2 u: }
  6. 2.302585092994046( P8 d. {5 i1 _; r" r
  7. >>> math.log1p(10)( v: p" b5 k. `. \& y. J' }% D0 y
  8. 2.3978952727983707
    2 C9 M! b, ?& J, B
  9. >>> math.log(11)8 I& P3 t$ R* J7 `# l
  10. 2.3978952727983707
复制代码
' d5 v' U; B( R
math.log2(x)  返回x的基2对数6 f  t7 ]+ t* o1 B
  1. #返回x的基2对数
      ]) n5 X- l- \: N! F
  2. log2(x)) b  G! u, |# E" B9 {" W
  3. Return the base 2 logarithm of x.; s# |* M  Q" t1 p
  4. >>> math.log2(32)
    + E# n* \1 ~% ~/ I* c5 R" ^& Y
  5. 5.0
    + E" K2 }  O. z$ a. l
  6. >>> math.log2(20)
    4 y- y- M, ]3 m/ C; [2 }: `- y5 n
  7. 4.321928094887363
    ; a  X2 P7 j5 r
  8. >>> math.log2(16)
    / ^0 J4 b. i2 S* B3 K
  9. 4.0
复制代码
  B' P* R6 g2 f# M
math.modf(x)  返回由x的小数部分和整数部分组成的元组( S  p$ X1 h2 x! g2 x
  1. #返回由x的小数部分和整数部分组成的元组
    - e; l& ^$ h0 Z# p# O9 `3 \
  2. modf(x)& v3 g$ f' s! G/ ]
  3. Return the fractional and integer parts of x.  Both results carry the sign
    & ]6 e# g# c2 C1 W/ N  J4 W  Y
  4. of x and are floats.
    $ q$ c/ S' L& s; E. t
  5. >>> math.modf(math.pi)) W$ U, m% L6 v1 z% G6 H
  6. (0.14159265358979312, 3.0)
    ; ?% M! x3 R( B: j  M2 o/ e6 Y
  7. >>> math.modf(12.34). ]5 @5 s0 O# r% c2 O( X. i
  8. (0.33999999999999986, 12.0)
复制代码

3 |$ f3 ?9 S; g, ymath.sqrt(x)  求x的平方根; L+ L1 i- e6 e3 n  W5 Z  e1 ?6 A
  1. #求x的平方根. S; j, e# B9 B3 [
  2. sqrt(x)
    6 J8 @) C8 ?+ f% P, f2 k. S' o
  3. Return the square root of x.: e( o1 C! d" b: V
  4. >>> math.sqrt(100)
    4 ^% s, a3 o2 _" r- B  t
  5. 10.0( H/ l4 m' b$ K% W9 I5 v" o
  6. >>> math.sqrt(16)
    ) `1 ]2 [: ^$ F1 ?2 h( e9 E
  7. 4.0
    3 i$ v# k  A# @7 J
  8. >>> math.sqrt(20)# \1 i" G" [2 U
  9. 4.47213595499958
复制代码
. z& B. r5 g- H& U' W% k8 |2 g' V
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ( v2 N, ^9 ?% e" T: J6 K
  2. trunc(x:Real) -> Integral: C# i  e: E. [$ B6 _' y& S
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 r* f; C7 O% V# K( j' W
  4. >>> math.trunc(6.789)
    " V! ?% l* c5 H+ M
  5. 68 v$ W9 K- s* m& o3 v
  6. >>> math.trunc(math.pi)
    $ B/ ^- w) g+ A! U. w/ `* ]
  7. 3/ g  v5 x* B1 c, w" d7 i, Y* k
  8. >>> math.trunc(2.567)
    " P& _9 l* x! T$ ]" u# n
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-3 20:12 , Processed in 0.087424 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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