新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

- @' H, \8 R+ ^. m【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; T  q$ H, S8 m5 I: o$ i) ^( x
; _* L$ b( u  l2 `1 _7 x. g0 m
方法1! T- \- T  `) A6 C
  1. >>> import math
    ) U$ V, F  `6 Z2 M
  2. >>> math.sqrt(9). [# z7 a: _1 }3 K; j, e$ P$ ?* R
  3. 3.0
复制代码
方法25 T, o+ D) Z) o  c
  1. >>> from math import sqrt
    4 B8 h+ Z4 A+ C' N: y% b
  2. >>> sqrt(9)
    4 r1 M3 C7 ]2 X, n% ?  q
  3. 3.0
复制代码
0 s6 Q& t2 w8 X# M0 ]

9 e/ F5 E' G4 l4 b, _: V& p
math.e  表示一个常量
* u0 X4 w' v0 t1 H! k
  1. #表示一个常量: ?1 F, F) X0 H
  2. >>> math.e
    % r& G0 D! g" k) [9 e/ D
  3. 2.718281828459045
复制代码
) q. a+ m5 c- I1 r6 H; a& j! o8 V
math.pi  
数字常量,圆周率

1 Y  U2 i7 H* s
  1. #数字常量,圆周率) Q/ d, {- P7 R3 N* X7 E
  2. >>> print(math.pi); H) A" Q- [) h- J0 M% v
  3. 3.141592653589793
复制代码
: \# |8 G  x$ j: ?0 J) b
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 O1 E5 L5 n0 i# W9 x! c" Z8 v% j* J
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x! Y1 i& ^. l" B/ M3 d
  2. ceil(x)
    8 e9 n% t2 D' S& }
  3. Return the ceiling of x as an int.2 }' `. w( G6 S0 g
  4. This is the smallest integral value >= x.4 m1 |' M# I8 b; t6 I! E; n. x
  5. - L4 n1 f9 g4 O" [: r
  6. >>> math.ceil(4.01)
    " H4 P( u- l% o- C4 F- [
  7. 5! H' ]- G4 Z8 T9 g
  8. >>> math.ceil(4.99)
    ; g$ A* F& R. r+ ]; H2 m4 o$ n
  9. 5
    5 X( y5 w. s) G) J" s* r
  10. >>> math.ceil(-3.99)" D( e* O5 ]' z- H6 z
  11. -37 ?2 h" ^9 r" A/ Q
  12. >>> math.ceil(-3.01)
      \5 F% \3 \5 K- i: ]
  13. -3
复制代码
) @3 h4 w$ X! f
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 a: b/ Q8 V% T/ P, @0 P
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ O2 V/ V6 `& m3 @
  2. floor(x)
    1 n/ I1 w# O3 w
  3. Return the floor of x as an int." t; G4 y6 Z8 F  N, J, h
  4. This is the largest integral value <= x.  J; Q6 X$ s7 c5 k
  5. >>> math.floor(4.1)9 Y* b6 u3 ]# u% _& V$ x. D" k
  6. 4
    ; d) V- s9 }7 K* W- G
  7. >>> math.floor(4.999)
    ( n) a0 n2 j) t/ z
  8. 4
    & h0 g) S* h8 I) C5 P
  9. >>> math.floor(-4.999)- ]" C7 Z& U* P  ?4 u. |
  10. -5
    ( k- x6 l) @3 v; _0 c0 m- |
  11. >>> math.floor(-4.01)
    2 V$ l  \# K" m2 h( L
  12. -5
复制代码
6 Z1 r5 ~+ x9 F* h
math.pow(x,y)  返回x的y次方,即x**y; B8 a" j) |6 ?8 L
  1. #返回x的y次方,即x**y: n: q- p4 s1 o: F
  2. pow(x, y)0 F) X1 Y4 D2 v3 P  v$ e+ Y
  3. Return x**y (x to the power of y).
    : `7 h/ V: G/ F" O8 J# N) P
  4. >>> math.pow(3,4)5 J- z, s) l9 j9 e
  5. 81.0
    ' ?. O& P4 _* k) k) L- s/ V
  6. >>> - i2 A9 e3 ], D' e, e% v4 a; ^
  7. >>> math.pow(2,7). a. L  E3 b# _0 e6 P
  8. 128.0
复制代码

1 b+ g8 a$ k2 c1 j9 Amath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" s4 z& m5 c3 g! Z" f
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    9 A5 c* p/ O& e
  2. log(x[, base])
    $ W7 L) ~  e. d# q. N
  3. Return the logarithm of x to the given base.7 z/ ~: b7 L% O+ u" s! G
  4. If the base not specified, returns the natural logarithm (base e) of x.
    * ^  w: G5 a# i2 X7 q1 [4 T4 F
  5. >>> math.log(10)* ]' Z* I; ]& l2 y: h
  6. 2.302585092994046
    & A1 }  O; ]$ W9 Q
  7. >>> math.log(11)
    . r* ]/ \7 V1 `5 B- |+ b' I1 P+ r; R
  8. 2.3978952727983707
      m6 P: N; s* M2 B# H
  9. >>> math.log(20)
    , h' W# L! H1 B/ m& K6 z; M3 c* e0 W, B
  10. 2.995732273553991
复制代码

2 @% ~: M  s5 Q/ V* R6 a2 imath.sin(x)  求x(x为弧度)的正弦值7 ]+ S$ K/ l+ a4 R) |9 n
  1. #求x(x为弧度)的正弦值7 ]. v) c$ [! u2 R% @- R# p) R
  2. sin(x)/ w$ j, E4 E. O' e
  3. Return the sine of x (measured in radians).
    9 F. A7 J7 }! l
  4. >>> math.sin(math.pi/4)& b- L1 i* |) P7 v
  5. 0.7071067811865475, L4 b1 e8 Z. x4 U0 b: L
  6. >>> math.sin(math.pi/2)( T% l6 v) W) K* z$ x# c
  7. 1.0
    4 j. b6 O0 u( u- \8 n
  8. >>> math.sin(math.pi/3)
    1 u2 l$ ~& F" ~! p6 K+ w6 U
  9. 0.8660254037844386
复制代码

' z, a' l  Q2 y+ Dmath.cos(x)  求x的余弦,x必须是弧度2 i" W8 D0 T/ J# K1 u2 x" I( q
  1. #求x的余弦,x必须是弧度
    - j( g# W+ H7 _. z/ |6 Q4 W/ S
  2. cos(x)
    4 [2 P9 n) f5 Q/ A0 s8 O5 w  Y
  3. Return the cosine of x (measured in radians).  B+ p2 a6 S$ U! P: O3 _. Y
  4. #math.pi/4表示弧度,转换成角度为45度
    & u. L) t9 ?/ x/ n3 U/ Q9 V1 n
  5. >>> math.cos(math.pi/4)
    ! o- y' J/ j4 W& O
  6. 0.70710678118654764 w# m$ y5 W1 H7 F* q- _" s5 z  R
  7. math.pi/3表示弧度,转换成角度为60度
    # y; \% X+ c. t% s3 J
  8. >>> math.cos(math.pi/3)5 D% N, Z0 W) k1 \1 G
  9. 0.5000000000000001' \6 _# o  E- l) W, c2 }4 c
  10. math.pi/6表示弧度,转换成角度为30度9 Q# l4 y! U" @  C
  11. >>> math.cos(math.pi/6)
    9 A7 B/ n* S  C. `: ]. Y$ @& u0 q0 c' L
  12. 0.8660254037844387
复制代码

% g6 g: w$ w! b% B& Q$ cmath.tan(x)  返回x(x为弧度)的正切值$ T6 i3 c4 @  j# {7 i/ Z
  1. #返回x(x为弧度)的正切值' |. ~. S* N; B) B# z
  2. tan(x)' e: T/ O0 {: `6 D( i8 p
  3. Return the tangent of x (measured in radians).3 L& h  m/ }* S1 F! l1 M8 ?3 u
  4. >>> math.tan(math.pi/4)) o" x" C$ b2 s, @$ Y9 I  O6 r* O
  5. 0.9999999999999999
    : `  R/ l/ o( f" ~- E
  6. >>> math.tan(math.pi/6)
    # l4 N' a4 j# t- {. _! ]( k+ g
  7. 0.5773502691896257! V8 d% r( a4 r# T
  8. >>> math.tan(math.pi/3)
    4 K7 j( z' [/ D; l' l+ p; L+ d
  9. 1.7320508075688767
复制代码

0 M$ @" X" [8 V' l' y- Hmath.degrees(x)  把x从弧度转换成角度
/ D9 e$ Z5 F+ K3 W- ]/ y" M; b! G
  1. #把x从弧度转换成角度" D! E# t$ B) l4 ?5 w  o; W
  2. degrees(x)* b& [& y: K8 f
  3. Convert angle x from radians to degrees.1 N9 S  Q7 s* [' D

  4. , H5 N5 k  \( [) R# c3 R) d
  5. >>> math.degrees(math.pi/4)7 ?" s& w: V! X+ [5 C
  6. 45.0
    " ~0 ]9 K2 z8 z$ m7 S
  7. >>> math.degrees(math.pi)
    ( F4 p/ j4 @, z+ Y% Q
  8. 180.0
    - W9 j. N6 U: U& w- g  G. D
  9. >>> math.degrees(math.pi/6)$ j5 b, q$ N6 \, ^' E) f( B. i% S, ]
  10. 29.999999999999996
    , z& }9 E7 ]8 V7 n4 z
  11. >>> math.degrees(math.pi/3)
    ! q5 Z) U% A# `- d
  12. 59.99999999999999
复制代码

. R! M8 l& j* o# ?math.radians(x)  把角度x转换成弧度& l8 ]5 t% k+ N" C8 U! f) f
  1. #把角度x转换成弧度8 a/ X7 e5 K" W: r' e2 y
  2. radians(x)& `* d) m: O1 W
  3. Convert angle x from degrees to radians.$ C1 t$ O& I2 U  _
  4. >>> math.radians(45)9 Y8 B: M5 _: z7 }. v4 ]/ k
  5. 0.7853981633974483
    5 {6 Q; Y+ d; N6 f5 R% r7 H
  6. >>> math.radians(60)2 n5 h$ [) N& F0 _  N* ~
  7. 1.0471975511965976
复制代码

5 ^1 u4 L; |& H& @1 a# X, C  Mmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
+ [- k/ _' L% ~2 E+ Z0 G% `( {1 S
  1. #把y的正负号加到x前面,可以使用08 K1 P( r6 m& _2 ]
  2. copysign(x, y)" F# I5 a0 G$ T
  3. Return a float with the magnitude (absolute value) of x but the sign
    ! `; j; t9 J) v' G
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    6 H, H. l: b5 D2 T7 J
  5. returns -1.0.
    5 e. g. r) d0 E  t

  6. 7 S1 \& n4 R* E
  7. >>> math.copysign(2,3)$ E2 c( _& @7 K+ M0 i  J0 g
  8. 2.0
    ; V! [; `9 D" H+ R, q
  9. >>> math.copysign(2,-3)
    7 m7 |1 B8 S- a& g0 \& e4 D! n9 ]
  10. -2.0
    - E1 b+ G4 V: }7 g
  11. >>> math.copysign(3,8)
    : z) z  P4 d6 F7 v# T+ s0 K
  12. 3.0" L7 [! G) H* d6 U% @0 u
  13. >>> math.copysign(3,-8)# h% d5 k" ~$ P$ Q+ f! q
  14. -3.0
复制代码

2 l: C' {" {/ B. \- wmath.exp(x)  返回math.e,也就是2.71828的x次方
7 j/ r5 H8 e1 X: [" o0 ]/ I
  1. #返回math.e,也就是2.71828的x次方- ]' n: A/ q- I% ?" r1 L
  2. exp(x); Q9 b* F0 d2 O) A
  3. Return e raised to the power of x.# g; p7 r3 n" s# q

  4. ( J0 Y6 r1 c5 I; @  {5 d9 ]
  5. >>> math.exp(1)
    8 O3 A3 H6 f+ T) n8 d0 O
  6. 2.718281828459045
    0 X8 t! Q$ I# Q8 U7 p
  7. >>> math.exp(2): Y( i# `# r. A. ^4 ]9 Y( N
  8. 7.38905609893065
    - N5 |! {' K7 h% g
  9. >>> math.exp(3), z) S& ?0 I. G9 u
  10. 20.085536923187668
复制代码
+ T: c; u5 [5 l: P8 Y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 ^2 u8 M* ]$ e
  1. #返回math.e的x(其值为2.71828)次方的值减17 M8 F6 u" g+ |/ b: K/ P" [* j$ W2 l* m; f
  2. expm1(x)
    * n$ Z( {3 q$ K9 Y: _$ J( c0 T
  3. Return exp(x)-1.
    3 c5 s+ i$ p) ?( B0 F
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ) x2 [6 R9 y' q8 M, @6 ^$ |) g
  5. 2 H% J2 p, K/ H* l) E9 k4 o
  6. >>> math.expm1(1)- o& l+ r* R2 {3 C! |
  7. 1.718281828459045
    6 P0 x1 F# i& f. e3 t
  8. >>> math.expm1(2)
    0 h" F" g2 n% B0 d$ E) _. E! X# i
  9. 6.38905609893065
    5 U; Y6 g- Y+ e! G4 c, \, Z& @
  10. >>> math.expm1(3)& w/ k9 i5 \9 E0 N0 H. Y; s
  11. 19.085536923187668
复制代码
& G8 H: G0 u% t' @6 J
math.fabs(x)  返回x的绝对值
& i* p% C9 y% \" C% m9 i7 y; p. @2 v! e
  1. #返回x的绝对值
    ( D9 S/ `' j( B
  2. fabs(x)
    * x1 Z  S& d  k: `
  3. Return the absolute value of the float x.
    . o. P3 s, Y; Q
  4. # _3 u- O# Y" t
  5. >>> math.fabs(-0.003)8 h! g+ `( g/ P" ^2 R
  6. 0.003
    6 y9 _7 F/ K8 v1 t
  7. >>> math.fabs(-110)" R; C/ P+ i; z$ i9 @! G: y1 k$ c
  8. 110.0; C# d/ Z1 k4 g5 b- H2 ?
  9. >>> math.fabs(100)
    + M8 L4 A# E$ o9 O$ ~- z: t" V' f$ g
  10. 100.0
复制代码

7 W; f  h8 e$ W: Lmath.factorial(x)  取x的阶乘的值# r9 H# w- t' e! ^, }
  1. #取x的阶乘的值. _# ?% s( q" ~. a4 T3 [
  2. factorial(x) -> Integral. h) L8 g, a5 K% A! \
  3. Find x!. Raise a ValueError if x is negative or non-integral." u" Z6 ?$ J+ v+ G
  4. >>> math.factorial(1)9 y9 q5 N1 w3 M9 e; q) n7 Z& j3 w7 F
  5. 1$ Q" P  a" h2 O% e8 g
  6. >>> math.factorial(2); Q# Z+ A0 Q$ B! u5 |, b% u5 g( I. b! I
  7. 2( S1 O* K3 G! Q7 _, c; X
  8. >>> math.factorial(3)
    % Z3 `) x: B# V8 i+ K3 `1 c% m
  9. 6
    . Z4 |! i7 X# N# N- r- ]/ l
  10. >>> math.factorial(5)
    " L+ D; X! Z9 S9 K/ B$ [
  11. 120
    4 O* B% x* B: l8 k  l
  12. >>> math.factorial(10)3 C9 ^9 z' P( _2 d  Y2 F, ^
  13. 3628800
复制代码

) f- n8 v5 b8 J  b9 B1 o3 omath.fmod(x,y)  得到x/y的余数,其值是一个浮点数. i) E# z* B! w* w  A- j
  1. #得到x/y的余数,其值是一个浮点数2 Z* v3 U/ S) F# y) l; j$ x. \" _  x) _
  2. fmod(x, y)0 R9 f: g) q( S! Z9 L
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    1 c! m* v$ g/ D. z. Z
  4. >>> math.fmod(20,3)
    & F) v/ ]# g& J, }
  5. 2.0
    : Q5 T/ d& g& j/ M$ y; ~
  6. >>> math.fmod(20,7)
    # Q" F% \& f2 i9 I$ R
  7. 6.0
复制代码

# F. \+ t  l( c  z5 emath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
+ \, U. k  d  y3 _* W& H
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* E7 _( Z, L" L- a
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    , P# N8 v7 t+ a/ D8 K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- Q* a* R) O2 E' e) w- J; f* O/ y
  4. frexp(x)
    1 I. M! s& {) _
  5. Return the mantissa and exponent of x, as pair (m, e).
    # {' m% L: n7 p9 ?
  6. m is a float and e is an int, such that x = m * 2.**e.
    % M3 s2 S7 x) z  p! t1 P
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    6 W& }! [4 t6 Y; b& J. u9 d
  8. >>> math.frexp(10)' W8 u' ~1 x/ |$ j
  9. (0.625, 4)
    , x* f  s1 M  F. n
  10. >>> math.frexp(75)
    $ s* Z% L4 [- n- H4 H3 ^# `3 U
  11. (0.5859375, 7)8 u$ V( [' g' K" R4 f+ f- @; G
  12. >>> math.frexp(-40)5 W5 J9 l' j. }' `, v
  13. (-0.625, 6)6 e6 A. d8 t3 N7 b6 m2 z
  14. >>> math.frexp(-100)" Y, J+ S, B! ?1 c  k
  15. (-0.78125, 7)# R6 ^. Q3 h4 E
  16. >>> math.frexp(100)
    # S7 n; G6 _3 C$ b5 s! i
  17. (0.78125, 7)
复制代码

! ^6 L8 k5 Q+ u9 Y' ]math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列9 }2 m' N) Z7 p$ C4 e/ l9 T( B
  1. #对迭代器里的每个元素进行求和操作
    & a6 Z& O. X; t
  2. fsum(iterable)4 E, U6 V1 Y( g* j. b% O! A$ b
  3. Return an accurate floating point sum of values in the iterable.% B. \1 R4 G. I; R$ e$ p2 [' [
  4. Assumes IEEE-754 floating point arithmetic.6 u) h' x3 j& B7 p0 k
  5. >>> math.fsum([1,2,3,4])" I* G+ ?: g6 i4 L% m1 r* ^
  6. 10.0( z0 ?3 D* s2 ~3 u9 x+ m
  7. >>> math.fsum((1,2,3,4)). P* q' E: i( C. k
  8. 10.09 k# C( x/ l3 A+ R0 c" b
  9. >>> math.fsum((-1,-2,-3,-4))
    4 A1 E7 V4 E: i9 ]' a7 |
  10. -10.0
    ( m. I9 S9 {- L, j
  11. >>> math.fsum([-1,-2,-3,-4])7 I/ y# h" r: @, f( B( o
  12. -10.0
复制代码
* j% l" M2 K# N& U- z- C
math.gcd(x,y)  返回x和y的最大公约数
, {$ }% J) ]9 m# s7 _; r6 f( ]
  1. #返回x和y的最大公约数
    $ e  o# E6 s% i* J! F! f
  2. gcd(x, y) -> int1 O, b% H5 Y0 o$ E, g# O8 E
  3. greatest common divisor of x and y
    9 H: [4 L! W  q2 \1 E& F
  4. >>> math.gcd(8,6)/ H0 D1 S+ H' ?# ^! g0 Z. M( p
  5. 21 |% B- u! s0 x& |) @1 m' s( ^
  6. >>> math.gcd(40,20)
    5 J( M, m. N. P
  7. 209 d) K& `/ Y: m2 _" E
  8. >>> math.gcd(8,12)/ S) Z# Q  n8 d) g7 f- F! V
  9. 4
复制代码

9 J5 ^  c0 b; Z' ?8 h" m- A7 X- v2 b8 Bmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
6 e) `; K. C% y
  1. #得到(x**2+y**2),平方的值
    7 I# H7 w4 I1 d3 a" Q
  2. hypot(x, y)* b8 R; d2 D$ k3 k  X0 P6 ?7 J
  3. Return the Euclidean distance, sqrt(x*x + y*y).& c7 O+ s. j4 J; E
  4. >>> math.hypot(3,4)2 o, x' M6 b5 N/ L% a1 c
  5. 5.04 }. n/ Z9 j* H% g+ l; I
  6. >>> math.hypot(6,8)0 `* G4 M* k! b1 a& I
  7. 10.0
复制代码

$ }) l. a. G6 E. w( omath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
' {# W1 }% E8 h% X
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    / a% Y/ L% Y# {6 q
  2. isfinite(x) -> bool
    / e1 E5 |- d- o3 V6 P4 p& x, F
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ; X8 Z4 m4 B" o5 I) |
  4. >>> math.isfinite(100)! D# Q/ o/ C8 w$ S# K3 e% e
  5. True5 M( r* M$ J1 [8 s: i6 L
  6. >>> math.isfinite(0)
    8 r# c! J- S  G
  7. True
    # @* i' v- y8 @7 X2 K
  8. >>> math.isfinite(0.1)
    ' D/ m2 P, c# v& D
  9. True
    4 b* P% E/ \0 O2 ^8 x
  10. >>> math.isfinite("a")0 ~+ }6 R, Q/ K8 H
  11. >>> math.isfinite(0.0001)
    $ q0 d! \5 @' I% C' p0 U% z
  12. True
复制代码
8 f! G8 I  J6 p4 j$ _
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
- s, E8 K" |4 c0 ^  d5 k: I, Q3 \
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False8 O) E( k9 @) C7 h; R  M6 F3 }9 Y$ J
  2. isinf(x) -> bool: ?& B* F# ?. V/ h0 S
  3. Return True if x is a positive or negative infinity, and False otherwise.; A- T* X5 e' K9 T
  4. >>> math.isinf(234)8 m5 a4 f1 I; {% O
  5. False
    # `2 ^$ }8 m1 {
  6. >>> math.isinf(0.1)
    + g9 t5 O7 s* k" N
  7. False
复制代码
. |8 x2 Z, l4 V2 L" Y& D# Q
math.isnan(x)  如果x不是数字True,否则返回False# k  }' ?. @% o5 a( ~/ n+ }( H
  1. #如果x不是数字True,否则返回False! }2 X) u/ J! N! r
  2. isnan(x) -> bool
    3 G. u/ B$ `& i, n6 G1 }& a
  3. Return True if x is a NaN (not a number), and False otherwise.
    1 m) u* p9 n6 g6 N; M# o
  4. >>> math.isnan(23)
    / n. n/ [3 U: ^% g! C" |& x1 y
  5. False
    : j" Y7 \& }8 B" g  g
  6. >>> math.isnan(0.01)
    % T1 Y, |( h/ f9 L
  7. False
复制代码

8 [$ `3 y  J" c1 @4 Wmath.ldexp(x,i)  返回x*(2**i)的值
0 j  V2 U/ y7 R9 G3 N- I
  1. #返回x*(2**i)的值4 W& [* r; E; [7 u
  2. ldexp(x, i)
    & |8 h$ q+ O& z
  3. Return x * (2**i).
    # u# G, l0 u4 \. u
  4. >>> math.ldexp(5,5)9 C& |2 w% v- u' v* a( x
  5. 160.0- i2 f/ B4 Y0 [, V4 q
  6. >>> math.ldexp(3,5)! y. L) e8 h1 n$ `
  7. 96.0
复制代码
7 j) j" R& ~+ l8 a  y' }& c& {
math.log10(x)  返回x的以10为底的对数
* x& G& f2 a. N- G
  1. #返回x的以10为底的对数
    0 s: T8 B/ P5 a5 c4 p% x1 p
  2. log10(x)" w$ p% S  B7 S9 x' u
  3. Return the base 10 logarithm of x.
    4 B$ x6 J/ D4 ^: X' W1 k
  4. >>> math.log10(10)6 T  ^, w' b% i- i7 l. F
  5. 1.0
    / D: v4 h; Z5 }* b8 B+ B/ `
  6. >>> math.log10(100)
    4 N, n  w' p6 ?, ]3 \
  7. 2.0
    & O6 k. l) s  e5 N/ @
  8. #即10的1.3次方的结果为20
    % {, P2 ?) q4 D$ w6 s
  9. >>> math.log10(20)8 c6 B( U' y( c" q1 e
  10. 1.3010299956639813
复制代码

3 M: y. y7 q, Y+ b# Omath.log1p(x)  返回x+1的自然对数(基数为e)的值
# x  H/ S6 W$ D
  1. #返回x+1的自然对数(基数为e)的值
    / F* S; ^8 Z+ ~  N" J; ?% g/ e
  2. log1p(x)
    ) ~9 p5 q9 S) u; X3 \7 F* J
  3. Return the natural logarithm of 1+x (base e).1 u4 Q( \( @: F: y8 F3 c5 D
  4. The result is computed in a way which is accurate for x near zero.
    " m  O7 [8 O+ f
  5. >>> math.log(10)" k4 b; O0 ?2 x4 y
  6. 2.302585092994046" P: }+ i$ g8 F$ T3 N- T. s. e" Z
  7. >>> math.log1p(10)
    % Z# y6 b. Y3 ~
  8. 2.3978952727983707
    2 \! N0 l! U+ h
  9. >>> math.log(11)* f5 e/ x" y% ?2 g+ D' q
  10. 2.3978952727983707
复制代码
+ [0 @4 p; A- I! I7 V, J
math.log2(x)  返回x的基2对数
5 v; |7 {& W8 p! z7 |2 S
  1. #返回x的基2对数5 j" [; \7 d$ ~6 s6 n6 X% s
  2. log2(x)6 K, a0 `5 `) ]; D* g! B8 R4 C' I
  3. Return the base 2 logarithm of x.3 C5 w. k! D1 E; B) D% H6 ]
  4. >>> math.log2(32)
    : F) g' X/ x7 t* x1 A; n8 Q4 m9 Z
  5. 5.0
    ) ^& A( a/ ?/ O7 `  N
  6. >>> math.log2(20)
    $ V3 ~! W- W7 n. _! D# q2 J+ b/ K
  7. 4.321928094887363/ \0 P3 j4 O- p3 b8 T6 R9 B+ ?( b
  8. >>> math.log2(16)
    5 ?  r2 G3 ], L( x% E# N- Q& U
  9. 4.0
复制代码

" H. a& q$ X9 h3 Y7 \1 q" \# [math.modf(x)  返回由x的小数部分和整数部分组成的元组' R( t  C9 S, c9 h9 u9 X2 A
  1. #返回由x的小数部分和整数部分组成的元组
    1 O  @8 i2 U5 K& H) E
  2. modf(x). F, s8 k$ ]7 ~' f+ K9 X( c
  3. Return the fractional and integer parts of x.  Both results carry the sign
    / y( z- g/ ~2 ]% |: _: `3 X( k
  4. of x and are floats.) j6 D& P% y5 E
  5. >>> math.modf(math.pi)
    $ M% `4 I) X7 i
  6. (0.14159265358979312, 3.0)
    * t1 J" A9 L9 \. T
  7. >>> math.modf(12.34)4 P! K# B0 _0 v8 w1 o
  8. (0.33999999999999986, 12.0)
复制代码

7 ]0 v$ q/ F: g0 C% |& Bmath.sqrt(x)  求x的平方根
$ E2 e& B$ \( h* h4 G+ I
  1. #求x的平方根
    * Q0 ?) @7 X, M
  2. sqrt(x)
    7 ~4 i2 _( X" C" }* y! B; n4 y* d
  3. Return the square root of x./ r9 x" N: ]3 q. e+ l1 l
  4. >>> math.sqrt(100)* I9 r( y- {1 `
  5. 10.0, Y3 y  {- A6 {
  6. >>> math.sqrt(16)& V$ ]* @+ g3 t; |! J: Y
  7. 4.0
    7 H( J; \  ~* U& ^% n- G4 B* A
  8. >>> math.sqrt(20)% v3 k! v- I* q) D2 n- K3 \0 i
  9. 4.47213595499958
复制代码
* m6 v4 R) ~# N- \) g
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    . _) W* t4 G: _+ |8 @' }4 C
  2. trunc(x:Real) -> Integral
    - E6 E0 s0 l$ O* p; m, t3 x
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ! a: ]9 r0 p. b4 o9 X
  4. >>> math.trunc(6.789)
    - W' J; S6 u3 b* f3 u
  5. 61 l' |" l* D5 C" Y& P1 a" N
  6. >>> math.trunc(math.pi)
    0 w% e2 q! j0 V* w" f' C
  7. 36 c/ p) R8 W% l1 N4 n) }
  8. >>> math.trunc(2.567)
      L+ ^2 b4 g6 D& z" p, Y* }
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-25 22:59 , Processed in 0.081322 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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