新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
% J; x# ~4 F( A1 D8 m) h' x
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 q' |# L- y; U9 P, H

' {! E& U% A. `$ G8 Z* J方法1
- D, i: E& E8 B1 ?+ J
  1. >>> import math6 l: a9 Y( F3 V8 J5 W' v
  2. >>> math.sqrt(9); P5 x8 t; _4 O. J7 o) s
  3. 3.0
复制代码
方法2
( P  {* ?/ A% S
  1. >>> from math import sqrt
    ! k8 I& d0 C7 O6 d7 F
  2. >>> sqrt(9)/ b! d$ J0 [) V# F3 p
  3. 3.0
复制代码

$ M+ y3 `9 F7 k( P
6 Y2 ^1 v$ I1 u8 L. u+ _5 J* ~6 `/ c
math.e  表示一个常量
+ j7 h$ p  ]3 ^( t6 a6 {
  1. #表示一个常量
    3 }* ?. G1 g7 ]6 \( v1 v
  2. >>> math.e- Y+ E2 ^) H$ k: R
  3. 2.718281828459045
复制代码

: X- W% e% \2 ]* w7 p# Qmath.pi  
数字常量,圆周率
& E( u7 M9 o8 z9 e% u% \
  1. #数字常量,圆周率# y. V4 u- C" r. I. d, |  t
  2. >>> print(math.pi)
    6 w3 E0 S' N# R
  3. 3.141592653589793
复制代码
9 I' l2 t' f: ^7 f
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
: m# ?; w6 S! H5 M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x( l2 T  c9 Y% G
  2. ceil(x)
    2 Z0 }; J( x9 v& I- M# M
  3. Return the ceiling of x as an int.
    2 z& ~/ f* L! }& |
  4. This is the smallest integral value >= x.
    ) I: ^* `7 ^) p+ K8 ?0 f9 a+ [

  5. - a" _( o0 e* x2 p! }& z% i
  6. >>> math.ceil(4.01)
    7 A8 ~4 A/ d6 ^2 b' x
  7. 5
    ( I2 O3 O2 q/ c" Q. p
  8. >>> math.ceil(4.99)0 o1 u- @# k6 b/ E+ _2 z: J
  9. 5
    8 r$ e' ?6 G) v2 j' }" N* C
  10. >>> math.ceil(-3.99)7 t+ \) l. V2 Q% A
  11. -32 i: ]2 ^; G4 C
  12. >>> math.ceil(-3.01)7 C1 J$ L/ H4 l& W/ _. A/ p
  13. -3
复制代码

# O0 D* o# Z: l4 F* e  J  V" Imath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& g: k& S1 E" ^5 y
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 s2 ]5 a" z: U6 ?# u
  2. floor(x)# F0 _+ w" ?- W4 D+ G
  3. Return the floor of x as an int., R( o! t! N1 g# z) I; v- u
  4. This is the largest integral value <= x.$ G8 X" l8 F7 q) B
  5. >>> math.floor(4.1)
    1 a# f8 z" f# x1 i7 f0 v
  6. 44 t. z! E; Q' @
  7. >>> math.floor(4.999)
    7 i$ H% x$ s% S1 l- V$ u: b( j# W
  8. 4" n& R& A% a& E) b' o
  9. >>> math.floor(-4.999)
    - n9 f5 w5 u1 O* [2 I, V  e
  10. -5
    % z9 |8 z6 m) A6 `$ B
  11. >>> math.floor(-4.01)& w4 Q1 j5 h: r( h9 [
  12. -5
复制代码

& B; u9 \* j* ~$ P- L3 `math.pow(x,y)  返回x的y次方,即x**y
4 p  h1 U/ m, A) r; l
  1. #返回x的y次方,即x**y
    * p* U# b8 O2 v% F( k' \  O
  2. pow(x, y)5 Z7 d7 o8 K0 @7 h
  3. Return x**y (x to the power of y).
    * M# a* [3 e6 `4 j$ Q  B
  4. >>> math.pow(3,4)
    1 |1 K  y4 Z* x% P
  5. 81.0
    9 e/ F& C2 F9 n  Q2 \4 m; h( T$ h+ ]: V
  6. >>>
    / C8 _5 m; g! @% c* X6 S
  7. >>> math.pow(2,7)5 v& M9 N; b8 Q4 ?3 O; K7 q1 a
  8. 128.0
复制代码
# \: }; C: O+ p- K( v* w8 h
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 @, u+ B- R8 u  U
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) \' q- W, W" T7 p1 Y, y$ J) o2 D) L0 p' m
  2. log(x[, base])' Z1 t3 l& D! b: c- b0 D7 Z5 r, g
  3. Return the logarithm of x to the given base.
    0 z  t6 B/ `3 M& h" W4 u0 c
  4. If the base not specified, returns the natural logarithm (base e) of x.) p# A- y+ p; _
  5. >>> math.log(10)
    ) e! `. R, ^7 M4 f  t( K3 `' C
  6. 2.302585092994046
    # m4 U, t2 w+ _9 t0 M/ O4 v1 h: V
  7. >>> math.log(11)3 n) m3 b2 X, Z  z% y; E, w
  8. 2.3978952727983707" \( [) D& R6 W$ a- b
  9. >>> math.log(20)
    + b# ?6 Q7 z- a) W% T( u  L
  10. 2.995732273553991
复制代码
3 _+ {3 t4 D! Y
math.sin(x)  求x(x为弧度)的正弦值& S) A( K' u9 `' q; L, r( a+ n6 x
  1. #求x(x为弧度)的正弦值8 H6 T5 d3 |1 ?8 ~1 U# S/ I: b
  2. sin(x)6 U% h6 ]( C3 w/ E% D* t5 n- h
  3. Return the sine of x (measured in radians).% A  M7 p% e5 ~5 s. L
  4. >>> math.sin(math.pi/4)0 a8 ^9 ]5 s* o4 o- I
  5. 0.7071067811865475
    2 |% M) R) T# q2 |5 F
  6. >>> math.sin(math.pi/2)
    ' ~1 m3 u" H/ ^0 v4 M
  7. 1.0
    ( y" K) p) M1 n& q8 i. S( @* H
  8. >>> math.sin(math.pi/3)
    * R. g' b2 a6 Q% d
  9. 0.8660254037844386
复制代码

6 A2 X$ L. {$ ^" }4 ~* Dmath.cos(x)  求x的余弦,x必须是弧度
8 j6 ?! |0 x6 P
  1. #求x的余弦,x必须是弧度$ f7 x5 l8 X; H" \% j) J# m
  2. cos(x)
    % O) L3 ?) z7 b9 x% z2 P# `
  3. Return the cosine of x (measured in radians).
    3 j$ I" J) V- O5 x* N
  4. #math.pi/4表示弧度,转换成角度为45度
    ; u8 c4 j& J5 |# y2 I  x: O
  5. >>> math.cos(math.pi/4)
    0 F+ Y8 K4 w% F) K) B; L6 Z( X
  6. 0.7071067811865476" V9 D- b2 v' `- _: S
  7. math.pi/3表示弧度,转换成角度为60度
    $ i4 W& o! T  k( r3 O& R7 |
  8. >>> math.cos(math.pi/3)
    " ~; {0 o* j7 N
  9. 0.5000000000000001& \5 f) O9 i$ ?6 @
  10. math.pi/6表示弧度,转换成角度为30度( L7 P9 a7 z7 C  K, `& _3 L
  11. >>> math.cos(math.pi/6)
    ; T: H+ h: Y# k* y1 ~$ T
  12. 0.8660254037844387
复制代码

4 a+ H" `  H3 N. U( c! dmath.tan(x)  返回x(x为弧度)的正切值$ A, a$ h* p/ N5 o- L
  1. #返回x(x为弧度)的正切值
    ) S5 p/ r- R$ x$ \: u  m5 Q
  2. tan(x)& u, w% P  x: P6 F
  3. Return the tangent of x (measured in radians).5 x' t9 u7 c- {5 y
  4. >>> math.tan(math.pi/4)
    6 `; J7 ?. a7 n# |0 X  [" v! e
  5. 0.9999999999999999
    + b( O( Y) X! a) O) o
  6. >>> math.tan(math.pi/6)% G% ]# x* N# z1 N6 w- Z, R
  7. 0.5773502691896257) m/ ]' q) A  J+ I# ^$ F  z
  8. >>> math.tan(math.pi/3)1 _& [  ~, o: e6 m  a+ Y. L$ \
  9. 1.7320508075688767
复制代码

+ z. j/ |* u8 F5 N$ G* ]  Amath.degrees(x)  把x从弧度转换成角度
" j# R% c: V; Y  H0 z% r
  1. #把x从弧度转换成角度( F; w9 b& m( B+ j2 H" q
  2. degrees(x)
    ! L/ C2 }5 M: ?8 q
  3. Convert angle x from radians to degrees.
    3 X, {. V# a& @7 x3 F
  4. : G9 k# [2 N& k" _. Y
  5. >>> math.degrees(math.pi/4)
    & E4 a! P8 N$ O' m. W6 P  H" }
  6. 45.0
    $ {, p8 e$ _/ h; b) p
  7. >>> math.degrees(math.pi)
    6 ^  Q. a- b. _, }/ P
  8. 180.0
    5 C) V7 q1 P5 Q1 |  v9 w
  9. >>> math.degrees(math.pi/6)% K( U# B2 h1 B: a" X$ t8 o
  10. 29.999999999999996. r3 ?: y1 x' S9 p3 L3 U
  11. >>> math.degrees(math.pi/3)1 ], O; C/ W( x1 l% @8 v
  12. 59.99999999999999
复制代码
: x# B; e3 D  `+ e
math.radians(x)  把角度x转换成弧度
; ^1 F6 x& `" A
  1. #把角度x转换成弧度  U1 i; m! w8 B
  2. radians(x)
    9 j. O' ?# j5 Q5 I
  3. Convert angle x from degrees to radians.7 w& c8 Z& U4 n7 M
  4. >>> math.radians(45)% |6 i( W8 P6 V
  5. 0.78539816339744834 W( t" \  O2 ^4 E, ]
  6. >>> math.radians(60)% a" ^. p$ S- K# `8 J/ m3 g2 Y
  7. 1.0471975511965976
复制代码
; C# F! x- q/ I$ \% w4 o5 m8 m
math.copysign(x,y)  把y的正负号加到x前面,可以使用02 C' _9 i/ D6 J& |3 Q
  1. #把y的正负号加到x前面,可以使用0
      S6 F3 ~0 [9 l% ]3 M4 E
  2. copysign(x, y)
    # A/ z/ K( E7 W
  3. Return a float with the magnitude (absolute value) of x but the sign . @) @7 J$ b7 y9 n3 J& A# D, C
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    0 T# n- `* e- ?/ v0 n  D  v) ]+ \# t
  5. returns -1.0.
    ) j- Y8 h( T+ s' l1 W9 _5 O
  6. 8 E9 p! P; f* l& n
  7. >>> math.copysign(2,3)
    1 x: K3 e6 O( M
  8. 2.0
    , F% D& i2 ]( _# z% ]6 t4 E
  9. >>> math.copysign(2,-3)
    ; s; U9 i0 W- D  e+ `/ e) f* _
  10. -2.0
      R# i0 b6 f  s# l
  11. >>> math.copysign(3,8)
    : }  v* o- n5 x- J4 H% y& h9 v
  12. 3.0
    8 E  w0 l5 W! S- J- }3 l
  13. >>> math.copysign(3,-8)* S6 a& G: I$ b+ U( [' W0 S2 U- b
  14. -3.0
复制代码
& ^9 \* h: S. ^6 k
math.exp(x)  返回math.e,也就是2.71828的x次方
5 L+ x9 f% Y, T4 V' x$ T2 i) u$ P
  1. #返回math.e,也就是2.71828的x次方
    8 u: s* x% s) x5 Y* R
  2. exp(x)2 E! N% y/ ?) J+ T4 t, f% q
  3. Return e raised to the power of x.2 ]" Y6 r- h, Q& a

  4.   W+ L: u$ P' m- V, q: l1 X% P
  5. >>> math.exp(1)0 G- @% ^8 p0 B6 ?3 r2 v  t
  6. 2.718281828459045
    6 g* x8 L' y" {
  7. >>> math.exp(2)
    ! g4 |2 Q1 D5 W5 S- C
  8. 7.38905609893065% X  C4 |% v9 U" p- W" H+ h
  9. >>> math.exp(3)1 @2 X0 c, q+ q  l. s8 M: D
  10. 20.085536923187668
复制代码
/ c0 a4 N0 w1 x* Q1 r' D3 u
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
1 y, V1 b3 l, s1 Z, W- w
  1. #返回math.e的x(其值为2.71828)次方的值减1/ q- b3 m: M/ l7 Q
  2. expm1(x): p! s" A; |4 J$ Y$ t6 h) }
  3. Return exp(x)-1.6 [% K6 x+ I0 V5 T
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 u8 K* i3 y, v+ ]# b5 E" G
  5. 6 }0 p& n, _' ?# c1 S
  6. >>> math.expm1(1)& U* i! Q/ n, s* _, ]5 I
  7. 1.718281828459045$ b' d- Y& f* F, T$ O0 b7 f
  8. >>> math.expm1(2)5 I# {9 n7 q( l* q3 `9 Y& f8 {
  9. 6.38905609893065
    3 V2 M2 z4 V+ Q2 Q! D; _' O
  10. >>> math.expm1(3)
    - o1 m* e, d" q! |5 @
  11. 19.085536923187668
复制代码

0 [% ^8 v* R/ ^- ^math.fabs(x)  返回x的绝对值- B9 v0 ^# G  r1 H# W" b
  1. #返回x的绝对值9 Y$ {/ V7 ^0 n- Z) U) d1 m
  2. fabs(x)
    9 Z* J. O+ ]4 B' w( K& v) I: }8 a
  3. Return the absolute value of the float x.' ~1 o) K% U$ F) U1 [, Z
  4. + J3 k4 `6 [- x7 E, Y4 q
  5. >>> math.fabs(-0.003)4 H# B4 x" |5 E4 z! g, F1 }0 k
  6. 0.003
    . U4 i" e, X! z8 c+ P$ b
  7. >>> math.fabs(-110)5 o9 Z. G& b$ x5 T
  8. 110.06 y; @- S3 P3 R8 M) e
  9. >>> math.fabs(100), Q2 s5 Y+ }# Z) \, o7 D5 q
  10. 100.0
复制代码

0 |5 W. l5 ?* ^, |math.factorial(x)  取x的阶乘的值
* w+ }1 S' M2 s3 v: G$ q
  1. #取x的阶乘的值
    9 v! w$ _/ T! b& P3 w
  2. factorial(x) -> Integral( j6 t7 R6 v5 w4 y4 t4 ~& H7 ?
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    & \6 z; {4 m) c$ D
  4. >>> math.factorial(1)
    + {: O; ?/ e# J- s& _
  5. 1$ M/ _$ Z: ]. y3 p
  6. >>> math.factorial(2)
    3 {( S5 N8 f* e0 O  Y; s8 o+ o
  7. 2! N" X  R4 d4 D( f5 b9 X7 y
  8. >>> math.factorial(3)$ X  l' X) d* b3 G. Q" E' S
  9. 64 P# ~/ N5 j/ |* s
  10. >>> math.factorial(5), D. g; j) G3 Q  \6 W7 I0 i
  11. 120  a5 F& f" r& N
  12. >>> math.factorial(10)
    ( Z& Z# U5 ~5 H. ]1 g
  13. 3628800
复制代码

0 Z) S# [3 k" V: cmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数9 N- h+ a6 W' ?. H
  1. #得到x/y的余数,其值是一个浮点数
    4 Z. y/ H6 [0 X- ]# ~7 [" |
  2. fmod(x, y)
    " C: V1 B. g" Y1 |# f
  3. Return fmod(x, y), according to platform C.  x % y may differ.0 c. D$ e. i6 |/ r  D4 v( U
  4. >>> math.fmod(20,3)% u3 j( v2 Z5 g. p8 \: r% }( A+ A
  5. 2.0
    1 y; R4 o, R& N: h. z8 Q  I. }
  6. >>> math.fmod(20,7)
    ! ^" q( T( x( f3 ^
  7. 6.0
复制代码
' C4 _0 ^4 |. q
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 r% m: U9 P- |% ^
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,8 P% R7 |  ^8 ]$ f; |2 ~/ L
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值8 X9 {+ M; A' \2 \0 P
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, S& M' A1 C2 `' p2 s( Q) [0 y5 T; E
  4. frexp(x)
    1 C% L& n( g1 d  Q7 Q: o/ W3 u- ~
  5. Return the mantissa and exponent of x, as pair (m, e).# P8 K) y( E9 u+ e3 D9 V) J* Q) w1 w
  6. m is a float and e is an int, such that x = m * 2.**e.# y' P4 l% X! c% U2 s# P; p1 {
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.4 f* x7 W0 e: j0 K! H7 S' i& Z
  8. >>> math.frexp(10)0 z% ~9 G+ }/ U3 l
  9. (0.625, 4)5 l7 _0 q" c2 L
  10. >>> math.frexp(75)
    9 w4 M! M; k1 v+ a& Z
  11. (0.5859375, 7)# L1 g7 y$ [+ u. f: l1 _
  12. >>> math.frexp(-40)* v9 g8 L7 u7 }% s! ]/ J5 F$ \
  13. (-0.625, 6)$ p+ J. U+ w* \2 [6 U
  14. >>> math.frexp(-100)  q$ {9 Z& m2 Y" C- ^
  15. (-0.78125, 7)
    9 @4 x( e1 ?. [' j
  16. >>> math.frexp(100)& K& {( `2 e, X  I) u' V
  17. (0.78125, 7)
复制代码
- H* ~: Q+ H: k, d7 _3 R
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
; X8 T3 W! @2 B) \! d4 P
  1. #对迭代器里的每个元素进行求和操作
    . w' p) m+ e  V  R; q/ {$ Q9 F
  2. fsum(iterable)
    + v& _. _& Q+ d! U% v, k8 Q' @
  3. Return an accurate floating point sum of values in the iterable.% J4 \8 n# h# g; Z/ _& h: P$ C
  4. Assumes IEEE-754 floating point arithmetic.
    $ Q5 _! G2 t% B) p3 s/ Y
  5. >>> math.fsum([1,2,3,4])
    ( _1 A: e& y, m3 m9 O" G; P3 l; r9 E
  6. 10.0
    : a/ x$ r" n" I- {
  7. >>> math.fsum((1,2,3,4))
    6 Z# f! s0 V3 Y" ^
  8. 10.0$ E" y  L5 L8 M) ^2 z$ i
  9. >>> math.fsum((-1,-2,-3,-4))
      k- d1 _# U4 `% R8 i( q
  10. -10.0
    # h7 e0 g' e5 Z/ E9 A
  11. >>> math.fsum([-1,-2,-3,-4])
    + C0 F( A8 R$ {' C/ M& y" a& K
  12. -10.0
复制代码
/ y9 G/ i* ~! }7 Q. D
math.gcd(x,y)  返回x和y的最大公约数
/ E7 k" }9 ~0 k1 d- a, _
  1. #返回x和y的最大公约数
    4 k5 c& v: p0 s, j2 G
  2. gcd(x, y) -> int; ]* \) s  ~: l
  3. greatest common divisor of x and y
    8 W- x' f6 N& ^7 p. D
  4. >>> math.gcd(8,6)) D! ^2 O  L) @6 t# i3 p4 c% K
  5. 2! M: G* P7 M; A' k
  6. >>> math.gcd(40,20)& v# p6 _# }2 s/ g, Z7 J& r
  7. 20: G9 p( }9 j5 _) R2 Q/ R
  8. >>> math.gcd(8,12)) T) S' ?: r6 S3 M6 ~3 l2 N8 F" L8 b
  9. 4
复制代码
( \7 H* x( ?$ J2 |' E! S  D
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
8 C: ^( f" H4 S/ w9 z
  1. #得到(x**2+y**2),平方的值
    $ R4 k7 E7 n+ |" d  b* s; i
  2. hypot(x, y)
    4 m' s- q( a" R) u* Z& r
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    % \# d8 ^- \; j% v
  4. >>> math.hypot(3,4)0 S  E4 t/ w+ s8 B. S
  5. 5.05 j" n8 w, F# l4 ?
  6. >>> math.hypot(6,8)# m1 P5 A. [, ~
  7. 10.0
复制代码

$ k; _, O1 q, q8 B7 z5 r8 K3 j' _! Dmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 M% c" o3 H" I; w& Y& Q
  1. #如果x是不是无穷大的数字,则返回True,否则返回False/ u: d# i' P8 W3 M( s; X  w2 i
  2. isfinite(x) -> bool
    ' ]. f+ v- L1 t% l+ Z; v' b4 B
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    4 A' A7 y8 g% F5 w9 ?% A- n
  4. >>> math.isfinite(100)0 w/ r. H3 n8 t* L: }
  5. True
    . v( ?; Z% c' l3 i
  6. >>> math.isfinite(0)
    3 A: J% j. A! N+ w- @1 Z( _! ]# {
  7. True0 P3 K; c7 d/ Q% Q7 d2 o
  8. >>> math.isfinite(0.1)
    ! R) k4 {: E4 z$ M: _$ s$ Q% l  B
  9. True1 V1 ?5 B4 A) C' Q- o7 F; m
  10. >>> math.isfinite("a")( h6 d" a! ]/ e7 T3 F; t/ `3 q) A
  11. >>> math.isfinite(0.0001)
    . N0 [% A0 t, V
  12. True
复制代码

$ ]- X3 H% c( q0 R# Dmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ U& g! w0 V7 ?. Q
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    * x) A5 a" _3 k3 @- v. S
  2. isinf(x) -> bool. x6 W9 n4 T0 t$ c& j
  3. Return True if x is a positive or negative infinity, and False otherwise.& W: j0 q+ _; k# G8 D* e! H
  4. >>> math.isinf(234)3 w' B  ]2 V4 u
  5. False
    3 r* b  A6 p! B4 p6 H/ ^
  6. >>> math.isinf(0.1)5 J- y9 [& F2 T/ r* q0 ]
  7. False
复制代码
& G& {$ E# t' f6 {2 A
math.isnan(x)  如果x不是数字True,否则返回False
9 ?8 z/ U$ @' N) z! u
  1. #如果x不是数字True,否则返回False& ?9 Y# V2 g7 T4 |- A( t+ j+ T
  2. isnan(x) -> bool
    & ?' r8 G9 X: H7 R" \9 K8 t/ ]) R
  3. Return True if x is a NaN (not a number), and False otherwise.7 ?  L* w: b" G& {/ `
  4. >>> math.isnan(23)% R. Z# v$ a3 L9 T9 f: m% M& z8 w
  5. False1 f) c% |- B  ~& ?2 n$ N
  6. >>> math.isnan(0.01)
    ! x' F; `, D& y& r4 g. J. _
  7. False
复制代码
3 J: M$ V" p2 j( |! v
math.ldexp(x,i)  返回x*(2**i)的值8 O: a) `9 n) j2 P! K- F
  1. #返回x*(2**i)的值
    , P5 e( ~: t+ c! k% P
  2. ldexp(x, i)$ J! u* d* W5 n& |- D. F: |
  3. Return x * (2**i)." P5 \" v  K, X4 K8 c* }
  4. >>> math.ldexp(5,5)& o6 |# d$ [3 [# q1 v/ \; s
  5. 160.06 P) X( n) t, R# }
  6. >>> math.ldexp(3,5)
    + E" I  y6 g9 J4 S9 L
  7. 96.0
复制代码

  R% m' d- u5 R( U8 ]9 |: tmath.log10(x)  返回x的以10为底的对数
1 o7 P3 t! F. _' s) K
  1. #返回x的以10为底的对数
    2 y7 Z! b* ]! q; N5 C
  2. log10(x)6 s2 A( q2 @9 a% c
  3. Return the base 10 logarithm of x.3 _# O: x  V# i# {+ P, v  H2 A
  4. >>> math.log10(10)
    7 W$ A$ t% S" h! _: W
  5. 1.0
    1 z. r0 B* @; e) N# ]  J1 o9 P
  6. >>> math.log10(100)- Y- c& s8 d% |# c
  7. 2.0. g- x) Z1 k/ y, u' x
  8. #即10的1.3次方的结果为208 c3 [0 e5 _& e2 n8 f- K
  9. >>> math.log10(20): J1 ]/ u5 b2 j. I- y, K# N
  10. 1.3010299956639813
复制代码

% o" G2 j- s" \math.log1p(x)  返回x+1的自然对数(基数为e)的值
0 ?! z1 W  _" X7 \  m! c# A& T0 f
  1. #返回x+1的自然对数(基数为e)的值
    0 Y7 X- l1 g+ Y0 E5 d  ?0 c) n
  2. log1p(x)2 d6 O/ e& V, }
  3. Return the natural logarithm of 1+x (base e).7 Q! v; Y7 a3 u$ T* a5 h+ k
  4. The result is computed in a way which is accurate for x near zero.% _( D& [7 s; \8 K8 H2 S# U7 h
  5. >>> math.log(10)9 S: R& A7 _* S; F
  6. 2.302585092994046
    " {9 X/ y$ o0 O: u1 t: {. K0 e
  7. >>> math.log1p(10)/ W; K; ]! p5 _' R1 F$ k
  8. 2.3978952727983707% K5 d. `1 x9 g* e
  9. >>> math.log(11)4 j* g  h; O5 L3 e  n" C7 D' h, x
  10. 2.3978952727983707
复制代码

; T$ J; i* b3 V" K0 W$ Y& g% o6 Bmath.log2(x)  返回x的基2对数
- d  Y+ J2 S& r) G  ~; _& L
  1. #返回x的基2对数
    0 `0 x8 W8 R. B9 }" X; l) I0 f
  2. log2(x)
    7 {! i/ G- M0 @' O. I+ }1 R* @4 o
  3. Return the base 2 logarithm of x.2 k' X2 R/ a! s
  4. >>> math.log2(32)
    8 T  ?- s5 O" T
  5. 5.0& ?) _9 I5 h  ~9 t' H6 A/ n3 |" [
  6. >>> math.log2(20)0 e# f' r5 {) S8 ]$ F8 E7 o
  7. 4.3219280948873630 A0 b: f+ x1 c) B; |* C( u
  8. >>> math.log2(16)
    8 m& A! q" h9 ]2 Q3 S* M
  9. 4.0
复制代码

9 g+ i5 ]  H* ~1 }8 Y2 |3 Wmath.modf(x)  返回由x的小数部分和整数部分组成的元组8 z# w! S+ {7 u1 B. [3 P
  1. #返回由x的小数部分和整数部分组成的元组
    # i: O" l- V( N5 S/ ^
  2. modf(x)4 a* o' j0 \/ D" D# _4 f3 p- W
  3. Return the fractional and integer parts of x.  Both results carry the sign& e0 X7 I7 ^8 F' H$ F: T0 o' B  f
  4. of x and are floats.
    % I" x. }$ P# O0 B. u) X
  5. >>> math.modf(math.pi)
    + T0 ~+ C4 a) T& M. M! k3 k- y# ?
  6. (0.14159265358979312, 3.0)
    1 T8 \$ ^; K1 e1 k/ e$ {, P
  7. >>> math.modf(12.34)9 i* m. j. y9 u" n
  8. (0.33999999999999986, 12.0)
复制代码

) h5 y  a- _8 Z4 o) w' imath.sqrt(x)  求x的平方根  M; E6 q" A% F* l0 w( F
  1. #求x的平方根' Z; |  _4 T+ h+ b: b* W; _3 u3 f
  2. sqrt(x)8 |  C' j  W% |+ \
  3. Return the square root of x.
    / a9 B9 q. z( Y0 m1 I
  4. >>> math.sqrt(100)6 z' N! b6 I+ ], E
  5. 10.08 t4 z3 E1 C1 F7 S4 a
  6. >>> math.sqrt(16)  Y4 }5 Y) w9 H* S) b: u% p
  7. 4.0) D- h5 ~" K2 f  M0 S. n
  8. >>> math.sqrt(20)- I% L9 \) l) S8 `/ a+ ?( y
  9. 4.47213595499958
复制代码

4 E2 Z6 ?7 w# ^$ s  r( R) A" vmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    7 f  k; g/ l( G5 J, N- Z; q% ?% H
  2. trunc(x:Real) -> Integral* z0 G' P- k4 S3 U6 h$ ?0 P! Q" f. q
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
      \  n2 D$ L. k. Q
  4. >>> math.trunc(6.789)7 p+ ]2 s! f& J, H( _1 q
  5. 6
    ' h+ \- B! `1 g/ B8 k& _/ K1 A
  6. >>> math.trunc(math.pi)2 ~& |9 T- S% D  z
  7. 3  O( F3 p" w5 [: q* L- j( h
  8. >>> math.trunc(2.567)
    3 D+ d9 \. k5 Y; h/ P% B6 n
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-21 10:28 , Processed in 0.077245 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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