新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
3 R; p7 J) t; g; e
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ N) G: S4 H6 C
& Q) ^5 \* h$ ]9 a/ p2 M
方法1) [: u9 Q) c. O; b: t# P' X
  1. >>> import math: L2 f% v- ~. a3 I* v& I
  2. >>> math.sqrt(9)9 I4 h' M$ S- A1 P& \7 q
  3. 3.0
复制代码
方法2
9 V2 N, C7 Z  w/ X! W# x0 {% C
  1. >>> from math import sqrt
    6 t5 Z7 T7 M8 P. N2 X% ?
  2. >>> sqrt(9)1 H7 p6 V7 z, B# ]2 e% U
  3. 3.0
复制代码
$ _+ e# y& w" S9 Q* z4 @

- n  k, R2 V- Q& ?- g% L5 A! R8 a! v
math.e  表示一个常量! J1 t# K+ V9 A$ ~: A) {
  1. #表示一个常量
    : e# ]2 A+ [7 p
  2. >>> math.e
    ; j3 \' d2 |+ p# I8 \1 F* `+ V" q  {
  3. 2.718281828459045
复制代码

5 A( V' h$ Y- h: Wmath.pi  
数字常量,圆周率
$ L$ j6 z( R# F0 ~1 {
  1. #数字常量,圆周率
    6 u! ^" C) b3 S- s
  2. >>> print(math.pi)
    % g/ o8 M" \+ n& M2 t. Y
  3. 3.141592653589793
复制代码
, c5 s+ _' h; g# {+ w( r# S
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
+ B$ K5 {6 r# h. Y  n* f4 g6 l
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x. O2 l; a& ]3 C4 g7 z5 f: I. y
  2. ceil(x)6 X/ A. @7 f: b! A9 f( d) F
  3. Return the ceiling of x as an int.
    5 H, ?" v# R& D  {. U3 l. _" c
  4. This is the smallest integral value >= x.7 n* g) E: H" J$ O3 e! Y
  5.   Q* ]. V8 b* d
  6. >>> math.ceil(4.01)1 c% t( o' y3 x5 e4 u: Q
  7. 58 k5 A% M) e; _# e* ^5 o6 w
  8. >>> math.ceil(4.99)
    9 s/ n) A: c) s: ~' v! p
  9. 5
    2 y% o  F% G4 C
  10. >>> math.ceil(-3.99): N5 w- e, C9 M4 B
  11. -3
    5 G7 F) j6 ?2 O7 X. W: u
  12. >>> math.ceil(-3.01)
    / B8 \& |3 n4 d! A, z7 |
  13. -3
复制代码
5 W- z  s' l7 j$ B( a$ K7 E& n
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 V% _1 M4 |7 E1 j# q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    8 s" p2 G4 ]: u5 J9 d( G
  2. floor(x)
    0 S2 J' W" f7 a  t; t3 p
  3. Return the floor of x as an int.7 ]* W5 b& p$ v$ z  f$ [" r
  4. This is the largest integral value <= x.6 f2 m  B- B; ?4 g. z
  5. >>> math.floor(4.1)/ [% \8 q; G; T
  6. 43 r  r$ @0 [2 G" o; e# A0 X# c
  7. >>> math.floor(4.999)
    / f' ]% Q/ ?/ R$ P( C# l! K1 N
  8. 4
    ! C/ K- m! p3 n% k
  9. >>> math.floor(-4.999)
    , i$ T& d% v0 b) k. V
  10. -5  }) ~8 M! e4 z& m7 z0 M. \
  11. >>> math.floor(-4.01)
    5 U; v) l( i3 ]7 e0 I! r: X* K
  12. -5
复制代码

( h* o; X1 ~  s7 q, b# s! Emath.pow(x,y)  返回x的y次方,即x**y9 n8 B% b) e% }& m. z2 q
  1. #返回x的y次方,即x**y7 x5 V7 Q0 m2 L5 w" z
  2. pow(x, y)9 B9 ^) ?$ m7 e% K- `
  3. Return x**y (x to the power of y).
    , ~6 k7 q9 v: p7 u
  4. >>> math.pow(3,4)
    & f. u) b7 j$ m, J
  5. 81.06 y8 x3 s& |& I! x. `) Q
  6. >>>
    # u4 _8 r' v6 B) @
  7. >>> math.pow(2,7)
    $ y  G! Y% z7 W
  8. 128.0
复制代码

( ^) |: h$ N4 C" m- L: z8 wmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 V0 J* _6 `1 U) G6 w
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ( b% |  N2 i, t; c4 j( K
  2. log(x[, base])
    # e8 f, o& t/ \% b5 X9 V: e8 A. p
  3. Return the logarithm of x to the given base.
    / [4 H9 Z0 r! u1 `3 u5 D
  4. If the base not specified, returns the natural logarithm (base e) of x.% S4 [& R+ B9 h2 E' [' `: o7 z: p
  5. >>> math.log(10)- x; h# w) A; X: Q3 L
  6. 2.302585092994046
    5 h$ i" U- X* s# v
  7. >>> math.log(11)2 {3 q# j1 N2 M2 N8 ~. B
  8. 2.39789527279837072 a% R7 _. _$ i# L
  9. >>> math.log(20)$ B( t; Z7 h, Y- a) Q7 \. F' m
  10. 2.995732273553991
复制代码

8 P0 x$ I9 M0 c. pmath.sin(x)  求x(x为弧度)的正弦值
8 C. V. R$ m& `) r/ b. t7 j* R
  1. #求x(x为弧度)的正弦值
    + I! ~( h1 A# S* i% X4 U
  2. sin(x)9 T1 n" [, b4 I2 r! V
  3. Return the sine of x (measured in radians).
    8 g% H$ n  s* V$ k
  4. >>> math.sin(math.pi/4)
    - J, L$ \9 x5 s- r4 g5 w
  5. 0.7071067811865475  h, o) I$ Y+ Y) _
  6. >>> math.sin(math.pi/2)
    ) R$ b8 t2 p5 L# q
  7. 1.0/ w" F& n8 @/ I5 t. I2 |$ r
  8. >>> math.sin(math.pi/3)- _7 V' l- |) P( ^1 c! a1 y
  9. 0.8660254037844386
复制代码

  o5 o; w7 ?) o* r# h9 Y- I1 Vmath.cos(x)  求x的余弦,x必须是弧度
' F8 ~- n: ~3 h8 d  y  b
  1. #求x的余弦,x必须是弧度
    . }2 r; J) s0 j$ \  |5 k; C
  2. cos(x)# j, K$ P, b3 J) Q* z+ w
  3. Return the cosine of x (measured in radians).9 _4 I2 l5 |! H
  4. #math.pi/4表示弧度,转换成角度为45度
    6 Q! i4 X$ R9 ?; b( T& C
  5. >>> math.cos(math.pi/4)7 y' _0 h& a, g4 ?# o$ |5 t- y
  6. 0.7071067811865476
    ( w4 m# |6 ~$ @2 d) I5 H4 w1 i" Z' H' x7 t
  7. math.pi/3表示弧度,转换成角度为60度4 f8 p& y/ W5 A4 a6 {4 k
  8. >>> math.cos(math.pi/3)5 y+ [& X: q, V( x0 ]4 R9 x
  9. 0.5000000000000001/ ~+ I. V, l5 O3 s+ r
  10. math.pi/6表示弧度,转换成角度为30度
    3 k4 R; D- x- y6 J  z, k; A
  11. >>> math.cos(math.pi/6)$ _2 `/ V! o' u1 N
  12. 0.8660254037844387
复制代码
- S4 p8 |% S4 O0 @8 V3 {/ N- l
math.tan(x)  返回x(x为弧度)的正切值
7 U7 B: _$ `" H# B# b0 l6 v( B
  1. #返回x(x为弧度)的正切值
    1 ~  b  U8 N  t& T# i* J0 j* X
  2. tan(x)
    ( m) O& a( S/ ^4 z1 |$ O3 h
  3. Return the tangent of x (measured in radians).9 x: e% w/ y4 X% [/ k1 {, @
  4. >>> math.tan(math.pi/4)5 r& W  L, l( D1 @8 d
  5. 0.9999999999999999
    . _- \8 _: Y4 z" t9 {, ^7 q8 t
  6. >>> math.tan(math.pi/6)) t4 U/ E6 `2 H+ C/ K
  7. 0.5773502691896257
      @5 C3 F9 o- f. y0 T, w  N- d2 t
  8. >>> math.tan(math.pi/3)
    3 ^$ {* R' l6 P* {$ k( {
  9. 1.7320508075688767
复制代码
: f, Y! |& R1 }0 U0 J% Q# V( K
math.degrees(x)  把x从弧度转换成角度
1 I9 s' ^& s# {8 `: b- Q
  1. #把x从弧度转换成角度
    & X2 s0 x5 A' W1 @# w, j; M
  2. degrees(x)6 f: r* T8 M2 `% W8 A
  3. Convert angle x from radians to degrees.0 m# L: Q+ _$ b

  4. ( @' S& B! H. n) K. z. q" S
  5. >>> math.degrees(math.pi/4)# ?( d. s  {7 M6 D; t
  6. 45.0, c$ R3 m- b- e+ r9 J1 T
  7. >>> math.degrees(math.pi)
    ' X" N. ]% y% S0 @6 ~5 M! D
  8. 180.07 e/ h% P: J+ |3 _, J
  9. >>> math.degrees(math.pi/6)2 P4 z. N! z9 d+ ^2 {" d
  10. 29.999999999999996
    $ H% e0 S: H% B9 ~- `
  11. >>> math.degrees(math.pi/3)1 B3 p( K- }, ~6 d+ H
  12. 59.99999999999999
复制代码

* `% a, z5 i4 r" omath.radians(x)  把角度x转换成弧度
; L# e1 K' N$ R" o1 u  |% }
  1. #把角度x转换成弧度
    . }" X# ]8 q/ w( o' s, ]
  2. radians(x)
    : q. D6 |! Y/ x' x2 z
  3. Convert angle x from degrees to radians.  P; I) ^( M3 @5 A  y5 c3 x6 }# B
  4. >>> math.radians(45)1 l9 M8 w/ T* b6 @- g* N
  5. 0.7853981633974483
    1 C' _  O* H  C
  6. >>> math.radians(60)9 c: M' l1 m+ p( W- f- [
  7. 1.0471975511965976
复制代码

' X8 T4 N9 a3 Nmath.copysign(x,y)  把y的正负号加到x前面,可以使用0, J. F, b7 k2 R* C
  1. #把y的正负号加到x前面,可以使用0
    ; N6 w2 i9 g+ E% c. H
  2. copysign(x, y)
    & w9 `5 x' r+ ?6 I: _' m8 c
  3. Return a float with the magnitude (absolute value) of x but the sign 5 D2 C' t# I, b# w# p
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    3 `6 p* [% J4 c% M
  5. returns -1.0.
    9 \' v3 g6 d% R- ]: u

  6. 3 v$ C7 Q! o2 b
  7. >>> math.copysign(2,3)
    9 j! N9 w: b; x
  8. 2.0
    5 O! G6 m% I$ j' m; G& j
  9. >>> math.copysign(2,-3)1 I- K- q: l) i/ _  F
  10. -2.0
    7 u: T) k" R4 t- l; @3 \* j/ R" a
  11. >>> math.copysign(3,8)" |) ^1 L4 C4 J
  12. 3.0
    , d" p  o0 i6 n. y1 a' a. s
  13. >>> math.copysign(3,-8), @- F- {4 o% k& Q/ ?- @: R% ]& @
  14. -3.0
复制代码
: W1 c' K( R& F: t. P( w
math.exp(x)  返回math.e,也就是2.71828的x次方; o1 T2 G, v  f+ y: o
  1. #返回math.e,也就是2.71828的x次方
    ! c$ |6 e. n& i( u* \6 B; i/ v
  2. exp(x)
    * I2 [3 h/ m. t1 j: p5 [
  3. Return e raised to the power of x.
    4 p% l" X9 F1 G4 K* Q5 ~& Q
  4. ( L3 M9 a4 v2 t3 s
  5. >>> math.exp(1)5 E- w; c4 C1 g" F# @$ U7 ~
  6. 2.718281828459045
    9 D5 K1 w; n1 P) l2 d* ~8 b
  7. >>> math.exp(2). ^, [7 g6 q' M1 v
  8. 7.38905609893065
    . u3 u5 U, \0 Y" C
  9. >>> math.exp(3)7 c! Q+ u% d9 I  G
  10. 20.085536923187668
复制代码
6 |: Z3 Q1 A& a& _6 G4 y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( ^+ N, n2 k" `+ e# G+ F
  1. #返回math.e的x(其值为2.71828)次方的值减1
    " b; B+ _$ _8 m# h7 [# J
  2. expm1(x)& M9 b* N' m5 C9 s% T  k5 w% u: h
  3. Return exp(x)-1.) }0 I0 b" U* L: l2 \" g! _& z7 w7 C  `
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 S8 H2 Q2 l4 B
  5. 1 R$ L, P! t( P
  6. >>> math.expm1(1)4 e7 y# R" D& {* P5 ^( L
  7. 1.718281828459045
    * a, f6 t2 P- p# s% L
  8. >>> math.expm1(2)
    * N# _: q( m- w% X) D6 Y# U1 k
  9. 6.38905609893065
    5 M/ o0 C1 l# [% o
  10. >>> math.expm1(3)
    ! d$ {: @5 X" `* |  J  Y" P* ~5 d
  11. 19.085536923187668
复制代码
( P1 i; K! |- y3 I) _" w  \1 \/ I
math.fabs(x)  返回x的绝对值
/ f9 j. r' S* h4 c  b: [% y
  1. #返回x的绝对值$ P5 q4 V& J/ y! C0 `% W+ M7 e
  2. fabs(x)
    ( C7 X. @2 \# H; a5 x* f
  3. Return the absolute value of the float x.+ h' Z  [0 t! L4 Z1 v0 D
  4. + s6 m( k$ n. T; W6 H* e* I1 X
  5. >>> math.fabs(-0.003)
    0 ]. z0 H$ n1 }+ d3 Q- d2 Z2 X
  6. 0.003
    3 x5 `( k& V& M/ I7 U# p* [
  7. >>> math.fabs(-110)
    * E+ ~$ D! W9 }( G4 e* V. M4 j7 C
  8. 110.0
    - Z+ |8 B) S# h  s3 B3 D* P! W
  9. >>> math.fabs(100)
    " L0 b1 Y7 s- y7 B% R! k. |' ^
  10. 100.0
复制代码

# a) ]6 A  F3 X  ?math.factorial(x)  取x的阶乘的值
; l9 R) v) c2 K; V' X6 [
  1. #取x的阶乘的值5 n+ |9 F; _) S8 |
  2. factorial(x) -> Integral4 F5 z; u" O8 q) O! r
  3. Find x!. Raise a ValueError if x is negative or non-integral.% E' |3 @% \4 v) W, ~" V
  4. >>> math.factorial(1)" c0 S! |2 a- X: s
  5. 1
    ( _" F! ^# |4 `% p0 [: w
  6. >>> math.factorial(2)! @1 m0 D) A& n+ t' z
  7. 22 ?9 w; \% s/ c3 O& i$ A$ d$ t& E
  8. >>> math.factorial(3)9 ?% ^& t( Y8 e% @1 f6 i. a
  9. 6
    6 M2 A0 @' a6 J0 V3 h4 @
  10. >>> math.factorial(5)
    / O2 W% ~, C) `& M" J. g$ y
  11. 120
    2 K7 {- Z/ t- c1 F+ T
  12. >>> math.factorial(10)
    ( q: K" y1 t2 p% t7 P
  13. 3628800
复制代码
; I0 M7 @5 m& i3 x. k
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数- z6 p- y1 B' `- r# U
  1. #得到x/y的余数,其值是一个浮点数
    1 O4 o' a" S  h7 z, [
  2. fmod(x, y)
    1 j7 c" T, }9 ~9 y! Q
  3. Return fmod(x, y), according to platform C.  x % y may differ.! Y% Y* ~' o6 P8 N1 D! Z
  4. >>> math.fmod(20,3)
    4 B$ W0 o4 ^! t% r  l. q
  5. 2.0) T5 Q' U% J2 G# M  t" a4 B
  6. >>> math.fmod(20,7)) N# V( N' M% u) v
  7. 6.0
复制代码
# c" P( _3 ]3 A7 x9 C7 p
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
( W3 R3 s- g/ u
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 N) p5 R8 }1 @$ ^
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 i8 H9 s; }( I
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( k4 {* [, C0 J5 [3 _
  4. frexp(x)
    . |9 a3 M( h" @- P
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 ~- k& ]) V9 Y4 O6 v
  6. m is a float and e is an int, such that x = m * 2.**e.
    ) a0 S  u# ^9 M6 l2 }4 }. K' r
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.) Y4 `+ [4 t5 i; L
  8. >>> math.frexp(10)# B7 `6 Y  D6 F
  9. (0.625, 4)2 c, e" h1 h1 @$ R+ N3 G
  10. >>> math.frexp(75)) p% G( d) ]" e' G. C4 e
  11. (0.5859375, 7)2 q# ~! J5 D9 K: }3 c' ?& e8 ]% H, f
  12. >>> math.frexp(-40)
    # ~4 F3 U% Z0 M4 m
  13. (-0.625, 6)
    ) k8 C9 {/ V* A( S
  14. >>> math.frexp(-100)
    % U  B/ f5 T% p% y
  15. (-0.78125, 7)4 K4 V* m5 T  w  s8 V$ U
  16. >>> math.frexp(100)
    0 p* e. o- A: {4 B0 `+ \7 Z
  17. (0.78125, 7)
复制代码

6 i/ s3 |3 Z6 E8 X+ d9 Imath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
9 B8 N) u( Y; K; E( _
  1. #对迭代器里的每个元素进行求和操作2 m, }8 @! t% g! O+ {
  2. fsum(iterable): p1 \  P9 f# G  F. B. e$ O) Q
  3. Return an accurate floating point sum of values in the iterable.6 ]+ F7 _5 n- t# a% f! }! F
  4. Assumes IEEE-754 floating point arithmetic.
    - Y9 l' F0 X7 c8 ^
  5. >>> math.fsum([1,2,3,4])
    - c6 @+ B( g! j# K
  6. 10.0
    + b$ K5 ?8 p2 ?% d% A7 R
  7. >>> math.fsum((1,2,3,4))
    ) T% r# e1 [, p* E# s+ m/ ]( A
  8. 10.08 O, v/ G' y/ o
  9. >>> math.fsum((-1,-2,-3,-4))( Q8 }7 X1 r( }' }5 t, t
  10. -10.0
    $ ]/ h* f% `5 d2 ~* M7 w
  11. >>> math.fsum([-1,-2,-3,-4])
    9 A) k( ?  e  d7 F9 `7 u- v
  12. -10.0
复制代码
8 p' q  f' B' x7 I- m3 r. B
math.gcd(x,y)  返回x和y的最大公约数0 q( S2 G7 i  @
  1. #返回x和y的最大公约数. K, p( _7 I" o7 S1 J
  2. gcd(x, y) -> int
    * D+ g" u) _7 O5 Y
  3. greatest common divisor of x and y
    & ~) r5 M' N, G& T
  4. >>> math.gcd(8,6)6 \# E) ?9 s; {3 ?, k4 G( j
  5. 2* [9 I' I; I9 ]% R7 o7 c
  6. >>> math.gcd(40,20)3 d: ^" ~- D0 }0 p
  7. 20
    ) D' K2 B/ g* X0 P& \3 c, w# ^
  8. >>> math.gcd(8,12)2 J5 J$ f) J4 Y3 @  C
  9. 4
复制代码

- j) j7 x0 b3 K, T% e$ Hmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
1 E6 r8 o% V7 S, _  E: \$ W
  1. #得到(x**2+y**2),平方的值
    - O+ V; \7 }7 Z  q$ \/ Y1 ?1 r
  2. hypot(x, y)
    7 w# A, K) B: M2 n& \0 `
  3. Return the Euclidean distance, sqrt(x*x + y*y).& Q$ O' d1 `  c% G3 K
  4. >>> math.hypot(3,4)! f& i7 s) `) r1 x9 `
  5. 5.0* I7 V8 W, E% V! s: d+ ~' c
  6. >>> math.hypot(6,8)
    + m4 a; J+ |5 X
  7. 10.0
复制代码

5 }2 n, j6 v- B7 J- i% `math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
! W8 Y$ W4 g% ~3 I, s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % }# q1 _' p, }4 U/ X
  2. isfinite(x) -> bool
    ) _" w, X5 _! \. F# n
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! ?+ g2 e: A- L# I) y9 v
  4. >>> math.isfinite(100)
    ( J' l8 x0 b0 s; [& k3 A- r( p5 P* ~
  5. True
    ( }; j* h# `" E/ B
  6. >>> math.isfinite(0)) f. M% J) w/ o# j- [
  7. True3 T- Y- W4 j+ k3 H& @4 |1 S4 A8 ^
  8. >>> math.isfinite(0.1). v: z% N7 Q1 F0 X( o
  9. True; k2 q2 M3 J9 t6 S5 H
  10. >>> math.isfinite("a")  _6 T6 b& j7 k9 ]2 T4 f* S
  11. >>> math.isfinite(0.0001)5 d) _7 `( `, Z$ q2 M; d, D. E' {  W
  12. True
复制代码
5 s: z1 I1 w4 A+ l
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False) O* O# O3 \' y. N
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ! r5 C- Y! z  {- @" F& w
  2. isinf(x) -> bool
    ) Q  O! T- {: \
  3. Return True if x is a positive or negative infinity, and False otherwise.
    0 l+ m, E" X- j# D8 O& ?
  4. >>> math.isinf(234), F! l! U+ `) C4 _+ F
  5. False
    0 l5 s3 c! g: l) c* S/ }
  6. >>> math.isinf(0.1)
    + {* A$ X+ ]* y
  7. False
复制代码
9 I3 j" h0 }6 v
math.isnan(x)  如果x不是数字True,否则返回False4 e' [# y5 l. }. z! p; ]
  1. #如果x不是数字True,否则返回False- A/ ?- g  N4 H. o4 C' |! F
  2. isnan(x) -> bool( P0 `% c" j5 h" t% j
  3. Return True if x is a NaN (not a number), and False otherwise.& d: e4 z, ]' I. k: P. [
  4. >>> math.isnan(23)' g6 q: o3 h) g: S0 o1 J2 K' G
  5. False
    0 m5 ^5 Q. ~' d# y
  6. >>> math.isnan(0.01)
    + F6 n8 ~% Q/ F# d5 m5 Z
  7. False
复制代码
/ P& n# K" K3 G8 \+ m5 }. ^% ]
math.ldexp(x,i)  返回x*(2**i)的值
" w% f  D* Q: r+ [4 Z0 W
  1. #返回x*(2**i)的值. l" \5 o$ ]# A2 l5 S) @
  2. ldexp(x, i)' W8 K' i% V/ F. r3 \
  3. Return x * (2**i).
    / v+ V& J) a( n# Q1 o9 Y
  4. >>> math.ldexp(5,5)
    / ^' o: ]; E% X, [) B
  5. 160.0
    ( V5 V! p/ Y3 c6 I4 g
  6. >>> math.ldexp(3,5)9 b% n$ J8 O0 W% I
  7. 96.0
复制代码

6 y, i0 ^9 Q2 v, lmath.log10(x)  返回x的以10为底的对数) u" p' ]4 D# W2 _
  1. #返回x的以10为底的对数
    7 u6 g0 I2 t3 E/ E! q! x
  2. log10(x)
    ) M: F! _4 w5 [( y
  3. Return the base 10 logarithm of x.
    : H4 O) t. a% z$ k3 I- j
  4. >>> math.log10(10)
    6 A5 n' l2 |9 ~" Q. d
  5. 1.03 {/ d0 w; M$ T
  6. >>> math.log10(100)
    . C* _* s! z7 A6 r  b$ H
  7. 2.09 C* R, H; z; ~& ]
  8. #即10的1.3次方的结果为20
    7 S- N6 k: }+ L8 k) E) J8 V
  9. >>> math.log10(20)
    : Z" B' y$ r/ W# K
  10. 1.3010299956639813
复制代码

( n  l' I9 p: g% b2 p4 R- vmath.log1p(x)  返回x+1的自然对数(基数为e)的值" P1 e' ^. B- x- |" \; K: s; P; P/ I
  1. #返回x+1的自然对数(基数为e)的值* a3 u+ H' u- ?! [8 p# w' N
  2. log1p(x)+ \$ R5 d' d1 Z+ E4 S3 h, ^
  3. Return the natural logarithm of 1+x (base e).% y5 n1 \9 b" c' s: l# X
  4. The result is computed in a way which is accurate for x near zero.: y3 s6 O1 b/ t' M
  5. >>> math.log(10)
    * T2 i* C* W3 t3 s8 F
  6. 2.302585092994046
    " d0 ^; q7 t& B
  7. >>> math.log1p(10)8 q3 k- y7 w' M4 S
  8. 2.3978952727983707
    8 d5 p6 B8 x/ ~" G9 I
  9. >>> math.log(11)
    9 k, e) F& M. J& l# `2 a0 @
  10. 2.3978952727983707
复制代码
8 T* m4 F) k4 _1 c
math.log2(x)  返回x的基2对数
; [* ^6 k. H1 A( ^
  1. #返回x的基2对数$ l$ p; r, s# O- q2 d; m* l, E
  2. log2(x)
    $ b' e' j& n0 d1 @$ |
  3. Return the base 2 logarithm of x.
    - A3 L) T' J& z. |# k& Q  Y
  4. >>> math.log2(32)6 _( X( G' W5 c" z* m
  5. 5.0
    & b, R1 R7 w# y+ a1 V$ F
  6. >>> math.log2(20)7 t5 D3 \1 s; r3 E; T
  7. 4.321928094887363
    7 s% q* Q8 Z5 N7 c8 k$ ^
  8. >>> math.log2(16)
    5 K5 ]. N: x9 G( Y% B- ]( ?5 s
  9. 4.0
复制代码
. \/ _3 v8 f  k5 ]
math.modf(x)  返回由x的小数部分和整数部分组成的元组
$ W" o" W; X, g
  1. #返回由x的小数部分和整数部分组成的元组
    9 S3 V" c6 c! e  d- `( G1 V
  2. modf(x)" P5 U1 w# M% p0 E) v- w
  3. Return the fractional and integer parts of x.  Both results carry the sign
      m: j" _! O/ t$ Q0 m5 ?
  4. of x and are floats.
    # K9 `! H; x( N! U3 J; K
  5. >>> math.modf(math.pi)' j% Q) i( a" E4 s
  6. (0.14159265358979312, 3.0)
    * y, o9 l; L* A; S/ E9 \( w9 z! y
  7. >>> math.modf(12.34)- J! B& ~; J- f/ W; r: {
  8. (0.33999999999999986, 12.0)
复制代码
0 m$ }- G2 i5 Y  z2 t5 Z
math.sqrt(x)  求x的平方根
) c" f, @7 a6 H, k0 S' C8 O- d4 c; Z
  1. #求x的平方根
    4 x! |9 Z% l5 b* e* m
  2. sqrt(x)/ \! d2 ?8 i  _& V8 o/ e, L
  3. Return the square root of x.
    . l  x' m5 ]* J' o
  4. >>> math.sqrt(100). i/ K& P9 }4 E; [; I6 k2 G7 c
  5. 10.0
    . B" h* \$ a& s  v
  6. >>> math.sqrt(16)* w/ {8 c# y. ^( g# ]
  7. 4.0; n( {+ [# I2 I1 B& z
  8. >>> math.sqrt(20)( G) e6 n; E# H5 G8 _5 N8 ?8 N2 I
  9. 4.47213595499958
复制代码

1 h' g& @8 e+ v2 ]& T$ Y- t: Hmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ! k. ~" r* k$ L/ V* O7 a& I
  2. trunc(x:Real) -> Integral- u8 q' T- Q& Y" _
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# A* V& p) U+ G% T' u& P
  4. >>> math.trunc(6.789): h* x  h& W* H! j; h/ H
  5. 6
    . Q0 p* K, F2 S5 S
  6. >>> math.trunc(math.pi)! a% t" R, _3 w2 C
  7. 3
    : z! T6 g% G6 O5 x
  8. >>> math.trunc(2.567)
    ( O2 p* d1 I# g% |
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-25 07:22 , Processed in 0.081127 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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