新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
0 D) O3 |; h- \- }9 @2 l9 ~; _
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% s% Z0 C$ t! p  {, H

- ~  |, R# E; f. o: P2 s方法1
  ~& w6 }5 n# [9 K
  1. >>> import math
    ; s: i" `/ g: c
  2. >>> math.sqrt(9)
    2 Y( ~0 ~9 J8 e. Q$ y( n& W
  3. 3.0
复制代码
方法2/ C5 b# R$ a+ m+ W* v5 d
  1. >>> from math import sqrt
    # A3 \- c7 s) @! U2 }5 R
  2. >>> sqrt(9)- V( t& |9 C& `7 u
  3. 3.0
复制代码

: h3 r0 @. R: H  r( K
! D( \8 P2 s9 m# b6 |; j8 p
math.e  表示一个常量5 m5 u* m. @, E( s! w- Q
  1. #表示一个常量
    + V! o$ ^2 @; B, E2 A, J) \1 M
  2. >>> math.e
    ) f8 l3 i2 w; i) t" E) Z
  3. 2.718281828459045
复制代码

4 F& L0 D; p- tmath.pi  
数字常量,圆周率
7 r$ ~- V, D/ l1 u* W0 |" {
  1. #数字常量,圆周率& |, z4 x2 f1 E7 `; ~& X
  2. >>> print(math.pi)2 b" O+ P: ]8 q8 z6 |9 Y
  3. 3.141592653589793
复制代码

( t) K; R. P  Z. C) z6 [( Zmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 @0 D9 X* v9 u, N* R% H
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 U4 R( c3 Q* M( i' V5 r# m
  2. ceil(x)
    ! R( j& l1 B5 A# r! G; E
  3. Return the ceiling of x as an int.9 a$ K# e1 Y% h( q8 r; A
  4. This is the smallest integral value >= x.4 `" B' ^. f& d  w4 R- n

  5. ! I8 ?5 j0 ~' T! _
  6. >>> math.ceil(4.01)
    * p) j7 i$ e2 T: q  [' x! G' a
  7. 5) c1 x1 |/ |. I" l6 a
  8. >>> math.ceil(4.99)  B. t4 m& K4 `1 e
  9. 5
    / I" @5 @' L+ C6 \1 y
  10. >>> math.ceil(-3.99)
    * W* _3 ?: a/ K+ b
  11. -3
    7 ^+ w5 ?" J) t" g2 l$ _8 N( M! N
  12. >>> math.ceil(-3.01)
    % L( m4 H7 g* S4 F8 M5 g# C* _
  13. -3
复制代码

+ Z  e/ F; P, z/ Dmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 q. _6 o2 `2 {4 L! c: y0 E
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    , m  H  Q4 ]3 U) n
  2. floor(x). X2 O. Q) h& x7 n/ ~' l! F
  3. Return the floor of x as an int.
    . [; k5 ^1 c2 [/ H
  4. This is the largest integral value <= x.. I. B9 R. Y3 L4 `3 F: y+ S7 B0 e/ r$ \
  5. >>> math.floor(4.1)
    ! a* e/ H( O" O0 U* F! D
  6. 42 Q: w& t7 T2 V
  7. >>> math.floor(4.999)
    . R2 l! B& `1 t2 n- }( G$ Y9 y0 Z
  8. 4" Z% k/ k1 l2 C. v4 \2 Q, Q2 n; q
  9. >>> math.floor(-4.999)- v, ?9 x( Q6 N0 {4 A
  10. -5- G% h0 |7 R+ ]- N; z
  11. >>> math.floor(-4.01)
    # G  y& l5 T  S( {4 j, J
  12. -5
复制代码

( y5 |$ G% N: [math.pow(x,y)  返回x的y次方,即x**y, Y) Q5 `& s6 H, }9 M
  1. #返回x的y次方,即x**y
    0 X4 S4 x* D/ X9 q6 U* A
  2. pow(x, y)/ N  h+ A, r. f: {/ b
  3. Return x**y (x to the power of y).+ j2 ~$ M3 M# t4 O) y, B
  4. >>> math.pow(3,4)) U$ j' {& T; A* g- ~) M" E9 @2 _% s/ D
  5. 81.0
    + K' v1 f& d( p$ o
  6. >>> : O! ?& T7 b2 G5 M
  7. >>> math.pow(2,7)
    & {' y9 [" b# q! f
  8. 128.0
复制代码
+ `" `7 k5 C$ g7 O" \
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 |& T+ J( H$ o/ O' S: I- }
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 {- U! u3 n; o4 X8 z
  2. log(x[, base])
    . z# R# d8 O. F: _- c6 V- _
  3. Return the logarithm of x to the given base.) b5 b9 n: g( @5 D/ f
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ k5 l" v) D/ n0 ?8 V
  5. >>> math.log(10)9 E' C4 r0 g4 d1 V, V
  6. 2.302585092994046
    + y2 {3 _! @# O5 g! o4 O; ]3 j
  7. >>> math.log(11)
    ! l/ D& t6 C2 Y. F& H2 M
  8. 2.3978952727983707" b& Y4 B- O& W' @( x% n
  9. >>> math.log(20)
    , U2 d8 {2 H5 Y
  10. 2.995732273553991
复制代码

5 i+ f# ~2 |# t% e8 S- Pmath.sin(x)  求x(x为弧度)的正弦值7 {9 D$ e& I% i" h) S
  1. #求x(x为弧度)的正弦值
    2 p0 K6 y! I4 C, b1 b
  2. sin(x)9 f$ E, @  |8 e6 }7 p
  3. Return the sine of x (measured in radians).
    6 a3 c8 r/ r. Q# U+ a* T
  4. >>> math.sin(math.pi/4)% u4 X+ _2 K4 l9 o
  5. 0.7071067811865475+ u% W# v; B1 |" C& R- r; f4 g
  6. >>> math.sin(math.pi/2)
    2 H$ R6 C3 M3 W5 @) i7 S+ c- d- L
  7. 1.0
    " a% V; M7 S- d
  8. >>> math.sin(math.pi/3)
    ( i. r6 r) `0 n5 f' M$ N" u4 ?
  9. 0.8660254037844386
复制代码
& @1 ]! I& C; H; j
math.cos(x)  求x的余弦,x必须是弧度
* j0 h- _1 _& q" ]1 z: V% l
  1. #求x的余弦,x必须是弧度
    6 L& d' c: n" {: m" R
  2. cos(x)
    ' r) r" j. [' g& c. U4 ?- B1 _
  3. Return the cosine of x (measured in radians)./ k* J. f/ ~# _# I
  4. #math.pi/4表示弧度,转换成角度为45度
    , h% W8 r4 _% n9 b/ [" |5 s
  5. >>> math.cos(math.pi/4). w3 u8 i- _: c# o, d
  6. 0.7071067811865476
    ! s% H& F8 A( v5 U6 H5 [
  7. math.pi/3表示弧度,转换成角度为60度
    : k+ \8 g! l9 J1 T/ l* X2 J8 w1 h  j% D
  8. >>> math.cos(math.pi/3)5 h# N" l- v7 e) |: \
  9. 0.5000000000000001- s0 O8 N$ S: i1 e
  10. math.pi/6表示弧度,转换成角度为30度/ B" O) O6 O- o3 }. s. L
  11. >>> math.cos(math.pi/6)) ^& A. n  b' Y+ B4 ~
  12. 0.8660254037844387
复制代码

$ Y) i( c+ D$ A9 j7 Wmath.tan(x)  返回x(x为弧度)的正切值
) i  \% i( q6 H% Q7 ?
  1. #返回x(x为弧度)的正切值: U8 V7 W6 L( S" e; C5 W/ ^$ X" G5 O: c
  2. tan(x)( B, H, N+ L- j3 I1 K
  3. Return the tangent of x (measured in radians)./ t1 ?7 }7 Q) m
  4. >>> math.tan(math.pi/4)0 m9 N5 Y: `: v/ ]. b
  5. 0.9999999999999999
    . Y( W* }. ]# {( p
  6. >>> math.tan(math.pi/6)
    5 P) o2 ?; n2 ?. h5 l5 O7 i
  7. 0.5773502691896257
    ; ^! @) o' v8 ]/ s. W; S
  8. >>> math.tan(math.pi/3)
    # q; y, l, j( B
  9. 1.7320508075688767
复制代码

1 x% p7 \8 i2 s* K' Wmath.degrees(x)  把x从弧度转换成角度4 G: L" ]8 I# m$ v5 E9 }7 s# V
  1. #把x从弧度转换成角度
    ) r4 k0 `( ~8 @8 ]$ M5 d* |9 _
  2. degrees(x)
    ' Y$ u" O9 L- v( N/ z3 ^: b& I  V
  3. Convert angle x from radians to degrees.5 ]5 j1 a# w! N+ s$ m/ F

  4. 5 s( h1 @$ B9 X5 f% i) Q
  5. >>> math.degrees(math.pi/4)
    3 T) t" F4 [& ]6 u9 Z2 q8 ~
  6. 45.0
    6 |. [2 W+ I. P! u3 U0 f: Q- O! I4 b6 Q
  7. >>> math.degrees(math.pi)' M  z* y# d! `
  8. 180.0
    ) G  b8 h! h# t4 e. j5 T
  9. >>> math.degrees(math.pi/6)
    1 _4 {& F4 u3 v- b
  10. 29.999999999999996$ D  E$ m6 j+ i; F8 V' F
  11. >>> math.degrees(math.pi/3)/ f; S) r- q/ }+ a( v* \$ j2 E- v: l
  12. 59.99999999999999
复制代码
9 [6 i* v7 @3 s2 {" y
math.radians(x)  把角度x转换成弧度
' C2 P: K3 N9 a/ }9 _% v  D
  1. #把角度x转换成弧度" T, S: L2 M: z9 L2 G; k3 \2 c, L% P
  2. radians(x)  j+ a; Y& [* h1 l0 n
  3. Convert angle x from degrees to radians.
    & u# g, U' w0 G, A" f( A
  4. >>> math.radians(45)
    8 d8 H: T5 Q$ U  t' D
  5. 0.7853981633974483% u' _, x/ V0 a9 s, g/ N
  6. >>> math.radians(60)
    5 ?9 h2 H1 h1 y% i: P2 ~
  7. 1.0471975511965976
复制代码
; f' ~+ y+ A8 V; C4 Y; F
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
2 w# m, ?$ Y$ ^5 N: ^, p. O/ G
  1. #把y的正负号加到x前面,可以使用0
    , P; |3 Q4 N: h
  2. copysign(x, y)
    9 _" q$ G: `3 f: i( a
  3. Return a float with the magnitude (absolute value) of x but the sign
    3 o: X, N( H1 K% C" c
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ( m- o; B2 z4 e- n( \0 U& C; W
  5. returns -1.0.4 s, q- w& t+ N* s/ O& [1 B) t; e
  6. 3 i% y: U: b( Y
  7. >>> math.copysign(2,3)0 n: Z+ F8 W9 H( N' u  `! ?2 p2 j8 i
  8. 2.0
    3 k+ k" H9 Q8 p, s
  9. >>> math.copysign(2,-3)
    ' e) x% X: e" x8 M* R
  10. -2.0
    : C, M, \) F0 u( T$ r0 T1 B
  11. >>> math.copysign(3,8)
    % G: n" T/ L2 f0 a0 Z
  12. 3.0# H. S( Q4 P# S, c8 s& _
  13. >>> math.copysign(3,-8)
    ; e& Y0 w2 B/ W; `4 s2 s2 F
  14. -3.0
复制代码

7 r1 p- h7 a) Nmath.exp(x)  返回math.e,也就是2.71828的x次方
2 ~# u! ~* B% B7 I. ]: }
  1. #返回math.e,也就是2.71828的x次方
    6 m8 o. H* J: D
  2. exp(x)
    3 p9 [1 a6 j1 S; Z
  3. Return e raised to the power of x.
    0 z$ I& Q( J/ H
  4.   `7 C$ R! g4 X' W- a) z1 Y
  5. >>> math.exp(1)
    ( L0 Y7 q2 C7 q. Q+ s5 @
  6. 2.718281828459045) t  o9 k9 O  D4 @( O3 X- \
  7. >>> math.exp(2)
    2 t. |9 x4 v& j& f
  8. 7.38905609893065, e& Y# l" o* Z1 w0 F3 |
  9. >>> math.exp(3)7 B) x: F6 R: w5 e, D
  10. 20.085536923187668
复制代码
. q+ j# Z, |$ d/ |
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
  I7 T: Z$ {3 l7 ~$ D* D; ~2 y! B
  1. #返回math.e的x(其值为2.71828)次方的值减1+ @4 Q1 @" ~! A
  2. expm1(x)
    1 K* Z- Z0 y! {3 H9 S) V
  3. Return exp(x)-1., F8 `' k- n5 ]% q  i) }0 O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    . _0 a. V& S, v8 U9 O+ w! z+ H7 \
  5. 2 b- ~# w' d, |/ I( T3 ]
  6. >>> math.expm1(1)6 c2 ~& N+ I8 f  P! }
  7. 1.718281828459045
    7 X( i, d, P4 G" F/ K( |' o
  8. >>> math.expm1(2)
    - E# u! ]' b, ~) |: A7 M' Q
  9. 6.38905609893065) E! `3 r# h" j0 p/ M% ?
  10. >>> math.expm1(3)
    1 y0 j7 F7 R( n* X# \
  11. 19.085536923187668
复制代码
3 x3 q2 a" {5 a8 A$ u
math.fabs(x)  返回x的绝对值9 t* r0 P) D+ @9 Z" |3 |
  1. #返回x的绝对值* g& C, k! \+ A1 W9 E7 ~
  2. fabs(x)9 D9 l* [7 _5 {/ @
  3. Return the absolute value of the float x.& ^  K% `: u8 D1 ]

  4. 5 m( r7 T+ Q, @( @& A  ?9 ?
  5. >>> math.fabs(-0.003)" N5 x9 q9 a; ~$ y6 n. r# [
  6. 0.003
    & f4 Q/ B2 o8 Z# ^& N4 t1 r0 m
  7. >>> math.fabs(-110)
    1 O& s: k. H) l
  8. 110.01 T8 F! q) U4 e1 T8 t
  9. >>> math.fabs(100)
    0 z9 j/ K' ^- k# F9 x  V7 F
  10. 100.0
复制代码
5 e/ ]7 R% n4 }9 \; i+ v5 x  G* l
math.factorial(x)  取x的阶乘的值
- }# B: S! e) L' C7 d
  1. #取x的阶乘的值' g  Z! N1 L, Q5 {& z% ^( E8 m0 e
  2. factorial(x) -> Integral% r% \* E2 c% i. G+ T1 {
  3. Find x!. Raise a ValueError if x is negative or non-integral.2 d5 h3 e+ L. M" P
  4. >>> math.factorial(1)2 e5 I7 {" |8 d' b* L3 J
  5. 1
    6 c* V3 ?/ @& n% U3 a
  6. >>> math.factorial(2)
    3 V: m* d) [! w% C
  7. 2. u/ V, F8 G& [8 T. L; I& W2 l
  8. >>> math.factorial(3)* S: k( Z# e# Q3 G! F
  9. 6. x+ F  ?/ U6 C5 H
  10. >>> math.factorial(5)8 e8 x) E- u9 I5 X) L
  11. 120
    7 i; i5 P. B, b6 R4 G0 J
  12. >>> math.factorial(10)9 E8 t0 }# w2 Z0 \" B1 ?
  13. 3628800
复制代码

% M; \3 u- ~6 h' M, Emath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
- F3 {' Y0 c+ m2 V5 r7 W8 s
  1. #得到x/y的余数,其值是一个浮点数3 S7 C' ?4 k! _$ \2 E% T
  2. fmod(x, y)
    0 d; Z% O- z8 g
  3. Return fmod(x, y), according to platform C.  x % y may differ.1 w0 S: ]. n5 j- Q. C( {
  4. >>> math.fmod(20,3)
    2 ~8 m1 B' ^6 W
  5. 2.0
    5 Q: [+ [, s  `- p; p
  6. >>> math.fmod(20,7)7 n+ O8 {, k( u6 `, T5 k. q+ k
  7. 6.0
复制代码
5 u2 |9 u& Z% ?8 u7 S
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! R" `# E. ]0 y+ Y2 U+ |6 [
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    : |. J1 I. R" P' Q0 h
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    9 T0 i! D5 U2 g$ M
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ) }& t! V- ]# i. q! |! Q0 X: x
  4. frexp(x)
    3 c7 V1 Q3 X8 O' ~- u6 l
  5. Return the mantissa and exponent of x, as pair (m, e).
    $ S: ]" N/ x: A; m  o4 p$ [$ n
  6. m is a float and e is an int, such that x = m * 2.**e.
    6 j2 `* w+ Z' ^5 m" g3 Z$ x
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.8 i1 n0 Q+ e% ?. S( `
  8. >>> math.frexp(10)
    3 s" M3 w! {4 F2 C3 h
  9. (0.625, 4)
    : I0 w. [$ V+ Z! H! x5 }2 ]0 y& ^
  10. >>> math.frexp(75)
    ; Z& Y! {9 b; I  W
  11. (0.5859375, 7)+ {0 d  |9 H" A& _6 [  v
  12. >>> math.frexp(-40)$ ]% C/ N) l* w; ~7 [5 M: t
  13. (-0.625, 6)
    ' h  Z* p  ^9 X2 @' u' e
  14. >>> math.frexp(-100)$ W6 K  H3 a- e; ~  s$ g  `
  15. (-0.78125, 7)
    6 X3 i8 N5 m" O( E9 r
  16. >>> math.frexp(100). W- _7 G( s% _7 a( {* k6 r' S
  17. (0.78125, 7)
复制代码
. M, h3 d9 j2 V2 }! @! B8 }
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列+ P( p- P, s, E) m& n) F! Q/ l
  1. #对迭代器里的每个元素进行求和操作
    % X& A/ F# L7 }: h9 ?/ A4 Z
  2. fsum(iterable)& T+ n8 B: |) n9 P& l
  3. Return an accurate floating point sum of values in the iterable.: x6 H$ s/ u7 G( g
  4. Assumes IEEE-754 floating point arithmetic.
    3 Q4 s4 ~4 }0 U' ~& Y4 [  c
  5. >>> math.fsum([1,2,3,4])
    / f: d4 C. ~5 B$ i, I
  6. 10.0
    5 P/ W7 J9 q0 q9 y" m
  7. >>> math.fsum((1,2,3,4))
    ( V8 X% w' b7 B' L
  8. 10.0* Y% O6 _! o$ e" m, [+ h6 {
  9. >>> math.fsum((-1,-2,-3,-4))
    6 T8 x. A3 Z4 ]8 {7 W8 r$ O4 H
  10. -10.0% c# F" M* ~( R5 R$ n
  11. >>> math.fsum([-1,-2,-3,-4])
    # E" Q8 ^5 i" y+ {
  12. -10.0
复制代码

6 K7 r9 R/ l8 b- a) m% Z6 [. q0 Omath.gcd(x,y)  返回x和y的最大公约数
! t4 q3 W0 B0 J% U; R: s
  1. #返回x和y的最大公约数7 @5 A9 j' U" v0 R0 T2 H, Z
  2. gcd(x, y) -> int
    : s- h6 x. i# c9 S7 c$ s8 ^
  3. greatest common divisor of x and y
    4 n! G; i- Z4 J
  4. >>> math.gcd(8,6)
    " r0 U3 U* g- P9 O
  5. 2
    ' J: \5 @+ P! Y0 E; ~+ Q- v
  6. >>> math.gcd(40,20)+ K1 G5 _% f3 P& c. J, Y. [8 Q
  7. 203 a' w) r3 B+ e) L& T. g9 d/ G
  8. >>> math.gcd(8,12)
    ' w, n5 C  u; r+ O' U# q. `
  9. 4
复制代码

( n& s5 f; X! b- B1 A, j( Vmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False, a. o- y  e9 l: O7 X4 ~
  1. #得到(x**2+y**2),平方的值
    7 U! Y& `7 S  ]' K1 K' N
  2. hypot(x, y)
    3 S' w) d6 U2 w- T. n% ~- M% P
  3. Return the Euclidean distance, sqrt(x*x + y*y).) k/ T3 V, f7 o; m  u. w$ C
  4. >>> math.hypot(3,4), z0 e! |3 ]- s9 @/ a
  5. 5.0' ]) [* D( _4 U% u# m
  6. >>> math.hypot(6,8)* {% R, f% C$ S) C" N8 P
  7. 10.0
复制代码

. o. L2 N$ n$ a! q  a3 Mmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
# y* [* ?( q+ {4 [
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    : |  ^2 c3 c( g3 E
  2. isfinite(x) -> bool3 b$ n9 [( v. O2 p2 x% g8 F7 ~% F
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    * J" t+ K) A1 j
  4. >>> math.isfinite(100)
    3 `0 t% g, t, K# q" f
  5. True
    ' d' D. `& `2 d1 L
  6. >>> math.isfinite(0)
    + s7 b. I$ U% E6 K
  7. True
    ! N% H# ]  u" P( l! Z; U" b0 y
  8. >>> math.isfinite(0.1)6 ?* e! y  C: E/ ]3 q( S9 x5 L) Y
  9. True/ |/ V. ]  D4 \# J$ l" F
  10. >>> math.isfinite("a")
    * [, Z/ `1 E  {2 U5 p
  11. >>> math.isfinite(0.0001)" E+ U! F1 C4 D% k" O
  12. True
复制代码
# v% ^0 j- }) W, p
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False5 V/ [3 ^3 I1 k& U6 ^
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False0 [9 O1 T# K* V6 N  y
  2. isinf(x) -> bool2 f4 A/ M6 I* _8 r
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ( B' E! ?0 I$ M# r" O+ R/ |! a' p
  4. >>> math.isinf(234)* B+ A8 K, S2 p2 G7 Y9 b# A0 Q2 |
  5. False
    * T2 z6 }, b& R0 _+ o. u
  6. >>> math.isinf(0.1); b- ?( w6 A# \; O5 ~  ~1 f' D
  7. False
复制代码

9 o* \" u' z, C2 m% Hmath.isnan(x)  如果x不是数字True,否则返回False2 l$ R0 a" S* r; ]5 V' Z. o
  1. #如果x不是数字True,否则返回False8 D9 l( B' |* _. F9 i8 I! k- T% |
  2. isnan(x) -> bool. A- \" A3 ^4 o) X1 w
  3. Return True if x is a NaN (not a number), and False otherwise.
    . f* s! s8 g7 m: w2 k- G4 T6 N! ?9 H
  4. >>> math.isnan(23)
    + l8 K$ U( o* B; O( I( g
  5. False5 `, q4 `. u# w- R$ F$ W( ]) d
  6. >>> math.isnan(0.01)7 v' D1 Z  P4 K
  7. False
复制代码
) Y# M/ V( l3 Y* d3 O, f
math.ldexp(x,i)  返回x*(2**i)的值$ H7 U4 O9 n. A% y5 w
  1. #返回x*(2**i)的值
    ! u0 [  X- J8 x7 F1 }. ]8 Y: N
  2. ldexp(x, i)' Q; b" j  t; u  L. v
  3. Return x * (2**i)./ u' T, o; e- F
  4. >>> math.ldexp(5,5)
    ) l8 t. D" N# J# i2 w& s7 K
  5. 160.0" ^; U, C* g7 z* A4 b% y3 A2 O
  6. >>> math.ldexp(3,5): t4 T; u( {6 K# {9 T% i3 n
  7. 96.0
复制代码

" u5 q; z& s+ W2 Q9 Umath.log10(x)  返回x的以10为底的对数
  x4 K1 z' W  Y. r8 S) V; s
  1. #返回x的以10为底的对数
    9 c* Y+ q+ `! e% _" s4 O
  2. log10(x)* z! E1 @! j) ]3 U" ^; `5 z5 {3 Y0 V
  3. Return the base 10 logarithm of x.% ^4 Q7 E2 _  Q, ], |9 n) Q
  4. >>> math.log10(10)
    8 T! t; O& G3 Z+ D2 L# u
  5. 1.06 k4 q0 Z! n! I9 ^* \+ b1 Z$ G
  6. >>> math.log10(100)
      v% v/ |  q; U9 q; s/ J
  7. 2.0
    ! g+ k$ Q! b+ _# D7 C, [- Q1 Y+ ]
  8. #即10的1.3次方的结果为20
    , _3 f2 O+ |$ [( V: k
  9. >>> math.log10(20)
    & d. n% V/ n9 f0 L4 o
  10. 1.3010299956639813
复制代码

' ~3 A4 o% ?; x' y0 M& pmath.log1p(x)  返回x+1的自然对数(基数为e)的值, c% W! k0 o* |9 t
  1. #返回x+1的自然对数(基数为e)的值$ d: {( `" @, i2 l6 N* \
  2. log1p(x)7 s& u- U! D! J! I! Z7 L/ n% ]
  3. Return the natural logarithm of 1+x (base e).# i4 z6 e2 k+ W. Q+ w: ]  e1 O
  4. The result is computed in a way which is accurate for x near zero.
    2 Q5 P: V2 f3 w  o
  5. >>> math.log(10)0 U8 V& H1 y# e( u
  6. 2.302585092994046- j  S3 g$ @& n% a% c( r
  7. >>> math.log1p(10)% S: W" s, v+ M6 y0 F  p
  8. 2.3978952727983707$ v3 R% g6 b6 ^1 ]" k" B
  9. >>> math.log(11)$ e, y( }! Q) }
  10. 2.3978952727983707
复制代码

) W( B# _) x9 omath.log2(x)  返回x的基2对数
$ @/ _' W# ^2 u( n
  1. #返回x的基2对数
    4 [$ f8 P& r. P7 \$ x( k
  2. log2(x)) t' V# {2 F) t4 m4 M
  3. Return the base 2 logarithm of x.; j- u; I# V4 ]$ V4 d  v5 {7 W! `! f/ u
  4. >>> math.log2(32)
    % N0 ~# q& {" h, F8 R9 X- i
  5. 5.0$ F/ w; _, x" U( W0 l$ Q  d" o
  6. >>> math.log2(20)
      w5 Z( }% N0 q9 m" w% N6 S
  7. 4.321928094887363# i; R$ ]; Z/ s  f. ^5 @9 ]4 K* ~
  8. >>> math.log2(16)2 c' D2 ~: C+ t- S7 \' ]  Q) y
  9. 4.0
复制代码
' [. \* D  M! n- s: Q; t, k
math.modf(x)  返回由x的小数部分和整数部分组成的元组
& x/ K( g: [$ w( k
  1. #返回由x的小数部分和整数部分组成的元组- p7 a. P: O1 v" G0 {  Q
  2. modf(x)
    0 X3 B( a0 {; F2 n' j: W
  3. Return the fractional and integer parts of x.  Both results carry the sign! m$ Z& A. w, q0 Q9 B5 j2 E
  4. of x and are floats.( T3 B2 ?+ R+ R" x) l0 v
  5. >>> math.modf(math.pi)/ b; D4 W- K2 B/ N  R
  6. (0.14159265358979312, 3.0)! e- K6 F: U: f  d" g2 H
  7. >>> math.modf(12.34)
    5 J. ^( H! e; G# m
  8. (0.33999999999999986, 12.0)
复制代码
+ D9 t4 ~3 ?' S9 I4 O
math.sqrt(x)  求x的平方根
1 z! a" Y- |6 y" w6 ?5 ?" x- Y; v
  1. #求x的平方根& f8 X7 p  a) `9 w( a6 o; Y! P" C
  2. sqrt(x)" s9 _" u; z4 w9 Z0 D
  3. Return the square root of x.
    ' ^( R. q8 ?) b
  4. >>> math.sqrt(100)
    ! j" A& n5 E6 I: n4 t3 y( {
  5. 10.0
    , r# [1 l3 |) ~4 ]  r- Q
  6. >>> math.sqrt(16); O/ j1 S3 s5 E* |
  7. 4.0" l3 u1 ?! ^# |' b
  8. >>> math.sqrt(20)
      R8 _, z& Z* C! v
  9. 4.47213595499958
复制代码

4 }0 c4 ^# O0 G6 D$ xmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 @; B6 _1 Y$ _: |
  2. trunc(x:Real) -> Integral
    - x6 i. D" ^: @. L6 |! W+ j
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# J3 O/ r1 o( N' ^  ^# t
  4. >>> math.trunc(6.789)
    + j0 Q4 v0 V. O" a
  5. 6
    * @) D, f' }0 \& l  p2 A" e
  6. >>> math.trunc(math.pi)
    ( x1 B1 J" T! ~5 U" y$ C
  7. 3- y$ K9 P" D$ O. \, h
  8. >>> math.trunc(2.567)" Z* {& W# b1 c' e  U7 z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-15 00:23 , Processed in 0.075502 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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