新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

8 q, E# L. p, |3 Q( X, a+ A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. {6 B9 A' A" v1 G

1 @3 e" S/ Q$ S6 a; y) f1 J方法1
7 y' U' n4 O% |. N) ?5 a
  1. >>> import math
    ) L3 I: y" _6 N/ p
  2. >>> math.sqrt(9)/ e" ^2 l$ U' v- F8 h/ |1 ?+ a" K
  3. 3.0
复制代码
方法2' b  B+ p' m( U. H
  1. >>> from math import sqrt
    9 }9 S9 `8 U/ t
  2. >>> sqrt(9)
    ; w. e7 P+ s$ J; ?/ m
  3. 3.0
复制代码

+ z. j$ n9 E5 y) g7 O2 y- k

( D% f! P4 L! f2 t, i. E) e
math.e  表示一个常量2 W% k$ y0 w# v
  1. #表示一个常量
    5 s! a; z- x  [8 F& ~7 o% s1 r
  2. >>> math.e2 s- F+ S: b5 w# j0 u6 V/ ?
  3. 2.718281828459045
复制代码
7 L6 H- N$ Q9 t  t( L# J0 O2 j
math.pi  
数字常量,圆周率
! J# d4 [& k8 F5 z+ D  x7 U7 [6 }
  1. #数字常量,圆周率" M3 H8 X, d, A0 H$ Y" F6 ?' o
  2. >>> print(math.pi)
    ! X! A% _1 k: v
  3. 3.141592653589793
复制代码

0 e2 B% k1 u% B2 D* p3 R/ l( Ymath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
* j* m( _: l$ t# F/ v/ @5 F( `+ `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    $ K  I, s# b9 @! ?# z
  2. ceil(x)
    / @- ^; E% i% {: _7 k, Y( |( D
  3. Return the ceiling of x as an int.0 Y) B  Q, G) Q. o+ P4 ?
  4. This is the smallest integral value >= x.7 m" A7 _6 I; e# v6 G

  5. / A/ W  l8 v8 q/ J. `# Q0 c* K
  6. >>> math.ceil(4.01)) ~& F$ A9 U) k4 i* l: P/ M( K
  7. 5
    7 y, }1 ~' k5 r
  8. >>> math.ceil(4.99)
    " Q7 h+ `4 Y# q6 n* I2 Y
  9. 5  N/ q7 [+ B. s
  10. >>> math.ceil(-3.99)
    7 p0 M6 @3 U4 u! m
  11. -3, x! r- k. ~. H& U' p8 M0 S
  12. >>> math.ceil(-3.01)! q) M! F' r0 B' @* P
  13. -3
复制代码

) O+ }: i; c# l9 B1 l8 y1 gmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ p9 l* D8 E' Z6 c4 K/ Z; ~
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    . s4 g7 c5 g/ k+ {  B
  2. floor(x)
    % M/ F$ _8 p* ?. H5 a2 V$ j$ M- |
  3. Return the floor of x as an int.9 F) [0 X; P% V% T6 m( O4 g3 M
  4. This is the largest integral value <= x.' b% ~2 R7 w6 |( H. ~7 c
  5. >>> math.floor(4.1). ^2 w0 j- H1 {4 D4 E" ^) ~/ Y  E
  6. 4: C) Z' b$ l$ a0 i
  7. >>> math.floor(4.999)4 x; ^6 W9 K& f2 {/ u' j) {8 Q2 r, j
  8. 46 v. K. L6 h- n9 Q
  9. >>> math.floor(-4.999)
    : Z4 S4 x% U$ j7 d6 _
  10. -5
    : a+ m4 z  J0 N" q0 s+ r; `
  11. >>> math.floor(-4.01)
    * y0 q3 b5 ~" ]' u( u1 I
  12. -5
复制代码
1 l9 m& Q/ k% r) @% U( Q
math.pow(x,y)  返回x的y次方,即x**y
) E7 F8 R: |/ v6 Q# b. g/ g% g
  1. #返回x的y次方,即x**y
    " s, f6 K& `% i5 V1 a* j
  2. pow(x, y)
    4 E$ g9 p6 a$ m- H! \( R% s. ?4 n1 s
  3. Return x**y (x to the power of y).  f. z8 X3 N" p- o2 r( A
  4. >>> math.pow(3,4)
    / g1 M/ `( \3 {0 J- s/ z; j
  5. 81.0* Z7 g$ |0 }( E+ ]
  6. >>> 4 Z/ B% O5 d$ ?& {' r- N
  7. >>> math.pow(2,7)
    & x  w) v. ]6 q/ U" t/ u9 _: g
  8. 128.0
复制代码
7 A( q. }7 {" }: \+ F. n
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" l# ?) Y6 x! T$ s% x* M# h
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  G& H3 T* X; A; Y3 G% |
  2. log(x[, base])
      M- I5 B2 {; E# c2 @6 N
  3. Return the logarithm of x to the given base.
    ! G. b& X- l* w0 ~3 E
  4. If the base not specified, returns the natural logarithm (base e) of x.# K6 z- A  s) x7 T3 Q2 B
  5. >>> math.log(10)# J! S( ~. n5 {% S7 B
  6. 2.302585092994046
    ' \# B  ~7 p7 w) i4 b8 B
  7. >>> math.log(11)6 |' R( Z! r. b- q' A5 H& c, ~0 D
  8. 2.3978952727983707
    & t7 Y! b% k4 P+ D( m
  9. >>> math.log(20)5 }) K$ b6 s+ @7 u% M6 M3 G6 |/ Y
  10. 2.995732273553991
复制代码

) N% [" r$ c( g+ @1 [& Y9 dmath.sin(x)  求x(x为弧度)的正弦值
, a+ l6 H9 L& b; ^% D% ?* k4 d5 _" M
  1. #求x(x为弧度)的正弦值
    6 v% U, R* H. ~6 ]6 J" G5 R
  2. sin(x)1 j3 f+ ^1 x! a; z# b! J
  3. Return the sine of x (measured in radians).
    - E3 Y4 ]$ c* `6 V$ b/ c
  4. >>> math.sin(math.pi/4)
    0 w! T; d: u3 o! \
  5. 0.7071067811865475
    : P# s( ~. k" u! [5 H. n
  6. >>> math.sin(math.pi/2)% o- O' k5 [/ E+ R4 l
  7. 1.0* _  X& i( |, a+ C7 {6 I# x  o' Z
  8. >>> math.sin(math.pi/3)
    6 D6 e0 q! y1 r5 Z2 s
  9. 0.8660254037844386
复制代码
8 h6 x. H8 }4 K6 J' @
math.cos(x)  求x的余弦,x必须是弧度
9 a7 d! U9 x- v/ W4 k3 G
  1. #求x的余弦,x必须是弧度
    ( t, r2 i* V( U. D  o
  2. cos(x)
    - k0 l* B% ^6 ?
  3. Return the cosine of x (measured in radians).
    * ]8 I7 G4 t, Z# ]' ?0 R% S7 C
  4. #math.pi/4表示弧度,转换成角度为45度
    # ~/ r% M  B4 q7 ^  |  A1 N
  5. >>> math.cos(math.pi/4)0 B+ b* X( P6 X4 l4 v! L- M6 ]
  6. 0.7071067811865476
    5 }" w/ \/ P5 Z
  7. math.pi/3表示弧度,转换成角度为60度9 `( i5 q9 N! ~" c  X
  8. >>> math.cos(math.pi/3)
    : k8 B* L( C. ~+ L" |1 I  B
  9. 0.5000000000000001$ {" a" p- }. u& ?  P; M" J
  10. math.pi/6表示弧度,转换成角度为30度$ ], G5 F) l1 y: d; \
  11. >>> math.cos(math.pi/6)
    9 f7 x$ y* N7 T5 w
  12. 0.8660254037844387
复制代码

' d/ G6 [, b- S* {# u2 w- X7 Qmath.tan(x)  返回x(x为弧度)的正切值
: g, b4 N7 f+ }7 y$ \
  1. #返回x(x为弧度)的正切值
    5 z1 a$ I) H2 B# r, J
  2. tan(x)
    % e8 r/ ^5 I4 {& v
  3. Return the tangent of x (measured in radians).% o  r4 Q) M. o( ~+ z. C7 y
  4. >>> math.tan(math.pi/4)/ G0 X; O! d0 z# l/ o
  5. 0.9999999999999999
    9 _: T; _$ J. D+ Q1 f
  6. >>> math.tan(math.pi/6)  F- |& `) d# m1 ?
  7. 0.5773502691896257* W( s) |( L" M* N! @
  8. >>> math.tan(math.pi/3)
    : l8 z, ^% v# G% C. p' [
  9. 1.7320508075688767
复制代码

2 f. _- `' D' nmath.degrees(x)  把x从弧度转换成角度3 C# S1 f  ?6 F# A/ l
  1. #把x从弧度转换成角度/ I- v  D7 Y) e
  2. degrees(x)# u7 |+ ?3 J) ^1 E9 [5 `
  3. Convert angle x from radians to degrees.1 g* I9 T+ f+ p  L) E

  4. ) K. d$ f3 H5 m$ I& ]8 s# x% o6 z
  5. >>> math.degrees(math.pi/4)* e: N4 \3 G6 ?+ m
  6. 45.0& w! v) u  J7 L9 ^- [
  7. >>> math.degrees(math.pi); _4 Q5 U1 x! f% e0 J# w6 [8 b6 X
  8. 180.0
    $ [# |% g2 P$ x0 H
  9. >>> math.degrees(math.pi/6)
    / n! c  a% R% I- Q+ t" _
  10. 29.999999999999996
    ; ~+ |5 M% f$ T# n2 _1 m
  11. >>> math.degrees(math.pi/3)
    8 ]0 ?; e5 [5 A  X# r' s2 Q
  12. 59.99999999999999
复制代码

# U& g$ B3 d8 T: C' H. N" |2 Jmath.radians(x)  把角度x转换成弧度# t0 ^! a+ i- w* N% x
  1. #把角度x转换成弧度) d; k& A2 v2 y, G" ~$ {/ W
  2. radians(x); m5 z0 n6 b. p$ H: R! a4 B8 ]
  3. Convert angle x from degrees to radians.  h7 V" E% r* a( B! n
  4. >>> math.radians(45)
    & V2 T3 X: e' O+ \% V
  5. 0.7853981633974483
    0 t  E/ L) \( G7 |* P
  6. >>> math.radians(60)) i6 H/ k' W" _! T) j$ l
  7. 1.0471975511965976
复制代码

. F: P2 C! W' n/ l" Cmath.copysign(x,y)  把y的正负号加到x前面,可以使用02 H, R; ~: E- |: ?  K/ r5 O
  1. #把y的正负号加到x前面,可以使用0
    4 c1 L- t6 z2 ?$ k
  2. copysign(x, y)
    : T" ]9 x. u) e$ ?$ t
  3. Return a float with the magnitude (absolute value) of x but the sign ' p, p0 w* Z$ D1 i) c" ?
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( P6 k; g$ {! \- `
  5. returns -1.0.
    ) d% ^& b# o7 a1 G
  6. . o- S( E+ g2 G2 y! J
  7. >>> math.copysign(2,3)) n+ S- J1 o2 e3 @( u) |
  8. 2.01 B  A! W) d1 t
  9. >>> math.copysign(2,-3)
    ; E; k( {) Y* @8 B
  10. -2.05 c% w& b' H% Q4 `! x6 [) a/ B
  11. >>> math.copysign(3,8)
    % l0 K6 H* h1 P5 j) j+ b* P
  12. 3.0- \. w- E1 o7 D- J
  13. >>> math.copysign(3,-8)
    : C( H& b# ]. A* ~- Z$ f
  14. -3.0
复制代码
; W7 l2 \4 p2 f( y
math.exp(x)  返回math.e,也就是2.71828的x次方
, h- u3 H* z; K2 z, O, a9 w
  1. #返回math.e,也就是2.71828的x次方  v4 D" y) I2 k! @3 p( V# ]. t
  2. exp(x)
    9 P" @# ~7 m7 E; ~5 I# M
  3. Return e raised to the power of x.+ O( o- b3 w8 J5 i4 `2 H

  4. - e" I: y: x, M; H: B# A8 m' f! B
  5. >>> math.exp(1)
    ( O- w6 P( t1 k% i
  6. 2.7182818284590451 P; q) u9 G6 f; \" X% s, W: j$ y
  7. >>> math.exp(2)
    7 I) i1 g) w, Y
  8. 7.38905609893065; Q# ~" ?4 p4 K
  9. >>> math.exp(3); \9 U  Z7 c" q. A) Q9 ]+ K0 H% [
  10. 20.085536923187668
复制代码

; K' z3 ]" F$ g9 `0 S9 S" fmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
5 K& v- i$ B9 [# f9 _
  1. #返回math.e的x(其值为2.71828)次方的值减1
    8 R# {. J' q4 E0 X0 b% C" M0 t$ E4 w
  2. expm1(x)  S+ t/ t8 _  q1 a* P
  3. Return exp(x)-1.
    + K/ f- K  |! g* w/ X. N
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    6 z0 h) b4 S/ h5 O# ^/ J

  5. 7 B" X9 F0 d( B8 n9 g
  6. >>> math.expm1(1)
    : L5 z/ _& b8 h1 c, R9 d" P+ ]
  7. 1.718281828459045
    $ k0 t6 j- k  e7 o
  8. >>> math.expm1(2)- n, b! |5 v5 h# l4 n
  9. 6.38905609893065
    5 m: J6 |; c- x# u4 c6 N  _
  10. >>> math.expm1(3); Q6 A  o0 H# h9 |! o: V
  11. 19.085536923187668
复制代码
0 g* b6 d3 E" X6 J
math.fabs(x)  返回x的绝对值1 w; u) A, D6 k
  1. #返回x的绝对值
    3 j  a0 \+ `8 W4 @7 ?
  2. fabs(x)
    2 E% s7 S) c+ U
  3. Return the absolute value of the float x.+ H5 e. X* k) u/ _3 j; T- }
  4. - q  s# ?/ T( L% a! s$ }
  5. >>> math.fabs(-0.003)
    4 ^7 W  p8 V# l4 m
  6. 0.003
    3 ?7 E/ U& z; K. ~$ G- @- u: b; n+ ]9 a
  7. >>> math.fabs(-110)
    ; |% B7 M' |$ e% E9 \2 i8 k7 w4 E
  8. 110.0, Z1 Y6 Z$ X4 q2 i# @/ {* L+ i
  9. >>> math.fabs(100)1 x; M! s6 M, c$ X' i4 _$ E" ~
  10. 100.0
复制代码
* J* c* a  G- F. a" @4 V* `
math.factorial(x)  取x的阶乘的值
# ^" @- r& x2 \: B
  1. #取x的阶乘的值
    , F! K; u( G7 [2 ~; M0 g
  2. factorial(x) -> Integral! v' s3 v. H# v& e) i
  3. Find x!. Raise a ValueError if x is negative or non-integral.% P$ g9 \5 T, a6 \+ h, O
  4. >>> math.factorial(1)
    ( o, r, W1 u7 u
  5. 1/ O5 q9 o$ z, L% x. p
  6. >>> math.factorial(2)" g" v2 j+ m) u/ }0 r# T
  7. 28 B7 l- |3 ~6 v7 g: c. w- U
  8. >>> math.factorial(3)
    ) ?% W; z. S. M& ?* d" V
  9. 6& [( [4 `/ P& e" S. l% r
  10. >>> math.factorial(5); Y# R8 `' O7 C+ s5 U8 S
  11. 120
      Z# o- |  K' o" d+ ]
  12. >>> math.factorial(10)4 k& k. V% F5 ?
  13. 3628800
复制代码

7 n- n7 m7 ?5 n% \! r8 Y) Kmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数; Z: u5 s& C9 [8 Q. m, l: I
  1. #得到x/y的余数,其值是一个浮点数
    * G+ I5 ^) V8 G' ]" R0 B
  2. fmod(x, y)
    7 v6 i* L# ], ]# Y
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    & H  P& n$ B* W
  4. >>> math.fmod(20,3); M3 M: _# U& |5 o: ]1 \
  5. 2.0! v# Z+ v/ O- r: \5 Y% M
  6. >>> math.fmod(20,7)
    8 z/ T- S* h5 q1 ]7 A1 V
  7. 6.0
复制代码

2 k1 ]) T- n& nmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! {; d8 P7 D7 o% N
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ! A+ k/ W  S5 F+ V- V
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ) S9 K8 `. Z- J' G
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    $ \8 O+ K4 L3 F! E
  4. frexp(x). Y3 P; U& m4 J. a) I
  5. Return the mantissa and exponent of x, as pair (m, e).* c4 H! Q+ }+ x+ m* o" R* T  p
  6. m is a float and e is an int, such that x = m * 2.**e.
    % D& B9 _$ |/ N" J; j
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0./ ]+ J1 m2 V; d  y; s; x- {
  8. >>> math.frexp(10)
    5 d- H7 v0 x+ {7 z, {
  9. (0.625, 4)# ~% `. o6 |- C. i1 U0 P- Q# ^2 S2 D% y
  10. >>> math.frexp(75)( h0 Y7 @% {  ~4 ^4 @) P3 ^: {8 q' k) o
  11. (0.5859375, 7)
    # p7 v( |  B( [4 i- U4 \
  12. >>> math.frexp(-40)4 Y; p& ?& S+ o: k
  13. (-0.625, 6)
    1 {* d! g. m: z0 @
  14. >>> math.frexp(-100)
    1 h7 E2 B" s5 |1 Y: [: }
  15. (-0.78125, 7)
    7 R% s# g9 L& `$ j: n
  16. >>> math.frexp(100)
      ]" [$ m2 `* w. L: A) H1 k- Y% y
  17. (0.78125, 7)
复制代码
+ p; G% L8 g4 P, H* O* I6 o5 H* L
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列$ W4 R6 x- p. P  \3 L  _
  1. #对迭代器里的每个元素进行求和操作
    ) K/ F; g( {9 c( m3 D& c9 k
  2. fsum(iterable)" l# V# e5 F$ ]% m3 G+ I7 A
  3. Return an accurate floating point sum of values in the iterable.1 s, U& T6 _) l9 _1 r
  4. Assumes IEEE-754 floating point arithmetic.
    0 J$ P$ Z/ ^1 y7 o/ F. i# N
  5. >>> math.fsum([1,2,3,4]), K7 o( C5 X& D' E% R
  6. 10.08 R4 y, l. \5 X# j5 V5 a
  7. >>> math.fsum((1,2,3,4))
    # i  I! h! T4 a& ]( i6 [
  8. 10.0
    * h9 Q: M0 }/ s5 S
  9. >>> math.fsum((-1,-2,-3,-4))
    9 d' K/ j, J5 U
  10. -10.0$ S/ u% B: R7 t5 r! D) E- I# s
  11. >>> math.fsum([-1,-2,-3,-4])0 d7 v& n8 o' a& R9 U: L
  12. -10.0
复制代码
% k* g, a! G+ m+ o
math.gcd(x,y)  返回x和y的最大公约数1 i% e2 J; P4 F* ~
  1. #返回x和y的最大公约数+ ?; [8 e" P( S8 W* |$ s0 r
  2. gcd(x, y) -> int
    : s2 _0 c. @* b  k
  3. greatest common divisor of x and y  [, ?- t6 D8 \3 \- g% m: R0 V' w* x
  4. >>> math.gcd(8,6)0 |5 L1 t6 L" f
  5. 22 G6 G4 o$ ]; O  {: z- u/ ]- ]
  6. >>> math.gcd(40,20)( S' X* G7 M" `  y& o3 x- o$ b
  7. 209 ^- C- b2 A. h' ~
  8. >>> math.gcd(8,12)' E4 K( F- _, e8 t
  9. 4
复制代码
6 t% X- f9 W6 s) C3 J) y1 v' |0 ~
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
4 s2 r, Z5 v/ x2 @( L8 Y8 E
  1. #得到(x**2+y**2),平方的值
    : a5 H5 ^: p$ d7 ^6 k" H' m' g
  2. hypot(x, y)
    , |- \) F& X8 W- S+ V9 ~
  3. Return the Euclidean distance, sqrt(x*x + y*y).0 v( n9 s# o( d7 w/ e6 _. j- \
  4. >>> math.hypot(3,4)& x, w+ z& \2 W; W
  5. 5.0! H, X$ G( y, F/ C8 N( a4 w0 m% {
  6. >>> math.hypot(6,8)2 O6 w/ a) X2 S% R
  7. 10.0
复制代码

. C0 f# ]) W: j6 Y% P+ Xmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
( p* E1 m' l) F! j0 j/ ~( M6 E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    7 t2 ]/ ?2 P2 F; U
  2. isfinite(x) -> bool
    & g- `5 P' z; ?* C! s1 L' Y. Q2 R# K" K
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
      `" C+ t) y, G% b
  4. >>> math.isfinite(100)
    ' S+ O3 q- [1 x6 ]3 P6 W# K
  5. True
    # c" K' r& i" W+ ~1 O6 M! u
  6. >>> math.isfinite(0)
      c  w/ w& k: w" A" z3 f- ?# _
  7. True
    1 ^1 Z8 J3 w$ B8 P+ c/ c/ x, _' d
  8. >>> math.isfinite(0.1)
      p( l0 ~7 x$ B# C: Z$ e) n
  9. True
    * q4 T5 V2 d0 V" o
  10. >>> math.isfinite("a")
    # t" B& ~; T" ?  w0 e
  11. >>> math.isfinite(0.0001)
    1 R" ~* m: c5 i+ F
  12. True
复制代码

3 t: [) {+ r$ {+ S$ R! Lmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ e* y: C. p% {$ ?* u4 A
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    9 O4 @$ {) Z  o2 T. @! e
  2. isinf(x) -> bool5 ?! u) N* Q3 b2 \, ~/ ]0 W
  3. Return True if x is a positive or negative infinity, and False otherwise.% C8 A. l: F+ o: O  H+ m$ C
  4. >>> math.isinf(234)
    9 C3 {% ?- O* _& K% ]
  5. False
    ; z0 x$ a! R# Q$ j2 H+ P
  6. >>> math.isinf(0.1)
    - Y  a3 W, H8 Z' v- w
  7. False
复制代码

. b0 |( j. G- w' H4 K8 [math.isnan(x)  如果x不是数字True,否则返回False
; O) ]/ M$ m5 O
  1. #如果x不是数字True,否则返回False
    ( k  N; Z* M/ k% H' w
  2. isnan(x) -> bool' l( v7 ], j% Q3 ~/ Q' p8 S
  3. Return True if x is a NaN (not a number), and False otherwise.
    1 X- a0 V0 e( }- n2 Z7 |3 A
  4. >>> math.isnan(23)% _- W. Z# G! d: ~0 \
  5. False
    " {9 Q0 X. w  ^5 R4 X6 q
  6. >>> math.isnan(0.01)& R: _2 R7 l( ^* h% S, b* @. X
  7. False
复制代码
9 D* ^% D4 }/ G- v# k
math.ldexp(x,i)  返回x*(2**i)的值2 `* Z; x4 C! G! V
  1. #返回x*(2**i)的值
    4 _! Q+ W% }# @
  2. ldexp(x, i)  |$ a  i. v5 r, R+ U% f, I
  3. Return x * (2**i).9 h: q# Z; y# t2 \+ ]
  4. >>> math.ldexp(5,5)
    / @, ~0 U! r, x- v# U
  5. 160.0
    7 c) a7 O: d- A# i
  6. >>> math.ldexp(3,5)8 _. b+ K/ U) L0 C! H* ]$ [! P2 ~
  7. 96.0
复制代码

' ^$ |% ]6 h" m& c  {; dmath.log10(x)  返回x的以10为底的对数
1 X. I: H$ D$ Y' F  d6 k3 ]
  1. #返回x的以10为底的对数1 f; G6 K9 j9 }/ X
  2. log10(x)
    8 `7 g- ^5 E' x
  3. Return the base 10 logarithm of x.
    , D# F# b2 W! f" G
  4. >>> math.log10(10)
    5 k& ^) y7 k7 o7 {% I
  5. 1.0
      t; k3 ]; I6 a1 q) F4 @2 V
  6. >>> math.log10(100)
    ( r" c' d6 o! |; B% J; ^) L
  7. 2.04 B; \$ _( c$ b4 W0 P+ h4 I
  8. #即10的1.3次方的结果为206 Z" |2 O# m! z; s* |; Q
  9. >>> math.log10(20)! o4 ]' h0 {1 ^% m
  10. 1.3010299956639813
复制代码

3 p0 y2 ~7 K+ m% r2 hmath.log1p(x)  返回x+1的自然对数(基数为e)的值: C1 A' r0 ^3 K/ W0 f% m- P
  1. #返回x+1的自然对数(基数为e)的值5 V) a5 S* d0 }! ~. A& F+ {
  2. log1p(x)
    # s3 I& w  A% G' L2 ]
  3. Return the natural logarithm of 1+x (base e).$ _+ O( q: ]* f
  4. The result is computed in a way which is accurate for x near zero.
    , V. V5 Y% q1 R
  5. >>> math.log(10)) L; I  U7 X: B8 }( B/ h  I0 ^2 l. A
  6. 2.302585092994046& O) d: u1 C( d5 K) A1 P
  7. >>> math.log1p(10)5 {) {& \, `; X( g% v/ q
  8. 2.3978952727983707$ L/ {, T1 q$ @$ ^7 Y. {5 T0 g3 s
  9. >>> math.log(11)$ s% ?, w, A) z$ _) q' f4 a* b
  10. 2.3978952727983707
复制代码
3 D* _* E8 i+ H/ Z' e; z& v7 P0 a
math.log2(x)  返回x的基2对数
; S5 t9 v) ~. @( @' s1 C0 ]3 V* m
  1. #返回x的基2对数
    & P( O4 f. e" h7 S* L/ b
  2. log2(x)
      Q6 ^4 g% r% x' B/ W1 d/ |
  3. Return the base 2 logarithm of x.9 e( g; m/ x. g4 R5 s( E
  4. >>> math.log2(32); F% u" K  J+ I8 W  m
  5. 5.0& f: x/ f9 z6 Q
  6. >>> math.log2(20)
    0 Y1 W! h6 G; E% U, K  H* B
  7. 4.321928094887363* ~7 h; D0 R( I7 Y
  8. >>> math.log2(16)
    ! }/ M3 R+ q0 y) o9 K8 g
  9. 4.0
复制代码

- F3 M% ?7 I, ~" {2 G& Y- Imath.modf(x)  返回由x的小数部分和整数部分组成的元组/ s- }3 \2 q# T1 ]1 `
  1. #返回由x的小数部分和整数部分组成的元组+ k% J" N0 B- P% e* n+ b7 E+ j5 V
  2. modf(x)
    / o0 j9 U% O9 [# o7 g. o
  3. Return the fractional and integer parts of x.  Both results carry the sign; h' c1 C: J. B
  4. of x and are floats.
    6 a3 r+ T3 r( e6 m
  5. >>> math.modf(math.pi)
    , z; ]7 j  l! m, a5 l" J
  6. (0.14159265358979312, 3.0)
    ) g) f9 T# i, y- D
  7. >>> math.modf(12.34)
    9 S5 ~5 B* a3 n" t" ?) V9 D; @
  8. (0.33999999999999986, 12.0)
复制代码

- _% \  T- r0 s0 [& n1 B5 lmath.sqrt(x)  求x的平方根2 \' [5 A, v4 x7 }; ^7 N
  1. #求x的平方根: L4 c  t+ F- |# Y+ x' s+ _# K6 l' v, ~
  2. sqrt(x)
    9 Y6 l; W2 E8 E! C, V! i
  3. Return the square root of x.
    4 q- Q, Z+ n6 p5 D- }. _
  4. >>> math.sqrt(100)
    ; j! h" n# d* {; j# e3 R0 Z# e
  5. 10.0
    8 V6 N: S+ U8 ]$ e, _; u
  6. >>> math.sqrt(16)
      X7 Z& t; R& M) G, e4 {2 F
  7. 4.05 a$ D' ~/ P. u. _. }
  8. >>> math.sqrt(20)5 y* o: U) D4 A' C0 }% g3 B$ `
  9. 4.47213595499958
复制代码

. o5 p' v! s- n7 a' y  A/ r5 Tmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    0 s( A" W5 N: E+ c1 g( J4 B
  2. trunc(x:Real) -> Integral" R/ q, s3 ]/ T  S
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 T/ r  n& C. A  u' d/ R% v
  4. >>> math.trunc(6.789)
    / {3 k" j" f' f6 N
  5. 6
    % l* R7 m2 I3 @2 g6 }; h- `! G
  6. >>> math.trunc(math.pi)  q5 l% ]$ c) [  l
  7. 3
    % r% m( H- u9 B) z7 m0 l
  8. >>> math.trunc(2.567)
    9 M" o6 O/ |- U5 d$ n) k
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-20 00:28 , Processed in 0.080104 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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