新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
9 I! U0 K1 v) b$ w
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。) F1 `  H% s+ g# x- v! k1 J

/ B4 B" V; y; \/ X方法1
2 ~1 F2 s/ E" b7 a& v# A
  1. >>> import math
    0 a" \* V6 s! q/ V# D6 ?
  2. >>> math.sqrt(9). w0 f2 J) O) ~' ~4 ?" O1 N! n
  3. 3.0
复制代码
方法29 m5 l1 h' E$ j
  1. >>> from math import sqrt
    0 W; f% r* U0 M1 r
  2. >>> sqrt(9)' j+ a1 i5 m0 y9 x
  3. 3.0
复制代码

% [* k( o* @' r' M
" ]6 j# o/ D; H+ r
math.e  表示一个常量6 J' i: c. q2 q5 M8 G9 g
  1. #表示一个常量
    2 Q. O+ I% K& r) z  s0 z; T; a
  2. >>> math.e
    9 N( L9 i5 |. G# n" s8 o
  3. 2.718281828459045
复制代码

* M, I" J: O! L- Bmath.pi  
数字常量,圆周率
5 i8 g, a+ D5 C! V' U9 {# U
  1. #数字常量,圆周率# t3 Q* z' r0 A/ A' p* l
  2. >>> print(math.pi)
    ( }* N0 N# U- i+ v4 V+ [2 y
  3. 3.141592653589793
复制代码

3 U" J( K) j0 a$ o0 ]; Zmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
& |' T% U3 P5 ?+ g: P' c. o4 a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x' L3 m3 I  K2 A/ N0 Q2 H
  2. ceil(x); t5 D9 Q& l$ @3 b* ?3 z  i
  3. Return the ceiling of x as an int.
    # ]" e" Q: |" k5 B6 Z+ \) w2 f
  4. This is the smallest integral value >= x.9 b+ K* j. I/ W+ h( L
  5. & R) O/ L' B; e* q
  6. >>> math.ceil(4.01)
      H% U3 T3 G# _* }& e8 f
  7. 5
    & c- l, l; r  a. T  [1 ~3 }6 Q
  8. >>> math.ceil(4.99)
    $ j- m( T9 M0 F1 S+ N, i+ f- A
  9. 58 Y' s, n8 E, C
  10. >>> math.ceil(-3.99)/ R2 L8 @4 \$ G+ i9 F
  11. -3
    ; j' d8 o0 y8 o0 M
  12. >>> math.ceil(-3.01)3 o+ U6 j; _8 G$ P0 B
  13. -3
复制代码

0 F/ E$ ^9 Q8 ~4 j7 mmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! g2 x' @3 `+ A3 Q. Q9 `" C
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    " ]- }+ f: Q0 O2 |1 q8 S
  2. floor(x)
    $ d9 w8 z% d6 C( a
  3. Return the floor of x as an int.
    8 Y/ m" v1 U) b% \, M
  4. This is the largest integral value <= x.
    5 v& j* P- @% Q
  5. >>> math.floor(4.1)5 ]  E  M( o7 @
  6. 4
    9 g' ]" ]. H& s3 }/ k! T" z1 c
  7. >>> math.floor(4.999)3 b5 y9 M! {. q* ^
  8. 4/ B1 x' I3 @  o" x1 X  o( x
  9. >>> math.floor(-4.999)
    4 T( B4 R3 k( d* G- K( I& z& O
  10. -5
    4 q0 ], \$ ?$ g; H! I
  11. >>> math.floor(-4.01)
    1 ^8 t/ W: O0 v7 G0 S/ c. n2 u0 E
  12. -5
复制代码
8 P8 Q" X3 o& i/ b/ A! H
math.pow(x,y)  返回x的y次方,即x**y
. }/ x5 v" n5 h
  1. #返回x的y次方,即x**y8 B9 P$ o( y! h3 N7 q
  2. pow(x, y)
    9 r5 U  k3 B$ h) {0 z) N
  3. Return x**y (x to the power of y).
    " p- A) W- y. _
  4. >>> math.pow(3,4)
    & [$ s  ~2 P. A9 V9 J5 N- N. ~  E. s
  5. 81.0* O- r9 O* i  g, w& R* h
  6. >>>   A7 b1 @1 a" @7 e
  7. >>> math.pow(2,7)
    5 b! G, w' {# a, }" A* G3 X
  8. 128.0
复制代码
# ?. R$ g' v9 X; Z6 t9 R
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 K) K/ F, o2 H2 ^: R! I
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    , a' r9 \2 v/ W6 y( g& k* d# G/ w
  2. log(x[, base])
    0 Q: [1 ?' y0 D" L
  3. Return the logarithm of x to the given base.
    $ t# \3 [: b; P8 |
  4. If the base not specified, returns the natural logarithm (base e) of x.
    - z" @& s* e6 k
  5. >>> math.log(10)
    * Q" o. f) S4 D5 ~8 Z
  6. 2.302585092994046! z) V& \9 Y" K7 _! J5 G. [! p
  7. >>> math.log(11)
    1 d1 L8 W( i1 O2 H9 k5 r0 D
  8. 2.3978952727983707
    0 @$ N: I8 m- y: a. B+ I
  9. >>> math.log(20)
    ' J& p  P) I/ V& U0 \
  10. 2.995732273553991
复制代码
. [" j0 U& M3 K
math.sin(x)  求x(x为弧度)的正弦值2 N/ D" T& h" X4 [4 g. {& c
  1. #求x(x为弧度)的正弦值4 q6 y3 b! [0 c
  2. sin(x)2 }% V7 c6 z" u" Z. @8 E
  3. Return the sine of x (measured in radians).
    ) V: I8 j4 |8 y4 y, u* i
  4. >>> math.sin(math.pi/4)
    / E$ J- \/ o: k2 }# H$ ]0 V
  5. 0.70710678118654759 @+ \  _6 R9 t8 o2 T  `4 R  S5 B
  6. >>> math.sin(math.pi/2)/ z- ?1 e/ @, m8 |8 \
  7. 1.0- K, {4 f" y% _
  8. >>> math.sin(math.pi/3)
    6 `; h- o& o% E
  9. 0.8660254037844386
复制代码

+ Q* e0 |2 S" N( Lmath.cos(x)  求x的余弦,x必须是弧度
& c2 R. R! P: s, b7 Y) ]9 d* r
  1. #求x的余弦,x必须是弧度; J" ~$ c% K$ E
  2. cos(x)
    ' W# X4 P$ a9 E( P/ j+ u
  3. Return the cosine of x (measured in radians)." I" b3 p4 B) N4 P8 C
  4. #math.pi/4表示弧度,转换成角度为45度0 ?% X- S' D+ q: M" M# ~# h
  5. >>> math.cos(math.pi/4)
    : x  O5 {; Y' ~* ]) Y
  6. 0.7071067811865476
    0 M3 B; n9 `$ E
  7. math.pi/3表示弧度,转换成角度为60度" n2 R5 G6 }1 r2 g/ e2 z) r
  8. >>> math.cos(math.pi/3). A$ O& i, L& n
  9. 0.5000000000000001
    - v# o; D: |+ R1 i
  10. math.pi/6表示弧度,转换成角度为30度$ q# N2 _9 L5 b+ O- m
  11. >>> math.cos(math.pi/6)$ L+ |* Q3 k( h# T7 u; _) n) {
  12. 0.8660254037844387
复制代码

. t% H% L# g) u7 x( v5 g' lmath.tan(x)  返回x(x为弧度)的正切值
5 t3 G; d) Q. n* \7 d
  1. #返回x(x为弧度)的正切值) w" }- u' @/ g
  2. tan(x)4 c! q: p# a0 s+ }. `  x
  3. Return the tangent of x (measured in radians).
    # {+ K, h# t3 ?" ?! d& K; C$ _: |; e
  4. >>> math.tan(math.pi/4)) i8 k* ~3 \* r) I, f$ V
  5. 0.9999999999999999
    5 O: |6 E" C' ]% f: f
  6. >>> math.tan(math.pi/6)* X* i7 O1 U" ?' J
  7. 0.5773502691896257
    ! M0 F- @/ p6 k$ H0 q' G
  8. >>> math.tan(math.pi/3)
    4 s8 e7 q3 J2 a( _, I: m9 @# m( U
  9. 1.7320508075688767
复制代码
3 N! z& G; e3 r
math.degrees(x)  把x从弧度转换成角度
! w- A3 B+ t& ^
  1. #把x从弧度转换成角度
    % X" ]4 p) S6 J, M$ e5 v, N" m8 \
  2. degrees(x)
    3 [9 e; j; z3 B# [8 O% C
  3. Convert angle x from radians to degrees.1 S, i, p4 j" [' N" ?" e( z
  4. ! ^1 P  H" o1 g
  5. >>> math.degrees(math.pi/4)% S2 G! M. ~, D* ?: T) _6 k5 Q
  6. 45.0
    - @* I  S3 v8 a, g* E; r- @/ ]
  7. >>> math.degrees(math.pi)) \1 T! R4 R2 y& s& ]/ F# ~( _4 `
  8. 180.0( I) Z; W: ^, @# a- f3 G6 {8 _! H
  9. >>> math.degrees(math.pi/6)% j2 @. p3 i& [' t1 t" d9 g
  10. 29.999999999999996
    - M1 {9 a: [' Q' l, K0 Z4 E
  11. >>> math.degrees(math.pi/3). O5 y; S5 b9 q9 S/ r! `9 b' X
  12. 59.99999999999999
复制代码

2 R' n* [1 f: w1 p, e) cmath.radians(x)  把角度x转换成弧度* C( H$ j) m4 i8 K/ {. o
  1. #把角度x转换成弧度
    + U" Z5 o* h. J# f
  2. radians(x)
    2 i$ L3 @% k; z$ }
  3. Convert angle x from degrees to radians.; Y' ^( [" U( @. z3 j7 o
  4. >>> math.radians(45)0 I; @3 h2 l" V/ o; q
  5. 0.7853981633974483
    ' W3 x( G" l/ R! k  v$ V
  6. >>> math.radians(60)
    " |. Q# n- D8 x( d2 j; ?8 n" K5 f' |- F# H
  7. 1.0471975511965976
复制代码
. a% f/ p, h( s, }4 L
math.copysign(x,y)  把y的正负号加到x前面,可以使用0  i! }3 u( ?3 G7 t
  1. #把y的正负号加到x前面,可以使用0. Z! ]2 G" K  R0 m" o+ K% L
  2. copysign(x, y)
    3 P4 j( a" C' J
  3. Return a float with the magnitude (absolute value) of x but the sign + P+ ^  M; l5 ~7 h9 j) K
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 D; s0 b# k% g5 q7 y
  5. returns -1.0.+ S9 s( }" ?. Q/ |
  6. / V+ s& |3 q; t
  7. >>> math.copysign(2,3)5 L! p3 l# _9 _
  8. 2.0# P3 N# Z* j5 ]+ B
  9. >>> math.copysign(2,-3)
      ^2 p0 ^9 \- h9 c9 B3 Y1 _
  10. -2.04 U, C! K7 p/ u
  11. >>> math.copysign(3,8)
    & B( _2 _/ g& N# |0 m
  12. 3.0. z: Q6 o2 |' h1 i
  13. >>> math.copysign(3,-8)
    ; V  Y+ c( I; ~  H/ p* z  E3 J
  14. -3.0
复制代码

6 o2 b  p! u! @' ~1 k1 ?/ I$ M$ Hmath.exp(x)  返回math.e,也就是2.71828的x次方
6 j4 r" P; d8 n. x5 \0 r
  1. #返回math.e,也就是2.71828的x次方; |) ]0 K' r* @4 }: y  H) F
  2. exp(x)
    / c+ |, C3 a4 x8 T* n; \
  3. Return e raised to the power of x.# g3 V. e+ ]; [0 X( K7 K; W
  4. 1 @: c9 n- V" r3 h( N
  5. >>> math.exp(1)
    # N. r! N: o. z$ Y9 G
  6. 2.718281828459045
    * ]. }6 Y5 _4 S& {7 ?/ e: _- M$ Z
  7. >>> math.exp(2)5 |" A% O% {' u' s! x0 w
  8. 7.389056098930659 c4 G$ }3 y% j9 W4 g0 ~, K/ X
  9. >>> math.exp(3)
    ' K; `' c: Q2 R8 v
  10. 20.085536923187668
复制代码
# H( L$ S" ]4 J. e2 u6 O+ Q" f
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
) p! `: s2 N7 }4 X) r6 {
  1. #返回math.e的x(其值为2.71828)次方的值减1
    7 t9 @5 G+ ^2 J0 P, P* H
  2. expm1(x)
    ' \, D$ |* A: ?0 J" }: W9 y
  3. Return exp(x)-1.8 H" D0 h. i& t  D6 @
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    6 y- }8 o8 B( i( g9 u8 \3 Y8 q
  5. 9 j2 a+ X- E4 B, B: C
  6. >>> math.expm1(1)7 x6 L) F1 `) O* W
  7. 1.718281828459045
    5 K: u6 j" G/ a, C
  8. >>> math.expm1(2); P4 b; l3 G& i# {: V
  9. 6.38905609893065
    ' R, l' l  w: ?) M
  10. >>> math.expm1(3)
    + x" e; |( q' \5 }
  11. 19.085536923187668
复制代码
) f+ g0 ]- R. S& @. \5 d2 Y9 C
math.fabs(x)  返回x的绝对值
' q' \; @/ e0 }% W; m% x, N
  1. #返回x的绝对值/ }7 {( _7 @3 J; G: q. W
  2. fabs(x)+ K& K% c, C! [  d$ ^+ y
  3. Return the absolute value of the float x.3 |; u; K( w2 {* j! l) s
  4. - Q* @9 ~" [: g
  5. >>> math.fabs(-0.003)  m- i# M) ]' l" p* f: y% ^" n
  6. 0.003, v, y5 `/ F0 s) H/ |# u) ^2 [# f
  7. >>> math.fabs(-110)# Z& v, d" i6 _8 s( t9 Q  O' w
  8. 110.08 I9 i" E4 `% }. F" G! X: ^
  9. >>> math.fabs(100)
    5 {8 \+ J1 H, G1 F( z$ F
  10. 100.0
复制代码

6 F0 y2 [' T& q1 w4 L- [+ xmath.factorial(x)  取x的阶乘的值
3 q0 e! Z$ R6 g: X. l% _
  1. #取x的阶乘的值
    " i8 h, l2 _; @$ I) J
  2. factorial(x) -> Integral' E9 e3 W; ~+ a3 M( N
  3. Find x!. Raise a ValueError if x is negative or non-integral.
      d: P0 s( v/ [
  4. >>> math.factorial(1)
    # V6 t6 r: d6 S
  5. 1
    ; b" D0 A* K/ ^) Z: m
  6. >>> math.factorial(2)
    3 P4 D. S6 _3 {' W
  7. 2
      `& l( R( L3 x; {( C# B
  8. >>> math.factorial(3)
    % l. U2 {+ ~" }3 P( o# w3 t' u# [& |
  9. 6
    / p* Z/ i/ \& L; L
  10. >>> math.factorial(5)
    ; t  p" \- F/ ^+ }& I( U  [6 R
  11. 120- u4 i2 i6 h6 Y2 l# j
  12. >>> math.factorial(10)- q5 p' \: r6 C
  13. 3628800
复制代码

" f1 \& u1 w  G' Lmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
, o4 e, P  g$ O2 g: k; ?6 N0 w
  1. #得到x/y的余数,其值是一个浮点数
    2 W- W2 |: r' q; w1 w* e
  2. fmod(x, y)0 S  `+ ^) `7 X$ H0 v0 R* s; [1 ~
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    : Y6 f2 N" P7 j1 P
  4. >>> math.fmod(20,3). M5 F  }5 _# Q7 B6 D; Q, L- J
  5. 2.0
      M: Y5 d4 A; H4 W8 m
  6. >>> math.fmod(20,7)
    + H3 E3 @( c- }
  7. 6.0
复制代码

& d8 W3 W" a6 b9 Qmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: ]% q# _, ?5 `1 z. Q- U
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,, ~% S8 I1 B- t. r2 B& y
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    # |  A' _1 Z1 N* `
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 E6 |2 y; Y4 z7 C( U% {
  4. frexp(x)) X* M1 }2 y' ]! C
  5. Return the mantissa and exponent of x, as pair (m, e).5 e7 _+ D# n% \" m/ X, b8 U
  6. m is a float and e is an int, such that x = m * 2.**e.
    ! V2 P! w8 F: E$ h
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    3 h) m. D/ ?  J( o9 p
  8. >>> math.frexp(10)
    8 l& W# {. C' T% T; T, Y
  9. (0.625, 4)4 q$ _5 P+ i% @7 A
  10. >>> math.frexp(75)
    / ^5 |0 w  W3 u: F5 a$ h- B2 [
  11. (0.5859375, 7)
    8 B% s' d1 F3 F
  12. >>> math.frexp(-40); f) H4 i6 G2 N) h$ e4 u. l
  13. (-0.625, 6); p6 Z. k$ [* R
  14. >>> math.frexp(-100)/ {+ r, ^$ Y+ H9 }' y% E6 ]# s  o
  15. (-0.78125, 7)1 w* @" m, ~* |0 s+ z, o# L9 B
  16. >>> math.frexp(100)
    2 c2 o' F. l6 n1 ~1 l- I' `. p. Y
  17. (0.78125, 7)
复制代码

4 ^8 P& n$ @1 a. f7 `math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
+ G/ A4 r. m% }
  1. #对迭代器里的每个元素进行求和操作6 I4 K& L/ G; K/ r7 O
  2. fsum(iterable)
    4 P, @% `) J& q- E) J1 q
  3. Return an accurate floating point sum of values in the iterable.8 a$ e8 M/ |3 {1 M( c# s4 q9 K
  4. Assumes IEEE-754 floating point arithmetic.* N$ N' |; z0 N* M. b. v) s0 q
  5. >>> math.fsum([1,2,3,4])% l/ B; D1 }' }9 g1 p" k
  6. 10.0- h" {6 T  m  k  y- n! o
  7. >>> math.fsum((1,2,3,4))  x! L0 S4 {9 @9 j8 W' e0 @
  8. 10.0' V1 g- d9 K- g  G! w. U
  9. >>> math.fsum((-1,-2,-3,-4))
    ! d( {+ b# [. ^" E" L
  10. -10.0
    0 k5 N) b) a, g' c& d6 U, ^* k0 W
  11. >>> math.fsum([-1,-2,-3,-4])
    ' ~0 V+ l8 z( r0 r2 N0 D, c
  12. -10.0
复制代码

; P" d* ?% a( J4 Xmath.gcd(x,y)  返回x和y的最大公约数
6 N: J+ o* l. `/ S
  1. #返回x和y的最大公约数( d4 }& f% V3 x
  2. gcd(x, y) -> int
      S. u- X. m: U
  3. greatest common divisor of x and y& o2 v3 R' O4 P, e# }. K
  4. >>> math.gcd(8,6)
    ! \+ d! Y% `  d
  5. 22 H# X  {; u# G) t) R; k
  6. >>> math.gcd(40,20)( }  m$ ~9 w8 ^9 \7 X; `
  7. 20
    8 i0 Y" W( Y2 N3 b4 w& \
  8. >>> math.gcd(8,12)6 v2 K) [4 n& F4 K0 P1 r5 [
  9. 4
复制代码
& N, ]* ]. i$ m6 q* w
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* d9 @. v- F3 ~2 f2 B7 M( i4 o
  1. #得到(x**2+y**2),平方的值
    2 Q5 Q1 D. i( S. {
  2. hypot(x, y)6 i, L3 o/ X# T# ?! K9 ~
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    " |1 J. {0 k5 J2 s- y3 \, Q
  4. >>> math.hypot(3,4)
    " I/ a% U' f6 l% |& I7 ]
  5. 5.0- L! e9 M$ y7 G/ Q9 M4 D
  6. >>> math.hypot(6,8)
      v: S1 Q* j7 b
  7. 10.0
复制代码
4 z6 t# k! t0 g! C5 ^# p5 K
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False( ?7 ~% e/ J5 S) s, h
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    / r4 s* N* Z5 i) N
  2. isfinite(x) -> bool
    - L7 w1 \! F) P3 P& Q+ e% {
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' K3 _; m8 M, R1 v8 G
  4. >>> math.isfinite(100)3 n! n# V+ H/ J# w, d: d% A& l
  5. True. r; _* z. i7 x% A% I  Y( T
  6. >>> math.isfinite(0)+ j" m6 M: Q7 ]* t) m4 U) a
  7. True- B5 H7 Z3 n' }- E; G
  8. >>> math.isfinite(0.1)3 Y, E7 d' x3 u6 j
  9. True! g$ D: i  L: O* G+ w+ n2 s
  10. >>> math.isfinite("a")8 _$ P: y, F" U% W  B/ L: \8 b
  11. >>> math.isfinite(0.0001)
      j$ ~& \2 I3 ?, g0 ]
  12. True
复制代码
, W. ~+ O5 \' {  g! l
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
3 H# V  }- \' h1 M& [
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ! ]5 d) t# q& o( ?
  2. isinf(x) -> bool
    % G& u9 t- F! o9 a/ |( v5 A
  3. Return True if x is a positive or negative infinity, and False otherwise.& O0 w2 }, \9 C, Q# {: J) U; V
  4. >>> math.isinf(234)0 `7 f( R2 E+ m# H  @7 u7 Y
  5. False
    . K  B4 T. W0 O* @& i) v5 q0 u
  6. >>> math.isinf(0.1)
    $ m+ D( X) l" S9 V& u3 ?
  7. False
复制代码
4 q+ H3 A7 L8 M
math.isnan(x)  如果x不是数字True,否则返回False
$ k- F9 Q; i- J5 {7 o2 J2 o
  1. #如果x不是数字True,否则返回False
    ( p9 q" a, x% R, e" X7 f0 V# _
  2. isnan(x) -> bool
    9 H" u9 n, K1 C% ]" v
  3. Return True if x is a NaN (not a number), and False otherwise.0 V6 m/ b, i- f2 u, X
  4. >>> math.isnan(23)
    6 u+ O) w7 B& T7 a
  5. False
    3 x4 k: q& z- h
  6. >>> math.isnan(0.01)
    * `) D2 W8 i* W2 ]) N
  7. False
复制代码
2 {# I, f6 f, z5 i
math.ldexp(x,i)  返回x*(2**i)的值! ^' L  w0 X9 ^7 x/ G
  1. #返回x*(2**i)的值
    ( b( l9 X) |6 Q0 Q" l
  2. ldexp(x, i)
    7 P/ z6 z0 x! n( ^
  3. Return x * (2**i).) Z1 K! t3 u# [, \7 _
  4. >>> math.ldexp(5,5)
    , V7 k" @) p6 e$ M
  5. 160.0
    * y1 o& Q, |+ D4 L( Y+ L
  6. >>> math.ldexp(3,5)
    ! j0 T( i: _8 f' Q& h3 h
  7. 96.0
复制代码

1 C5 `2 T6 |4 o0 h: h# Umath.log10(x)  返回x的以10为底的对数: d$ t3 X$ F* X7 A( c4 ?( U
  1. #返回x的以10为底的对数
    9 ]' o1 r# A( J9 L4 m
  2. log10(x)
    . G% e% D+ \$ B4 u1 l  ^: t. ^
  3. Return the base 10 logarithm of x.3 F6 E, ~* U- V2 x$ u
  4. >>> math.log10(10)/ O/ V, T: F$ E' y' O/ R; c( {+ e
  5. 1.04 K1 t5 Q) X2 E* E
  6. >>> math.log10(100)& M. Y- ]1 Z+ ~* D# l
  7. 2.0
    * }; S6 F6 a! N6 d; n/ o
  8. #即10的1.3次方的结果为20
    9 p- b  l; A: n& H7 L) i
  9. >>> math.log10(20)
    2 a) z0 Z4 h4 ?/ U, r6 V+ n/ ~
  10. 1.3010299956639813
复制代码

+ X& N4 ?$ q7 cmath.log1p(x)  返回x+1的自然对数(基数为e)的值* q. V4 G+ y! T! ?7 U! f: A
  1. #返回x+1的自然对数(基数为e)的值
    1 t' N8 D; c7 ^; c5 b( d
  2. log1p(x)
    ! W  p: B+ P! g) ~) _7 O+ Y) `
  3. Return the natural logarithm of 1+x (base e).
    # Y9 V( i8 {8 a) X+ _4 Y$ [" R
  4. The result is computed in a way which is accurate for x near zero.8 W, t3 O( F& m7 c) m% a; U; \
  5. >>> math.log(10)
    9 b+ K1 f. |& t) u7 K
  6. 2.302585092994046
    , S3 T; U6 z' N0 b
  7. >>> math.log1p(10)) l/ M: F) q$ [- W
  8. 2.3978952727983707
    4 o* C0 |; t7 |
  9. >>> math.log(11)
    ; J! ]+ X5 c& n7 ?2 P
  10. 2.3978952727983707
复制代码

  W' t; X% c/ _. M- D( P: Rmath.log2(x)  返回x的基2对数! k1 S' }; P5 l+ x: ]- n: B" X
  1. #返回x的基2对数
      l; G/ n/ S% P
  2. log2(x)0 i1 W# i, c3 G, ^$ I9 E- ]0 e
  3. Return the base 2 logarithm of x.
    7 Z/ _# J& d2 }, _
  4. >>> math.log2(32)
    ) B  S& z8 a; G6 \" U& f
  5. 5.0  U4 l& W! z3 b2 W- \
  6. >>> math.log2(20)+ {" L8 Q4 ~4 T/ O' D( Z
  7. 4.321928094887363; M/ o  o- i0 T  _% @" @
  8. >>> math.log2(16)
    6 R4 Y8 \( @+ A; u9 G
  9. 4.0
复制代码

5 R$ V$ _% H! d5 ]5 ^math.modf(x)  返回由x的小数部分和整数部分组成的元组
" ^/ V+ q+ s3 P* o$ j0 P6 W  f8 r# s
  1. #返回由x的小数部分和整数部分组成的元组1 T6 S! N) n$ Y) C
  2. modf(x)7 m/ ~6 Q: l! x5 E$ [: X
  3. Return the fractional and integer parts of x.  Both results carry the sign9 i$ M: m1 b% T. x
  4. of x and are floats.
    * G, l: ?5 w2 ^) l* E( I( A% N
  5. >>> math.modf(math.pi)
    ' |0 N7 K- k  {9 @; @# N/ O$ T
  6. (0.14159265358979312, 3.0)# _. L- p/ X. q; B
  7. >>> math.modf(12.34)
    9 W  Y4 @! \* ?; u* I) }3 k. |
  8. (0.33999999999999986, 12.0)
复制代码

2 |$ i. b/ U/ P3 _6 smath.sqrt(x)  求x的平方根
% T+ j* @' D# W: T, ~" @' i+ Q
  1. #求x的平方根3 n9 D' I: a6 t1 R
  2. sqrt(x)
    ! p( `* x; k3 V0 z4 P  x
  3. Return the square root of x.
    7 d9 @( A3 Y$ L) f8 E2 Y4 g! y1 T
  4. >>> math.sqrt(100)
    ' P! a5 c. X& _" `8 q) M* f$ r
  5. 10.01 }, `; `, T- h- I
  6. >>> math.sqrt(16)
    + k+ r# c4 e5 H% ]
  7. 4.08 L. u/ n% |$ I
  8. >>> math.sqrt(20)9 t# {% C& C0 K# t/ c" {5 }9 [
  9. 4.47213595499958
复制代码

: Y* T* b! C( S$ _* X* `& cmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    1 u  Z) B6 I( o% z5 g% \7 Y* h
  2. trunc(x:Real) -> Integral
    1 Z. U( |7 N2 Z6 [2 X5 R- ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    3 R3 e( R) E# L. K* U
  4. >>> math.trunc(6.789)8 M$ e" [9 y2 [4 E5 [1 ~, z1 ?1 k# D
  5. 6; {9 [, m7 ^- U0 _. d3 J( |, a
  6. >>> math.trunc(math.pi)0 K1 w( Z: [6 u- b' \2 F
  7. 3
    , n0 Q2 x! c# A+ ^' @
  8. >>> math.trunc(2.567)0 O5 g, A5 J! Q- g7 k  x
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-14 14:06 , Processed in 0.091206 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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