新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
0 p. J5 o0 G  h: ?0 ?8 ]( ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
8 L* f2 W% ^) ?! A5 ~" H
! E" x3 b# g% t8 J/ |; B2 g% Z3 ?
方法1( h& }7 }, T4 Z
  1. >>> import math
    9 F) l1 f( H7 ?- ]
  2. >>> math.sqrt(9)
      \. V8 d- I0 e$ P
  3. 3.0
复制代码
方法2
8 S9 o* G  ^2 m7 Y
  1. >>> from math import sqrt
    / M0 r0 d/ W+ C5 Y5 [1 G" N
  2. >>> sqrt(9)0 G* V' z8 p( Q) |0 d# J9 o& ?# x
  3. 3.0
复制代码

( q2 d. z5 T  q4 @6 Z0 F, \
8 c0 \; D: z, {5 g8 K, ?  `5 b' L
math.e  表示一个常量
  J6 f# b+ \  f
  1. #表示一个常量, i0 X- a& _- E+ X
  2. >>> math.e1 {4 i' T0 ^5 y; y
  3. 2.718281828459045
复制代码

3 V( J1 @' G8 j* V2 H+ _math.pi  
数字常量,圆周率
  K! R3 u! J1 n+ k4 ~
  1. #数字常量,圆周率
    % c* {3 V$ x6 b
  2. >>> print(math.pi)
    - l# n' L& Z4 g3 A* D# K6 `. I
  3. 3.141592653589793
复制代码
. e7 ~% q8 d2 g- @* B9 h& G; L
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
) d3 C; q: ~6 p3 ^/ ]9 L
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    % ?. x0 [/ Z' e" n: |, ?# h2 p2 P$ `
  2. ceil(x)
    : O" e; T4 {4 Y/ z2 e3 k
  3. Return the ceiling of x as an int.3 s5 ^2 ~# G3 ~5 I  {$ K
  4. This is the smallest integral value >= x.
    + X; U9 U) z1 F( W, E

  5. * D$ P/ m5 x  ?$ z; Y+ ]4 B; n# `
  6. >>> math.ceil(4.01)
    ! ]1 A' Y4 n; M4 ^
  7. 5: G  Q& t6 T3 x
  8. >>> math.ceil(4.99)
    ! o5 @# v( W1 ^$ s/ |5 s' _8 S
  9. 5* w1 }3 b# T) E
  10. >>> math.ceil(-3.99)' x  _- I4 \$ x( R0 V- L3 }
  11. -39 v! ^7 G, Y' x6 V
  12. >>> math.ceil(-3.01)
    2 |7 b/ @% y! p
  13. -3
复制代码
* e' m; H" S& X* f7 n8 }
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) X0 {+ R9 z+ i# e4 Z6 Z4 ?
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    $ M+ T, b/ b$ c/ U8 S
  2. floor(x)# F* y" y, J) L
  3. Return the floor of x as an int.8 Y, J2 @! O1 y% k- @* r$ B) u/ X
  4. This is the largest integral value <= x.
    7 p9 M- q+ `6 j$ J5 M
  5. >>> math.floor(4.1)
    0 z7 s) I$ c& u* j1 s# }" a
  6. 47 ], Q* g) \. ~) o9 t: C
  7. >>> math.floor(4.999)
      z" [9 ]3 k) C) J7 \
  8. 4
    1 f; I! \- q/ n6 u4 Y" h0 x1 d: k  z
  9. >>> math.floor(-4.999). }7 n2 r% l# C, v# `4 z
  10. -5
    . j( v4 i3 d  h0 ^( V% R. p) P
  11. >>> math.floor(-4.01)
    2 z. C7 A: u1 S5 i. L+ R+ m' m
  12. -5
复制代码

) X" H- K, m6 R8 v: H8 L0 T4 ?- imath.pow(x,y)  返回x的y次方,即x**y
3 L! N$ F" J9 x8 _1 W7 u
  1. #返回x的y次方,即x**y
    7 y# Z5 x' v8 l; ~9 `+ r+ X5 v& k. ~
  2. pow(x, y)
    . v; s' k" P3 ~; _7 y. W
  3. Return x**y (x to the power of y).
      f. }+ u, T- j7 U, q) `
  4. >>> math.pow(3,4)! f7 d$ i& ]* I4 q; J1 N% o
  5. 81.0! U- P9 P9 I: I  g8 v
  6. >>>
    6 x% |2 S3 z% V
  7. >>> math.pow(2,7). w5 O5 K- E' v" J- C
  8. 128.0
复制代码
: a! v/ w: n" |' h
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
; o2 B3 R6 b7 }) A
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ) L. }0 s, f  @
  2. log(x[, base])
    3 Z6 n, T( _. ?# a: G
  3. Return the logarithm of x to the given base.
    $ t& Y9 Z( _- E
  4. If the base not specified, returns the natural logarithm (base e) of x.
    9 M; o. m9 x8 C7 D5 M; Y
  5. >>> math.log(10)
    9 T, ~2 Q7 N9 f/ W/ m
  6. 2.302585092994046
    & ^1 K! O7 x7 @: X- {' m0 [, V
  7. >>> math.log(11)
    / H; u! c5 `' B) y2 L, v
  8. 2.39789527279837077 [) b* z  o4 v8 z7 Q
  9. >>> math.log(20)" F5 |$ U* [  L9 G  s( @  \! _
  10. 2.995732273553991
复制代码
9 r2 W1 D) w, R+ I( r9 ~2 Q
math.sin(x)  求x(x为弧度)的正弦值, o3 {4 N% Z  h/ _4 A/ c4 H
  1. #求x(x为弧度)的正弦值
    % r2 a5 L; R* T$ M* F6 ?
  2. sin(x)
    ' t3 x$ I; a4 D1 G1 l4 b4 b
  3. Return the sine of x (measured in radians).
    : I4 M  }, ?$ R' I( M1 ~
  4. >>> math.sin(math.pi/4)6 _9 a% S6 n2 b8 @9 I6 Y
  5. 0.7071067811865475
    + O7 N, k% a, {, d6 Z
  6. >>> math.sin(math.pi/2)" Y' i0 l5 i& F6 N0 l
  7. 1.09 }* Y& Z! m3 F
  8. >>> math.sin(math.pi/3)
    # ]* n; t/ B1 e& u- ]. J" t$ C" p! D
  9. 0.8660254037844386
复制代码

/ r' O1 C+ |7 n' emath.cos(x)  求x的余弦,x必须是弧度
2 N' c4 b' w( ], \6 Q0 ?2 |
  1. #求x的余弦,x必须是弧度
    1 I* c& r' p  j  D6 j
  2. cos(x)+ G7 l- u1 q1 m. w% C
  3. Return the cosine of x (measured in radians).
    # w8 y+ X8 S5 Y4 D8 a& B
  4. #math.pi/4表示弧度,转换成角度为45度6 o+ L7 f* |, o+ V1 T
  5. >>> math.cos(math.pi/4): T) M3 c' f! T5 e
  6. 0.7071067811865476" _# |1 }" w, b6 T7 `# _- P, K/ n
  7. math.pi/3表示弧度,转换成角度为60度
    4 e- d0 Z/ n5 G0 e/ }
  8. >>> math.cos(math.pi/3)  v/ L4 L+ r) A/ N% b/ E1 _/ E
  9. 0.50000000000000019 z, ?% _# L# W7 }
  10. math.pi/6表示弧度,转换成角度为30度: c! H7 r1 P- e6 N1 ?+ d
  11. >>> math.cos(math.pi/6)
    . F9 {5 h  k( ?6 H& P$ Z% L( a
  12. 0.8660254037844387
复制代码

' G& @6 t( ~3 _0 [math.tan(x)  返回x(x为弧度)的正切值
! i" ]- D8 B; k% T6 g4 S
  1. #返回x(x为弧度)的正切值5 e  z" q4 L$ p6 Y
  2. tan(x)9 K0 O& Y) A9 N
  3. Return the tangent of x (measured in radians).* w' c8 @& p3 r, {' f1 K: |. R
  4. >>> math.tan(math.pi/4); i4 |+ z0 H: v2 o+ V3 ?3 }
  5. 0.99999999999999991 m& N; N0 ^+ E8 e
  6. >>> math.tan(math.pi/6)" {6 s2 u4 i4 |- |- P0 @1 A3 V
  7. 0.5773502691896257
    7 M+ a2 m* a& S: u' J5 ]
  8. >>> math.tan(math.pi/3)- a" C6 B# t) T  a
  9. 1.7320508075688767
复制代码

! [3 M1 G$ Y! Amath.degrees(x)  把x从弧度转换成角度1 }4 S  Q3 |9 q# [8 D7 `- J) W# d
  1. #把x从弧度转换成角度+ w2 c- i6 X" R* }6 Y
  2. degrees(x)
    & h  ^# A  S& o; b
  3. Convert angle x from radians to degrees.3 h% R8 S* ^: A* p

  4. , T( D' X, V& M! G/ j$ d  C( }
  5. >>> math.degrees(math.pi/4)
    " L- t/ o% f! g4 }, d% C
  6. 45.09 @( \5 E: f3 s( S
  7. >>> math.degrees(math.pi)
    5 T; |" `% J) C: i7 S* ]) a6 q* g/ N
  8. 180.0; _  J. O/ @- c  ?5 P
  9. >>> math.degrees(math.pi/6)3 @/ J) z- r: `/ |- A/ q  K
  10. 29.9999999999999966 W. @, V* b- N! B
  11. >>> math.degrees(math.pi/3)
    ; M1 [; Z. ~3 \
  12. 59.99999999999999
复制代码

& d: q) j% G; A2 smath.radians(x)  把角度x转换成弧度- Q/ d8 v7 f3 H: N, p
  1. #把角度x转换成弧度( P) I, s7 a- D' _8 K' Y( q
  2. radians(x)7 y' o0 v$ ~1 b& H' T7 W8 C
  3. Convert angle x from degrees to radians.
    2 A7 X8 V3 I4 x+ Z0 |, f
  4. >>> math.radians(45)
    ( G) P+ n8 K) s' U( O, Z
  5. 0.7853981633974483& B: a5 }8 y! ^1 X3 I9 s
  6. >>> math.radians(60)
    $ A: v+ e! _; s$ w
  7. 1.0471975511965976
复制代码
+ V! O% t; ?- F" G
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
. K1 t7 g  U1 X
  1. #把y的正负号加到x前面,可以使用0
    & l# ~6 d* y5 Q9 L( c' g2 Q3 M
  2. copysign(x, y)
    6 I( Y+ O9 u2 W1 X; q' @
  3. Return a float with the magnitude (absolute value) of x but the sign ' y# W9 A( I5 x$ U9 A
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) . g: _% Q. `+ V4 [6 l2 f
  5. returns -1.0.
    - {! E; K9 Q% i2 g  h. J

  6. & X+ Z6 B4 K3 f, W# @6 v
  7. >>> math.copysign(2,3)
    + G) V/ l3 x% k1 R) p6 b. K1 }
  8. 2.05 {  Y, l( m% q4 z* Y
  9. >>> math.copysign(2,-3)
    ) _1 C+ D: }7 j2 w' R
  10. -2.0! J9 }: f& d6 d1 D) S
  11. >>> math.copysign(3,8)1 n1 `# M7 E: H2 e! k( }) O2 D: K/ A
  12. 3.0( n! g% A2 Q6 L! n# P4 `
  13. >>> math.copysign(3,-8)
    6 K/ l( i8 u) ]( A, k
  14. -3.0
复制代码
# R+ k; K# z8 K9 C: q1 h" N, v
math.exp(x)  返回math.e,也就是2.71828的x次方
6 J8 d$ l( I3 v' R7 ~0 U0 J
  1. #返回math.e,也就是2.71828的x次方
    5 \  V' i+ T* n7 s
  2. exp(x)4 l2 S( ^- }: R$ w, S: J9 o3 @
  3. Return e raised to the power of x.! m& [- @( Z  }$ ?
  4. ! r- x. U5 @8 b! }! Y  f0 S
  5. >>> math.exp(1). t! \% T: P9 u2 C
  6. 2.7182818284590458 z/ F6 N! X5 z0 L
  7. >>> math.exp(2)
    / }8 C: [9 X- h5 c
  8. 7.38905609893065
    9 y8 k* x% U0 }2 u! D& k3 x
  9. >>> math.exp(3)
    2 y, N$ m* |# R" a
  10. 20.085536923187668
复制代码
' z' s' ]/ X) p2 X+ R' ?. x
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
/ s0 T) r- R& k/ b; A* q
  1. #返回math.e的x(其值为2.71828)次方的值减1" L1 U1 L$ [3 U! Y; \0 W7 o+ F
  2. expm1(x)- \# |7 x. H0 K% h. [
  3. Return exp(x)-1.
    , X) y4 k! H" T0 b# P
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    7 k  \, Y" ]3 V# k# P& a' U4 D

  5. 7 K5 p4 e: t* N6 I) {
  6. >>> math.expm1(1)
    ( l8 N$ x7 L* m6 q
  7. 1.718281828459045
    , I0 l6 f8 H, q
  8. >>> math.expm1(2): y3 s/ ~0 L8 V  G
  9. 6.38905609893065
    % y0 _. i2 q+ N" j
  10. >>> math.expm1(3)! f  m7 Z2 r( q" _' @/ @$ Y7 E
  11. 19.085536923187668
复制代码

% _$ G: Q- U1 ]* o+ V! C2 P' f0 smath.fabs(x)  返回x的绝对值
& \! x9 K/ @. y& X6 }
  1. #返回x的绝对值& h6 n1 `3 V5 ~4 h0 K4 t" D6 s
  2. fabs(x)
    5 @" W9 z0 [3 |  B8 _
  3. Return the absolute value of the float x.% W1 G" o: b6 O% h( a/ e
  4. & ]9 g. O( C, ?, g. `% k% Q
  5. >>> math.fabs(-0.003)
    ( ~7 ~& L. P4 N0 l: B5 F1 P
  6. 0.003) t. q3 O3 y" G1 j
  7. >>> math.fabs(-110)
    9 m0 I  f3 \; L: x
  8. 110.00 W0 H  @; \2 Y  N; r" e, E
  9. >>> math.fabs(100)0 y/ l$ e9 y" t
  10. 100.0
复制代码

6 Z; u! h: }, j) u8 h$ U3 smath.factorial(x)  取x的阶乘的值+ R" z+ X: A4 D! n
  1. #取x的阶乘的值
      J  U3 E( Y/ R  d; s* v" R6 L' M
  2. factorial(x) -> Integral
    . Z9 N9 P; `( z2 B/ b
  3. Find x!. Raise a ValueError if x is negative or non-integral.  l# D1 |) ]/ n0 W
  4. >>> math.factorial(1)
    / x% o0 i2 g/ ~' I7 h" a
  5. 13 u3 j0 w7 T3 l6 {' a
  6. >>> math.factorial(2), Y; ]* q6 G0 ]' m6 H0 b: c
  7. 2: B! G. C9 [8 Y4 B  b( U: r: c$ A4 p
  8. >>> math.factorial(3)
    , }" \9 j$ T/ I9 C, ^2 T
  9. 6
    # y) w% j6 l# {. M
  10. >>> math.factorial(5)
      z5 O2 c( Q( g& q% H: X5 N
  11. 120; C! P5 Q' F) J, ^
  12. >>> math.factorial(10)+ s# X; `6 O* L# y" @% r  Q
  13. 3628800
复制代码

; ^" h  ^2 e) t, cmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数. M" x/ U& q, \, ^: b3 u
  1. #得到x/y的余数,其值是一个浮点数
    # L1 d0 K. m& k5 d2 W' @5 `
  2. fmod(x, y)% W: o9 h6 k% o( I' e
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    : \7 \/ _# X! i; s+ V4 o
  4. >>> math.fmod(20,3)! W  q* y0 |, c* N1 v2 w# N3 n
  5. 2.0
    0 {" L0 `& S+ U" t: }( z
  6. >>> math.fmod(20,7)9 R' y) g, u+ b' F1 K0 e
  7. 6.0
复制代码

' r% x& ^" @' [math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! f; }- D6 q( z% s
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,6 H% ^! ~2 m+ C
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; _# q3 Z2 F4 ]* {5 J% p
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' Y0 p- k# J- j" q$ T
  4. frexp(x); _6 s4 n: V& b; u' Z- D2 M3 V$ `
  5. Return the mantissa and exponent of x, as pair (m, e).
    ' d# _/ W; Q3 A8 i- h
  6. m is a float and e is an int, such that x = m * 2.**e./ R9 a5 m& n$ a1 {* |7 l
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.# J1 Z! p+ m2 F& l( m4 \
  8. >>> math.frexp(10)  M4 s" _/ ?( J
  9. (0.625, 4)  T: N/ D2 k5 _! B# e4 r
  10. >>> math.frexp(75)8 P' k. R) P1 I$ ^0 w
  11. (0.5859375, 7)  g: e8 D: \: G2 @
  12. >>> math.frexp(-40)3 y4 \2 m  l' N) y7 o
  13. (-0.625, 6)1 K' }, N$ j& \7 D* W9 r+ ^9 H. p
  14. >>> math.frexp(-100)  ]  L) H; m# t
  15. (-0.78125, 7)" l- n& h, n! D- t8 c; I, A/ _# q
  16. >>> math.frexp(100)
    8 s/ X4 K: @4 ]3 Q" T8 ^6 ?
  17. (0.78125, 7)
复制代码
: L' N% {  z4 }' `3 k* p
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
$ B0 L3 D7 \" w- m
  1. #对迭代器里的每个元素进行求和操作
    % X" B! S; b& h" c6 Z& P# v# P
  2. fsum(iterable)( M3 F( R  X) a% _: F- g
  3. Return an accurate floating point sum of values in the iterable.
    2 V$ H3 w: j' z  r: ]- F( d& F) r
  4. Assumes IEEE-754 floating point arithmetic.9 {- Y& E+ R  H, H1 ], d
  5. >>> math.fsum([1,2,3,4])8 G( r$ ~( d/ [, i
  6. 10.0
    8 H3 c' ~2 g( o2 `
  7. >>> math.fsum((1,2,3,4))
    / S6 h) Z  c- c8 }* w
  8. 10.0% [$ b3 S; {& X5 f
  9. >>> math.fsum((-1,-2,-3,-4))
    + o' m/ ~4 g! _6 L
  10. -10.0" a" n5 Y% f. I% S) s
  11. >>> math.fsum([-1,-2,-3,-4])! z. @8 i2 Q! P% p
  12. -10.0
复制代码

9 c5 c/ V& h6 v2 |( _: Hmath.gcd(x,y)  返回x和y的最大公约数& d  A% o3 q+ Q+ \1 z
  1. #返回x和y的最大公约数8 O# n- I; {. s8 P8 f! g1 {# s
  2. gcd(x, y) -> int
    4 o  X; T6 T& t# l# Z) R; a) r, z
  3. greatest common divisor of x and y" ?5 U% l* r: K8 V/ W/ o. V8 }
  4. >>> math.gcd(8,6)
    & c; U; H& F( r2 S: C
  5. 2% @9 ^( L1 c, U" x" r- N
  6. >>> math.gcd(40,20)
    - ?* |! a% O) X8 E6 i
  7. 205 |( e* `1 n5 Z
  8. >>> math.gcd(8,12)  P9 l9 ]/ O" ~( C5 u" H
  9. 4
复制代码

  ~% B2 n5 N, kmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
% @! x: K+ }3 V0 z5 h
  1. #得到(x**2+y**2),平方的值
    2 u. S7 O1 O" L: L, d
  2. hypot(x, y)
    . N$ g+ v* w; C- {
  3. Return the Euclidean distance, sqrt(x*x + y*y).. P, m7 }0 }( z6 p1 P/ D
  4. >>> math.hypot(3,4)
    ' W0 o" w. F6 K6 w  i
  5. 5.0
    + W) J  ]- l2 T/ y
  6. >>> math.hypot(6,8)
    9 S8 u, w! C* m, E- ^' K/ i
  7. 10.0
复制代码
0 U! L2 [, c4 f( D
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
' D0 f" X. A+ b( W# w
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
      @# M/ Z7 J1 h, H# j
  2. isfinite(x) -> bool1 X/ u' U9 e2 O
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.2 s! p; ?9 l8 z1 L# z3 S0 N
  4. >>> math.isfinite(100)) ?, Y' h# _( N# J( _3 y* _
  5. True
    ' j1 G$ e7 o* r6 g' Z5 ^8 s; g/ Y, A
  6. >>> math.isfinite(0)3 Y2 l! _; H0 V
  7. True
    * G; {" F$ N# Q% a0 r( D2 m
  8. >>> math.isfinite(0.1); V. j+ ^5 X0 O$ u
  9. True) a; e: X4 W0 ]3 g& `. x
  10. >>> math.isfinite("a")
    ' d1 q( p* ?- @9 x$ E: i) q4 Z
  11. >>> math.isfinite(0.0001)7 ~; V) M' A% p( V) I2 o3 ~6 K" Q
  12. True
复制代码
$ |& b! H/ \. n3 l9 u
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False4 m; f2 ?( ?4 C4 W$ ^
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ' B  m+ Y/ U/ X# u
  2. isinf(x) -> bool
    % o8 i  w2 e* \- l
  3. Return True if x is a positive or negative infinity, and False otherwise.
    . l4 d' t, l* t. q; D& S8 l
  4. >>> math.isinf(234)
    6 u: @$ n% ]5 ]4 @( n! {
  5. False  q8 j  V: F+ k0 _. R" Q5 R
  6. >>> math.isinf(0.1)3 F# z1 ~% N  B! J  M' n
  7. False
复制代码
- {- D; y" }6 ]( Y. a3 ^
math.isnan(x)  如果x不是数字True,否则返回False
  R" ]  p  C" N% o
  1. #如果x不是数字True,否则返回False
    * Z' @# i) n: m$ {
  2. isnan(x) -> bool
    . S7 `& p/ [; H3 D( v5 M
  3. Return True if x is a NaN (not a number), and False otherwise.
    % }) b7 V8 X0 F( l4 q( ?; U, x
  4. >>> math.isnan(23)) R; ]; U: l4 S' [# `% y
  5. False
    + t% Z5 j/ V. ?' s) q+ |" f
  6. >>> math.isnan(0.01)
    + Y* ?; T* i* u0 T
  7. False
复制代码

( V+ b, C9 R8 j* i# O) Zmath.ldexp(x,i)  返回x*(2**i)的值
" @# O. V6 a: l2 I% h! k
  1. #返回x*(2**i)的值# J3 Z1 Y/ b: E$ g, x0 c
  2. ldexp(x, i)
    1 p8 b1 ^" j, X. |. D' o' i
  3. Return x * (2**i).- l# _2 m7 v  f  K
  4. >>> math.ldexp(5,5)' Y2 ~3 |" h6 M
  5. 160.03 p  e, L! {# q
  6. >>> math.ldexp(3,5)
    9 q( Y/ x! o5 [
  7. 96.0
复制代码
' j7 K+ q1 u) h" f. ^, e( r* M* S& o
math.log10(x)  返回x的以10为底的对数& V  d# ~- v4 d% n% h; S# [
  1. #返回x的以10为底的对数
    1 k3 A: t: D" S, ~+ a& H/ J: B8 q3 [
  2. log10(x)3 a5 b. c7 t6 l
  3. Return the base 10 logarithm of x.5 g& _! t/ a  Y- f; F$ Y( ?, X* H9 C
  4. >>> math.log10(10)
    8 p, P: {1 M1 w8 g& j9 s2 m
  5. 1.0
    % h: B# b8 z: G( r
  6. >>> math.log10(100)+ @6 q7 A. d/ J, V& {9 K
  7. 2.02 Q$ @8 w! G4 q" R; t: C3 X( ?
  8. #即10的1.3次方的结果为20
    * C) ~3 m) V. H7 T* |" H5 v
  9. >>> math.log10(20)
    2 ^" F2 ^- e. z0 _- C
  10. 1.3010299956639813
复制代码
& F( c: Q# S0 H' |& v' \7 m
math.log1p(x)  返回x+1的自然对数(基数为e)的值; W! V! i# A% e/ e
  1. #返回x+1的自然对数(基数为e)的值% d/ y% z" t, n" [+ Y0 I5 c# K* K. h
  2. log1p(x)
      A- h, J$ D& c3 ]7 ?6 @, i
  3. Return the natural logarithm of 1+x (base e).
    9 C/ L% N3 X8 N  A$ p$ R9 l
  4. The result is computed in a way which is accurate for x near zero.2 W8 ^- c9 ~& n
  5. >>> math.log(10)2 N: F5 l; e0 m
  6. 2.302585092994046
    2 L3 S  s- a& P. b/ y+ X
  7. >>> math.log1p(10)
    5 t# x' }7 M( S( I* H$ y1 X
  8. 2.3978952727983707
    0 g5 m+ M4 E" ^. L' O# i
  9. >>> math.log(11)/ [8 w7 t9 {) b; p2 x
  10. 2.3978952727983707
复制代码
: n6 S3 Z2 U( F3 a! C+ H  G
math.log2(x)  返回x的基2对数
  T% w2 h( D% f0 r) H6 k
  1. #返回x的基2对数
    9 k' S: _& L- x& i* P3 d% r. F
  2. log2(x)
    " X- x; T. t! |5 T# G
  3. Return the base 2 logarithm of x." b+ U4 a+ X& |' x5 M) Z' R
  4. >>> math.log2(32)
    0 T+ J8 V1 _$ b  _" S. r( Z
  5. 5.0
    : A; m3 k! H6 N6 j5 ^7 O
  6. >>> math.log2(20); m; a: b$ m2 P1 O
  7. 4.321928094887363! ~' w% c' R  A  K: A  F9 K
  8. >>> math.log2(16)6 T  t+ F; G0 B: C  {  o; t
  9. 4.0
复制代码
) O% r- U: J7 e- U( Z, R  b; S4 h
math.modf(x)  返回由x的小数部分和整数部分组成的元组# u, p2 _' X' u$ ~* `
  1. #返回由x的小数部分和整数部分组成的元组9 X4 A' b+ K& Q/ R. @/ |  C# M
  2. modf(x)6 W6 K, K0 E& x
  3. Return the fractional and integer parts of x.  Both results carry the sign
    1 |2 B* v; W1 c' n
  4. of x and are floats.
    9 m2 t$ D/ L0 a! `; J( @0 V4 Z
  5. >>> math.modf(math.pi)/ `" z4 {6 ]9 |3 ?! v
  6. (0.14159265358979312, 3.0)- ?6 h+ q+ f/ l
  7. >>> math.modf(12.34)
    % y* f! H& E0 R, Z
  8. (0.33999999999999986, 12.0)
复制代码

$ J( G3 j4 o5 Mmath.sqrt(x)  求x的平方根
4 o' O7 D+ U/ B9 T  K5 A7 s1 @7 |
  1. #求x的平方根
    3 z' {, C3 V- U" J- R& @) j
  2. sqrt(x)$ L. ?; a( W' t, N. [/ f
  3. Return the square root of x.
    - b0 Q6 V, ?& U6 b' u* M* Q: Z& s
  4. >>> math.sqrt(100)( J# E: }# M  |! R/ ]
  5. 10.0, m* V0 ?; ~4 ?* q4 d& @! b. S
  6. >>> math.sqrt(16)
    3 u  w' Z! G7 L, Y8 }
  7. 4.0
    " R) B5 n0 N$ ~# @2 @2 R
  8. >>> math.sqrt(20)$ l6 Y/ S/ `  U' n" F
  9. 4.47213595499958
复制代码

# k. g# d  r( p1 k  lmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    9 H4 l6 l  m. y1 b
  2. trunc(x:Real) -> Integral
    : N. x. ~9 Q4 W6 _4 F: L* |7 W. x( o4 l
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.7 f7 y% l$ D5 Y5 j6 {9 u; t
  4. >>> math.trunc(6.789)4 A3 q0 J- ]1 Z% G( Z- L
  5. 68 u* |" B$ L6 U1 d% z
  6. >>> math.trunc(math.pi)
    % r: C" p: z$ S! P( W" h! V
  7. 3
    8 ^( R% H; L' i+ S% b( f
  8. >>> math.trunc(2.567)
      t* w( e$ V* b7 `
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-7-2 07:13 , Processed in 0.077450 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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