新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

! p2 u8 Q% j, Z+ g4 H* z8 A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 R1 ^6 F) y" A1 r

. O, E; t' j2 I5 p" ~$ |方法1' ~2 d3 \  f+ s0 T. I  o/ \3 X. K( A
  1. >>> import math. D# A  t$ |% X3 _% U3 T
  2. >>> math.sqrt(9)
    7 J+ K5 A; o7 v& z( h
  3. 3.0
复制代码
方法24 i. O1 I# g, X# o, p2 c! L4 r) ?
  1. >>> from math import sqrt
    ) I; F: p$ ^6 H) Q: ^
  2. >>> sqrt(9)
    ' f1 ?# m4 H- [5 Y! H0 k- Z
  3. 3.0
复制代码
& f" Z6 `* d& `/ u. i  i6 _, X

  ?4 A$ T/ g- N* H) L
math.e  表示一个常量
( g! @* U: M6 ~$ I" K$ H3 V7 _
  1. #表示一个常量, y" U* F6 ^' q0 s% h5 p
  2. >>> math.e
    7 [  w2 b1 i# r0 R% ?5 Q
  3. 2.718281828459045
复制代码
# ]8 d( K+ w0 a) U
math.pi  
数字常量,圆周率
7 i" d8 y- z/ U4 @" ~+ u4 p
  1. #数字常量,圆周率
    + \. V2 t- s5 y
  2. >>> print(math.pi)* d# {$ o- _9 n2 f4 p: s
  3. 3.141592653589793
复制代码

( E! X7 d9 J, jmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
" @6 W: L2 P% C. u
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, a! ]2 Y5 V; @) o6 u
  2. ceil(x)7 E& r8 e2 a6 L
  3. Return the ceiling of x as an int.
    * c! ]8 [& n. @" h
  4. This is the smallest integral value >= x.
    ' [: q( \2 n% ?# L

  5. 6 ~1 W: J( n& R3 b3 h
  6. >>> math.ceil(4.01)
    5 R- v" z6 m* g2 Y1 g/ P
  7. 5' P+ }4 T- j8 b9 N5 L4 P
  8. >>> math.ceil(4.99)
      `4 j( C, G- F
  9. 5
    4 ]# x1 \" h8 ?% M
  10. >>> math.ceil(-3.99)
    # `/ C1 R4 c! k& S- J& O
  11. -38 X0 W9 j& n: T! `3 T
  12. >>> math.ceil(-3.01)
    : z. v6 U% Y8 u: L- n8 v; y: T; V2 \
  13. -3
复制代码
! f# O) u, U8 s4 J
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 T$ k, u* ^5 l! |; m
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身. [2 h& y8 V; L9 z' a! [. h
  2. floor(x). h% b5 r3 _: k7 \+ z7 Q! ~) ^, b! u2 }
  3. Return the floor of x as an int.  {5 f2 d' z; Y1 ~$ j5 X
  4. This is the largest integral value <= x.
    , V5 p  m& N. w; [6 B: a
  5. >>> math.floor(4.1)" y  ?$ T* H" r
  6. 4
    7 O& C+ m: U; X3 y: x) U( k6 V
  7. >>> math.floor(4.999)
    1 G- G- l9 Q; E: L. |
  8. 4  O5 i( Q8 {; k7 B9 `: n* F% i
  9. >>> math.floor(-4.999)# S" o# }5 o3 \# T
  10. -51 M# }- _) b$ u& j% F, y
  11. >>> math.floor(-4.01)2 u6 }- L( t# ?! ]& e
  12. -5
复制代码
5 K% Z. l  c" {+ i: |, l
math.pow(x,y)  返回x的y次方,即x**y; b; M" z1 s4 E5 \' o
  1. #返回x的y次方,即x**y* Y5 w& i& |5 z5 L( p0 c" `: t
  2. pow(x, y)
    5 Z4 J1 H4 O: g4 B" [6 m
  3. Return x**y (x to the power of y).
    4 @9 R- F+ c7 M: D
  4. >>> math.pow(3,4)* L* b8 w# l  Y# O& A
  5. 81.02 S8 `# F' g" ?8 G% Y. F
  6. >>> 8 Z8 z* K8 x4 \. ^( K& N( c9 k4 ~
  7. >>> math.pow(2,7)
    & n) W( {8 W. y  ?9 B
  8. 128.0
复制代码
9 i1 b' ^  l" e+ S. U2 p
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 N. F+ N/ k; `. H% ~! W: f
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    7 f! i( u9 s& V7 @5 `9 y
  2. log(x[, base]). o: Y0 x# {/ r
  3. Return the logarithm of x to the given base.
    0 T" P6 h! R3 o5 L  e$ j
  4. If the base not specified, returns the natural logarithm (base e) of x.
    * K8 a7 d: O2 d3 O, d0 O( w2 B
  5. >>> math.log(10)
    " i( g/ J0 e/ g8 \0 S/ L
  6. 2.302585092994046+ O. t9 R/ g: [- m' |
  7. >>> math.log(11); P1 }5 R* c6 [  U
  8. 2.3978952727983707/ }& b5 V, \: I! s2 S% j& u
  9. >>> math.log(20)
    ! m$ K1 }+ N1 {! z+ O+ [
  10. 2.995732273553991
复制代码

4 P% A! E# Q( M' |* pmath.sin(x)  求x(x为弧度)的正弦值
* ^2 r6 B3 Z' N; K
  1. #求x(x为弧度)的正弦值
      B' G* M& D2 W: L, t' B
  2. sin(x)
    ' _& u6 {" U9 o2 n9 R$ Z  y
  3. Return the sine of x (measured in radians).
    & F0 e8 u5 F7 R! E
  4. >>> math.sin(math.pi/4). L$ n) F  N) [" W( Y& c
  5. 0.7071067811865475$ Z4 ]/ G+ M% p& f: v% c
  6. >>> math.sin(math.pi/2)
    5 `* k4 |9 O# T# u, S" W' H
  7. 1.0- O" K6 E& f7 V
  8. >>> math.sin(math.pi/3)
    / e7 I8 u: `4 _" \4 t& t
  9. 0.8660254037844386
复制代码

) D; X# u3 _  u1 u' y, }$ dmath.cos(x)  求x的余弦,x必须是弧度
4 }% Z0 Q  X( [: t
  1. #求x的余弦,x必须是弧度( F6 t8 c/ E( ?! J4 F1 }, y* y
  2. cos(x)( x4 q- Y$ \% `, c' K
  3. Return the cosine of x (measured in radians).
    6 f5 g9 [/ G7 U/ I, y
  4. #math.pi/4表示弧度,转换成角度为45度
    - @5 w) R* B+ r; Y( ~: [6 O7 |& z& o
  5. >>> math.cos(math.pi/4)
    1 C( ^5 x% s# ^+ Y! r
  6. 0.7071067811865476& x# m" L/ z3 \
  7. math.pi/3表示弧度,转换成角度为60度7 c7 I, Y, c0 Y6 ]
  8. >>> math.cos(math.pi/3)* i8 l  _! E1 @% V+ X! J) B. t
  9. 0.5000000000000001
    : s$ ~: W# g: ^, ^3 ?
  10. math.pi/6表示弧度,转换成角度为30度
    % @7 S/ I, \' @; X+ t
  11. >>> math.cos(math.pi/6)& ?1 k) G- j' U7 @6 ]8 C) F
  12. 0.8660254037844387
复制代码

" d  \2 l) {/ Z: a6 }- }math.tan(x)  返回x(x为弧度)的正切值
. Z+ C) a; ~9 e  i
  1. #返回x(x为弧度)的正切值# H4 P( V' o/ j6 ?% Y+ U
  2. tan(x)' `* t+ h" k, q  \, r
  3. Return the tangent of x (measured in radians).
    : `, N9 {& v3 W+ O) S& d# T5 d* V. U
  4. >>> math.tan(math.pi/4)+ J* c' K0 t3 N9 B1 ]9 l
  5. 0.9999999999999999' l- `# L" j3 q7 x2 e* Y
  6. >>> math.tan(math.pi/6)
    5 L& s7 ~. [9 S' ^: }" d' T
  7. 0.5773502691896257% O0 P0 d' L, B  H, @
  8. >>> math.tan(math.pi/3)) O& l" a) U# G. Q* }0 s
  9. 1.7320508075688767
复制代码
$ M; T& N  j: Y0 s
math.degrees(x)  把x从弧度转换成角度7 |; a( V4 q; r( k$ d, a/ f* A2 |
  1. #把x从弧度转换成角度
    * J" F: c, {& B. Y  O: C: L
  2. degrees(x); A# p/ t2 a& i3 E, J
  3. Convert angle x from radians to degrees.- J1 z  I( C$ D' @8 v5 \. i  Y
  4. 7 V# I5 e1 @7 g
  5. >>> math.degrees(math.pi/4)# V  o/ a7 J, \
  6. 45.0
    # U! K; M7 e% V/ H6 V
  7. >>> math.degrees(math.pi)# x  ^' J5 r3 j5 l* `
  8. 180.0- S, X$ y5 q" _& j: f! d
  9. >>> math.degrees(math.pi/6)! l  W9 {$ \; Q8 W
  10. 29.999999999999996
    8 O9 m5 `$ V, j" t5 r
  11. >>> math.degrees(math.pi/3)6 M/ H2 S/ Q2 [1 D
  12. 59.99999999999999
复制代码

4 Y, @, p4 t9 k. fmath.radians(x)  把角度x转换成弧度& U* ^- o. [. `8 G9 m5 F/ z
  1. #把角度x转换成弧度
    6 R. T; g5 @- Z5 Y! L# n0 H) {
  2. radians(x)
      d% i& `# E6 ~6 N1 p2 F
  3. Convert angle x from degrees to radians.: p: r) q+ P! u6 @3 _- b( s  G
  4. >>> math.radians(45)6 p2 H/ q  Y# B
  5. 0.7853981633974483: w- q: I' M  R8 \7 {* v! c" I$ e
  6. >>> math.radians(60)
    : u1 @9 p/ N- b& T2 c
  7. 1.0471975511965976
复制代码
# [' m6 ]' b0 p% T
math.copysign(x,y)  把y的正负号加到x前面,可以使用04 G/ t" L# v- X. X. ~
  1. #把y的正负号加到x前面,可以使用00 ?: [4 k$ G) l4 H
  2. copysign(x, y)
    0 t2 f6 r7 M& l! N, o5 n
  3. Return a float with the magnitude (absolute value) of x but the sign * l; h2 T  ]$ \
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    7 Z3 g: G4 r# i6 }/ `
  5. returns -1.0.
    ) ^5 ?& m1 ?8 }
  6. 5 C3 a4 g9 Z1 L1 N2 \
  7. >>> math.copysign(2,3)' I( ~. L  I- I$ R8 d1 H2 C
  8. 2.0" N7 J) p1 D# X" Y; Z4 h1 e
  9. >>> math.copysign(2,-3)
    4 n: n5 K& N  T+ Q* I' l" t
  10. -2.0
    ' ~" u5 s6 [( r, ^5 a% K2 t
  11. >>> math.copysign(3,8)) s2 E" |6 Y7 r$ U( U0 r
  12. 3.0# n/ m- b0 x# i2 w; u
  13. >>> math.copysign(3,-8)
    / o) W; `5 k" w, p, t
  14. -3.0
复制代码

- a* J7 z: H  ?; ^math.exp(x)  返回math.e,也就是2.71828的x次方. d# z3 d  A. U$ S* ~1 t
  1. #返回math.e,也就是2.71828的x次方# u" E0 O" H, J- ^2 R; V* a, \* E3 i
  2. exp(x)
    & b0 u" o' q% u. E5 [
  3. Return e raised to the power of x.
    ) W+ P0 s2 h& ?5 S
  4. , u* m6 _: r. i$ K4 B- O
  5. >>> math.exp(1)4 D& E& S7 ]- a
  6. 2.718281828459045
    9 s# I: K8 M4 z7 A7 E
  7. >>> math.exp(2)+ {' `5 d3 ~- M+ P- w# B/ Y9 F
  8. 7.38905609893065* x; Q3 Q8 \1 s  m9 f
  9. >>> math.exp(3)/ A' B) L* E& a/ {
  10. 20.085536923187668
复制代码

3 H$ M$ G' M1 i: Q4 d4 X& {4 R) `math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1% T9 n5 B8 U9 x+ c: r% h
  1. #返回math.e的x(其值为2.71828)次方的值减1* m0 E7 a* Q  c
  2. expm1(x)4 R2 k8 W4 \6 A. B
  3. Return exp(x)-1.
    ! T" F: \# Z; W3 I4 ~# d. |( B
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 K% I/ V! P7 `" m: H+ ], M! n. i
  5. 5 Q0 R: C  v9 @7 `  p- a) ^* w& D
  6. >>> math.expm1(1)& d  k  k3 U1 A) \
  7. 1.718281828459045
    8 M9 O7 d0 P4 ?' T1 E
  8. >>> math.expm1(2)
    : F, \4 k; t% ~% M& d5 P' y$ z" r
  9. 6.38905609893065
    : h/ z) h' x, o
  10. >>> math.expm1(3)5 M6 G. `: l. a; n
  11. 19.085536923187668
复制代码
3 C4 O$ G% K1 O# i% r
math.fabs(x)  返回x的绝对值
' B! O' E$ ^! C
  1. #返回x的绝对值1 a+ E' z6 U- N" B( G
  2. fabs(x)) u# K* J' s, `! k: r) N6 i3 u, b
  3. Return the absolute value of the float x.! |/ A  T0 x/ l& p2 L

  4. ' \2 R5 f5 ?6 [; S8 V# l3 O. r& k
  5. >>> math.fabs(-0.003)2 u) g2 e1 x2 ]) K. P  w
  6. 0.003
    + r( H' ?  x2 |4 f* b* }6 s) p
  7. >>> math.fabs(-110)
    2 X3 N9 B7 R$ {+ v" p1 G* F$ X" O- {
  8. 110.08 l+ i$ z. f% }. I) D
  9. >>> math.fabs(100)5 N" E, \9 m2 o! N& ^3 o9 m
  10. 100.0
复制代码
' q3 c- ]) N9 s- p. M2 I, }
math.factorial(x)  取x的阶乘的值8 f) J% n5 w/ A, y8 _# x
  1. #取x的阶乘的值
    % r6 C9 [) r9 ~
  2. factorial(x) -> Integral
    0 S' q/ p8 O) M" F/ A* h" }
  3. Find x!. Raise a ValueError if x is negative or non-integral.. W" H5 W' i& i' @; ~
  4. >>> math.factorial(1): P( u/ A7 Q" O, \7 _& r
  5. 1
    . _4 b9 G$ d0 I  F3 i% _  N
  6. >>> math.factorial(2)
    + r4 X% [# |2 A% ?; G* L" B
  7. 2
    7 g$ l( U, e- i6 d  C6 S
  8. >>> math.factorial(3)0 h7 t# T# j: v! d, h) l$ }9 ?1 W
  9. 6
    & Z& f5 O( h; a( `, S
  10. >>> math.factorial(5)% J7 }6 R/ F4 s  [$ c. h
  11. 120
    9 ^# o% b! ^  W: ]% X, z7 `3 @  `
  12. >>> math.factorial(10)* l9 U$ N: M2 n2 T! x
  13. 3628800
复制代码
2 X0 p2 I1 Q( B# |" t: l
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数5 Q: J0 y! I, T( H& f
  1. #得到x/y的余数,其值是一个浮点数
    + {, t$ b; x4 G. d
  2. fmod(x, y)
    * k0 Y' _+ z7 L* a# P( ?
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + _" U% d, v4 g- h) U2 e& s7 O
  4. >>> math.fmod(20,3)% C( D% H. z  p  M! l: f3 s
  5. 2.01 ^2 Z$ x; {( ?# u
  6. >>> math.fmod(20,7)
    1 L: `& g5 N# G4 ^
  7. 6.0
复制代码
' @- V; K, i9 {+ |9 v* b
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 _" I; u$ U3 \3 w3 M
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ; B; u( x( V2 l$ \* z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    $ m1 O& H' @/ V; t& K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 ?; R4 u+ U$ Y7 k& r
  4. frexp(x)6 a. N: @$ a# {
  5. Return the mantissa and exponent of x, as pair (m, e).
    % C  l. ~- C+ X
  6. m is a float and e is an int, such that x = m * 2.**e.1 P1 X' a  D# a& `
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0." h! n/ \$ g, z# f3 Y; i! B4 b. C
  8. >>> math.frexp(10)5 m* V3 z& }+ l& t
  9. (0.625, 4)" S; R, t( j, W4 _3 D$ ?% E; V
  10. >>> math.frexp(75)
    9 ^% A+ N1 x( ~* F
  11. (0.5859375, 7)
    3 h5 e' e; `$ C# o0 V, \) i
  12. >>> math.frexp(-40)9 O- {+ k% J+ }' c
  13. (-0.625, 6)
    - h8 f/ z, I, _. |! X8 ^3 G2 Z
  14. >>> math.frexp(-100)% t6 s: X& n& F% d) h/ Y
  15. (-0.78125, 7)
    - ?* ]# X+ H: C4 C- [0 o
  16. >>> math.frexp(100)
    8 E  ~; S& n, t  T) ~
  17. (0.78125, 7)
复制代码
) k6 y% S( L3 B
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列% h. O5 U) R; r* B: A5 @% Q
  1. #对迭代器里的每个元素进行求和操作
    ( [- u& H: H" h. w: |
  2. fsum(iterable)- i( Q- d  u: l; z! b4 A3 t
  3. Return an accurate floating point sum of values in the iterable." v/ Y4 D: ]" v& c2 r1 j# ~2 G
  4. Assumes IEEE-754 floating point arithmetic.
    . x" J! P8 W3 D4 g7 D
  5. >>> math.fsum([1,2,3,4])
    7 W/ C! L0 x* _: ?; w6 h1 q
  6. 10.0
    / e0 O% B7 ~  H* a1 J4 H5 r
  7. >>> math.fsum((1,2,3,4))% @+ _$ e. H1 a* X* j8 P1 t8 Z
  8. 10.0& k$ q% @$ q' v5 \; J' F6 [
  9. >>> math.fsum((-1,-2,-3,-4))! k; z3 y) j- ~4 A/ _
  10. -10.0! H* Q+ n- v# \" n: N1 P
  11. >>> math.fsum([-1,-2,-3,-4])# k, ]" C6 a% u( w8 d" y9 F* J
  12. -10.0
复制代码
* }: q0 _- s4 e/ H; y2 P
math.gcd(x,y)  返回x和y的最大公约数2 S4 p% `! b" F1 E2 {( F1 e
  1. #返回x和y的最大公约数5 D5 M4 T5 s" Z; V
  2. gcd(x, y) -> int
    $ f+ E- \5 J- G, {
  3. greatest common divisor of x and y/ G( k1 I. v& k
  4. >>> math.gcd(8,6)5 m# [& R1 H2 W" L1 H' Z  c
  5. 24 j5 f; q8 y4 }8 C- f( c
  6. >>> math.gcd(40,20)$ _$ e. v* k" k- A- F( r# I* H
  7. 20
    ! n& j1 s  u" y1 G9 Y
  8. >>> math.gcd(8,12)
    . o% L* m2 F; y4 D5 z: f
  9. 4
复制代码

/ G% M  M0 g# Smath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False! W. c- |( J. O' w  b. I" Y  m
  1. #得到(x**2+y**2),平方的值2 f$ ^! s0 f, D+ i+ h# H* n$ G
  2. hypot(x, y)
    $ Y6 G6 |. ]8 o' t4 [/ [
  3. Return the Euclidean distance, sqrt(x*x + y*y)., D: m* y; m% C/ q8 O1 ~: `
  4. >>> math.hypot(3,4)
    ! G; @, {6 M& s- G$ L5 X
  5. 5.0
    * P/ s3 o  k( N/ O) _
  6. >>> math.hypot(6,8)
    1 U: F: o" C. l' c
  7. 10.0
复制代码
7 I7 X5 ^" D. D
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
3 I" K5 k! L2 G2 B4 ~
  1. #如果x是不是无穷大的数字,则返回True,否则返回False! [7 [4 j. ?, N$ \  c
  2. isfinite(x) -> bool. }4 Y5 J; t) t4 y" H- e
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.- G. y7 r7 X: C- @
  4. >>> math.isfinite(100)% N* p9 Q1 N% S6 T2 E  ~
  5. True& z' V! x( O* m8 e0 K
  6. >>> math.isfinite(0)0 V1 }0 C$ _" b! s8 L
  7. True
    8 l( c9 a; P+ f8 Q2 t% ^
  8. >>> math.isfinite(0.1)
    , b; A' ~3 |4 Y) M- k" Q" L
  9. True
    : O% [; B' ~. Y. X. V  M
  10. >>> math.isfinite("a")
    5 U0 D6 f1 D, z: X# L' p
  11. >>> math.isfinite(0.0001)
    : P# J  M$ r0 H' S) @$ p
  12. True
复制代码

2 z7 _6 u4 c; T: F) |! \  Smath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
- |( q9 _9 _  z2 |) J. r( B9 h
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False3 y9 `* N6 z8 g: `4 R3 j9 J: x* ^
  2. isinf(x) -> bool+ s& Z& b2 W+ O, }
  3. Return True if x is a positive or negative infinity, and False otherwise.7 }; d: S* |# e- P& n
  4. >>> math.isinf(234)/ d* V* c% `0 b8 Y
  5. False
    ' {7 |% c% s" C% g+ N- V
  6. >>> math.isinf(0.1)
    7 |' q( B5 j! j! l
  7. False
复制代码

$ b- A% Q7 S2 y, X! Z) g' Bmath.isnan(x)  如果x不是数字True,否则返回False
/ j) _8 ]- i0 R8 o
  1. #如果x不是数字True,否则返回False
    ; Y2 R4 {7 ~9 ]7 a6 W+ {
  2. isnan(x) -> bool7 x/ \8 w, \- C
  3. Return True if x is a NaN (not a number), and False otherwise.
    , |5 g) O* ]2 L6 w! U
  4. >>> math.isnan(23)* V! L  w  g7 d. V
  5. False
      \% O7 G# V; F0 ^; U$ n
  6. >>> math.isnan(0.01)5 m' J$ F7 E/ A* f0 Y: h1 n
  7. False
复制代码

7 f# ]: r. J. V/ B& [3 o. p- Cmath.ldexp(x,i)  返回x*(2**i)的值
' z( f2 {, O2 ]: t" X  e  r% p
  1. #返回x*(2**i)的值
    7 I1 Y7 }8 ?) w2 }! u6 P
  2. ldexp(x, i)
    $ E0 p; _1 @) z& y) d
  3. Return x * (2**i).8 l" h/ Z% [3 t+ k; l0 n$ d. Y
  4. >>> math.ldexp(5,5)
    2 R- r: K6 ], x4 c7 n3 o7 l$ _' y3 n
  5. 160.0
    ' d" m# E" J  s3 C- z5 b" M
  6. >>> math.ldexp(3,5)
    ; J% \5 P" W& l. W
  7. 96.0
复制代码

: N1 P) v% ?) w6 C8 Pmath.log10(x)  返回x的以10为底的对数8 G8 e9 [! [8 m, I& N  d
  1. #返回x的以10为底的对数) g3 X% a7 B$ V% Y) s
  2. log10(x)# k2 k9 N4 m& R% H; ~1 r$ z" S
  3. Return the base 10 logarithm of x.
    2 c0 t* D3 l0 r" f8 N
  4. >>> math.log10(10)# L) q0 S" Z9 h& c/ ~. S
  5. 1.0; f9 {# ?- C- G3 Y9 E
  6. >>> math.log10(100)1 @, E1 |7 Z: @/ t& y7 [
  7. 2.08 c/ W; U" B# Z1 P* g( N% Z
  8. #即10的1.3次方的结果为20, T- e* k2 p% @0 \; I
  9. >>> math.log10(20)
    - |) U* S4 ]# G9 l: T9 B& ?
  10. 1.3010299956639813
复制代码
: a" w8 {5 Z7 e. V* v' j
math.log1p(x)  返回x+1的自然对数(基数为e)的值
* ?' x! z) z2 r. b3 z2 m7 h
  1. #返回x+1的自然对数(基数为e)的值
    " q4 ~% s# N: h  }) ]% H& d
  2. log1p(x)9 E3 I6 @% h' r8 i
  3. Return the natural logarithm of 1+x (base e).( o0 Q6 d% k( T8 I0 e/ o1 s, u
  4. The result is computed in a way which is accurate for x near zero.9 ?# m5 s0 p9 z8 P6 k4 t- w! b' T
  5. >>> math.log(10)+ d) R& e' W, {# N! _7 D
  6. 2.302585092994046
    2 M2 K" s5 Q* S0 M5 h9 v( E
  7. >>> math.log1p(10)! y& Z$ h- r% n
  8. 2.3978952727983707
    + R9 m2 S) P7 z9 j
  9. >>> math.log(11): j, I5 T1 b* ~6 v* Q/ W1 }
  10. 2.3978952727983707
复制代码

6 i3 x, u4 o, B, D1 \math.log2(x)  返回x的基2对数% O/ c6 A8 K8 Z6 l3 i  O
  1. #返回x的基2对数
    7 J7 z3 V/ N' L2 Y; v
  2. log2(x)' I" ~! B! l. v. o  G
  3. Return the base 2 logarithm of x.7 w0 @( P4 V8 O- T& T
  4. >>> math.log2(32), W, ~8 y  w' G" z# P' Q. P
  5. 5.0
    0 J. @4 ]( _8 v% d
  6. >>> math.log2(20)
    & B4 G5 Y) [  h6 V, W/ F
  7. 4.321928094887363
    ' I0 u' N0 C0 v: I1 e! y) ]% [
  8. >>> math.log2(16)
    . i6 D2 O) B( I2 C( l
  9. 4.0
复制代码

9 O* i3 w# n1 M6 R; emath.modf(x)  返回由x的小数部分和整数部分组成的元组- i- I. p1 q9 p$ ?0 M
  1. #返回由x的小数部分和整数部分组成的元组
    : B3 H9 C+ n+ M; ~) v
  2. modf(x), d: w  P+ d1 V: V/ d6 Z/ i: s5 ^
  3. Return the fractional and integer parts of x.  Both results carry the sign
    : e+ j, P5 C/ p" W! ~- A: ~9 L
  4. of x and are floats.
    * b& Q; c% U# B0 k+ A) D, H
  5. >>> math.modf(math.pi)7 d3 K7 w& X$ S9 v' V: A
  6. (0.14159265358979312, 3.0)/ q7 O9 F" n7 I" t6 e' P
  7. >>> math.modf(12.34), E  _0 H  p4 |0 ^# E
  8. (0.33999999999999986, 12.0)
复制代码

! m2 ^( o  f0 X0 Q; L: Wmath.sqrt(x)  求x的平方根" N) }3 @8 C& S) `' F! b
  1. #求x的平方根
    6 B9 N- E+ J/ W0 \  `$ o* r3 l2 J
  2. sqrt(x)
    ; ]8 z( l5 H1 _7 _6 g% q
  3. Return the square root of x., p3 n* Y8 h  g
  4. >>> math.sqrt(100)
    3 _% t) i5 a+ E5 r, s, y
  5. 10.0; J% ~2 `/ K3 L8 H  i
  6. >>> math.sqrt(16)
    ; r$ S" m3 k# E$ |, o
  7. 4.0
    # H% k+ S+ L4 `
  8. >>> math.sqrt(20)
    ) k' I/ I3 |1 o; ^3 t, V  Y. X& w
  9. 4.47213595499958
复制代码

4 y7 w; W4 i1 n8 Q% amath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    - t: w; N8 x" I7 u
  2. trunc(x:Real) -> Integral' O( f" w9 k& x
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* B% Y: r( G, Q. F  G( a
  4. >>> math.trunc(6.789)
    # U! Z$ S0 [" x3 P; x9 _+ ]" F9 G
  5. 6# E: M$ J+ u! Z
  6. >>> math.trunc(math.pi)
    * B- L' s6 |' v; ]: F
  7. 3' T, ]: W2 }2 S/ _
  8. >>> math.trunc(2.567)
    % y! R2 o* S. s4 ?
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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