新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
0 I, ^) d: u  C& ?% i7 e; l
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 J1 T& ^4 l% ~/ a* j2 x* A

; \; p: J! k& }+ d  p方法1& T1 B4 H4 }2 [$ ~8 w
  1. >>> import math, s- G3 _2 d1 e
  2. >>> math.sqrt(9): x% A/ V$ M" M3 _7 Q/ M7 g
  3. 3.0
复制代码
方法2
$ C4 H& Y" U0 {$ s
  1. >>> from math import sqrt! T5 L; x5 `2 E
  2. >>> sqrt(9)
    * b* V( N! P' D+ z: ~( v
  3. 3.0
复制代码
( T4 f) @& L& ?' Q3 H9 ]! ]7 W7 f


/ u9 h  W5 [; y9 p4 {& a
math.e  表示一个常量8 h" d3 P' v" [) _5 X* u% Y
  1. #表示一个常量
    5 f, Y) Z' W  p# K4 a. b2 L! |
  2. >>> math.e
    5 G' u% c; A; @! O* ?9 \+ ~$ f
  3. 2.718281828459045
复制代码
6 |  k9 w3 X- t% ]( D
math.pi  
数字常量,圆周率
2 ]( p& ?# D6 M! ~1 J; J
  1. #数字常量,圆周率) h; _( c3 Z4 B4 r, V# \5 i7 m
  2. >>> print(math.pi)
    4 C" f# X0 q) O+ N
  3. 3.141592653589793
复制代码
. [5 b+ f0 o9 S6 V0 ~  r
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

8 z% A5 l: I& l; t8 Q
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x) F% T- B" r3 [; C' a
  2. ceil(x)% \. `/ G9 J2 P' Y! A9 W
  3. Return the ceiling of x as an int.
    , J. c( l, _  b; v
  4. This is the smallest integral value >= x.
    # N! V- h- \4 ]+ _
  5. ! c# f1 Q. a6 ~0 Q$ E7 M
  6. >>> math.ceil(4.01). m" d; j6 }$ w  V; o
  7. 5
    9 r9 d: {* P1 [0 f# |. V1 |
  8. >>> math.ceil(4.99)3 P) w; K% |0 \, M
  9. 5
    3 }7 H( M/ e+ ^1 H* z1 }4 C( d' }
  10. >>> math.ceil(-3.99): q7 h% a8 a$ s/ A; X: S, Y
  11. -3
    4 z4 s8 h* m* u7 K
  12. >>> math.ceil(-3.01)
    8 E; W8 I* h' R5 ^) a  P
  13. -3
复制代码
8 C" K- l' R$ S, `) n' V7 z
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) _/ E# ?: [( i
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    * n% S. t7 ]/ a
  2. floor(x)+ h/ G, V$ I# p. @5 P$ |
  3. Return the floor of x as an int.$ R2 {' y" p, {/ x2 _5 J
  4. This is the largest integral value <= x.
    " V' m% m/ G& t5 i
  5. >>> math.floor(4.1)# e0 |: a4 J, ^6 j+ G8 M9 b
  6. 4
    $ }% F- r0 Z# X
  7. >>> math.floor(4.999)0 `0 @8 M( P: G
  8. 4
    9 U6 J2 W1 ?: t- Z% g
  9. >>> math.floor(-4.999)
    ! N5 [6 L8 O' |8 l! x' }
  10. -5
    9 k/ y/ x7 S1 P! g
  11. >>> math.floor(-4.01)- ~9 M! K2 A  y+ P/ E: n# p: q1 F
  12. -5
复制代码
  b+ y9 O7 F0 [& ^
math.pow(x,y)  返回x的y次方,即x**y' c2 W6 \' y, c7 b3 V1 v
  1. #返回x的y次方,即x**y
    5 u/ T9 ~4 l, r% b8 h
  2. pow(x, y)
    $ Q/ i2 g! Q+ J# E3 e4 I
  3. Return x**y (x to the power of y)." f5 m5 B4 g' G, e1 W
  4. >>> math.pow(3,4)
    8 P8 J4 ]% F: F
  5. 81.0
    1 F) P) G' u8 v! h
  6. >>> ) v! d3 |3 d8 W
  7. >>> math.pow(2,7)4 g6 ]: j- i. W1 @
  8. 128.0
复制代码

: l! ]- j8 H6 C  R% N1 G  Q6 b, Smath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ s( S* `+ S4 ~) B7 r' t$ r
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    $ i- R: W1 i+ X) `( Y$ j/ W
  2. log(x[, base])3 k' _  D& m6 G
  3. Return the logarithm of x to the given base.
    - s- X% O6 O4 L" J# Y
  4. If the base not specified, returns the natural logarithm (base e) of x.
    + X0 X5 k7 G; E
  5. >>> math.log(10)4 s1 q! r7 h5 j4 o
  6. 2.302585092994046
    1 _0 N) X6 ^3 r; z  `
  7. >>> math.log(11)
    7 w! y6 x( a( J: N
  8. 2.3978952727983707, t* o% {/ u! C+ A
  9. >>> math.log(20)+ p# a  d0 A" t6 a. l# h$ g
  10. 2.995732273553991
复制代码
$ u$ Q. y' t. E/ a9 S- o
math.sin(x)  求x(x为弧度)的正弦值; m$ {% \- {5 l% O5 a. C2 d" X
  1. #求x(x为弧度)的正弦值8 `" B6 S! `  x
  2. sin(x)
    . X% Q0 E) ^) p' j
  3. Return the sine of x (measured in radians).  U3 {$ s  G  d4 N. y+ @) a
  4. >>> math.sin(math.pi/4)
    % j8 @; |! g# f1 C- `' s
  5. 0.7071067811865475
    0 f) Q# H+ W  K1 W$ |- o
  6. >>> math.sin(math.pi/2)
    . s0 e& v; `& M
  7. 1.05 o1 Z! }, X) k
  8. >>> math.sin(math.pi/3). c' K5 O2 a8 T8 x6 a
  9. 0.8660254037844386
复制代码
, p( A* G% X, O8 i4 o1 w
math.cos(x)  求x的余弦,x必须是弧度
/ h+ b& M  k9 v
  1. #求x的余弦,x必须是弧度
    6 }# ^+ d& |/ W
  2. cos(x)
    4 K4 N- K$ w/ j& ]3 X/ t8 i0 O
  3. Return the cosine of x (measured in radians).
    + }1 p) y$ k7 k: Y  b/ r5 y( M
  4. #math.pi/4表示弧度,转换成角度为45度) x1 H7 k9 f$ a+ K0 U
  5. >>> math.cos(math.pi/4)
    6 ^! q  f7 [0 I
  6. 0.7071067811865476
    0 J& x3 H2 d: h" V5 x
  7. math.pi/3表示弧度,转换成角度为60度! o5 ]+ L: y, F
  8. >>> math.cos(math.pi/3)
    4 O  @8 ^9 r( b+ V6 V; L
  9. 0.5000000000000001$ n# [7 i  ~* L" m# D
  10. math.pi/6表示弧度,转换成角度为30度
    / p2 ?7 t. {2 Q7 j
  11. >>> math.cos(math.pi/6)4 @% h% m& k! F9 L# B# Q( Y" L4 ?
  12. 0.8660254037844387
复制代码
6 H* S' I' |+ b3 E) a4 G
math.tan(x)  返回x(x为弧度)的正切值) Y8 w/ L3 a. ~! o' z2 f: y
  1. #返回x(x为弧度)的正切值
    3 M9 W* J, n: N
  2. tan(x)
    9 @8 E- p- `# E2 N1 S; j
  3. Return the tangent of x (measured in radians).4 P* F  K- @: b9 q0 u8 N
  4. >>> math.tan(math.pi/4)3 v9 k" e9 Q; p$ w( O
  5. 0.9999999999999999. o) |( j* H' u2 d( O
  6. >>> math.tan(math.pi/6)! v* q: J  R* g9 z* K# m* M; B
  7. 0.5773502691896257
    ! k# i1 t7 ^! u; U1 `" [" W, H/ j$ m
  8. >>> math.tan(math.pi/3)
    ) t8 I: [; Q2 X& c* z
  9. 1.7320508075688767
复制代码

: V& W% J2 C- H: _& W5 G; imath.degrees(x)  把x从弧度转换成角度0 }2 A% H- H  P8 U% t4 T
  1. #把x从弧度转换成角度# X+ m1 @: K* o5 |( ?( U
  2. degrees(x)8 C. e, ]" k+ g# e% V( s
  3. Convert angle x from radians to degrees.
    1 ^+ j5 k( @% Y4 ]$ Z( P
  4. 2 A* g; G1 q. P- d2 P1 _2 P
  5. >>> math.degrees(math.pi/4)
    - O+ f, t$ f8 i4 W5 S. I
  6. 45.0
    # U- o  x  S! z% D1 [
  7. >>> math.degrees(math.pi)
    5 @+ b4 C9 v. b2 [% j
  8. 180.0
    * \1 ^% O3 v0 `- ^
  9. >>> math.degrees(math.pi/6)
    # M! P5 f' ^3 N* h' l
  10. 29.999999999999996, H) o2 u# O1 h) d
  11. >>> math.degrees(math.pi/3)
    ( R" ~# P; K, O" I# j
  12. 59.99999999999999
复制代码
+ U$ Q3 l3 p" Y* Z) H
math.radians(x)  把角度x转换成弧度: i; \# W. i/ {
  1. #把角度x转换成弧度
    / y' H4 l! {7 r( @/ p: Z
  2. radians(x)) z% G3 I5 J" F7 Z6 F
  3. Convert angle x from degrees to radians." B0 w' c( Y% ^+ V% q
  4. >>> math.radians(45)
    ; D5 Z9 ]6 s4 j& ~0 P' j
  5. 0.7853981633974483
    ) ?% h7 i( a' N# U
  6. >>> math.radians(60)
    * J4 e+ B! O: ^7 R& L
  7. 1.0471975511965976
复制代码

% ~* [7 \/ O! O7 ^) Xmath.copysign(x,y)  把y的正负号加到x前面,可以使用0% r9 T% A# k) u
  1. #把y的正负号加到x前面,可以使用0
    7 R, O2 G( J4 x+ }
  2. copysign(x, y): o! a3 q( b7 k1 A  J! T* M( A
  3. Return a float with the magnitude (absolute value) of x but the sign
    & x9 _" ^# S$ q  T
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) # ~9 z9 |6 ]8 u. v
  5. returns -1.0.
    % }; b: L# R7 y
  6. ( |0 r$ R; w& w! `) r0 I' M
  7. >>> math.copysign(2,3)
    3 [0 I% N. m$ B( j0 l) L% ~
  8. 2.0
    5 Y1 w4 L1 E, f2 m/ D7 d( f# a
  9. >>> math.copysign(2,-3)8 F+ T& K5 s2 C+ t, j$ V
  10. -2.0
      G7 d+ v, Q. L& \* P; o
  11. >>> math.copysign(3,8)3 b; }+ W6 _4 _3 s* H
  12. 3.0
    * K! P9 [( ?' t$ I
  13. >>> math.copysign(3,-8)
    , N: `8 J- d$ \! w0 N+ i- e
  14. -3.0
复制代码

' a  K" R! `0 vmath.exp(x)  返回math.e,也就是2.71828的x次方
8 {8 L7 `" t: k+ F# p
  1. #返回math.e,也就是2.71828的x次方# |- n+ x+ y$ g3 b
  2. exp(x)6 }$ C  I# Q  Z5 W1 t8 s2 w
  3. Return e raised to the power of x.
    % q0 g) b  L/ k2 [! b
  4. 4 f( W1 j3 f3 d2 U0 @$ [/ q
  5. >>> math.exp(1)
    / [; }' I% R# C' s0 M9 h$ F' @; w
  6. 2.7182818284590451 D5 i: S/ ~  d" D9 k
  7. >>> math.exp(2)
    1 H8 b& ?' s& Y) g5 E9 Z: n
  8. 7.38905609893065% ^. h# |2 O- x* g. P; L( V2 X: }
  9. >>> math.exp(3)
    2 ], F* Z( |7 Z( h; p4 v5 c
  10. 20.085536923187668
复制代码

- i: q% c7 W+ N+ i, Smath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1) y  e$ G$ k! K! N8 ]# n
  1. #返回math.e的x(其值为2.71828)次方的值减1
    - a" M/ s* G7 t; I2 H; H
  2. expm1(x)
    3 I2 [" F. H8 Q  \
  3. Return exp(x)-1.( o3 u; M* m' p
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 g4 x  E% \: @6 a3 @
  5. $ l3 s! Z; V9 o# O' ?/ v$ F
  6. >>> math.expm1(1)' Z. q, @; Z3 o2 K% s0 C, w+ o, C4 ?7 E
  7. 1.718281828459045
    2 ~# N/ _+ G1 z2 M; u$ ~
  8. >>> math.expm1(2)5 ^& z5 a: a6 O  T" h9 x) U
  9. 6.38905609893065
      h% z" p' q/ P* y  e( S
  10. >>> math.expm1(3)8 Y' r5 E* _6 C
  11. 19.085536923187668
复制代码
; T' i* A" ]; N
math.fabs(x)  返回x的绝对值0 a% c8 B. i; Q
  1. #返回x的绝对值
    4 p/ p9 v' b3 e" Z1 `; [5 @( w
  2. fabs(x)% B7 x2 J  g7 E+ i& J
  3. Return the absolute value of the float x.2 |% d( B9 {% g4 _8 Q# `  P. n
  4. 2 Y) w# f; V, x* I
  5. >>> math.fabs(-0.003)6 p; C! |8 U# |! a
  6. 0.003
    9 C8 y. ~1 D8 A- w6 ?
  7. >>> math.fabs(-110)  J* Y$ S4 z4 H8 ~
  8. 110.0  w* r* _4 u0 I6 R  {
  9. >>> math.fabs(100)
    , c4 d( R$ Y  L( h8 \! ]4 g8 D
  10. 100.0
复制代码

& q8 k6 \6 N, L+ o' \/ N' B0 hmath.factorial(x)  取x的阶乘的值1 S1 f% O) D. q! Y
  1. #取x的阶乘的值; j5 p* D$ X( h) l- x7 d& |! v' K
  2. factorial(x) -> Integral
    - A# s1 H. G: d8 ]' Z. @
  3. Find x!. Raise a ValueError if x is negative or non-integral./ l: S# A5 d4 J/ x
  4. >>> math.factorial(1)& d2 t+ A& z8 f! \$ w
  5. 1% [8 m2 O% z! q2 [  O' Y  _- @, A
  6. >>> math.factorial(2)
    % W" m$ {4 w; L1 x
  7. 2
    8 z$ Z) Q( z  M+ r6 e) y6 T
  8. >>> math.factorial(3)
    # r0 t% ^( M6 f  p/ v
  9. 6
    8 ]4 f/ W4 B- ~1 d0 F5 Y7 a( R$ @
  10. >>> math.factorial(5)
    & x5 H' z, b9 w- @4 A
  11. 1200 U! P4 O5 I* \
  12. >>> math.factorial(10)
    : d( H( {. ]4 q7 Q' ~' O+ a
  13. 3628800
复制代码
0 b7 i& Q2 j- P
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
1 _$ x0 n( K/ V3 C" b; ~' e% A7 q7 H2 V
  1. #得到x/y的余数,其值是一个浮点数; n* J, n: y# b2 i
  2. fmod(x, y)' ^1 c. E2 t# Z# ?
  3. Return fmod(x, y), according to platform C.  x % y may differ." G7 v9 F( S' w0 p' ?
  4. >>> math.fmod(20,3)
    7 M/ s, x- L, e/ {! Y. n
  5. 2.0
    ' ]5 Y4 u  x5 b% F( h8 r  X
  6. >>> math.fmod(20,7)
    # U# j, c! K4 }& t. C
  7. 6.0
复制代码
$ z" v+ B* Y# j
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 `- Q; E# a: K3 k) c- c0 F1 R; {/ ~
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    - c* p) @) c  p6 c5 C$ `1 }
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    % l1 p, _$ i2 J7 K& b* J, P  ?
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ! A7 t: x# q' c
  4. frexp(x)  q+ l. k; k  W. |0 c8 y
  5. Return the mantissa and exponent of x, as pair (m, e).5 t: L1 [9 v8 Z
  6. m is a float and e is an int, such that x = m * 2.**e.
    # C' Y; c6 _; W/ b+ b* l
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    . F6 Z4 |# U' y3 K1 A# m
  8. >>> math.frexp(10)" m7 s( n" n: x1 @( I8 v* S
  9. (0.625, 4)
    7 r9 H! [8 x1 d0 w, w; a
  10. >>> math.frexp(75)
    , F: K8 K0 k. n  j. _1 h) B5 T
  11. (0.5859375, 7)
    $ M" H! E8 z0 P3 n0 Y9 c/ `2 ]# J' i" C5 {3 e
  12. >>> math.frexp(-40)0 e7 R& l' F& V3 @8 o
  13. (-0.625, 6)
    7 Z3 I% P; }" B8 o0 i, t
  14. >>> math.frexp(-100)1 b6 b7 O+ @0 |1 X, `
  15. (-0.78125, 7)* P' V  Z$ Y! C4 W" F
  16. >>> math.frexp(100)
    * y" |- p0 T4 U
  17. (0.78125, 7)
复制代码
- O3 Q' P" z& l
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 O/ g' Y; K. I/ s
  1. #对迭代器里的每个元素进行求和操作
    8 A1 g1 \8 H1 X" B3 q6 z
  2. fsum(iterable)
    - }+ J; h# X, H8 x) t- a" O$ }, o
  3. Return an accurate floating point sum of values in the iterable.3 |' J7 h  I2 R7 j
  4. Assumes IEEE-754 floating point arithmetic.2 B. K+ N) P# R" u6 [4 j$ ]
  5. >>> math.fsum([1,2,3,4])$ k$ p- n1 Z( K# `2 f) R
  6. 10.0
    # m0 ]! C8 @7 S- x5 [( V( `1 |6 j
  7. >>> math.fsum((1,2,3,4))5 J* ^' _, {; F  e* Y7 y+ ^/ O0 Y6 j
  8. 10.0
    7 J7 D% _" Z. f( ~  c; h! f) A: H) f
  9. >>> math.fsum((-1,-2,-3,-4))
    9 ?1 [  M' I' Q
  10. -10.0
    ) J+ t- h4 ]' @1 |
  11. >>> math.fsum([-1,-2,-3,-4])( b/ b& j0 y+ [7 _2 D
  12. -10.0
复制代码
2 a& Z/ L) y" G  S
math.gcd(x,y)  返回x和y的最大公约数
4 M" k$ a/ w9 y5 ~* i4 x" v
  1. #返回x和y的最大公约数
    & m5 _) c) Z8 k! Y
  2. gcd(x, y) -> int
    # D" N' O# _' K( y
  3. greatest common divisor of x and y
    , A- _- X. F& l) C
  4. >>> math.gcd(8,6)
    ! H, i# f# m$ E1 @5 K5 |
  5. 22 o. X. \6 R$ V! \4 O7 |
  6. >>> math.gcd(40,20)
    ! E) |1 ]1 k" Z; X% F. D
  7. 20( ~; T" p% \  ^
  8. >>> math.gcd(8,12)$ T2 i! G2 Q1 {5 h& F/ I
  9. 4
复制代码

5 H; I1 W# I2 @4 N" [5 zmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* y& R) c# l) Q8 Q/ u( i4 M. Z
  1. #得到(x**2+y**2),平方的值3 N. B6 q0 `8 E, I+ Q
  2. hypot(x, y)8 }  H* L+ N0 t9 ^3 F" O
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    . Y9 K4 I; Y/ c' u
  4. >>> math.hypot(3,4)  \& k4 r  G) h
  5. 5.0' S3 U0 @2 {8 _7 P2 x3 Z( G+ e; M1 y) i
  6. >>> math.hypot(6,8)( ~- E; ^( ]" ?
  7. 10.0
复制代码
" L2 y1 x5 I) S
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False3 {: K- s+ W3 C2 K
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    4 \5 A+ x# i% `9 Y( v& l
  2. isfinite(x) -> bool
      ]+ O; h6 O  s3 K# `. [' @: b
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.6 h' m* a! [$ {5 z/ E* h8 F; J" O+ a- K
  4. >>> math.isfinite(100)
    - |; I* l* w/ m. u
  5. True; N0 N2 q: ]& Q% G; j8 i3 R
  6. >>> math.isfinite(0)! |. Y6 y9 E, \: n# a+ ~
  7. True
    % n, P5 D- j9 @! j
  8. >>> math.isfinite(0.1)! j' J% h) f& O
  9. True
    ) M7 y7 u, `4 u! c% I  j4 n4 F* O  n
  10. >>> math.isfinite("a")! R2 Y: m# H% K& z0 |
  11. >>> math.isfinite(0.0001)( u6 i$ n/ z& y. c8 S: y
  12. True
复制代码
% @3 ]$ l4 S1 J+ e& H. z% b1 |
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, x1 U1 ?0 L3 @5 C+ R6 X
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False9 a$ |) X( {+ u. i
  2. isinf(x) -> bool" Q" y5 d4 y7 _, h- J
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ) D2 f! S0 z" Y3 Z7 K$ v
  4. >>> math.isinf(234)
    ; `5 J% t7 X+ {4 {0 W
  5. False: R- J8 r0 Y$ W% z
  6. >>> math.isinf(0.1)8 s. e+ ^! m2 x0 y5 y7 y1 C3 @
  7. False
复制代码
& ]% S+ B  i6 }2 d9 H7 H6 a2 I9 k- l
math.isnan(x)  如果x不是数字True,否则返回False
" T  t" z. u# ^3 E3 O
  1. #如果x不是数字True,否则返回False: U( B+ M& }7 t+ B
  2. isnan(x) -> bool
    $ z# ^9 q% E' G2 N/ G4 I( \
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 T  v: q/ Q; h) _( k4 B* y# I$ X+ u
  4. >>> math.isnan(23)
    1 [1 C+ ~% C7 P% _- V+ T
  5. False
    . ?) E# s! _5 U9 p. I% t5 U
  6. >>> math.isnan(0.01)" o8 A9 o3 D+ d& O! _
  7. False
复制代码
( p+ R8 g0 Y( `  S8 _
math.ldexp(x,i)  返回x*(2**i)的值# `* b: d* B  N8 z+ O
  1. #返回x*(2**i)的值! f2 c" p2 \* t' F/ ~1 W1 K
  2. ldexp(x, i). T( @( Y4 e5 r
  3. Return x * (2**i).
    1 g  q1 a. u0 P( ~  @0 O
  4. >>> math.ldexp(5,5)7 ]8 t9 P2 n2 r, D# o% O& ~$ P
  5. 160.01 x% r1 b/ q; y4 w0 r, G) ?
  6. >>> math.ldexp(3,5)
    6 a; L8 }* K0 s: F
  7. 96.0
复制代码

. t& |2 L! u, e- w1 Y: n2 D, qmath.log10(x)  返回x的以10为底的对数* }& G5 |8 C8 o* c" ^$ X* ?
  1. #返回x的以10为底的对数
    0 ~9 Q( }  O- _5 B( Y  _$ B4 T
  2. log10(x)$ ~2 Q' S) f6 T9 D  h2 |
  3. Return the base 10 logarithm of x.: R0 G8 q( W' ?$ i
  4. >>> math.log10(10)1 p* G! J* U. J! p
  5. 1.0- [; `; R( h* k
  6. >>> math.log10(100)
    8 \. t4 w4 n" E2 ~6 a1 r/ U
  7. 2.0' J6 {; b  d( W/ I5 j
  8. #即10的1.3次方的结果为207 I7 ~4 a8 Y! |9 Y& [8 f1 S% N! |
  9. >>> math.log10(20)
    2 i5 Q& m8 F7 x3 b! O
  10. 1.3010299956639813
复制代码

' [$ G* P5 H2 ?; y1 Z* ~math.log1p(x)  返回x+1的自然对数(基数为e)的值
8 Z% x5 S2 l! X5 c1 d" S! o
  1. #返回x+1的自然对数(基数为e)的值! L* w& A3 f6 H, ]9 H& p  y' E
  2. log1p(x)
    & j, P/ C, Y# W8 y
  3. Return the natural logarithm of 1+x (base e).
    & i* ~8 i/ k8 O' P% u1 z7 f, C" R
  4. The result is computed in a way which is accurate for x near zero.
    , n9 G; J9 J! }# ]; e
  5. >>> math.log(10)# _& ^7 a2 w- O; D( a
  6. 2.302585092994046
    ) J6 T  N4 L0 z4 y9 W( [, M- D
  7. >>> math.log1p(10). g$ e/ q' ^, {! r. {- s: A5 f
  8. 2.3978952727983707
    8 p4 s3 Y9 M; F
  9. >>> math.log(11)- y1 ^0 j0 z" S# e; S
  10. 2.3978952727983707
复制代码
4 h3 _2 g9 s8 v# p& i
math.log2(x)  返回x的基2对数  u, C. |! f4 @3 ~1 X4 _/ q
  1. #返回x的基2对数
    6 k9 a$ j5 l5 z" A! q9 h9 E
  2. log2(x)
    % s/ \9 b) D6 T0 o+ i1 }
  3. Return the base 2 logarithm of x.0 X& k3 Q6 z! i$ H4 I4 P- M2 p% o- w0 F
  4. >>> math.log2(32)6 \  b5 [& d9 h' j6 q) V
  5. 5.0
    , }! n7 H0 v: Q
  6. >>> math.log2(20)
    ( s+ f" P% k" G: u
  7. 4.321928094887363
    - e- B" X1 n7 @, [* U
  8. >>> math.log2(16)2 u8 `- N6 U$ ]
  9. 4.0
复制代码

0 d, h9 |5 U& M9 Vmath.modf(x)  返回由x的小数部分和整数部分组成的元组9 I. E2 F% D1 C1 o( q- S3 y1 Q# n5 B
  1. #返回由x的小数部分和整数部分组成的元组
    & V0 t% }, Z3 x3 M5 u
  2. modf(x)' z7 u# j/ m0 P! M! T1 n3 Z% }
  3. Return the fractional and integer parts of x.  Both results carry the sign0 @1 J- N, |" o  I% k& [
  4. of x and are floats.0 U& b+ K3 |! t3 V- L& S" f( O
  5. >>> math.modf(math.pi)
    / Z8 H5 s3 n6 @) M+ b  M  M* l
  6. (0.14159265358979312, 3.0)
    9 U, C" I2 Q& L( X
  7. >>> math.modf(12.34)3 T4 ^# N; Y& O( |0 a
  8. (0.33999999999999986, 12.0)
复制代码

6 H# n5 X3 w8 z! S/ ymath.sqrt(x)  求x的平方根
# N8 z. ~5 r! _4 b  ?1 @
  1. #求x的平方根7 n. d6 e4 h. ]) n' D+ {% F
  2. sqrt(x)
    $ _: O. F/ \6 [" }6 T7 s
  3. Return the square root of x.& ^  g: K: h! O, s
  4. >>> math.sqrt(100)* C4 A( N- J5 {+ ]6 Z/ t
  5. 10.0( L' K1 w% A2 q! O7 f
  6. >>> math.sqrt(16)+ C% Q7 Z$ x2 H, y! E  s2 }
  7. 4.0
    4 p# o( s* }1 s6 K% C$ J6 b  R
  8. >>> math.sqrt(20)1 c# N! y( ^! ^: Q( R& _# `
  9. 4.47213595499958
复制代码
% G2 i2 e: v7 `5 o6 D, J! @* g
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分3 a. E* B( m& \, K( h* l
  2. trunc(x:Real) -> Integral
    ' H& ]+ |8 o1 E" [0 ~2 c2 ]  j
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ C4 L! D* @& e' N
  4. >>> math.trunc(6.789), d2 @; F5 R, f$ |/ F& p. H$ N5 W
  5. 6
    % F5 N' `3 s8 e  x0 k: b
  6. >>> math.trunc(math.pi)4 T; ~! g: L. o: V; }( {1 h
  7. 3
    8 p/ |# f2 m, g; y% n
  8. >>> math.trunc(2.567)8 \5 W1 _( d/ e+ q6 G
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-4 13:59 , Processed in 0.086637 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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