新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

0 p) J% |+ E5 a) e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. D) Z: O7 `# [& f/ D1 e
- q, n, ^- o4 k( A
方法14 t% u0 p6 ?5 K# F  l: ~+ {! L
  1. >>> import math7 d' {. E7 P0 \' k( \
  2. >>> math.sqrt(9)
    / d  J. i8 e6 ]3 x8 u# {
  3. 3.0
复制代码
方法2# G' ]/ E  m) f  `3 g; d
  1. >>> from math import sqrt
    2 M- A2 J; A9 J; H+ b
  2. >>> sqrt(9)( q# ]- \5 S4 }0 Z
  3. 3.0
复制代码
5 j. X4 V, d7 E+ n- d3 O

8 l5 D. y, ^6 X% X, j
math.e  表示一个常量
7 S3 e8 E/ L8 i4 U  E
  1. #表示一个常量
    / Z- A9 j% G4 \0 Q. D
  2. >>> math.e* s! Z5 B5 V1 T" C
  3. 2.718281828459045
复制代码
; \) ]+ r# C2 a* `! u
math.pi  
数字常量,圆周率

: W( G! h& O6 N# A
  1. #数字常量,圆周率
    & C& O. {2 o0 V% G2 {/ x6 k6 [: [  @
  2. >>> print(math.pi)
    - I. P5 v7 c$ ~, f0 x7 A' s4 X
  3. 3.141592653589793
复制代码

8 q# y! p: f1 c& Q  nmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ u9 C+ D7 a2 D; [4 _. {
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ O, X- ?5 l9 ~/ x" @
  2. ceil(x)) Y: C, }9 I2 U0 C, c3 ~
  3. Return the ceiling of x as an int., ]3 M1 B8 `$ R5 F% Q/ T& d5 U
  4. This is the smallest integral value >= x.' G  [. t4 ^9 c* n" T
  5. / T) C! J7 p8 m8 x, D; k
  6. >>> math.ceil(4.01)
    * C( b) U4 t' L* ]9 \
  7. 5' s# _/ w! }, \
  8. >>> math.ceil(4.99)1 P, ^. ^8 e4 U
  9. 5' v: Y1 {  F/ k
  10. >>> math.ceil(-3.99), N( S% N6 y  p1 D4 _4 W
  11. -3
    6 ~3 z! U: x( l! L2 U  @
  12. >>> math.ceil(-3.01)
    5 Y% R+ e  e9 Y) f9 ]4 h, k* C
  13. -3
复制代码
: M+ T' T# k2 L  g
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身: X) O$ k9 u, h% K) z8 v
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ) n( v( v" s- @/ V# @
  2. floor(x)# {' ?+ k% `) `7 b- d, F
  3. Return the floor of x as an int.
    0 u0 g; o# s, |6 O6 m2 [3 I
  4. This is the largest integral value <= x.
    & q$ E& Y4 d6 M( I7 a0 N
  5. >>> math.floor(4.1)
    ' S- l) E4 Y& o/ \0 I/ y$ K
  6. 48 u& ?9 @% ]) ]) a
  7. >>> math.floor(4.999)+ d5 E( {! Z1 ]0 Q
  8. 46 S' b, y5 S2 T- z( B- o% B  L
  9. >>> math.floor(-4.999)0 b( [2 p* F& n# j* E; ~
  10. -5* }" d9 i6 n$ w5 w) Z( \2 G
  11. >>> math.floor(-4.01)+ x3 Y  c, ?5 j9 [: S8 P7 l
  12. -5
复制代码

* D0 Q1 ~) E0 ^( K# V% C% \, ~math.pow(x,y)  返回x的y次方,即x**y
! d$ }  Q  d2 k* h9 n8 u
  1. #返回x的y次方,即x**y
    . s& _7 g/ s; i7 D8 o" |4 J3 o
  2. pow(x, y)
    # n; f9 X5 c. [
  3. Return x**y (x to the power of y).% ?4 W* N% ?! x8 [
  4. >>> math.pow(3,4)
    7 ^8 |5 |% A5 R/ G
  5. 81.0% P& L9 |4 P8 t/ y7 G5 B7 ^$ s
  6. >>>
    # u) d9 W) G4 P$ ]
  7. >>> math.pow(2,7)
    9 D9 L; R( n3 l+ x# p! C& P
  8. 128.0
复制代码
7 Q( v& h# z. Z3 W& B( j9 [+ w* q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 k1 Y# _; U9 O# w0 q
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! M# I% a* ~8 h- Y% D
  2. log(x[, base])( }8 ^! r3 Y5 j" N& a" m
  3. Return the logarithm of x to the given base.
    & R, o, O& I7 G( Y
  4. If the base not specified, returns the natural logarithm (base e) of x./ c9 R' I4 p2 g( a
  5. >>> math.log(10)# S- R- e$ Z/ I; a* ]# J, I: f( f3 z
  6. 2.302585092994046- G% R- s3 C$ o- B$ i: n# ]4 K
  7. >>> math.log(11)
    ' s, {; c( Q2 S2 N, L& _
  8. 2.3978952727983707
    - s$ Z2 ^* d; r8 |! U' ^
  9. >>> math.log(20)6 f$ Z8 ?  `  {" x% ]" Y0 `1 `4 l
  10. 2.995732273553991
复制代码
" J+ M0 h/ M$ _; Z: a! X
math.sin(x)  求x(x为弧度)的正弦值
, R& C$ d0 S* u! N/ |% L" W
  1. #求x(x为弧度)的正弦值
    & t5 T: ~* n/ I
  2. sin(x)
    . \1 J8 c$ n" W% [) b* D
  3. Return the sine of x (measured in radians).
    $ X/ z1 E) @% h$ a$ G# ]4 D
  4. >>> math.sin(math.pi/4)
    + ]0 B) F+ F5 t9 k, f) d' g# s
  5. 0.7071067811865475
    # n  \* l" l) j1 X8 s7 ~5 m
  6. >>> math.sin(math.pi/2)' N1 X% d6 M6 ~) H! @
  7. 1.0/ |/ [0 ~7 r6 p) l
  8. >>> math.sin(math.pi/3)$ w. [5 {4 d1 Y# d% @
  9. 0.8660254037844386
复制代码

4 n4 k8 Z+ I$ a8 K* ^math.cos(x)  求x的余弦,x必须是弧度* _2 J5 l, l( |% Z
  1. #求x的余弦,x必须是弧度
    . N( y, w2 o; _& B+ d1 p
  2. cos(x)$ `7 x2 Y7 u: B* d( f- @2 E
  3. Return the cosine of x (measured in radians).: @( y& ~0 o# ]! @' f! O# f' P# F
  4. #math.pi/4表示弧度,转换成角度为45度' K! N3 P) Z. v5 Z
  5. >>> math.cos(math.pi/4)
    6 h% P: I7 _  y& w2 _' U
  6. 0.7071067811865476
    4 E0 I2 z1 X/ {% X$ K- B6 j
  7. math.pi/3表示弧度,转换成角度为60度
    + f( f& I7 y- m3 @
  8. >>> math.cos(math.pi/3)
    # {% i  f3 U* z7 E9 ?
  9. 0.5000000000000001
    7 J1 \, ]" v0 ?$ }) L
  10. math.pi/6表示弧度,转换成角度为30度
    ) n5 A, t# K- Y
  11. >>> math.cos(math.pi/6)
    & z% R" A( i8 a; s: q) @, d
  12. 0.8660254037844387
复制代码

2 l9 E3 }  m# X7 R6 x4 r, ~math.tan(x)  返回x(x为弧度)的正切值1 N" k- n& y0 u( ^, `
  1. #返回x(x为弧度)的正切值
    9 ^- Z% W0 [: v; V" W# W
  2. tan(x)
    6 s8 H# A/ D& X+ o% w$ F
  3. Return the tangent of x (measured in radians).
    8 J' `' B  {5 b  M, J
  4. >>> math.tan(math.pi/4)
    " l2 a, w: L8 p% J7 L. F
  5. 0.9999999999999999( \' Y: S# ^# [$ ?
  6. >>> math.tan(math.pi/6), }3 l0 x, P" z5 S
  7. 0.57735026918962578 a) W; {: d' d2 x
  8. >>> math.tan(math.pi/3)
    9 I5 v6 X( `/ C+ \5 h
  9. 1.7320508075688767
复制代码
1 W9 ~/ _7 C5 A! R& K* [8 r
math.degrees(x)  把x从弧度转换成角度
/ a/ Y, O4 p; \
  1. #把x从弧度转换成角度
    / b% r& k$ s2 o2 R8 a$ M9 s
  2. degrees(x)
    3 @& o3 Y8 x; m
  3. Convert angle x from radians to degrees.! R: {+ K0 x3 M8 \& j( N
  4. 5 L& w" \$ ]$ m! [: p
  5. >>> math.degrees(math.pi/4)) c' T. F  E" y/ _
  6. 45.0
    , w) j: T' e9 U4 h* l/ l
  7. >>> math.degrees(math.pi)
    9 c3 @+ k* J4 l4 \
  8. 180.0  b  ~/ \, S, U; U- T% P, n0 I8 ^: C
  9. >>> math.degrees(math.pi/6)0 Q. Q4 o. o# V( U% n' M
  10. 29.999999999999996
    ; \* h$ `7 U3 k! y; Z
  11. >>> math.degrees(math.pi/3)
    3 N% c+ n2 j% x. ]9 r
  12. 59.99999999999999
复制代码

: Q: |0 r& L9 h& f, L" z9 S: bmath.radians(x)  把角度x转换成弧度! P! s% A, a0 k) T8 `2 d
  1. #把角度x转换成弧度. J2 y7 D% L, G' _
  2. radians(x). I( t( P! \  t4 u% S: Y) `; t8 C0 \
  3. Convert angle x from degrees to radians.
    ( J$ J* x+ \+ D0 ?$ R2 p& F; l
  4. >>> math.radians(45)
    # v( ^+ P2 B0 W: ?
  5. 0.7853981633974483& @1 E+ \) w. K4 _2 J" F
  6. >>> math.radians(60)
    2 e( o7 p" y  i; p* R7 C3 w
  7. 1.0471975511965976
复制代码
; Q2 O! C4 p& T5 n
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
" \2 n" f! O6 p% S8 }
  1. #把y的正负号加到x前面,可以使用0
    : S5 X- U) l0 @* r0 Q3 T
  2. copysign(x, y)' Z+ \& f' x& c& D$ c$ Y
  3. Return a float with the magnitude (absolute value) of x but the sign
    # I3 W' \$ Z3 u, A& J' C
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    9 `5 Q. U- B0 J* ^- d$ T/ P
  5. returns -1.0.  Z& Z- N& q! ?! h

  6. / n: R4 }" O& U0 A9 |
  7. >>> math.copysign(2,3)
    - y% }' {$ t" N0 t
  8. 2.0
    ) O$ b# k: o3 y! x
  9. >>> math.copysign(2,-3)
    ( K2 p7 ^, n; \6 k5 j
  10. -2.0
    7 f/ a/ I4 I1 Q0 f  |
  11. >>> math.copysign(3,8)
    ) p( D* G$ Q! s; {! I" M( _! n
  12. 3.0
      c' k' q6 }* b$ Y" W% C
  13. >>> math.copysign(3,-8)
    , b/ U. E9 x0 W
  14. -3.0
复制代码
& P8 ?/ |, q, r, }
math.exp(x)  返回math.e,也就是2.71828的x次方7 ^0 g/ V, q: c0 a& i
  1. #返回math.e,也就是2.71828的x次方
    ! V, h7 Q$ W# X2 `) E
  2. exp(x)
    ' C4 j) F0 y; J
  3. Return e raised to the power of x.+ U. }5 [( N5 t4 t) T5 U/ M4 `
  4. + s5 ~5 S# j  K: h9 F- }& U
  5. >>> math.exp(1)2 i6 p. P! R# n; v4 Y% r# n; C  C
  6. 2.7182818284590454 ~8 h: _/ a. V% z* ]7 j
  7. >>> math.exp(2)
    * a3 B$ x: b$ B
  8. 7.389056098930650 B1 v) E5 L& ]! j1 U
  9. >>> math.exp(3)
    4 V" C2 p- d" g2 D* S
  10. 20.085536923187668
复制代码
2 O1 \7 A) P% T
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
6 B  D5 x4 _+ u. r; T  O6 Y5 [! m
  1. #返回math.e的x(其值为2.71828)次方的值减1
    - P- ^0 y0 e2 w1 I$ ?* H
  2. expm1(x)- u9 H; n' Q+ _- C$ u5 ~, g6 p
  3. Return exp(x)-1.
    / r! L0 L  h. L3 W- s0 G9 O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    , M) J2 c- A- t" @/ W: Z# l

  5. . C1 T1 H+ u* W& M4 S6 M# @$ S
  6. >>> math.expm1(1)" f! t- j$ L5 G+ {
  7. 1.718281828459045! x* N: U$ O- ]8 Y6 R  [; Z
  8. >>> math.expm1(2)3 W0 t# v  p5 e1 o. }
  9. 6.38905609893065
    7 \( S. ]1 R- u4 [, y, e
  10. >>> math.expm1(3)
    / z" O  T; w/ r5 Q3 d$ Q
  11. 19.085536923187668
复制代码

1 s. @4 o2 L4 b# A9 Umath.fabs(x)  返回x的绝对值
# U: m8 a; [, Z9 {3 L1 ~1 L) ^5 B# w
  1. #返回x的绝对值
    5 F( Z! j8 m  Y) T4 _$ }
  2. fabs(x)3 z# a( G& |" Z0 Y# I' A$ {. v' |
  3. Return the absolute value of the float x.
    0 g$ N! {. d; S+ {) U4 N
  4. ! O/ g$ ?: h1 u" [
  5. >>> math.fabs(-0.003)
    ' x1 W. E/ a6 m" s* O
  6. 0.003. Q7 y$ }$ X  [6 V
  7. >>> math.fabs(-110)5 ~+ ~' _9 d3 h: j: M
  8. 110.0
    3 W! V% q) F& C6 Z; M/ Z; ~
  9. >>> math.fabs(100)
    / `6 r& c, C" {) X( y
  10. 100.0
复制代码

2 ^  M* y( n$ tmath.factorial(x)  取x的阶乘的值, F- y4 a/ B# ?6 a" b7 y# x
  1. #取x的阶乘的值
    . n5 i4 X+ s, I$ d$ m$ t
  2. factorial(x) -> Integral+ U% h0 M( `, L
  3. Find x!. Raise a ValueError if x is negative or non-integral.1 H8 `, j* Y. q8 B) l
  4. >>> math.factorial(1)
    : d: k" B1 }# U/ d0 ?8 U& {: I, J
  5. 1
    ! g2 z3 V* ?6 E) ]4 V1 ]; S( x
  6. >>> math.factorial(2)# x4 ~; S( G% m7 Z4 d
  7. 2
    - ]! [1 B: x6 a% q( n' V* H
  8. >>> math.factorial(3)4 u+ c- |7 X7 Z- w: G4 [7 D3 i  @) Z
  9. 6
    & [& l& E/ ?- W1 M& \
  10. >>> math.factorial(5)0 B7 d& D  b, ]
  11. 120
    9 o5 u6 C7 q. d* K4 V7 O7 K6 e
  12. >>> math.factorial(10)
    0 J' c3 }& H7 w! z7 D# z
  13. 3628800
复制代码

' }5 K; n: _7 Z6 ?# R. Amath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
% q0 L9 ~3 N6 N8 ?: s
  1. #得到x/y的余数,其值是一个浮点数
    & _! O' I& f) T4 `: ?! r! r
  2. fmod(x, y)
    0 u& D$ n- F, G1 L/ n6 e! S
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    & f+ @8 M6 ]; Z9 E6 x: q
  4. >>> math.fmod(20,3)
    # d$ C5 H2 b3 _5 D0 A" c
  5. 2.0) S: E$ w3 G) n4 e) l, g6 V! X1 z
  6. >>> math.fmod(20,7)! }: J8 c* x( k# p
  7. 6.0
复制代码

2 M7 @+ W" w* K. p* Z1 a: d& v4 tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! ?$ d. L5 R& F2 `
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    7 S8 M( V+ j! }: h/ n
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ! c$ V0 v  {9 q6 y5 t( E( F0 o: c
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, K7 _& T  N; N
  4. frexp(x)8 d9 s. C1 s* N& B8 d2 L* B! m
  5. Return the mantissa and exponent of x, as pair (m, e).) W  e2 O) }& Y! d* \
  6. m is a float and e is an int, such that x = m * 2.**e.
    0 i# u4 E" ^. Y5 L( ^
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    % q' s5 u; M% _% Q& w2 {$ P
  8. >>> math.frexp(10)! g7 n8 |+ b. r
  9. (0.625, 4)8 O0 S7 |5 u# C3 K5 G" A0 f
  10. >>> math.frexp(75)
    * ]8 i) X; `+ @" ?
  11. (0.5859375, 7)7 a- p' u% k" Q# D4 x$ j
  12. >>> math.frexp(-40)
    9 i7 n/ Q% Q1 U' T3 u
  13. (-0.625, 6)  C) Z' f# R& v4 C
  14. >>> math.frexp(-100)7 ~+ ^$ M; s$ g& ~; Q  n2 k
  15. (-0.78125, 7)
    - n5 ]2 c; d! Z& s% ?0 p% W; n
  16. >>> math.frexp(100)6 f5 H9 T+ q. ]6 s9 H* j1 n
  17. (0.78125, 7)
复制代码
4 K" K( y+ K/ I  _. X9 A5 {
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列. `+ i! X6 c' I6 E: i
  1. #对迭代器里的每个元素进行求和操作
    ) X7 c( q) n% i) M3 `8 O+ O
  2. fsum(iterable)1 L: i: V+ p; j5 [3 a3 s3 o
  3. Return an accurate floating point sum of values in the iterable.
    ( L" r$ ?; h: [0 ~! D3 J% h
  4. Assumes IEEE-754 floating point arithmetic.5 z' }8 ?/ G' Z
  5. >>> math.fsum([1,2,3,4])
    9 X7 j4 ]7 g/ m3 W: E" A0 p8 y# j
  6. 10.02 v1 _6 k" Y' t1 _9 m# k
  7. >>> math.fsum((1,2,3,4))  o8 Q, V) u* g' f, Y6 H2 l% X
  8. 10.0
    ; O1 Y7 D+ g4 N+ Q+ O( p/ s
  9. >>> math.fsum((-1,-2,-3,-4))0 A: N( i0 I2 _6 D5 c8 A; K( I* ]) W" k
  10. -10.0! c' ]# l+ S( z) O
  11. >>> math.fsum([-1,-2,-3,-4]); |# W" @: Q. ]" P5 U) l6 d
  12. -10.0
复制代码
3 W5 _- r9 g9 Y. h
math.gcd(x,y)  返回x和y的最大公约数/ X; l- W- |$ Z  k
  1. #返回x和y的最大公约数
    , J- _/ |, ^1 Q  w# h
  2. gcd(x, y) -> int
    8 D/ d, C' C; S- V
  3. greatest common divisor of x and y
    ; B3 G# j, u! Y1 T3 A6 M7 b
  4. >>> math.gcd(8,6)
    % X; s& X: B2 ?' O
  5. 2
    3 w# Q& n4 x) H$ r6 ^- H/ j" y
  6. >>> math.gcd(40,20)$ v# w; M# w1 |2 i
  7. 20
    , u7 l* n$ V& V+ G
  8. >>> math.gcd(8,12)7 I0 X9 B! V4 ?  A; y! M% L5 K
  9. 4
复制代码

5 f/ q$ ^2 l/ y2 t: l* [8 o/ Omath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
! _1 }* G6 v% n. J
  1. #得到(x**2+y**2),平方的值- l5 v1 m3 p6 W, @/ [- Q* B
  2. hypot(x, y)
    7 y* [3 v+ Q- x+ i
  3. Return the Euclidean distance, sqrt(x*x + y*y).% k" X2 d- f+ f# D
  4. >>> math.hypot(3,4); ]% L+ ^. U  V5 c% h$ t
  5. 5.0# o% Z' ?+ i- E; i: g
  6. >>> math.hypot(6,8)0 z/ A$ l5 m9 q) b- B- \  X
  7. 10.0
复制代码
. l# B" }3 a) y9 r, }
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 @5 N  A6 H9 H/ P4 m
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    6 x; Y, D+ F2 i/ b, N
  2. isfinite(x) -> bool
    2 d- J. |. m) D3 v: \
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    9 E) i3 C. L' P3 F# v
  4. >>> math.isfinite(100)
    ( D& r4 N1 s1 N2 _4 ^  U
  5. True! D" {2 `1 f5 y1 m2 b
  6. >>> math.isfinite(0)
    9 N# h) u6 T5 F0 x1 T3 P9 P. I& P
  7. True
    ' S2 H% n. J; [% V0 Z$ u* i
  8. >>> math.isfinite(0.1)
    2 p5 [- q4 z: ~8 L. ~2 _7 U
  9. True5 i# e6 U  {: R5 v% x+ ]  s
  10. >>> math.isfinite("a")
    3 Y( o' C8 X1 S
  11. >>> math.isfinite(0.0001)
    4 Z1 Z! r5 V2 R
  12. True
复制代码
1 m9 S$ |% w: _" I
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
  u% o$ |+ B% Z  W" j' c
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    , `0 P  ~+ h; Z& r: v
  2. isinf(x) -> bool/ h1 R  Z% b' H' K) H
  3. Return True if x is a positive or negative infinity, and False otherwise.
    : `. l; [4 \$ Z8 R' g
  4. >>> math.isinf(234), w6 T! c+ o" ^
  5. False
    + t+ C- J; B6 z, x# v3 {0 z! z' s
  6. >>> math.isinf(0.1)
    + I3 `4 q5 M) ?% {( J, e5 X
  7. False
复制代码
% h1 c5 z* _5 _, @: K) w8 d# T: n+ a- Z
math.isnan(x)  如果x不是数字True,否则返回False" h9 e9 X; F5 o
  1. #如果x不是数字True,否则返回False% a+ M- C( X7 C+ o7 I/ u. O
  2. isnan(x) -> bool
    ) u3 y6 D+ L( A( a( O9 ]
  3. Return True if x is a NaN (not a number), and False otherwise.: B+ |6 K. I* m4 v8 ~, c. c) M; r' F
  4. >>> math.isnan(23)& u& V  q2 J! M9 @  ?# y: p5 O8 Q
  5. False
    % O7 n( ?0 S8 n2 b7 Q
  6. >>> math.isnan(0.01)
    5 a$ \3 @3 g" m, H/ u+ q
  7. False
复制代码

- ]  M& z3 c$ ]) \! U8 g: Bmath.ldexp(x,i)  返回x*(2**i)的值+ n/ \0 a' i9 |$ f: x
  1. #返回x*(2**i)的值
    , k$ v% z) I2 z7 M. j, w
  2. ldexp(x, i)( [1 O0 {% ?( p+ j3 x
  3. Return x * (2**i).0 F7 I0 u6 s6 R) o* w
  4. >>> math.ldexp(5,5)1 j2 H% l$ o6 A/ `" C
  5. 160.0
    & n4 A- g6 e& `, C4 ~# Q1 v# W
  6. >>> math.ldexp(3,5)
    6 \% S5 T% `9 v+ a, o3 [4 j; I6 U
  7. 96.0
复制代码
6 o% @6 e7 P2 v) z' x2 I3 l
math.log10(x)  返回x的以10为底的对数  |8 u  d& A: `+ Q. G5 B4 q
  1. #返回x的以10为底的对数
    / K: J9 g0 o9 y9 v
  2. log10(x)
    * L2 m' Z3 i2 O# e8 t
  3. Return the base 10 logarithm of x.% Q; `4 |" G2 N$ s
  4. >>> math.log10(10)# b7 A# d! t! c
  5. 1.0' [4 C1 B) W4 {' l: S* H1 I
  6. >>> math.log10(100)) ^# s  k, I& X( ^1 V! I  d
  7. 2.0
    2 j1 m' r  P8 p/ k9 F0 A
  8. #即10的1.3次方的结果为20
    # e" v1 t+ m3 b
  9. >>> math.log10(20)4 ~0 O+ m  K9 j' y3 e
  10. 1.3010299956639813
复制代码
+ m1 J9 l5 s2 f* t6 _
math.log1p(x)  返回x+1的自然对数(基数为e)的值
4 _* G9 [/ ?; H7 t
  1. #返回x+1的自然对数(基数为e)的值2 o' h$ l8 o+ x* h9 r+ Z
  2. log1p(x)
    / \6 B7 ?6 J# `; R! }2 y/ C
  3. Return the natural logarithm of 1+x (base e).
    / q2 `: L% h0 H: s9 V6 l
  4. The result is computed in a way which is accurate for x near zero.! h* p! j+ |; z  }* W$ |0 w# T) n
  5. >>> math.log(10)
    # M# s+ c# z! B) B
  6. 2.302585092994046. l' o1 b! z+ [( ]! c  q9 h4 q
  7. >>> math.log1p(10)
    7 P" y7 H# ^6 Y9 T! x' ~& O
  8. 2.3978952727983707. m8 C5 ]& |) W5 I! e
  9. >>> math.log(11)
    9 c3 V0 F; v' N" T8 S! g  W1 n* d
  10. 2.3978952727983707
复制代码
  B$ ?: v6 F1 Z$ q
math.log2(x)  返回x的基2对数
) R2 X8 I" c  `: q1 q& _8 q1 L
  1. #返回x的基2对数& p, E$ m9 N# q' T% a( r
  2. log2(x)0 Y- y7 x- V2 `" d
  3. Return the base 2 logarithm of x.
    % Q9 x. E1 g/ m/ O% \0 l
  4. >>> math.log2(32)' V  F% Q% U- h8 r' M
  5. 5.0
    # g+ ?! n, X; V8 J
  6. >>> math.log2(20)1 `' ^* ^, ^% |* B' H8 ?
  7. 4.321928094887363
    ; u& h8 K" T- X7 f! z
  8. >>> math.log2(16)# ?  b2 R# Q. a: I2 |
  9. 4.0
复制代码

7 X- Y7 p7 i) V: R3 _, _/ ~' ^math.modf(x)  返回由x的小数部分和整数部分组成的元组' e; R, u$ r" V9 {9 W+ k/ O. [2 p+ o
  1. #返回由x的小数部分和整数部分组成的元组# C3 W* z' S( }( R0 k/ p
  2. modf(x)" G8 r; Q* E' B1 f- p- a& T
  3. Return the fractional and integer parts of x.  Both results carry the sign% E* [/ w. X( A2 a
  4. of x and are floats.1 X0 Y* Y3 G  w) I8 P& B$ E* z3 {! \
  5. >>> math.modf(math.pi)
    ; X0 P' P/ i. ^: @
  6. (0.14159265358979312, 3.0)" P/ f. l( e, i3 V
  7. >>> math.modf(12.34)4 ~3 M2 }3 _4 W4 @0 |
  8. (0.33999999999999986, 12.0)
复制代码
: A, F0 {9 o: K; w
math.sqrt(x)  求x的平方根
8 I! O1 S( c: R1 y! I& J
  1. #求x的平方根
    0 s, O8 i+ ^+ U: |$ N2 w
  2. sqrt(x): I2 Z! B+ t3 o- b- [' {5 t9 U
  3. Return the square root of x.5 I% X% }9 ?  e7 `0 P. j
  4. >>> math.sqrt(100)- V2 c6 A* O! E
  5. 10.0
      n2 t0 u$ K6 F% Z1 i3 Z/ O
  6. >>> math.sqrt(16)
    $ ]) B" L7 R* Y0 E# m5 |
  7. 4.0/ {- ?% W# g9 U" V
  8. >>> math.sqrt(20)
      j7 l& R0 v% i. y  B. o  x6 j
  9. 4.47213595499958
复制代码

7 v7 m9 R% X& tmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分' e( _. T% ^6 y
  2. trunc(x:Real) -> Integral3 {, R' W) i9 c' z8 h" l
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' p1 `+ r; L) v1 ]- C6 O5 \
  4. >>> math.trunc(6.789)
    # R3 V/ E. Y' k" @$ ^: v. z( q# k* k( |
  5. 6; B+ Q3 A, M0 K+ i
  6. >>> math.trunc(math.pi)/ I1 o: O4 g- h7 h' p3 z
  7. 3
    ; d0 p- c5 d8 |4 E( a
  8. >>> math.trunc(2.567)+ R: \, k( ~* X0 i1 W8 ]/ m# I
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-5 19:37 , Processed in 0.085405 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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