新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1856|回复: 0

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

 关闭 [复制链接]
发表于 2021-7-24 10:21:24 | 显示全部楼层 |阅读模式

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

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

x

3 Q/ m( f4 D+ U4 A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
  W" o( T( @6 k& T, B
% \& {+ m2 |! P
方法1
# C1 z0 Z( E0 X$ M' X. p
  1. >>> import math; l( \# z) H1 k6 n  M9 y' s
  2. >>> math.sqrt(9): b3 i9 [9 c: Y) J; {
  3. 3.0
复制代码
方法2* A4 C, [: V& O8 @7 B/ F
  1. >>> from math import sqrt
    , ?- z- G3 P8 F# R) Q. F
  2. >>> sqrt(9)8 G/ n) I# N$ `. [9 P
  3. 3.0
复制代码

/ M0 d4 s+ |6 A. D! A  @- ~. _9 p  Q
6 ]' s; c5 l8 c+ I- b
math.e  表示一个常量
8 P# ^! g  i. X. [- n6 A
  1. #表示一个常量0 Y: |5 w3 }) c: s$ m5 L
  2. >>> math.e8 _9 a8 ~! S0 D# _. z
  3. 2.718281828459045
复制代码
, w- T/ T7 Z4 @5 u3 _. o5 n
math.pi  
数字常量,圆周率
9 U+ m- Y2 C5 l# g
  1. #数字常量,圆周率% G' q. c" L8 K9 |  G
  2. >>> print(math.pi)( ?/ j' f1 l! V; p* Q! W8 h2 ]8 y! K
  3. 3.141592653589793
复制代码
; G' ~, [* u  l6 [% \. o
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

4 t6 U2 v% P" }" ^( e6 q$ k
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    " G+ R- H2 P# x0 @3 l& U. Z
  2. ceil(x)
    ( i! f5 j9 T/ s$ O, F
  3. Return the ceiling of x as an int.
    4 V# A+ j5 a( ]# D/ I! c& E3 e4 T7 R3 F
  4. This is the smallest integral value >= x.6 i# @0 t1 _9 B) B1 b1 r1 x: {

  5.   p  S4 ~/ F% ^3 s* k& y
  6. >>> math.ceil(4.01)3 b, a* Q* f2 e' ^) k
  7. 5
    ) {4 B: B* w$ s( b" i0 X$ d  H
  8. >>> math.ceil(4.99)+ B2 }, `5 m4 v2 o0 {
  9. 5
    . [8 f3 c' j, [+ W3 t
  10. >>> math.ceil(-3.99)9 h. x0 ^& \% R7 ]# q: H% K8 P% Z
  11. -3
    ' e$ K( S! P  R( |2 I
  12. >>> math.ceil(-3.01)7 g; s. p: T& H! X0 S
  13. -3
复制代码

* d* P3 Z& G( @4 amath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ F; }" B, {9 r2 Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    5 I6 S! Y* Q7 x; {5 s3 S3 D
  2. floor(x)
    ( L$ O" E2 U- o- _
  3. Return the floor of x as an int.
    . E/ @2 H! J& e
  4. This is the largest integral value <= x.
    ; j8 U' }! Z9 u6 d, ^4 R8 n
  5. >>> math.floor(4.1)
    9 s# I# i. i6 n( H( u3 H4 W" K$ n
  6. 4
    1 t$ a9 O/ x# o; w6 V$ f
  7. >>> math.floor(4.999)% r1 U) f0 w0 N" W
  8. 4
    ( K6 Z. k* N. M0 P
  9. >>> math.floor(-4.999)
      y+ _+ `% U! u) a3 _6 V
  10. -56 M6 o: G: R- {4 r, h
  11. >>> math.floor(-4.01)3 v/ T0 B( b: n% x6 w
  12. -5
复制代码
4 X- V9 ~) p6 h7 g8 U# z
math.pow(x,y)  返回x的y次方,即x**y
7 e. v+ R' j- R4 ?- O5 `: U
  1. #返回x的y次方,即x**y/ [, S& W) a* d% R9 V3 n0 [
  2. pow(x, y)
    $ y2 y# n9 x- n: _1 Z
  3. Return x**y (x to the power of y).
    " P% E8 X; m% c. a- e; C5 ]
  4. >>> math.pow(3,4)
    5 l0 d& g+ o6 J: ^' d- q- |
  5. 81.08 n9 i  Y, A! u+ t& }. M- g
  6. >>> % a' b6 Z0 o  k  D. H, j0 }
  7. >>> math.pow(2,7)
    # k% N& H! e4 ~4 y
  8. 128.0
复制代码

/ ~8 g6 v& k* D+ c, Z" jmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 C& P% R4 Z* [, k
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). n+ ]: o( ~9 m- b
  2. log(x[, base])
    4 D" s+ L% G2 C0 {! T
  3. Return the logarithm of x to the given base.: j) e8 a& ], F- `
  4. If the base not specified, returns the natural logarithm (base e) of x.
    & L# ?( n5 M& U. G
  5. >>> math.log(10)
    5 N! z- s( I% t: A7 o
  6. 2.302585092994046
    ! A  [- h. X. a' E) x5 K
  7. >>> math.log(11)6 G9 h& ]& h: u" e
  8. 2.3978952727983707
    5 `) K! o$ L5 D, b+ X+ `
  9. >>> math.log(20)5 c4 `1 O# @: L1 _
  10. 2.995732273553991
复制代码

1 I( N0 \6 k/ Wmath.sin(x)  求x(x为弧度)的正弦值; n+ d3 G/ o# t2 [
  1. #求x(x为弧度)的正弦值
    : _$ k: t( W6 w7 n
  2. sin(x); F* b7 I+ `8 e2 Y. t
  3. Return the sine of x (measured in radians).
    % o: t+ s' u0 N; u6 b; m1 f- L
  4. >>> math.sin(math.pi/4)
    9 u7 W% a- L) H7 P/ h
  5. 0.7071067811865475' S* U& I" w, a; f6 H; Q
  6. >>> math.sin(math.pi/2)
    6 i/ N/ _0 T  d6 j3 M
  7. 1.06 ^( b9 K8 k6 a4 _) z3 N! a
  8. >>> math.sin(math.pi/3). V2 m$ A3 s$ Y) X( u! U
  9. 0.8660254037844386
复制代码

4 ]! j6 @4 N2 P, Y4 mmath.cos(x)  求x的余弦,x必须是弧度8 o# u! K; O6 Q( I5 j0 O
  1. #求x的余弦,x必须是弧度
      k7 @; `* K2 ^/ H, g
  2. cos(x)0 I. f# B% G5 ?" i; K/ Y( X6 ^
  3. Return the cosine of x (measured in radians).
    - M6 f0 {& y8 I3 ^  t3 C7 m
  4. #math.pi/4表示弧度,转换成角度为45度* v: w8 e, s  m: X
  5. >>> math.cos(math.pi/4)
    7 L+ _' G1 f8 Y8 b2 z
  6. 0.7071067811865476
    9 `. w  a! [/ \4 Y+ H
  7. math.pi/3表示弧度,转换成角度为60度- q7 b; X& f; g: p" f# f
  8. >>> math.cos(math.pi/3)
    1 R- u9 M$ U, u- |$ _' v( _0 }
  9. 0.5000000000000001" ]( k7 Y" m1 m  Z2 r+ X
  10. math.pi/6表示弧度,转换成角度为30度' s% y4 i0 L0 S) U
  11. >>> math.cos(math.pi/6)- P2 ~, N1 D7 {  n7 C" \
  12. 0.8660254037844387
复制代码

7 O+ Q5 `+ g7 B+ vmath.tan(x)  返回x(x为弧度)的正切值$ Y1 q% X1 [+ f8 K. _4 f) ], \. @
  1. #返回x(x为弧度)的正切值6 y& }1 f* M# C2 N8 m- T
  2. tan(x)% r# Q7 k' ^7 _: |6 ?
  3. Return the tangent of x (measured in radians).
    . ?  x" H) m- v
  4. >>> math.tan(math.pi/4)" V5 [6 N: E$ j% j" S5 \
  5. 0.9999999999999999
    $ w+ b/ Z8 ~  n2 q: @
  6. >>> math.tan(math.pi/6)3 R9 U4 G- j4 c* p2 \( D
  7. 0.5773502691896257
    ; ]5 H1 F1 q- J; ?
  8. >>> math.tan(math.pi/3); e8 ?6 t# L& y8 f8 S6 T. u* r- |
  9. 1.7320508075688767
复制代码

' H  A4 `3 q- N& {% ?  z, Lmath.degrees(x)  把x从弧度转换成角度" k+ W# C" v+ X( t
  1. #把x从弧度转换成角度
    6 R" Q7 y, @  N* i
  2. degrees(x)$ [( ]& J( S- T3 R1 a8 W. H
  3. Convert angle x from radians to degrees.
    ( r5 x& z- [! j- h" D7 {/ Q
  4. : g; L' S8 C5 B% _) s2 ?
  5. >>> math.degrees(math.pi/4)) M0 V5 Q+ k1 C$ Y3 x
  6. 45.0
    " k1 O# K7 f8 R: l) K. \
  7. >>> math.degrees(math.pi)
    6 @/ E: Y0 z# e4 D3 O# P* S9 t# u
  8. 180.0
    . L6 q8 I$ Z+ w# A5 l
  9. >>> math.degrees(math.pi/6)2 C. v8 A, p& [  k: m
  10. 29.999999999999996" i" O( H/ y( Y3 u- d- e
  11. >>> math.degrees(math.pi/3)5 c1 P8 t7 g# j  S, R# v
  12. 59.99999999999999
复制代码

9 @9 s( o9 Z6 |3 e" R; J1 j' t0 nmath.radians(x)  把角度x转换成弧度
5 c$ z7 X- p$ @) |
  1. #把角度x转换成弧度
    2 i) J( q7 p1 v) Z. W
  2. radians(x)
      Y8 e' g. S) q
  3. Convert angle x from degrees to radians.
    - ?" `0 }/ q  K/ |) v, P8 S$ F0 I
  4. >>> math.radians(45)
    ' }5 X' P4 P  G5 K
  5. 0.7853981633974483
    * M& Z' k/ @& v5 p  h" ~8 k% K' n0 K4 c
  6. >>> math.radians(60)
    ( J, ~% K4 n% s
  7. 1.0471975511965976
复制代码

5 u2 ?  U/ h1 m! _  g5 i% cmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
( |3 w9 c- X" ?% s
  1. #把y的正负号加到x前面,可以使用0
    ! a/ ]9 q5 ?% L3 K
  2. copysign(x, y)8 x3 l/ ^! V1 b: S2 @( Q
  3. Return a float with the magnitude (absolute value) of x but the sign $ _# q' K4 f  d
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 5 |+ |# ?5 X+ l& j) `
  5. returns -1.0.8 F2 U  F) M1 k: T/ D" W/ }3 x

  6. 7 l) a4 b9 X+ q/ N8 _2 a0 t
  7. >>> math.copysign(2,3)
    ) Z: m$ k- l8 \9 Z0 H6 R
  8. 2.0
    ) u# X* D/ X0 M* T
  9. >>> math.copysign(2,-3)
    4 X8 E  I, I  q2 q% g
  10. -2.0, g: j! y& t+ K$ V% g. }  f  h# b
  11. >>> math.copysign(3,8), w1 V( {2 i- \  G( g
  12. 3.0
    / u( P7 R; d. {; \; F$ P( Q
  13. >>> math.copysign(3,-8)
    " F) n/ C/ w" _: x7 D
  14. -3.0
复制代码
" G* w7 A9 \( _4 q0 O4 z6 J
math.exp(x)  返回math.e,也就是2.71828的x次方0 c( B' j4 G0 g: Y, h" D. E4 i' F
  1. #返回math.e,也就是2.71828的x次方: I! }3 h5 [/ i% ?  f* }/ k" V
  2. exp(x)- U8 A! S+ O+ F8 y
  3. Return e raised to the power of x.
    5 X+ F' E- Q5 b  D" `
  4. 5 Y+ G- w& D% G! q6 M
  5. >>> math.exp(1)
    & H0 c8 }& V+ `/ ~$ }8 _: \4 F
  6. 2.7182818284590454 B; Z( ~$ J. r) r; ?
  7. >>> math.exp(2)/ e+ x4 U7 L* `1 h
  8. 7.38905609893065. F9 S5 r: U1 v2 S  m' p8 _! _
  9. >>> math.exp(3)6 `" w) H& Z) U5 z9 L$ T3 P+ o  m
  10. 20.085536923187668
复制代码
: ]5 Y; Y# H8 F! C% R
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
4 R9 q$ p: j& [' d: H: S& I
  1. #返回math.e的x(其值为2.71828)次方的值减1* o% ?3 _( G9 [4 E, B
  2. expm1(x)
    $ i5 n1 j: C! E( ^7 w4 f, ^. Y: `9 i
  3. Return exp(x)-1.
    , v2 v) b6 r' ?' ^$ _
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ _0 W5 B/ q" ]. o

  5. 5 @- @' j& S; |+ L% z) n8 t
  6. >>> math.expm1(1)
    * {/ u1 |- v( j9 S; @+ U% _8 X, L
  7. 1.718281828459045
    1 e3 @1 V9 n* w& T
  8. >>> math.expm1(2)# P: p* x  j1 L& |+ |% g. ]+ e8 u/ |
  9. 6.38905609893065, S1 B9 A6 P6 L+ s
  10. >>> math.expm1(3)
      }3 c7 _$ d* L3 i& D
  11. 19.085536923187668
复制代码
/ ?, o6 @" O$ G/ c
math.fabs(x)  返回x的绝对值
- ~9 _: k5 ?/ J5 x. F) ~8 h7 x
  1. #返回x的绝对值& N! b; ~  Q; f5 I; _7 r$ k/ R: H! {
  2. fabs(x)
    # D, o" n/ c) B' N- x' ]
  3. Return the absolute value of the float x.# q  ^" v+ i/ ?* w

  4. $ H; n4 ]& t" f0 g
  5. >>> math.fabs(-0.003). d2 a4 W. w9 \8 {6 d+ Z
  6. 0.003
    9 p7 I3 w5 a$ [
  7. >>> math.fabs(-110)
    * Y2 r7 G: X7 t
  8. 110.0
    # U: B: I# |& j5 F
  9. >>> math.fabs(100)
    % [& J5 Z4 p, k. ?$ Y
  10. 100.0
复制代码

- r3 v4 y) Z# R. H' `math.factorial(x)  取x的阶乘的值, K) c0 D' c  f6 q0 r. K6 D
  1. #取x的阶乘的值) X  w3 s  g2 f$ p! x' ^3 F
  2. factorial(x) -> Integral
    4 P$ _3 `4 X3 c; M; w9 a
  3. Find x!. Raise a ValueError if x is negative or non-integral.8 z) i7 N9 q/ y! b5 e4 `  |$ F
  4. >>> math.factorial(1)7 _- t9 w' X' M3 f- Q2 U# ]
  5. 13 T( F9 r* r6 h/ ^8 `3 s
  6. >>> math.factorial(2)1 h9 j% v9 K. Y4 `4 J
  7. 25 @+ o. S% u7 ~) `6 j% t; J
  8. >>> math.factorial(3)% z6 i2 o& \  s; [7 ~/ n8 j6 `
  9. 6
    + ]3 N& ?. ^2 y  z' g  O
  10. >>> math.factorial(5)
    " N: X: B. K5 O0 L5 ]
  11. 120
    7 w1 |9 d: @9 e' I  K/ h
  12. >>> math.factorial(10)
    & y- ?4 W- q. x; B
  13. 3628800
复制代码
2 p/ p# E1 _* Z4 [8 r
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
* s% Z" v6 h2 z: i; D( u0 [
  1. #得到x/y的余数,其值是一个浮点数( D: m& ~3 A4 O9 y( C$ z# M
  2. fmod(x, y)4 Y$ d* d. N7 I$ |1 c' d$ K. O" \
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    / H+ l5 N& M7 n! [& S7 B3 _/ T" v
  4. >>> math.fmod(20,3)2 M2 a* N# ~: r0 g
  5. 2.0
    4 G4 C0 _% S: r/ @1 @4 x
  6. >>> math.fmod(20,7)+ z8 L- C% Q( m! A2 o7 O
  7. 6.0
复制代码
2 l- N  o" }3 a# I2 \
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
+ i- x; ]; s0 g2 A& c$ A
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    1 p# O8 f( _1 i* o& c: U
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ J& _5 N& l* d& `* i6 `. \3 S2 S/ |: m; H
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ) h( f  @; b8 a+ a6 W" A6 m4 j
  4. frexp(x)" o4 p8 p+ ?, x+ |, [5 |
  5. Return the mantissa and exponent of x, as pair (m, e).
    " g. i9 m. g* c( f" _0 K5 h" q1 e
  6. m is a float and e is an int, such that x = m * 2.**e.
    ; f. E2 I) a; S" c& Y; |
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 X' W& [' }5 k* b
  8. >>> math.frexp(10)! p3 d1 S$ C' E6 Q: s( S
  9. (0.625, 4)- t4 p- C" f& n/ P: V
  10. >>> math.frexp(75)0 P: g5 t+ |+ [; P
  11. (0.5859375, 7); S' H$ g' H4 a- v* \% m$ d- R
  12. >>> math.frexp(-40)1 `# m5 ^/ m% I6 S9 b* j
  13. (-0.625, 6)
    7 l  i( S! F; Y+ M
  14. >>> math.frexp(-100)4 y. ^2 u) e% x* H  H
  15. (-0.78125, 7)/ m5 r! L1 W- P$ G) S
  16. >>> math.frexp(100), D1 Y2 M7 P6 V: M+ z6 W  h
  17. (0.78125, 7)
复制代码

1 ^! {* Z' M% ?  z$ r% Gmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 E3 m6 ]9 r$ Z7 J& ^( I0 o
  1. #对迭代器里的每个元素进行求和操作: b$ w* Y( S9 Q+ ?
  2. fsum(iterable)! k; }! k% D1 g: p1 n
  3. Return an accurate floating point sum of values in the iterable.
    # z7 K+ v. y) o1 ~
  4. Assumes IEEE-754 floating point arithmetic.
    4 E6 @/ t2 l7 N8 ~: X4 Q8 ^
  5. >>> math.fsum([1,2,3,4])2 @1 j" a# G5 k' h# K
  6. 10.0
    % ?5 f8 k$ [' `$ h  g- Z# v; `
  7. >>> math.fsum((1,2,3,4))- ~: l% O& T' P: `* f
  8. 10.0
    3 z5 a7 E3 R6 ^6 D
  9. >>> math.fsum((-1,-2,-3,-4))
    ) t3 l2 y. X; ]  W2 L0 B8 }/ z
  10. -10.0
    ) ~" T! b" C2 H) m9 ]; B4 A
  11. >>> math.fsum([-1,-2,-3,-4])
    $ J+ d0 g+ M" e9 L1 ?1 r; g4 s
  12. -10.0
复制代码

1 |& n% F3 h: Q' d! }! Y; G3 {& ?math.gcd(x,y)  返回x和y的最大公约数, r% j( T; j7 b$ z, A
  1. #返回x和y的最大公约数
    6 Y2 U( l' `. k- K
  2. gcd(x, y) -> int1 S* C2 D1 p6 M- p/ T! _
  3. greatest common divisor of x and y3 t' y( Y0 M, f7 _7 G6 Y
  4. >>> math.gcd(8,6)
    $ A( t3 M9 Q& z9 F; f5 H. ?
  5. 2
    * r* n3 f% J& N0 K
  6. >>> math.gcd(40,20)
    % s7 O, o* W' b
  7. 20
    9 B$ K' X% M6 C% K- C! [  r$ ~* I
  8. >>> math.gcd(8,12)9 t: Q! V! ^% F3 T6 p( D6 K
  9. 4
复制代码
" b1 t9 K8 ~5 M' P" V+ \: L# q0 u- a
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False0 l, q  J& M! v
  1. #得到(x**2+y**2),平方的值6 ^4 t, F$ a; }
  2. hypot(x, y)/ F8 o5 H- ]( m; L8 ?
  3. Return the Euclidean distance, sqrt(x*x + y*y).2 ]# K/ e" {3 f% u4 @0 j- n+ }5 R3 h
  4. >>> math.hypot(3,4)
    . E5 G( s7 Q  I( Q  D* n# l4 `( r$ ]
  5. 5.0
    2 t- ]+ e2 s5 U& x- P
  6. >>> math.hypot(6,8)% ?/ ?9 `& g5 n1 V! t! o4 q
  7. 10.0
复制代码

' b8 \: [1 Q* m5 I" i9 amath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
7 f; W9 \- l9 t1 Y
  1. #如果x是不是无穷大的数字,则返回True,否则返回False: \, N6 l7 c6 q# k3 y6 I
  2. isfinite(x) -> bool
    ' a" U1 f$ k( c, l6 o& v& H
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ; G& E8 a, e* G3 ^0 M( n
  4. >>> math.isfinite(100)
    6 n  @( M% k7 ^+ p
  5. True+ t1 Y. i2 g. ^
  6. >>> math.isfinite(0)
    5 M5 A* [$ Q$ F3 [$ ^4 I
  7. True) M: l3 \' A# E, s
  8. >>> math.isfinite(0.1)& q. D0 K0 n" X% n* D1 O0 h
  9. True8 I* J! o# i0 o/ D( d7 n
  10. >>> math.isfinite("a")$ c( Q+ r4 i3 c4 ?# a& |! L' {
  11. >>> math.isfinite(0.0001)
    9 F+ Z3 q" V. t, `: A/ S# q& k
  12. True
复制代码
% K- k" S0 q; t4 O4 W
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 V1 ]% A) x: ?) t8 }. }( {
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 e4 n- @1 V8 B+ C
  2. isinf(x) -> bool1 V2 B+ d) c) |4 _
  3. Return True if x is a positive or negative infinity, and False otherwise.* n1 Z1 @, V% @# L8 ~9 C
  4. >>> math.isinf(234)
    : v. s4 d- e& O. d; g" f6 e  b
  5. False
    6 L! Z0 C$ E' u- t
  6. >>> math.isinf(0.1)
    5 P9 k+ f" o' `  L
  7. False
复制代码

% F3 v- P& L; i& J5 T/ I; o8 J$ p% s0 b! vmath.isnan(x)  如果x不是数字True,否则返回False
; X2 c% z, \6 N( ^
  1. #如果x不是数字True,否则返回False
    + U7 X# e& R. |" f2 t' k- K. a
  2. isnan(x) -> bool
    . E- J6 u. T4 Q; ^2 E1 S, y
  3. Return True if x is a NaN (not a number), and False otherwise.- j0 ]) \! U% P+ I" e
  4. >>> math.isnan(23)
    ! E6 f  Y6 H$ T
  5. False( N9 G" W# p! ~& T
  6. >>> math.isnan(0.01)
    ; W: O. K- X" q! G
  7. False
复制代码

$ b/ u3 F) B/ r/ ^3 {2 h/ f/ Smath.ldexp(x,i)  返回x*(2**i)的值
# F) _+ p/ p$ w9 q5 R
  1. #返回x*(2**i)的值
    - [; R9 T6 Y/ z8 N
  2. ldexp(x, i): m6 Y8 F/ h  X+ X& S6 m+ j) Y  z0 a
  3. Return x * (2**i).
    / {7 H* Y- j( d4 G+ c6 q& v. ^
  4. >>> math.ldexp(5,5)8 }4 s2 A; N0 ?( {# o
  5. 160.0. I6 Q; t& D% x
  6. >>> math.ldexp(3,5)3 p  z7 z+ {( M4 n. ~
  7. 96.0
复制代码

, A% w+ `5 V# Q7 z, G' Q4 \% Kmath.log10(x)  返回x的以10为底的对数* D; R; @" G) B5 q& w
  1. #返回x的以10为底的对数
    3 q0 Y0 G. V: z8 c5 }* f( U
  2. log10(x)& }' ^! x- I5 F& W9 d
  3. Return the base 10 logarithm of x.
    1 n; c1 f- B8 q, g  @* a9 x
  4. >>> math.log10(10)
    : X' _1 Z# n* z6 z
  5. 1.03 Z% n  O- R6 g& S" l! `
  6. >>> math.log10(100)8 Y" _% |5 d$ n& G
  7. 2.0* r( |) g% Y; t' [6 _/ ^
  8. #即10的1.3次方的结果为20
    0 |' l  H$ l5 z1 ^' A% g. {
  9. >>> math.log10(20)/ h! k, `! V0 e
  10. 1.3010299956639813
复制代码
  ^7 x, Z( _8 W% R1 I& J5 Y
math.log1p(x)  返回x+1的自然对数(基数为e)的值
8 P6 Q: u6 Q% h2 F+ d; Z: _$ `
  1. #返回x+1的自然对数(基数为e)的值
    0 N4 u* c/ T) [  [, d# w
  2. log1p(x)
    2 W4 `! Y; P: Y, z! {
  3. Return the natural logarithm of 1+x (base e).
    2 V, x- ?% M* ?* b& T
  4. The result is computed in a way which is accurate for x near zero.: k* J/ X9 Y# d
  5. >>> math.log(10)% @$ Y* t  ?: w/ W9 O! f! Z
  6. 2.302585092994046
    * h- F& L7 u# Z# d& g. R/ X/ l
  7. >>> math.log1p(10)
    * l8 B6 g/ ]' m0 B9 Z
  8. 2.3978952727983707* n9 ~! V, s; p$ P& O
  9. >>> math.log(11)
    ( E/ N  a# H2 r: D$ ~
  10. 2.3978952727983707
复制代码
7 D+ @) d8 ^% V2 V' i
math.log2(x)  返回x的基2对数: M- L& G& \+ e( ]9 X$ c0 t
  1. #返回x的基2对数
    ; k# l0 R1 B" s0 c& n
  2. log2(x)
    8 q* s/ I: l* l
  3. Return the base 2 logarithm of x.
    2 X3 @) `+ g# l. o9 n
  4. >>> math.log2(32)
    & S4 p. z8 w( b0 B% ?' m) H
  5. 5.0
    9 y, X  X6 H3 C$ l6 P- ?
  6. >>> math.log2(20)8 o+ \, n& M$ R0 q
  7. 4.321928094887363
    % h/ K( R% v' h6 d1 s! f9 e/ L
  8. >>> math.log2(16)
    , X7 |! g+ [& c1 v1 b4 @
  9. 4.0
复制代码
" h' o9 y0 ?0 p9 t' ?, p
math.modf(x)  返回由x的小数部分和整数部分组成的元组
) ]/ C3 H% ~* Y8 X: D# u
  1. #返回由x的小数部分和整数部分组成的元组
    : J- h( G7 E1 h( \; [+ C! \
  2. modf(x)
    0 [: Y$ ^+ Z! g, r+ h
  3. Return the fractional and integer parts of x.  Both results carry the sign0 b; W4 N; d- W+ V# F2 {
  4. of x and are floats.( u# _5 h7 T$ E
  5. >>> math.modf(math.pi)
      T/ y. Z0 E+ b1 X3 j/ S7 P. F
  6. (0.14159265358979312, 3.0)
    % M( |8 l1 A9 w! N$ G% U
  7. >>> math.modf(12.34)
    5 D5 s* Z! ]; {. H. q9 a* O
  8. (0.33999999999999986, 12.0)
复制代码

# K  u  {$ B4 w/ s4 t& Pmath.sqrt(x)  求x的平方根
  D  H. m7 u" |3 N
  1. #求x的平方根7 B- v5 T8 X6 r$ \$ }' B
  2. sqrt(x)5 k* H1 r( Y) S) u3 o4 Q; k
  3. Return the square root of x.% @( n" I  ]/ x3 m
  4. >>> math.sqrt(100)* w! k( C# R: L. e6 M9 T* r# J
  5. 10.0
    5 k8 y3 N9 d( D( }
  6. >>> math.sqrt(16)
    8 _2 D7 T! K" ?- M& X
  7. 4.0
    ( G. k# C5 c% s
  8. >>> math.sqrt(20)3 c# N- q/ `' o* N  b
  9. 4.47213595499958
复制代码
) `: l, w, H9 V& E6 m5 t8 x
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ( `, S' i. s' U; P
  2. trunc(x:Real) -> Integral
    ; O* A: Q0 E  O& @2 q
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.; d( ?) I! o: M8 T
  4. >>> math.trunc(6.789)
    ; Z' ^- \, V2 _* l9 P# W' H
  5. 6' c5 a# P' v; [0 `: n
  6. >>> math.trunc(math.pi), ]  R+ W  D8 O7 y# {  Y
  7. 3
    ( p8 y  a! d) j6 `
  8. >>> math.trunc(2.567)
    ) z# M' G: J/ l5 J4 w. h
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

GMT+8, 2025-11-16 11:17 , Processed in 0.075548 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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