新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

- C0 q+ C) G: q* a: h6 X  V' N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
8 U1 t* ~) x2 Y6 ?- A) s

3 k0 A" U5 O# K- p7 _4 _方法1% v, @+ }( G. c. X
  1. >>> import math
    2 ~$ Q! Q  o+ y# n8 Z0 D# S
  2. >>> math.sqrt(9)
    $ a! U3 h5 F$ V2 ^" h" `# x6 k( D
  3. 3.0
复制代码
方法2* h; a; _; L, d; Q
  1. >>> from math import sqrt
    5 v" k* H- \' G7 Y% V- B/ ?- ?3 ?
  2. >>> sqrt(9)
    1 o8 `! D$ z6 r$ d" ^8 \
  3. 3.0
复制代码
3 P$ @! k# O- q5 s


# c7 m% T( K; p2 ^5 A
math.e  表示一个常量
, N. X% t! S5 y
  1. #表示一个常量; I( [0 R2 j. g
  2. >>> math.e
    ( Q# P; Z0 _& s. H& F8 _9 u+ M
  3. 2.718281828459045
复制代码

0 t, }  }8 Y4 t2 \" i8 emath.pi  
数字常量,圆周率
( c& B' G$ C6 x" u+ ~/ V
  1. #数字常量,圆周率6 r9 d* {" K) V4 G2 f/ T
  2. >>> print(math.pi)
    , ?0 m! [2 }( }
  3. 3.141592653589793
复制代码

! u( m9 g0 D! O9 O. Q9 Mmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
( X* y) Q0 @1 D1 T8 B, `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    2 s- k- E3 A- ?
  2. ceil(x)
    & ?1 l" @5 X5 c# U( \, Q
  3. Return the ceiling of x as an int.: T$ B: l- Q1 V- \6 ^6 `
  4. This is the smallest integral value >= x.- r0 F1 e4 G& \8 |' t# p( a/ T8 s

  5. . |0 |/ D" B0 |7 s0 A
  6. >>> math.ceil(4.01)
    ( S& Y0 x) W5 Z- T( d
  7. 52 I+ E+ k2 S7 D7 t" e' J
  8. >>> math.ceil(4.99), N/ p9 M& l/ N3 p# N' a- Q5 ~3 @
  9. 5
    7 S- A: T5 C8 H6 J5 B- s- M
  10. >>> math.ceil(-3.99)
    ! |9 t, \: k, l
  11. -3
    ; |: ~7 i% H; _- q0 c* x
  12. >>> math.ceil(-3.01)
    2 B/ _8 b$ |( f4 ^( I9 ^
  13. -3
复制代码
0 m0 A/ {  [& W$ F% Y9 a
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 f- p- G4 Y( Z5 C* ^
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    + C3 ?3 ?1 a4 M
  2. floor(x)
    ; x7 G: Q/ S( {3 o4 H
  3. Return the floor of x as an int./ r! x; d4 s7 x% ?
  4. This is the largest integral value <= x.
    # ^/ E; U5 q- g  A7 m
  5. >>> math.floor(4.1)
    ' D: V6 U- H5 L9 T& i5 e
  6. 4
    3 u6 g0 c" }1 O" N! J+ ]; i# v
  7. >>> math.floor(4.999). h4 m% `6 k' W3 k3 e4 h8 S
  8. 4
    " H( k9 O. j2 M# j
  9. >>> math.floor(-4.999)/ W" b6 U. }' V/ e
  10. -5
    $ O. p7 b8 W3 h: P# X% F
  11. >>> math.floor(-4.01)
    " k+ V: r: \  P6 \" c. i' z9 Y
  12. -5
复制代码

5 j0 R9 X5 Q1 v. A; V8 dmath.pow(x,y)  返回x的y次方,即x**y) _( M; H8 `% A5 G. H9 p8 w
  1. #返回x的y次方,即x**y
    * h6 e6 a) o4 H  f" I, J
  2. pow(x, y)
    - N1 L+ `9 H5 p- d; [
  3. Return x**y (x to the power of y).  U4 ~/ `# M/ Z" }4 f0 u4 P
  4. >>> math.pow(3,4)
    ( C- [' l8 V+ s" i% E" _
  5. 81.0
    % h4 g! W2 i9 l
  6. >>> ( `: f$ n' w3 m8 W
  7. >>> math.pow(2,7)
    : G, ~0 d( Z) r
  8. 128.0
复制代码

; e3 @& @, J* o+ v' }; ]* t- E! }math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 \% q  |( P% I+ c) [
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' r+ m9 ^& p6 b
  2. log(x[, base])
    / @4 a* f3 J  h! a' M! f
  3. Return the logarithm of x to the given base.
    9 ~, y6 M$ G/ u* b
  4. If the base not specified, returns the natural logarithm (base e) of x.  `/ c* h6 |1 u
  5. >>> math.log(10), N0 `. }" p0 k9 n' |2 Q! q% N
  6. 2.302585092994046% p5 Y& q6 \' o
  7. >>> math.log(11)& g/ Y& a% d! i- i5 A
  8. 2.3978952727983707
    3 Q/ ]6 G" t6 I  {2 v+ G* x
  9. >>> math.log(20)
    % T/ B/ _4 g" e) {, t1 f) {0 r
  10. 2.995732273553991
复制代码

7 Z' a# O! ]! y' Q1 P* A0 s- qmath.sin(x)  求x(x为弧度)的正弦值8 v$ b0 c1 {# e6 G  d; i* R
  1. #求x(x为弧度)的正弦值$ i4 |; l# k4 D! r' A( a' u; z4 @" @
  2. sin(x)
    5 J4 J$ J2 C3 A  o
  3. Return the sine of x (measured in radians).( B3 h8 F# D. }7 o2 R
  4. >>> math.sin(math.pi/4)
    & n/ ^4 f7 S1 f# G6 [. q6 z2 F
  5. 0.7071067811865475: J* Q6 E4 o" j& `
  6. >>> math.sin(math.pi/2)
    5 ?3 L" ~7 B; C; E
  7. 1.0
    0 I, e& \* T6 x8 p
  8. >>> math.sin(math.pi/3)3 |( m6 W  z3 K& B) i6 U- _$ M
  9. 0.8660254037844386
复制代码

  x( P5 Q: N( n- j# [% \& l* s0 I) S; smath.cos(x)  求x的余弦,x必须是弧度8 u5 Y& O( h8 V' `' q5 x
  1. #求x的余弦,x必须是弧度* G% ^; p% `- I, P1 ~
  2. cos(x)
    : \4 W" p% v$ A! U9 R
  3. Return the cosine of x (measured in radians).' `  n% d. T  K. Q0 S4 Z
  4. #math.pi/4表示弧度,转换成角度为45度
    - ], n4 \. }3 V" r
  5. >>> math.cos(math.pi/4)
    9 u4 n: D$ x; d
  6. 0.7071067811865476
    0 d- a! Z+ C# p( x2 c
  7. math.pi/3表示弧度,转换成角度为60度
    $ O5 T8 z0 \; I+ Q8 b
  8. >>> math.cos(math.pi/3)
    " T( Y  T/ s$ K# h1 l+ x& t9 t
  9. 0.5000000000000001
    2 g6 v! x$ X& ]
  10. math.pi/6表示弧度,转换成角度为30度
    $ H, S0 D$ F" t! j5 n8 X0 H
  11. >>> math.cos(math.pi/6), ]! g  s5 k& b9 F, E: }1 p
  12. 0.8660254037844387
复制代码
  I+ Z: L" k4 P1 M4 S3 L/ `
math.tan(x)  返回x(x为弧度)的正切值; I/ t# s4 t: e7 ~" }& b
  1. #返回x(x为弧度)的正切值$ ~% V. u( Z& L/ B& y% I
  2. tan(x)( q$ y- a, f  Y" t$ E  S- c8 h5 c$ z
  3. Return the tangent of x (measured in radians).
    : P, t, F5 v5 k  T) L9 i" S+ C0 o  a) k
  4. >>> math.tan(math.pi/4)
    . K! p5 Z% G! W1 b! r) [  f
  5. 0.9999999999999999; N9 p5 F( L- I/ h. I
  6. >>> math.tan(math.pi/6)
    & p+ W; b1 n5 H6 d" F+ l
  7. 0.5773502691896257' o2 \9 V$ m: t( [6 {9 _7 O
  8. >>> math.tan(math.pi/3)
    5 Y/ X" ~# N# r  S* a/ U& R0 ]% P3 ~
  9. 1.7320508075688767
复制代码
1 J& \2 X% w7 a. v# i
math.degrees(x)  把x从弧度转换成角度+ j% K! I2 G+ k1 B) a
  1. #把x从弧度转换成角度* d/ S$ _) s/ k5 _/ S, A
  2. degrees(x)9 V4 E( w# P8 E/ p
  3. Convert angle x from radians to degrees.
    # q% V  q3 N+ @* v

  4. 6 O& F: ~/ i* x: n+ q5 r3 [! k. f
  5. >>> math.degrees(math.pi/4)
    & g+ C+ ~. u9 _2 o0 T0 r
  6. 45.0
    3 n2 {* N9 P4 b+ w
  7. >>> math.degrees(math.pi)
    ' [0 T6 A6 z& M- K' R
  8. 180.04 ]4 f& z1 z) Y$ C3 |
  9. >>> math.degrees(math.pi/6)
    , Y9 y' P. C: o) H4 _) g9 r
  10. 29.9999999999999962 O7 P  U/ i% w" m. i( J3 Z( I( D
  11. >>> math.degrees(math.pi/3)
    ( T4 Q% t, T# y) G: V; d
  12. 59.99999999999999
复制代码

' r8 U8 A' ]$ O% q) vmath.radians(x)  把角度x转换成弧度: S' I. G" f; C* d  Q. T
  1. #把角度x转换成弧度
    4 K3 Z% U" C2 t/ w8 _8 J; x+ ?
  2. radians(x); y: J% I6 s3 c. E: }
  3. Convert angle x from degrees to radians.
    ( P8 i8 ]! I7 B2 w* a+ M
  4. >>> math.radians(45)
    5 V5 w# N* a4 c8 ?+ @3 T5 _0 h
  5. 0.7853981633974483: V: o& M+ y' W: K" u1 Q
  6. >>> math.radians(60)  h5 ?; M. s' ]: a
  7. 1.0471975511965976
复制代码

' r$ u3 b, Y' [/ Vmath.copysign(x,y)  把y的正负号加到x前面,可以使用0* Y  C* K0 F4 J: p
  1. #把y的正负号加到x前面,可以使用0+ [3 d& H3 z5 G: l% P& d! Y
  2. copysign(x, y), K  ~, `% L1 a; \! Z+ r. R
  3. Return a float with the magnitude (absolute value) of x but the sign , v$ h# J" H# q
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 l: K+ S( J) W
  5. returns -1.0.
    8 [7 `4 [( X) _! j; j

  6. 0 a1 V& i5 }* r
  7. >>> math.copysign(2,3)
    2 y! d! A4 Y/ L, h( l
  8. 2.0
    9 G) Y0 H9 u$ x6 U% K
  9. >>> math.copysign(2,-3)
    : e# f1 e" r1 n1 L% `9 x0 N$ }
  10. -2.0' s4 d  r/ U6 n5 Z9 p# K
  11. >>> math.copysign(3,8)% C- \( }1 t8 T2 J$ w) a
  12. 3.0, p. y9 ]( G7 A5 f" X; _4 b
  13. >>> math.copysign(3,-8)
    , p9 }4 x  Y( \$ Z% e! U1 G/ `' r
  14. -3.0
复制代码
8 c& C5 `; B* n4 z: g1 P/ L
math.exp(x)  返回math.e,也就是2.71828的x次方- X; n& A3 y1 S2 ]/ T  j7 j$ t
  1. #返回math.e,也就是2.71828的x次方
    2 |& d' V7 M) x2 S
  2. exp(x)
    : J5 r1 R" l9 a, |' Q( J' g  h6 t; g
  3. Return e raised to the power of x.% c( y, ]( o- d

  4. ( d: e3 N( ~1 h2 ^" m. B4 B( S
  5. >>> math.exp(1)5 V2 M  V  {$ K0 t) p
  6. 2.718281828459045
    # D+ G4 Z0 `( r  c6 A
  7. >>> math.exp(2)  b. {, V2 j/ {, }, ]
  8. 7.38905609893065
    , g2 C, I  O. S: t& C
  9. >>> math.exp(3)
    8 `( E. I) _' }- A% D
  10. 20.085536923187668
复制代码

( q% m4 M+ J1 ]( M& D& umath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
) ~  a- q! F7 y3 T  `! Z* d( V
  1. #返回math.e的x(其值为2.71828)次方的值减1
    : }- C. H; F. i+ U* X+ X
  2. expm1(x)1 s, B1 A: @. K6 |! d
  3. Return exp(x)-1.- e& N6 w: c( k! @! T" H# H
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.3 ^/ w5 e7 ?  v" c: b' D% n; S
  5. . S9 Y$ u- A4 }* U  r' q# C
  6. >>> math.expm1(1)
    # ~8 ^$ G" V9 }5 M3 _/ O
  7. 1.7182818284590451 h( f. w' g6 [) h
  8. >>> math.expm1(2)
    9 j, S( `! ]1 B! D$ o
  9. 6.38905609893065
    5 Z6 O9 ?& J8 j/ O4 n9 \
  10. >>> math.expm1(3)
    ) L& A; F- V5 q; F4 \
  11. 19.085536923187668
复制代码

, g* B$ R' l) `2 m8 B$ A  zmath.fabs(x)  返回x的绝对值
7 l& K  L  M/ H! I7 k: i
  1. #返回x的绝对值
    2 j8 x7 [5 s1 k2 Q' ~, [
  2. fabs(x). z2 c% {$ i4 q# _# g- Y4 D0 a" t7 W1 v
  3. Return the absolute value of the float x.
    - _! \% n/ }1 Z0 J/ w* s. r

  4. 4 e6 Q4 Q, W0 o5 i3 Y5 P, D9 ?
  5. >>> math.fabs(-0.003)
    ; e# P9 |* x6 s! [( A8 e* D, Y: l
  6. 0.003, }+ B, ^' o# L. ~
  7. >>> math.fabs(-110). z' t3 F) {0 x* S6 r; @3 l% T
  8. 110.0
    5 T1 H, N9 |- @& b. j
  9. >>> math.fabs(100)* o' P& X. x6 s" U+ o
  10. 100.0
复制代码
) S( J5 K* G% i3 T1 |; q
math.factorial(x)  取x的阶乘的值( G2 o) u2 h2 [/ H
  1. #取x的阶乘的值
    2 D8 E) ^8 m5 O) {8 u
  2. factorial(x) -> Integral
    $ ~  ~. c# ^  Y* u
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 c. Z* F" [- T: j0 \' {% w
  4. >>> math.factorial(1)
    . j; o/ Y; l& K/ E
  5. 1( B( ]2 r+ E) G
  6. >>> math.factorial(2)( L- m* y) u. L9 i
  7. 22 |! g$ d9 u# f# u  g/ M0 x) r
  8. >>> math.factorial(3)3 m. W3 G* |& c9 ^8 {* S$ g7 l1 \8 b
  9. 6
    * g8 k8 M* _9 X, \- U5 X
  10. >>> math.factorial(5). C% F3 l# _  P; z8 s! A
  11. 120
    ) y- T: C. a0 Q
  12. >>> math.factorial(10)
      ^9 E7 b, D( J. T4 w' \! a+ z3 {
  13. 3628800
复制代码

/ v5 R) A, y4 {3 f: u+ r# }7 ymath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
  ~2 Y' b: l2 V8 O) ?: z3 _
  1. #得到x/y的余数,其值是一个浮点数
    % ^0 i% L& U( L$ }
  2. fmod(x, y): m: u( s* m( P, m  j9 |7 S
  3. Return fmod(x, y), according to platform C.  x % y may differ.4 V1 k4 p  O! [) z; ~
  4. >>> math.fmod(20,3)
    % A' f, a3 I1 _% R. N: {6 T. W) z& ~7 f
  5. 2.07 O3 Z( h. ~9 |! [5 H  B
  6. >>> math.fmod(20,7)
    . p4 M8 H  C! `* T. |
  7. 6.0
复制代码
' e( W& E7 H3 z6 n1 W5 _$ _/ G
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围0 i. L& g1 g% s& a
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,  K; k$ _: F! W
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 a; K  H1 [* l1 n
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ b7 U& @+ j/ A9 n) Q2 x
  4. frexp(x)
    3 H% y* u2 X$ \, w/ G
  5. Return the mantissa and exponent of x, as pair (m, e).
    " ^- b0 S$ k+ F, ]
  6. m is a float and e is an int, such that x = m * 2.**e.- d  g' o* E! d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 S1 C4 y- a( R, N: q/ q7 s
  8. >>> math.frexp(10)
    : V' j& h/ G7 g$ D
  9. (0.625, 4)# a. J7 x, _7 O" f+ {5 q9 |
  10. >>> math.frexp(75)
    % H2 @4 H3 I. J- ~% s, A  B- W! }
  11. (0.5859375, 7)
    7 ]! c  {5 E; f" F# M' s" {8 R* ?
  12. >>> math.frexp(-40)# d' y/ i8 g6 V) g
  13. (-0.625, 6)- P- d8 q2 u3 P  F2 h- b9 }0 w
  14. >>> math.frexp(-100)5 d7 P! h7 b/ V- l
  15. (-0.78125, 7)4 n; b! \7 S. s! R* x; ~9 r
  16. >>> math.frexp(100)! `, }4 v$ E' J+ X- s( G
  17. (0.78125, 7)
复制代码

6 A% `) C( l( G6 Bmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
3 _9 i, Z+ G( I) }  s
  1. #对迭代器里的每个元素进行求和操作
    7 I' v6 ~1 T7 Z* }/ d  s* ~
  2. fsum(iterable)
    3 ^# L5 s$ A, p8 e6 G9 j
  3. Return an accurate floating point sum of values in the iterable.6 {. X2 a! J1 h+ q
  4. Assumes IEEE-754 floating point arithmetic.
      s" e  r5 O2 X% d! |) @
  5. >>> math.fsum([1,2,3,4]): M0 m* @7 H9 O& [2 ~& [9 E$ {
  6. 10.0/ X% w: K2 d$ P& I+ @  Y0 X& Z' |
  7. >>> math.fsum((1,2,3,4))
      q! E+ |& n) N; M1 H
  8. 10.0
    6 u( _  W" B, t- u% E* [
  9. >>> math.fsum((-1,-2,-3,-4)). y2 I; i1 Q! p" a: b! F
  10. -10.0
    + t2 T$ l+ p1 s  `1 \# K
  11. >>> math.fsum([-1,-2,-3,-4])
    ( R6 c4 W" d* Q* c1 y4 k$ O+ d5 U& C
  12. -10.0
复制代码

. X8 |* L  I; G2 @& @math.gcd(x,y)  返回x和y的最大公约数2 @/ Y& a5 W. H/ I2 Z& ^% _" d& \0 N
  1. #返回x和y的最大公约数+ \9 [% ^$ c9 t5 T
  2. gcd(x, y) -> int! ^- M7 E1 U  _  l$ n
  3. greatest common divisor of x and y
    - k: d/ F$ _5 K$ G: `) ], U
  4. >>> math.gcd(8,6)& e& g( I3 J- H8 K) j
  5. 26 |# M9 e3 s6 y3 f' a1 ]
  6. >>> math.gcd(40,20)
    ( }. ]: [' R, w' G" Y( U( f
  7. 20
    & ]# o/ L8 }8 N) {( ^: f
  8. >>> math.gcd(8,12)0 T4 S; o5 h  X  U& I3 @8 R, I
  9. 4
复制代码
6 |5 {- g4 W1 S+ W; o4 \$ \; ^
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False- W4 g8 [; y* H* S
  1. #得到(x**2+y**2),平方的值
    5 Q9 V; Q/ M* `  Z! M
  2. hypot(x, y)
    % W" ^. a+ B/ O6 I
  3. Return the Euclidean distance, sqrt(x*x + y*y).. d: ?! k* `1 R; D% ~! |, u
  4. >>> math.hypot(3,4)- ?4 b. V& @) m. a
  5. 5.0
    " d1 O* N. |3 R. V* }) g# N$ t  A3 ?
  6. >>> math.hypot(6,8)0 H' g' ?; H/ b* r  ^  G
  7. 10.0
复制代码

$ G+ m1 r' }6 v% H. j/ N$ Umath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False1 j' j) w. c0 N- r* V" l( F
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    8 s5 Z: A) _4 t9 Q
  2. isfinite(x) -> bool4 H, O8 p- P: Y) p* n# S+ G: g% z# r$ o
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    0 n, a' r( {% W' R# _+ V4 d
  4. >>> math.isfinite(100)
    6 Z( X$ {" T! n$ A( E/ `
  5. True
    - Q0 |, f' s5 g9 H. j5 b4 z" I
  6. >>> math.isfinite(0). J6 a, h' o3 c
  7. True
    # y0 h/ U" ~( H
  8. >>> math.isfinite(0.1)6 v, |! V; I( x) F% P$ E
  9. True
    ' z, ?1 u  q: v% f4 L
  10. >>> math.isfinite("a")
    6 u' m8 N; h: J8 q8 R! ~
  11. >>> math.isfinite(0.0001)
    8 S/ e2 f1 x( _7 |* a
  12. True
复制代码

2 Y4 N! D6 E2 a9 ~8 J8 q/ `math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ g, H/ b: r9 m" x0 B0 q1 X
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False* G9 |8 I1 {3 k. q% D! Z2 E5 W
  2. isinf(x) -> bool
    5 `5 {: f& u; a- z/ u
  3. Return True if x is a positive or negative infinity, and False otherwise.
      m4 D* }$ ?& A+ U9 T7 K5 P
  4. >>> math.isinf(234): ?4 ^- l# v- ]$ \2 S
  5. False
      d0 z0 J& V! R% ]" G( B
  6. >>> math.isinf(0.1)
    2 Y" D; w8 z  W. Q0 ^6 {. l
  7. False
复制代码

' t5 k2 }4 I5 W8 ]$ E# Vmath.isnan(x)  如果x不是数字True,否则返回False
- V6 O: F5 X8 [8 s" Y% q
  1. #如果x不是数字True,否则返回False
    ; V5 j% D' N: \* N5 }. a: F
  2. isnan(x) -> bool
    4 v9 k( ?- R1 F
  3. Return True if x is a NaN (not a number), and False otherwise.+ ~% c3 s/ q% V/ ^4 G# V
  4. >>> math.isnan(23)
    $ y8 j1 b  n/ O. e; Y
  5. False* w" K& C$ q# b( ]
  6. >>> math.isnan(0.01)# g$ W0 ^& ?- D3 D6 \/ v
  7. False
复制代码

- m' l0 t! k# l1 i& o2 ]  ?+ U3 tmath.ldexp(x,i)  返回x*(2**i)的值+ J. T$ ^9 m) m/ W. P2 C4 Z  Q
  1. #返回x*(2**i)的值/ x5 C+ ]: h; s& b
  2. ldexp(x, i)+ m# O4 k1 B. i$ f; s; B+ b3 b
  3. Return x * (2**i).
    / H( Q* D' q9 g6 _( r1 Y8 K
  4. >>> math.ldexp(5,5)$ H5 x/ f3 O2 z, M% S: I
  5. 160.0- \' M- O% @" k3 N+ n1 E! B
  6. >>> math.ldexp(3,5)% _( l3 w* s5 Q7 [- m" Q8 G
  7. 96.0
复制代码
3 W3 D$ @2 y' v2 N
math.log10(x)  返回x的以10为底的对数
- G  ^/ l- X+ A: W+ z: s6 W
  1. #返回x的以10为底的对数7 ^& h7 j2 \" t
  2. log10(x)
    : K+ B& I# X' A. h1 K- M: W+ o. [
  3. Return the base 10 logarithm of x.( P0 m# {5 f! s
  4. >>> math.log10(10)# c/ M  N9 d) t1 O0 q4 |' j9 |1 B: p) n! x3 j
  5. 1.02 i  L' t) ~) }8 W7 i
  6. >>> math.log10(100)
    1 S" N9 a0 e1 g  @% ~
  7. 2.02 H( o9 |7 ~+ a0 h. w
  8. #即10的1.3次方的结果为20
    / T" c0 O: q3 l
  9. >>> math.log10(20)
    / R5 f: W+ q! s( Z  w; z) e
  10. 1.3010299956639813
复制代码

* y) _9 X  }$ M5 {6 f! Q  V0 fmath.log1p(x)  返回x+1的自然对数(基数为e)的值
$ d5 N( w1 ^% n
  1. #返回x+1的自然对数(基数为e)的值
    " Z$ E1 O! g/ s6 v8 J4 u$ B
  2. log1p(x)$ G) d9 e5 T3 l& X" s
  3. Return the natural logarithm of 1+x (base e).! i5 [7 j: \0 s; b1 a
  4. The result is computed in a way which is accurate for x near zero.' Q8 T9 U- V4 P% R7 m8 M+ o
  5. >>> math.log(10)/ a- e6 S- t1 c' k( q# n3 O$ b5 \# I
  6. 2.302585092994046
    % o# L3 e3 a) v& Q* J( P# H% F* Q% h
  7. >>> math.log1p(10)
    3 y, X, n7 b" c
  8. 2.3978952727983707
    8 D  r% j) c& l7 Q$ S! G
  9. >>> math.log(11)/ n3 a# k- o6 a, I9 O+ I
  10. 2.3978952727983707
复制代码

2 G- }# l4 v7 F; m6 n( O; Wmath.log2(x)  返回x的基2对数
; M8 ~# \/ v+ O7 g. U) |! ]2 U0 c& {
  1. #返回x的基2对数
    9 z" ?" B, T4 p. |/ y
  2. log2(x)- }" ^/ U, [5 ]' d. A
  3. Return the base 2 logarithm of x.
    1 m! L6 R  K5 g# D
  4. >>> math.log2(32)
    $ ^8 k' {1 s* [) B1 o' i
  5. 5.0
    ' L- p2 y* T) P' }% b1 y3 T# }# P4 w
  6. >>> math.log2(20)
    / H; d! r* l2 t# k7 O
  7. 4.321928094887363
    # E4 u0 R8 j, {
  8. >>> math.log2(16)
    9 m6 O2 ?' I9 r$ Q6 `
  9. 4.0
复制代码
6 k8 j% P9 Y( M3 ^; Y
math.modf(x)  返回由x的小数部分和整数部分组成的元组- L( L: M1 I5 k, q' W
  1. #返回由x的小数部分和整数部分组成的元组
    + y4 X1 B% g5 f
  2. modf(x)
    * \4 v) T7 v" ~1 c
  3. Return the fractional and integer parts of x.  Both results carry the sign: U: s' t& w) W9 k$ C
  4. of x and are floats.
    : B( G6 @) Y% X( s
  5. >>> math.modf(math.pi). s7 ^6 ]; H6 U/ \) Y6 d+ d
  6. (0.14159265358979312, 3.0)
    $ y: U7 |# K% H5 s! a* y
  7. >>> math.modf(12.34)
    . |) E& a& P3 M  }- H
  8. (0.33999999999999986, 12.0)
复制代码
/ f' V& R/ y* K# I& d
math.sqrt(x)  求x的平方根
; L+ |: m2 M& U
  1. #求x的平方根
    0 y% Y; p0 b. a+ q
  2. sqrt(x)
    & U, Q7 s+ ~3 _: y6 Q
  3. Return the square root of x.
      n: w4 A0 M9 m" y6 Y: u0 q
  4. >>> math.sqrt(100)% Z. l* j" p- S" K
  5. 10.0
    " _/ z8 P( u$ y1 T9 B
  6. >>> math.sqrt(16)
    / ^) q* M! g5 {9 U
  7. 4.0) n1 K$ h6 r: w" _. X) b8 c2 H
  8. >>> math.sqrt(20)) [, h% I# @) u
  9. 4.47213595499958
复制代码

: i" ^, x/ E$ ]" V' ymath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分- V  Y4 @5 Y, y: t$ H: a' M+ }1 v
  2. trunc(x:Real) -> Integral; ]- t5 \4 f  `- K8 J5 t
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    1 ?; A. F$ _9 U
  4. >>> math.trunc(6.789)+ H- W$ k& z1 I* ^7 V, @7 s. y) p2 z- w
  5. 65 `* G) Y/ O: w+ |; r2 B2 V" \6 e
  6. >>> math.trunc(math.pi)
    $ ?8 H6 ?: C) C& w. F
  7. 3
    7 L7 _. M+ Z* [- a( k; _
  8. >>> math.trunc(2.567)
    6 z; j. u* ^) F5 B+ ^
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-14 21:29 , Processed in 0.083789 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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