新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
# M1 x2 ?, J" |' ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: P5 }4 E! x2 N' J6 u0 M

2 M+ Q! [8 v3 V- A" [+ F: U/ M方法1- j5 Z/ N6 z# Y* S9 B
  1. >>> import math
    ) i4 O- [8 o% f) t! S
  2. >>> math.sqrt(9)
    7 P' M9 V5 J5 G5 ]# ^5 H
  3. 3.0
复制代码
方法2# s+ y/ q; _  @7 m" w: t
  1. >>> from math import sqrt! l' R7 u/ C" r
  2. >>> sqrt(9)! m: A; y/ @* _2 v3 k
  3. 3.0
复制代码
- G( L; u1 q$ t: Y: \' r# M. ~4 R7 R* X

, U  z* F$ R! ]7 N- [# n4 |% I( \
math.e  表示一个常量
* b% K  V2 z) I9 g: a
  1. #表示一个常量
    - [$ A: O5 `3 F0 n" g7 ~  m
  2. >>> math.e: m0 }* ?' T0 A/ g6 r5 h
  3. 2.718281828459045
复制代码
/ v8 |6 [+ u+ Q
math.pi  
数字常量,圆周率
6 {$ s6 ~1 F* o% g( x* H
  1. #数字常量,圆周率
    % I  @$ t- P: [/ ~& J* p# H
  2. >>> print(math.pi)! z" {+ W9 R; \3 W6 ?/ p
  3. 3.141592653589793
复制代码

6 j2 t0 J8 F: v" b$ D7 X5 amath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

& D+ V) l- W' _% @4 J; ?) z
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    - `3 [$ Q+ X9 C6 Z6 P) O
  2. ceil(x)
      e, ~4 y5 r) w' j+ {
  3. Return the ceiling of x as an int.  e. Z/ ~0 C  m* X' k6 L
  4. This is the smallest integral value >= x.
    # ]5 W, R4 M& f( Q! e+ s8 h

  5. $ m! A  R. a3 B* K3 F5 ?  b5 Y5 B
  6. >>> math.ceil(4.01)4 r7 A% [* w8 D$ _' R/ f
  7. 5" b9 A8 @8 j5 ]3 `5 M- u1 y4 c
  8. >>> math.ceil(4.99)
    $ Z3 W$ P* H/ W0 d, u) v
  9. 5
    - ^6 I" S+ V# K: k
  10. >>> math.ceil(-3.99)6 B+ ^6 F: n4 g7 U
  11. -30 l! U- t4 w9 G* s% _
  12. >>> math.ceil(-3.01)
    7 A7 X7 u. c3 }. \# m4 H) {( ~
  13. -3
复制代码

9 r  X/ x; B, wmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身. c# P+ @4 n- O/ B  `
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ k) _/ T! H- R, O
  2. floor(x)
    ! X. i- A# u! }* A8 z
  3. Return the floor of x as an int.0 S: S6 Y. Q, k; ^0 U5 b- f' k
  4. This is the largest integral value <= x.
    , H( e( l, L8 c' ]( q& X
  5. >>> math.floor(4.1)
    / q2 @/ c% H; L: D6 \
  6. 4
    + b5 ?5 L9 l" j* J/ N0 C
  7. >>> math.floor(4.999)( E% f6 U4 e4 q! _! m1 J
  8. 4
    ' G8 `( J$ G2 z( V& d5 i; L# @
  9. >>> math.floor(-4.999), {% }2 w* k0 h3 e1 w' h, S
  10. -5$ Q1 Q" E8 c  B3 `+ D! L* b# ^4 W$ `
  11. >>> math.floor(-4.01)( [# x. a0 |* n. t( f! h5 g
  12. -5
复制代码
" V" ]" v' D" b0 O0 Q' q
math.pow(x,y)  返回x的y次方,即x**y
& Z' @% U* U# F; f! Q3 c
  1. #返回x的y次方,即x**y* U0 g* X' ?+ F: r/ d
  2. pow(x, y)
    / m% g. R  j3 E8 H8 \
  3. Return x**y (x to the power of y)." p+ X7 k$ ?* c, @8 J& f) Y
  4. >>> math.pow(3,4)
    , L; t7 x7 k" |3 J
  5. 81.0
    % P* e: b/ ]! N3 J8 e7 ^  }6 ^: H6 P
  6. >>> 2 X  Z) U) l. b$ k  J& L8 T7 G' {  b
  7. >>> math.pow(2,7)2 W; i7 s' P' \# _2 m+ F
  8. 128.0
复制代码
) o$ W1 N0 V, f& ~; @
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 g& @3 s8 `: H2 R$ C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* X: T" `4 r/ t* Z
  2. log(x[, base])
    : V( a+ H. I, B% z
  3. Return the logarithm of x to the given base.% f5 n9 N% l& x8 C3 e
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ F% k* Y  g) [* W  ]5 J
  5. >>> math.log(10)# v/ a% u  s* ]4 p
  6. 2.302585092994046
    5 j/ {% P9 v6 e' o4 }, p- |& t
  7. >>> math.log(11)
    4 C9 F; Q' F( J7 y$ c5 V: n8 B
  8. 2.39789527279837073 k7 ]* _, g( i1 X0 g" K
  9. >>> math.log(20)
    , {2 d- q0 N; Q" v  A
  10. 2.995732273553991
复制代码
- [1 U1 q, C3 ?4 O" z4 {! P8 `
math.sin(x)  求x(x为弧度)的正弦值. _! O. n* S9 ?& v
  1. #求x(x为弧度)的正弦值9 V- W5 S* y! G7 Y! }' |
  2. sin(x)+ m9 {) l) c: p/ N
  3. Return the sine of x (measured in radians).+ z. ?. n0 |7 m& y
  4. >>> math.sin(math.pi/4)! r' B( O7 G) X( r- D2 I
  5. 0.7071067811865475
    , o9 G( f. @5 i" n6 T1 F
  6. >>> math.sin(math.pi/2)
    1 X+ B" F6 ~5 \- ]- j$ e
  7. 1.0* p: e# e3 T- d% G# D0 V$ b$ f% |
  8. >>> math.sin(math.pi/3); I5 m4 A' L: P' Y1 D
  9. 0.8660254037844386
复制代码
4 |. ~3 s1 q0 R/ f4 P
math.cos(x)  求x的余弦,x必须是弧度% Y% P  S7 o2 o* m
  1. #求x的余弦,x必须是弧度+ v" J) F% N% I7 w$ V- B% e
  2. cos(x)
    + N3 J! P5 O) s; l5 F
  3. Return the cosine of x (measured in radians).) }0 w* z5 S* r
  4. #math.pi/4表示弧度,转换成角度为45度8 X3 `% X7 g1 Y& ]/ T2 I3 X
  5. >>> math.cos(math.pi/4)7 H4 C- t# C+ t. R1 M) G1 j
  6. 0.70710678118654761 n0 Z' P1 q& X8 I  f4 ~
  7. math.pi/3表示弧度,转换成角度为60度
    ) V, x; f% ]# c2 p2 R. T: x
  8. >>> math.cos(math.pi/3)
    ; w1 {0 @3 x* q: q! t- l
  9. 0.50000000000000013 g+ I5 G2 o4 v. \
  10. math.pi/6表示弧度,转换成角度为30度0 |" `1 F4 n" e! Z
  11. >>> math.cos(math.pi/6)
    ! D& y. |' r$ K4 r
  12. 0.8660254037844387
复制代码
. j5 ?; H, x  O+ K5 o; m
math.tan(x)  返回x(x为弧度)的正切值- W  ~/ A5 P" }8 r7 B
  1. #返回x(x为弧度)的正切值
    3 @6 o4 P" p) i+ h) q
  2. tan(x)
    ' O- _0 J2 U$ a' q- }0 g
  3. Return the tangent of x (measured in radians).
    ) W  z) {- e/ i9 n9 l1 T7 [- `
  4. >>> math.tan(math.pi/4)- W) a2 R1 w6 r) H+ ]: J" N
  5. 0.99999999999999991 P, y7 [0 W* A. F
  6. >>> math.tan(math.pi/6)
    % I0 O3 I- t3 Z4 I
  7. 0.5773502691896257
    : r2 h- e, Y: z
  8. >>> math.tan(math.pi/3)! {# n1 t  m2 T# ~6 `7 @
  9. 1.7320508075688767
复制代码
# v7 o' F7 q4 d$ a: n
math.degrees(x)  把x从弧度转换成角度2 O: Y1 ]& r& Q' L  W+ `# J
  1. #把x从弧度转换成角度9 X2 E  J* }: H" e5 O5 S
  2. degrees(x)
    5 Z8 Y$ P" O0 [' b
  3. Convert angle x from radians to degrees.
    $ e* F, d" L# R: j2 ]( }$ j

  4. 3 r: m7 T6 [5 a  D7 [
  5. >>> math.degrees(math.pi/4)9 I0 ]! B# Z/ o# M
  6. 45.04 i: v7 t2 ?& T: _0 w2 X
  7. >>> math.degrees(math.pi)
    9 `# N: r' n! q: ~
  8. 180.0* @0 N; I+ h! e8 ^: R( q. @
  9. >>> math.degrees(math.pi/6)
    6 Q/ G) m& H8 u9 }( X+ S
  10. 29.999999999999996
    : h, Q* w& X+ r+ K
  11. >>> math.degrees(math.pi/3)
    / y, o3 f, r! I, \) [! K
  12. 59.99999999999999
复制代码

2 j/ V6 o/ x" imath.radians(x)  把角度x转换成弧度9 I! [8 o5 o7 A9 {7 Z) V
  1. #把角度x转换成弧度% ~3 t( ?* b% V
  2. radians(x)  j+ S" z! @/ c4 I8 L& C! m
  3. Convert angle x from degrees to radians.
      X  e7 p0 q6 }- @- J
  4. >>> math.radians(45)
    ( O* f+ F& V0 Z9 Y9 r; |
  5. 0.7853981633974483
    - ], d2 L% C! [
  6. >>> math.radians(60)
    7 l5 @8 z0 N0 B3 q" v
  7. 1.0471975511965976
复制代码
2 g: U; _# P1 K6 _
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
) b* A$ a3 V' @, y! U
  1. #把y的正负号加到x前面,可以使用0
    0 P  ~0 |$ W9 B* b. \/ o$ G$ O. `/ Z
  2. copysign(x, y)
    : }: N( {. A( s. w+ s& H" B# V' S
  3. Return a float with the magnitude (absolute value) of x but the sign 8 }4 ~! R! y! C% l* F# {
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) , d1 l6 s% `  k. p, U4 \: ~
  5. returns -1.0.
    ! {+ B- ~) E# T2 f

  6. 1 ^. Q- J3 E( n: J$ U1 ^" U
  7. >>> math.copysign(2,3): d8 p2 E7 ?) t/ |
  8. 2.0
    . Y- c5 f: g, O  @3 J
  9. >>> math.copysign(2,-3)
    ) i/ w8 M& t* @0 j. P1 i2 P
  10. -2.0: l0 [4 F2 ^& [( R/ I
  11. >>> math.copysign(3,8)
    ) ]8 `" ^- c2 Q3 N& e
  12. 3.0) n$ Z% M/ y/ ^9 `
  13. >>> math.copysign(3,-8)
    2 v' s# M$ s! M0 E  g
  14. -3.0
复制代码

3 I+ ^! m8 x+ f' X6 ]: s) Rmath.exp(x)  返回math.e,也就是2.71828的x次方
8 S1 F: x4 F5 M
  1. #返回math.e,也就是2.71828的x次方
    ; C# y; A, i/ J* P
  2. exp(x): O; o3 g1 ?, [9 ^, X  G
  3. Return e raised to the power of x.
    7 _$ n7 L* t2 V
  4. ) j! f, x6 O- G) b
  5. >>> math.exp(1)
    7 g% J: l: p8 @6 ~8 K, R) s! k& z
  6. 2.718281828459045! \$ |* S( B. o  l% |; ^
  7. >>> math.exp(2)
    - E" _0 z8 Z; O! b* x0 t
  8. 7.38905609893065
    2 |# S+ n. ]2 e& o6 M# u" S
  9. >>> math.exp(3)
    % O' A$ t. `) h# {
  10. 20.085536923187668
复制代码

- y* j( t2 w, b" a! ?math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
7 |; W4 D: Y- Q% Y* D% F
  1. #返回math.e的x(其值为2.71828)次方的值减1
    $ b5 U9 K4 N. A3 A' L
  2. expm1(x); i, i; n, P7 d2 [+ J: f
  3. Return exp(x)-1.
    . Z) R7 ?2 B9 K: Z5 y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    + `* F& n* e4 r6 j

  5. % s5 E8 F+ \: F' [
  6. >>> math.expm1(1)
    & J9 l$ W$ r5 l5 B- F% Y0 C) P
  7. 1.718281828459045
    0 O6 W6 x8 u3 t
  8. >>> math.expm1(2)3 L4 u3 p- S# A2 Y
  9. 6.38905609893065
    / u5 F3 R3 Y8 ~# v6 f
  10. >>> math.expm1(3)2 V% i, X; p8 [. c. j- {
  11. 19.085536923187668
复制代码
. V3 q- k7 O( n2 w% v5 z$ z
math.fabs(x)  返回x的绝对值
  h! @9 k$ D$ X0 j' U0 P# \7 O  P
  1. #返回x的绝对值
    $ z2 e1 z; l  G
  2. fabs(x)& x. `' ]1 v1 O. A4 j/ q/ @
  3. Return the absolute value of the float x.5 S. \7 B2 S8 C
  4. 3 O9 s. a" F) e) A- D
  5. >>> math.fabs(-0.003)# S4 p3 y4 B' `( D
  6. 0.003, q+ v3 A  \$ V5 e: A
  7. >>> math.fabs(-110)% ^" e' ~- ~& K! j& @
  8. 110.03 z4 z' G% a8 W8 f% I
  9. >>> math.fabs(100)
    7 @; C) T% y4 E, L* O* G/ ?% l
  10. 100.0
复制代码

9 n. z8 e7 v, a* U/ @math.factorial(x)  取x的阶乘的值, x1 N) B. F) ]8 l
  1. #取x的阶乘的值
    9 j6 J) {$ J; o" f5 n
  2. factorial(x) -> Integral; Z1 l( k9 x+ }1 ^1 X* ~. @- j
  3. Find x!. Raise a ValueError if x is negative or non-integral.  t( x9 R' s1 J
  4. >>> math.factorial(1)$ z! p' Z& f* \8 |' X" A. a
  5. 1
    " P' u/ \1 c2 H% h' z6 L3 e$ q
  6. >>> math.factorial(2)% A2 i% Z: ?2 d( Q4 ?" }
  7. 23 b' _0 I  M1 x
  8. >>> math.factorial(3)
    % W" d4 }4 q/ H3 P8 c( w
  9. 6
    ' `+ W) w+ i  {$ T& a+ o
  10. >>> math.factorial(5)( ^* a! I7 I( V1 [7 d
  11. 1205 S$ g) S% I, ?2 i' _. h- b, Z) w& I" F
  12. >>> math.factorial(10)
    1 c- w+ h- [4 I- o! b
  13. 3628800
复制代码
6 P. v$ i. y9 ^/ J% Z% ]
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数0 j6 T  R$ f  `8 W
  1. #得到x/y的余数,其值是一个浮点数$ y9 g9 V1 d& k9 R2 D3 B+ Q
  2. fmod(x, y)
    1 h1 b; ~) a0 Z! u  D, }# I' G: D
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + n& f! I% d6 U% E: Q
  4. >>> math.fmod(20,3)
    / O  t( K/ p" U# _
  5. 2.0
    " u9 N  p1 P, p
  6. >>> math.fmod(20,7)
    ! [- o2 V( S( ?7 M+ _
  7. 6.0
复制代码
9 r/ _$ U7 X! A$ K$ O' j
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 ^( _  ?& }% w' _
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    - B4 V8 Z4 L3 ~" m4 k5 o. ~. W
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值) s# b4 T* y$ x( E/ u: p
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 q. U  s# s& R6 b4 g% P+ P
  4. frexp(x)
    5 @9 g, h4 b7 T6 K5 N5 _
  5. Return the mantissa and exponent of x, as pair (m, e)." |" R& f' w5 U& c8 V
  6. m is a float and e is an int, such that x = m * 2.**e.7 s7 I: L' i' Q; A# [8 ^
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    - }# A6 d" h* r* p
  8. >>> math.frexp(10)
    ! d3 F9 o7 d4 j; X
  9. (0.625, 4)
    3 H7 @" x$ h( k8 Q! G3 C
  10. >>> math.frexp(75)
    ( J- _+ j' s% B. ?* h' L
  11. (0.5859375, 7)5 l) p8 S9 F2 a5 V, _, e/ Z0 H$ A
  12. >>> math.frexp(-40). y4 Q  q1 z/ k% V1 J  Q! g
  13. (-0.625, 6), G0 T0 h( l- R7 \: k
  14. >>> math.frexp(-100): K5 Z+ Z" F4 R0 K; W& |
  15. (-0.78125, 7)" D$ ^8 e/ Z2 t: b
  16. >>> math.frexp(100)5 k2 k) B7 D* |% X3 R. i/ O
  17. (0.78125, 7)
复制代码

0 `2 Q( c0 j( kmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
' b$ t5 |5 M3 x2 I& ^
  1. #对迭代器里的每个元素进行求和操作
    9 N3 h9 f; x( [" f3 @
  2. fsum(iterable); S. ^8 L1 I  ~* b& o
  3. Return an accurate floating point sum of values in the iterable.
    6 J, ^; J' R$ b7 x+ ?
  4. Assumes IEEE-754 floating point arithmetic.
    & C6 O, W" @+ {* R  @, R" }; T3 S9 w% t! U
  5. >>> math.fsum([1,2,3,4]): |* i+ L6 L/ u0 d/ A* K/ B/ V/ Y. _
  6. 10.0
      _) s: p! b( @: G' q
  7. >>> math.fsum((1,2,3,4))+ a1 B& l  J9 N. ?
  8. 10.0
      j, g; g& ~" _, }
  9. >>> math.fsum((-1,-2,-3,-4))
    9 W* s5 L3 A4 \( s4 b1 r
  10. -10.08 ~6 K' Z8 y. |; ?% X
  11. >>> math.fsum([-1,-2,-3,-4])
    + X6 p& y, l9 I3 x8 Z
  12. -10.0
复制代码

% ]" Z2 N+ J) H. v) U, fmath.gcd(x,y)  返回x和y的最大公约数3 F/ _. X6 Y1 o# g( V, p
  1. #返回x和y的最大公约数: m1 ]# m+ x  ]
  2. gcd(x, y) -> int
    3 W& D) T- u: V" ~
  3. greatest common divisor of x and y
    1 C2 Q- n  f+ i+ S3 g0 i3 |
  4. >>> math.gcd(8,6)
    9 w; J: i/ }/ x0 d- R# a# m
  5. 2
    / b$ ?' F& V1 u6 Z3 e3 K6 n
  6. >>> math.gcd(40,20): O0 o- ?" \4 P( y$ R: J
  7. 20; W) m0 o, I/ M6 p
  8. >>> math.gcd(8,12)
    0 x6 B! u% O6 M) q
  9. 4
复制代码

0 F. o8 O' p( w$ xmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
0 J! O) \' d9 M+ j" @8 h& J
  1. #得到(x**2+y**2),平方的值
    ; G- n5 O1 c1 s7 B
  2. hypot(x, y)
    + A8 F2 S( D. k2 W+ Q3 i
  3. Return the Euclidean distance, sqrt(x*x + y*y).% e; C7 }1 |0 L9 ~
  4. >>> math.hypot(3,4)
    1 ?, a% R( Q$ V7 W
  5. 5.0
    8 P; j9 \7 D( g. M3 n; u" A% `
  6. >>> math.hypot(6,8)! i3 Z7 k* ]5 J1 C" \) J: E; C$ k
  7. 10.0
复制代码
+ d+ q: W4 n4 `+ f, F; ]
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False1 U* }4 e9 C. e! H; q; @' C
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    $ S0 p' b+ f" U
  2. isfinite(x) -> bool! v6 |4 E- a6 y
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    6 h1 U$ p* T3 D2 c7 s4 u* K
  4. >>> math.isfinite(100). r/ y3 J  Y) \" e8 M
  5. True2 ]/ j# i+ `# \  [# ^
  6. >>> math.isfinite(0)6 T/ h" k5 P1 G; r
  7. True
    4 m8 j6 n$ a* Y5 K
  8. >>> math.isfinite(0.1)  e5 f. ^" ?8 i- ], ]+ z6 y
  9. True& \$ \8 }9 f9 a
  10. >>> math.isfinite("a")
    - x2 f' J% T% S0 z2 S7 S( j0 v
  11. >>> math.isfinite(0.0001)
    $ g) q. x3 N7 J: O* o+ r
  12. True
复制代码

; s$ f+ e9 `# B* jmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 s" _0 |+ v: s2 P4 |  r1 _0 F
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False1 P3 k% ]' @) V  [
  2. isinf(x) -> bool
    5 j5 ~* T9 u. v0 R7 a) O$ N4 U
  3. Return True if x is a positive or negative infinity, and False otherwise.' f5 }8 r3 @; r) l* b" |
  4. >>> math.isinf(234)
    ! x+ c6 R* ^& q2 D; \' ?: ^
  5. False% I0 N1 T3 E8 J: Q
  6. >>> math.isinf(0.1)1 d5 N' g5 Z4 e& O
  7. False
复制代码
& M4 z) H3 Y, B
math.isnan(x)  如果x不是数字True,否则返回False
, v- A4 X, @" ]
  1. #如果x不是数字True,否则返回False. j, I# i' I, ]) e
  2. isnan(x) -> bool
    8 }+ \# {& {. W3 v& K9 B  G$ g
  3. Return True if x is a NaN (not a number), and False otherwise.
    8 e2 q* H) R/ `' A: c3 c3 o
  4. >>> math.isnan(23)
    - P7 y8 {( R. a" z5 ~$ C
  5. False
    ' I) o6 J. J& G1 y" @. V
  6. >>> math.isnan(0.01)
    6 R7 q5 \) m0 l- O5 D
  7. False
复制代码

7 \' N" `: W$ x1 P  f- lmath.ldexp(x,i)  返回x*(2**i)的值
- r" R/ a! a0 n0 i
  1. #返回x*(2**i)的值
    * l0 w) O, M. a6 S2 y! s
  2. ldexp(x, i)
    6 f' E8 R2 B8 }$ N* _  y$ l6 Z
  3. Return x * (2**i).3 R1 u2 K: Y) m
  4. >>> math.ldexp(5,5)
    ! N, V, B& I1 ?! q
  5. 160.0! c2 x3 a& v5 g7 [
  6. >>> math.ldexp(3,5)
    " W/ Z8 x2 H" ]+ ]  m
  7. 96.0
复制代码
5 U/ X- ?5 |* ?: x
math.log10(x)  返回x的以10为底的对数
. c# K* V2 S8 e, `& V9 T
  1. #返回x的以10为底的对数, {" L7 k$ N' M
  2. log10(x)( c0 M3 {. H$ j0 m8 Y' b* L
  3. Return the base 10 logarithm of x.
    0 c. h. c' Y0 Y; }7 I
  4. >>> math.log10(10)" m& i0 f9 c9 ]8 d& ^$ f
  5. 1.09 _- p1 s  N2 |
  6. >>> math.log10(100)
    2 C8 l0 E* T. }/ |- G
  7. 2.0: C! M6 m. [, g: o- D
  8. #即10的1.3次方的结果为20
    ( h/ h- [# B* {& Y! X3 x$ b
  9. >>> math.log10(20)1 f; U- n, K' m7 N8 u1 a
  10. 1.3010299956639813
复制代码
8 v$ j9 z0 B7 W2 n8 `9 e
math.log1p(x)  返回x+1的自然对数(基数为e)的值
3 Q- i9 Z3 E% ^: u, q& w
  1. #返回x+1的自然对数(基数为e)的值, G& X! T+ ]5 `9 m- k
  2. log1p(x)
    6 ^, Q4 a- C! `# U5 c& w0 T0 Y0 B
  3. Return the natural logarithm of 1+x (base e).& Q6 @4 X6 a! F4 W% H* c7 x
  4. The result is computed in a way which is accurate for x near zero." _' }' w0 i5 \2 [( U
  5. >>> math.log(10)
    4 Y, e0 H" b  v2 [4 F
  6. 2.302585092994046
    8 O7 M# v, }2 t& Z* b6 d, K* c" w
  7. >>> math.log1p(10)
    5 q& _1 W2 t0 z
  8. 2.3978952727983707
    3 m0 k3 G' J0 {$ n, V2 J1 v
  9. >>> math.log(11)% f/ R4 Q; u$ m! D( ]/ R
  10. 2.3978952727983707
复制代码

  g3 N6 J1 C9 H* ymath.log2(x)  返回x的基2对数
9 D0 A( }; N& U: y( n) g
  1. #返回x的基2对数% G; l6 g, e3 q5 p/ M+ h2 z
  2. log2(x)
    " v% z  H. }  ~* J
  3. Return the base 2 logarithm of x.0 h/ {' ]0 E2 N" H
  4. >>> math.log2(32): _3 R* y$ ^! ^* [* @2 @
  5. 5.04 h, q" |2 I1 y- ^  u2 F
  6. >>> math.log2(20)
    & L8 F" l9 C; x8 C
  7. 4.321928094887363% y2 t6 H- ~6 l, E2 e7 B6 K
  8. >>> math.log2(16)8 q8 t3 Q7 ~8 \5 e- H$ r# |3 C
  9. 4.0
复制代码
( g8 {/ H) t3 N# o
math.modf(x)  返回由x的小数部分和整数部分组成的元组
, }9 X! K" h; ^! @# G2 o
  1. #返回由x的小数部分和整数部分组成的元组. a  J1 C+ K3 \4 d% J
  2. modf(x)9 B. q+ K2 I' L* Q
  3. Return the fractional and integer parts of x.  Both results carry the sign
    1 K( l4 S% I- r
  4. of x and are floats.  u) I6 B, C9 n% l$ l' m% u
  5. >>> math.modf(math.pi)8 b5 O- u4 e+ Q( n5 f
  6. (0.14159265358979312, 3.0)
    # H% w4 I! C+ @" W$ h
  7. >>> math.modf(12.34)
    & P, J  y9 B4 q
  8. (0.33999999999999986, 12.0)
复制代码
0 O% y& c' `; K- B0 E( B
math.sqrt(x)  求x的平方根
# y+ s. \+ z3 L" i8 @
  1. #求x的平方根
    % W$ |8 D3 Q$ H! p7 f+ r) H4 k
  2. sqrt(x)
    9 M4 {9 {' f# S8 }
  3. Return the square root of x.
      h9 G6 C# j3 M2 M/ D' a. o
  4. >>> math.sqrt(100)' s7 o( g/ M. l; D/ g- t/ h, M
  5. 10.0
    + e; O3 c' y" \' n! i0 A
  6. >>> math.sqrt(16)
    0 G7 H  X+ q1 d  E
  7. 4.0  R) M) T5 ]/ ~4 Z2 l! t
  8. >>> math.sqrt(20)
    6 K/ B& U! u8 p  P" b2 k
  9. 4.47213595499958
复制代码
7 l. F4 b) _/ j
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分! u2 \1 \. c' n* @% A4 {" |% P
  2. trunc(x:Real) -> Integral
    & i+ y8 ~) X' u& `& H! ]3 E% l
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ( Y  O% ]- ~( {; Y7 O4 j& ~
  4. >>> math.trunc(6.789)4 j% ~& N* Y$ U8 u% [5 g
  5. 6
    & u9 N& l3 z* W! a1 F
  6. >>> math.trunc(math.pi)
    ; k9 G8 k/ I5 @
  7. 37 O+ o, v/ `9 k% w1 A
  8. >>> math.trunc(2.567)
      b* O) o! n" T6 |: C: i9 X$ L0 U: D
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-2 17:45 , Processed in 0.089722 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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