新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

2 u1 z; R4 r2 H0 q5 K/ I【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
# E- R1 m. _& |+ {) B; T4 y

3 N, z0 _0 L$ t3 B6 x方法1
% P2 G  {/ N5 ~/ ^
  1. >>> import math
    , j9 f  K  a2 k8 X& B
  2. >>> math.sqrt(9)# |( _3 n& m& [
  3. 3.0
复制代码
方法2
+ X* E+ b: \3 M: B9 a
  1. >>> from math import sqrt7 |! Y" K/ r! @. U+ g: B4 e5 S
  2. >>> sqrt(9)0 k! Y' {4 L  P! Z% `, }4 v, Q; G& a! V
  3. 3.0
复制代码

: m6 n# Y, x5 y) ~3 p

+ d7 q! `1 d% Z( \) o3 _
math.e  表示一个常量  A" ?" D- {8 M0 l. I$ i  A8 M
  1. #表示一个常量
    - f7 c% ?. [1 B" N4 i
  2. >>> math.e  k2 @8 C2 s1 N
  3. 2.718281828459045
复制代码
# ^# B; ]6 O- @+ s3 j
math.pi  
数字常量,圆周率

* g( i6 b; `% O9 i
  1. #数字常量,圆周率
    2 N4 z1 D0 c2 q  m. V4 f8 l3 `  m4 O0 j
  2. >>> print(math.pi)
    % h( P, g6 `: ]1 U5 K
  3. 3.141592653589793
复制代码
  ^% }1 W" m- o* @8 v# ?7 I
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 [) C4 j% ~% N6 u" U7 F( B
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ; y- D2 P; ~( ~  O* g1 A
  2. ceil(x)! w  ?2 z# a& |' R& R7 b
  3. Return the ceiling of x as an int.
    % _. M" v! J' g7 @9 g& ~
  4. This is the smallest integral value >= x.0 d* `1 c* B# g
  5. : w9 k/ q2 s* _9 t5 o9 S$ P
  6. >>> math.ceil(4.01)
    ' C- Y2 i" ~, W4 ?, W% a
  7. 5& G" I! O8 O+ R& A9 n
  8. >>> math.ceil(4.99)! P8 z( y5 W+ s( R5 m
  9. 5% G* ]0 Z0 C  j
  10. >>> math.ceil(-3.99)& Z3 q; d( x) D8 w- S
  11. -3
    . ^' x# b, j4 b& ~' m
  12. >>> math.ceil(-3.01)
    ; p/ C. m% f% F$ m5 r
  13. -3
复制代码

# D8 d1 _  X3 u- fmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ N+ r9 X7 S, F2 E
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    - v8 w3 O5 U" S
  2. floor(x)
    7 I# T  U& g. [5 s7 }
  3. Return the floor of x as an int.
    4 B9 U# H! b7 }
  4. This is the largest integral value <= x.* ^6 Y- k  O4 U7 V0 o$ `3 n- m0 Y/ y
  5. >>> math.floor(4.1)
    ' v6 I. e9 P* x( c2 S8 }
  6. 4
    4 O3 P. x' s( ?# q) q5 H
  7. >>> math.floor(4.999)  _/ e7 i; d# H  X5 T1 j9 O( r6 d
  8. 4  L: E' d1 J6 t9 F: N
  9. >>> math.floor(-4.999)
    , p. E1 _7 b7 p- s
  10. -54 N0 K# y8 x* ^" Q( I5 f
  11. >>> math.floor(-4.01)/ z2 m% b3 T# H5 s/ W( Q7 G
  12. -5
复制代码
2 q9 N7 ]% k0 Z# U* x' k3 v
math.pow(x,y)  返回x的y次方,即x**y
( Z# `/ f. e! H0 N
  1. #返回x的y次方,即x**y, E3 r( r/ c5 e1 y7 U
  2. pow(x, y)2 N) O1 Z" a$ ]4 U1 B/ s
  3. Return x**y (x to the power of y).
    ' m! f4 ?; p7 U7 F8 f
  4. >>> math.pow(3,4)
    ' ]0 P2 R9 T4 g) @* `
  5. 81.0
    4 S1 P7 t9 M" ~) I8 t8 x
  6. >>> / `. I& @4 k0 _& B& m) o4 M' q
  7. >>> math.pow(2,7)' j5 Y( s7 r, l3 y, f# _$ }
  8. 128.0
复制代码
6 m5 D! Q; O3 w3 N
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), y# W+ v% @/ K# s0 R/ z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 x" X1 ~) C- ^* }; E' q" l) Z
  2. log(x[, base])
    # F# k8 b6 C( S' t5 P9 A& e9 U) Y& H, W2 v
  3. Return the logarithm of x to the given base.& b# S0 w  _4 o+ q/ h3 C" b, ?1 g
  4. If the base not specified, returns the natural logarithm (base e) of x.- Y: d- O: _: |* j. S; l% o
  5. >>> math.log(10)& L# M- t" g4 f/ _) h) [- o
  6. 2.302585092994046' R* A+ |) T! {5 I8 i# F
  7. >>> math.log(11). u; p* @( w: l
  8. 2.3978952727983707# W) D5 `' `* B+ c& L6 P
  9. >>> math.log(20), k1 o' g4 r0 e2 R' j
  10. 2.995732273553991
复制代码

6 I% K/ C0 x+ }( ?, Rmath.sin(x)  求x(x为弧度)的正弦值
9 d& u; j5 F, e( ^1 E2 H6 \# \
  1. #求x(x为弧度)的正弦值; y) ^/ w4 U9 K( U: E6 `7 y
  2. sin(x)7 L. |, F# u5 ?' v2 J* f$ F
  3. Return the sine of x (measured in radians).
    7 _# D4 V% o" c
  4. >>> math.sin(math.pi/4); A5 j- `3 D4 p/ W
  5. 0.7071067811865475
    / E7 B  K' f% X. R# [
  6. >>> math.sin(math.pi/2)
    + |3 x4 S; h- I6 Z' G; H
  7. 1.0
    * v% F1 U; j, N' V' e
  8. >>> math.sin(math.pi/3)0 ^' a6 ^- w: t8 t. l
  9. 0.8660254037844386
复制代码

' Y: N: S% W) Y2 j3 i) g- bmath.cos(x)  求x的余弦,x必须是弧度! B7 e, t" W; E  l& ^0 S
  1. #求x的余弦,x必须是弧度
    5 S8 k7 K8 R: B- `8 h2 G+ X1 D
  2. cos(x)3 s  T* Y2 d8 V4 Y; n
  3. Return the cosine of x (measured in radians).3 e- r  _: u% C
  4. #math.pi/4表示弧度,转换成角度为45度$ @5 p- }+ a6 E
  5. >>> math.cos(math.pi/4)# r4 j! _; g( l+ C# H
  6. 0.7071067811865476' Y" C  s/ a8 D' }
  7. math.pi/3表示弧度,转换成角度为60度( k7 V4 ?- R* a* t
  8. >>> math.cos(math.pi/3); j" Z4 a! b7 E/ N; u: Z
  9. 0.5000000000000001
    + d$ p1 Y4 e- Q  `" _$ _3 ^% F
  10. math.pi/6表示弧度,转换成角度为30度" n8 B2 U; g* M: G( p( Q/ {
  11. >>> math.cos(math.pi/6)
    0 m' n# G9 r6 C& p1 W
  12. 0.8660254037844387
复制代码
1 ~% _& E5 P8 u! x) x
math.tan(x)  返回x(x为弧度)的正切值
; `0 ?$ Z# b3 l; Q3 F
  1. #返回x(x为弧度)的正切值* m, J* X, Z* p
  2. tan(x)# Z& `$ ]& P, E0 W2 ^9 C
  3. Return the tangent of x (measured in radians).
    8 J0 `, M0 t3 A" ?: c4 Q
  4. >>> math.tan(math.pi/4)
    6 h) M, P8 `2 f! h) J6 a
  5. 0.9999999999999999
    3 K- \9 ^! t+ m7 s) L: _) x3 F
  6. >>> math.tan(math.pi/6)
    & i" z6 F& w$ s1 n" o/ ^
  7. 0.5773502691896257  M0 O! E- s7 g, b( K6 X0 k
  8. >>> math.tan(math.pi/3)
    $ {# E9 ]2 S0 z6 ~9 A. C- l
  9. 1.7320508075688767
复制代码

5 x& l  t: L5 ?* T1 k% c0 Pmath.degrees(x)  把x从弧度转换成角度, M, G, w" u" k; v4 g9 c: J7 d! r
  1. #把x从弧度转换成角度
    1 \* r0 J- q, B6 E0 |( i
  2. degrees(x)) @- O: K, k, m# W
  3. Convert angle x from radians to degrees.
    9 z8 p; |6 R: M% B) W

  4. 9 `! y2 B+ j* ]8 B0 @7 y5 W
  5. >>> math.degrees(math.pi/4)
    & L4 E+ }) n! ~3 a$ J
  6. 45.0$ V6 `% s% L$ T  f1 v
  7. >>> math.degrees(math.pi)1 c$ N6 z0 u6 H% R: k/ C+ }; ~
  8. 180.0# S, W5 b: f/ V  n9 d1 b
  9. >>> math.degrees(math.pi/6)' h: T7 n% y- g0 V- A# P
  10. 29.999999999999996' S$ B. U& |& R/ |/ X% `5 L
  11. >>> math.degrees(math.pi/3)1 Y2 b$ W4 F6 v8 u$ w4 q' T
  12. 59.99999999999999
复制代码
# u4 d5 O9 ^' @( ?2 [, u. m/ n/ K. C
math.radians(x)  把角度x转换成弧度
6 o& W0 D% s0 d  A, Q
  1. #把角度x转换成弧度
    ; C% \& |2 d& p/ r
  2. radians(x)8 y4 [$ h5 S/ Y: H* Q! ~
  3. Convert angle x from degrees to radians.7 r  D3 e2 @7 \* v0 i/ B- R
  4. >>> math.radians(45)0 g, }5 a2 A- s
  5. 0.7853981633974483
    " O9 c9 \6 r3 B+ p
  6. >>> math.radians(60)
    : n2 o' ?, V3 J! H: D2 Q
  7. 1.0471975511965976
复制代码
) P! R+ F: f6 E/ c. U
math.copysign(x,y)  把y的正负号加到x前面,可以使用07 k( f  E- \/ }% }
  1. #把y的正负号加到x前面,可以使用0
    9 T1 [* o' I# W
  2. copysign(x, y)
    - r) {+ O. F: g* X5 @! J, h0 z8 [7 M
  3. Return a float with the magnitude (absolute value) of x but the sign 7 m# ^/ j2 i3 m- P6 c' h1 C
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    6 j) Z) N  e- A/ I$ S
  5. returns -1.0.. y# m6 U. m5 p9 `# y

  6. 8 y# a" w+ B+ v0 s2 R
  7. >>> math.copysign(2,3)+ _/ k7 r2 C+ N, o; K9 F6 f
  8. 2.0
    . b. M) n/ g/ v6 h
  9. >>> math.copysign(2,-3)1 ~: x4 l; H! n4 X/ S( Q- H: ]
  10. -2.07 ~$ b' X6 U. V% _3 F  a
  11. >>> math.copysign(3,8)/ e- k2 Z* X4 Q- N/ K
  12. 3.0
    , b5 @- ?, Y1 ^
  13. >>> math.copysign(3,-8)! O, `* A/ e( O7 G& ~- I
  14. -3.0
复制代码

( f9 I* F% S% j$ F2 B2 ]! wmath.exp(x)  返回math.e,也就是2.71828的x次方1 g, b) b: g% B1 [
  1. #返回math.e,也就是2.71828的x次方( L+ \7 v' m6 k9 p8 f+ N
  2. exp(x)
    7 l$ C  O. S$ e0 F  Y- w
  3. Return e raised to the power of x.9 |4 ], ^# `: F% P# d

  4. ) E3 G0 _& Z, V
  5. >>> math.exp(1)! a8 H9 p+ t0 I3 X4 B) p
  6. 2.718281828459045
    ' A. f, D: ]' N
  7. >>> math.exp(2)3 y$ ?$ r, A" U
  8. 7.38905609893065
    6 A  U$ l  s8 r+ i5 _
  9. >>> math.exp(3)
    3 D. X+ |, I8 I' G
  10. 20.085536923187668
复制代码

- g% e% A4 W' A1 h# s2 L" cmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1# X: J  E. m+ y- ^9 V/ i; t& {( N
  1. #返回math.e的x(其值为2.71828)次方的值减1* H% E  j/ d- g- U6 x' F
  2. expm1(x)
    $ a- _8 Y, P- T' h
  3. Return exp(x)-1.5 v- b6 C! [' H: u$ Q
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.9 e& D0 e( ~" _$ X6 E0 ^/ ^+ Z

  5. + I% C: x6 w. `: S& l3 o& o1 e
  6. >>> math.expm1(1)
    $ d+ t! c7 s1 h% V
  7. 1.7182818284590455 _' r7 i8 r  X/ k
  8. >>> math.expm1(2)7 C# V) q5 M3 Y, D; C
  9. 6.38905609893065
    : w- a! @4 {: Y% }
  10. >>> math.expm1(3)- ^% A- U1 n% ^0 i; H7 U/ [
  11. 19.085536923187668
复制代码

8 g3 ]- H2 {% o: lmath.fabs(x)  返回x的绝对值! N% h9 m4 L! r% z
  1. #返回x的绝对值
    . r5 A0 y$ h4 z' E
  2. fabs(x)- `3 n+ j% o' W. n
  3. Return the absolute value of the float x.
    + x; i0 i  l* c. v6 |

  4. " b9 ?* h; d0 ~- y4 k' Y8 q
  5. >>> math.fabs(-0.003)
    4 H+ a5 y! t- x) X
  6. 0.003$ h4 y8 a1 b$ u+ ?* V$ U  T; E" A
  7. >>> math.fabs(-110)7 u9 C1 `& @7 J. N6 h
  8. 110.02 t3 [8 x& O. j" \5 `$ x. A# T% b
  9. >>> math.fabs(100)! @" U) B( J! @: X& E- M) C1 c
  10. 100.0
复制代码

+ f2 B7 M% D: {) imath.factorial(x)  取x的阶乘的值
$ j8 K7 ?, m# p! z+ B3 P$ p, Z9 `
  1. #取x的阶乘的值
    ( m2 D: y+ W( k" ]
  2. factorial(x) -> Integral8 v  e7 M6 k! M: C  f! K0 U
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; v' o7 [: z) y9 _" f
  4. >>> math.factorial(1)3 l6 W; V- u% \0 ?9 G
  5. 1% B+ U+ d- j1 w: }# i7 Q! ^
  6. >>> math.factorial(2)+ X) Y$ W- q9 U
  7. 2, l: E- ^8 R: L
  8. >>> math.factorial(3)
    / h, v) t) R( R* J% ~4 h5 L! }
  9. 6& z% f# v) ]9 W( D) O- {8 c3 a$ F
  10. >>> math.factorial(5)4 K4 t+ t1 U4 \2 A
  11. 1206 i) O, d1 n2 l; G
  12. >>> math.factorial(10); X& E8 L" b5 u# U3 z/ j& U1 Q
  13. 3628800
复制代码
* f4 _3 c2 d! u: s$ b
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数1 e: v- T+ i; V) k9 m) J
  1. #得到x/y的余数,其值是一个浮点数) r" z/ O' I0 h) w$ e% Z1 z
  2. fmod(x, y)
    7 ~9 g0 P* t# v0 H+ a
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    % c) v% p/ z- w- u; h3 \
  4. >>> math.fmod(20,3)
    ) B) j4 R; w1 }1 Y
  5. 2.0
    9 w/ k, v9 \! h9 \0 f1 o. O7 G
  6. >>> math.fmod(20,7)' ]: k+ L) g3 E6 q. E
  7. 6.0
复制代码

' L1 P( Y2 d/ r! }. fmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 c2 V$ O% w6 C  U0 ^6 _1 H
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    " `5 w8 d2 a& a3 N% {- `3 @6 {
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    + i, J0 ^& i+ O! w3 r# z
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    , G" V) m( P  S* y
  4. frexp(x): C: L9 R# E2 f  a+ J: v$ b1 _- D
  5. Return the mantissa and exponent of x, as pair (m, e).) M; I9 ~/ F' a- L
  6. m is a float and e is an int, such that x = m * 2.**e.$ t$ X$ c2 J  _8 H" F
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    . ^  M( w- E; U0 Q" X  S+ p
  8. >>> math.frexp(10)
    0 B# a  J( v+ ^1 Y% K$ @( H- a7 |
  9. (0.625, 4)" a, d6 _- e9 y% y2 H
  10. >>> math.frexp(75)6 A( e. h$ f: a# \0 n
  11. (0.5859375, 7)8 I8 s7 c) V4 \+ K( g
  12. >>> math.frexp(-40)
    1 w; ], V' ]8 Y% Z+ {% x
  13. (-0.625, 6)
    # p' U% R6 W" q; f. y
  14. >>> math.frexp(-100); ?; k" K+ t3 F1 V9 s4 I
  15. (-0.78125, 7)
      Q4 S* T" Z9 q( k' @, d# c
  16. >>> math.frexp(100)
      c$ ]/ Y6 ?5 a1 Z! R; ]
  17. (0.78125, 7)
复制代码
9 b- r) Y: D/ A1 ~5 [) k8 ]/ ~  X
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列1 d- D$ Q' [% `8 B: P  d/ C
  1. #对迭代器里的每个元素进行求和操作% C, p" X0 X6 M+ ^8 P
  2. fsum(iterable)
    1 _+ C; }9 _) B4 k5 A
  3. Return an accurate floating point sum of values in the iterable.
    7 X4 K  N) z' x
  4. Assumes IEEE-754 floating point arithmetic.
    8 J7 X3 t! I* j. Y9 j+ \
  5. >>> math.fsum([1,2,3,4])
    3 o2 f" p! ^7 h
  6. 10.0
    # a4 P! \. C" H/ P! y. w. A- i
  7. >>> math.fsum((1,2,3,4))+ B  K3 @* x5 ^
  8. 10.0
    / B' {* i3 X# @4 F' |" C) A. H
  9. >>> math.fsum((-1,-2,-3,-4))
    . C5 ?2 a( G8 n9 Q
  10. -10.0
    # E  N$ U6 f0 X5 G
  11. >>> math.fsum([-1,-2,-3,-4])+ ^% \+ X* Q; y9 `$ i$ i$ c" \4 r
  12. -10.0
复制代码

2 t: C% f5 K8 P, n' x, s, Zmath.gcd(x,y)  返回x和y的最大公约数
% r/ b8 y& f& q8 a2 r" b* M" e5 i
  1. #返回x和y的最大公约数/ A  z: U% s$ }8 x& d" h% Y
  2. gcd(x, y) -> int
    1 s2 |7 g" r1 p8 O3 j
  3. greatest common divisor of x and y
    , a/ P+ H5 ?, Z. \/ W
  4. >>> math.gcd(8,6)
    4 r$ S: W8 u) p1 y- Z) N! W( @3 h
  5. 2
      |! N0 ~  h* B' k/ g
  6. >>> math.gcd(40,20)
    4 V8 P  r0 W' z6 A8 J* n
  7. 203 C8 E/ k% m: g* k
  8. >>> math.gcd(8,12)
    4 ?0 w+ n2 `9 d6 D
  9. 4
复制代码
* [2 N4 r( ]- S! U+ e/ \4 x
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
0 F- k. l8 |+ ], g# U7 W
  1. #得到(x**2+y**2),平方的值6 q, z" X. }: W2 d/ H/ ?& s2 t
  2. hypot(x, y)( E, w: P1 F% M: }! ^0 P/ P% j, s6 o
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    , R1 d" Z! o4 b- G* u  M
  4. >>> math.hypot(3,4)
    , U2 d" @) P9 d- o6 \9 ^- S
  5. 5.00 n/ H! F7 S: f- V) b! b
  6. >>> math.hypot(6,8)# N% y) m/ d( a4 d. y
  7. 10.0
复制代码

! h7 G4 u/ W2 D% imath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False, g9 k  a+ j( n6 J
  1. #如果x是不是无穷大的数字,则返回True,否则返回False: E8 F+ L$ R. x: K( ^, `
  2. isfinite(x) -> bool* e9 ]$ I# {5 f4 D- a2 M! i
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    * P8 x% Z. T/ Y5 {; S
  4. >>> math.isfinite(100)
    * q2 h6 W: E% D- F6 {
  5. True6 }  |7 J8 |. U
  6. >>> math.isfinite(0)
    1 w# C3 I/ |2 K( ]
  7. True) g, `: Y$ }3 \( P' m% h1 @4 A
  8. >>> math.isfinite(0.1)9 @7 b  N  u6 c$ j" w; l% Z
  9. True& d8 u+ L0 R6 ]" n8 S, s8 ~  j
  10. >>> math.isfinite("a")
    ) }9 {; A3 V  s* s: W# Q0 H
  11. >>> math.isfinite(0.0001)
    , x6 G0 v% |* |8 I  L6 |2 D
  12. True
复制代码

2 N- ]" }$ \' Qmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
, x! p" D' }- m8 I: C" {7 }9 K
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    . E) V1 O* S, P# r# v
  2. isinf(x) -> bool9 h- `. s# d: u
  3. Return True if x is a positive or negative infinity, and False otherwise.
    8 F7 b! ?- g: g4 `
  4. >>> math.isinf(234)7 R5 D5 n: G: t# D1 R6 H2 T! W
  5. False
    " V( K# Y+ W# \% Q
  6. >>> math.isinf(0.1)
    $ l0 Y3 c* i- l+ e4 F) k
  7. False
复制代码

% y$ I5 d6 e/ q3 omath.isnan(x)  如果x不是数字True,否则返回False1 l3 X8 M$ N, X( S
  1. #如果x不是数字True,否则返回False
    6 ]) l4 ^. s6 w- R/ o1 n( ~
  2. isnan(x) -> bool$ m0 D3 ?3 Q/ ^+ [6 p; g% o
  3. Return True if x is a NaN (not a number), and False otherwise.$ V/ `6 ?0 Q7 `% ~. B; D
  4. >>> math.isnan(23)
    9 R' t2 C: V0 s) ]# {) L
  5. False
    , `* _% Y! k- _1 U* `+ E: ^( K1 `
  6. >>> math.isnan(0.01)
    , h# E4 z  a  q  w8 f$ N, W; c8 H
  7. False
复制代码

* p  S7 O1 ^+ o6 p2 `math.ldexp(x,i)  返回x*(2**i)的值
! x- m( h3 p% t+ }. U  J
  1. #返回x*(2**i)的值/ x" N" [5 \1 S- S4 m- ?( o
  2. ldexp(x, i)
    - ~% P# X/ B5 C1 l. A, ]
  3. Return x * (2**i).0 x' q  J4 f! c5 E  |& e( [
  4. >>> math.ldexp(5,5)1 b9 L* d4 ~4 g! e
  5. 160.00 a5 {" p: W5 B1 \0 Q  q/ _# K
  6. >>> math.ldexp(3,5): y$ a; {/ \! ?
  7. 96.0
复制代码
7 L0 Y$ t( O4 O4 N7 i5 ]6 a+ Y4 _
math.log10(x)  返回x的以10为底的对数+ p, _1 ~# G8 Y9 v- u  q$ w3 |  F
  1. #返回x的以10为底的对数5 b2 _& M; _( V* x6 U
  2. log10(x)
    % @% c. f, e$ L
  3. Return the base 10 logarithm of x.9 J/ h1 C5 }5 b9 a& W- q8 ^
  4. >>> math.log10(10)" D5 V. L- g' l
  5. 1.0: U9 U6 L6 i. U; a4 P( J
  6. >>> math.log10(100)) g. L3 u! g6 _4 l- o0 n
  7. 2.0
    ' T* I9 N& g. w
  8. #即10的1.3次方的结果为20
    2 p3 q% c0 h& K) }  u1 l% c( d# T
  9. >>> math.log10(20)
    1 Q/ i& w7 a1 L# A7 M2 {. [
  10. 1.3010299956639813
复制代码

$ m3 v  k" L# {# b& emath.log1p(x)  返回x+1的自然对数(基数为e)的值% B9 c8 n, b7 ?& E0 Q3 ]( O. \
  1. #返回x+1的自然对数(基数为e)的值0 ]/ Q2 e9 Z0 _2 R7 D, l, r0 {
  2. log1p(x)
    " o8 {- ?) o7 a  A; S2 {, R4 m5 K
  3. Return the natural logarithm of 1+x (base e).5 p( D# F# z9 t& D) n% a
  4. The result is computed in a way which is accurate for x near zero.- T) U; m' ?/ F' T: m2 N
  5. >>> math.log(10)9 L% A5 n/ ]+ C: M( |% C$ _
  6. 2.3025850929940469 y  i* v6 l- Q
  7. >>> math.log1p(10)
    & g2 S4 @" y, ^' q1 Q- Y' P
  8. 2.39789527279837075 g5 g) }" t& K1 G5 e
  9. >>> math.log(11)9 H" f7 `- ^; Y- _$ C# a
  10. 2.3978952727983707
复制代码
" `8 D/ s- a" H4 g4 ?$ X
math.log2(x)  返回x的基2对数
+ \* k: ~2 p' U- Z/ g( ?# G
  1. #返回x的基2对数
    * [( q. _4 d, |* X: X
  2. log2(x)7 G+ `; |0 t7 f  m1 C# Q- n" d! p0 R
  3. Return the base 2 logarithm of x.& u" E9 v! O# I6 U- A2 b/ J
  4. >>> math.log2(32)
    2 J# ]$ v: ~# `, g
  5. 5.0, B& W3 c" p$ }% B( b$ \
  6. >>> math.log2(20)6 u. i8 j1 J. f4 ]4 S# @7 C& O
  7. 4.321928094887363
    6 S: _9 u# u" g$ @
  8. >>> math.log2(16)
    " O( O+ G( Q$ F8 y8 B! a: Y8 C; `5 [
  9. 4.0
复制代码
+ R% H7 [7 L- K0 V0 [2 W
math.modf(x)  返回由x的小数部分和整数部分组成的元组
  X; ^4 Y$ I* N7 X+ o/ E
  1. #返回由x的小数部分和整数部分组成的元组
    - s4 [9 }4 \1 L" I1 s) W7 l# l
  2. modf(x)" _/ c( V5 z! W! r/ ]( O& s
  3. Return the fractional and integer parts of x.  Both results carry the sign0 f! u) t+ f" q/ z6 u
  4. of x and are floats.
    . @/ y6 n" a& U+ A  q% X$ Y
  5. >>> math.modf(math.pi)
    0 \/ M/ C; x& A% }4 F
  6. (0.14159265358979312, 3.0)' \! U' r7 _5 j& J) e
  7. >>> math.modf(12.34). o2 Q7 ?( O; ~8 \! y6 H5 O
  8. (0.33999999999999986, 12.0)
复制代码

6 v' Y( x8 j" v& s$ j2 Zmath.sqrt(x)  求x的平方根
) ^1 M& c) f  f
  1. #求x的平方根
    5 q4 R: I/ j7 e' ]3 O
  2. sqrt(x)5 e. L4 R  e4 d
  3. Return the square root of x.  V( @& e- k. _4 b  V: h3 g% l; w+ M
  4. >>> math.sqrt(100)" z" B  C2 K/ N
  5. 10.0  C/ y; g5 p/ p: W$ Y: y3 }
  6. >>> math.sqrt(16)( e2 M) z" m" [& v5 k% G: w3 }
  7. 4.0, y4 z: E1 G8 c0 d" P5 |
  8. >>> math.sqrt(20)
    * I  G2 ~' {8 Z; }8 u6 O7 b
  9. 4.47213595499958
复制代码

% L' T7 V6 g7 W6 }/ dmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分5 W4 X; l/ t2 q4 i6 K; T" N, g
  2. trunc(x:Real) -> Integral
    ! b4 j. C; X$ z
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    / Q9 w' O% N' m1 t# b+ Y5 Y
  4. >>> math.trunc(6.789)
    0 C  S* _7 g  G0 }
  5. 6
    6 |  K' \3 i9 }
  6. >>> math.trunc(math.pi)
    3 w7 K, F4 x1 U8 g
  7. 35 X3 I6 b2 P7 @/ L" T8 f4 h- z
  8. >>> math.trunc(2.567)
    - d4 R% E1 u- `! V- p- N2 s
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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