新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

9 w" A( |0 M. ?3 R: S& p* C【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. h( f# {9 j8 D% ^
* j+ {5 i: b' {$ X) [3 \, b
方法1
& A2 f; I0 v; s$ v0 M4 E
  1. >>> import math0 p$ a4 e  E: ^6 {0 x
  2. >>> math.sqrt(9)
    , K: c0 h) T9 Y9 E% p
  3. 3.0
复制代码
方法2
% [2 T* z+ |, }/ ~7 \4 K
  1. >>> from math import sqrt% b, o; l# z/ s
  2. >>> sqrt(9)! A8 p1 k. j6 F; J4 t+ I" A" ~
  3. 3.0
复制代码

( v4 }4 R* _: P2 h
5 s2 F7 o- @( z( w
math.e  表示一个常量0 C! m/ U: a# x2 _" o; x
  1. #表示一个常量
    ; F7 l; t$ `5 U& E4 `
  2. >>> math.e+ y) h8 |# L. R0 m; `' `! v0 r
  3. 2.718281828459045
复制代码
) R( \8 _* `) E# D# Y, ~
math.pi  
数字常量,圆周率

" q  S3 \0 [) U, a6 Y
  1. #数字常量,圆周率% Q8 C5 T& P3 x9 c
  2. >>> print(math.pi)2 T5 O( C8 }5 v% Y" A! h
  3. 3.141592653589793
复制代码

* s+ x0 e8 J! u! vmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 z# A7 K! ?; K  G
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x4 b' t. h/ Y& y; i) j; H* D
  2. ceil(x)
    * o) U& {6 S* v, y. G1 ~6 `) d, ^
  3. Return the ceiling of x as an int., [  b8 B6 m' z3 a( m2 m
  4. This is the smallest integral value >= x.- p- Q! ]  W+ V8 s' ^' _& j

  5. " a$ t; i. r$ D( [
  6. >>> math.ceil(4.01)# h9 G( @% E& I% m" j1 i0 d
  7. 5
    . `% S+ d6 K: |1 q
  8. >>> math.ceil(4.99)
    ' P. R0 S8 b4 _: B& I# e
  9. 5
    1 x1 d1 W6 N) Q+ J3 o
  10. >>> math.ceil(-3.99)
    : _! e' m- ~. g" q/ @
  11. -3! Y/ Z2 L" U2 b9 f! c
  12. >>> math.ceil(-3.01)$ }. f% U* ~* G' c4 y
  13. -3
复制代码

' D( _/ I% ~  d# c: }! j+ s5 |6 Z( y0 \math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身' ]1 h5 _1 H3 T- j" P+ }
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# e& m+ X: G# @2 b
  2. floor(x)1 X5 Q! M/ T1 F) {' B
  3. Return the floor of x as an int.: h( t. E' Y5 q+ [& E' Q
  4. This is the largest integral value <= x.
    6 z( G* a! K% v  T" V! E" H8 \
  5. >>> math.floor(4.1)4 a1 b/ t7 }7 H8 n* e. s/ l9 y
  6. 42 {4 k6 Y- }9 d
  7. >>> math.floor(4.999)/ d. }! j7 h/ s% e
  8. 4
    4 d8 a0 I6 I  P3 }4 J
  9. >>> math.floor(-4.999)
    8 D$ o" M+ D' L
  10. -5* m8 A# f# V) ?/ z2 r, p2 [
  11. >>> math.floor(-4.01)
    * O- n! _$ J$ g. Q: B( D: J
  12. -5
复制代码

5 T( _( L: X! s, omath.pow(x,y)  返回x的y次方,即x**y
& I) D, [  z2 P# K, F7 k
  1. #返回x的y次方,即x**y
    $ r' Q4 [* R" x4 t7 D
  2. pow(x, y)
    6 x0 X- C- h! j* g. q5 H6 C3 B
  3. Return x**y (x to the power of y).
    / w! f; g" f7 m7 l
  4. >>> math.pow(3,4)  l. c8 O+ i( N( E; m3 u  h
  5. 81.0
    + X! k3 w: M+ z4 ~0 i
  6. >>>
    3 s: V3 y1 c: V
  7. >>> math.pow(2,7)
    : [. g$ Q( Y4 J) y
  8. 128.0
复制代码

0 y' [) M( i" i% o, z& o2 Pmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& p3 e1 T7 c! r# R( G
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    * \* ?- ?" |; ?
  2. log(x[, base]); R+ X$ a6 b  C0 N% x% ?$ t$ _
  3. Return the logarithm of x to the given base.7 l# c& n0 p! b+ a. T% s  f
  4. If the base not specified, returns the natural logarithm (base e) of x./ x" ?3 O! }/ {- P! k9 N$ E+ ?  R
  5. >>> math.log(10)4 e1 C- s0 K/ H3 n
  6. 2.3025850929940468 S8 U" P0 f6 K8 i! H1 E
  7. >>> math.log(11)7 J6 L. }" t. {" y( t
  8. 2.3978952727983707, D7 u; p+ b1 z9 G9 C! u
  9. >>> math.log(20): i3 [. Y* U$ h  j: m6 V  H3 N. \- w
  10. 2.995732273553991
复制代码

6 ]* H; w. g# x/ r6 n4 l8 lmath.sin(x)  求x(x为弧度)的正弦值8 I3 c# @% ?* X1 t
  1. #求x(x为弧度)的正弦值
    ; d' G9 N6 p' m% U- H* G( F' w
  2. sin(x)
    - s8 X$ d+ v; s/ r9 V" {' j& y
  3. Return the sine of x (measured in radians).
    - s& c9 @5 g/ P+ }$ _! y
  4. >>> math.sin(math.pi/4)
    ! z. ]0 u* q3 w0 q
  5. 0.7071067811865475
      t% t& d9 U+ l0 f0 ^$ n
  6. >>> math.sin(math.pi/2): H: u+ V2 F( }" U* R1 |' @
  7. 1.03 d- G, F, l9 j; h6 x: J$ z
  8. >>> math.sin(math.pi/3)
    ' Y- V2 h  T! @: D1 Z) F" ~3 o& E
  9. 0.8660254037844386
复制代码
+ o& |( A6 H* _
math.cos(x)  求x的余弦,x必须是弧度
( ]9 V4 K* R( O$ _/ J
  1. #求x的余弦,x必须是弧度
    1 e# x" Z; ]& E1 ^2 j5 o8 ]
  2. cos(x)! P/ J% B6 @9 U4 V! e
  3. Return the cosine of x (measured in radians).
    . w' p2 ?  ~# R" q' T& x2 `5 b$ s
  4. #math.pi/4表示弧度,转换成角度为45度
    & X% N( U$ J- T2 c; U+ K8 B" f
  5. >>> math.cos(math.pi/4)7 C" o( h% ?( w6 ]+ e
  6. 0.7071067811865476
    6 n0 f9 ~; w3 \% z1 C( {
  7. math.pi/3表示弧度,转换成角度为60度
    * J/ W0 E) c  k: Z, _
  8. >>> math.cos(math.pi/3)5 }& p) w* }8 {! ?1 z
  9. 0.5000000000000001
    & W! p  U- F* y3 x, y
  10. math.pi/6表示弧度,转换成角度为30度: e2 R3 E1 Z" W- I/ Z9 U
  11. >>> math.cos(math.pi/6)# d8 X: ]7 h( M/ [9 `6 x
  12. 0.8660254037844387
复制代码

$ b9 q3 Y1 C' Wmath.tan(x)  返回x(x为弧度)的正切值
/ _; c- F& y( v! L( K
  1. #返回x(x为弧度)的正切值( P+ v: C6 i6 r/ [- z( }
  2. tan(x)& Z3 B6 H/ o3 ^3 m
  3. Return the tangent of x (measured in radians).
    8 q0 Z  V; c2 U& B* Y
  4. >>> math.tan(math.pi/4)3 d* c( @  p6 W" k! Z
  5. 0.9999999999999999
    ) N% V- c: M; }. B; C
  6. >>> math.tan(math.pi/6)
    3 O' C3 |% F3 o6 C( a# l
  7. 0.57735026918962579 l' J# Z/ R) ~* k0 r
  8. >>> math.tan(math.pi/3)6 }1 r- K# d! W; u) X/ c
  9. 1.7320508075688767
复制代码

9 p" N; ~3 \$ p/ l: ~4 W7 m! wmath.degrees(x)  把x从弧度转换成角度7 t; k1 K* r  t% R" O
  1. #把x从弧度转换成角度
    : V% I) y# b" U0 e
  2. degrees(x)
    * m! R* q" H, H  B9 S! P8 t$ k: h
  3. Convert angle x from radians to degrees.& l5 c9 U& [3 R
  4. % B1 a4 _$ T* W% o) C& v& z
  5. >>> math.degrees(math.pi/4)
    # [2 d. G4 g) j, I' q3 Y) ?" H
  6. 45.0  i: D2 X: y) D* R
  7. >>> math.degrees(math.pi)- e8 i3 {9 V. U6 Q
  8. 180.0
    ' F/ u8 l8 X+ f' w: K  F& Y
  9. >>> math.degrees(math.pi/6)! j/ w- n/ z5 F0 C8 P
  10. 29.999999999999996- J0 J; T- ~* C+ ~- w$ J% ?
  11. >>> math.degrees(math.pi/3)
      _$ B% }' X7 u. n( x8 D) R
  12. 59.99999999999999
复制代码
# \& m% s5 c- @7 E; e
math.radians(x)  把角度x转换成弧度0 R) i$ Q' n% \6 G; N5 m- @
  1. #把角度x转换成弧度
    ! b! ]% G. Y5 ]- l! C  ]
  2. radians(x)3 v8 m! ]& G2 H' u1 Z
  3. Convert angle x from degrees to radians.6 e- i; ^! |% I, d" Y
  4. >>> math.radians(45)
    / o) [* x0 s- u, H4 O
  5. 0.78539816339744838 x/ B9 s. ]- D' h- R: N5 h
  6. >>> math.radians(60)* h8 f) u( m  f) t
  7. 1.0471975511965976
复制代码
! D  o* E# I9 T& q
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
4 i0 q1 z6 k( H% i4 v0 Q. }! M
  1. #把y的正负号加到x前面,可以使用0' t* ^/ J$ A% M; L  R, c6 v
  2. copysign(x, y)
    ! T, S! {/ W6 [2 o+ Y; c& |* f
  3. Return a float with the magnitude (absolute value) of x but the sign 0 ]$ m. ^; ^' v+ ~7 |
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 w5 Q6 \. Y3 |- P8 }
  5. returns -1.0.9 U# ]' n3 C1 o% J, I6 R) b+ O

  6. $ E5 k2 h' F; E4 }
  7. >>> math.copysign(2,3)& b0 X. _; q. S/ ~9 K' V
  8. 2.0
    ' c; [3 @6 _4 i1 q4 q8 d! M
  9. >>> math.copysign(2,-3)# [1 ]5 X! [5 z0 B! T
  10. -2.0- ~+ T+ I# x- S5 ~9 v+ w
  11. >>> math.copysign(3,8)
    - T; X  D2 J% l) D
  12. 3.0$ K! H# R1 f/ @% t
  13. >>> math.copysign(3,-8)
    7 \9 P$ \* l& ?3 u; @3 X
  14. -3.0
复制代码
) E/ C) z- X7 H' X' h
math.exp(x)  返回math.e,也就是2.71828的x次方
+ K( Y* F! s. `! \
  1. #返回math.e,也就是2.71828的x次方& {$ {% p. _: R' f7 J: A3 s
  2. exp(x)5 K  R5 j; @3 U4 p  k9 y" u7 C4 f5 r
  3. Return e raised to the power of x.- D2 d, i; T% g+ N0 J
  4. # v8 [' S# Q( Q' @& c, A
  5. >>> math.exp(1)2 b6 m; J, \; v: J3 I6 N
  6. 2.7182818284590455 l, u, ~- F9 Y% z7 h0 B( D8 H
  7. >>> math.exp(2)
    # {* w. s- G4 q" n" Y% |9 t4 a1 c
  8. 7.38905609893065
    - [7 c/ T! [% f' \
  9. >>> math.exp(3)- r1 U* T) N& E
  10. 20.085536923187668
复制代码

: P" U& k/ h$ k, @% a( H: J: w+ zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1' l& v# _: \6 F
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ) A5 E0 {3 s  j5 o- Q, ~
  2. expm1(x)
    ! Q, g( T* n$ d8 M2 W7 I' z
  3. Return exp(x)-1.
    9 X7 Y+ \5 K! x8 p' H
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 ~# I& m! w' @5 n% `% {& d% h& `

  5. % A) @/ ~" z% ~& B1 i8 d% c( A. s
  6. >>> math.expm1(1)* f: j. F5 q' T0 E4 n
  7. 1.718281828459045
    ( t0 K+ z/ a+ u6 k) j( |
  8. >>> math.expm1(2)
    % T0 N3 }8 a8 f( u$ u* Y
  9. 6.38905609893065" T7 u; f$ {4 u: k" m. [
  10. >>> math.expm1(3)& ~* ?  L4 X7 @' d5 C/ k
  11. 19.085536923187668
复制代码
& O' ]+ S. j% B' N: u- _
math.fabs(x)  返回x的绝对值
' }2 [  I6 T+ x, P
  1. #返回x的绝对值$ O! H. S3 {( F" b: ~- y
  2. fabs(x)
    - h+ Z2 t: w# M0 T5 R, T4 p
  3. Return the absolute value of the float x.' u2 j5 r  I" g7 Z1 d
  4.   v, C; N3 Z: r2 A. j& c; ^
  5. >>> math.fabs(-0.003)
    & \) a. k. l7 c1 N0 p& Z1 O9 A) _
  6. 0.0031 s0 G$ _3 v( n# R( q
  7. >>> math.fabs(-110)) a# ~) O% X' ?( w& U. x
  8. 110.0
    ( v5 H; G0 z+ K+ Y! O& \: ~# z
  9. >>> math.fabs(100)
    * D9 ?8 O, ?5 @( p
  10. 100.0
复制代码
5 J2 N, i9 v  Z0 d# Y# M; V
math.factorial(x)  取x的阶乘的值
& U' ^+ r% k, ?1 k- z: x6 p
  1. #取x的阶乘的值
    ; X6 W+ ]- v" X3 ]4 K
  2. factorial(x) -> Integral
    & G( g6 m$ |5 C+ }
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ( H  X7 N( S$ R/ }: b1 _
  4. >>> math.factorial(1)
    * r# l: B- U% g, d
  5. 1
    $ G9 o* G$ _9 h7 s
  6. >>> math.factorial(2)8 L- O( G3 d1 j* X5 i6 w/ N
  7. 2( W; H) h* j0 x; ]+ V
  8. >>> math.factorial(3)
    ! [  i6 i8 u. n& \
  9. 64 m" a" ~# V* O
  10. >>> math.factorial(5)
    ! Z( e* s  y& G
  11. 120
    : q$ J7 ?  q/ o# s6 a
  12. >>> math.factorial(10)
    8 O0 l/ ]* f6 r" t
  13. 3628800
复制代码
7 m4 j2 q0 Y8 i& A9 t
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
2 D) D" G0 y8 d8 z% m
  1. #得到x/y的余数,其值是一个浮点数* J" r8 T4 S3 @+ T( s
  2. fmod(x, y)8 Z2 Y, T$ r7 j1 P- z
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + g8 e. W" I0 l$ V; ^
  4. >>> math.fmod(20,3)
    ; l' Z% {- @' \' _
  5. 2.08 E+ ]! H$ w" J: @' n, J% E
  6. >>> math.fmod(20,7)
    ; w% a) i1 f" r) v0 P1 K
  7. 6.0
复制代码
6 g0 _: c3 d: C8 _
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
. v  t) s; e8 s3 V7 @
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- p* ]1 O- R4 b* ]7 {0 [6 I! E% ~
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    # j: L$ F% Q$ R- E/ o4 S
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 I- o2 M( @" c5 u8 i* s
  4. frexp(x)
    3 M& ]* S3 j- ^
  5. Return the mantissa and exponent of x, as pair (m, e).* S6 b1 h$ V  W8 l5 q" w+ k$ ~4 H, \
  6. m is a float and e is an int, such that x = m * 2.**e.
    . W1 c9 P# l: a" D, a- J
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.: k  G5 d7 n( i* s9 u# Y
  8. >>> math.frexp(10)
    1 [! A8 u" f" X
  9. (0.625, 4)' |; {% u7 {& g/ E2 T
  10. >>> math.frexp(75)$ x3 x. Z' J9 L3 m$ Z/ m- `1 N
  11. (0.5859375, 7)
    ) A3 ~# H2 p' Q2 N# P( X, P
  12. >>> math.frexp(-40)
    : Y" V: x) ?. `4 Q* ]- ^- x; x, k. r
  13. (-0.625, 6)
    4 Q/ d+ ~* s. l( y4 t( i0 B
  14. >>> math.frexp(-100)
    7 b3 R& i5 V6 E# I! y7 Y
  15. (-0.78125, 7)
    . P  j  `7 t4 U+ f8 \+ t( l6 s, I
  16. >>> math.frexp(100)
    ! p; b! N5 g, d/ F) y! \
  17. (0.78125, 7)
复制代码
2 _+ F  D' j% O# t4 @/ Q
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
8 f  B! T8 |) h3 c' T9 q3 `
  1. #对迭代器里的每个元素进行求和操作% \* A% m# [! n; X+ o
  2. fsum(iterable)
    ) b5 \6 A$ D) @; q+ {9 k6 \' r: ^
  3. Return an accurate floating point sum of values in the iterable.6 l) E  C7 H' `& V) b
  4. Assumes IEEE-754 floating point arithmetic.
    0 W4 Q% q- }# y7 _" R) u% B* ?+ g2 m
  5. >>> math.fsum([1,2,3,4])7 Y& e* w9 X. E( k
  6. 10.0$ U" k' ^; V2 {7 i) n' U
  7. >>> math.fsum((1,2,3,4))
    ! L8 b3 c4 ]4 P! K2 }
  8. 10.0( I% u8 H0 X. H# Q& }! m  K
  9. >>> math.fsum((-1,-2,-3,-4))
    # M, H5 o6 O1 p3 v8 @
  10. -10.0# P. e. t# }* J. Q. Z$ O& d
  11. >>> math.fsum([-1,-2,-3,-4]): n( \' F& F! M& V( @% d0 p8 B; q
  12. -10.0
复制代码

, g+ y" f# ^* j" ^3 j6 q* v  l% Imath.gcd(x,y)  返回x和y的最大公约数
% [4 ]: ^) Y# R
  1. #返回x和y的最大公约数
    ! m' W5 e# S+ v. E
  2. gcd(x, y) -> int  P3 C/ O# p. G" G7 [
  3. greatest common divisor of x and y
    ' N) X* v: Y7 k; H6 D
  4. >>> math.gcd(8,6)
    6 y4 v) Y: Z2 f+ E8 D1 k
  5. 2
    0 {. L- ^7 R  H: [5 k. o8 R. a
  6. >>> math.gcd(40,20)6 l0 X3 p8 ]; D' f% b# B
  7. 20$ s! X6 k# x* t$ D+ }. b5 s- y
  8. >>> math.gcd(8,12)* P8 g" D# L3 K- h" v
  9. 4
复制代码

5 t% O- t2 d# d2 h  @" `math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
' y; z$ v* g: `3 Q* k
  1. #得到(x**2+y**2),平方的值6 s8 f& n, j( b8 S: j
  2. hypot(x, y)5 h5 D' X- C1 A( S7 B
  3. Return the Euclidean distance, sqrt(x*x + y*y).1 V5 J- M5 ?3 J$ k
  4. >>> math.hypot(3,4)
    0 U  _* N( I, B
  5. 5.0
    ! |4 J2 g  Q" b2 y' K' G
  6. >>> math.hypot(6,8)9 t, [. C4 Z9 I- L$ r% Y
  7. 10.0
复制代码

4 O4 z% R7 @" Imath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 Q" w4 K6 V# u8 Q6 H: ?. u' j
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    7 \3 o8 h) I3 u# u
  2. isfinite(x) -> bool. e. B6 t5 H  e1 z" ~7 ]# ?# w7 k
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    0 f& ^- `) b& K- n2 N; M- Z
  4. >>> math.isfinite(100)4 G- T5 r& o2 U9 R  ]2 z3 l
  5. True9 V& L& A/ k' [) O0 ]4 H0 l5 X
  6. >>> math.isfinite(0)& [3 l" L/ n2 _# h/ J2 c6 L0 I
  7. True
    * ]+ C: ^/ j& o1 F6 [
  8. >>> math.isfinite(0.1)1 u9 o: j9 r2 `4 Q8 E0 ^1 X8 \  T
  9. True. `" U* i* e% H2 }( @) q
  10. >>> math.isfinite("a")
    $ u9 [/ d' y3 A3 P( w% O( x( }
  11. >>> math.isfinite(0.0001)
    " J& p4 `1 ?" v4 A7 s
  12. True
复制代码
  k4 ~  c( U0 t& A! S
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False7 V3 }8 _& Y( ~/ |
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    7 L- z* q+ \; g  }6 g9 o1 i
  2. isinf(x) -> bool0 u: V6 c. z& B* o: \; q- J) a) I
  3. Return True if x is a positive or negative infinity, and False otherwise.$ E* u( V2 A5 m5 w6 {. z4 d
  4. >>> math.isinf(234), S0 q6 D7 t2 J0 g: W7 h
  5. False# C1 H" u" t0 b* n& c% }5 b
  6. >>> math.isinf(0.1)
    9 {9 K0 o4 @+ P/ l& V  ?, Z
  7. False
复制代码
3 u! [" L4 e+ U' ?% P, W
math.isnan(x)  如果x不是数字True,否则返回False% w' A* ~/ e" A6 U0 _
  1. #如果x不是数字True,否则返回False
    $ L; h9 Y$ z( Y5 Z* F
  2. isnan(x) -> bool3 l8 Z& }5 e( N- a7 }
  3. Return True if x is a NaN (not a number), and False otherwise.
    * r4 U" K' W1 r$ ?2 Q" U* y- i
  4. >>> math.isnan(23), T( \9 ]9 ^& W# {* w& t$ e/ o, X
  5. False, t) g3 h( m: B; j& ^) N
  6. >>> math.isnan(0.01)
    $ n! U  n0 ?% V- F; w. s" f/ @; X
  7. False
复制代码
/ }6 y6 x; i5 n  z! Z9 h
math.ldexp(x,i)  返回x*(2**i)的值( ?3 V! T2 q2 J# H* o) _* w
  1. #返回x*(2**i)的值
    $ c2 c/ }, T5 ^% ]: X' T, e
  2. ldexp(x, i)6 G, p0 ?/ Z# O) |+ j/ v
  3. Return x * (2**i).
    % q" Y4 e' T0 [0 l
  4. >>> math.ldexp(5,5)! D! f' g9 \( e) F
  5. 160.0
    , z, |% ^$ Y  x. [; ?
  6. >>> math.ldexp(3,5)
    / ~; l1 I6 t2 H. P; b* m
  7. 96.0
复制代码
& n! T/ I& F4 y# V1 x" r
math.log10(x)  返回x的以10为底的对数+ O- M% M  a8 Q- N* u8 x4 @: ?
  1. #返回x的以10为底的对数
    4 e1 \3 h& `4 s: Q
  2. log10(x)
    : }3 t& o4 n9 Q9 F% W1 @6 g) r/ s
  3. Return the base 10 logarithm of x., g: ]+ e! h8 T6 H1 i
  4. >>> math.log10(10)
    3 j2 E/ c; R/ l7 ?2 c
  5. 1.04 u' A( O8 b) @
  6. >>> math.log10(100)4 W7 m! Z7 a  M  [; h4 |) V
  7. 2.0! X( ?0 w4 A: j$ y1 e1 R$ Z
  8. #即10的1.3次方的结果为206 H" c: d5 `. l& C0 F! y
  9. >>> math.log10(20)) S" O- \& F+ @% A6 e+ F. b
  10. 1.3010299956639813
复制代码
- r1 X: C  Y- @' f
math.log1p(x)  返回x+1的自然对数(基数为e)的值
( A9 v, E: K6 r  L; x2 @* I
  1. #返回x+1的自然对数(基数为e)的值
    9 V6 m& X( R: `0 r
  2. log1p(x)
    ; E" I7 g4 ?6 ]+ L: j& _# T
  3. Return the natural logarithm of 1+x (base e).
    + T' Y6 p+ O* {. i# _
  4. The result is computed in a way which is accurate for x near zero.
    ( b. i" c* P: ~$ o5 Z
  5. >>> math.log(10)
    # ~+ i, G: O. ^' \8 m- p# Z. J
  6. 2.302585092994046* Z! e  u7 L4 m% t8 r( K2 P. o
  7. >>> math.log1p(10)
    7 `, H8 F* |) I$ c9 Q1 i0 }
  8. 2.3978952727983707
    9 b: D8 P" I3 z" A3 H, R* Z
  9. >>> math.log(11)
    ! p$ [, ^& ]% H3 k( b' |' Q
  10. 2.3978952727983707
复制代码
- X7 F" _" @8 A; a0 \; O. {* T
math.log2(x)  返回x的基2对数
+ _* o4 U0 u  \0 ^2 z2 \& |
  1. #返回x的基2对数. @/ P$ V( b9 o1 h( K7 W
  2. log2(x)3 N5 Z- I9 r7 y8 `( ^. l
  3. Return the base 2 logarithm of x.
    1 @- C! j- ~. \% `( p
  4. >>> math.log2(32)) U; X2 ^" O" r' g- b
  5. 5.0
    ) v% y" ~& N% @7 A
  6. >>> math.log2(20)
    . w% a( u( g; U4 E* }
  7. 4.321928094887363; X( @" U9 H  g& V: u
  8. >>> math.log2(16)
    8 `/ T9 v, I, z9 g6 K  I
  9. 4.0
复制代码
5 M! t9 F  x6 y  Q. h
math.modf(x)  返回由x的小数部分和整数部分组成的元组
! ~4 Y2 q' W/ T
  1. #返回由x的小数部分和整数部分组成的元组
    ) X& ?% Y9 J  V+ g/ B4 u
  2. modf(x)& r  }) [* s2 `, s/ K9 D! @7 o
  3. Return the fractional and integer parts of x.  Both results carry the sign
    3 G9 D" @( _% f! J- w  [
  4. of x and are floats./ R! E9 _: M/ O3 o! [9 u
  5. >>> math.modf(math.pi)
    9 f. M2 P& }. m* k, M& ]
  6. (0.14159265358979312, 3.0)' @/ S/ x  I! s! z; z* w/ t0 K& P
  7. >>> math.modf(12.34)
    ' L- }( H; T5 V7 Z* s  U, S
  8. (0.33999999999999986, 12.0)
复制代码
9 A3 S5 g- E* k: ^
math.sqrt(x)  求x的平方根
, s0 k* @; H* T" ?
  1. #求x的平方根
    * V% M4 Y, L9 r- ~& _' s$ X# |
  2. sqrt(x)
    2 |5 S& h; Y  x" y/ |  ~
  3. Return the square root of x.8 i4 z' @/ A) o! H* N* P2 s. U
  4. >>> math.sqrt(100)
    $ @3 j  c8 P0 `3 S/ d0 k( D
  5. 10.0
    & J* u7 i' q% d) K* _
  6. >>> math.sqrt(16)
    - u1 m7 P, i% j& l7 q3 O
  7. 4.02 ]  W, a8 P5 D/ L* s$ ]
  8. >>> math.sqrt(20)9 p6 M# T4 F; v5 ?6 f4 `5 v  N- S
  9. 4.47213595499958
复制代码
, A9 b0 A! [; A0 t( T( H# T" B
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    + ?7 j9 Y3 D' i% |. J
  2. trunc(x:Real) -> Integral
    # I' J: ?7 Q5 a4 s' d
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    # j0 N8 [) K! O5 Y2 z5 t% O
  4. >>> math.trunc(6.789)
    . C5 s4 Z( I- T- T* ]0 N
  5. 64 x& T4 h2 I/ N8 u2 V
  6. >>> math.trunc(math.pi)$ b) S% O; H8 Y& h7 z/ x: U3 @
  7. 3
    - X+ H6 h- ?. H3 f( |5 M# Z& I
  8. >>> math.trunc(2.567)
    2 ]: b* x) A4 Y; N( M* V
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

浏览过的版块

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-28 12:30 , Processed in 0.086346 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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