新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
# l! X, ~& B: b& @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 `' ]; \( {; o& }" b* l
3 l5 u5 r$ F( Y" ]
方法1
/ T- d0 J* U. E  r1 k
  1. >>> import math2 W, D2 e9 I" d4 a
  2. >>> math.sqrt(9)8 E5 W/ P( ~2 N) o1 F/ t1 o
  3. 3.0
复制代码
方法2- G2 `1 h! {! p* w
  1. >>> from math import sqrt
    ( ~6 Z  g/ `: _, D5 v9 C
  2. >>> sqrt(9)
    " w; T) ~1 U, V; I& x
  3. 3.0
复制代码

0 Q3 T: F( u8 P7 ~5 c

; F5 a- }7 h/ p- N5 t
math.e  表示一个常量
2 p+ _  e3 m0 H* \
  1. #表示一个常量6 m; I* M9 u  F) s
  2. >>> math.e
    $ t& a: C' A) ^% R6 x
  3. 2.718281828459045
复制代码
) J" q6 F1 F& e/ |8 c
math.pi  
数字常量,圆周率

4 m# o6 T8 c4 u
  1. #数字常量,圆周率0 I0 H, [5 i1 s% e
  2. >>> print(math.pi)& [$ j; E4 ~0 _' z6 s3 u
  3. 3.141592653589793
复制代码
0 _+ X2 i) Q" S% x7 E
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 |% F3 {) f# J% K6 t" r
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 l4 J* H( b6 S: E6 T
  2. ceil(x)+ ?5 _. z  }4 K8 W- u0 o8 a0 _1 k
  3. Return the ceiling of x as an int.
    + J( f9 s7 m4 C4 c7 O7 ]0 }5 s) \
  4. This is the smallest integral value >= x.
    ) z; ^4 V. V* M7 l

  5. , o3 N# d# O9 _0 f8 B
  6. >>> math.ceil(4.01): i4 X  G" v$ h$ n( [7 C
  7. 5/ M, E! J& x2 P8 W/ j' F; M2 b# C
  8. >>> math.ceil(4.99)
    # }( a3 v) _* e( h
  9. 5. g0 F; `: k8 }; o$ R# L9 N" \/ p
  10. >>> math.ceil(-3.99); r, I9 R  E/ G8 f  T* r* y4 Q
  11. -3
    $ }/ U" p# [4 `" Q
  12. >>> math.ceil(-3.01)* p$ \7 F6 _! V$ O
  13. -3
复制代码

. T% G6 O- L. c* Umath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 p* z8 \. ^9 Y- L( V" k
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    8 T" P% B! y4 r3 z9 E
  2. floor(x)0 v: ^- t. |# h- h
  3. Return the floor of x as an int.
    : E6 b4 ^9 p% J( W9 V: [
  4. This is the largest integral value <= x.0 y: e9 M! D  k" }' d! l6 j3 ?; V7 ?
  5. >>> math.floor(4.1)+ K7 `2 n8 q& @. ^+ j6 a
  6. 4- R* ?' Z  k% G$ b3 K: @
  7. >>> math.floor(4.999)
    ; j( b. X/ v0 n9 q2 _& W8 j/ ^. E
  8. 4- q9 @: o3 t1 u$ u
  9. >>> math.floor(-4.999); m7 O& k: y5 o6 \3 [; L8 i; m, A
  10. -5% v2 @0 q+ D, `. ]. c0 s
  11. >>> math.floor(-4.01): v4 f" K/ ]  A( O  H) k7 z
  12. -5
复制代码
: N+ p! E& o3 `1 F+ S" a5 x
math.pow(x,y)  返回x的y次方,即x**y. M  n. k9 P& r9 G) ^
  1. #返回x的y次方,即x**y
    * c  q9 h) j: g$ _
  2. pow(x, y)" T8 v- R; P$ J$ X6 D3 ~7 l3 R2 R0 o
  3. Return x**y (x to the power of y).
    ) x9 i2 W3 v7 G& e; `
  4. >>> math.pow(3,4)
    ; Z( n) P; b4 P! Z& L4 b5 {
  5. 81.04 V  W; ~& C1 u$ P: w1 L* C# X
  6. >>>
    3 M9 b& G) F1 \
  7. >>> math.pow(2,7)
    : S* R$ ^* p6 h& x6 v' Q$ p" t
  8. 128.0
复制代码

+ ~1 t' f+ x1 Umath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" ~0 g% M# p$ a& |0 E
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) C  l: j% Y* h' z  Q, L" {
  2. log(x[, base]): M3 a$ T" @% Y  B7 C3 S
  3. Return the logarithm of x to the given base.9 n7 K# n1 V0 o
  4. If the base not specified, returns the natural logarithm (base e) of x.9 }9 G& u; g! o) `1 K$ P1 l
  5. >>> math.log(10)
    2 Q  U# r4 F2 R: m5 u6 {
  6. 2.302585092994046  u+ S5 C0 n! {; ~% U
  7. >>> math.log(11)
    3 r3 n6 {! G( g) i8 l2 l. r# L0 o+ M1 Q
  8. 2.3978952727983707
      W0 y0 `& ]# f! r# O0 p$ }4 O+ k
  9. >>> math.log(20)
    " T, i9 ~# ]- D
  10. 2.995732273553991
复制代码
* Q( m1 n$ R4 I4 u; O
math.sin(x)  求x(x为弧度)的正弦值6 X: @* H9 }* [- a1 }
  1. #求x(x为弧度)的正弦值
    + P6 v9 g7 ?  E& D7 M
  2. sin(x). w0 P4 l7 A. H2 G  E2 p  N
  3. Return the sine of x (measured in radians).
    9 y% _' S3 Z# |: _6 q( Y* N
  4. >>> math.sin(math.pi/4), Y' Z& R0 U) c' Q# ]) t' K
  5. 0.7071067811865475
    , {  Y0 @/ l+ J
  6. >>> math.sin(math.pi/2)
    / ?4 N2 c% B: w  b  l/ ^" W; @
  7. 1.0
    / q9 s7 Z, H6 }* g8 o( ^
  8. >>> math.sin(math.pi/3)
    , I  i" h$ d+ }3 w  c
  9. 0.8660254037844386
复制代码
: m7 d' c6 X0 A  U0 @0 v4 w2 ~
math.cos(x)  求x的余弦,x必须是弧度
9 D- z  m7 s" [8 U7 o
  1. #求x的余弦,x必须是弧度7 o  v) N9 n9 d& m5 y8 A
  2. cos(x)
    & s* _  ?" B- v) N" V) ~0 b
  3. Return the cosine of x (measured in radians).
    . L  R* K- n: _6 c5 j
  4. #math.pi/4表示弧度,转换成角度为45度8 {+ ^" T" r4 q+ L4 {1 W
  5. >>> math.cos(math.pi/4)
    7 W$ u5 g1 L( z' o& Q
  6. 0.70710678118654767 v. O) ]% J3 O
  7. math.pi/3表示弧度,转换成角度为60度9 ~+ i+ Q! ^2 k3 \" k2 B% Z
  8. >>> math.cos(math.pi/3), R9 j( O$ D9 |0 I
  9. 0.5000000000000001
    : B- N, W6 l% q& C! F
  10. math.pi/6表示弧度,转换成角度为30度* D2 P2 X( q. {. x/ ^
  11. >>> math.cos(math.pi/6)
    % V, W5 @7 [6 [4 x
  12. 0.8660254037844387
复制代码

6 z6 d2 i' |& j" L" F( v# Nmath.tan(x)  返回x(x为弧度)的正切值
. |( V3 C/ K+ D! `8 x, X! G
  1. #返回x(x为弧度)的正切值+ d) _3 y: d8 w& ^
  2. tan(x)9 ]: u7 a1 ^% O8 N# T' }7 C5 A
  3. Return the tangent of x (measured in radians).& X# n. |' s: X5 F& J1 i/ U: Y
  4. >>> math.tan(math.pi/4)+ a8 S# [- p6 p. X) t, \0 N
  5. 0.9999999999999999" n. ~5 _* j+ X
  6. >>> math.tan(math.pi/6)
    . [- L- M4 v% h0 J: P
  7. 0.5773502691896257
    2 R/ u; j8 A# C3 H- t( M
  8. >>> math.tan(math.pi/3)
    $ P1 c2 ?' v8 d6 P' T7 p
  9. 1.7320508075688767
复制代码

" l2 b& G2 r# n9 d# @math.degrees(x)  把x从弧度转换成角度* `+ K4 \, C. V" l$ O
  1. #把x从弧度转换成角度
    + F+ l: P* Q2 c7 l1 Q. s" V
  2. degrees(x)6 _/ g6 B2 K& ~3 V# o" D
  3. Convert angle x from radians to degrees.6 U4 a: h2 v& {' K: I0 f+ q

  4. ' P) S, U! P0 m) ~) e+ p1 `
  5. >>> math.degrees(math.pi/4)+ b, u' M- m/ f: U
  6. 45.0. l. ?) O/ j& I4 V
  7. >>> math.degrees(math.pi)
    0 N/ R6 f* I6 v/ H
  8. 180.0" ^2 [: s; k8 J* ?% z
  9. >>> math.degrees(math.pi/6)+ \1 `  p6 s1 ^# N, D( R; r) G
  10. 29.999999999999996) K& P6 }, [. u& e
  11. >>> math.degrees(math.pi/3)
    5 y  w( j/ k' a. j& F1 c" ~, ^, g# j
  12. 59.99999999999999
复制代码
3 w% G8 o: q4 \7 ^+ \# ~/ S3 H1 b
math.radians(x)  把角度x转换成弧度
2 F0 y2 E( T8 G/ o* Y* N6 n. R
  1. #把角度x转换成弧度
    9 y% g7 \$ e, B+ h1 w
  2. radians(x)
    8 b, c4 j/ c3 ?* M2 R$ ?3 A
  3. Convert angle x from degrees to radians.! l+ s. t$ A, V% g
  4. >>> math.radians(45), g" F- k# [; a9 M7 y( M' }; x
  5. 0.7853981633974483/ z7 r, V3 G( l" Y4 K
  6. >>> math.radians(60)
    - R5 g' _& r# ]7 I0 g1 s
  7. 1.0471975511965976
复制代码

3 Z1 Q' l8 h6 Q6 {math.copysign(x,y)  把y的正负号加到x前面,可以使用0
9 U! G4 O# K- I, |
  1. #把y的正负号加到x前面,可以使用0, `1 R/ S0 V3 w0 R" H5 w7 n" A8 d4 C
  2. copysign(x, y)
    % ?2 T8 M$ d# O# Q- m3 E2 A9 S
  3. Return a float with the magnitude (absolute value) of x but the sign 8 f8 x; o  W9 r( O6 D( J
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) # r! {% g3 {4 ~9 g0 r; [" `, N
  5. returns -1.0.! g) u% r, r. o) G4 J! @: [9 z% g2 C

  6. / Z. H( |0 F8 f. _1 J" T& \; U; M
  7. >>> math.copysign(2,3)
      ^$ l0 k! M* b9 c& K( B+ O
  8. 2.02 i& P4 W! t$ @! U1 c
  9. >>> math.copysign(2,-3)
    0 J6 W9 d5 y" Y- G8 J# O2 u5 u
  10. -2.00 l* C& C  Q0 r9 p
  11. >>> math.copysign(3,8)
    ) X, |+ P! W+ f5 t+ C( B, ]
  12. 3.0# ]& W1 Y" R8 ^& q: U; _; e
  13. >>> math.copysign(3,-8)
    0 S; h9 t, J4 a
  14. -3.0
复制代码
: m5 q/ t6 l: j
math.exp(x)  返回math.e,也就是2.71828的x次方
6 F' Z  A" ^# F
  1. #返回math.e,也就是2.71828的x次方
    6 W  Q" j) s" g( J
  2. exp(x)
    6 t# g; |7 u) s, ^* o3 s/ p
  3. Return e raised to the power of x.$ R0 }+ x& r3 K$ h2 ~

  4. # r$ w2 h2 `1 ?/ {0 D9 J. K4 t$ G/ U* A
  5. >>> math.exp(1)
    # _4 P" L+ A/ f0 Y. Y0 J- ~0 V# b
  6. 2.7182818284590454 t7 n. [: v- p* R4 J' d
  7. >>> math.exp(2)! E% T) Y2 t3 w9 r( k
  8. 7.389056098930653 \8 Q2 U( c& k+ {
  9. >>> math.exp(3)
    * k" t& d" ^& y2 g" I
  10. 20.085536923187668
复制代码
, D( Z; I1 |; ?" s7 L1 }
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
9 q* B% t0 S4 C9 y; T2 o
  1. #返回math.e的x(其值为2.71828)次方的值减14 h5 U8 w9 I* n% q) O
  2. expm1(x)
    / g5 h& z$ t) c- T: Y7 @
  3. Return exp(x)-1.
    * I: e4 |0 t4 R8 J
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    7 Q/ ]1 j' Q, G; j' s3 K
  5. ' Z6 J# _/ K9 |  R* y7 }6 `
  6. >>> math.expm1(1)$ g# r5 H4 q' z4 K9 p
  7. 1.718281828459045
    + O  r1 n. u0 ~: B% z' P
  8. >>> math.expm1(2)
    % ^) ^9 T! p* e  N2 j
  9. 6.38905609893065
    ' z; r9 G: s- O( V; g4 J0 h! r
  10. >>> math.expm1(3)9 C% p( Q: B* `# R% j: y+ {' [, d: d
  11. 19.085536923187668
复制代码

. m( ]' `" K" W& G& gmath.fabs(x)  返回x的绝对值2 @7 A' B4 t1 j
  1. #返回x的绝对值
    " n  T4 L; Z$ i9 @$ j$ j
  2. fabs(x)
    + u# e- t2 v6 P  @0 t0 @& Q
  3. Return the absolute value of the float x.
    & n: r& T! d5 g: V; B& W
  4. $ a0 R: H( y. {3 g* w: b
  5. >>> math.fabs(-0.003)
    # V  k9 O6 \; X0 `+ j) K* t- m. J
  6. 0.003% I& {4 L' H  {- \, R2 m
  7. >>> math.fabs(-110)
    3 m& W& E- C; B6 H, o( P0 O5 a
  8. 110.00 C: c  a1 a+ C. ~
  9. >>> math.fabs(100)7 k$ d1 m2 W9 J0 V% g
  10. 100.0
复制代码

+ m# O: ]: \& L( E+ W. h0 Ymath.factorial(x)  取x的阶乘的值
& \/ @0 t, E0 i; ^$ F
  1. #取x的阶乘的值
    8 U2 I9 n" T2 U0 h! L
  2. factorial(x) -> Integral
    3 M8 V( ~% l: M6 @& k
  3. Find x!. Raise a ValueError if x is negative or non-integral., X' h* X$ I) j/ H" k  S- U
  4. >>> math.factorial(1)
    0 h& X5 s* @8 T7 z/ ~
  5. 19 N! Q4 m2 L  q% a
  6. >>> math.factorial(2)
    % `5 I- A5 p2 ^) C& c- R
  7. 21 R. d3 C' G. ^1 [; {. |
  8. >>> math.factorial(3)" |0 @( D, F1 L5 i+ h/ g
  9. 64 u/ w! k6 A9 F8 K0 {  H
  10. >>> math.factorial(5)
    ; ^/ G- \% Y( z3 r8 r( d' f
  11. 120% A3 ?$ j6 E3 h$ M0 I0 J
  12. >>> math.factorial(10)3 X7 ~" z7 d: j$ }1 Q
  13. 3628800
复制代码
! E# m9 O; ~9 Z! C0 G- I
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数3 |" n8 h- f5 u& N& [
  1. #得到x/y的余数,其值是一个浮点数& _5 y6 E7 f* s# m6 ?
  2. fmod(x, y)" _" P7 f0 U( j- _
  3. Return fmod(x, y), according to platform C.  x % y may differ.. Y- y7 ^* X$ F
  4. >>> math.fmod(20,3)
    ( T( K4 }% p9 [! W+ E! {
  5. 2.0
    ! |2 ?8 Z. C+ }9 K! q. C" O
  6. >>> math.fmod(20,7)
    7 ^/ n6 Q0 ?1 F$ N5 i# r
  7. 6.0
复制代码
1 Y. v$ J. e" y# d, p9 m
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围5 _# M0 `* r; X
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ Q$ Z7 f3 U+ m  B
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    1 n) G$ A% d* L8 y1 r) G5 I' r
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ' t% n* Y) ]* c2 E. |; W2 O
  4. frexp(x)5 N$ d' o: C/ G) k; p1 ^  a: f/ q
  5. Return the mantissa and exponent of x, as pair (m, e).
    8 ~4 Q' [0 f! F3 q
  6. m is a float and e is an int, such that x = m * 2.**e.
    * t/ k; z' a- _7 ^
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.& d! J- Y& u3 `% Q9 F  c5 `: l
  8. >>> math.frexp(10)
    5 M; ?! ]2 a, G( I; q! \2 z
  9. (0.625, 4)
    - f/ r) o: H* J" ^- r, [  m
  10. >>> math.frexp(75)- Q) `+ @: M+ y% ^' M6 u
  11. (0.5859375, 7)
    0 X4 T- S- e% H, z9 I4 F
  12. >>> math.frexp(-40)
    & ^; f3 s2 }6 }: u7 A* Z
  13. (-0.625, 6)
    0 ]2 f, |: H, O2 X
  14. >>> math.frexp(-100)6 s  R+ }: ~! Q7 u' j- t, L' S
  15. (-0.78125, 7)
    0 N3 p. w6 Q& Y* g
  16. >>> math.frexp(100)1 ?' C. j+ r3 \& B) g5 }
  17. (0.78125, 7)
复制代码
0 o. R/ a9 w4 ]$ S# p* e- ?3 i& n% N2 p. Z
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
: E! w/ O$ `. q5 ]
  1. #对迭代器里的每个元素进行求和操作
    : p: u1 g, T- o9 q
  2. fsum(iterable)
    5 F6 |2 z8 C2 _( n& J% a" J
  3. Return an accurate floating point sum of values in the iterable.
    # r  b5 \9 [6 c
  4. Assumes IEEE-754 floating point arithmetic.
    - `% n' r4 g+ F7 J
  5. >>> math.fsum([1,2,3,4])* q9 z. A' P6 A. l0 }1 E/ X; _
  6. 10.0
    0 o' e+ M& |1 s" q9 z9 G, C
  7. >>> math.fsum((1,2,3,4))% J( F" ?5 J3 x) ^) Q  y% ^
  8. 10.0
    " R0 e2 X5 e3 j5 k4 P! e% v. g/ c. P
  9. >>> math.fsum((-1,-2,-3,-4))+ _, S1 b4 c  d3 Q/ W/ k
  10. -10.04 @! i: J9 N$ [- ~" j- ]* v
  11. >>> math.fsum([-1,-2,-3,-4])
    4 t& O4 r$ d$ ]' u  E" W
  12. -10.0
复制代码

2 V- ]! h+ ~; qmath.gcd(x,y)  返回x和y的最大公约数
- f/ v# ~4 M- t  u8 p+ R
  1. #返回x和y的最大公约数3 p( M$ l$ t& p+ G# z
  2. gcd(x, y) -> int+ m. k5 B2 H5 U$ c& V
  3. greatest common divisor of x and y0 i5 w, ^; X: P% r9 M/ O
  4. >>> math.gcd(8,6)
    " h- s( T" H, g5 Z" x1 P% z
  5. 2
    . d& M% M  D3 C" {4 ~
  6. >>> math.gcd(40,20)$ l& w  z- U+ F0 ^# \8 s7 L
  7. 202 W4 m% Y- A; o  W
  8. >>> math.gcd(8,12)
      P+ [; u$ I7 A% y' V8 h
  9. 4
复制代码

; S+ y9 c. i& ?+ C; h) emath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
2 @8 ], Q# I) z( M* b
  1. #得到(x**2+y**2),平方的值+ E/ l) N* e3 U* {3 r' R/ O
  2. hypot(x, y)
    ) f( r# l6 X! C% ^& v! J
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    6 z1 v% Q2 T" D: b! q' I  ~+ {! c
  4. >>> math.hypot(3,4)
    " m" C2 A% |: @& |- K$ f2 e
  5. 5.0
    $ D  G' M- R+ b# X1 ~
  6. >>> math.hypot(6,8)- R9 V3 n* N& r4 Q4 J7 Q2 z7 K
  7. 10.0
复制代码
8 x1 ]8 H  f0 ^$ s$ s3 u+ G' O
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
4 `& C; o  ^3 ?  z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    - \, s, O9 W/ v, I& d) u5 \7 o
  2. isfinite(x) -> bool# E, f6 i( ]9 _6 ]4 d6 s( n& f" j
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ; E5 x8 R6 |6 A- M/ c/ s8 d2 y) b
  4. >>> math.isfinite(100)
    $ I0 C4 o: L. t5 Q( S) x4 S
  5. True  |9 m5 P7 d* q' {& l2 Z* R) N1 {* Y! `
  6. >>> math.isfinite(0)
    ; k  ^& v  G( E
  7. True! m4 }6 G8 o% J; D% d
  8. >>> math.isfinite(0.1)
    . {5 N+ B: g( a  l' ^2 s4 L5 W
  9. True2 g0 R* R2 [# [, q2 ]' S: \
  10. >>> math.isfinite("a")' z7 [. e2 _* E: h1 P( D- D9 q
  11. >>> math.isfinite(0.0001)- w0 N: `, m. D
  12. True
复制代码

; @7 O( `/ r0 `+ v# Imath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False  L9 a7 p! |9 m* f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    - f- \. X; b2 Z
  2. isinf(x) -> bool* N- P# X+ ]3 ~# h: V
  3. Return True if x is a positive or negative infinity, and False otherwise.
    * j0 N  ]2 Q4 i& x. P. u. v
  4. >>> math.isinf(234)
    5 L9 Q, d0 z' v0 G" @
  5. False
    ! I% l& r( _6 `8 B1 p0 h( C
  6. >>> math.isinf(0.1)
    ! ~/ L0 Y$ |1 p- g
  7. False
复制代码

; V9 z' h% [( f, H6 P- cmath.isnan(x)  如果x不是数字True,否则返回False
/ {) @" `* O4 l! b
  1. #如果x不是数字True,否则返回False- i7 d- y$ {( l% v% d
  2. isnan(x) -> bool
    + \# ^/ b1 A" Z- w% b
  3. Return True if x is a NaN (not a number), and False otherwise.1 |9 Q/ {+ J5 u0 }- ~$ n* K8 W
  4. >>> math.isnan(23)- u, l1 J- a! q/ K3 M  i& W1 K
  5. False) H& B* e1 E9 q
  6. >>> math.isnan(0.01)
    : |/ Y, @/ A9 h. W
  7. False
复制代码
: Z: L$ D5 Q3 k
math.ldexp(x,i)  返回x*(2**i)的值1 q: @) ~" q% V. w8 J) d
  1. #返回x*(2**i)的值8 |1 e& e8 l& F5 l0 y( c
  2. ldexp(x, i)
    * s" W* ?; @4 m1 M" O
  3. Return x * (2**i).
    , l# K/ X+ @; q. ?) ?6 h8 \
  4. >>> math.ldexp(5,5)
    - s" B/ X3 T6 K/ N$ P
  5. 160.0
    / }1 b7 @$ M: @; [9 P
  6. >>> math.ldexp(3,5)' N2 h1 ^2 v$ B7 F8 Q: _
  7. 96.0
复制代码
8 V" U- I( b) N. k/ Z7 F
math.log10(x)  返回x的以10为底的对数* e/ K3 N4 e5 H. d2 q
  1. #返回x的以10为底的对数  C1 f1 Y* ~2 U7 n& s- W
  2. log10(x)
    2 G9 T& v# m5 V9 c
  3. Return the base 10 logarithm of x.: S+ G1 b- j3 o% g( ?" R6 L
  4. >>> math.log10(10)
    9 l1 l, L+ w: A7 F. F
  5. 1.0( }0 \" {$ T1 [# ^
  6. >>> math.log10(100)
    ( J, ~+ ?% @: p9 I0 `
  7. 2.0
    % h2 ^/ T$ `( D4 E  p
  8. #即10的1.3次方的结果为20$ Y: }- \7 t7 S0 u. k+ _3 y& ~
  9. >>> math.log10(20)
    0 `, f1 `. s+ I+ |7 k
  10. 1.3010299956639813
复制代码
" A. ?8 o: J2 H+ X/ k6 P% r% o
math.log1p(x)  返回x+1的自然对数(基数为e)的值
' g9 |$ z% l( J! i, M; Q
  1. #返回x+1的自然对数(基数为e)的值
    & z" C0 l  w, G% _6 T
  2. log1p(x)) C& R2 |( m; L- @/ R! E0 \' }
  3. Return the natural logarithm of 1+x (base e).4 {- g/ j7 _) U9 ~  |
  4. The result is computed in a way which is accurate for x near zero.( H2 M5 [. \9 E: c
  5. >>> math.log(10)
    $ m; ~  x6 J' R  x3 }  G+ m' T
  6. 2.302585092994046
    & n3 P( D8 A; s& M( [2 c, M/ U
  7. >>> math.log1p(10)
    : e, a7 T. F+ n
  8. 2.3978952727983707
    2 v" h5 `  X! `: r. S4 t8 ^. [
  9. >>> math.log(11)5 p: T# E2 u3 E; S. O  E
  10. 2.3978952727983707
复制代码

. e# k: ~$ T4 r5 Umath.log2(x)  返回x的基2对数7 M6 z) c- b5 G1 r. r
  1. #返回x的基2对数- G! g% C: H. h1 \5 j* p  v+ D
  2. log2(x)7 u: m9 n; [8 C8 Z' N5 i* v
  3. Return the base 2 logarithm of x.
    ; Z; A, x8 k5 B
  4. >>> math.log2(32)
    3 L* s+ B, H* d/ ^- Z% H' B) }1 N
  5. 5.0
    : X/ k, I8 j2 `" F4 U$ r& L/ T
  6. >>> math.log2(20)
    8 l8 U# K9 F2 `% @8 {
  7. 4.321928094887363
    # ^8 o# Y. `% c& ^+ b% O, h
  8. >>> math.log2(16)
    % K; b7 _2 I& g1 A- h( P6 b9 t
  9. 4.0
复制代码

. K7 c& \( w1 Q3 y  o" ?8 I& xmath.modf(x)  返回由x的小数部分和整数部分组成的元组# q% _. |# P# k6 R" J9 L
  1. #返回由x的小数部分和整数部分组成的元组  X9 k, Q$ T# z
  2. modf(x)- X' B! ^) G$ c4 d6 l& P3 H  }
  3. Return the fractional and integer parts of x.  Both results carry the sign4 j! u) R6 V) f! M% y4 j8 d( B9 p
  4. of x and are floats.0 v8 \! p# b1 H7 e4 \- O( D
  5. >>> math.modf(math.pi)
    + x6 y' @/ a1 u4 Z) ?
  6. (0.14159265358979312, 3.0)
    / {6 X8 ?5 i5 A/ b7 A
  7. >>> math.modf(12.34)
    $ e- P4 ?4 ^% w. q3 ?6 F6 Q
  8. (0.33999999999999986, 12.0)
复制代码

9 v1 b0 a4 X* ~math.sqrt(x)  求x的平方根
1 h4 M, q" `8 @% W
  1. #求x的平方根
    ! J5 F! \$ R1 J) y' w
  2. sqrt(x)$ t* ]* c. r% l) k2 Z  x
  3. Return the square root of x.$ _* P+ z4 Q3 G0 ~8 s* Q2 p  D
  4. >>> math.sqrt(100)
    5 [1 ]# R* w- w' p) }; ?* m
  5. 10.0
    . [5 @7 p% K  \5 z2 b9 C
  6. >>> math.sqrt(16)9 J* f4 a* D5 z$ k
  7. 4.0% K1 ^6 |- z& F
  8. >>> math.sqrt(20)
    2 |, h% F+ A9 {+ K
  9. 4.47213595499958
复制代码

, ?6 {) v. g! H2 N1 l0 vmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    % O3 t' j( S: Z8 j4 m6 M. {
  2. trunc(x:Real) -> Integral1 J8 ~7 A$ q$ c  z; X
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    4 p- j5 i' t# M5 Q9 K. b& Z
  4. >>> math.trunc(6.789)2 u; ~* s' u( z: s5 t# {
  5. 6( C: A. o5 c4 g
  6. >>> math.trunc(math.pi)
    % G& t% w7 C1 |
  7. 3
    9 r) [5 `* V" ~3 O) o, h2 I6 c
  8. >>> math.trunc(2.567); R/ [5 i2 y& _+ u' R* v
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-18 09:38 , Processed in 0.134856 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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