新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
- e% G9 Z; U% D; R. L; @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。; |- i  q% C$ S8 t+ G% v
1 `' J4 ?0 n  i5 h6 W+ ~
方法1
! O, _# f  ?/ M+ r8 {5 w" q- J
  1. >>> import math
    / J) x! v2 ?& G; Q- d$ `
  2. >>> math.sqrt(9)
    8 H! a# D2 t$ ?  i/ D* v, P
  3. 3.0
复制代码
方法2( \7 \* C, {" X, [2 `" e; z  |
  1. >>> from math import sqrt9 O8 h6 Y( n- [, |( o
  2. >>> sqrt(9)) j( X+ r7 e- b! j
  3. 3.0
复制代码
4 _7 B4 X/ D4 ^; t4 \

$ Y: s' H  r& N7 `
math.e  表示一个常量* ^% P% m4 a: K' d' {* u
  1. #表示一个常量! z! }: ?1 G2 f+ t) v
  2. >>> math.e2 B" }" ?0 A; b, E: w, M6 S! Q, L
  3. 2.718281828459045
复制代码
7 m1 }+ ?" V* Y/ B
math.pi  
数字常量,圆周率
5 u$ B5 B2 t* p/ H2 Z2 Y0 K' |& x
  1. #数字常量,圆周率; p5 z) f/ J  H1 t3 c) U' M
  2. >>> print(math.pi)" i5 I- Y! S) c- O6 m
  3. 3.141592653589793
复制代码
2 ]8 v0 l9 }" z& m, S3 @0 U. Y4 @
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 x7 \. m( }- |0 y% V( Y
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    1 R% A( R( ~! |: |0 b
  2. ceil(x)6 Q3 Q3 g: R$ O# V- h5 N- G
  3. Return the ceiling of x as an int.
    ) M5 t( r! b/ \- ~
  4. This is the smallest integral value >= x.0 O* d; x( G; J( E. u

  5. 5 ]1 x  ^& U) \. Z
  6. >>> math.ceil(4.01)
    " |. F( \! Y) T
  7. 5
    # e, T! s0 J0 a" b
  8. >>> math.ceil(4.99)" ?. v4 P' C, J2 \# B2 w
  9. 5( l1 n9 P+ u( ]8 F% N% V3 a
  10. >>> math.ceil(-3.99)8 V2 i+ u5 l$ Z6 ^9 k- f
  11. -33 w1 l" }5 y: T* @, B% R
  12. >>> math.ceil(-3.01)
    % s3 u( ^5 C& d# o
  13. -3
复制代码
$ T3 E3 V/ r7 y+ G
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 H6 `$ \9 Z0 J
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 T! s9 t8 @6 `; a0 L/ L& @4 |- S
  2. floor(x)) |5 r% u6 S1 D5 g+ c: \
  3. Return the floor of x as an int.
    * T# s$ L3 R- A* D2 w
  4. This is the largest integral value <= x.
    6 u8 ]; [; ?* ~' L
  5. >>> math.floor(4.1)
    0 r( ^+ `, z1 f
  6. 47 t7 I% L# M0 x1 |+ g0 F9 ^7 \
  7. >>> math.floor(4.999)3 [1 s# E) h- V' n' E, x- Z# T" }
  8. 4/ u+ Q- K  C% X# \: q
  9. >>> math.floor(-4.999); U% ^$ _* \7 [1 t
  10. -5" `5 e, C0 f1 e( B, Y' ]
  11. >>> math.floor(-4.01)# @( C+ Z: L9 E, h1 ]$ z
  12. -5
复制代码
( ?) j6 i8 E# [) d9 ~7 D
math.pow(x,y)  返回x的y次方,即x**y% v$ S8 R; [' J4 |1 J
  1. #返回x的y次方,即x**y+ Z4 }4 X2 [2 M. U+ E# y1 j5 t, u0 h
  2. pow(x, y)) Q* U, l) e% k
  3. Return x**y (x to the power of y).
    * S( A* ]/ S% R! o- @
  4. >>> math.pow(3,4)1 ^; m& m. w) y; b
  5. 81.0
    : |2 u3 f# g" k8 @
  6. >>>
      g% w( c; _" o7 U) j: l" C7 B3 h
  7. >>> math.pow(2,7)
    & s6 m$ P& B' J1 l3 z
  8. 128.0
复制代码
2 [) y* M# W! j* U! P
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 y# T# L/ Z$ `, X  b9 i
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# i: f# r; \5 ^" d* _; M! P( ?
  2. log(x[, base])& o; S' V, p6 a. m) E: |1 S' i+ A, s, g
  3. Return the logarithm of x to the given base.0 `7 t* e2 _9 j4 g3 v
  4. If the base not specified, returns the natural logarithm (base e) of x.& ?+ p; ]  |9 E( t' T  L
  5. >>> math.log(10)
    ! ?2 L- U* `; _! Y2 B7 t- V  a
  6. 2.302585092994046. F7 d5 }, e' X; j5 N0 q! p
  7. >>> math.log(11)
    0 G7 G6 X, ]! m! K  U# B
  8. 2.3978952727983707
    ' Q4 B3 f9 a" J$ a9 O' T1 b; P
  9. >>> math.log(20)
    $ t( Y; i2 e0 ]8 c" N+ t
  10. 2.995732273553991
复制代码

( x( M3 f8 E# ]. b2 v! e0 \! I2 b! V* \math.sin(x)  求x(x为弧度)的正弦值/ ?2 w# o! W7 ^0 s, ^) B9 g; H
  1. #求x(x为弧度)的正弦值
    & C6 S& ^' G$ W; ^
  2. sin(x)
    1 v' ^4 Y6 v* [- C0 J
  3. Return the sine of x (measured in radians).
    * v3 l' H9 h1 t7 ~8 s$ Y
  4. >>> math.sin(math.pi/4)
    " X- q' R4 g2 n& R
  5. 0.7071067811865475% U' W) H* \& f
  6. >>> math.sin(math.pi/2)" S& K3 U; h8 A9 z% E5 T+ `/ H4 y# W
  7. 1.0/ z0 G! S; z7 m3 J9 g+ D# u
  8. >>> math.sin(math.pi/3)) `% Y. D4 P, ?
  9. 0.8660254037844386
复制代码
- t* I8 x! D: @( i* ^: r
math.cos(x)  求x的余弦,x必须是弧度
! w5 [* Y+ ?; X
  1. #求x的余弦,x必须是弧度6 T, N2 S7 o% ^, E4 X  y: \
  2. cos(x)* P5 g4 {' Q, ?2 i! O
  3. Return the cosine of x (measured in radians).
    8 [: W0 [9 x0 k1 T) ^
  4. #math.pi/4表示弧度,转换成角度为45度
    ! u" M% Q0 g2 C) t; l
  5. >>> math.cos(math.pi/4)5 q; w6 B$ L$ w! g( W+ B
  6. 0.7071067811865476' r/ ?/ q3 D, ]) p9 R; Z; J
  7. math.pi/3表示弧度,转换成角度为60度
    1 k- m# @& C* U* h- j3 f. ]7 T
  8. >>> math.cos(math.pi/3)
    - m: X9 C  J& D# p! ]! f
  9. 0.5000000000000001
    ; [& v/ u/ i; N8 C6 ]  t6 T
  10. math.pi/6表示弧度,转换成角度为30度
    5 B; D, Q7 g( z+ e+ E( t$ ?+ [8 K9 W
  11. >>> math.cos(math.pi/6)
    # L; j9 \8 T3 x; n
  12. 0.8660254037844387
复制代码

8 q; t' R5 K5 S/ q& p  \( V( Fmath.tan(x)  返回x(x为弧度)的正切值' N4 e! A) Z* j, t$ p
  1. #返回x(x为弧度)的正切值
    6 a1 ]1 \/ a) n9 |
  2. tan(x); F  z- i# ?0 v$ c+ t
  3. Return the tangent of x (measured in radians).1 G( ]% g. N  Y# {3 R
  4. >>> math.tan(math.pi/4)
    & a/ i- |% P; P# N% k( X- O
  5. 0.9999999999999999
    ; P6 L0 X0 i8 e5 ]2 \
  6. >>> math.tan(math.pi/6): M9 d$ r5 |) \+ z& j! Q) p
  7. 0.57735026918962571 ?$ A. a' W# O
  8. >>> math.tan(math.pi/3)
    5 z8 D( x; o& t6 X, s
  9. 1.7320508075688767
复制代码
, F1 V0 W/ f0 q1 j$ d
math.degrees(x)  把x从弧度转换成角度2 x0 W5 W$ u) t. E! O7 F6 e" U, i/ @
  1. #把x从弧度转换成角度
    0 V2 x% ?  E# ~2 e& E2 z
  2. degrees(x)) I! H% n" U; ]# X* V
  3. Convert angle x from radians to degrees.! K$ ~: g6 C+ O9 v% ?8 V: L
  4.   }# W# ]+ t6 K7 F+ R/ g) Q6 d
  5. >>> math.degrees(math.pi/4)7 i8 ~! A" t' i% i' e
  6. 45.0: b* Y7 ^  N) p' B# W6 W
  7. >>> math.degrees(math.pi)
    ) z/ a* o3 c; }5 o# y) W% Z
  8. 180.0
    2 r6 ?3 y* l# a# h1 F& c+ m2 \2 X
  9. >>> math.degrees(math.pi/6)
    - D/ ~9 [( ^9 J5 d% {9 h1 X5 r
  10. 29.999999999999996
    + n6 T* o5 i: M( ]: X
  11. >>> math.degrees(math.pi/3)
    8 t, S  t% s2 Y3 Y
  12. 59.99999999999999
复制代码

3 S1 \4 u4 k7 V5 xmath.radians(x)  把角度x转换成弧度
' ?0 m* r4 a- r+ A
  1. #把角度x转换成弧度9 z- J. h& ?# Z, X/ G* X' W
  2. radians(x)5 v9 t$ H" S! {$ @- O( k) E
  3. Convert angle x from degrees to radians.( t" j1 U! c& B/ Q6 o
  4. >>> math.radians(45)
    . r" t- X- n  B9 q' R
  5. 0.7853981633974483
    - i. n  B7 t5 ]
  6. >>> math.radians(60)
    & ~, Q: b0 |  I$ M' J9 U
  7. 1.0471975511965976
复制代码

4 G2 ^8 x7 O' U2 m& gmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
* o, c* f- I8 |: ~! b1 d# R2 x$ v) K8 M
  1. #把y的正负号加到x前面,可以使用0
    9 \3 n% [2 P* A. s1 W
  2. copysign(x, y)" P$ f( f& y. ]. b: a2 b, B% `  h
  3. Return a float with the magnitude (absolute value) of x but the sign ' p- R$ x' D$ O8 C, F! l1 B9 K  @0 a
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 S3 h. o- J, O+ D) D
  5. returns -1.0.
    * E" z$ h1 g. E+ `2 g

  6. $ b$ _: d1 Y+ B/ E8 q4 e
  7. >>> math.copysign(2,3)2 f1 A8 A' |8 |8 e5 A
  8. 2.0
    & j$ c# g  m& m( k
  9. >>> math.copysign(2,-3)
    " A# z5 ~9 E  V# x1 E2 s8 r
  10. -2.0
    3 Y" A. D1 H0 f6 [
  11. >>> math.copysign(3,8)7 a, M9 g# O1 T* ^, K; ]# l  k7 U
  12. 3.0
    " e% G/ @# J' z$ D! x
  13. >>> math.copysign(3,-8)
    % r$ O5 h# k* Y- t6 M
  14. -3.0
复制代码

& @8 a2 U  x! X1 A1 K7 Rmath.exp(x)  返回math.e,也就是2.71828的x次方5 S; g7 B. B8 O+ T, q7 B, q, L
  1. #返回math.e,也就是2.71828的x次方
    " D9 N1 [: b! n+ N
  2. exp(x)
    / g7 ^8 c  ]* |. _, ]' t( v3 a
  3. Return e raised to the power of x.' c' [4 i6 k4 o1 o: X2 B
  4. ( n* ~6 @% q2 G( w. @0 K8 `8 g" S
  5. >>> math.exp(1)# y% r, z7 e4 Q2 l
  6. 2.718281828459045
    - ~4 s( R# i* c% x
  7. >>> math.exp(2)
    & W$ o3 ]" a& D
  8. 7.38905609893065- f. R( P$ l, L$ W. ?2 C
  9. >>> math.exp(3)
    7 p% C4 r) s& I" Z) P6 K0 R
  10. 20.085536923187668
复制代码

) G" e: J! K- o! m5 O6 G9 v" `: fmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
* A% J# b6 \. D: n. N1 ]7 `4 V
  1. #返回math.e的x(其值为2.71828)次方的值减1# e; r( C* l& o
  2. expm1(x)0 E3 b/ p9 Q3 b, N% w
  3. Return exp(x)-1.* Y. }( l, C4 q9 f
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 v; i6 Z: m* c1 w9 D  T' `
  5. & A8 v0 z) j4 ^3 a3 H
  6. >>> math.expm1(1)' L" M' }+ A; \: d
  7. 1.718281828459045
    , R( K$ I7 m& t
  8. >>> math.expm1(2)0 W1 J; w5 x% ^9 A% M! G
  9. 6.389056098930653 ^. ]# M1 _( \3 W, K
  10. >>> math.expm1(3)
    ' J! U) a2 K; l
  11. 19.085536923187668
复制代码
1 Y- u5 V' |* C
math.fabs(x)  返回x的绝对值
! \4 x' I2 g; P  A8 @, z
  1. #返回x的绝对值6 T% P$ b1 `! o% k) i+ Q0 c
  2. fabs(x)4 M5 x1 {" L7 t! g' E$ Y) G
  3. Return the absolute value of the float x.# j" v2 w6 ~0 g! s

  4. ! ~, t. S6 }  X6 k' Y
  5. >>> math.fabs(-0.003)1 @$ Q/ B7 C/ `/ t( c# n6 ^1 R0 p
  6. 0.003
    - U. q* D# b' R' Z! t* Y/ h
  7. >>> math.fabs(-110)
    ; b! O, l) o* E" c% T% B0 A; d
  8. 110.0
    1 b5 J" a% @" P7 P% j, S
  9. >>> math.fabs(100)  x+ b% Q* O( Y$ |5 U9 T4 Z' A8 f
  10. 100.0
复制代码
5 a& L/ ]) f1 S% W, `/ B! e
math.factorial(x)  取x的阶乘的值
9 a2 Z2 n& a* F# l+ a% m3 o
  1. #取x的阶乘的值
    3 r+ G6 K* h& q* Y
  2. factorial(x) -> Integral
    # [, l1 S5 d5 T& P# Y* R8 {0 u( L
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ! j) l/ p7 ^# l9 Q; j
  4. >>> math.factorial(1)
    ! d6 z6 B6 |9 }' B+ @. M
  5. 1
    8 L" H3 e: B7 i0 ?8 E
  6. >>> math.factorial(2)# }6 F7 s0 m) M% V
  7. 2
    9 J2 _" L8 r% ?5 q) W( k5 o
  8. >>> math.factorial(3)3 x, h6 u6 T2 t8 E  X: y0 O4 R" ^6 C6 P
  9. 61 q: b/ Z# ^9 V. `( c
  10. >>> math.factorial(5)" o" H$ Z; u% N8 h
  11. 120. Q2 o% `8 q% K/ S, r
  12. >>> math.factorial(10)
    - {% N) p  d9 S5 J  k9 M
  13. 3628800
复制代码
. }! Q1 }/ W( C: m& v
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数* y$ }7 P$ z) H4 _) X2 Q: l# y  X
  1. #得到x/y的余数,其值是一个浮点数
    ( s8 `# Q1 _! F  K4 n) z
  2. fmod(x, y)
    6 `: h; c3 P; B0 Q- S8 f
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    3 N, N9 p* R! ?. x
  4. >>> math.fmod(20,3)6 X: ^/ T) o1 E) B
  5. 2.0" R4 [* ?) w- h8 k
  6. >>> math.fmod(20,7)+ d/ X1 {# _! i4 Z- [
  7. 6.0
复制代码
5 b3 `0 g5 j0 [. l
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围% O+ j; W* T+ O/ ^4 k. A/ _  Q
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    & O9 W' m9 s( A! `
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ' w3 ^! c1 ~" X5 |9 _# f6 k7 @
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1+ A& [/ F* I( B, j3 K* X* A
  4. frexp(x)
    ( ~. D0 T8 P8 n6 t  Z! C
  5. Return the mantissa and exponent of x, as pair (m, e).
    % u- b0 }1 r9 G
  6. m is a float and e is an int, such that x = m * 2.**e.# \( ^) R( g: G* I% q, v/ q8 k
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0." f6 @; d! N" u/ d- d, I
  8. >>> math.frexp(10)8 O3 h$ W6 o# ^% ]) Z% H- P" \7 K
  9. (0.625, 4)
    " f1 F% F4 \* q. ?! @
  10. >>> math.frexp(75)" ]) b, S2 X% N% j4 H$ X! y
  11. (0.5859375, 7)* T( ]; M! ]: B/ t" O
  12. >>> math.frexp(-40)2 m/ c: u$ p; D! T
  13. (-0.625, 6)
    8 m: E) i3 x' U6 t: b% ]; z* w
  14. >>> math.frexp(-100)
    9 f) Q! R# T4 L" V( B# E8 l
  15. (-0.78125, 7)
    : k& M0 `9 E" H- S
  16. >>> math.frexp(100)
    ' K/ ?7 W' O+ ^' f3 y5 e  \
  17. (0.78125, 7)
复制代码

  b! L6 h4 f% Xmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
  n9 v8 w: c4 \! U! X1 j! N
  1. #对迭代器里的每个元素进行求和操作
    2 J6 |8 L: F0 R( z
  2. fsum(iterable)
    & b. P  j2 j7 Z1 t
  3. Return an accurate floating point sum of values in the iterable.
    * `, J+ ~9 M# }+ Y/ U" e
  4. Assumes IEEE-754 floating point arithmetic.
    1 F4 b* o) p$ h7 q
  5. >>> math.fsum([1,2,3,4])/ X- x( v5 H: `6 P' P; y9 t
  6. 10.0  D9 U8 a2 v9 P& B* B! b5 S* |
  7. >>> math.fsum((1,2,3,4)); X/ M' ]$ N8 e' v
  8. 10.0
    ( m1 i! q+ N# G1 l
  9. >>> math.fsum((-1,-2,-3,-4))
    # f& f8 l0 w6 h  e: A8 |7 w4 R
  10. -10.0
    8 B$ T( N3 C! M
  11. >>> math.fsum([-1,-2,-3,-4])
    3 d, I  n% ~& ]2 _0 y* O
  12. -10.0
复制代码
+ k7 s; y; B6 v! F$ l% {, v
math.gcd(x,y)  返回x和y的最大公约数
4 n( d( s7 d; Q! `! _4 s2 e8 X
  1. #返回x和y的最大公约数
    ' R  y2 y; b1 v2 A
  2. gcd(x, y) -> int
    : b+ A# _  H2 ?9 Q2 g
  3. greatest common divisor of x and y
    1 L, P) x/ R# v
  4. >>> math.gcd(8,6)3 J5 n. [+ F( h7 x& I9 x
  5. 2. `/ E$ z: w" Q* H# y' O; C2 x
  6. >>> math.gcd(40,20)1 j- l+ E) `$ r8 q! Q7 r+ ^
  7. 20
    5 e/ z5 W* P, D
  8. >>> math.gcd(8,12)
    # c) a' h0 R$ u; b; v; e
  9. 4
复制代码
$ w2 T4 E% B$ Q! K+ q' y9 o8 m1 R
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False; ~7 ?% c7 R/ p7 v
  1. #得到(x**2+y**2),平方的值
    & I& A) k+ [% g% v
  2. hypot(x, y)
    2 w4 T0 w$ m9 d4 U
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    $ n, J6 \: v& s
  4. >>> math.hypot(3,4)
    ; d! p; W- W* y5 S% r# e0 k/ I
  5. 5.0
    ) @7 E# `, r) l0 n5 X
  6. >>> math.hypot(6,8)
    1 [0 Z/ w8 z- A) x; h$ p9 x
  7. 10.0
复制代码

4 s! i1 X+ p& ^+ H; m8 U; X2 Z# vmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
; B# ~# d8 j  c4 B/ r4 b6 @2 X" u2 S% G
  1. #如果x是不是无穷大的数字,则返回True,否则返回False2 A+ v5 v# W: m6 F. D6 o
  2. isfinite(x) -> bool' c* x( A+ S! u  h4 q
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.* E& I& G8 h- I/ H
  4. >>> math.isfinite(100)8 [$ G2 r) w  K) A+ @! _
  5. True
    - X, a# ^9 c7 F5 ?6 \* V$ }4 K
  6. >>> math.isfinite(0)2 Y) e2 U& L2 A) T5 O  J1 G/ {
  7. True; L" K, w$ i  Z% D, a: K9 ^* _
  8. >>> math.isfinite(0.1)9 ~0 o. Q# P" y! ]$ x; R' c
  9. True
    : M2 ^4 d6 c' N, M4 g, j
  10. >>> math.isfinite("a")
    & ^! r9 a+ F  o+ F+ u' `
  11. >>> math.isfinite(0.0001), `9 P' b: O! s! r2 Y
  12. True
复制代码

* }' ?" y3 b% {1 tmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
) R4 V0 B  ~6 c3 E
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False0 E7 m" u8 a; S0 e5 Z7 X- s
  2. isinf(x) -> bool
    2 r; O7 }# E: `
  3. Return True if x is a positive or negative infinity, and False otherwise." f7 O6 w. ~& N2 G0 \+ i/ N
  4. >>> math.isinf(234)
    1 N. f; u" m8 a. h" E) |. _% C
  5. False  g4 j5 k9 Z. h! m9 j
  6. >>> math.isinf(0.1)
    : |! @7 c" s- [% ?( a! @& x
  7. False
复制代码
+ U" f9 X, ?6 z: \: ]! h5 M  j
math.isnan(x)  如果x不是数字True,否则返回False- B- r- R9 V, T5 [) t  A/ {
  1. #如果x不是数字True,否则返回False! u% [3 O3 |7 [, _' F4 h# R+ e
  2. isnan(x) -> bool  w! E, O& S3 ?3 D& `
  3. Return True if x is a NaN (not a number), and False otherwise.1 M3 I+ A4 y; ?! W. }
  4. >>> math.isnan(23)
    . O4 x5 t/ W. m, U' J
  5. False2 o3 q5 z; E2 m8 q$ f
  6. >>> math.isnan(0.01)$ K; {/ e: O) F, |( R9 w9 B
  7. False
复制代码
; Z* g4 T0 y- X2 y3 E; \" a
math.ldexp(x,i)  返回x*(2**i)的值
) B2 D9 R- B; J: s$ @4 H/ I1 o
  1. #返回x*(2**i)的值
      C4 i; |: j6 y3 n7 k8 s, J
  2. ldexp(x, i), S$ }: R& y: L6 |6 E
  3. Return x * (2**i).
    : s+ k+ t8 E6 Q
  4. >>> math.ldexp(5,5)6 V3 _* X& j/ I
  5. 160.0
    ) m+ K: t: x, {/ V2 o
  6. >>> math.ldexp(3,5)# }& z- @6 N- L8 q8 ]8 Y1 M
  7. 96.0
复制代码
" _  A, l& O) @; K
math.log10(x)  返回x的以10为底的对数" k# Z" B% o2 ?$ C
  1. #返回x的以10为底的对数
    : q* p. j; z- e% D7 O
  2. log10(x)" W( K: P. J; u0 G
  3. Return the base 10 logarithm of x., x  N8 T4 {1 @
  4. >>> math.log10(10)3 k9 y3 \* x8 n7 U8 r# h% f
  5. 1.0
    + r5 S' }9 U, \# Y1 H
  6. >>> math.log10(100); A+ H6 J5 Y9 J  p7 m
  7. 2.0- Y$ b$ S1 E/ A' O3 t
  8. #即10的1.3次方的结果为204 i: p5 e, u) X
  9. >>> math.log10(20)9 @$ S9 k% u* F' L4 `! |' X' t
  10. 1.3010299956639813
复制代码

; a* K5 b) W0 o& v8 k4 p: j2 @4 o$ Zmath.log1p(x)  返回x+1的自然对数(基数为e)的值& I# U3 u1 q; ~: K. Z- o
  1. #返回x+1的自然对数(基数为e)的值
    ( y$ |% M# x, K) d
  2. log1p(x)
    ( I( \: k' Q& A0 ^% s9 M' ~2 H
  3. Return the natural logarithm of 1+x (base e).. [3 D" A- A: |4 Q0 y' K1 I( A$ t
  4. The result is computed in a way which is accurate for x near zero.
    ! l5 o. S/ e3 H) h$ N8 U
  5. >>> math.log(10)
    ' N2 [: h5 |, \, P. r8 K9 M5 K! Z
  6. 2.302585092994046( l' o! M% j, b- ?9 @
  7. >>> math.log1p(10)
    & t4 U3 e* I) J: a2 T
  8. 2.3978952727983707
    ; A5 C1 X1 `7 H, H- Y9 @
  9. >>> math.log(11)
    + G; _+ _$ d9 |; s" c/ C
  10. 2.3978952727983707
复制代码
6 s3 |5 _* l6 A2 m
math.log2(x)  返回x的基2对数+ k' s. x  C! _# d/ d/ l& T
  1. #返回x的基2对数
    ( r2 J& l7 M  Z; |( Z2 j
  2. log2(x)2 a3 O& x4 p! J$ k) g* f" ]
  3. Return the base 2 logarithm of x.4 ]/ ?( Q6 c7 \5 z' H9 ^8 a0 G) g
  4. >>> math.log2(32)1 C5 k! s0 Q8 T
  5. 5.0
    % j; {7 q$ ~/ }( K7 s
  6. >>> math.log2(20). I9 y% X2 {& L  R
  7. 4.321928094887363! {0 w) P8 m! p4 ^# v* e8 u- c
  8. >>> math.log2(16)
    ( W1 v) v$ F' `( @# ^
  9. 4.0
复制代码

' `: H; J+ h6 D$ Q, z. P1 `- gmath.modf(x)  返回由x的小数部分和整数部分组成的元组
1 b9 T/ b( z1 O8 Z6 z
  1. #返回由x的小数部分和整数部分组成的元组1 O2 N# P. ^& R0 @$ g
  2. modf(x)
    , U7 s% m8 L# Y/ W) f+ K/ l/ _
  3. Return the fractional and integer parts of x.  Both results carry the sign4 x  i1 ~# g# j' D
  4. of x and are floats.
    ( b0 [4 x8 J: B- W
  5. >>> math.modf(math.pi)
    0 m5 o" O: I: X. u" F- a9 h
  6. (0.14159265358979312, 3.0)/ t6 ]) \0 p8 W9 r: N' O) x7 O
  7. >>> math.modf(12.34)3 R3 [; ~0 J( K8 f2 k3 c+ R
  8. (0.33999999999999986, 12.0)
复制代码

7 C0 K9 d3 D$ p' C6 t: V8 n- Emath.sqrt(x)  求x的平方根
, S, v/ o1 t8 ?: P4 A
  1. #求x的平方根- z+ K" F. C* e- r6 J" C5 n4 ~
  2. sqrt(x)% d1 \8 k  A; P# O3 u
  3. Return the square root of x.- N. \3 @) q5 W8 {/ k
  4. >>> math.sqrt(100)
    & o: T. S5 N. Y. D. T1 \2 F
  5. 10.0
    1 c/ z8 Y" F3 n/ ~5 c% Y5 r
  6. >>> math.sqrt(16)  S/ ?' i& K6 C/ x" a) t: k4 K
  7. 4.04 W8 |& N$ S6 \$ S. ]" f
  8. >>> math.sqrt(20)+ U8 g  E; F& P/ u" ~7 j9 d, M1 t
  9. 4.47213595499958
复制代码

6 O8 j- t6 ]  k7 z: [2 _" Smath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分/ R1 g6 S: `: n; v, i! {+ y
  2. trunc(x:Real) -> Integral
    9 A2 U6 h! y2 V8 y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* e5 p/ r. C: q4 B0 ?8 }5 P$ I# q  T
  4. >>> math.trunc(6.789)2 _4 N$ K2 i+ S  S
  5. 6& G8 h% d% O) s& D  ^2 r$ d
  6. >>> math.trunc(math.pi)
    ( Z, ?+ V# K/ _+ x# j
  7. 3
    . h1 j9 R! I+ z4 }) f1 v. f' _2 J
  8. >>> math.trunc(2.567)! V1 D; n. V$ f3 l8 G7 y9 z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-13 20:57 , Processed in 0.077009 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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