新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

8 C$ T+ S- ^& o1 k0 w【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. M+ H* a7 }( u/ o
7 n* s- `9 Y6 u* Z. [/ ~$ J9 H
方法1
. r) ^& i& Z$ b0 ]/ g' e3 b
  1. >>> import math
    1 ]' x; J" o3 h- H& k0 C! R6 s! \
  2. >>> math.sqrt(9)* v, ?7 b% R) b# K7 \" o+ [
  3. 3.0
复制代码
方法2
, f$ [; Y  K/ |/ B: d2 l, f
  1. >>> from math import sqrt
    5 z2 A- M& ?9 [; E( S/ L5 p" P, c
  2. >>> sqrt(9)$ T8 d/ \" z, S+ S. h) J
  3. 3.0
复制代码

6 l2 Y1 N; I) p0 x" r

1 e. {0 _7 q8 ?$ R
math.e  表示一个常量8 ?6 c: Y& ~; B" B
  1. #表示一个常量
    # F0 _- p% p( G$ O
  2. >>> math.e
    7 w9 G) S+ c8 o) Y  e8 J
  3. 2.718281828459045
复制代码

5 }5 ~; d0 G+ P" W1 omath.pi  
数字常量,圆周率

4 c! @5 k) O5 z8 G
  1. #数字常量,圆周率- K3 A0 I6 V0 m6 ~2 e2 E
  2. >>> print(math.pi)
    " ]/ B1 r7 [- S
  3. 3.141592653589793
复制代码

. R7 z# b% U2 A& {math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ O! S6 E* _. P. p0 @
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 b% R; e1 L0 [
  2. ceil(x)
    ! B: D, L7 j' j
  3. Return the ceiling of x as an int.1 F" _$ s/ m- V+ u+ j
  4. This is the smallest integral value >= x.
    0 M2 _, r' t" z) ~; g  U( e- ]

  5. / p' s# j* [0 {
  6. >>> math.ceil(4.01)0 T3 J1 k1 |9 H
  7. 5
    8 u" q" g( G. X4 _! m0 {
  8. >>> math.ceil(4.99)$ ]* S3 W* |* \$ O4 g
  9. 5
    4 R5 Q& ~. O$ q: S( P; ~3 {) y! N' l
  10. >>> math.ceil(-3.99)% R- |# C) H& Z4 s; q, ^" q
  11. -3
    " ~0 N/ @: B( b2 [, ~7 b
  12. >>> math.ceil(-3.01)
    2 l' ]) `! I* |( W8 b
  13. -3
复制代码
, O- ~, Y4 _4 N/ T% ?& J- E: [6 j
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 z6 y- |% M( g# w1 U8 Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    % t, M  G* L+ x6 {
  2. floor(x)
    2 o$ J/ A6 x# n9 ]& m- D
  3. Return the floor of x as an int.1 B8 u* V2 ~4 Z  R$ h6 l* u
  4. This is the largest integral value <= x.
      r. j* }  o+ z. y& [
  5. >>> math.floor(4.1)1 ]3 k/ k- s. v! r4 n2 w, z
  6. 4
    ) L7 `- e5 M* [& O: P2 S! z
  7. >>> math.floor(4.999)  c( A0 f: K# S0 c
  8. 4
    : s2 p3 V$ c7 ?7 P7 N& M* U
  9. >>> math.floor(-4.999)1 \  ^5 j' u# }/ @* q0 x
  10. -56 K( F+ m/ W; @5 {9 ~: @
  11. >>> math.floor(-4.01)9 `) V2 Q) X/ B' s
  12. -5
复制代码
: u" W5 I0 C. t9 u) L6 S) O: ^
math.pow(x,y)  返回x的y次方,即x**y
  P3 \2 {8 \" _* O
  1. #返回x的y次方,即x**y7 o! J2 e% G) S3 U. k
  2. pow(x, y)
    8 O. |1 O0 a) X% z: X& y
  3. Return x**y (x to the power of y).4 _- |& U# }3 C, `0 }  |
  4. >>> math.pow(3,4)
    $ u. \9 _3 t. {2 o
  5. 81.0
    , L0 K8 s! k& T/ J* h0 ^
  6. >>>
      y4 e6 M* O* N' V* e+ z) ]- A6 o; U
  7. >>> math.pow(2,7)
    & b7 p2 |  D4 x! E5 h
  8. 128.0
复制代码

8 T4 v( Z( {# Z( h) b3 E: x5 o9 r! omath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 |8 b8 W) R0 U$ ~6 }2 C" E; E7 P" j
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    1 h: ^' L9 v; u$ f+ p+ K
  2. log(x[, base])( R1 P/ {- ?2 K4 r9 G* N
  3. Return the logarithm of x to the given base.
    , ~3 U: C" K& Z! f( H( Q2 e
  4. If the base not specified, returns the natural logarithm (base e) of x.' O) L% p' l1 |. ]4 T
  5. >>> math.log(10)0 L& M# o* k5 D% W5 i; |# h
  6. 2.302585092994046
    # T5 e9 F% o7 n3 a
  7. >>> math.log(11)  ]' Z! F2 u, s1 j+ T) C5 @5 v  j( j
  8. 2.3978952727983707
    ) ^/ _3 {+ M  [' ?$ P
  9. >>> math.log(20)
    9 F' [7 O4 \2 V  I, H5 r+ ]- N
  10. 2.995732273553991
复制代码
) K9 [, \0 D1 d( {3 C* J8 P; p% I
math.sin(x)  求x(x为弧度)的正弦值; E* ]* \: j+ h
  1. #求x(x为弧度)的正弦值
    + l0 o; H7 F- `7 O) g- U
  2. sin(x)
    % {! T7 u5 Y0 O* f4 l4 \
  3. Return the sine of x (measured in radians)." d7 Y/ B' r/ K6 S3 N3 K( j6 W$ f
  4. >>> math.sin(math.pi/4)  V/ s% C3 z- `( r
  5. 0.7071067811865475
    3 a9 n( F+ A* v7 z1 e, G
  6. >>> math.sin(math.pi/2)
    $ q+ n/ z: X/ T5 }
  7. 1.0
      a9 y$ \4 v2 h* `' v3 H
  8. >>> math.sin(math.pi/3)
    : B; P1 d+ k0 b9 ?3 J+ F
  9. 0.8660254037844386
复制代码

. L# `2 r% J* S% y% cmath.cos(x)  求x的余弦,x必须是弧度
+ P' q% Y( I2 B" t; ?5 e
  1. #求x的余弦,x必须是弧度: L- u3 A# ]' q
  2. cos(x)
    3 w1 u4 }( a3 p% f3 V. E
  3. Return the cosine of x (measured in radians).# l1 S. x0 ~& H0 v& O
  4. #math.pi/4表示弧度,转换成角度为45度
    8 {' B5 K0 h4 Z+ ~6 v  h
  5. >>> math.cos(math.pi/4)9 v6 h) Q9 m& c, R
  6. 0.7071067811865476
    4 h4 Z7 Y8 W( C' }' g: l
  7. math.pi/3表示弧度,转换成角度为60度8 D" S* {9 L" Q! S1 A% K2 I! r
  8. >>> math.cos(math.pi/3)
    ' ?2 {( {$ l  r
  9. 0.5000000000000001
    " Z/ i4 P$ K9 a# ~, n% t" W1 i8 w8 b
  10. math.pi/6表示弧度,转换成角度为30度
    7 O% }. C* q( B( L8 m
  11. >>> math.cos(math.pi/6)
    1 t) b" ]3 P1 F' g  `$ R, a2 q
  12. 0.8660254037844387
复制代码

  Y$ i9 u% d& i2 Mmath.tan(x)  返回x(x为弧度)的正切值% ^& |+ V4 J# ^$ f
  1. #返回x(x为弧度)的正切值
    9 R+ L/ o! h' |% L, ~+ s) C
  2. tan(x)
    6 ?8 L. a& A. w3 {9 U, }3 U- x
  3. Return the tangent of x (measured in radians).
    1 `5 q  O$ O- ^- n3 r# c
  4. >>> math.tan(math.pi/4)
      T& d8 h- @; f$ e# I
  5. 0.9999999999999999
    ) u% I2 M3 L: ]7 b
  6. >>> math.tan(math.pi/6)5 ^; D0 _: [) [7 I
  7. 0.5773502691896257
    ) H# }$ g% }5 d# ]
  8. >>> math.tan(math.pi/3): Q6 Q- D3 F7 F5 l% y6 \$ u
  9. 1.7320508075688767
复制代码
0 M+ L. x3 g/ H! O
math.degrees(x)  把x从弧度转换成角度
5 h7 Q8 y! F& d
  1. #把x从弧度转换成角度5 \5 b% q, P6 k" ^) @$ G
  2. degrees(x)0 f7 Z. |0 J  }+ T7 ^) Q7 j9 h
  3. Convert angle x from radians to degrees.
    " `) [, }8 R8 F' t# a* V" }" z5 @

  4. 5 D0 E2 V' Q. T- \3 g8 R8 z
  5. >>> math.degrees(math.pi/4), B. {- U/ }, V: Z! g
  6. 45.0, B# n3 ^9 `4 T5 g, u% |
  7. >>> math.degrees(math.pi)
    / e& I: t; `% t3 u: N
  8. 180.09 a) u) L5 @; |4 i, ]! C+ f1 c
  9. >>> math.degrees(math.pi/6)
    ; b; G2 G: d5 E  p: x. \
  10. 29.9999999999999967 K9 W0 n  `0 L& u
  11. >>> math.degrees(math.pi/3)# n2 l) a0 B  P+ A$ F$ v
  12. 59.99999999999999
复制代码

$ K" R  w! t1 ?* x( O+ p( q1 p" A7 p: Gmath.radians(x)  把角度x转换成弧度& h' i5 {% H7 G0 L- T# j/ ?
  1. #把角度x转换成弧度7 _) s5 M" ^& \7 T! w9 ^. }$ H8 W
  2. radians(x)
    $ z" s4 N' K& @  O
  3. Convert angle x from degrees to radians.
    ; s; t* K$ ~9 v* y6 L
  4. >>> math.radians(45)
    6 n+ J& S; }8 W7 \; [
  5. 0.7853981633974483
    . Q1 F0 J& ]5 |. r, @1 ?7 G
  6. >>> math.radians(60)- Z3 {) P4 A, U& z
  7. 1.0471975511965976
复制代码

0 v% M* u" d& R3 W& v7 umath.copysign(x,y)  把y的正负号加到x前面,可以使用0
8 C2 o/ q: B: S1 \. ]4 s
  1. #把y的正负号加到x前面,可以使用0
    7 |' L0 u+ ]7 w, Z( h3 z% U) Q
  2. copysign(x, y), ~; _( ~) V3 L
  3. Return a float with the magnitude (absolute value) of x but the sign ; p# W+ C' V$ Q3 `5 {' m
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    . }8 x4 K' j3 K
  5. returns -1.0.5 v7 D( L2 A8 l3 P, e: j

  6. 8 E9 @9 |3 p( W* U' A- ^' M3 x5 }
  7. >>> math.copysign(2,3)
    ) I# ?+ f, T4 E( J+ K: n) t
  8. 2.0; T5 c/ h8 n) g7 g
  9. >>> math.copysign(2,-3)
    ; r# l+ ?4 h  Q+ q: ~8 O
  10. -2.0" {% ?7 R7 ?0 C; k
  11. >>> math.copysign(3,8)
    1 l4 c# r: Q6 n+ G( J
  12. 3.0
    + B( [2 J2 u! e- M
  13. >>> math.copysign(3,-8)
    ! G9 Z8 Q' u4 I, T" z# a9 ~. u. k0 {" ^
  14. -3.0
复制代码
; b3 U5 S! d6 u* y* Q& x) @
math.exp(x)  返回math.e,也就是2.71828的x次方
- C2 u& v# b1 J
  1. #返回math.e,也就是2.71828的x次方$ p7 e1 p8 ^2 ?5 z6 ^
  2. exp(x)
    / _! n( F% ~7 U9 C
  3. Return e raised to the power of x.$ z/ H& n, g4 g

  4. / r/ _$ C5 F/ g$ f9 O- z3 c
  5. >>> math.exp(1), B( x. _" A$ D4 R- D5 E% {
  6. 2.718281828459045
    / j( `) w4 ?  [( u% C$ A
  7. >>> math.exp(2), Y$ N! H6 P0 |1 H# T7 r+ j# k
  8. 7.389056098930650 N3 G+ p+ P; `; y3 t
  9. >>> math.exp(3)
    # p. s+ y/ ]4 c5 W5 _
  10. 20.085536923187668
复制代码
* ?6 Q& z) L9 \
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# C/ Z& a+ j% P+ r
  1. #返回math.e的x(其值为2.71828)次方的值减1; J0 Z; ^' z2 m$ G: V. [' x
  2. expm1(x)7 |5 @+ n# T# ?8 I" X; M
  3. Return exp(x)-1.! I, q- I" J. M5 \
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    / C3 t6 V1 r* a7 O& q2 W( J; {% W. t
  5. ' v2 R# Z+ y) E
  6. >>> math.expm1(1)% _2 I2 J$ ]& v
  7. 1.718281828459045& y( _5 k/ u% ~8 ^4 L
  8. >>> math.expm1(2)
    2 w5 C  R, i. r5 ^# o; j  n
  9. 6.38905609893065, @8 h. i) Z3 Q5 e  A
  10. >>> math.expm1(3)9 O% B% ]% {( g" p# n
  11. 19.085536923187668
复制代码
* q3 E, A- b- L7 f
math.fabs(x)  返回x的绝对值
" `/ u, I% w( n3 ], Z
  1. #返回x的绝对值
    ; l& }5 F6 s# ^( A% L  `& `
  2. fabs(x)4 j1 {  K4 {" t
  3. Return the absolute value of the float x.# [6 L7 g; y* z% I0 O* h; Y0 j6 L* M
  4. * p8 r- x1 d3 I1 U% y7 s' a
  5. >>> math.fabs(-0.003). i3 P6 O7 l# x
  6. 0.003
    ) U0 s8 C( N6 Q! L0 Y: e2 a
  7. >>> math.fabs(-110)6 o, ~7 u6 k* N! n0 I" ~+ _; T
  8. 110.0+ _! f1 B% n9 M- d1 u
  9. >>> math.fabs(100)) _$ e" K0 N& o5 |  ~
  10. 100.0
复制代码

2 O& ^9 G- E) C) C2 e, J! Vmath.factorial(x)  取x的阶乘的值2 u3 y7 I) ]+ f. B7 o
  1. #取x的阶乘的值' w! d1 N/ p4 B. |
  2. factorial(x) -> Integral
    " `: c" A5 {# q" J. P* k( l
  3. Find x!. Raise a ValueError if x is negative or non-integral.6 x2 U: Z9 n2 L+ X$ @  P. T
  4. >>> math.factorial(1)3 W. P  D# {4 F8 o
  5. 1
    ) ?) x; y, ^5 z- C9 }
  6. >>> math.factorial(2)7 o% ~% f! N  a& ?- U
  7. 2
    # f0 {6 M: ^$ K
  8. >>> math.factorial(3)$ O1 p9 M  V3 ^1 @) h* [5 a+ s! V
  9. 63 s: k; Y( {0 d. m3 a
  10. >>> math.factorial(5)
    - p0 H3 D0 P! g  _9 a6 ?9 ~
  11. 120  }7 b' G! i% g  v  `' X
  12. >>> math.factorial(10), B, p; I. i# [, g8 y
  13. 3628800
复制代码
2 U1 f2 n6 {" e! Q0 h
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数' }! r$ e- Q% J1 Q
  1. #得到x/y的余数,其值是一个浮点数7 ~& r5 L1 @. l& k/ I. G3 v
  2. fmod(x, y)
    + `6 }1 C- q4 W1 @; M0 T
  3. Return fmod(x, y), according to platform C.  x % y may differ." o9 T, f5 o) i, j5 A9 q
  4. >>> math.fmod(20,3)- }7 h" c+ l& r$ ^" u
  5. 2.0
    ) I  z4 q9 O0 D% i3 N: Q
  6. >>> math.fmod(20,7)
      b  @# z( [7 i$ O% e7 i$ {" j* f7 }! \
  7. 6.0
复制代码
5 m' `, C+ A9 q4 @; K
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ G  y1 q2 S' g. u& v, y, K1 y
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* {2 D, z" U2 E5 N  L
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    + O3 S' M" i6 j& O! L! k
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1  s' V# H: z& b9 q3 T
  4. frexp(x)
    , v6 G* u' P- n: e: `- t8 Y
  5. Return the mantissa and exponent of x, as pair (m, e).
    7 x  I1 F' a* R/ p
  6. m is a float and e is an int, such that x = m * 2.**e.! |, C8 T/ Z' {8 n$ R1 i
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.( F7 E  `+ ^% s' _
  8. >>> math.frexp(10)
    + W4 O3 J  }! ]( o, ]0 b
  9. (0.625, 4)1 R$ g' ~) n4 o. _4 @
  10. >>> math.frexp(75)& a  q! x+ P! \4 V  n9 B
  11. (0.5859375, 7), I6 W, Q  `" ^1 ^0 p+ I! W5 K3 L  U
  12. >>> math.frexp(-40)
    4 j3 M! y; h$ @& z& j$ N7 b
  13. (-0.625, 6), C/ p! U9 c; V/ t- o
  14. >>> math.frexp(-100)# w2 _) \" T2 r2 c$ s& x; F; ^
  15. (-0.78125, 7)' V% f4 U( ^" j7 R
  16. >>> math.frexp(100)+ |8 t8 ~$ {3 _9 _6 n
  17. (0.78125, 7)
复制代码

$ y3 \4 I+ |8 i$ i. W' p9 d4 v2 Kmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
# q. A6 A7 s. v7 J
  1. #对迭代器里的每个元素进行求和操作- J/ f! Y6 y" }2 c1 l2 O7 o
  2. fsum(iterable)" x* J3 K9 @6 I+ y% Y! F9 N% T: J
  3. Return an accurate floating point sum of values in the iterable.
    * u7 M8 i* Z5 d8 N; r- K7 U+ C: ?
  4. Assumes IEEE-754 floating point arithmetic.. A- k2 g- O5 s2 S2 ^
  5. >>> math.fsum([1,2,3,4])/ K, _2 ~8 U5 r( j
  6. 10.0
    / S) o. R2 n+ g9 E; r- C' @  A, z
  7. >>> math.fsum((1,2,3,4))
    5 w  T/ n/ S3 ~) o# ~8 Y
  8. 10.0; [3 f5 I  c6 @8 X' D- s7 Z
  9. >>> math.fsum((-1,-2,-3,-4))
    ; ]/ ~3 s# y9 b
  10. -10.06 Z. ?$ W& ?6 F7 T, ]* j
  11. >>> math.fsum([-1,-2,-3,-4])1 }' o3 R1 W, A/ ?
  12. -10.0
复制代码
2 ?3 b2 C2 @8 d1 K/ b" u6 g
math.gcd(x,y)  返回x和y的最大公约数
7 d" Y/ I5 ^/ s. ^
  1. #返回x和y的最大公约数. u: j; \$ b5 B
  2. gcd(x, y) -> int
    " m: l# l* A) O3 Q  a* C
  3. greatest common divisor of x and y. E: N9 Y) M) l+ R
  4. >>> math.gcd(8,6)
    . e) l' P$ V! Q" c* C4 c
  5. 2
    9 P( q5 g( L! d# G
  6. >>> math.gcd(40,20)
    * s) s# w5 O! k( D
  7. 201 ~: I* k# x. o
  8. >>> math.gcd(8,12): M: w* ]$ t9 j+ d8 c  E
  9. 4
复制代码
/ ?+ M4 v7 ?& x7 o" Y
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 q$ }; M% ~3 a
  1. #得到(x**2+y**2),平方的值1 T) \" A( ]! r
  2. hypot(x, y)% S5 c$ n1 E/ `; K7 r+ T
  3. Return the Euclidean distance, sqrt(x*x + y*y).. C# n" L& o& `* Q: z  w) i
  4. >>> math.hypot(3,4)
    , \: e: @/ e* z3 ]
  5. 5.02 `7 d$ p5 ]4 F% v7 o* q
  6. >>> math.hypot(6,8)  _7 h7 B+ j; d# n0 G' |- {- X
  7. 10.0
复制代码
# D, i; a" z: M1 q  Y
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
. m( ^' d+ D6 q  b- m) x
  1. #如果x是不是无穷大的数字,则返回True,否则返回False2 G, q% `; F3 Q
  2. isfinite(x) -> bool8 p3 j4 u0 [) L- j0 \! ~% h( t
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.2 }' N! {' }  Q7 ^- a
  4. >>> math.isfinite(100)
    ' h% D) t; m8 _8 ~4 C
  5. True. r6 r2 N+ ~; J" {, v
  6. >>> math.isfinite(0)
    : m2 ?, F& f; ]0 [6 H) d* n% I
  7. True: a, t3 s# b; R; }9 V
  8. >>> math.isfinite(0.1)
    0 U! [; [: y" N* q1 m( C
  9. True4 {$ \1 |3 A9 _9 c$ L
  10. >>> math.isfinite("a")( n2 ^. O# C" g. q
  11. >>> math.isfinite(0.0001)9 |2 }7 J. q$ u5 `0 G
  12. True
复制代码
; e6 U# N; w' C" d2 H/ o& h7 j) U
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 Z, _1 ?1 {/ F& r# p) l
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False5 Y% i& J% b0 @
  2. isinf(x) -> bool
    ( {% h) s( P5 Z  ]8 N/ w" e
  3. Return True if x is a positive or negative infinity, and False otherwise.1 R9 o! |4 |0 U6 S
  4. >>> math.isinf(234)
    1 A6 V: a4 ]& ~+ M
  5. False' T" g) N! x! w, D& ]1 C$ A
  6. >>> math.isinf(0.1)# K6 l9 b- |/ b
  7. False
复制代码
* g7 S: h! e0 s
math.isnan(x)  如果x不是数字True,否则返回False
! j& {  N/ C8 C: Z6 C
  1. #如果x不是数字True,否则返回False8 M$ |. d9 T4 v( b8 d4 v, _
  2. isnan(x) -> bool  D5 w( p* Z; Q" E- F, B
  3. Return True if x is a NaN (not a number), and False otherwise.
    ( j% B5 F: ]: n$ ]3 i9 s
  4. >>> math.isnan(23)  g' S9 U6 L* P0 Z( M+ G
  5. False
    5 b3 z# o2 C$ v, V4 N* O
  6. >>> math.isnan(0.01)
    . M% s) t7 }% ~6 x# y! }
  7. False
复制代码

+ J1 F( n  ]+ o% O" Cmath.ldexp(x,i)  返回x*(2**i)的值5 C6 P# ]! ^3 {$ ], z
  1. #返回x*(2**i)的值" w1 B3 Z2 N8 Z1 H" D
  2. ldexp(x, i)
    & }( ?% W! b# i  C8 }. @' n
  3. Return x * (2**i).8 `" n4 ~( g6 x% C9 C& W8 u
  4. >>> math.ldexp(5,5)
    2 r4 r: P' e8 g
  5. 160.03 v" @$ h6 g8 U) F  G
  6. >>> math.ldexp(3,5). ]4 u) Y% V/ _% ?# B# d
  7. 96.0
复制代码

  w$ X' ?' W) O: p* E, Dmath.log10(x)  返回x的以10为底的对数: ], l  B+ @4 \4 i& T' q+ ^
  1. #返回x的以10为底的对数
    + f% S: J  p' F4 w4 u' x
  2. log10(x)
    $ ~  q7 T. t, y! L7 b2 s% M
  3. Return the base 10 logarithm of x.
    9 O3 b, @3 G3 l8 _  R% G
  4. >>> math.log10(10)
      a( J2 M( v# n4 A+ c9 g: x' ^
  5. 1.0
    6 C* ]) h5 c0 B. r
  6. >>> math.log10(100)
    - X) X% A/ l, F: B* h
  7. 2.0
    6 U) s+ {, z# s$ r* j, M
  8. #即10的1.3次方的结果为20; d  n- W; t9 x( e( p% |3 l7 K1 S
  9. >>> math.log10(20)
    6 Z6 i- p; p. Y' ]  C* x
  10. 1.3010299956639813
复制代码
1 z$ U. E6 F- V6 k, J) ], l
math.log1p(x)  返回x+1的自然对数(基数为e)的值1 V2 D; j  P: s8 I8 B! W
  1. #返回x+1的自然对数(基数为e)的值
    8 o% Z# J6 g# P, D
  2. log1p(x)
    8 j" L2 X1 e) B! U( ]1 S
  3. Return the natural logarithm of 1+x (base e).
    $ G* m) R( M: D
  4. The result is computed in a way which is accurate for x near zero.% j4 |6 W2 }( b& j; n
  5. >>> math.log(10); X* E* v: U9 F: B, j
  6. 2.302585092994046
      @) L& i, G- U7 I
  7. >>> math.log1p(10)5 M% l. _) q( |$ N8 u* h/ b& B
  8. 2.3978952727983707
    6 c$ x: B& J- J% h2 _% y
  9. >>> math.log(11)
    * {+ B8 f( r6 m" g. \' I
  10. 2.3978952727983707
复制代码

4 B, D- J+ k: Dmath.log2(x)  返回x的基2对数
* z- m, q& c* {9 y9 ]
  1. #返回x的基2对数
    ! u+ W6 d1 n. U  U
  2. log2(x); ~# j% ~$ N/ Y! L, J# ^
  3. Return the base 2 logarithm of x.
    1 M9 S4 A- o4 _& W
  4. >>> math.log2(32)& g' b5 x  e5 p3 ], }6 S' n
  5. 5.0; y+ N) V! q% G  G0 r
  6. >>> math.log2(20)
    9 K3 ]8 c' r% h4 F4 z
  7. 4.321928094887363+ i, q, ^- p: y6 r; ]0 b+ ?2 N
  8. >>> math.log2(16)
    + ~8 ^5 v+ j( S* S3 z4 ~
  9. 4.0
复制代码

$ D# T9 p3 k# s3 Mmath.modf(x)  返回由x的小数部分和整数部分组成的元组( S3 Y1 I/ t/ ^, ?& g# q2 A- t$ V1 C! ?
  1. #返回由x的小数部分和整数部分组成的元组4 A$ z) u0 m+ P
  2. modf(x)
    ; K5 S. I$ `+ Z
  3. Return the fractional and integer parts of x.  Both results carry the sign# C. ?2 n3 X6 U/ _
  4. of x and are floats.7 d" z6 c$ c5 R
  5. >>> math.modf(math.pi)
    ' e9 R9 t) b, t( ^% ?  l
  6. (0.14159265358979312, 3.0)1 m/ u. W7 m7 `6 J7 j
  7. >>> math.modf(12.34)3 F7 o6 i' V* r& C( Q. U
  8. (0.33999999999999986, 12.0)
复制代码
( Z7 b7 ~9 I+ _% ~0 u
math.sqrt(x)  求x的平方根+ M4 G. v+ a8 G* }
  1. #求x的平方根
    / D! R; ~' @* a4 N% z
  2. sqrt(x)
    # \& A) R& {1 i6 O) `& t
  3. Return the square root of x./ N- C# }4 y( l7 ?
  4. >>> math.sqrt(100)
    , \( W0 k% k) X. b
  5. 10.0
    4 Y2 r5 R9 p8 G6 j4 j
  6. >>> math.sqrt(16)
    " e4 B$ Q8 E" E8 p+ n4 s( ?3 |
  7. 4.0
    4 y% R, T" l# G  B1 Z, ]+ _1 w
  8. >>> math.sqrt(20)
    0 D4 b% u- {. w0 G" V, M3 h
  9. 4.47213595499958
复制代码

$ N2 U# g4 W0 R# rmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; s' W; I  _9 {$ d/ \- K9 b
  2. trunc(x:Real) -> Integral6 d' T3 v7 D. S
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    . f- V5 _6 Y1 z
  4. >>> math.trunc(6.789)
    / R1 E2 d! i( Y
  5. 6
    $ o$ U4 [) ?1 u7 y4 F; }# u8 y( I' z
  6. >>> math.trunc(math.pi)9 f- u% E7 Q0 o: \0 D% q" J! q. D6 p$ I
  7. 3
    0 U  h- D  q5 Q+ W9 L( g/ v( F7 y
  8. >>> math.trunc(2.567): {: G* h" g9 _
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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