新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

6 z: x- S/ e' f* @4 C' L8 _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& |' l3 q8 ~0 M8 r

% r) i. [  \) p6 k6 j方法1
; n. I% B! n" m3 S
  1. >>> import math
    ! x( R* z8 D8 ]; ^% w6 U. u
  2. >>> math.sqrt(9)
    3 q; a2 N0 I3 p5 k
  3. 3.0
复制代码
方法2: Q$ I9 ^* _6 W7 D1 K1 W5 s. l5 h
  1. >>> from math import sqrt1 D) ~9 d" v, ~& l$ b
  2. >>> sqrt(9)* A. v+ Q7 H5 P! y+ d$ X
  3. 3.0
复制代码
0 `# ?7 S( ]' l7 A+ u

; ]9 h# u: z. E" B  w, J  O8 y. }5 a5 |
math.e  表示一个常量( z( r; c2 ~- w! j
  1. #表示一个常量) i$ {) @  W% P
  2. >>> math.e
    ! m5 q* ~7 z, d$ ^/ a: V
  3. 2.718281828459045
复制代码

, m7 n: y* o/ p0 jmath.pi  
数字常量,圆周率

- A) T2 ?" q/ ]0 ]1 d8 U# B; Z
  1. #数字常量,圆周率
    4 C( e2 p0 ]7 K1 I- I2 K- Z) e; W
  2. >>> print(math.pi)
    + z! T5 j: d) B5 S: q6 \, _  Q
  3. 3.141592653589793
复制代码
7 K8 `9 k+ H! U; `% O
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

) S- F9 Q; O: u$ J  R
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ U$ g/ K' n5 p) w+ ~* x# `
  2. ceil(x)
    4 [$ S( f5 ^5 p6 q" k/ a
  3. Return the ceiling of x as an int.8 |5 z2 v1 H: O, q( f6 Z5 o
  4. This is the smallest integral value >= x.+ R8 V/ ]% U3 t7 y& {% S  h

  5. * N# Q, K' _. D
  6. >>> math.ceil(4.01)
      E& |) _6 R$ V9 F
  7. 55 Q6 S; M6 F2 b$ X" g
  8. >>> math.ceil(4.99)
    . r+ Z$ l6 q2 Q2 h
  9. 5
    2 M/ g( N. H! ?, Z+ L
  10. >>> math.ceil(-3.99)
    & _9 E$ p0 z5 Y" f7 b2 i6 C/ _
  11. -3
    3 M  w+ z8 u2 q5 P
  12. >>> math.ceil(-3.01)7 r( I- `# A% t( [
  13. -3
复制代码
9 p! o3 k: z: ^# U+ e
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身, ], R9 y& `1 ]- @* J
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身: {) n# \7 l9 k5 X
  2. floor(x)
    3 P, q$ B7 Z# P% t; D
  3. Return the floor of x as an int.
    - N8 Y/ w5 M* L, [+ N9 o1 b
  4. This is the largest integral value <= x.
    4 I1 K" g, ^. k- A
  5. >>> math.floor(4.1)
    ) q  L$ X7 U" M3 P: J1 D
  6. 4+ F) o1 \: u+ C+ z3 |
  7. >>> math.floor(4.999)
    ( e" K  L  m' Z/ ?6 V
  8. 4! L5 _7 }: X: H# N9 m# S+ ~
  9. >>> math.floor(-4.999)( ^6 x+ l. v% W5 ~# c! w2 d% j
  10. -5! _4 x$ [9 a& d" s2 m% G
  11. >>> math.floor(-4.01)
    % W$ ~/ v' ]0 t0 w1 X2 `
  12. -5
复制代码
& S( p! G# M" V% x! O
math.pow(x,y)  返回x的y次方,即x**y6 Y0 f5 Q4 |; D; `$ i
  1. #返回x的y次方,即x**y, T: a4 w, I/ J/ Y! s3 [
  2. pow(x, y)& u! i& R' N5 c2 v# C* |+ y
  3. Return x**y (x to the power of y).5 k; U$ o. `/ W
  4. >>> math.pow(3,4)
    ( q/ [7 M  J2 e. ]4 c: X. J- v
  5. 81.0$ J$ N7 b( \( c0 q( g5 Y: z0 x
  6. >>> 3 n3 Q& O5 W7 X, L
  7. >>> math.pow(2,7)& F& D- P5 `0 q( @. C2 o
  8. 128.0
复制代码
: z4 r+ m! P  ?  z$ H  q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 s: L" a3 s/ b$ P, v: [: N4 l
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    0 I% C/ p3 D5 {
  2. log(x[, base])! T, Z1 F& A9 J( ?, k7 l3 L7 V
  3. Return the logarithm of x to the given base.
    * ^. H! [2 m% f
  4. If the base not specified, returns the natural logarithm (base e) of x.
    8 Z6 K3 M6 _% b+ \' `" o. V4 Z
  5. >>> math.log(10)9 {( y1 u9 n, O! P) O  H4 r
  6. 2.302585092994046
    * k2 J' R1 W9 K
  7. >>> math.log(11)# C: V9 T/ k0 A# _
  8. 2.3978952727983707
    / ]7 i2 q  ^# a6 B) M4 u7 C& D
  9. >>> math.log(20)- ~& o$ e0 o) \) N, Z0 Q
  10. 2.995732273553991
复制代码
1 t" ]+ [( H4 F# O
math.sin(x)  求x(x为弧度)的正弦值
( j- O# [( N4 h6 S
  1. #求x(x为弧度)的正弦值% S1 \. y/ N) U/ E
  2. sin(x)- s2 Z% ^8 j& r
  3. Return the sine of x (measured in radians).
    ( o$ {9 i) n4 c5 [) [
  4. >>> math.sin(math.pi/4)
    & w6 w  U1 c; p/ p
  5. 0.70710678118654753 ^. _! V) S, @7 q& U
  6. >>> math.sin(math.pi/2)5 @, L% L7 b) J; L. j5 _2 z6 K
  7. 1.0
      A( e: F0 U5 I1 W) {! s
  8. >>> math.sin(math.pi/3)
    ' g. I% K! x/ C: O: N7 d
  9. 0.8660254037844386
复制代码

4 u. F0 m0 i& G4 x. ?0 imath.cos(x)  求x的余弦,x必须是弧度4 Y0 g- m" t# J) f  {. `/ _
  1. #求x的余弦,x必须是弧度
    . P; L" [& s0 s4 k- ^
  2. cos(x)" O- l7 }+ n' C0 f0 X4 K
  3. Return the cosine of x (measured in radians).; H1 T2 v5 R  ^! z) d
  4. #math.pi/4表示弧度,转换成角度为45度
    ; m+ a/ R& l8 g. \: f7 }8 l
  5. >>> math.cos(math.pi/4)
    3 o0 K7 [2 l) q& F6 L) a& U- k
  6. 0.7071067811865476
    ! W7 `  ~8 M4 K
  7. math.pi/3表示弧度,转换成角度为60度
    # ~1 `  _# ?2 E. b0 O
  8. >>> math.cos(math.pi/3), P8 c% L( h8 ~$ P+ B+ E" k
  9. 0.5000000000000001. \2 d; p% B/ q, l: U  L! t4 {* {
  10. math.pi/6表示弧度,转换成角度为30度
    ( o( @( j  P2 w9 Y7 Y  D/ F
  11. >>> math.cos(math.pi/6)1 @5 p2 ]: t9 I. _: ]4 U
  12. 0.8660254037844387
复制代码

6 Y6 z9 J) e, \8 f* V0 Pmath.tan(x)  返回x(x为弧度)的正切值
- U9 J! o) U7 W% W5 H
  1. #返回x(x为弧度)的正切值+ ]6 I) n$ E$ v
  2. tan(x)4 X$ V2 n, }- Q2 o# ?8 U& U
  3. Return the tangent of x (measured in radians)./ Y9 U6 z  l1 W2 A$ G" t3 g+ x
  4. >>> math.tan(math.pi/4)8 C& x5 j2 h: ?5 Z) K. a& E
  5. 0.9999999999999999; U  |0 v- y2 Y, f4 \- E
  6. >>> math.tan(math.pi/6)8 ]1 ^. N$ R0 ~; ^0 h
  7. 0.5773502691896257* h% k! h; R, F; n& R
  8. >>> math.tan(math.pi/3)$ N) ~/ C; K% ?, K9 U" \& f
  9. 1.7320508075688767
复制代码

8 ]% M" i1 @# {# K) ?, fmath.degrees(x)  把x从弧度转换成角度& Y2 Y! g4 T! O( J1 k
  1. #把x从弧度转换成角度/ \3 B" N" X7 t% Y5 [9 F3 v
  2. degrees(x)
    2 ~! X  v4 e7 N! a, W/ I
  3. Convert angle x from radians to degrees.
    + s( {) x1 k# {9 n

  4. & A5 C5 M2 a) `( t& `
  5. >>> math.degrees(math.pi/4)
    . v% w/ q$ p* \7 f6 @& R+ _
  6. 45.0* w2 Z) D' p4 M8 u
  7. >>> math.degrees(math.pi)0 S( u9 M- ~1 h  r) C1 ]
  8. 180.0
    ) T7 \  I+ l; i( p/ v
  9. >>> math.degrees(math.pi/6)
      h( H! \5 U) {% e) `1 R( g4 r$ x( _
  10. 29.999999999999996. a$ ^  z9 t# j3 c# |4 G( C
  11. >>> math.degrees(math.pi/3)/ D* p* X! o$ `" l, L! P% D9 H- u
  12. 59.99999999999999
复制代码

9 f/ S3 u7 [6 n4 e' ~* fmath.radians(x)  把角度x转换成弧度* k3 @" |% `# x
  1. #把角度x转换成弧度
    " u+ \/ s# [8 i5 K9 k& z
  2. radians(x)" j$ O2 P# y6 o: @9 p# f# y
  3. Convert angle x from degrees to radians.+ K/ i0 `) O2 x2 k( C$ t1 X
  4. >>> math.radians(45)7 M9 s% z; U; \
  5. 0.7853981633974483+ W! b$ m# v9 l
  6. >>> math.radians(60)1 {) u" F' [! Z8 \7 x7 c
  7. 1.0471975511965976
复制代码

7 \$ |( S* G' I, L+ I4 Ymath.copysign(x,y)  把y的正负号加到x前面,可以使用0: E& X1 I" @- G$ S% F8 D; \! n9 J2 h
  1. #把y的正负号加到x前面,可以使用08 c9 Y, o2 A3 |+ f3 M2 J, I: M
  2. copysign(x, y)# `7 x* L) v% x) z  |1 f( o# J
  3. Return a float with the magnitude (absolute value) of x but the sign
    . S- T/ }, b; V* p6 j  e/ `
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) + y7 s- D3 [/ F' y& X. O
  5. returns -1.0.
    & a# w' A0 n1 c; r
  6. $ v1 |- J0 A1 ?1 Z7 v5 W
  7. >>> math.copysign(2,3)/ N1 Y3 j, q0 u% `$ x
  8. 2.0, `) p% P7 O6 x9 A, ~
  9. >>> math.copysign(2,-3)
    6 H3 ]1 V  F8 v0 X
  10. -2.0
    2 P) P: i2 s2 G- d8 i( J# q; I
  11. >>> math.copysign(3,8)
    . _1 f1 v' E9 L* I) I3 F1 W" }2 ?
  12. 3.0+ k& e( H! F# @8 ?) y
  13. >>> math.copysign(3,-8)' C' w7 D, ~. j0 w7 a
  14. -3.0
复制代码
0 h+ q* ?2 x# g( H. E1 V
math.exp(x)  返回math.e,也就是2.71828的x次方- h, h& b0 f# S9 s9 D
  1. #返回math.e,也就是2.71828的x次方
    , m% N' }9 {  l& t/ [/ a. x
  2. exp(x)  j, c+ n* ~7 Z( A
  3. Return e raised to the power of x.
    , J4 n% I& y4 ^" k

  4. 3 G( L& m2 v. x: ^$ ]' N
  5. >>> math.exp(1)
    0 ?6 \9 h4 g* f, O) s
  6. 2.718281828459045
    ) w( Y3 D2 z6 p# R5 ]
  7. >>> math.exp(2)5 M' ~: {  q1 j$ r' Z: U4 _
  8. 7.38905609893065
    ; i* S5 d+ l$ G. R
  9. >>> math.exp(3)8 n" u) d6 `/ [; v4 h% m# _
  10. 20.085536923187668
复制代码

3 U. \! B" X/ imath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
0 r# H+ Y. v; J
  1. #返回math.e的x(其值为2.71828)次方的值减16 x) G1 k% ~. D, ]" c+ ^) O
  2. expm1(x)4 d8 c" ?7 T4 X& j7 v
  3. Return exp(x)-1.5 k. H0 ^9 w5 f+ H0 _6 D% Y" q9 U
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    9 z7 A2 c3 j, h; F* w
  5. 1 I  ?# S2 i" W+ j% m
  6. >>> math.expm1(1)
    : {; H  T% @8 X: o" `3 Z; _+ u" h
  7. 1.718281828459045
    1 N( {1 h8 B  u: s1 U0 H3 T5 Q
  8. >>> math.expm1(2)0 \1 ]$ y) }- ]! j
  9. 6.389056098930654 r- J* Z0 c$ N4 [4 Q
  10. >>> math.expm1(3)
    + r) `2 q8 O! I+ j: u# c5 _( X
  11. 19.085536923187668
复制代码

/ M% i* Z1 G9 Q6 |8 Ymath.fabs(x)  返回x的绝对值
  H. w# E* C9 B
  1. #返回x的绝对值
    5 x% }: x" P! a* G, j
  2. fabs(x)
    2 E' k, t% S4 Y7 K6 x
  3. Return the absolute value of the float x.5 i0 Z. C" v, J- Q' ~1 v: f
  4. & g2 y* K8 |7 }& @) f0 g; \' r2 C9 i
  5. >>> math.fabs(-0.003). L- g, Z1 n/ O! r
  6. 0.0039 v% u# @/ A) C; O, M
  7. >>> math.fabs(-110)
    & Q' _; R% `# A) c% O
  8. 110.0
    2 a) g7 d( h0 m, F
  9. >>> math.fabs(100)
    : q; e6 e9 t% `8 G
  10. 100.0
复制代码
! i9 z: A/ T5 @' t
math.factorial(x)  取x的阶乘的值
; }5 c+ _+ P( G3 T+ g& C
  1. #取x的阶乘的值8 N  |/ N9 |1 V  @! ^* ?
  2. factorial(x) -> Integral$ B& b  x! W7 ]  z7 m% f
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    3 m: A  m4 f( R+ t
  4. >>> math.factorial(1)" ^/ S9 {1 L0 D2 ]& I( X7 Q. G
  5. 1
    ! u+ E! h' o$ |8 v
  6. >>> math.factorial(2)
    1 P* u! ~0 q2 O' i  e4 v
  7. 2
    9 ^% z9 `+ m& X2 o7 j7 c0 W
  8. >>> math.factorial(3)
    6 |. Y# i5 f; W! D1 ]
  9. 6
    $ i5 a$ h+ d6 F+ ~  V% b5 r6 m
  10. >>> math.factorial(5)1 e& d6 J1 r$ w& C, J
  11. 1200 Z& t/ _% G6 V5 L0 g
  12. >>> math.factorial(10)
    9 d- K4 c; w% a) Y5 f* L8 w$ a
  13. 3628800
复制代码

. x* }$ v9 ~7 B, l8 k# E3 Emath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
/ O0 v- ~/ J  ^' \9 |- v1 E
  1. #得到x/y的余数,其值是一个浮点数
    ; n0 P) H( z6 H# J0 g
  2. fmod(x, y)
    ' x6 `0 d  U  T8 p0 I0 g: a
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    * d% {( S3 q) C" W7 j% }
  4. >>> math.fmod(20,3)
    ( d% s2 B0 i! G7 r5 y
  5. 2.05 r3 G) l0 F( c$ o$ q3 f
  6. >>> math.fmod(20,7)
    & y1 c- ^4 X; P  H* D
  7. 6.0
复制代码

  p3 r/ N/ A% d9 nmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 F% r1 l1 u  o9 K5 [) j
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . [! `3 [# _7 I4 Q9 u4 s
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ @; J1 `7 M& }6 C, f
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' ]& p: h. `' X% `1 E2 k! A
  4. frexp(x)6 S0 ]) o% C3 ]0 H3 v
  5. Return the mantissa and exponent of x, as pair (m, e).
    8 l9 \2 V! E- N
  6. m is a float and e is an int, such that x = m * 2.**e.1 C8 P- v" N: |- k/ j7 ~
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.2 Y( v! l. y; [
  8. >>> math.frexp(10)
    + {! E# L. r) W; V7 r: n; e# _  k
  9. (0.625, 4)* }/ R: J) I2 X. F$ s5 l
  10. >>> math.frexp(75)2 h3 Z. R; \9 }4 N" O. A' O8 I
  11. (0.5859375, 7)
    - y) t' b1 X( D  s- @8 I& t
  12. >>> math.frexp(-40)
    ' \$ ^0 X" V4 x, K, S
  13. (-0.625, 6)5 B' W+ k# A0 f8 t1 o* l
  14. >>> math.frexp(-100)
    - d/ F: C- w7 ~: W0 c
  15. (-0.78125, 7)
    8 n- U! c; A  O- @/ s  g
  16. >>> math.frexp(100)6 ~0 O. m4 b; }2 s
  17. (0.78125, 7)
复制代码
, o. v- \& ]5 g# ~
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
. i3 Y7 u% {# q. `
  1. #对迭代器里的每个元素进行求和操作, C4 K/ P) u6 }* D5 [2 l+ r: ]; D6 }
  2. fsum(iterable)! |6 Z7 T5 c, S
  3. Return an accurate floating point sum of values in the iterable.) Y  f3 Y; [- s
  4. Assumes IEEE-754 floating point arithmetic.; h; ~% g2 A4 q: e& \  H
  5. >>> math.fsum([1,2,3,4])
    & K; S( d; {* W, V" b) b; d
  6. 10.0( S# a6 F1 e$ C% h5 h2 I! N
  7. >>> math.fsum((1,2,3,4))
    + i' p# C+ e* Z$ N' w. |# W7 ?! @+ N
  8. 10.0$ v& B2 |8 u! s% W- q
  9. >>> math.fsum((-1,-2,-3,-4))" `5 s: w' v7 O: `
  10. -10.0& X; l7 D- y* N4 f
  11. >>> math.fsum([-1,-2,-3,-4])
    7 U1 _5 R2 ^5 a
  12. -10.0
复制代码

( c- A+ t- [" k" X, ~4 a, omath.gcd(x,y)  返回x和y的最大公约数0 E/ ~5 q2 X0 @$ z
  1. #返回x和y的最大公约数; ?* P% @$ d  I( c: p+ _* e
  2. gcd(x, y) -> int' L" n: J& P8 J; o6 K% F" }
  3. greatest common divisor of x and y7 }* F3 M$ H, c; B% D
  4. >>> math.gcd(8,6)8 b& u1 L$ ~# ], d1 [6 |. r
  5. 2
    2 _( Z9 f8 q8 I; G1 f8 ~
  6. >>> math.gcd(40,20)3 N3 W; b( p. U
  7. 20# c! y# c& |- d9 v7 G% ^
  8. >>> math.gcd(8,12)& c$ \$ W& `! a* h) p
  9. 4
复制代码

4 {: T5 I; z& o8 A  Q" amath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 b/ P  \5 N& u* Y2 Q
  1. #得到(x**2+y**2),平方的值+ z4 Q, S* p7 J- F
  2. hypot(x, y)% d; `9 [* ]% w2 |4 @! H
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 p) S8 B1 ]/ Y8 m
  4. >>> math.hypot(3,4)
    3 J; }. H4 D/ m9 v
  5. 5.0
    * n2 X1 Z6 E) d8 s$ P2 E
  6. >>> math.hypot(6,8)
    . \& W5 Z% T! N, i7 }7 f, y/ a
  7. 10.0
复制代码
, x$ ?( h: J2 N: v$ ~1 J
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False5 U7 r( e2 S( F5 ]/ _3 x. _
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ! ?  ~6 c! U# W
  2. isfinite(x) -> bool
    7 a4 v. \+ y1 w3 n
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    " K9 @9 u' @" D4 t5 K3 h
  4. >>> math.isfinite(100)' D4 d0 x# @! A( A' i
  5. True- {7 u% n" m* U' v  [
  6. >>> math.isfinite(0)! p7 j/ ]% [; E+ K% c: Z8 B
  7. True
    ; x, p5 Q/ {% U- C/ F2 I6 \" J2 i* m
  8. >>> math.isfinite(0.1)
    + K2 ^1 x/ j- ^- K* t% w
  9. True
    : ^$ ?% J/ ^4 O4 q
  10. >>> math.isfinite("a")7 S( ^, \4 J- J; I( M: ^8 V
  11. >>> math.isfinite(0.0001)5 S5 l8 a' q. m3 j; {
  12. True
复制代码

* |7 i- j8 E. K3 Zmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 }3 ^: W$ `; x# k; g8 W
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    4 R' L* N8 |2 b9 k" p9 i
  2. isinf(x) -> bool4 k( B" A  l5 P1 O7 O
  3. Return True if x is a positive or negative infinity, and False otherwise.
    6 p  B# i; C$ c! K
  4. >>> math.isinf(234)
    ; Y" H& a, d8 F9 {! v3 o1 }
  5. False
    7 T3 l# V' C. P. T6 y! `
  6. >>> math.isinf(0.1)# |+ T; H2 f8 W
  7. False
复制代码
6 a$ O0 r3 e8 a5 b
math.isnan(x)  如果x不是数字True,否则返回False) I, s. H5 v2 \- e& n+ O
  1. #如果x不是数字True,否则返回False3 i7 X. L3 C9 q6 p7 L' F; f
  2. isnan(x) -> bool
    + W' w( F8 }1 R
  3. Return True if x is a NaN (not a number), and False otherwise.
    1 Q0 T/ {" r/ @% u- P
  4. >>> math.isnan(23)
    0 s4 X& ]! v/ l
  5. False( L( y% V% r( H1 g) U
  6. >>> math.isnan(0.01)+ c. I" |9 p. S
  7. False
复制代码

+ p( A' ?+ _) S) z$ m; K7 Ymath.ldexp(x,i)  返回x*(2**i)的值4 u' _% h: {- O% {! u+ c
  1. #返回x*(2**i)的值# y$ v3 x3 Z% j! a
  2. ldexp(x, i)
    * E- l# ?6 E  h* I# R: L& F
  3. Return x * (2**i).+ e& w$ a3 e9 P$ b* Y3 `
  4. >>> math.ldexp(5,5)
    1 }: m4 i, U( h; x1 c9 J, p
  5. 160.0; e6 F% L: j% U2 D
  6. >>> math.ldexp(3,5)& H- O3 _' s8 s# _
  7. 96.0
复制代码

' u9 k" O2 f! lmath.log10(x)  返回x的以10为底的对数5 {' Z4 u) Y- n' n* O! J; z
  1. #返回x的以10为底的对数
    4 c$ l& W. f% g- ^6 S
  2. log10(x)7 ~$ [5 f$ \+ i; C. n* m" f
  3. Return the base 10 logarithm of x.' c8 N  K7 }5 L
  4. >>> math.log10(10)% W  d1 x: v) a
  5. 1.0/ T/ O1 Q' M" k. p
  6. >>> math.log10(100)
    ; S( c  d" u+ [9 ~; H) a! M
  7. 2.0
    $ [) W# v4 E9 `2 G% i
  8. #即10的1.3次方的结果为20
    - M6 e# M+ V9 H# D5 Q- Z# H- T3 Y
  9. >>> math.log10(20)* y* K& P: |; y# k* H) n
  10. 1.3010299956639813
复制代码
6 a: ^  P) h; }8 `3 W* _7 s2 q5 u
math.log1p(x)  返回x+1的自然对数(基数为e)的值% p. Y3 S6 x/ Y6 E- z
  1. #返回x+1的自然对数(基数为e)的值
    " ]/ {6 _" a( s9 L6 M* ~
  2. log1p(x)
    ) H# P0 O' q0 g9 t2 ?* g
  3. Return the natural logarithm of 1+x (base e).4 T* T& Q2 W. {9 R7 l- P) F+ B/ I
  4. The result is computed in a way which is accurate for x near zero.
    ( Z( y. o& L5 F/ u, p% i: t+ X
  5. >>> math.log(10)
    " Q; u8 b: K7 F( y4 `, b
  6. 2.302585092994046' q, H4 ^6 h& R8 D
  7. >>> math.log1p(10)% [. ~8 n% r- t* y
  8. 2.3978952727983707: b/ _" B4 e' n; T  e
  9. >>> math.log(11)' r- q% w: t/ j* M
  10. 2.3978952727983707
复制代码

) N+ ^& _8 h7 J9 T7 O; ^- t0 R. `math.log2(x)  返回x的基2对数7 D- [2 m* K5 c' G7 w- j& d- C2 o
  1. #返回x的基2对数
    7 ]) U8 S9 x) a; U& k
  2. log2(x)8 |9 S' A! B" u" T3 R1 C
  3. Return the base 2 logarithm of x.+ u( z. C% W% V4 G
  4. >>> math.log2(32)' s8 p' F& q0 Q9 U
  5. 5.0
    # t2 M4 e4 {% Y3 P
  6. >>> math.log2(20)9 V+ t0 }# p9 h( ?7 J/ z) e3 m
  7. 4.321928094887363
    - N3 z2 s  G& j) S: ]' I3 L# m
  8. >>> math.log2(16)/ c& k, J8 U  G; V3 b: P
  9. 4.0
复制代码
* I3 K" @! `. h
math.modf(x)  返回由x的小数部分和整数部分组成的元组
$ I* O, J; K* a! c; H' p! {
  1. #返回由x的小数部分和整数部分组成的元组
    ' _. k* p* }: [2 j/ ?: `5 ]+ x
  2. modf(x)
    1 i5 b9 s) ?0 t: T! {
  3. Return the fractional and integer parts of x.  Both results carry the sign
    * b! e# j5 t# n
  4. of x and are floats.
    9 K: O7 l  @: {6 j( b' T
  5. >>> math.modf(math.pi)
    " {' u$ D+ Y. i. [. l# |
  6. (0.14159265358979312, 3.0)
    $ K# g% g3 P/ E. p9 A
  7. >>> math.modf(12.34)
    7 B3 s  }6 ?. k6 ?" K# W- U9 q# L
  8. (0.33999999999999986, 12.0)
复制代码

  G1 G' H( o- D. \math.sqrt(x)  求x的平方根
# _% _  j' c3 K; g' R8 X. U  L
  1. #求x的平方根
    : T0 B4 B3 T* o+ V
  2. sqrt(x)0 ?- Y: D  t9 N1 k, \0 E4 l, C
  3. Return the square root of x.
    % C* u  E1 u2 s# Q' i
  4. >>> math.sqrt(100)
    & K6 [5 Y' ]3 Y& N8 {, _* o1 d
  5. 10.0
    % \: I! ^. u2 r. }+ g
  6. >>> math.sqrt(16)
    , @" x$ a: _$ s
  7. 4.0
    % w2 Q) C* t3 {; Y2 ~+ N
  8. >>> math.sqrt(20)  q' `( g$ g% t: W+ k
  9. 4.47213595499958
复制代码

, r( \! \3 ^: s/ q/ O6 h: Lmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分! Q  I) i; y- i7 S
  2. trunc(x:Real) -> Integral
    7 Y  B  C2 f' ~( a7 a6 n
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    / S7 R: R  Q9 @, X# X8 w
  4. >>> math.trunc(6.789)
    1 S- }5 F9 h+ F, u. z
  5. 6
    0 W$ U9 R" d& _& s# h0 ]
  6. >>> math.trunc(math.pi)" ]& `0 k+ R% D* z* O- F( a
  7. 37 g; p' u0 A7 I3 Z! P/ P% K- w- ?  q
  8. >>> math.trunc(2.567)8 H. u0 Z) N8 a" b0 X
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-28 21:41 , Processed in 0.084702 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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