新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

7 S0 q& o: ~9 b1 W【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: T9 y3 K: j! b2 ]9 N# p: K
9 Q. E, u  {0 z
方法1! `9 I0 Q1 v( S) S/ y6 ?+ |% L
  1. >>> import math) C* k9 Y9 O% ]; P# j
  2. >>> math.sqrt(9)
    ( `/ f2 F; ~& k+ Y  J0 }! p0 n
  3. 3.0
复制代码
方法2
; |0 |9 k; a; x) a( X1 R
  1. >>> from math import sqrt( u* K- z' k. q  A7 }6 u" m! z0 [) \: l: f
  2. >>> sqrt(9)" U2 I  M; W8 j: c7 }
  3. 3.0
复制代码

) D" h3 n& p* x8 l0 ]% b  i/ j  B: z$ R

7 H7 y1 G0 _- _$ E7 x
math.e  表示一个常量
  c- L! L2 ]: Q! C6 Q1 H
  1. #表示一个常量! `5 L, a. u* J0 z; v* t, p, A
  2. >>> math.e4 k: ^$ c! M1 _& M
  3. 2.718281828459045
复制代码
' V9 \+ z1 I- [( B+ N% M
math.pi  
数字常量,圆周率

  ?* v6 |  w5 u2 [$ F$ ?
  1. #数字常量,圆周率
    2 h, x% h$ Q: C& r1 g% Q# k& ]  I
  2. >>> print(math.pi)2 L) C. i, Z5 H8 T6 {! m! J0 ^
  3. 3.141592653589793
复制代码

3 t8 }. p# ^+ V; g$ f, u0 P4 qmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

5 P0 U( @* f+ K! g9 n
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x: x, }1 r# G* I, \% ]" ~) C. q
  2. ceil(x)7 ?8 x' o' i8 r/ d6 C- w
  3. Return the ceiling of x as an int.8 W4 ~  o4 x; A0 A
  4. This is the smallest integral value >= x.* x' t. |' p- X; o7 n
  5. # i, v: Y  s' p
  6. >>> math.ceil(4.01)+ g6 `; h3 ]5 u0 {( s' ~
  7. 51 }. U; _. Q( J: ~9 V8 b: Z4 w# q
  8. >>> math.ceil(4.99)2 x' [8 t. Y& j
  9. 5
    1 y7 b9 u- C: Q
  10. >>> math.ceil(-3.99)8 s, r/ _  B$ h9 u5 \
  11. -3
    1 D* M2 b) g+ b
  12. >>> math.ceil(-3.01)
    4 h8 r0 U, x! p. p5 {" ~; o# L
  13. -3
复制代码

6 I& Z9 k. L% Z& {+ `- k- @2 C+ Imath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& Q/ v8 `$ M, l
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    3 A" n( j" _4 c* a
  2. floor(x)  H( z7 X) c  f6 v" Q0 u, Q
  3. Return the floor of x as an int.& y& {! N% ^& R$ _
  4. This is the largest integral value <= x.
    . ^7 R3 i: p* e: Z5 u0 ^+ K4 z! m) S+ I
  5. >>> math.floor(4.1)
    5 {, r( }0 M: @+ I9 Z
  6. 40 C+ F2 G" P9 g: K: h
  7. >>> math.floor(4.999)( {& u4 w, G9 d; S3 F4 N! M6 H
  8. 4
    & z) O- c3 d5 C* \2 J
  9. >>> math.floor(-4.999)
    / V0 }$ A& g3 D& ]
  10. -5- j7 i/ K- S8 a6 t1 p1 H# t7 a
  11. >>> math.floor(-4.01)
    8 |7 [+ i) C( o  {9 T2 d$ I* m
  12. -5
复制代码
& j" A% C3 h! p/ {* a
math.pow(x,y)  返回x的y次方,即x**y
  Y8 [& a& N* ?2 f# x! J
  1. #返回x的y次方,即x**y; K/ x2 I3 m( H7 a
  2. pow(x, y)& b3 s7 E' E1 Y" L! o& K9 e9 w5 G
  3. Return x**y (x to the power of y).
    ( l" N7 j# A- g" U) Z! {0 _# Q
  4. >>> math.pow(3,4)! Y% i4 D4 ~1 ~# Y+ R
  5. 81.0: y% h+ S+ y8 M( ?4 H) \" D
  6. >>>
    . f  _) I2 B6 @0 t; ^9 [
  7. >>> math.pow(2,7)
    - X5 K) s8 r2 C- s( T
  8. 128.0
复制代码

" `' n* u! I  I8 t% ?math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): P0 I2 K, w) p6 j& h3 m* Y
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ' @. A5 S- z& V. R- a
  2. log(x[, base])
    ' a+ l. H  I+ k- V7 R) S7 m
  3. Return the logarithm of x to the given base.
    0 r0 E" ?( C  R6 ^% [
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ; e& i/ [$ V! `1 j1 z, G/ \( ?
  5. >>> math.log(10)
      Y1 ?8 {7 h+ o3 x6 i) v- W
  6. 2.302585092994046
    ) R0 P$ {( S, I- t  ~2 C, C
  7. >>> math.log(11)+ A1 }7 C" T  n7 ?7 k  F8 V
  8. 2.3978952727983707
    5 ~/ z( H* K( Z3 N) A* ~
  9. >>> math.log(20). d' q9 G2 n) @2 `7 [" j
  10. 2.995732273553991
复制代码

) Q0 Y$ E4 a, J/ e2 Tmath.sin(x)  求x(x为弧度)的正弦值2 T! D( w# @0 q% y
  1. #求x(x为弧度)的正弦值" B+ B+ ?1 F1 B* y, f0 S
  2. sin(x)
    3 z" f" o' f  q3 I$ J
  3. Return the sine of x (measured in radians).
    ! `# k% X  |" }2 s
  4. >>> math.sin(math.pi/4)
    5 K4 Z. x/ s% s( `+ f8 Z
  5. 0.7071067811865475/ Q0 o+ E6 m( E
  6. >>> math.sin(math.pi/2)% B# {& }; o) r
  7. 1.0
    0 Y3 U1 z, b# s" Q4 G6 B5 Z! y
  8. >>> math.sin(math.pi/3)
    $ g, W% L' i+ s- s' G# ]
  9. 0.8660254037844386
复制代码

3 K9 b) a( r  O" q! S+ cmath.cos(x)  求x的余弦,x必须是弧度  b- M0 {3 H6 n4 U9 a* o1 s
  1. #求x的余弦,x必须是弧度
    , _$ Y* d, ~" X% m
  2. cos(x)
    9 |0 `: |( W9 h( [4 r4 L/ z2 J
  3. Return the cosine of x (measured in radians).3 X: [& `9 `9 E+ U
  4. #math.pi/4表示弧度,转换成角度为45度1 d; G( o  g3 ]
  5. >>> math.cos(math.pi/4)
    % ?2 v! @7 G$ X+ i2 V3 Q6 u0 _' `
  6. 0.70710678118654765 f0 p; N; _( |' k# V3 h- F
  7. math.pi/3表示弧度,转换成角度为60度9 p/ a% C( B9 ?
  8. >>> math.cos(math.pi/3)
    3 ?2 o% w# U, a4 q) f; d3 f9 P
  9. 0.5000000000000001
    ( P# _) t2 B0 y: \( q. g
  10. math.pi/6表示弧度,转换成角度为30度: Q) A7 T" J4 Z% Z* P7 \
  11. >>> math.cos(math.pi/6)
    5 t  N" b2 ?" K3 P
  12. 0.8660254037844387
复制代码
- p, z0 j# k& N
math.tan(x)  返回x(x为弧度)的正切值) T# U% y% a8 h4 x- L
  1. #返回x(x为弧度)的正切值8 ~9 z2 t$ U6 q+ a+ q! J0 I
  2. tan(x)
    2 ?1 t* g9 ]' T: p9 ?( D
  3. Return the tangent of x (measured in radians).# s  u5 V' ~1 k* v4 t
  4. >>> math.tan(math.pi/4), k' n8 K  }* V( T" k
  5. 0.9999999999999999
    & J5 s# F; ?6 D# G7 z$ `
  6. >>> math.tan(math.pi/6)
    , P+ x3 d- H; o0 z# b: q1 i% V0 p
  7. 0.5773502691896257- ~' h) \5 B0 s
  8. >>> math.tan(math.pi/3)
    9 W( j- S; E" p
  9. 1.7320508075688767
复制代码

% _- }. E6 w! w: l: `; i: ymath.degrees(x)  把x从弧度转换成角度2 X8 r, ?$ s, i4 C+ f$ j4 C
  1. #把x从弧度转换成角度! M* G3 y6 F/ m
  2. degrees(x)5 b9 W3 S* U  d1 n% z
  3. Convert angle x from radians to degrees.
    : |- _; v2 R. l) p" c8 O- _
  4. : P. e- Y! u9 q; u6 D
  5. >>> math.degrees(math.pi/4). b- M2 M9 k+ ~, }5 V7 e# Z9 [
  6. 45.0
    5 S- N4 c/ d- u3 ]& H
  7. >>> math.degrees(math.pi)8 h9 ?+ a4 W& H' O
  8. 180.04 G3 k8 i: T: t' h; [; t& \- f. D
  9. >>> math.degrees(math.pi/6)) U$ J! l$ J5 n9 p7 C
  10. 29.999999999999996
    ' C9 T7 ^+ T) t" g0 c
  11. >>> math.degrees(math.pi/3)( i/ r5 `: T, n2 P8 m
  12. 59.99999999999999
复制代码
2 e% N. D; W$ B+ y6 W
math.radians(x)  把角度x转换成弧度/ Y$ z! S% b* X: K5 Z$ b
  1. #把角度x转换成弧度
    4 t" z- ]' R4 Q8 t% Q* _
  2. radians(x)
    ' y- v0 M* m- c5 U% P0 B9 n) T
  3. Convert angle x from degrees to radians.  \( l5 k) s$ J7 \% D- i  \
  4. >>> math.radians(45)# W1 ]( R$ K4 Y: {# T2 H; r1 W$ ]
  5. 0.7853981633974483
    / X) S: T0 ^' b5 h* a
  6. >>> math.radians(60)
    . a2 ?' M# k9 M7 k9 G4 z0 C6 Z# N
  7. 1.0471975511965976
复制代码
$ G0 d# y! ?, q- \+ @
math.copysign(x,y)  把y的正负号加到x前面,可以使用0- R) w3 `% C: n$ y4 V- A! D
  1. #把y的正负号加到x前面,可以使用0' Z! t# U" T( w- k
  2. copysign(x, y)5 F. ?1 S5 H  P* p- P
  3. Return a float with the magnitude (absolute value) of x but the sign
    % ^9 V: }1 Y# ]% o
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ' Q) i4 z! g* g8 C* C: ~6 N
  5. returns -1.0.- T6 o8 e0 A/ x( M. L' X
  6. : L6 J" n* J# L& }8 C' H
  7. >>> math.copysign(2,3)
    * w' n) B$ D- P- D8 w: F
  8. 2.0+ V& T2 M$ l5 H4 H$ b. n
  9. >>> math.copysign(2,-3)2 b) V5 N) |' S) d+ }
  10. -2.0
    4 c) ?* k; H$ T- b' Z3 a
  11. >>> math.copysign(3,8)' A' u! q7 v  k) F0 M
  12. 3.08 Y% G; b1 `; o0 u9 L1 X6 d
  13. >>> math.copysign(3,-8)
    0 j- J( H& x1 v4 \
  14. -3.0
复制代码

- A3 C, r0 |2 m0 N9 T& \math.exp(x)  返回math.e,也就是2.71828的x次方
" D& {, M- X, s# B
  1. #返回math.e,也就是2.71828的x次方3 c0 c5 I! b" V* `) p% p, h
  2. exp(x)
    - ^3 H% }3 F' z: N6 {% y
  3. Return e raised to the power of x.( k; A( K' D% g& y: l6 U

  4. - C/ b0 V5 n3 s! G# j
  5. >>> math.exp(1), H$ N- M& ~7 |; a" [
  6. 2.718281828459045
    8 J0 N; J, k. [" e! x0 _. T
  7. >>> math.exp(2)3 \$ I& h7 Z: ]( h' s! {
  8. 7.38905609893065! |" s- a! {- W; i$ @
  9. >>> math.exp(3)
    * p  k9 o# [' n5 [
  10. 20.085536923187668
复制代码
" V' W0 W5 O- S+ p# T
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1" j& ^0 B* o, [" F
  1. #返回math.e的x(其值为2.71828)次方的值减1
    9 E& Z: U1 e! d3 F* D
  2. expm1(x)
    # C( k$ v5 U% c6 C, r
  3. Return exp(x)-1.) b. c4 n: f# b; G
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    + i3 w6 x. z) n7 C9 v- Z0 }

  5. ) A0 m/ A: W& m
  6. >>> math.expm1(1)
    + ~  U% F8 C* M# w- B( \7 r4 s
  7. 1.718281828459045
    : q# h% D# _  U7 _, G4 I( b% h- X
  8. >>> math.expm1(2)# P$ v& r6 T0 l
  9. 6.38905609893065
    2 l" g. Y  M9 F
  10. >>> math.expm1(3)
    ) e  h. N0 o, Q4 }9 G
  11. 19.085536923187668
复制代码

7 Z  V# {8 c0 f! ?math.fabs(x)  返回x的绝对值
) O# ?5 y: ?) T
  1. #返回x的绝对值
    : J4 \* g' l, v  q9 }! n7 g/ K
  2. fabs(x)+ O3 t3 U, b  q1 H
  3. Return the absolute value of the float x.
    8 l; H- G7 P5 f1 Q% ?4 A

  4. 7 e! q3 ]) A1 T4 d4 f
  5. >>> math.fabs(-0.003)8 J6 I. I9 o! S' d* C5 X  |1 A3 P
  6. 0.003' V8 Y: A6 L! B) ^; `, j8 j8 K
  7. >>> math.fabs(-110)
    0 V. n3 x+ n' U4 E& R
  8. 110.06 H9 l9 V5 ^# z
  9. >>> math.fabs(100)9 x3 o! W) A, B2 t& M; y- D4 L
  10. 100.0
复制代码
1 k) i2 m3 |: L2 }) P% B
math.factorial(x)  取x的阶乘的值+ |/ k0 ^% j  b4 K: X7 g
  1. #取x的阶乘的值9 F) w2 C" X6 C" p( ]; I8 C" r
  2. factorial(x) -> Integral
    3 v# L* k) u0 b( F
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    2 M, W8 v& d* ]- l; z' f
  4. >>> math.factorial(1)* @/ G; V0 B/ a
  5. 1! ?3 g' d6 E2 m8 H, ^. w
  6. >>> math.factorial(2)7 W1 S2 c0 X0 d
  7. 2
    + t* o! F( R7 N+ ]' H. c
  8. >>> math.factorial(3)
    2 ^, w' M2 A4 d; c& U1 s( T9 S
  9. 6
    2 t% `7 r1 m: b1 y0 h$ O4 k
  10. >>> math.factorial(5)  n% n9 u/ s6 S6 l8 s' U; P, [; B
  11. 120
    7 m, y) p6 q! E) {& j7 G3 p
  12. >>> math.factorial(10)! H" ]3 L: t/ \) _4 E1 x5 \
  13. 3628800
复制代码

/ Q8 D8 ^6 r# ^# h0 Emath.fmod(x,y)  得到x/y的余数,其值是一个浮点数7 o5 A' u& k9 |! s4 P
  1. #得到x/y的余数,其值是一个浮点数
    * k+ m- P! ~! O9 S7 ]
  2. fmod(x, y)  w/ X+ @" D7 c
  3. Return fmod(x, y), according to platform C.  x % y may differ.) J+ m3 |- {) t" Q
  4. >>> math.fmod(20,3)% |- A7 a" v, S! D+ O
  5. 2.0
    , |! U6 `( o+ V& s6 l
  6. >>> math.fmod(20,7)
    . k$ w  w3 X" J
  7. 6.0
复制代码
2 H- Z" }3 e! \& Z. e
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围+ w- Y! ?6 |; o4 M: A) h
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    : ^% W5 e& s2 J6 F& l0 d& A% R
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ! U! t# _  K- R- e0 w+ D
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" X" w3 _% P; q- k2 |! {: X( A8 p0 g
  4. frexp(x)
    # g- s9 W% {$ T% V; g/ F0 Q& M
  5. Return the mantissa and exponent of x, as pair (m, e).0 r$ Y# x4 ^% y  r8 }0 x9 w
  6. m is a float and e is an int, such that x = m * 2.**e.
      U* u8 r2 l/ \. P* ]
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    6 c& b$ Z' Z' d) n0 o- U
  8. >>> math.frexp(10)
    1 L6 P. K3 {) v. a% i- Z
  9. (0.625, 4)
    7 F5 N  t6 a$ E
  10. >>> math.frexp(75)
    ( B( h  x2 b  K) d$ m: ?% M
  11. (0.5859375, 7)
    $ a4 o7 M! o- ^" ]0 f' y
  12. >>> math.frexp(-40); r' t+ Z7 X7 M, O1 _! D
  13. (-0.625, 6)  h4 `' B* f" h# m8 C! g6 _
  14. >>> math.frexp(-100)
    6 w8 U1 Z4 G9 c3 s
  15. (-0.78125, 7)3 ^( v- y4 Z( B9 U% F
  16. >>> math.frexp(100)) [- r' T& U8 [
  17. (0.78125, 7)
复制代码
' H; p/ Z9 ~; Y$ q
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 z6 u# |  C+ A( }7 K
  1. #对迭代器里的每个元素进行求和操作
    , a% e5 W% m  {
  2. fsum(iterable). h6 C' E: n8 b1 u7 I6 _: s
  3. Return an accurate floating point sum of values in the iterable.
    8 ~3 f. G/ ]2 ^! [4 @/ o4 ^
  4. Assumes IEEE-754 floating point arithmetic.
    % |* C+ \  |7 E# `2 x1 s2 E7 H7 X% k
  5. >>> math.fsum([1,2,3,4]); }! \+ b/ k. W/ k, q
  6. 10.0
    4 ]. q9 l, A. p  O. ]' v- a, v
  7. >>> math.fsum((1,2,3,4))
    $ L8 ]! e3 q; {. q- J% X( v
  8. 10.0! r5 z) x  s' }7 A) B
  9. >>> math.fsum((-1,-2,-3,-4))
    & v7 L! f2 V1 E  l% L  P* ^
  10. -10.0
    % w1 k! j8 H% b/ a
  11. >>> math.fsum([-1,-2,-3,-4])
    $ Y! ^; w% G6 F: c# g" K
  12. -10.0
复制代码
6 [$ G- [. N5 `2 r
math.gcd(x,y)  返回x和y的最大公约数
% p( d; [- s6 ?. _
  1. #返回x和y的最大公约数# B$ e8 r. n  b! g$ s7 C
  2. gcd(x, y) -> int' E% L; N8 D+ O* {
  3. greatest common divisor of x and y! L2 Z1 T7 F; ^4 a3 M* T- B/ a2 ~/ M: X
  4. >>> math.gcd(8,6)
    ( {) x9 B* ]3 Y9 W' {% U
  5. 2
    7 }! b& o# K/ X3 X3 ^7 s
  6. >>> math.gcd(40,20)
    + N' t" v$ V$ \; j, n: z5 }
  7. 20
    2 {  H8 H* A* ^6 g7 t1 ^
  8. >>> math.gcd(8,12)8 \% L% h; A' b; O( h, U
  9. 4
复制代码
0 k5 s, g* O) j( x4 ^: i3 n# C: l
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False; g" \- m: ~3 |, _8 r
  1. #得到(x**2+y**2),平方的值& T+ J" Y* G! I5 ~/ p/ I
  2. hypot(x, y)
    6 T% L0 G8 N: ~, |+ Q
  3. Return the Euclidean distance, sqrt(x*x + y*y)." D% B/ L* c  u. y/ h$ I8 j9 c) M
  4. >>> math.hypot(3,4)4 E, u) b& ?: i5 Q
  5. 5.0
    / O( v  |: W. ^/ F; U8 \
  6. >>> math.hypot(6,8)
    # l8 M; I1 A( p/ u) i
  7. 10.0
复制代码
* S7 M1 X- E7 L* V1 v
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False2 |! n; e6 p- h$ v: e' M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ) u9 j' P2 O) B# M
  2. isfinite(x) -> bool
    4 v& S# j& }- @+ o% N7 f4 s" N
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    " F/ a7 H, V9 T# ~6 O  E) F6 [+ P+ f
  4. >>> math.isfinite(100). H* A5 Q/ F& L- b8 T4 C7 |" ^
  5. True
    ! c; s& d- D2 K/ v/ s* ]
  6. >>> math.isfinite(0)
    0 P. O/ N* g/ G& ]! a/ [+ @5 k  h
  7. True7 ?9 X0 f7 i% K6 U; R8 B# A/ `) P
  8. >>> math.isfinite(0.1)
    $ |7 c! i4 F% u* e
  9. True
    5 [. L, A+ q' d2 B/ \3 V" H
  10. >>> math.isfinite("a")
    & W; W3 w# D+ L! o" h* X
  11. >>> math.isfinite(0.0001)
    5 V3 S4 P2 P) j( `& D
  12. True
复制代码

8 u8 l; s/ F# B* u9 k0 e/ k: lmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, d* f5 q& h0 a; z( {( e; G' D7 F
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
      i# L, p. c6 }' n' J/ r
  2. isinf(x) -> bool
    8 }. K4 C* {, x9 i# Z: f3 D
  3. Return True if x is a positive or negative infinity, and False otherwise.
    " G7 @+ U  N( N  q& ^) V
  4. >>> math.isinf(234)# ^: l5 z: ]3 \4 [
  5. False
    # L0 e' h9 D2 W- K, _  R9 M- \
  6. >>> math.isinf(0.1)
    ) S2 m6 `& ^0 Y5 q9 X
  7. False
复制代码
! x+ ^" \' N3 O/ q+ Z- Q& {
math.isnan(x)  如果x不是数字True,否则返回False2 H# e  \, F; Q* P. @9 o
  1. #如果x不是数字True,否则返回False8 ^: l" }9 ~, Z! ?5 h& K
  2. isnan(x) -> bool
    3 x6 e0 |/ X  c" |4 N# U  t
  3. Return True if x is a NaN (not a number), and False otherwise.$ ~& G5 Y) i: H2 Q9 T/ h" h
  4. >>> math.isnan(23)5 N, c3 @* k2 j9 J' P! |
  5. False4 z1 z& M) l7 L
  6. >>> math.isnan(0.01)5 y/ ~2 P# ^& j0 G& O; w  e
  7. False
复制代码
2 i, s+ \% e4 v# f9 T. h5 T4 C
math.ldexp(x,i)  返回x*(2**i)的值
9 N* P+ V( \' k8 F; k4 k" D5 Z
  1. #返回x*(2**i)的值7 x6 x6 b( a2 [* B3 g! v: S
  2. ldexp(x, i)
    2 a  M0 D- a2 @" X5 c8 Z
  3. Return x * (2**i).
    ' @0 ~0 N+ o- g. C/ r( v
  4. >>> math.ldexp(5,5)
    ( [! X) V2 N, f, b; Y& r- W- q
  5. 160.0  R8 w5 i+ j; {( R+ c7 X: B
  6. >>> math.ldexp(3,5); F( ^6 @' @6 u5 t( k
  7. 96.0
复制代码
) L: a3 i' z8 U: h2 K* y# [- |
math.log10(x)  返回x的以10为底的对数
8 m* m) B; p- E0 Q& O, j
  1. #返回x的以10为底的对数# B' e+ ?: a5 N1 C
  2. log10(x). f1 p3 l- Z1 F9 t
  3. Return the base 10 logarithm of x.
    : E$ E$ h$ X4 W5 j# M1 l
  4. >>> math.log10(10)3 X( T/ h) g/ P6 j7 M1 k
  5. 1.06 W; _2 |# E; g% n  N/ j  @
  6. >>> math.log10(100)
    ' P: p! I2 u, k2 V) ^& W8 r
  7. 2.0
    : h# `. l# [2 b( p0 E; K2 }
  8. #即10的1.3次方的结果为20
    ( v  R' V9 [. y4 Z4 Z8 M
  9. >>> math.log10(20)
    , p4 j% ?/ `' F" ~; ?: l
  10. 1.3010299956639813
复制代码
% G% S3 J( m% `4 r% j5 n5 c& E( h
math.log1p(x)  返回x+1的自然对数(基数为e)的值
! [; K9 I% J. _& O: f
  1. #返回x+1的自然对数(基数为e)的值6 K0 J; T5 F6 D% _  y
  2. log1p(x)/ r0 G" }% d6 N# N, L' v$ c* U9 g
  3. Return the natural logarithm of 1+x (base e).( x0 B' C7 S' e' D& Y# W( Q' W
  4. The result is computed in a way which is accurate for x near zero.
    , B# V1 U4 _7 m
  5. >>> math.log(10)  s* ~: |# A% \
  6. 2.302585092994046
    2 _3 x& [2 z+ L% X  X
  7. >>> math.log1p(10)
    3 S, N2 ?. P. D$ V( U. ?
  8. 2.3978952727983707; M! S3 j) s# F. V2 F
  9. >>> math.log(11)
    % S/ I7 P8 ]* R' _& i
  10. 2.3978952727983707
复制代码

- U" s+ {4 ]- r" Rmath.log2(x)  返回x的基2对数
2 K/ D3 H( s& l9 H( w# J$ I% J3 e
  1. #返回x的基2对数
    ) O7 T# W$ _) ^* _& e
  2. log2(x)2 a  F8 U: B- w' ^0 ~) x) j5 x
  3. Return the base 2 logarithm of x.
    % U  j' Y9 n. B2 h. ~, w/ ~3 p
  4. >>> math.log2(32)4 j# A, k& O1 v# s) b/ I& r
  5. 5.01 l! x  P/ D9 ^0 }$ n6 Q$ F
  6. >>> math.log2(20)
    , j: f6 v2 A+ r; \3 t  Y: v- S) m
  7. 4.321928094887363
    ( k, P5 l9 ?: Z! |9 C+ R
  8. >>> math.log2(16)* @' m# u, H) o3 k% V2 Q
  9. 4.0
复制代码
/ ]7 i* S) [0 |! Y  v" o+ N4 n- p
math.modf(x)  返回由x的小数部分和整数部分组成的元组
$ A$ d) e- U! j: y, u/ q
  1. #返回由x的小数部分和整数部分组成的元组& q3 _6 h# {$ e; l
  2. modf(x)4 R$ Y# v" u# y1 Q' K' i+ n. c) u; w
  3. Return the fractional and integer parts of x.  Both results carry the sign* E0 \1 L4 T* f, B9 ^
  4. of x and are floats.- x/ H0 T' s) W' X
  5. >>> math.modf(math.pi)
    : p' v5 P& a$ w3 n$ r
  6. (0.14159265358979312, 3.0)
      K5 d6 ^& m6 r0 U% G+ a/ A4 a& ^
  7. >>> math.modf(12.34)) h) W$ O. o. y* m- u
  8. (0.33999999999999986, 12.0)
复制代码

- _% X, w$ n, r5 m# w- Jmath.sqrt(x)  求x的平方根: }  ^3 k# |: I0 K0 ]  O
  1. #求x的平方根
    0 c) D+ s$ n. J( D3 x4 v! t
  2. sqrt(x)* J: S& m( y* w- W7 w0 U
  3. Return the square root of x.
    8 T7 z; G( [) B
  4. >>> math.sqrt(100), Q) t+ r3 \+ ^, y/ v( w$ H' i
  5. 10.0
    % j0 P) L; G' B/ h& w: f1 d# S
  6. >>> math.sqrt(16)0 ~5 l' W& h7 |6 n6 C# k
  7. 4.0# Q& O+ l/ p7 ^+ ~
  8. >>> math.sqrt(20)
    9 k- f' x" v1 s6 c
  9. 4.47213595499958
复制代码
# n  z( ?9 p' V7 G
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    6 W! `# N- d' W" w2 }. Y7 U
  2. trunc(x:Real) -> Integral# q  |9 X  m8 s% ~" |+ p
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ! i; o3 o$ H1 A+ e, j2 r
  4. >>> math.trunc(6.789), E2 W: x: z0 W9 l% \. Z
  5. 6; O8 y  d, k9 }4 r# ^
  6. >>> math.trunc(math.pi)
    ) J+ f( [# Z/ A2 x% H9 q% o
  7. 3
    + h, k, }& T* A5 D7 S0 T
  8. >>> math.trunc(2.567)
    - ~' A: x# _$ u+ A& g8 c! k
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-4-17 07:19 , Processed in 0.083030 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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