新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
2 [. J. R+ N9 C
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ p1 `5 F" x% R/ D) e- G9 v, R
4 ~+ d9 P& B, L+ X9 [, S  @
方法1/ g% J+ k0 C/ R
  1. >>> import math5 U$ G/ Y1 e$ k. `
  2. >>> math.sqrt(9)
    + A. n# m8 m5 D
  3. 3.0
复制代码
方法2
6 }0 d2 e. b! q
  1. >>> from math import sqrt
    % G. S) K  ?* T9 q
  2. >>> sqrt(9)1 H5 w% ^. q6 v1 E
  3. 3.0
复制代码

7 `! L4 p, }8 j0 q1 o
0 J5 L' {1 P  L2 R  r! r. R9 C' H
math.e  表示一个常量
& S- l+ B7 X) T" X1 k# ^. G
  1. #表示一个常量+ p+ M; U! x, J% v' |. u
  2. >>> math.e
      |! H4 p  k2 ]5 _: W$ U
  3. 2.718281828459045
复制代码

/ ?4 @# O6 F. a# O9 R0 tmath.pi  
数字常量,圆周率

* E9 J* Q5 g: w8 _# {: ^
  1. #数字常量,圆周率
    , s6 n0 }, I4 v
  2. >>> print(math.pi)
    ' c% O/ Q; }% l9 C
  3. 3.141592653589793
复制代码

& i! Y- U7 ?# Z/ I0 T3 b+ Jmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 j: _# A- x  i- H
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    , o/ d+ o! o4 P% m; N, a3 T8 O. D+ k- t
  2. ceil(x)
    . H5 A5 k* |: M5 R: h" Z
  3. Return the ceiling of x as an int.: y/ [# y( {1 c) s, j5 f4 M5 p
  4. This is the smallest integral value >= x.
    8 o+ [  i* S/ i3 J
  5. 0 u2 G  i. k0 f( T) Y3 Y
  6. >>> math.ceil(4.01): E) X3 H8 V" ~7 Y4 ^; A. v
  7. 5" K9 q- }/ B7 s0 e% S, H
  8. >>> math.ceil(4.99)8 b; N0 D  T1 n* ~4 |: t6 @; R
  9. 5
    : k7 k0 a) l6 ?; \$ b
  10. >>> math.ceil(-3.99)+ a  y: [& a2 K8 w
  11. -3
    4 S+ U+ K8 ~# Z4 m4 P9 z& F
  12. >>> math.ceil(-3.01). t- R% b' }2 N* K9 a
  13. -3
复制代码

' x6 h1 H1 m9 n7 p' A* Gmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( ^% L; j: U6 T  ]
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ' h! w% j$ d8 z6 S. s3 r
  2. floor(x)
    9 W- \6 B: X  V
  3. Return the floor of x as an int.9 R1 z7 c6 S" }
  4. This is the largest integral value <= x.
    9 c7 _! K9 F; J' D( N0 b5 }* H9 o
  5. >>> math.floor(4.1)
    4 ^9 h  A9 _* d9 L9 l' T
  6. 4& V3 L1 Z6 y/ i
  7. >>> math.floor(4.999)
    & m. T' w" H* \, K3 w& B7 t
  8. 4/ N! @; ~* `# j" S6 h* D( F
  9. >>> math.floor(-4.999)
    $ M: y9 f( r# w) J5 X4 X1 V
  10. -59 y* A0 E, P/ [" j  ?* ]0 f
  11. >>> math.floor(-4.01)5 `; [0 P1 B" @' z8 A% q+ k
  12. -5
复制代码
. O* [6 D% k5 @" N7 `2 b4 z
math.pow(x,y)  返回x的y次方,即x**y  I" O6 Z( Z2 l
  1. #返回x的y次方,即x**y% t# V6 @& c/ ^& v0 {  K3 [
  2. pow(x, y)
    % @2 }9 ]: q" V- K$ ^# p
  3. Return x**y (x to the power of y).. s& v' ?0 q. m: o( `; k
  4. >>> math.pow(3,4)
    + |" h- [9 {* d# N8 u* x
  5. 81.0' m9 `" k# o5 l8 i: @8 k
  6. >>> 0 P) c; A% d% J
  7. >>> math.pow(2,7)
    5 \. D- i8 }. c
  8. 128.0
复制代码

# H7 T9 s9 i; }. b2 [, E# U6 ~1 umath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' V' v# i1 X" c( e0 B
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! Y# p' `5 ?( u5 e) M3 H# r
  2. log(x[, base])  h( W, D' v8 \" C1 t! F0 L
  3. Return the logarithm of x to the given base.
    9 R% H  d3 r7 n3 t
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ' M% U' y( e' ~7 \) [( w
  5. >>> math.log(10)5 {8 t- d( O( x" j' K: k, X" N7 F
  6. 2.3025850929940462 Y! X) w0 G& p, O
  7. >>> math.log(11)
    8 \1 I+ Z8 T* R: O, r8 h
  8. 2.3978952727983707% P( t' U( g. [% h- j
  9. >>> math.log(20)2 W: o& y* b! B5 }% C; X7 @
  10. 2.995732273553991
复制代码

. @( g" Z6 I5 X* q2 Wmath.sin(x)  求x(x为弧度)的正弦值" B. K8 f# d- m! K  a; W; ?8 [
  1. #求x(x为弧度)的正弦值5 j7 m4 a" r2 P/ ]4 K) `
  2. sin(x)
    8 X9 g+ C, ]+ V
  3. Return the sine of x (measured in radians).
    * N; W2 x6 {  ]6 U- y8 \" C
  4. >>> math.sin(math.pi/4)
    , @7 L% Q4 v6 }1 A: {) d
  5. 0.70710678118654754 ?9 @6 X  v) B9 c; L" `! E) f5 }
  6. >>> math.sin(math.pi/2)
    ) }( B( ?7 B, l; `8 F
  7. 1.0
    & K& i' ^- O: `$ t6 j
  8. >>> math.sin(math.pi/3)
    3 `" U) z7 w/ C: Y0 M# u% K/ T
  9. 0.8660254037844386
复制代码

7 q  u  M6 b0 h5 |8 n% e: Wmath.cos(x)  求x的余弦,x必须是弧度
# u) u5 R1 \2 P/ S( u) V+ I
  1. #求x的余弦,x必须是弧度
    8 J% [5 _1 H% o. c$ |1 D5 T5 Z) O
  2. cos(x)
    4 P' k; H, V; T- d2 l
  3. Return the cosine of x (measured in radians).
    % u# e- K% I$ A/ \
  4. #math.pi/4表示弧度,转换成角度为45度% E* g  D# f4 O7 f! Z& Y
  5. >>> math.cos(math.pi/4)7 e9 _3 ?% A, o9 L9 F3 Z1 A
  6. 0.7071067811865476% \  [  ?; E6 T+ z) O- [" t$ _
  7. math.pi/3表示弧度,转换成角度为60度  ^% K% c7 `& `. C" B
  8. >>> math.cos(math.pi/3)
    ) L! I6 [" y+ o
  9. 0.5000000000000001+ o, m0 o( G$ k' a8 A9 `
  10. math.pi/6表示弧度,转换成角度为30度8 T$ F# `! S! v9 ~$ _
  11. >>> math.cos(math.pi/6)
    4 B) l* b% Q3 F8 X4 |- {, I5 y
  12. 0.8660254037844387
复制代码
! a9 n+ g7 L' F9 E
math.tan(x)  返回x(x为弧度)的正切值, a  x! O- M" j' O
  1. #返回x(x为弧度)的正切值8 Y9 _4 R0 Y, j; t8 K! ~
  2. tan(x)& y: N+ t0 o  a$ f
  3. Return the tangent of x (measured in radians).6 R. Z: Y! M% }/ ?5 ?
  4. >>> math.tan(math.pi/4)4 Z9 @0 j* z3 O$ r" J. ^" T9 f
  5. 0.9999999999999999  `: ?: Z5 i! b
  6. >>> math.tan(math.pi/6)
    : Y  z3 Y% h; t( {- r. F# A" o
  7. 0.5773502691896257( Q4 h4 j3 y2 \; o$ M
  8. >>> math.tan(math.pi/3)
    9 b) @% j- w1 A2 n  ~
  9. 1.7320508075688767
复制代码
9 O1 m( \9 j& [9 a0 P( a, A" \
math.degrees(x)  把x从弧度转换成角度
+ r: o' l, ]8 W5 e  p9 ~! `) t
  1. #把x从弧度转换成角度0 V+ G. `: i) p& [9 J
  2. degrees(x)
    - k; a) z0 j3 A1 r
  3. Convert angle x from radians to degrees.
    4 n  B) J1 ~( Z& k! k

  4. 3 D# @! `. H# ?; ^
  5. >>> math.degrees(math.pi/4)  v8 e5 F# A: r! c, H; y( s
  6. 45.0
    # h0 o: Y5 M/ d9 V' l. u5 m
  7. >>> math.degrees(math.pi): m  g  T- V& ~% {% l, h( [
  8. 180.0
    0 m( X! ], {% E% H9 V' G+ O4 {! |
  9. >>> math.degrees(math.pi/6)" G& g* f2 R+ r. [# n, O* j2 M5 c
  10. 29.999999999999996- w3 w. a; P" }
  11. >>> math.degrees(math.pi/3)# V; z5 ^. A3 e4 N3 e9 t
  12. 59.99999999999999
复制代码

" ^' B1 B4 [1 S7 K. L# G0 _math.radians(x)  把角度x转换成弧度
( O& o6 ~5 o. b* }' Y
  1. #把角度x转换成弧度$ H" Y/ ]) p/ n! N/ j* H2 I6 j: N
  2. radians(x)
    . I. @8 q6 e- S& c; s" v5 K
  3. Convert angle x from degrees to radians.
    / c6 i' N% a% ?0 X
  4. >>> math.radians(45)
    ' d& Q9 u7 [- \) |) N
  5. 0.7853981633974483  C. L& b$ b3 v% Q  k
  6. >>> math.radians(60)- `% M2 a; X4 ^+ u( ^
  7. 1.0471975511965976
复制代码
; N+ F& K9 \, O; `
math.copysign(x,y)  把y的正负号加到x前面,可以使用0# p( b: ~" o1 P9 ~2 [
  1. #把y的正负号加到x前面,可以使用0
    . _4 U- w4 O6 `2 P+ n* |# I
  2. copysign(x, y)& `+ l( T7 l3 i
  3. Return a float with the magnitude (absolute value) of x but the sign
    1 S( p* [# n6 X) B  r% c
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 R$ q1 _* C( J  i- f8 X
  5. returns -1.0.0 c9 h) a6 u1 e' z+ o% p

  6. * J! [: {7 |3 ]8 f* @% C0 a& d
  7. >>> math.copysign(2,3)9 ]" |5 O0 ]1 A4 |- V1 h1 r  B! w
  8. 2.0' r+ m+ M9 j" b7 X
  9. >>> math.copysign(2,-3)
    2 z2 }1 m& H, ~3 d- s- n9 B
  10. -2.06 k& T0 V$ @# |, j8 H3 }
  11. >>> math.copysign(3,8)
    7 S1 p$ O: h5 Z8 k, z
  12. 3.0  K- i9 E2 B0 z: I. X7 G
  13. >>> math.copysign(3,-8)
    4 m2 [3 i7 l6 ~
  14. -3.0
复制代码

8 A/ P+ y: F" J% p8 n* Nmath.exp(x)  返回math.e,也就是2.71828的x次方
; Q" W* b3 y7 i/ }
  1. #返回math.e,也就是2.71828的x次方% J0 @4 w7 s/ H4 \1 c
  2. exp(x)
    8 A$ d/ _7 g; J6 n+ E+ r9 U' V
  3. Return e raised to the power of x.& \2 z* H/ S5 N- A) G
  4. " J$ J- O4 a4 O
  5. >>> math.exp(1)
    ( ]7 y" d+ r3 B0 \3 X) v
  6. 2.718281828459045
    : j$ f, {# Y- ?) Q! E, X
  7. >>> math.exp(2)& C+ k3 V8 z; R+ ]: V
  8. 7.389056098930654 n+ X& e# g, H( t
  9. >>> math.exp(3)) s7 k: u6 ?2 `7 P& N
  10. 20.085536923187668
复制代码

# _3 G* X9 ]  i% h5 G( qmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( }; E; P. Z% s" J1 F
  1. #返回math.e的x(其值为2.71828)次方的值减18 A# x1 l6 S' P) g( |  T/ o
  2. expm1(x), A) o- |$ c0 [4 a2 _+ b
  3. Return exp(x)-1.
    3 p5 D5 v4 c! S$ |3 H: z+ D/ S" o0 _+ ^
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.0 ^. }7 u$ H& X' S* `9 `6 A
  5. % X& i# ^6 g) h
  6. >>> math.expm1(1)
    * J4 ^4 L, G1 e) @; Y
  7. 1.718281828459045# ?8 O4 \9 }% r  K% O4 ^
  8. >>> math.expm1(2)
    4 M1 I0 u6 B, t( L0 K
  9. 6.38905609893065
    * w' c* I! c4 W) \( R
  10. >>> math.expm1(3)/ g* p# M' X5 R: k' e% M0 Y
  11. 19.085536923187668
复制代码
; e; q6 ^4 n3 `' e
math.fabs(x)  返回x的绝对值
  l! Q; ?0 g8 _4 K; X; U; z
  1. #返回x的绝对值
    - q' s! d4 R2 M  B2 t$ g' a
  2. fabs(x)8 U2 E4 Y) _! T
  3. Return the absolute value of the float x.
    # B" o0 q1 Z0 E6 q# K
  4. ! c, J0 `' ^8 f5 A9 L* o+ m$ u& N
  5. >>> math.fabs(-0.003)
    6 U- `* V$ l7 q+ [
  6. 0.003( x: U: \- F' l  w. f% ?
  7. >>> math.fabs(-110)
    8 u) g1 f# U8 B1 p( ]5 o
  8. 110.0
    1 M# y) V) t" b! F! q2 y
  9. >>> math.fabs(100)' q0 B9 }7 g- v8 O6 k
  10. 100.0
复制代码
- v! ^7 E# ?1 {3 J, I! X8 B
math.factorial(x)  取x的阶乘的值9 S( _4 D3 B7 e# s) H% v  s
  1. #取x的阶乘的值1 l/ X. F5 T4 t$ T7 |( t9 ?, F
  2. factorial(x) -> Integral: `2 L1 g) @- R$ w" w
  3. Find x!. Raise a ValueError if x is negative or non-integral.: i- S6 ?" ~  c, M1 K& R3 ~8 c
  4. >>> math.factorial(1)
    - N# c  D$ y# [/ o
  5. 11 C2 ?4 E7 u1 @% e, W
  6. >>> math.factorial(2)0 ?6 X# Q/ t  y; R
  7. 2
    7 i1 m  }# a+ m7 Z8 l8 c% ?
  8. >>> math.factorial(3)
    - C' d, r  ?( [6 k
  9. 6+ p8 ]) _: W' G# H% ^
  10. >>> math.factorial(5). c  N2 }/ l9 X, C8 h, N( _
  11. 120! |& ^/ p" B5 R" z* W: T, Z
  12. >>> math.factorial(10)3 W  y. x  ^; L
  13. 3628800
复制代码

* s5 _8 D; Q4 {* H( ymath.fmod(x,y)  得到x/y的余数,其值是一个浮点数$ v2 J" P. `2 I
  1. #得到x/y的余数,其值是一个浮点数$ S0 t8 ?" u3 Z$ d1 n
  2. fmod(x, y)
    3 Q0 A# O4 O) c7 g& |. h& [" Z0 r  f
  3. Return fmod(x, y), according to platform C.  x % y may differ., ^+ m, d) [/ R) T+ V' p- T- m
  4. >>> math.fmod(20,3)  Y, r* f) ]5 I+ `/ X3 w6 P
  5. 2.0
    0 r  s! B4 Z4 O9 Z: D( b# M2 w; V
  6. >>> math.fmod(20,7)
    1 H% Y$ L  Y/ o) N, P- m, i
  7. 6.0
复制代码

2 D8 R7 z( m& N+ j: Tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 Y  b3 \3 z( `8 C8 w, j
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    5 U! B  W0 L- S& X+ C  z% [
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 c( ^: t$ @1 z" }8 W
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 W6 |; H( `. g7 `
  4. frexp(x)
      r. a# }  `: d: J
  5. Return the mantissa and exponent of x, as pair (m, e).
    6 G& u+ K/ P; J  C0 j9 l8 N
  6. m is a float and e is an int, such that x = m * 2.**e.
    ' m0 i$ b/ w8 N2 u8 f0 t8 l7 N
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! d/ _( D' [7 Z7 w8 X. l
  8. >>> math.frexp(10)
    7 T0 i3 w" b! j, t9 D
  9. (0.625, 4)2 K5 S" x/ _4 \2 c$ T) p6 \
  10. >>> math.frexp(75)4 Z+ _  N. i' s' c7 C$ O& i
  11. (0.5859375, 7)' J5 Q$ q: z- u  D" f' w3 V
  12. >>> math.frexp(-40)0 n* ?+ \$ V2 Z3 Q7 t7 w
  13. (-0.625, 6)2 j# y1 c% I, w" d5 r5 W
  14. >>> math.frexp(-100)* S$ D. L# l; y5 Z
  15. (-0.78125, 7)6 C  l/ y) A( H4 @4 u2 ]
  16. >>> math.frexp(100)
    ! j1 `4 m& D* U1 }$ Q
  17. (0.78125, 7)
复制代码

5 ]% U* h( o  |# Omath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
7 R8 v; M. f2 I
  1. #对迭代器里的每个元素进行求和操作$ {+ ~) x3 `6 r
  2. fsum(iterable)6 g/ y3 ^- Z* L1 {4 o7 ^1 m; l" w
  3. Return an accurate floating point sum of values in the iterable.
    3 a' n- h" J$ G. ?- F0 {3 ]! L6 J
  4. Assumes IEEE-754 floating point arithmetic.) f& a4 ~; P5 i
  5. >>> math.fsum([1,2,3,4])
    1 M$ L9 H1 v: |. H4 r
  6. 10.0
    . ~9 ]& J7 e/ Y& H: s6 C
  7. >>> math.fsum((1,2,3,4))
    ; g; C# g% O3 j3 m7 ^& @" f8 ~
  8. 10.0" X+ F+ P% F3 X6 ]0 }
  9. >>> math.fsum((-1,-2,-3,-4))3 w/ G8 B& m  x7 g# T3 M
  10. -10.0& Q7 \7 t* s5 J: M
  11. >>> math.fsum([-1,-2,-3,-4]): [5 Q; d5 D* E  l
  12. -10.0
复制代码
# U1 [6 `( a* J; I7 f7 B, ^6 D
math.gcd(x,y)  返回x和y的最大公约数+ a( ?2 A+ ?' r# I8 @! h+ g4 u
  1. #返回x和y的最大公约数5 ~9 {! R: f& V. a. X( b/ z
  2. gcd(x, y) -> int5 U) A& |( [' ]8 b& n
  3. greatest common divisor of x and y) B% f0 }* Y4 a: ]  Z5 ?
  4. >>> math.gcd(8,6)8 o0 {4 W5 G0 Y6 O% s1 i
  5. 2
    + C) m5 @/ j) `2 B/ w6 A8 A
  6. >>> math.gcd(40,20): n7 M1 t5 R! S
  7. 20
    ' k* [* e" r; d$ I
  8. >>> math.gcd(8,12)
    * J- y( C; }7 d0 T0 P
  9. 4
复制代码
, R( m+ b; o- v  X* D: T% K3 ]+ {
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
/ ~0 _& h  e, ?" a  ]. K4 m, x
  1. #得到(x**2+y**2),平方的值: U5 V* s; ]3 v2 L
  2. hypot(x, y)4 @, N2 b' r. o0 c9 {. [9 r
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    $ u2 J0 C: E' @9 N
  4. >>> math.hypot(3,4)
    2 S! I" W6 ]% v( s# Z/ V  @. P
  5. 5.0- A( e& q9 _" ?; F" f2 @
  6. >>> math.hypot(6,8)- W6 \# V( m9 b  h# u
  7. 10.0
复制代码

; W4 t' n( b- |+ S0 Z/ h: O; j/ Zmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False8 M7 b- p- n% i4 e1 P6 e
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    9 m7 i) i' `' y  p; A6 b0 J
  2. isfinite(x) -> bool7 \: \- v: O) p1 e  \) D4 [3 i
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' \$ a& |- r5 X) g" }7 A& Q
  4. >>> math.isfinite(100)2 q: G# J$ i8 t( H( N) h5 J
  5. True
    * X8 o% d9 p+ _0 U
  6. >>> math.isfinite(0)
    9 W+ A  t- O( ^2 ?% N  l; V" P
  7. True/ p  S' g6 E- i8 R5 o' o. t
  8. >>> math.isfinite(0.1)
    + Q5 T" n) G% o
  9. True2 G+ P9 P8 [+ l$ G) B  J
  10. >>> math.isfinite("a")+ e8 I- d7 _+ ]$ R4 N( f, @% k5 @6 P
  11. >>> math.isfinite(0.0001)  M8 D. M' c5 A- P3 P! U5 }+ t' @: A
  12. True
复制代码
) T% H2 y1 W, g& \
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False+ n; \/ X6 C' B$ Y# s6 o
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False" b  w  P) e0 @' D0 m' m: D
  2. isinf(x) -> bool: b) ^8 v- u+ L. e
  3. Return True if x is a positive or negative infinity, and False otherwise.5 j3 `4 X" ^: p# V# `. q
  4. >>> math.isinf(234)
    7 C% O9 p" s3 }% x5 G$ ^# B
  5. False
    4 N- Y# b* _. A6 U, C& K0 ?
  6. >>> math.isinf(0.1)
    / d! l; t3 P9 A. {2 Y( z5 y0 m' E
  7. False
复制代码

. w/ D' w. ]# e! w! U2 k$ rmath.isnan(x)  如果x不是数字True,否则返回False
9 f9 y* J6 w, N/ B, ^+ ^0 k
  1. #如果x不是数字True,否则返回False
    - v7 ~1 B( X! l, A4 D6 v" D
  2. isnan(x) -> bool$ |5 m" j3 r, H- ?5 y
  3. Return True if x is a NaN (not a number), and False otherwise.' C7 l; G# h4 k. V0 m: s9 r! ~
  4. >>> math.isnan(23)
    ! J% e# s6 P; y3 Y9 K' {/ S
  5. False7 A: g+ z6 ^7 m# m% b" c: u
  6. >>> math.isnan(0.01)$ t5 K$ a6 N) n0 @
  7. False
复制代码
7 ?9 a& A6 E) z3 B+ W! T
math.ldexp(x,i)  返回x*(2**i)的值
" _: D% u* r4 A" v, b4 x7 s7 s- W2 }
  1. #返回x*(2**i)的值1 ]0 {$ P8 M5 O( n( W# Y
  2. ldexp(x, i)$ w. j3 w: e* _- ?: x
  3. Return x * (2**i).
    . [) `" u7 L7 U0 C3 g5 }
  4. >>> math.ldexp(5,5)7 w6 T% }. Z3 I6 B/ f' b
  5. 160.0* K3 f' R/ a. ^* d" b/ F5 H
  6. >>> math.ldexp(3,5)
    $ i( Z4 U- r3 h! a. l
  7. 96.0
复制代码
5 b) p' j. d  c9 v& n& H
math.log10(x)  返回x的以10为底的对数  C3 K+ I' S9 b3 v$ n" Q! f8 l
  1. #返回x的以10为底的对数- h# _$ C6 c( x+ K9 J2 M- Y
  2. log10(x)
    ' k# b1 D% T+ [6 I8 r
  3. Return the base 10 logarithm of x.
    - r+ l3 H! m2 c7 x- o5 ?6 N/ a
  4. >>> math.log10(10)3 t: i/ P  e. {4 _
  5. 1.0
    6 a$ I" P* o. _3 [0 f9 {8 n" f
  6. >>> math.log10(100)
    / r# S, g' e( A) o7 A" c% n& _
  7. 2.00 ?4 N( u9 i9 J8 G0 c; t% G- Z$ ^
  8. #即10的1.3次方的结果为20
    ! Q/ X/ [- |; f# ~, [4 T# d
  9. >>> math.log10(20)
    # T/ O8 E/ O) E
  10. 1.3010299956639813
复制代码

; R" m. k2 v* b, w* imath.log1p(x)  返回x+1的自然对数(基数为e)的值4 M7 O2 z  ?1 \! X/ J, B) B
  1. #返回x+1的自然对数(基数为e)的值' R& y& I8 ^8 K" `% I" w3 c
  2. log1p(x)
    3 x+ L" n, R8 v
  3. Return the natural logarithm of 1+x (base e).
    , x. {/ J: g, U( v# Y2 B
  4. The result is computed in a way which is accurate for x near zero., a; K/ K7 D5 d& }5 o
  5. >>> math.log(10)- Y+ a6 O" K& F: f% t" n7 A& T
  6. 2.3025850929940462 f- b2 S& x+ M* b& f$ ]' ]
  7. >>> math.log1p(10)
    # A3 P3 e# y# ?9 P& |' N
  8. 2.3978952727983707
    & C, c  G  b2 n. h: U
  9. >>> math.log(11)
    7 J$ i1 k# R) F; V% V7 A; C$ ~
  10. 2.3978952727983707
复制代码

2 O! F1 S4 X5 h( o7 o8 `, |, vmath.log2(x)  返回x的基2对数% F0 x0 u. X* T( @8 {6 d
  1. #返回x的基2对数1 C) ]1 R2 s, j- \5 Q$ B3 Z
  2. log2(x)+ P! Q& Q* l$ r0 f- }3 @# F
  3. Return the base 2 logarithm of x.
    0 [$ N, x" a& L* t
  4. >>> math.log2(32)( o  a2 M* j6 ~
  5. 5.0
    3 O3 X8 @2 ^- `( A& [; o
  6. >>> math.log2(20)
    0 U# v4 p$ V! |6 C
  7. 4.321928094887363; R6 x  E: B9 A! w0 f# h+ @
  8. >>> math.log2(16)
    1 j: \2 H3 e( F
  9. 4.0
复制代码

% b* {& @9 j% n% Nmath.modf(x)  返回由x的小数部分和整数部分组成的元组" w3 P0 p7 H2 E! D; q
  1. #返回由x的小数部分和整数部分组成的元组
    8 Y& W( D. i6 o  ^8 A% c
  2. modf(x)
    : X! I) t& f* N. C, ]0 L
  3. Return the fractional and integer parts of x.  Both results carry the sign
    6 q* t& ^* G6 V+ L7 M2 V
  4. of x and are floats.
    + U: {, |! \2 C5 I5 \
  5. >>> math.modf(math.pi)* m1 q* B: K" J% ~7 r+ C5 }
  6. (0.14159265358979312, 3.0)
    : T5 V! w  C( z- k7 P1 D; y
  7. >>> math.modf(12.34), @/ y0 Q; _( l  x" O
  8. (0.33999999999999986, 12.0)
复制代码

2 n( h' m9 R" Xmath.sqrt(x)  求x的平方根6 R: P( v0 q2 `* C7 ]: w) @
  1. #求x的平方根! p# u4 T$ k+ \# _  Q1 K) m
  2. sqrt(x)- m+ f$ Q$ I6 I0 T
  3. Return the square root of x.& E( t( F8 v2 ]; j; q) c
  4. >>> math.sqrt(100)" O5 J, N! J' `
  5. 10.0
    ( u) @, ^+ c1 v' O7 H5 q
  6. >>> math.sqrt(16)
    3 O9 D. O- C, a# d/ J/ m
  7. 4.0- Z9 B% [/ A/ j- N- N; [9 L
  8. >>> math.sqrt(20)
    % Z; D: q7 n8 _% Q
  9. 4.47213595499958
复制代码
3 B2 C) z" x9 s$ y, i
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分' l( ]) Q: M' L4 S' {
  2. trunc(x:Real) -> Integral
    1 I0 @- W, t6 g& G: |5 o, a) |
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.- \6 B+ B5 U. K8 T  k
  4. >>> math.trunc(6.789)
    % G5 d5 F7 }: F
  5. 60 c& n, y* w, f5 W; ]% [7 e+ \- B
  6. >>> math.trunc(math.pi)& e% W! t" Z3 H: g) g; ?
  7. 3
    ; z, d- c' g  A" u  L/ U4 e) h
  8. >>> math.trunc(2.567)
    ' F) {* u: ?2 I2 _7 N$ u4 t
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-2 06:12 , Processed in 0.074317 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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