新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

/ r6 o" A* P; F* Q. Z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。4 h) \- M# X* z0 n, @, b& T
) l" _, l9 T0 a3 d9 Y. j$ @
方法1
* t4 S) M0 p/ ?/ w9 l# \
  1. >>> import math
    / G3 V5 a' L$ r+ e+ X, `
  2. >>> math.sqrt(9)
      o( v# E9 d; X5 @
  3. 3.0
复制代码
方法2
* f' B) R0 b8 }/ r9 X
  1. >>> from math import sqrt* S( I  @6 S, i5 W; y7 K: ~
  2. >>> sqrt(9). P3 Q# {  _. g+ A) t) x6 \' Z3 q
  3. 3.0
复制代码
& f7 u) |) n& u1 P" f; `


7 l, ^  n/ P( D
math.e  表示一个常量. O: x! T1 B3 W0 p. i
  1. #表示一个常量
    6 S% ~# R% G/ M+ d6 B
  2. >>> math.e. T$ ]# k3 h* a) r
  3. 2.718281828459045
复制代码

: a1 ~- L- J4 [! Tmath.pi  
数字常量,圆周率
; z% z0 O" l; E3 \1 e" W( P
  1. #数字常量,圆周率
    ) ]% U  J, B: ^" y# @
  2. >>> print(math.pi)7 _# C' b2 A1 ?& B9 B
  3. 3.141592653589793
复制代码
- L/ c) l/ I. C8 T1 x% Y4 F
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

3 d4 l* s0 G. r: M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    2 C, }( W/ E, O9 o2 w$ d
  2. ceil(x)
    $ o. O5 |% z) Q
  3. Return the ceiling of x as an int.
      H! B. O7 g* A4 s6 d
  4. This is the smallest integral value >= x.
    6 \" {7 t5 F7 H+ _0 k3 o7 K2 ]; F: x

  5. 5 X' w% G; I# `3 `- O6 H/ A* `# [
  6. >>> math.ceil(4.01)1 F9 z/ A2 n0 P
  7. 51 Z+ @. w5 X8 M( {+ i" m. M
  8. >>> math.ceil(4.99)2 j1 j; q6 S+ I# p
  9. 54 r& s+ X7 [8 L: E9 j
  10. >>> math.ceil(-3.99)
    # X5 G+ u* ^  z7 R
  11. -3
    $ R( x: L/ c) q1 E2 [4 P. P
  12. >>> math.ceil(-3.01)
    6 c9 P: }' E( P( D$ ~% W
  13. -3
复制代码
+ I6 E6 C) D0 Z* A8 l( M
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ E7 Y2 p6 ~& Z/ t
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    " a- s" A& R5 ?, G2 W
  2. floor(x)' I7 l: ?- u" V* t5 c1 \
  3. Return the floor of x as an int.
    & _7 Y; N* [0 s0 C& M
  4. This is the largest integral value <= x.8 _3 U* c1 n# M' k, g
  5. >>> math.floor(4.1)( t1 l) S. k8 @$ l! [5 M
  6. 4& h8 j+ {; |; U+ y
  7. >>> math.floor(4.999)% O) X& K* @6 g. p! h' d6 W% }" i
  8. 4
    : m7 U0 W/ d5 q+ b' ?, N- G* C6 v3 r
  9. >>> math.floor(-4.999)+ ?! {* l* {9 |3 J5 u4 A
  10. -5
    6 L/ _. G; n0 b9 @6 L
  11. >>> math.floor(-4.01)
    ) Z1 Q% p3 y3 ]- C& ~* I
  12. -5
复制代码
. E8 W' R( u& P! i& J
math.pow(x,y)  返回x的y次方,即x**y
' q7 I  V2 i" ^. V7 A" s7 p1 N
  1. #返回x的y次方,即x**y
      ^- U, U- L) T: z3 J$ S
  2. pow(x, y)
    . s# ?0 a& B: X1 c0 J+ x) B
  3. Return x**y (x to the power of y).
    - D+ `' E* _! L1 h- i- T, B4 ^
  4. >>> math.pow(3,4)
    : K/ g/ X2 X! d& F  I
  5. 81.0
    # \! }) v2 A: H1 l$ @1 J- Q
  6. >>>
    9 Q) J8 w" F0 B7 J
  7. >>> math.pow(2,7)
    ( B8 w9 v3 d6 E& C
  8. 128.0
复制代码
' h' n3 a7 s% I
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); }' Q) J' R% r, f" K9 D) e5 T8 x
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 `& a: t" U/ m# n" u
  2. log(x[, base])
    ) u* F3 d/ E- R) v( B( h
  3. Return the logarithm of x to the given base.% \; }" f; l8 P! u+ A/ p4 p, N
  4. If the base not specified, returns the natural logarithm (base e) of x.
    / T% \$ G# E2 Y+ m
  5. >>> math.log(10)8 d# {  }* c% M# a8 t
  6. 2.302585092994046  p3 j' O/ u- i! g' x
  7. >>> math.log(11)$ O7 ?5 Z5 y: R# v  X* h6 ^$ o( ?2 e
  8. 2.39789527279837079 m! ^) P9 \) V* d$ x; i
  9. >>> math.log(20)( ]+ \/ b, U/ d, c) V" f% M
  10. 2.995732273553991
复制代码

" B4 t5 Y* i) }$ {; Lmath.sin(x)  求x(x为弧度)的正弦值
, A) W3 P* g! w9 |6 l8 C6 \
  1. #求x(x为弧度)的正弦值
    * E- c; I* N/ C# C  }- m. ]
  2. sin(x)
    6 ^( w6 w' ^/ ^8 K0 l$ p- I
  3. Return the sine of x (measured in radians).1 }' g1 q! T2 O$ T
  4. >>> math.sin(math.pi/4)
    4 J3 X9 i* v% N; a$ R
  5. 0.7071067811865475
    2 _& B" z. W4 H" ?) F( k
  6. >>> math.sin(math.pi/2)4 q% @/ Q3 d( X7 d% ?2 b, ~
  7. 1.0
    2 ?: k6 `6 `) c* l3 H2 s7 L, t
  8. >>> math.sin(math.pi/3)! Z# C! x" P4 d7 V2 x# c. g  Y  `! I
  9. 0.8660254037844386
复制代码
5 {4 \: [9 Y0 q) A+ h9 `
math.cos(x)  求x的余弦,x必须是弧度8 H* G5 \1 ^# X3 s
  1. #求x的余弦,x必须是弧度; Z0 p' B( T0 P7 j
  2. cos(x)- D7 u* \: M7 B
  3. Return the cosine of x (measured in radians).
    0 `- [8 h7 E6 p) \; |$ c8 @4 q$ c5 \
  4. #math.pi/4表示弧度,转换成角度为45度
    5 h' H8 v' C/ @2 `# ]) ]1 D# Y/ f# v1 Z
  5. >>> math.cos(math.pi/4)
    ; l7 z" i, I' W% e3 s$ f+ S+ \! |% E, h& r
  6. 0.7071067811865476
    1 p: _+ D% J/ j. S. T9 @2 p5 C% S
  7. math.pi/3表示弧度,转换成角度为60度
    2 ?( ^/ ?" P0 X) u/ q4 j  v; |, U+ F, n
  8. >>> math.cos(math.pi/3)
    % e6 ]1 f5 }% m8 T8 j' |
  9. 0.5000000000000001
    3 l. O# E" J2 q1 T2 x3 o# A6 h7 B
  10. math.pi/6表示弧度,转换成角度为30度
    / S* ^, t" O! h0 L& F, m% G
  11. >>> math.cos(math.pi/6)
    3 r8 X# F4 s+ @( q% w- O2 P
  12. 0.8660254037844387
复制代码

2 k+ W2 o/ d( a6 C% _math.tan(x)  返回x(x为弧度)的正切值* U2 \8 m6 L9 n
  1. #返回x(x为弧度)的正切值/ E. D( s9 m/ i/ y$ Q6 c/ g) P7 k- p) O
  2. tan(x)" N' k1 F+ Y+ r8 ]: `
  3. Return the tangent of x (measured in radians).
    ( ?2 \& K$ h- E! I
  4. >>> math.tan(math.pi/4)7 q. h# V& f) q1 \
  5. 0.9999999999999999- w3 I: K2 `6 \( X
  6. >>> math.tan(math.pi/6)
    3 e1 e( E: Q! g% u# g5 Z- \9 H
  7. 0.5773502691896257) r/ i$ {+ O1 E9 O
  8. >>> math.tan(math.pi/3)+ n8 I- D) L- ]7 b
  9. 1.7320508075688767
复制代码

+ h: k& c& [8 p: g$ m) a) p" Rmath.degrees(x)  把x从弧度转换成角度) ^+ y0 ^, G/ I* a
  1. #把x从弧度转换成角度, `; F1 f" k0 M5 q
  2. degrees(x)
    . F  Z2 v  c. z( }, F9 f
  3. Convert angle x from radians to degrees.
    6 d8 _  ~- s0 ~3 w

  4. 0 m6 }* H+ e1 Q/ u7 z8 ~
  5. >>> math.degrees(math.pi/4)
    # ?' `. Z/ Q- @" \+ {
  6. 45.0
    ! }$ o, |" \* g& v; M
  7. >>> math.degrees(math.pi)
    , S; }/ V$ H! g4 N6 q7 o
  8. 180.0
    9 k- {$ Q8 w% `1 C3 _
  9. >>> math.degrees(math.pi/6)$ |- u) P  D2 k/ c$ R- ~
  10. 29.999999999999996
    / y& Z1 l. H( c) e* O
  11. >>> math.degrees(math.pi/3)
    6 n. |2 p. `/ [3 R
  12. 59.99999999999999
复制代码

# }2 K0 a5 ~6 B0 P1 @6 [8 u$ cmath.radians(x)  把角度x转换成弧度' {0 ]' J9 j6 k0 O
  1. #把角度x转换成弧度. U7 @( b7 k' \' r5 s
  2. radians(x)
    8 x+ {* D* P2 J" H6 J6 @
  3. Convert angle x from degrees to radians.
    $ ]1 Q$ R- T# m# c$ }4 Z5 `
  4. >>> math.radians(45)2 ^2 _% u" D( o+ ]. w3 x. K
  5. 0.78539816339744835 O9 f4 T  _9 G* {2 u  A/ D
  6. >>> math.radians(60)" Y3 }# L0 y! c8 B: t+ s6 q% a
  7. 1.0471975511965976
复制代码
2 ^; o% A, l! V- {& D( Y# z
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
+ W7 c$ |& i) w% p
  1. #把y的正负号加到x前面,可以使用0+ G' o5 d- x& u  E$ d3 ^  ]
  2. copysign(x, y)# y0 g+ U2 N* [* {% O2 J" N2 T: R
  3. Return a float with the magnitude (absolute value) of x but the sign - a0 ^. E) ~  Z( I: K6 p
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ; N8 E" j) X% z6 n( O
  5. returns -1.0.
    9 ?8 B  \; h+ c
  6. 6 V' t1 K: J4 O9 E5 N
  7. >>> math.copysign(2,3)1 o" }+ {0 @8 |- H5 c% T
  8. 2.0
    4 ?9 A0 R; K' T4 B9 k% b
  9. >>> math.copysign(2,-3)6 I  l! N. D6 F# I  b# g
  10. -2.0, T6 P) _' E4 _" N2 g
  11. >>> math.copysign(3,8)! W" D2 M0 @+ M5 j* _1 T
  12. 3.0
    2 I: D& B5 E$ @- Q: k7 T3 F# y5 Z7 e
  13. >>> math.copysign(3,-8)
    / X( d, v) O' A+ M8 S$ R
  14. -3.0
复制代码
. p" Z! ~# U% T1 l6 C. Z4 K4 `  n
math.exp(x)  返回math.e,也就是2.71828的x次方- z! y9 j2 t' S2 a1 M" j6 w
  1. #返回math.e,也就是2.71828的x次方% y3 e, Y! i) e. \$ W7 L
  2. exp(x)
    6 F" Z" d; R. o2 l0 n4 B/ F
  3. Return e raised to the power of x.
    2 \( J% p* Z4 N& s( C" V; n3 `/ M3 B
  4. . ]/ E( b0 |9 t
  5. >>> math.exp(1)
    ) ?& T: D/ l( ^' `! N9 \- X
  6. 2.718281828459045
    * V  L5 q6 N" i( x5 T: G6 ~
  7. >>> math.exp(2)
    $ f( D! ^8 m, x+ C! ~5 O
  8. 7.38905609893065
    5 x+ X$ t3 U  e) N0 ~
  9. >>> math.exp(3)
    3 U1 F8 k- p  l7 W
  10. 20.085536923187668
复制代码

, c3 y8 y9 g; `9 `" T& zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1$ R  w) P8 X3 z. ~8 D2 e# V
  1. #返回math.e的x(其值为2.71828)次方的值减19 o; z( Q; G- W- v/ V% o. h
  2. expm1(x)
    . s$ o; [  s6 }9 G: g7 d
  3. Return exp(x)-1.
    9 v5 L1 R7 E. j. L
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    + I2 b8 T" s, ]/ {- F

  5.   J; k' H9 `8 F- P6 R/ C5 y
  6. >>> math.expm1(1)2 {: q5 Y( }' p# [8 N2 n7 v) ^  f
  7. 1.718281828459045" f2 Y) E9 O* h. D; G5 J6 D
  8. >>> math.expm1(2)) x+ @* c" ~3 z1 D; _5 L( A4 F
  9. 6.38905609893065
    ) V9 J6 m( L) Z' ^7 g  e
  10. >>> math.expm1(3)
    ) |  w6 B, @9 p/ D1 ?
  11. 19.085536923187668
复制代码

* Q& F% {- I! _6 C6 s& dmath.fabs(x)  返回x的绝对值
# n4 ~; G" W1 l% ~7 ]
  1. #返回x的绝对值
    - D1 V7 F. ]  v. N+ n
  2. fabs(x)
    3 `8 F! K' [& Z9 e
  3. Return the absolute value of the float x.$ {( B6 E* @% M$ {

  4. . S- d8 C( c) ~5 S( m- K2 P
  5. >>> math.fabs(-0.003)
    % s+ W1 A7 _, [$ U$ ^# h, c
  6. 0.0037 o3 w/ }# W' R" q
  7. >>> math.fabs(-110)  {& n. L3 \! f% b. z1 [/ O5 y& z
  8. 110.0  m% h$ B4 s* }
  9. >>> math.fabs(100)# @" \3 a/ l* k7 h6 Z- n; N
  10. 100.0
复制代码

' q9 d% L6 K: e! y/ rmath.factorial(x)  取x的阶乘的值! b( O5 m4 d5 n% Z% J: S
  1. #取x的阶乘的值
    ( x+ g: B% R8 b- n$ t4 O) g1 z: o
  2. factorial(x) -> Integral% b+ v1 p8 `: D: `% F+ B
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 S  l2 ^7 ~' \8 ?) v1 A
  4. >>> math.factorial(1)
    4 ~. h7 w) ?/ B* X  P9 i- n, R! g
  5. 1
    ; S/ X8 }& @) x' U1 X" \
  6. >>> math.factorial(2)
    8 K0 Z2 i3 u; I- h# K; Q
  7. 2
    9 d3 k- a4 g; q; F( Y
  8. >>> math.factorial(3), J" t7 ]/ D) |, n
  9. 6
    * Y% M# L6 M; H8 L0 ^6 G
  10. >>> math.factorial(5)
    0 G1 L  k4 U( T9 f; j9 Q0 H
  11. 120
    ' O& a6 |- h1 o# M" Y0 |1 l
  12. >>> math.factorial(10)2 o* ?; S/ a1 f; M: F/ c
  13. 3628800
复制代码

* A! g1 c% D) j) h2 s/ Zmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数$ ~4 a% f  U8 b, }( D: u
  1. #得到x/y的余数,其值是一个浮点数2 y+ s) d. p5 {& p& A
  2. fmod(x, y)& c9 `9 {6 j6 Z4 u1 p! Y2 J
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    # h3 W1 Z5 j/ Q) a, Y
  4. >>> math.fmod(20,3)
    5 D) m& F* Q- _& D+ t# L
  5. 2.0( E! B- y" k# I! |
  6. >>> math.fmod(20,7)
    2 c, u, V' o! d  ?2 T$ |
  7. 6.0
复制代码

5 g( l* c& U/ r, }math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 d; t! m. S8 @) @& x
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    9 }- d8 G8 E2 O3 T* |
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 Q* e$ D% k" G; M- o% x. j
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! _& s" Q& B7 U$ {6 @
  4. frexp(x)/ G7 e1 `8 D1 B) S: Y# q# ?) e
  5. Return the mantissa and exponent of x, as pair (m, e).
    " k$ c" q% U, n3 p( m+ \/ t
  6. m is a float and e is an int, such that x = m * 2.**e.
    , [# w, z! l' }( ?/ ~( D
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    9 f4 l, n  t' `) Y% o. I
  8. >>> math.frexp(10)
    " d; H- c1 O+ @. ^
  9. (0.625, 4): I1 P1 f' C8 o$ Y
  10. >>> math.frexp(75)0 k5 P2 ?% H& B* w4 _  ^
  11. (0.5859375, 7). w8 x2 K) Q. T) C1 B
  12. >>> math.frexp(-40)
    * e5 ~. x7 s/ ~+ @( C: r
  13. (-0.625, 6); X& r! q1 e1 G
  14. >>> math.frexp(-100)
    ! D4 z- g  L) G: z2 s( Y, |: k7 ^
  15. (-0.78125, 7), R# A$ r2 n; a
  16. >>> math.frexp(100)* U0 L& q* W5 O' T
  17. (0.78125, 7)
复制代码
- Q% M) \2 r2 D& z) _6 i" Z! M  n
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列3 f7 V7 @8 v/ D; V2 p
  1. #对迭代器里的每个元素进行求和操作" n4 _: O  t  g$ E
  2. fsum(iterable)( N8 u5 t& K! Z  \  C
  3. Return an accurate floating point sum of values in the iterable.+ `; J! w6 I7 p
  4. Assumes IEEE-754 floating point arithmetic.
    ) ^) O: C* [% i: v5 `: ~- ~
  5. >>> math.fsum([1,2,3,4])- W1 {/ T, H4 ?
  6. 10.05 T# d( ?0 _/ W0 b
  7. >>> math.fsum((1,2,3,4))$ ^5 ?/ s# ?$ n0 K" j
  8. 10.02 E4 j# p% `9 C9 [; s3 U
  9. >>> math.fsum((-1,-2,-3,-4))" F! o2 J. }6 e$ I
  10. -10.0  |, B- f8 k3 {' X# I
  11. >>> math.fsum([-1,-2,-3,-4])
    2 g) X/ r" y3 @8 i6 g, E
  12. -10.0
复制代码
4 Y" P) N" q* P# B; J. m/ X
math.gcd(x,y)  返回x和y的最大公约数5 h* ~/ U1 o6 f* q% b3 W" b
  1. #返回x和y的最大公约数3 \% J$ }3 V: \' u4 H3 J
  2. gcd(x, y) -> int7 {2 e, B8 p6 }; k$ a9 l5 D
  3. greatest common divisor of x and y# c' `6 H. {" `8 Q9 p) Q
  4. >>> math.gcd(8,6)
    ( a$ z; R! `! j7 v$ m0 `; U9 R
  5. 29 f3 R2 D9 M, r: Z. H
  6. >>> math.gcd(40,20)
    ; O/ I+ C& d/ q7 {' D5 i
  7. 20
    4 O# C* M! g5 C& I( |; Y! ^6 I7 Q
  8. >>> math.gcd(8,12)2 n# s9 G' P- j
  9. 4
复制代码
1 d+ [, \) ^3 K1 f6 _; E. I7 r, k
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
7 }! V9 Q7 j0 W- ?1 Q
  1. #得到(x**2+y**2),平方的值  w% l  A# w; ]' X. e
  2. hypot(x, y)
    : k; h: n2 r5 w* p# u; j
  3. Return the Euclidean distance, sqrt(x*x + y*y).- d7 o! [8 K) ?+ [) m9 q- C5 \
  4. >>> math.hypot(3,4)
    ' q% T: A; t: S( c' S& _
  5. 5.06 u$ u/ w, d- y  C2 b; L
  6. >>> math.hypot(6,8)" I7 P+ \8 x  O
  7. 10.0
复制代码

/ f( a# a+ m( O- T! c; Mmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
$ N7 H6 r5 d, \2 {
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ' O7 ^# m" H- x8 y8 z
  2. isfinite(x) -> bool8 t2 V7 O7 n  l! w6 j9 `4 P
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.3 s1 E/ H& R9 T% a8 J& U- g: L
  4. >>> math.isfinite(100)
    & u# R) \+ m5 w& [8 N! y1 ]
  5. True
    + ^- T3 E" Q8 K9 V; l* |! z
  6. >>> math.isfinite(0)
      d* B( J4 H# m! \7 j' M$ ]! b
  7. True1 y: Z; d! a( ]8 ^; z' X" e% j+ X. H
  8. >>> math.isfinite(0.1). z( }: `7 N- Q7 w/ V" a# S, o5 H
  9. True
    6 s6 W% S& n, l- |! \
  10. >>> math.isfinite("a")
    ' i8 E4 S+ G+ p* x3 |5 T- \
  11. >>> math.isfinite(0.0001)
    * ~0 i3 M  Q6 V. |
  12. True
复制代码

! @' J; S) i" z5 I. s( wmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
! s4 _, |. {  ~- ?0 w3 A
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False1 T. c8 b- [% |% O6 y# A
  2. isinf(x) -> bool- I( w6 ]( S1 Q: P# {
  3. Return True if x is a positive or negative infinity, and False otherwise.
    4 T, K5 C+ W. [1 r: b" }
  4. >>> math.isinf(234)
    4 m' P. z" I  M4 Y! H: O" n
  5. False/ m/ b" s, D9 ~
  6. >>> math.isinf(0.1)
    . Y+ ]' P0 A+ T' P, v3 t9 o
  7. False
复制代码
- u* {4 J- B! R# x' x. p
math.isnan(x)  如果x不是数字True,否则返回False
2 p" n, h. s& O# O; u( V9 p! Z: {
  1. #如果x不是数字True,否则返回False
    : h# Z) ]' }* Q+ e0 Z6 B$ t
  2. isnan(x) -> bool( y% P0 @* g" h9 j& H4 I
  3. Return True if x is a NaN (not a number), and False otherwise.
    9 Y! L* g/ a' Q/ r3 i0 s
  4. >>> math.isnan(23)3 B& Z( t3 J+ w" u) t& k
  5. False8 _' X5 V  ^! X. H9 C2 b: }, |
  6. >>> math.isnan(0.01)1 w3 H( s: W( r' Y
  7. False
复制代码

. k2 h/ Z+ v' f# f' H) ^math.ldexp(x,i)  返回x*(2**i)的值
  n# e  y. X3 J) \4 m( ^6 _  }/ Y; P: ^  {
  1. #返回x*(2**i)的值
    5 ]  P5 r. H0 M# r% F/ Y9 J
  2. ldexp(x, i)
    9 K! V# p6 E. J4 |- p
  3. Return x * (2**i).
    - I2 |; y, G: I. l* a7 A: B3 ]
  4. >>> math.ldexp(5,5). [# d+ d: T9 H2 ^4 V
  5. 160.0; R8 X( A. N- a# U, ~
  6. >>> math.ldexp(3,5)
    " i' l; Y1 |( L+ I4 ~( h9 V
  7. 96.0
复制代码
$ W' @) X8 b$ R/ D2 O! O/ d
math.log10(x)  返回x的以10为底的对数
9 n: i( V$ ~8 m
  1. #返回x的以10为底的对数
    1 w- V7 ~6 @: z' \# }
  2. log10(x)
    - u( \, k+ E0 G. K7 Q3 O( K
  3. Return the base 10 logarithm of x.  D" H7 A" P" }# h( [
  4. >>> math.log10(10)
    1 b8 p  X& |& h- t
  5. 1.0
    8 I$ e4 c, W5 ?! ~: x6 q! n
  6. >>> math.log10(100)
    $ v  p+ K# ^2 d1 ?! w
  7. 2.0
    * {5 s3 d$ n& E5 Z% E4 b: y
  8. #即10的1.3次方的结果为20! k  k) T; F; o# c8 D. x
  9. >>> math.log10(20)" h$ @: b6 ?9 ~
  10. 1.3010299956639813
复制代码

% y) Y; l$ J. \math.log1p(x)  返回x+1的自然对数(基数为e)的值
. i- s! g6 j/ V& W6 M* Z
  1. #返回x+1的自然对数(基数为e)的值. P6 w5 V! m# @# T, {! H3 Z3 y
  2. log1p(x)
    8 y' K  K/ n5 e: Y- ^7 F
  3. Return the natural logarithm of 1+x (base e).( w2 n0 z: x% ?" o3 w
  4. The result is computed in a way which is accurate for x near zero.
      [% n" d2 ?# |
  5. >>> math.log(10)
    1 f+ D3 M" d$ a! `3 ^! }8 Y& ?, J7 z
  6. 2.3025850929940465 ?4 U1 ]* {( \- K2 m( X" C
  7. >>> math.log1p(10)8 {4 a7 F# O1 v. V( q9 D  u
  8. 2.3978952727983707
    0 ~( R" p) @+ ]# }1 L
  9. >>> math.log(11)  c3 I  K5 V' N" y
  10. 2.3978952727983707
复制代码

( K. @& O( U! T$ j% `7 Xmath.log2(x)  返回x的基2对数
7 E  t# v9 a9 `# x1 {7 ]7 h, S
  1. #返回x的基2对数
    ! o1 o0 `. ?4 X. W9 x4 z( ]' r
  2. log2(x)  b' ^3 I! I5 ], n& G/ R: h6 A
  3. Return the base 2 logarithm of x.
    & g7 X' X: n3 ]! D; `% O, v
  4. >>> math.log2(32)
    0 f  s! K6 {. w8 y6 ~, Q
  5. 5.0, f! o. z6 _0 G! F& O
  6. >>> math.log2(20)/ I0 A. A' E" n
  7. 4.321928094887363
    # v3 V' v  i( Q
  8. >>> math.log2(16)
    2 e+ I2 U) e( _: v
  9. 4.0
复制代码
' W5 ~) R$ r, x) U
math.modf(x)  返回由x的小数部分和整数部分组成的元组3 F/ |  _2 G7 s! g" H& v$ t( T
  1. #返回由x的小数部分和整数部分组成的元组! |3 u& L( ]. I. T. F
  2. modf(x)
    : T5 y, A% I: u! P7 j! F8 |7 n
  3. Return the fractional and integer parts of x.  Both results carry the sign- t% a. A0 ~+ L8 o1 y  h$ u
  4. of x and are floats.
    ( C1 I( t- P: D
  5. >>> math.modf(math.pi), ]) Y$ O, w6 [
  6. (0.14159265358979312, 3.0)
    8 V4 J- b  a. P
  7. >>> math.modf(12.34)
    . ^" i7 t# C' B4 L& W6 Y
  8. (0.33999999999999986, 12.0)
复制代码

! L$ y  _, v5 g3 bmath.sqrt(x)  求x的平方根  b/ x& S$ `1 b: u
  1. #求x的平方根
    . e0 z3 q9 t: Q* J
  2. sqrt(x)
    2 O, A& X( r4 E4 S9 Y
  3. Return the square root of x.* A# W% P1 `3 N# z
  4. >>> math.sqrt(100)
    4 K* r! F' F% }/ e7 j. M
  5. 10.0
    ' a: ?# s. ]2 E( l
  6. >>> math.sqrt(16)
    ; B- G  D1 v) H8 a1 t2 _
  7. 4.0
    ! g& K! x, R* j
  8. >>> math.sqrt(20): ~( l7 f. p. |  C
  9. 4.47213595499958
复制代码

! x+ s8 T6 v" e6 G% X. ~math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    , }8 R3 r1 U( `& u/ E& ^
  2. trunc(x:Real) -> Integral
    1 S5 Q* A  @, R+ n+ P. R$ G
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    # F/ ?6 z# L( T5 m, a' P5 `
  4. >>> math.trunc(6.789)* ?* n6 e/ G; r. [4 Q$ [
  5. 6
    ! t% c/ `# E, U% X1 r
  6. >>> math.trunc(math.pi)
    0 E' i' Z# S. W
  7. 3. H/ ~2 l$ i) J/ ?
  8. >>> math.trunc(2.567)
    % w  o. G7 U8 x9 k# J9 c. Y
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-19 10:18 , Processed in 0.100179 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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