新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

* _) R2 W: w1 w* q8 t' `+ g【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, G! s# h. L' Q& Z
% _! f* [1 _! m$ P. r! G  U0 Q
方法1( s2 O8 v3 R) M. Y* ?
  1. >>> import math
    / O* g) _3 a7 L
  2. >>> math.sqrt(9)  ]  o9 k1 ]  E/ |7 F$ T0 `! F
  3. 3.0
复制代码
方法2
) `8 m" `1 U( A& c% X0 J3 T
  1. >>> from math import sqrt4 F0 j& ~6 d3 b- W3 Q% P
  2. >>> sqrt(9)" h, j) Z9 ?, T" z% n! Y
  3. 3.0
复制代码

/ d8 C8 P4 S+ s9 C

' N9 E( e; W% ?: _( v
math.e  表示一个常量9 }" S4 P, ^" Z# [, E" z
  1. #表示一个常量
    4 v9 T: N. V5 D7 }; ^
  2. >>> math.e
    $ o9 @% @! a1 Y# t0 i# W& K- E
  3. 2.718281828459045
复制代码
# B# e  B% ~: p$ x9 e
math.pi  
数字常量,圆周率
$ L' O4 k) s4 ~1 W, a% f
  1. #数字常量,圆周率6 g, ^$ L  K* _  G; n
  2. >>> print(math.pi)
    4 x( A* i8 I- w0 y7 E6 M# X5 U. s
  3. 3.141592653589793
复制代码

1 v; H3 _  G) xmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

4 w. @6 D* B' d# \
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    7 J( Y+ ?) I$ c, U: f& B- C# l" o& x
  2. ceil(x). k5 j& V& m( w6 w
  3. Return the ceiling of x as an int.0 ~; a; N# Q# ], B6 c8 o' A
  4. This is the smallest integral value >= x., g7 F( Z0 j8 q" h
  5. 4 v! d+ h! ]+ o& `" J
  6. >>> math.ceil(4.01)- [4 j; D9 W* b( k6 S
  7. 5
    ) f5 M( v& a6 j, o/ A( h* ~' K
  8. >>> math.ceil(4.99)
    4 m' g0 E" ?2 J; q5 N
  9. 5
    6 C/ ^% A# v0 a! J
  10. >>> math.ceil(-3.99)- _, u# T8 V: Y7 ~; B: k- x, ^) ~
  11. -3% F6 {: b) o2 R4 R# c
  12. >>> math.ceil(-3.01)
    - s% S* S! N3 h; k/ I1 r+ ?
  13. -3
复制代码
  P: p9 p( x5 ]: D# P" q
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身  p( Q6 ^: ~. K1 q5 [$ E
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身  S# a8 z' ]! N# ~% F* ]
  2. floor(x)$ F1 H! E8 g) G. j
  3. Return the floor of x as an int./ C. N- H; l7 H; U; i# f
  4. This is the largest integral value <= x.2 L' L5 _% @8 ^- n" p" S  x
  5. >>> math.floor(4.1): E* D& ~) y5 D
  6. 4
    4 \! z9 _, V. f' w
  7. >>> math.floor(4.999)0 h  g9 Q$ R" x, @' S
  8. 47 N. _: k7 K9 z" i0 H. r% [/ L
  9. >>> math.floor(-4.999)$ t( o( P) @; Q! D5 q2 R9 A% ^
  10. -5& ]+ {( s: y6 @
  11. >>> math.floor(-4.01)) J2 v9 [5 Q" ^
  12. -5
复制代码
  X: z4 u. R/ v* s7 W+ H
math.pow(x,y)  返回x的y次方,即x**y
8 k+ ^4 t, A' w3 b0 x, C, A" V
  1. #返回x的y次方,即x**y
    0 a  b) c. k- a; W/ _% X
  2. pow(x, y)
    ; }5 H  w6 p- L$ C" Y6 Y! l( P
  3. Return x**y (x to the power of y).
    ) T0 _9 {( h& y- M+ T
  4. >>> math.pow(3,4)
    & W2 I, Q; ]- a' ~. d. `
  5. 81.04 o# e1 S& n1 W- {: ]6 i
  6. >>>
    ( w" a5 g8 y, s& r
  7. >>> math.pow(2,7)
    # o* _5 \* v/ E4 m* G/ a# B! X2 j
  8. 128.0
复制代码

5 R% l3 [" _# `! t2 _/ Qmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* X( {7 I1 G$ G3 l: x- W
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). ^1 F0 o! T- W1 _7 ~( F
  2. log(x[, base])0 `' d0 p$ ?+ n' S" k6 s
  3. Return the logarithm of x to the given base.9 Y  A5 z/ z# d) d
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 k3 g  G  z& Y, `( Z* g2 G
  5. >>> math.log(10)
    # x- ?7 H8 E4 M" j9 z4 b
  6. 2.302585092994046/ x* }5 i2 E' H9 k
  7. >>> math.log(11)
    ( x. c( O4 O! t. p! ]+ n
  8. 2.3978952727983707, B6 K1 m+ [$ D; Z) i
  9. >>> math.log(20)
    + y  X8 G4 S# a- l, b8 C( E4 ^, E
  10. 2.995732273553991
复制代码

- I" S) ?0 F& d) g" t* @- p8 Imath.sin(x)  求x(x为弧度)的正弦值  t( d9 o1 X, M6 e; y! [& z- x
  1. #求x(x为弧度)的正弦值4 f& T# O  G* f; U
  2. sin(x)& z# }/ W3 y* V% O2 G! N4 O
  3. Return the sine of x (measured in radians).2 {0 J8 B3 y* W8 K) ?: h5 H3 }) l* ^
  4. >>> math.sin(math.pi/4), p! u0 c% E0 I) D+ d
  5. 0.70710678118654753 p" d3 m: z! k- g; \7 {0 @
  6. >>> math.sin(math.pi/2)1 A6 L* i; C1 w: J, y1 J. i4 d
  7. 1.0; P7 f8 G4 \4 R& P) W
  8. >>> math.sin(math.pi/3)
    # C3 Y* V9 Q1 x  }5 ~
  9. 0.8660254037844386
复制代码
; R% v, m3 m# H# C4 e" H; N* e
math.cos(x)  求x的余弦,x必须是弧度
: M( }, ?! i# e8 C$ I
  1. #求x的余弦,x必须是弧度9 g/ M5 w0 E; h( m/ W
  2. cos(x)/ c% v" O: Z' T# w
  3. Return the cosine of x (measured in radians).+ i/ b5 H1 T& P+ T$ |
  4. #math.pi/4表示弧度,转换成角度为45度# ]+ t9 X% o: ~
  5. >>> math.cos(math.pi/4)
    4 [. i7 D/ b: Q  M  K
  6. 0.7071067811865476
    6 J0 @7 J- f4 ^" f% D
  7. math.pi/3表示弧度,转换成角度为60度
    ) y, h! _% o+ X- A$ k" p  t0 a" n
  8. >>> math.cos(math.pi/3)5 w1 p% {( S  p% ~. W
  9. 0.5000000000000001+ C" Z6 ], g) N; B9 g, H$ x
  10. math.pi/6表示弧度,转换成角度为30度# F) j2 e! _, a; `& t
  11. >>> math.cos(math.pi/6)) z! `( T/ t( g8 N9 w9 S
  12. 0.8660254037844387
复制代码
& k( o. X2 L& M1 l9 ~
math.tan(x)  返回x(x为弧度)的正切值+ h; b9 f* T, v
  1. #返回x(x为弧度)的正切值
    / X- h" T; \! K6 q7 J
  2. tan(x)
    ( a6 p1 Y3 |" q6 _8 c) S/ H
  3. Return the tangent of x (measured in radians).& I7 R+ @/ y4 x/ y: x: v, l
  4. >>> math.tan(math.pi/4): v& r0 ?( w! Q# i' O" v7 ~
  5. 0.9999999999999999
    : o% w; N5 k0 y0 D; V
  6. >>> math.tan(math.pi/6)' b. L. U/ ?, A
  7. 0.57735026918962570 X2 q/ I$ N7 _) D8 E
  8. >>> math.tan(math.pi/3)
    # a& B/ T; J3 \6 p
  9. 1.7320508075688767
复制代码
/ U/ [- c0 p3 \% ~
math.degrees(x)  把x从弧度转换成角度
" J3 S( H  D: K8 |, k$ _6 ~
  1. #把x从弧度转换成角度
    8 T7 y8 K4 O# Y. q. Q0 _: w) Q3 ]
  2. degrees(x)
    6 t, {0 V$ s$ `! k! @* a
  3. Convert angle x from radians to degrees.
    9 v2 X5 ^' ^7 ?% Z3 {
  4. # L$ }( m; `2 V+ p( y2 l3 R
  5. >>> math.degrees(math.pi/4)6 }% `9 }0 X$ f6 x/ J
  6. 45.02 H0 C7 W. q( z) @
  7. >>> math.degrees(math.pi)- m+ B5 ^$ J. c: _/ o3 Z- m) N7 y8 C9 o
  8. 180.0
    * h) |& M& Y2 X' o0 \
  9. >>> math.degrees(math.pi/6)
    4 c; t4 K! W7 o% e  t3 @" B% n" }: r
  10. 29.999999999999996! x$ Z; P( p5 W" \. O" u% u9 g' L: x9 ^
  11. >>> math.degrees(math.pi/3): K% z' c, ^( A0 F& j9 N% c
  12. 59.99999999999999
复制代码
, m8 |, e, l  H1 {
math.radians(x)  把角度x转换成弧度
( O) j7 S  Y9 V  x! [1 E) _1 c
  1. #把角度x转换成弧度
    2 @: q1 z3 _* e6 D
  2. radians(x)$ `( z& a, g- w) J( t" t; g
  3. Convert angle x from degrees to radians.
    3 s' [: H; F7 H5 ]5 F  E$ J
  4. >>> math.radians(45): h  [8 z9 T; z/ [9 W$ d% w
  5. 0.7853981633974483
    . }+ I9 K/ F5 J6 ^7 W
  6. >>> math.radians(60)/ E4 N# d$ o+ y
  7. 1.0471975511965976
复制代码
# A  f8 _. {. d* I4 T8 T2 D& Q
math.copysign(x,y)  把y的正负号加到x前面,可以使用00 Q: a( u2 U( r" \+ `. C
  1. #把y的正负号加到x前面,可以使用0
    3 r  |8 P+ r8 @1 c! c% S
  2. copysign(x, y)1 `. w7 S& ^% |# x
  3. Return a float with the magnitude (absolute value) of x but the sign
    ; ~9 V6 m: y' U0 v9 e
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , c0 F- p: q" T5 E- l: i% }
  5. returns -1.0.
    / ~: v8 d8 ]3 o# w1 T: I. g' j& x
  6. $ E0 r, F7 \9 i$ M* q" O% H/ W
  7. >>> math.copysign(2,3)
    ( @) @6 s( M1 h: i/ }, e
  8. 2.0& O1 ?* c: N0 x! d- v
  9. >>> math.copysign(2,-3)8 B* I! M( o" `  ?8 g7 W# N
  10. -2.0
    6 j5 ?: X* C% S' T4 f
  11. >>> math.copysign(3,8)
    + c/ B, l: z! @6 P/ R, c
  12. 3.0. V$ b9 q! p  E0 [, c6 S2 C2 C
  13. >>> math.copysign(3,-8)
    ( R, P0 Y: u" q. p& |: t
  14. -3.0
复制代码

9 t- _' ^8 p& V) Lmath.exp(x)  返回math.e,也就是2.71828的x次方
: Q- l4 J, P! U; o7 |# c' T* i
  1. #返回math.e,也就是2.71828的x次方
    ; d$ ~: a4 G& h/ A; a' W/ o' x
  2. exp(x)3 A  j3 l" w" c5 {
  3. Return e raised to the power of x.
    5 f8 U5 z: A) \* X/ X

  4. 8 N7 i. C  S/ F' @# t
  5. >>> math.exp(1)
    . Z; A& M; f- V
  6. 2.7182818284590459 D  y' {3 w0 B7 w
  7. >>> math.exp(2)
    : Y8 j6 N; _5 e  _& u# D8 c
  8. 7.38905609893065
    ) j) e: |; }, i% `% H; j3 \$ k
  9. >>> math.exp(3)
    7 ]- Y4 l# }$ c/ I. _
  10. 20.085536923187668
复制代码
4 D1 ~4 a6 G1 A  G
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
7 N3 u# H: J( K5 G$ X3 v( [
  1. #返回math.e的x(其值为2.71828)次方的值减1. s: A) _! V' E& z4 x
  2. expm1(x)
    7 d0 W. O: A$ ]( N) F5 N) _: T! ?2 y
  3. Return exp(x)-1.- M2 s1 J  [% q7 s
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    , r2 s8 Q0 @5 h7 g! r

  5. % P0 X3 y6 k/ z! Z+ a
  6. >>> math.expm1(1)
    & e4 ^. `8 G; @7 ^% z' }  z, Q
  7. 1.718281828459045
    / i0 ?. F$ o) m
  8. >>> math.expm1(2)/ m9 F* J0 K6 j" Q0 [  I& s
  9. 6.38905609893065
    + B" ^. K! C2 \" n! G  y; R1 g
  10. >>> math.expm1(3)8 m; o* `  @7 G
  11. 19.085536923187668
复制代码
$ g" p7 w) Y! J
math.fabs(x)  返回x的绝对值
" P6 _: }& ^5 j  D; [* `
  1. #返回x的绝对值
    2 l5 z( {# Q; O5 y8 L8 D
  2. fabs(x)
    ) y. F% D! Y  ?$ m1 n
  3. Return the absolute value of the float x./ I9 p0 J3 |9 @/ v

  4. ; B3 M" ~% k; U; T& v& n. m. r" T: `
  5. >>> math.fabs(-0.003)
    ! N2 x9 h( N% U% o" D
  6. 0.0034 Q/ ~! B# `" t
  7. >>> math.fabs(-110)- |7 Q) \5 h4 A8 V- ?5 d& O7 M
  8. 110.09 k) W+ c% p. Q+ @5 Z) M5 ?1 H4 C, \
  9. >>> math.fabs(100)8 s& [  _0 d  ~6 {* o: o, l$ q
  10. 100.0
复制代码

3 N" Y- E4 @  G% _- h' pmath.factorial(x)  取x的阶乘的值
4 |* U/ ?1 C! M4 H! j8 O8 n$ B
  1. #取x的阶乘的值
    ' [7 p1 A4 I- |) @2 R- C. N
  2. factorial(x) -> Integral
    * O8 ]# f' U. G% K9 ]# ?/ A
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    6 b3 P$ Q$ C; q2 k/ {
  4. >>> math.factorial(1)# C1 N9 I0 K8 n! v. A( i% K
  5. 1; \  J5 e2 U0 y  ]
  6. >>> math.factorial(2)
    " I( z  z& Y: G" T! k
  7. 2
    * E# {  G$ C" p& W7 l: E
  8. >>> math.factorial(3)
    / ^, o+ K# Q9 F. e+ ]2 L+ j$ `
  9. 6
    0 b7 b/ }1 u+ P& v% l
  10. >>> math.factorial(5)
    : ^" c* t. s4 `: `" [0 g; w7 |
  11. 120  ~6 x/ g/ G  ?0 C
  12. >>> math.factorial(10)
    ) {0 c- N: Q. F
  13. 3628800
复制代码
$ R. v% b6 ~) J# Z
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
. {: H; l% T* P3 C
  1. #得到x/y的余数,其值是一个浮点数7 [, L0 b1 @" t: |* T) O
  2. fmod(x, y)+ C+ D  L! j7 c9 o4 ^6 T
  3. Return fmod(x, y), according to platform C.  x % y may differ.* Z, s- }2 O! v/ t7 q- x: Y8 \5 i' e
  4. >>> math.fmod(20,3)
    4 }. I! l6 {/ R2 Z+ y% |2 T' n
  5. 2.07 |5 l! N/ o/ O/ C3 Q- l
  6. >>> math.fmod(20,7)
    4 U5 g9 a2 @% `# n- x* r$ c/ I
  7. 6.0
复制代码

- {: f% R& W4 o+ e' |* R, ymath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围# i9 ^' H6 v# _2 v) t. O4 N. d
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    , {9 N- d2 [$ w, u; b
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 [( X& p* @/ E* J
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    9 L6 h( u1 _  C9 B3 Q2 G* l. c
  4. frexp(x)  \: Y7 r- l% V, I
  5. Return the mantissa and exponent of x, as pair (m, e)./ i3 {5 y6 h! U4 @7 i3 u
  6. m is a float and e is an int, such that x = m * 2.**e.3 \8 K. w5 r1 b% ?2 m: P1 z) a
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    ( ^& i% ?  t8 M
  8. >>> math.frexp(10)
    ! ~$ V( d* m* v) ]/ _7 M9 K
  9. (0.625, 4)
    5 c5 v2 |" T* k* e, _. @2 R! \
  10. >>> math.frexp(75)+ {0 d0 k% Q. Y" S/ y0 h
  11. (0.5859375, 7)
    * \/ {9 j, J. M% a9 N- |+ Y' o& a) }# t
  12. >>> math.frexp(-40)
    / S3 f) q2 d. }
  13. (-0.625, 6)
    * |7 K+ Y5 c$ w: \7 I9 V
  14. >>> math.frexp(-100)
    * w4 I8 D0 L& _8 c/ G
  15. (-0.78125, 7)
    8 u+ D- `1 {/ y( C, O1 Z+ a6 J
  16. >>> math.frexp(100)8 d& |& X% ~0 `4 H( I* [# g
  17. (0.78125, 7)
复制代码
9 n" z. z7 \/ H8 p0 X# r
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列( A$ T7 R5 ]/ F7 Q+ `
  1. #对迭代器里的每个元素进行求和操作" v0 h" y) i& S5 y2 m
  2. fsum(iterable)
      i  i/ p$ s  |1 }/ J
  3. Return an accurate floating point sum of values in the iterable.
    : [2 @, w+ H: X* v
  4. Assumes IEEE-754 floating point arithmetic.0 [& i5 y4 }; _
  5. >>> math.fsum([1,2,3,4])* C  V& ~3 F4 x- W" J( N
  6. 10.0
    " b' r; s5 \: X5 G
  7. >>> math.fsum((1,2,3,4))
    / m7 E3 t$ I, n& B
  8. 10.00 ?6 Q6 C7 t' _' M4 g
  9. >>> math.fsum((-1,-2,-3,-4)); R0 B+ X' l# F) x  z1 Q$ B6 Y
  10. -10.0  S( c" R6 V( s2 E
  11. >>> math.fsum([-1,-2,-3,-4])5 n4 s. U5 V8 t4 U& |7 K8 i
  12. -10.0
复制代码

5 h$ H9 F' T9 n  w! L" [math.gcd(x,y)  返回x和y的最大公约数( v2 K* F2 t# i/ }7 f
  1. #返回x和y的最大公约数
    5 e" S$ F3 o+ W* J( ?. Q( t1 b
  2. gcd(x, y) -> int. I6 B8 T: M& F5 J. u. U
  3. greatest common divisor of x and y
    4 a" U( N0 |% v, P0 ~" E
  4. >>> math.gcd(8,6)
    ; V- `- A) a; h: R$ l
  5. 2
      f2 _* \* q3 j7 P0 O% ^: ~
  6. >>> math.gcd(40,20)! G2 s; H3 h- k- w) S/ S# v
  7. 20
    ( t4 Q( f; v! E6 B
  8. >>> math.gcd(8,12)% y2 \8 R  h  J6 x9 n9 ^& g
  9. 4
复制代码
$ i: T0 n: m" V& i2 K5 @
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False% j& h6 n0 l' q0 g: ?
  1. #得到(x**2+y**2),平方的值# W  g/ m3 Z9 u/ S% Q
  2. hypot(x, y)
    1 r4 {2 `) x2 d- `
  3. Return the Euclidean distance, sqrt(x*x + y*y).1 S8 z; m( J( W) m
  4. >>> math.hypot(3,4)  V( p# X0 M1 U% N7 e6 L1 N7 j
  5. 5.0' X9 N6 m( h$ |& F) d0 Q, L
  6. >>> math.hypot(6,8)' H( n8 u& Y2 c+ u
  7. 10.0
复制代码
0 I. }) s' P6 o* W7 ^' Y
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
4 m# J; Y9 P$ ?( h; C
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" C) w2 ]7 ^$ V2 p$ E- F- P
  2. isfinite(x) -> bool; o( `  n3 z/ |# D3 ~! y
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    4 {4 a0 ~1 X# K& Z5 }
  4. >>> math.isfinite(100)  n% ~" N5 _8 M" a7 J
  5. True
    0 ]" w3 ?! o+ o$ Z! v- i
  6. >>> math.isfinite(0)
    ! _, O$ S3 i7 v  Z8 t4 j
  7. True2 N0 x8 ~, T' S6 {. _
  8. >>> math.isfinite(0.1)
    % h. m4 s8 d+ S6 ]' G8 N
  9. True
    ( t( `5 }) H, ^8 {
  10. >>> math.isfinite("a")
    / U3 B2 W8 w4 {, |1 V& K
  11. >>> math.isfinite(0.0001)
    1 p# @5 f* P3 n( n9 e& m* _
  12. True
复制代码
+ ^+ L9 ~1 q0 E8 p/ y
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
: e) t* f% W, v6 i$ w: o
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    $ Q; B6 T9 [: z, ]# w" Y9 d) S% s  e
  2. isinf(x) -> bool
    5 Q' w- a# m) |  @) [
  3. Return True if x is a positive or negative infinity, and False otherwise.
    9 m1 g6 G/ j, l8 a5 ~% g- o
  4. >>> math.isinf(234)
    6 B4 ]1 C0 v+ s8 [
  5. False) z8 D% B1 Z  u" \1 d
  6. >>> math.isinf(0.1)
    7 W& M+ ~- Q7 y7 Z
  7. False
复制代码
& ~1 R% p: J, n% \7 d8 J% \
math.isnan(x)  如果x不是数字True,否则返回False, H0 {" W! n" Y- U
  1. #如果x不是数字True,否则返回False
    + ^% y) y. d8 @+ H
  2. isnan(x) -> bool" N, I8 k# ^3 q& Q" F" H
  3. Return True if x is a NaN (not a number), and False otherwise.
    ( [# ?2 G" c8 `
  4. >>> math.isnan(23)/ u$ y, s5 b- D) d5 }* Z
  5. False
    8 j9 U/ e* `% ^! F/ R' R
  6. >>> math.isnan(0.01)4 [4 n8 V- ~/ _' S# b. r& e. I' n
  7. False
复制代码

. Q4 }! e6 r$ L5 Fmath.ldexp(x,i)  返回x*(2**i)的值) u  l  e% D  Y; ]% e
  1. #返回x*(2**i)的值' U+ x) d; [4 ^" \
  2. ldexp(x, i)0 ^1 D+ B, V5 ^
  3. Return x * (2**i).
    2 a2 m  S# \, U  s5 H: M
  4. >>> math.ldexp(5,5)! B+ Q2 S$ |4 r4 x8 s
  5. 160.05 l8 ?: b# W6 H+ F. F; O7 [3 v: B
  6. >>> math.ldexp(3,5)5 o& d( Q) u$ l& d8 |# \; U8 Q7 C
  7. 96.0
复制代码
& \) d) w9 [) s2 q  @$ u0 U9 B
math.log10(x)  返回x的以10为底的对数% l- E/ O, D" O
  1. #返回x的以10为底的对数) o) f9 I' `( ^
  2. log10(x)* w2 J" X6 `$ d! i
  3. Return the base 10 logarithm of x.5 i8 q# L! M7 x' ^  V6 y
  4. >>> math.log10(10)
    : s8 e1 Y0 k8 C; I( r; h
  5. 1.0  `& q! G$ F% t5 L7 B; y
  6. >>> math.log10(100)' ^7 I* P, J# h% Z
  7. 2.03 T7 t( ~# A( ~6 W1 x, j3 n
  8. #即10的1.3次方的结果为205 K8 F/ m. i2 \) C4 C
  9. >>> math.log10(20)
    - x" b9 N: R5 k. j9 Q/ h
  10. 1.3010299956639813
复制代码
  r0 N! d' m1 X0 ~# I
math.log1p(x)  返回x+1的自然对数(基数为e)的值
4 o( v* S* p# y, d8 C/ v
  1. #返回x+1的自然对数(基数为e)的值+ @8 U$ T; K% Y$ o
  2. log1p(x)
    . x5 X) q' j0 ]; |5 e
  3. Return the natural logarithm of 1+x (base e).
    ! o+ F3 J) i5 {
  4. The result is computed in a way which is accurate for x near zero.  ?! b) m0 p/ G( W7 n2 s
  5. >>> math.log(10)9 E/ u8 A. }2 D; Z. r
  6. 2.302585092994046& o! U# {# n4 Q" w; I( T
  7. >>> math.log1p(10)0 @3 D: B3 M) f3 m) s5 n9 u
  8. 2.3978952727983707
    ) `! U! y" \; t
  9. >>> math.log(11)6 n+ v" v2 F2 @6 C" c( y
  10. 2.3978952727983707
复制代码

. @$ J2 R% f6 C( o' ?+ lmath.log2(x)  返回x的基2对数7 }6 [: x% G# I/ M7 w9 k! K* j; F6 u
  1. #返回x的基2对数& f; \0 D* ]" l) S" l
  2. log2(x)8 h( R% f6 C+ K  L9 f/ A
  3. Return the base 2 logarithm of x.0 ^7 a) \  G5 S! c+ d
  4. >>> math.log2(32)
    0 G6 F/ |' P; t3 F" r6 z9 @; o9 y
  5. 5.0
      k0 I. q% V% Z% W4 r! r  o9 x9 n
  6. >>> math.log2(20), i* ^+ n& z  K2 F6 e% O" e
  7. 4.321928094887363
    % x4 T( [& R. M5 {, R) \  O
  8. >>> math.log2(16)
    ; q* A- m, G& r
  9. 4.0
复制代码

: o/ Z9 X8 ]& r4 k6 t) R9 vmath.modf(x)  返回由x的小数部分和整数部分组成的元组
% M" K. U: N' O
  1. #返回由x的小数部分和整数部分组成的元组; ~% Q' c) n7 e  m! N9 t/ D* Z, Y3 H
  2. modf(x)
    . B; ^% E& i- t( n
  3. Return the fractional and integer parts of x.  Both results carry the sign
    8 s1 M) ^5 V! T9 R2 g1 }. \0 C* x0 a
  4. of x and are floats.
    / s6 a) @) a$ U8 j
  5. >>> math.modf(math.pi)$ O/ l; M, c2 m8 _$ U1 h
  6. (0.14159265358979312, 3.0)6 B: }! @  u9 b( e
  7. >>> math.modf(12.34)+ R% y9 p5 r: y
  8. (0.33999999999999986, 12.0)
复制代码
; }% `4 F% W7 y& [. O; {
math.sqrt(x)  求x的平方根, j8 y3 B: F+ H7 n4 r$ j- ^# s6 t: I
  1. #求x的平方根1 Q: k) J) V9 g! G/ U
  2. sqrt(x)
    , A/ V$ K4 p9 Z4 ^* Z
  3. Return the square root of x.& {$ ]  V' u* Y7 U' K3 |
  4. >>> math.sqrt(100)4 T9 K0 u/ u3 Y, ]9 q( R6 B. k$ n
  5. 10.0+ w1 i) a! I- ^5 a2 P8 q. E5 X
  6. >>> math.sqrt(16)5 j0 X: O7 u' m6 @
  7. 4.0
    0 R# v' w& U: \
  8. >>> math.sqrt(20)
    7 R  m: ]* \! f1 i8 g
  9. 4.47213595499958
复制代码

/ G8 o6 q4 D4 C4 l6 @math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分3 _9 w  ~; {; Y7 R% d
  2. trunc(x:Real) -> Integral
      q& j9 n# X/ \: O" `/ s- l  A
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.5 Q/ N# b/ |9 A. C6 e' w+ a# p5 I
  4. >>> math.trunc(6.789)
    & h/ v' P$ X5 G
  5. 6
    ; v* a/ K  l1 E0 H7 u
  6. >>> math.trunc(math.pi)
    $ K$ n2 c& \' X! E: C
  7. 3
    3 N7 S! ]4 H+ `
  8. >>> math.trunc(2.567)# s% t2 {6 q, ?* x+ `: L; G
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-1 06:22 , Processed in 0.075819 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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