新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
3 K! }) w( ?: K! e/ g
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- Z, f$ n0 n6 H! o

( W- Y7 h  J* h$ f  q1 _方法1
& d  T. Z' ?6 t2 ]9 ^- d3 [# C: u
  1. >>> import math0 B) a- i! t- q6 B
  2. >>> math.sqrt(9)
    , [  L5 a6 j# w* z
  3. 3.0
复制代码
方法2, m& s" G; m" Q. o) h
  1. >>> from math import sqrt  Z4 U5 j  ?0 r$ j* O
  2. >>> sqrt(9)
    / a2 n6 V, b' _( s/ P" ~; O% I) I; |
  3. 3.0
复制代码
3 }# S3 V% J2 }


. m( X/ N( B1 s. K3 H
math.e  表示一个常量
1 {7 f7 b0 }7 d- e( m3 e
  1. #表示一个常量
    7 c& x! l& v. j, z: j) Z
  2. >>> math.e/ M$ n/ [5 d0 B# H$ j  h' s
  3. 2.718281828459045
复制代码
; Y. X) V* u+ ^* l
math.pi  
数字常量,圆周率

2 o5 q" c( y& p: k* \
  1. #数字常量,圆周率
    5 b- C6 d1 W5 }" J% t6 T3 L
  2. >>> print(math.pi)0 Z- x$ P, i- c( P
  3. 3.141592653589793
复制代码

. M& M* R6 C' B8 h% F0 A: I6 T9 cmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
( q' A4 [% g) j  ]" ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    9 K1 Z# {' l7 Z# d1 D
  2. ceil(x)& j. |; x& n* u) ~2 t: p( H# n
  3. Return the ceiling of x as an int.
    . q. S5 `, p' A/ V( W  A
  4. This is the smallest integral value >= x.
    7 b9 c2 P4 `1 F
  5. # N# G9 ?0 E: C! ]1 o- Y2 k
  6. >>> math.ceil(4.01)
    " @# A* J( W! B. S
  7. 58 z- t/ L* Y3 Y1 |
  8. >>> math.ceil(4.99)
    ( f' Y, k1 B, B
  9. 5; Y  J5 T8 I1 T! T2 b1 t
  10. >>> math.ceil(-3.99)7 |  N1 S/ I. F) W- u, k- H
  11. -3
    7 T* Z; |9 o( _! P. k; o# d
  12. >>> math.ceil(-3.01)
    ; L* _1 O  N; S
  13. -3
复制代码
6 W1 J' Q9 Z+ z* S. N+ Y) d: L0 T
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# X$ W: n9 Y6 h; E- K/ n
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    0 D5 K# h( e! {2 W$ P
  2. floor(x)7 d+ C4 K  K% H; D- p+ n  }
  3. Return the floor of x as an int.
    1 W) s) J" Z2 N) j5 v0 ]
  4. This is the largest integral value <= x.- T! j2 o) ~$ I: q) Y6 A% Q" \
  5. >>> math.floor(4.1)- K4 J4 L* x* q/ K2 d% P2 n% Z
  6. 4
    ; |9 B4 t3 r$ j) b# H& D
  7. >>> math.floor(4.999)" o7 ]+ i% V1 Z- R7 b) [
  8. 4! y' \/ H, s$ T* e. x9 y
  9. >>> math.floor(-4.999)
    " g8 P4 u4 e/ v! `  P" T# w
  10. -5! _- q9 O$ f) M& |
  11. >>> math.floor(-4.01)! F8 [6 Y+ |4 E9 k4 I' e  F9 c
  12. -5
复制代码

1 J5 }: P2 O7 d: D  ^math.pow(x,y)  返回x的y次方,即x**y
: x& n- _2 }, g% Q1 u/ l& J
  1. #返回x的y次方,即x**y
    % A, j. E- ]' h; |5 b
  2. pow(x, y)
    ; l$ F0 a8 }* c2 b
  3. Return x**y (x to the power of y).7 d. h" U6 g! e
  4. >>> math.pow(3,4)  H' A: L# ^! W3 Y
  5. 81.0+ ~+ h1 k) `0 g/ W& w4 O
  6. >>> 6 U( p* g- e; U) M3 J
  7. >>> math.pow(2,7)  Q, ^9 o& V0 m
  8. 128.0
复制代码

( V; {% h3 F/ {) {0 h$ Pmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 C; E7 z* {5 C. a% |
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ l: o' x3 F$ M. L- A( k) c
  2. log(x[, base])
    , c/ G$ c& H( {# u2 h* W
  3. Return the logarithm of x to the given base.
    + _- {/ H* K% c$ `, W- P4 T
  4. If the base not specified, returns the natural logarithm (base e) of x.$ `2 u6 ^! q: T6 o0 O
  5. >>> math.log(10)
    . p8 Z. Z4 G' R! i/ |
  6. 2.302585092994046
    1 D8 N0 T2 R+ K/ z3 L
  7. >>> math.log(11)
    9 J" l. K- x8 O" Q. g% M7 Q
  8. 2.3978952727983707, w+ W& W4 {5 u8 D
  9. >>> math.log(20)
    $ `) u, d# M, E7 t2 D  x
  10. 2.995732273553991
复制代码
( b( t8 y  q" K  ~/ r8 ]4 {; M7 w  C* h
math.sin(x)  求x(x为弧度)的正弦值4 t7 D% Z9 Z( Y. G+ r3 ~- Q/ }
  1. #求x(x为弧度)的正弦值
    & Y# e: L. s0 {' Y
  2. sin(x): e' r( ]6 N; I0 H9 {7 f3 d
  3. Return the sine of x (measured in radians)./ Z9 x% x; |/ m# U/ g, v1 R
  4. >>> math.sin(math.pi/4)2 P- p( c. e, R) C; ~( N2 M- n
  5. 0.7071067811865475/ `  u8 R6 ^8 B- H: W
  6. >>> math.sin(math.pi/2)7 k+ n8 Y7 A" J3 n
  7. 1.02 i/ C( t0 u! s- c4 ~
  8. >>> math.sin(math.pi/3)
    1 D% n  c, F, r8 }/ c) C1 }: T
  9. 0.8660254037844386
复制代码

& z' [2 V; X/ p) C/ Z1 l! p) Wmath.cos(x)  求x的余弦,x必须是弧度6 f# W$ p. G) I
  1. #求x的余弦,x必须是弧度
    5 g3 U; z4 {4 o; V' Q' I' H5 L3 a
  2. cos(x)9 Z1 f; ]! T- W4 P3 J; f
  3. Return the cosine of x (measured in radians).' g/ u0 m6 o4 K# l# a! r4 _+ |
  4. #math.pi/4表示弧度,转换成角度为45度
    5 V/ \& g6 u4 `0 p+ {9 s3 F, Q
  5. >>> math.cos(math.pi/4)
    , Q& F9 g& Y9 |8 l  B: h- D
  6. 0.7071067811865476# j1 [+ ]" t$ y" q
  7. math.pi/3表示弧度,转换成角度为60度5 l$ B- c! P& C: V; y: |
  8. >>> math.cos(math.pi/3)) S9 b! H- v7 k8 d4 C* H
  9. 0.5000000000000001
    , M. o2 x. H7 ~
  10. math.pi/6表示弧度,转换成角度为30度
    : [, }/ ^% z5 l7 t- Z( i$ B
  11. >>> math.cos(math.pi/6)* c/ P+ R) U6 C
  12. 0.8660254037844387
复制代码
( D# u1 ?( d5 J8 G. ~' R
math.tan(x)  返回x(x为弧度)的正切值
7 |+ V4 t5 {$ f
  1. #返回x(x为弧度)的正切值
    ! b1 A, }  K2 M! N8 ~
  2. tan(x)9 \/ O1 x! h6 V0 T, Y4 x
  3. Return the tangent of x (measured in radians).
    " a* g4 F$ K( B6 `6 r
  4. >>> math.tan(math.pi/4)0 V* @4 W. s$ J: @! l" s( A+ U' J! R
  5. 0.9999999999999999
    8 m, y6 p3 n+ \, x" c. M4 i
  6. >>> math.tan(math.pi/6)- l4 }& i4 }+ S: S, h; G
  7. 0.5773502691896257
    ( Y/ h3 A6 a# A* q  u
  8. >>> math.tan(math.pi/3)
    8 Q4 d8 T3 ~" y* s9 u6 g  ?, C4 v2 q+ `
  9. 1.7320508075688767
复制代码

' z% d9 K" `' Omath.degrees(x)  把x从弧度转换成角度
6 a/ i  u' P- e9 r0 A) j9 Q
  1. #把x从弧度转换成角度$ P. ~2 V- U, @) {8 q
  2. degrees(x)/ v* `$ l4 S) p; f0 r' U
  3. Convert angle x from radians to degrees.9 `+ d# T3 V; \' t
  4. ) m' y5 A  s' h* ]/ N
  5. >>> math.degrees(math.pi/4)  A6 C0 ?: {% V  K* g' O9 n  _
  6. 45.0+ H8 R/ b7 ~2 m2 R2 t7 F
  7. >>> math.degrees(math.pi)
    " P6 {8 _+ S$ P4 \* A& u5 R# J
  8. 180.0/ h# F: S; g( M% x$ w& M
  9. >>> math.degrees(math.pi/6)
    % O) ~" T% q6 m7 T: s) f* R
  10. 29.999999999999996
    " C  I; a. P' r; y) E% n. h5 `: {
  11. >>> math.degrees(math.pi/3)
    / D) H) v$ J( @8 t; F. D
  12. 59.99999999999999
复制代码

. P0 S2 I8 S. t' e% wmath.radians(x)  把角度x转换成弧度
( v4 [; W* n+ B' \! F
  1. #把角度x转换成弧度
    " Y) n. t; b9 ^0 H
  2. radians(x)* Q' M1 E& Z  M& p9 \  g0 Q1 g! x
  3. Convert angle x from degrees to radians.
    1 [  t3 J" Z9 w
  4. >>> math.radians(45)/ T7 e$ @9 o6 T! n9 d+ d2 V! ~# |
  5. 0.78539816339744833 I1 D2 W% v0 s  s9 f6 e
  6. >>> math.radians(60)( R, u/ }% T6 f* O% d! h- i5 X
  7. 1.0471975511965976
复制代码

: P2 p- F  F6 Cmath.copysign(x,y)  把y的正负号加到x前面,可以使用04 O2 s& i' Q! U- j$ ?
  1. #把y的正负号加到x前面,可以使用0
    6 P1 R7 y# ]- w- }7 Q' a
  2. copysign(x, y)
    & {; }6 ?( R6 N2 b+ i+ I
  3. Return a float with the magnitude (absolute value) of x but the sign + q; n# n3 P/ I7 l& p; c6 ~$ j( e
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , M7 {  |$ @0 W  ^8 C/ k2 _
  5. returns -1.0.# P1 ^" o2 P; ]/ R+ m+ q
  6. - {% z; V4 n  p+ }, O' U! C, B+ l( [
  7. >>> math.copysign(2,3)
    & X2 d# u, Z3 m) Y: a9 I" e: ^1 z
  8. 2.0
    1 a$ K+ P1 N9 Y% L( k  F$ p5 d
  9. >>> math.copysign(2,-3)$ U& z4 k6 _/ |0 d
  10. -2.0
    * I& u4 A1 v$ W; A% q, ]
  11. >>> math.copysign(3,8)
    1 H# R" m; S& S' H2 W4 l2 u
  12. 3.0% K. i' h. y4 g6 G2 N
  13. >>> math.copysign(3,-8)% q8 y5 a" v; z8 w
  14. -3.0
复制代码

1 k1 H* I; ]) k' {3 }* L1 zmath.exp(x)  返回math.e,也就是2.71828的x次方7 m% z6 u6 J  a0 j9 J8 ?& a
  1. #返回math.e,也就是2.71828的x次方
    % c, Y" W. }3 d4 Y
  2. exp(x)" G, V+ F/ c, A1 p* C: K& K
  3. Return e raised to the power of x.' G' {! Q" w; K- A; P" G+ ?
  4. 3 e# n$ ?0 H3 N9 J6 z% u4 o
  5. >>> math.exp(1)
    : b3 y/ U7 Y, o: a( r6 K' \, T- O% D
  6. 2.718281828459045* F: E* \( D  m! y( S
  7. >>> math.exp(2)1 W$ f+ L9 c; f, |: t( k8 |
  8. 7.38905609893065: C4 `: s9 U, s2 r5 M1 c
  9. >>> math.exp(3)# R# ^7 q- s0 z0 y* T  g# F
  10. 20.085536923187668
复制代码

# C6 L6 B: I1 N6 \( Omath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 ~' e# R% T" C3 P- Y" n% v
  1. #返回math.e的x(其值为2.71828)次方的值减1
      l: h/ H* Q) N, F5 @9 q% C% Q. }
  2. expm1(x)8 d* `; h( {; t' x& P7 |
  3. Return exp(x)-1.
    9 ~1 c( D% ]+ K* r
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ( g/ n! S  Z( |4 C4 Y

  5. + ^5 _2 m4 y/ q  |; Y, z" d/ ^) q
  6. >>> math.expm1(1)- ^7 h9 |0 @7 _
  7. 1.718281828459045
    " M" g' h, Q% o' f2 q
  8. >>> math.expm1(2)
    ! ]; |# ~3 A5 w" ]  t
  9. 6.38905609893065/ O' W" }8 S' X1 X8 \2 i
  10. >>> math.expm1(3)& q3 N3 h8 C0 w5 T  O8 R
  11. 19.085536923187668
复制代码

6 i& n/ B. t  D& G0 K" q9 _math.fabs(x)  返回x的绝对值0 Y( l9 n: C2 K& U% @
  1. #返回x的绝对值0 R, l# S; e/ m) O2 S
  2. fabs(x)
    * E( b  W  [; J/ k8 `
  3. Return the absolute value of the float x.
    + P+ f( \6 V" J+ l% z

  4. 1 ]( f! y. V5 k2 l" [# Z
  5. >>> math.fabs(-0.003)
    ; ?8 E3 x5 n8 Q$ L
  6. 0.0031 F4 N# c4 A+ a: R  T7 }
  7. >>> math.fabs(-110)4 ]* P) N0 a0 I9 n
  8. 110.0
    * F! k. h% _# c: _
  9. >>> math.fabs(100)
    0 G: Q) q2 a- X& o) R7 B1 I7 q
  10. 100.0
复制代码
7 U, H+ T: R7 c5 \5 B, H7 Z  ~+ i
math.factorial(x)  取x的阶乘的值
+ @( k  u9 O! U$ q7 b2 d
  1. #取x的阶乘的值' s6 S! i0 R- b8 K4 S
  2. factorial(x) -> Integral5 f/ u4 o. I6 i8 A: E- M
  3. Find x!. Raise a ValueError if x is negative or non-integral.5 Q% j2 M1 p; |+ r  u; q
  4. >>> math.factorial(1)
    / Q* u7 S* |+ n( I
  5. 1
    ) y  [+ N! g4 \- p9 i
  6. >>> math.factorial(2)0 K, R* S8 H4 c) Y' x4 o
  7. 29 \( }! b4 O, H3 h
  8. >>> math.factorial(3)$ {) B7 I# U$ E; T
  9. 6
    . G# @- }9 m3 r3 M- b
  10. >>> math.factorial(5)
    " X5 B9 P/ B! O8 Z* y# w
  11. 1201 d7 X# \9 Y$ ]0 G8 q
  12. >>> math.factorial(10)0 W7 \, m# t; y0 a' r/ A
  13. 3628800
复制代码
' b- j1 |) [3 t3 S
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
9 l: L; o8 f7 u9 J
  1. #得到x/y的余数,其值是一个浮点数
    ; O- E* B7 j- x; f$ C
  2. fmod(x, y)
    1 y- j+ D' x/ M7 E
  3. Return fmod(x, y), according to platform C.  x % y may differ.9 L, @4 c* k/ {$ s6 R6 n; P
  4. >>> math.fmod(20,3)
    2 P. X% _% H; l3 A
  5. 2.0
    $ ^' T2 X' D5 O- s
  6. >>> math.fmod(20,7)* l0 d4 a! s, o, k9 M2 I: q# h
  7. 6.0
复制代码

3 B) j- r( r3 i* o4 h1 Mmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2 a% R5 Z) p- m) _* W; O
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    0 i( L  P0 H0 E/ m" l. |) x
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: B# s9 }- y, ]
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 U) q9 h3 s$ ]8 ~8 Y. @
  4. frexp(x)! y! i/ n& ]- `. b- Q* s
  5. Return the mantissa and exponent of x, as pair (m, e).
    7 g) C; v0 {3 E  S  H
  6. m is a float and e is an int, such that x = m * 2.**e.
    : \  S4 |7 @8 U1 w; f: t7 n
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    % J. Y( S7 z, A
  8. >>> math.frexp(10)
    1 j& C, h4 S& A7 G' p2 ?
  9. (0.625, 4)3 J' Q+ q* z& ~- \
  10. >>> math.frexp(75)4 i6 c) n# G2 I3 W# }& N
  11. (0.5859375, 7)
    # Y. M7 c7 @, t* R
  12. >>> math.frexp(-40)& U2 B! C0 o1 A4 a5 P% i
  13. (-0.625, 6)
    ' d$ h% \2 g1 I9 w% }7 t
  14. >>> math.frexp(-100)
    1 X3 ?& K  E* d4 ]
  15. (-0.78125, 7)
    . T% q( V- m& M3 K
  16. >>> math.frexp(100). [# {8 I) S/ }4 F
  17. (0.78125, 7)
复制代码
) P: |& X% f1 V" K% j) t
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
2 c4 \* N; Q3 R4 Y
  1. #对迭代器里的每个元素进行求和操作
    / K  w' S. e8 G- |; v; Z
  2. fsum(iterable)7 F& ~* }; G# e* [% X- k
  3. Return an accurate floating point sum of values in the iterable.4 M2 h5 D# Z: {* ^1 u( t
  4. Assumes IEEE-754 floating point arithmetic.
    4 h1 |* m3 f& w1 I3 x5 Z  M
  5. >>> math.fsum([1,2,3,4])
    ' }, W  B0 x6 O1 h3 ]; i: O; l( a/ {
  6. 10.0
    / ~9 K" _0 }% n1 z# z/ f
  7. >>> math.fsum((1,2,3,4)), G4 ?5 O# D* W0 b
  8. 10.0
    4 g; U1 X9 R. \  P: S
  9. >>> math.fsum((-1,-2,-3,-4))7 Z  w, }+ Q* i! K- l( A  o# O
  10. -10.09 Q( q! i0 _, _: a, H) i( S* M
  11. >>> math.fsum([-1,-2,-3,-4])
    1 _, a& k7 q" o0 Q' `
  12. -10.0
复制代码
. K; `) a1 G2 `+ B+ ^
math.gcd(x,y)  返回x和y的最大公约数
" R) i, w9 ]- {8 T1 l$ t8 s8 m/ B
  1. #返回x和y的最大公约数' Z6 |6 S' c3 K
  2. gcd(x, y) -> int
    0 }9 O- U2 l/ Q, X4 ]4 U
  3. greatest common divisor of x and y2 ~) Q7 m" c7 `% k1 K  O
  4. >>> math.gcd(8,6)
    1 l1 l7 a1 f) r% Q5 S
  5. 2
    ( c. e- N, T. _. k) S; u* Z' l, |
  6. >>> math.gcd(40,20)
    , M! ]6 I& t7 b! f  q6 h; T
  7. 208 @' y( a3 c. x! F; B# k! L1 o" }
  8. >>> math.gcd(8,12)3 \: Y, P" A3 f0 \5 h; U" m9 n; {
  9. 4
复制代码

1 ]# S9 ]" Q5 d0 Ymath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
; x6 X9 l. O8 I7 w( n
  1. #得到(x**2+y**2),平方的值
    # h! x4 d8 n8 [* z
  2. hypot(x, y)
      m) |- ~. k% @  k& A! m% f
  3. Return the Euclidean distance, sqrt(x*x + y*y).! g$ g( W2 k; W2 h7 D) l) T
  4. >>> math.hypot(3,4)
    ) {9 ]) {0 `8 L0 h
  5. 5.08 q! l, ?& \: [# A
  6. >>> math.hypot(6,8)
    3 ^% w; g  ~, ~' X! _2 \6 N1 u
  7. 10.0
复制代码
, ?" r$ f3 z" y0 t* s
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False  h% f6 D* I+ t' w
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    2 E; S7 e9 {6 u( ?
  2. isfinite(x) -> bool, z$ R1 W- ^! {% u" q! L$ ~
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    % e) ^4 h! x4 q* R. s
  4. >>> math.isfinite(100)
    6 L* e% m9 L: y6 s. v, C1 @5 e
  5. True
    + t. F' ^0 m8 q# y# e! C2 }0 M
  6. >>> math.isfinite(0)) Y, i. T. d1 X! h' V6 z* p
  7. True3 Q; v# C. A& S
  8. >>> math.isfinite(0.1)8 P. l5 `. w$ i0 I
  9. True
      i  H5 ?) W6 b- \8 J" Q
  10. >>> math.isfinite("a")0 ^6 s3 B0 }& L$ Y4 M! v/ d& _
  11. >>> math.isfinite(0.0001)2 m  z0 F' z9 s; |( I- z
  12. True
复制代码

6 W! s9 Q% E9 a) Xmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
/ ?; b% Q3 i/ |
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False" S. D9 f2 W4 ^# m$ Y# q
  2. isinf(x) -> bool
    ) i9 @9 H# U+ G+ m3 ]( Q
  3. Return True if x is a positive or negative infinity, and False otherwise." v5 e8 m' e0 W& |3 }/ a
  4. >>> math.isinf(234)" m9 K+ U7 D  s- t6 K# J
  5. False
    . s% E% o2 G% i4 H9 q
  6. >>> math.isinf(0.1)
    1 j$ h+ L' o' q
  7. False
复制代码

- ^5 ~. E9 c* N& Xmath.isnan(x)  如果x不是数字True,否则返回False
7 ?- [" G4 h) d9 [7 |
  1. #如果x不是数字True,否则返回False
    * }' F7 _- k2 t9 Q) O7 X
  2. isnan(x) -> bool. f' E0 I* r6 X  Z
  3. Return True if x is a NaN (not a number), and False otherwise.( A/ D& c9 j( N7 y
  4. >>> math.isnan(23)' O' g8 J. U" [; S$ Q
  5. False
    ! k" w, r/ c5 Q6 M& g! S8 B
  6. >>> math.isnan(0.01)9 N1 Q& t; F+ d  X* o
  7. False
复制代码
1 H! z8 ~3 i0 Y% G7 A" p9 x
math.ldexp(x,i)  返回x*(2**i)的值. m& `, h' d  K) |/ o
  1. #返回x*(2**i)的值
    ; w7 i( ~" q' r, R; w6 [
  2. ldexp(x, i)
    ' s: m3 \( ?8 n# r2 n0 \) _6 u
  3. Return x * (2**i).
    " K8 K1 ]$ e8 Q: V' A
  4. >>> math.ldexp(5,5)
    9 s1 i3 i! g6 N, }/ {3 f
  5. 160.0
    7 K% t/ P/ |5 w% @1 m$ R
  6. >>> math.ldexp(3,5)
    7 \# @5 U& z) E+ M
  7. 96.0
复制代码
' {. W3 y- m# q0 Q$ s5 q% T
math.log10(x)  返回x的以10为底的对数. U; A; {6 Y8 ^
  1. #返回x的以10为底的对数
    6 h; l' m1 m5 v0 V! @2 \
  2. log10(x)
    3 E3 a0 v1 [$ J8 k4 N6 K( ~8 R
  3. Return the base 10 logarithm of x.
    6 {& E- U; f& Y$ j+ L
  4. >>> math.log10(10)2 L% Y1 g* W7 W' I
  5. 1.0
    ( f5 I, f0 x+ O' d' `* I
  6. >>> math.log10(100)
    $ r6 O% W& x" Y
  7. 2.0: ?3 @+ {# Y3 I# o% I
  8. #即10的1.3次方的结果为20
    8 R: @+ }7 ^. D3 K. I5 T
  9. >>> math.log10(20)
    4 b4 A& `" q+ P2 R- e& \! H9 E
  10. 1.3010299956639813
复制代码

, r) b& u( C) ]: amath.log1p(x)  返回x+1的自然对数(基数为e)的值
! ~" g, L4 v! h" G. e$ b8 ?
  1. #返回x+1的自然对数(基数为e)的值" N6 K6 n, N1 h$ ?" q
  2. log1p(x)- R! |$ r3 _: o% j5 m" m# _
  3. Return the natural logarithm of 1+x (base e).
    6 L8 Q5 S" f( [# o9 q
  4. The result is computed in a way which is accurate for x near zero.
    3 E7 s6 X) e& [0 ^: O" ?3 P/ E1 F1 M: q
  5. >>> math.log(10)
    " v% ^8 h) U: P4 D/ W+ A
  6. 2.302585092994046
    $ g) d: }* N( u: Q
  7. >>> math.log1p(10)
    ) b4 z7 }$ L5 B! e
  8. 2.3978952727983707
    , T9 S1 k7 q, z6 m% L, k
  9. >>> math.log(11)8 w( P- r& T! V, R
  10. 2.3978952727983707
复制代码
% `2 }& g$ r- M" G' j1 L. v$ z  W1 y
math.log2(x)  返回x的基2对数; I! S& S/ E3 }7 m( K
  1. #返回x的基2对数
    - Z/ h4 P: B9 Y# S, U2 V* G5 y/ B
  2. log2(x). g4 Y% O2 `4 N* y, J# V! b9 m
  3. Return the base 2 logarithm of x.
    ) L5 R. ?  ^1 t# x$ N2 z
  4. >>> math.log2(32). k9 b' z% d! F  p% @+ a. _
  5. 5.0. x& t; |% \3 }* ]6 w+ D7 l
  6. >>> math.log2(20)3 R/ a) c5 n( f
  7. 4.3219280948873636 X/ W$ E% A5 S2 r' T% t: X
  8. >>> math.log2(16)1 _7 V, o! h# e0 C' y4 U
  9. 4.0
复制代码
3 l* Z' B9 R$ P" r' i# J4 e2 c6 r" X
math.modf(x)  返回由x的小数部分和整数部分组成的元组
2 C- N. A( ]! t% Y7 {* A: k
  1. #返回由x的小数部分和整数部分组成的元组# f  B; Y/ c6 k
  2. modf(x)# a! A+ e; @9 H# F$ c* O/ p6 t
  3. Return the fractional and integer parts of x.  Both results carry the sign
    $ A* k7 b4 A. s3 c- Z6 y6 J
  4. of x and are floats.# W+ Q7 P& C8 x& H# s4 l. x' Y
  5. >>> math.modf(math.pi)4 L) [2 o; e4 Q6 A; R) `
  6. (0.14159265358979312, 3.0)4 J' w  W& s* m, T. b2 B
  7. >>> math.modf(12.34)
    * ^+ S6 q+ t# N, r, _
  8. (0.33999999999999986, 12.0)
复制代码

: ]& a: o$ B+ ?0 a8 i4 I( Zmath.sqrt(x)  求x的平方根2 m8 b% q8 n8 Y# @# B0 y( L
  1. #求x的平方根: c' U5 e3 s6 k9 m; H9 f- V- ]% w
  2. sqrt(x)
      b; T* ]; z% S; T6 T
  3. Return the square root of x.
    2 w6 d, g0 h; L3 e( t5 I
  4. >>> math.sqrt(100)
    8 h5 ]4 G9 A0 r& Z
  5. 10.0
    # |6 ]4 Y, W$ _9 N' Q* D- o
  6. >>> math.sqrt(16)
    $ ^$ K/ Y5 D" O* h$ c
  7. 4.05 E* V3 W0 @" @4 S) M% [) R/ |
  8. >>> math.sqrt(20)7 X& |! V( s' R
  9. 4.47213595499958
复制代码
" p# x" e. }% M
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分9 f9 C7 j7 {4 R  Q+ r  V2 P- ]' ]
  2. trunc(x:Real) -> Integral
    ( n+ [! A5 g$ t( i% l* n0 j9 y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    0 d7 A! p: E  X
  4. >>> math.trunc(6.789)
    : a& I7 ?1 ]' g8 M5 M. `/ k
  5. 6/ t5 W. ?( J! [1 M3 ~$ A' V: l3 `: O
  6. >>> math.trunc(math.pi)
    4 d- X+ f: }+ ]1 u2 m$ P
  7. 3. T/ |9 ~7 a* y, I! ~; Q: G  B
  8. >>> math.trunc(2.567)1 ?' v2 M7 O  |' E7 f8 }, q% E
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-3 18:08 , Processed in 0.084564 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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