新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
; W) j5 V: [7 ^) e
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! @/ g- `" G. r7 K2 P# A* [9 E2 p

' g+ I3 [7 {. C/ v' |8 d6 D方法1
0 G2 r5 {9 v6 v
  1. >>> import math
    8 Q" ^3 L( l2 k/ o8 ~
  2. >>> math.sqrt(9)+ p# t/ \4 Z7 ^
  3. 3.0
复制代码
方法2
1 R( o  }# w$ A2 ^: K" X- c3 i! f
  1. >>> from math import sqrt7 }3 k  f- z( K; e# Y: v
  2. >>> sqrt(9)
    ; {, A* p# D! z/ x0 O
  3. 3.0
复制代码
2 I- f6 M* j7 c3 P! j! q7 f' B6 ^

6 N3 q# c" J: ?3 @
math.e  表示一个常量' e1 b5 H) {# T3 b- N1 B
  1. #表示一个常量2 l$ F* u+ t0 \2 p4 V0 {) ~& p* \2 p
  2. >>> math.e) [3 ], M  L# F3 S) U0 g+ P
  3. 2.718281828459045
复制代码

! v1 z# h7 `2 d6 f, ^, M( X' Dmath.pi  
数字常量,圆周率
  q. {; W$ N4 o& Y$ W, ?( F3 E
  1. #数字常量,圆周率
    - u0 F& J/ G  W4 I9 z3 _
  2. >>> print(math.pi)
    4 L9 ^: w" }+ q: d4 V
  3. 3.141592653589793
复制代码
6 L- @  n" z, x
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
, K9 h4 M! w  p. N
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 G# d) t( k7 E5 F6 f" m7 o
  2. ceil(x)
    & H4 B  k6 d7 z- L- B! v
  3. Return the ceiling of x as an int.4 Z# T2 w  \% U  Q( q, w
  4. This is the smallest integral value >= x.+ l1 q4 P# t  ^" s3 S$ v+ U; L9 v$ p3 n

  5. 6 t7 g" r! f0 w/ M4 `* e5 J: o
  6. >>> math.ceil(4.01)
    2 r+ _8 s$ {8 J4 W& ?! p
  7. 5
    ( q' F. m6 z' S% _7 X* V' {1 h
  8. >>> math.ceil(4.99); Y3 {7 I# K4 O+ D7 B) _3 F* Z5 o
  9. 59 k& g, l$ p% L; \9 d
  10. >>> math.ceil(-3.99)
    , R3 C5 P* S: z  ?
  11. -3' J3 g% V8 x( p9 W
  12. >>> math.ceil(-3.01)1 \, a* \1 ?( w4 B6 H7 l( c
  13. -3
复制代码
6 C7 b: o5 d4 O, Z; c8 o
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身" k' d" w0 @  D5 z0 A
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    / E$ w: c: Q, R2 Q: j/ W; Q* L
  2. floor(x)5 I8 `' ]9 Y( ~& }
  3. Return the floor of x as an int.
    " T! X0 X( U5 c  w; W: {4 x. N
  4. This is the largest integral value <= x.
    ) I4 X; k) I& z! w' Z6 I" G
  5. >>> math.floor(4.1)
    ' |- {! ]3 N0 Z$ z7 b
  6. 4
    7 z" k  R  l& B- t
  7. >>> math.floor(4.999)$ A7 e8 G( z: j' P7 U& G+ [! A
  8. 4
    1 i; a2 h  e7 _1 Q+ S7 _
  9. >>> math.floor(-4.999)6 A8 x0 F1 w$ F/ w6 o2 j2 \2 a$ c
  10. -56 |8 @; \/ M; f8 Y" d, ~
  11. >>> math.floor(-4.01)
    3 |* U8 J+ z( p: q1 ^
  12. -5
复制代码
: [4 z1 ]' t! X2 t! o& |. y
math.pow(x,y)  返回x的y次方,即x**y
: g( T) k. x' U$ U* o
  1. #返回x的y次方,即x**y
    & E" U1 ~1 q7 W# a1 x
  2. pow(x, y)
    2 M% i- t. N: H+ M) |
  3. Return x**y (x to the power of y).% X2 H5 Y/ c# {
  4. >>> math.pow(3,4)
    + Z; c, L( E* U' B. D' o7 M
  5. 81.0
    0 y0 K" l# x# v4 j$ v
  6. >>>
    ( T1 R8 o. ~2 {; T! z
  7. >>> math.pow(2,7)
    ' C' ]* y: z2 P8 K; j: V. R, P; @
  8. 128.0
复制代码
* |0 t. P; |7 l4 U( [$ g
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 x3 ], }* C- g7 y
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): W0 X$ Q' L# K* f  r+ y6 B
  2. log(x[, base])
    0 V- G4 K3 l) T: R' e
  3. Return the logarithm of x to the given base.
    ) D# g5 Z) E0 k7 J( ^. a" i
  4. If the base not specified, returns the natural logarithm (base e) of x.
      A5 g2 b( S" q& w* [5 T6 D( v6 z
  5. >>> math.log(10)
    $ ?6 D' R: _# z7 w: e/ }
  6. 2.302585092994046
    ) A: V) `& b( e, v. ~% I* J
  7. >>> math.log(11)) G/ h& a" T5 |/ A! n7 c2 d
  8. 2.39789527279837071 Q7 H+ g; C) _
  9. >>> math.log(20)
    4 G( ?9 S* ]. |$ B& c
  10. 2.995732273553991
复制代码

# Y+ ?6 w% L# |" Emath.sin(x)  求x(x为弧度)的正弦值
1 [7 V' X! K( Z9 F7 K/ R
  1. #求x(x为弧度)的正弦值
    6 s; z+ W, K+ \; v
  2. sin(x). v- S# ]$ E, l. m1 c* G
  3. Return the sine of x (measured in radians).
    * Z) l) L8 O, s3 u) B
  4. >>> math.sin(math.pi/4)
    / B6 R1 C8 H3 m! V: \  y- m$ z& s
  5. 0.7071067811865475
      [( O- q* o- N0 D( h1 K( g9 T; p8 x
  6. >>> math.sin(math.pi/2)) ?6 T+ q6 k" R: Y: f8 e
  7. 1.02 Q5 `+ k) B) s' c( h' r1 b2 G% }& p
  8. >>> math.sin(math.pi/3)
    - O( Q7 F. o# F
  9. 0.8660254037844386
复制代码
& a) T' e( f) h; L7 Z. S. ]' X2 j5 ~
math.cos(x)  求x的余弦,x必须是弧度
1 h- M$ B' M) |2 P7 D
  1. #求x的余弦,x必须是弧度
    4 ], y7 S% s* Q1 h4 I2 X4 \
  2. cos(x)' }1 s9 T3 G" i* U0 }. b
  3. Return the cosine of x (measured in radians)." B; M) L* H9 G6 q
  4. #math.pi/4表示弧度,转换成角度为45度* F0 r0 a  }# G- w, F1 ~8 X8 d
  5. >>> math.cos(math.pi/4)- k7 Z: U0 X% y
  6. 0.70710678118654769 u" v- @7 n" {- k0 L
  7. math.pi/3表示弧度,转换成角度为60度
    2 @8 _  [8 q& @9 G7 }$ H! I
  8. >>> math.cos(math.pi/3)
    * a( n. o8 S* J; K# e) ~: k. ?: w% a
  9. 0.5000000000000001+ F2 n6 S- M; Z  o. w( _
  10. math.pi/6表示弧度,转换成角度为30度
    . r1 u% R. e  ?8 \2 n
  11. >>> math.cos(math.pi/6)
    * _1 ]- F# d. V' T5 n. Q( l
  12. 0.8660254037844387
复制代码
: L" q6 q! S- j, j! H
math.tan(x)  返回x(x为弧度)的正切值
- E1 }' }5 F8 t* }' h, O7 F
  1. #返回x(x为弧度)的正切值
    ; w8 ~& z2 V' f7 _; z: ?5 _
  2. tan(x)
    # |  R& F- ]- D8 ^: ^' t: T! J+ a
  3. Return the tangent of x (measured in radians).
    % b5 t/ O; Q1 r$ B9 G  B* b
  4. >>> math.tan(math.pi/4), f# M% b) n0 ~* z( l" N: M
  5. 0.9999999999999999+ S4 _5 w$ ?; x; y, @! R/ r
  6. >>> math.tan(math.pi/6)
    % F7 L, p7 u) Q3 y
  7. 0.57735026918962570 l) O  A* J8 {0 n% K. |( t. m( X: C8 Y
  8. >>> math.tan(math.pi/3). ?& R9 C; a8 `2 x
  9. 1.7320508075688767
复制代码
+ o, w3 D6 R+ V" z9 ]7 O) C
math.degrees(x)  把x从弧度转换成角度
8 P# f; _: |! K2 m' }
  1. #把x从弧度转换成角度% }6 X4 x3 i* s6 a6 i8 t
  2. degrees(x)
    6 S& R: p' a, O3 z9 l% g
  3. Convert angle x from radians to degrees.
    . {+ w3 \7 ?, y* h& k4 l7 O

  4. + ^' `, u# @% Y3 \/ }. ?+ N
  5. >>> math.degrees(math.pi/4)1 [. J' x1 u& P$ l& D
  6. 45.0* J  m; \( g2 }6 @- C
  7. >>> math.degrees(math.pi)
    / P: `2 K5 w4 s( g2 z' i7 D
  8. 180.0
    * Q, i' Z7 r  f" P. g9 S& N
  9. >>> math.degrees(math.pi/6)
    0 {7 l5 ^4 ^) N( H
  10. 29.9999999999999962 b0 m9 o6 L+ W& O. g
  11. >>> math.degrees(math.pi/3)
    3 |2 @, U0 }; O1 A- i5 C  x
  12. 59.99999999999999
复制代码
1 T, o3 ~( j) S' S8 G4 l! m
math.radians(x)  把角度x转换成弧度! Z  R/ v( W- Z* |' k+ l9 {
  1. #把角度x转换成弧度1 b% F, O& t6 A8 E. @- c7 o' M
  2. radians(x)  s& b/ k" D1 D: x( t
  3. Convert angle x from degrees to radians.
    1 Z, T: {6 N% r" B: J/ m
  4. >>> math.radians(45)' M  ]5 Y- x0 ]- y8 B" G. T# ~2 K6 [
  5. 0.7853981633974483
    $ i5 F0 u( [9 [7 a' y1 ~3 u( T1 b$ w# D4 Z
  6. >>> math.radians(60): V, j9 |" b+ @
  7. 1.0471975511965976
复制代码

2 I5 l; D( U+ i& p; Cmath.copysign(x,y)  把y的正负号加到x前面,可以使用07 \( n0 M0 q3 ?! l6 j
  1. #把y的正负号加到x前面,可以使用0
    * v, ]* \; o' Z- B% W! Q
  2. copysign(x, y)- u0 g9 g9 s6 v& a9 K
  3. Return a float with the magnitude (absolute value) of x but the sign
    " \$ }2 k! K$ Z" g9 ~% a3 R* L3 N
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) : i8 O+ S2 D. y2 k# t
  5. returns -1.0.
    / l% q% q! n5 d5 Q/ w& S! B
  6. 2 {! I8 _. w  d
  7. >>> math.copysign(2,3)
    6 a) k% ?- [$ s+ z8 E7 Z
  8. 2.0
    $ Y' W% t( R5 Y4 b( |: j
  9. >>> math.copysign(2,-3)
    : H5 M/ d$ x; s3 E- r3 i# b
  10. -2.0
    / H  r& t6 P% l2 S# A. k5 P# `
  11. >>> math.copysign(3,8)
    & k( L- s0 ^' p' s0 Y  v2 Q
  12. 3.0# u) t0 K- r4 \
  13. >>> math.copysign(3,-8)3 M% a0 G+ X/ ]- ~: D: W' b
  14. -3.0
复制代码

  C& q- t& c- U( F; Ymath.exp(x)  返回math.e,也就是2.71828的x次方
% ^: @1 H0 }. @
  1. #返回math.e,也就是2.71828的x次方( i  c, G8 J8 |; ~( k( Z" k4 G# s
  2. exp(x)
    * u5 p- X# q4 G
  3. Return e raised to the power of x.
    ' ^0 a4 u* J: n* m: X/ _* d8 \
  4. , A; _1 R7 q1 Z
  5. >>> math.exp(1)- W! @+ I# J; u2 H! _: N
  6. 2.718281828459045
    1 s3 J9 @# i' a" `
  7. >>> math.exp(2)
    3 h- |4 }5 P, }0 C' {! q1 X
  8. 7.38905609893065# Y5 d& m. C6 r% |/ u1 j$ O
  9. >>> math.exp(3)$ ^3 r' K" J+ }4 k3 X6 f9 J
  10. 20.085536923187668
复制代码

& Y, r+ u! e; emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
8 X% a. x2 B; a+ f7 \
  1. #返回math.e的x(其值为2.71828)次方的值减1
    , |& @) `- ]- b. s
  2. expm1(x)
    - |) t" @  }. A% P! S
  3. Return exp(x)-1.; u# K$ s' X% i
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. l3 G& V) C! P3 a7 V8 o) Q' v

  5. 6 o; t% w2 ]% w' x% A$ ?
  6. >>> math.expm1(1)
    - d. h  s" d. X# ~
  7. 1.718281828459045% ~+ Y+ |. P' D# w+ S
  8. >>> math.expm1(2)2 T# I2 Q. N! O* c9 k9 I
  9. 6.389056098930653 @' h* W2 M' U4 O. ]/ O3 ]0 S! Z
  10. >>> math.expm1(3)
    ; P2 j* @- z- }
  11. 19.085536923187668
复制代码
3 E8 q: J0 O0 O! ^, X6 w3 @- a% v. b1 i
math.fabs(x)  返回x的绝对值
( i" F/ V+ x0 I5 ~; x0 }
  1. #返回x的绝对值# d: }  {4 J& w! U
  2. fabs(x)1 b; b( q4 P+ ~: B5 _, Y* ?8 F4 H
  3. Return the absolute value of the float x.
    : v  n' V$ I  c' r. q/ y

  4. . t. u0 R5 W- Y2 T9 L+ ?
  5. >>> math.fabs(-0.003), y4 W2 V. r3 ^# J. I1 t' O& F
  6. 0.003
    ( [& I9 l1 N9 _9 m
  7. >>> math.fabs(-110)6 e, R- c! B( ^- ]4 _3 k
  8. 110.0
    * l# l5 [0 t6 M5 ~8 j5 Y8 |
  9. >>> math.fabs(100)5 q7 x: y7 \; b( K5 s
  10. 100.0
复制代码

: l  `% l) K! C& X8 w* Smath.factorial(x)  取x的阶乘的值+ X( W/ l3 C" _, c/ w3 v6 @
  1. #取x的阶乘的值! ~7 {! k) g/ E$ h5 E- o; ^
  2. factorial(x) -> Integral
    7 f" S* L% N5 [+ K5 G
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    * U- }8 P3 K' Y) d; u1 ]+ h
  4. >>> math.factorial(1)4 a0 `& z7 L/ A; C
  5. 19 l1 w. B; E' \' m- Y
  6. >>> math.factorial(2)
    8 h- h+ k/ _8 W" X9 ?* J
  7. 2
    9 ]/ n! M6 J4 L
  8. >>> math.factorial(3)
    - l. u+ W* ]2 V+ E( R8 u/ X
  9. 6' R  y$ _5 H  k3 A
  10. >>> math.factorial(5)
    0 L+ F- _9 ]; N% y9 R
  11. 120
    * H4 Q, M; @; g3 |/ |2 ^& `
  12. >>> math.factorial(10)$ w9 p( Z  z- m$ C  N9 l
  13. 3628800
复制代码
. ]2 N2 i' X' m5 c
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
; N6 x' y: d2 k; i
  1. #得到x/y的余数,其值是一个浮点数
    % B, i: B; V2 Z6 A, E. L. g, b+ |
  2. fmod(x, y)3 I6 |" b, A8 w" ~- ^' D
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + M5 I0 I+ h4 R' }1 @  I" [5 f
  4. >>> math.fmod(20,3)
    2 F8 k# p1 R7 y% `: ]! z# E
  5. 2.0
    ( i+ H3 w# U5 y6 O) @' D
  6. >>> math.fmod(20,7)3 R# {, s0 [) ?9 N4 t( f0 y! ]
  7. 6.0
复制代码
( [5 e; d8 b1 A
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 G1 X+ q, y; u1 V2 K7 F+ A
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ j* t' E, O+ ^# y% L1 i# N* v. [( t0 p5 [
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值) \; `& {6 n$ ?/ f7 T8 }
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 q& e. ^1 L0 B" o+ G0 j0 x' `
  4. frexp(x)
    6 ~" ^1 }* K9 R! |  k) s+ |
  5. Return the mantissa and exponent of x, as pair (m, e)., v, v. |1 a; \" f
  6. m is a float and e is an int, such that x = m * 2.**e.7 ]. X7 H  N( J
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.1 J. L! p0 R+ Y3 w- i
  8. >>> math.frexp(10)% T6 f/ w5 j7 u/ h& C
  9. (0.625, 4)  m& d- S# [% ^$ S
  10. >>> math.frexp(75)
    0 n. h2 E% O' S7 f) H
  11. (0.5859375, 7)
    & B& A9 \3 R( \6 b4 _* h+ Z
  12. >>> math.frexp(-40)
    2 p' ], h# R/ _* A
  13. (-0.625, 6): L% s! {* i1 v2 x
  14. >>> math.frexp(-100)
    % Q. H5 A5 o1 g: ~" j
  15. (-0.78125, 7)1 p6 ~' e7 ~5 k  f7 e
  16. >>> math.frexp(100)
    # l0 U* u$ B( u' U4 H
  17. (0.78125, 7)
复制代码
  w9 z8 @' k; L- c
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
3 T8 q  A/ E  |' A: X4 ~9 S
  1. #对迭代器里的每个元素进行求和操作2 k: K& l6 m0 a! N2 L8 r  D7 m
  2. fsum(iterable)
    + [* K7 h& o- Q8 \  o$ ?
  3. Return an accurate floating point sum of values in the iterable.
    " E9 v; s9 O" s; z+ U# f+ `
  4. Assumes IEEE-754 floating point arithmetic.
    9 Q6 g5 A2 x& ~
  5. >>> math.fsum([1,2,3,4])7 R, c9 n# f8 k8 r7 A* c4 ^: t
  6. 10.0- e; v7 ]+ j- x% ^1 Q
  7. >>> math.fsum((1,2,3,4))
    ' j9 z1 s/ V7 d) Y
  8. 10.0
    $ |3 \9 X( {& e
  9. >>> math.fsum((-1,-2,-3,-4))7 f: X; I. p6 p# v' r2 u5 |
  10. -10.0
    " Z- r3 z$ J, W! L4 ^6 n1 N$ u
  11. >>> math.fsum([-1,-2,-3,-4])$ b$ f8 _1 E# C) o! Y
  12. -10.0
复制代码

6 B, z. U: m& u+ I# e9 R6 `7 D6 Imath.gcd(x,y)  返回x和y的最大公约数
+ E3 A; D: F: K. X& A- G0 P1 ^
  1. #返回x和y的最大公约数6 J/ b7 X3 z% z3 F# ]
  2. gcd(x, y) -> int6 D- [. X& ~7 g5 S& ~% m
  3. greatest common divisor of x and y) a3 }2 @+ R; U9 O% p/ r
  4. >>> math.gcd(8,6)
    : ?+ v" J/ w* ^6 k  p( c# X1 V8 R3 n& _
  5. 2
    - q7 I, q! Z6 o7 v7 C
  6. >>> math.gcd(40,20)5 T) t: M* \$ z& G2 \3 y1 a9 d) O
  7. 20
    * C: _; v/ l. z  T( ^
  8. >>> math.gcd(8,12)+ c4 ^  a% B5 V0 r$ H2 l+ \! g
  9. 4
复制代码

0 q3 B7 @) |+ X) y- F3 fmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False/ ^. @! x: x( t/ \1 R
  1. #得到(x**2+y**2),平方的值
    $ r- _# P9 _) r! g
  2. hypot(x, y)
    5 l, A6 ~2 ?9 P3 ?' f
  3. Return the Euclidean distance, sqrt(x*x + y*y)." R# P$ Y+ c5 j  i# t" F" W( [7 B
  4. >>> math.hypot(3,4)
    7 W" U6 X3 G6 _- C; S" e
  5. 5.0
    7 k0 F& Z; e8 I  A$ E
  6. >>> math.hypot(6,8)1 |8 G( T, A0 N# m3 J" b; f9 ?
  7. 10.0
复制代码
5 }9 L' x4 c9 S4 m0 Z
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
7 t: V2 J; ~3 n3 r
  1. #如果x是不是无穷大的数字,则返回True,否则返回False- \" m  \9 t$ o5 f: N/ U. N9 d! @6 G
  2. isfinite(x) -> bool; e( s) V6 f' j: Z
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    & J; P& O( H( Q) P! d
  4. >>> math.isfinite(100)
    " e/ q3 `8 N) k
  5. True
    , T9 |$ O3 @3 K$ e. g% R- i
  6. >>> math.isfinite(0)  v* @! V1 i# N
  7. True
    8 _4 }6 k# \' i+ o" z
  8. >>> math.isfinite(0.1)' v  \( \2 w6 l) @0 s# d. o
  9. True
    3 o/ o7 v# d$ Z; q+ H
  10. >>> math.isfinite("a")
    3 v& W3 ]/ L2 u2 ]
  11. >>> math.isfinite(0.0001)
    5 t# Z) ?5 M) U$ d
  12. True
复制代码
+ B) @: c% Q7 y. W2 H5 M2 @7 C
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
! H) R8 S1 r) `4 P- a, q
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    5 u! O; T9 e$ K( N; D
  2. isinf(x) -> bool
    4 ^. t9 s9 k( h& W
  3. Return True if x is a positive or negative infinity, and False otherwise.+ T$ Y4 A* ~6 J- M: a5 f
  4. >>> math.isinf(234)8 H0 T& f! |4 l  ~- [8 K' d
  5. False
    3 Y1 ]8 }5 ?) S' t; g! _# L% [) m
  6. >>> math.isinf(0.1)
    $ z6 V& i" ^' G; y5 i
  7. False
复制代码
( z* a1 C; b" @) Y0 R1 }  o
math.isnan(x)  如果x不是数字True,否则返回False
% b% E/ U7 t( {3 |. |. c' s) L
  1. #如果x不是数字True,否则返回False2 f  n% I  L+ C- u5 m* {
  2. isnan(x) -> bool- N: k$ X& [; V5 M+ v9 ~' a8 y
  3. Return True if x is a NaN (not a number), and False otherwise.
      w1 @0 E. o5 n' y
  4. >>> math.isnan(23)
    4 j3 D" R& }2 Z; ^
  5. False% Z% r7 U7 V' K
  6. >>> math.isnan(0.01)
    ; T6 n' z# }+ D6 ?  o5 L$ u
  7. False
复制代码

6 U7 i/ [8 U4 W0 ~. lmath.ldexp(x,i)  返回x*(2**i)的值" A& X: n$ }4 w, Y2 P8 e
  1. #返回x*(2**i)的值
    2 {9 N! `$ a& D% m
  2. ldexp(x, i)
    4 e6 Q/ q- L+ c) R  Y+ e
  3. Return x * (2**i).) q* [/ v4 G' C2 b3 E
  4. >>> math.ldexp(5,5)4 ]  ~3 o: p* I' J
  5. 160.0/ j5 a. D% C# H- b
  6. >>> math.ldexp(3,5)4 x1 U! C( J, `
  7. 96.0
复制代码

1 ?8 M! R: ]: g% ^math.log10(x)  返回x的以10为底的对数
' U6 }% ~4 r$ A! O0 |
  1. #返回x的以10为底的对数
    * {4 _1 x( v, A+ X; \8 |# G8 Y
  2. log10(x)
    1 u9 y0 Z/ b% H, o# N, d2 ^$ i
  3. Return the base 10 logarithm of x.) Z3 Z( L% w1 O; k& j1 f  h
  4. >>> math.log10(10)
    : u2 @) s  y$ X
  5. 1.0; a' |2 z2 A' D( K; g" \
  6. >>> math.log10(100)
    ) U  }6 |0 q9 a) l/ y
  7. 2.0
      S& o) r5 R* z' f5 x7 b
  8. #即10的1.3次方的结果为20* _# Z: E2 \( L) x( D
  9. >>> math.log10(20)
    : |3 _7 L/ o+ G6 x* E
  10. 1.3010299956639813
复制代码

) X' z/ {- b( E0 j! L2 Gmath.log1p(x)  返回x+1的自然对数(基数为e)的值" P+ c7 s* v3 `' |: f: v, G
  1. #返回x+1的自然对数(基数为e)的值) @) N% _6 I$ d1 I& n
  2. log1p(x)
    ; q- U/ ^! o% C3 p* ^$ x
  3. Return the natural logarithm of 1+x (base e)., [$ c' d. c  n( a- x4 G5 o- p
  4. The result is computed in a way which is accurate for x near zero.6 B) H6 J1 _3 V1 s
  5. >>> math.log(10)7 K$ X8 J- s) r+ A7 l7 ]: D' y. x
  6. 2.302585092994046
    ! f; Y4 w! b, `) h) l
  7. >>> math.log1p(10)- I2 ~) D- U( I$ w5 m
  8. 2.39789527279837073 I, u# ^" k0 l2 N# c0 H
  9. >>> math.log(11)
    # O" [. f2 J8 o" o- d1 y1 a8 ?1 O
  10. 2.3978952727983707
复制代码

' d3 B0 d" @! E- Y- ]" y' q( dmath.log2(x)  返回x的基2对数
3 p8 d6 j4 O7 {2 Y$ L& \
  1. #返回x的基2对数
    8 W  p  L9 @  W1 |4 t7 h, X/ O) u
  2. log2(x); z, K6 D. U3 L2 h6 {7 g: J
  3. Return the base 2 logarithm of x.2 t( i3 z+ P, y* {
  4. >>> math.log2(32)
    ( K+ e/ w, ^6 F9 @
  5. 5.0- F7 B& O7 E: n* e% m0 |
  6. >>> math.log2(20)6 _2 b8 d5 K8 t
  7. 4.3219280948873638 l/ k- \( x# h. t7 R4 f! H
  8. >>> math.log2(16)4 H" [+ g; D$ m' V" l8 `! D$ n: G
  9. 4.0
复制代码
# q9 H- ?! ^4 L7 A: X. S1 i
math.modf(x)  返回由x的小数部分和整数部分组成的元组
5 ^8 ^/ j7 B+ b/ l$ a
  1. #返回由x的小数部分和整数部分组成的元组' \7 _: g! x6 k6 g% v4 r
  2. modf(x)& g8 \) m8 _9 }
  3. Return the fractional and integer parts of x.  Both results carry the sign" I) {% ]3 x  E" n
  4. of x and are floats./ a0 S# t5 J/ g% P8 ?2 d) f
  5. >>> math.modf(math.pi)6 E5 m7 k0 k+ X8 G; G0 @8 Q1 Q
  6. (0.14159265358979312, 3.0)
    ! q% v4 g7 H& t- y. u
  7. >>> math.modf(12.34)& r) |! S" D. L4 _" \- K- ~9 A2 v! T
  8. (0.33999999999999986, 12.0)
复制代码

- `  Y% t$ V1 Imath.sqrt(x)  求x的平方根
% ~1 c: j$ V0 Y' O2 p3 b
  1. #求x的平方根
    4 @. P; f0 i  X3 K9 Y
  2. sqrt(x): Y6 B5 r  c* M# s
  3. Return the square root of x.2 ]6 t6 e1 S: \+ M
  4. >>> math.sqrt(100)+ |- g0 k4 c1 t3 s
  5. 10.0  D, g1 n" W4 ?( g2 h5 w. U
  6. >>> math.sqrt(16)
    0 i+ W  m( G5 d4 a! b! }
  7. 4.0
    8 U; z2 x' F3 I" f
  8. >>> math.sqrt(20)/ G" }! q7 l0 U$ N2 J
  9. 4.47213595499958
复制代码

& H8 K8 x7 e0 h# v* x6 m4 V3 M0 }+ k2 emath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    * Y  T( i8 S+ L  L7 y# x, R
  2. trunc(x:Real) -> Integral# T6 X5 G% \5 s+ F) E$ ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% q2 L& i7 j* K# y  x. q6 Y, a- _
  4. >>> math.trunc(6.789)
    ' d. T# M! p) X9 S0 w5 Y% l* ^
  5. 6% l5 I: g7 |; f3 H0 W5 D$ G; H( W. U
  6. >>> math.trunc(math.pi)
    9 T  O( ^5 F! _9 ?6 i1 e
  7. 3
    6 k: T$ ~& z+ E, N. M! b
  8. >>> math.trunc(2.567)% b6 K$ t$ ]; j4 @  D" q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-22 22:15 , Processed in 0.087141 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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