新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
0 M0 l' }+ |9 ^. ]) c+ d0 t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& d' T  C: S. C# P
; Z- r% D+ Q  j) l$ ?7 D
方法17 K; Q; w$ @/ ~' v8 U& S, N6 x
  1. >>> import math8 |$ }2 [+ ?2 V; p
  2. >>> math.sqrt(9)
    , Y3 P+ t/ H1 ~& c7 _) h( F
  3. 3.0
复制代码
方法2, C0 L5 E# K% a) n9 Q
  1. >>> from math import sqrt) q' g! b5 K- k9 k3 {; J; o1 ^3 k' O
  2. >>> sqrt(9)1 p- M5 l+ O0 W6 o, U
  3. 3.0
复制代码
6 w  P- Z3 {7 a9 \" o

; ^+ i5 C  `% x3 j/ F# r" O
math.e  表示一个常量4 b5 n/ ?8 Q- R2 R1 g( s
  1. #表示一个常量, W! S- ~9 `! d& H
  2. >>> math.e, ]( n* F0 F) S- v
  3. 2.718281828459045
复制代码
  H# n. F. T# E
math.pi  
数字常量,圆周率

. ]3 |: E! Z2 u0 L* e
  1. #数字常量,圆周率
    4 m/ p# h9 r' L# M; i
  2. >>> print(math.pi)1 X4 _) V6 S: A! Z4 Z' K
  3. 3.141592653589793
复制代码
4 q( M+ t4 U- O* C$ U3 j. H- A
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
& p2 x+ L: L( i$ u  ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    # ^% c% B: i$ M& x
  2. ceil(x). u5 O5 w" n$ t$ ^- P- a
  3. Return the ceiling of x as an int.
    ' ^% W' D4 T  H" W) t+ u4 T
  4. This is the smallest integral value >= x., h* E9 D3 W5 s( g

  5. - G: B! T3 L' i7 T$ G% {
  6. >>> math.ceil(4.01)
    + W* j" j7 ^! U5 X" B7 p
  7. 5
    ; O: N, ]' I8 s' C7 |2 M+ t
  8. >>> math.ceil(4.99)
    4 f2 q/ j- n9 {9 _
  9. 5
    & R! d4 c8 x: W: P
  10. >>> math.ceil(-3.99)
    " ^6 [  o. ?3 m" E
  11. -3
    # t" d" q2 W, H+ A
  12. >>> math.ceil(-3.01)7 \( t' D8 d) ?. z+ p3 b
  13. -3
复制代码
- ?5 p! J# `: X5 Z" b2 @* J0 d
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 i& Z- f" D9 S7 U
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    . V4 U- d/ K# u; h
  2. floor(x)3 u2 I3 E" W" p' m6 d1 P5 x
  3. Return the floor of x as an int.
    2 @* y; o) c& M; r4 x/ A
  4. This is the largest integral value <= x.
    ) V" i) x) U) J+ m
  5. >>> math.floor(4.1)
    2 A. |, k  e# g* F
  6. 45 Z3 w5 v  _$ m( _( }
  7. >>> math.floor(4.999)  r! N4 X% g7 _0 X& u/ F0 H
  8. 4
    - U7 r* v- G. A2 w/ n6 a4 c- u
  9. >>> math.floor(-4.999)
    ' g" {" r& F' s3 A) d( i
  10. -52 ]5 h0 E! P- z5 m; p
  11. >>> math.floor(-4.01)6 ]* F* K6 r3 D0 H5 ^3 g" w
  12. -5
复制代码
6 Y' K9 {! O" ^7 v
math.pow(x,y)  返回x的y次方,即x**y$ x3 Y( H6 Q3 Q
  1. #返回x的y次方,即x**y: U0 k. y+ \/ a0 v
  2. pow(x, y)- s7 z8 r; Z3 B0 q
  3. Return x**y (x to the power of y).
    $ D, n) C0 \- x6 f$ _( X
  4. >>> math.pow(3,4)
    4 y! D8 x% U$ l5 w' T; @4 ]0 ^1 n
  5. 81.0$ M9 Q, r3 m! z' t% j- Z
  6. >>> # X7 ]& C) B/ |3 k: I$ ~$ k  V$ I: \
  7. >>> math.pow(2,7)
    3 e5 {7 O/ j! e+ F* e+ ]3 w( x
  8. 128.0
复制代码
) B9 h( }7 Q% L% F  U( i( h/ x/ X3 W
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 p6 o; V/ k' Q& y5 n# z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    * v' d  e/ \3 Q/ z7 C
  2. log(x[, base])( Z: J9 \/ i) \
  3. Return the logarithm of x to the given base.
    5 {& {: m5 }$ J8 D6 y0 f
  4. If the base not specified, returns the natural logarithm (base e) of x.
    # w9 d+ e" g* R
  5. >>> math.log(10)
    4 g# U' O0 r4 b" s1 d" m" F
  6. 2.302585092994046
    0 `# Z0 H- a# k* y; z
  7. >>> math.log(11)4 n7 ]( `6 T* h' H( X2 \
  8. 2.39789527279837075 T4 z- W% A2 r) G8 U! M( X
  9. >>> math.log(20)
    9 z& ~" I) M7 G7 K
  10. 2.995732273553991
复制代码
( @: x9 g6 u- ^
math.sin(x)  求x(x为弧度)的正弦值0 O/ F% V7 ^& \3 p9 N
  1. #求x(x为弧度)的正弦值+ A6 ~6 n: A& o" ^8 x2 n5 `; g" j
  2. sin(x)5 T: w9 P* o1 |& c, ~. _
  3. Return the sine of x (measured in radians).
    $ N* G* ^6 N0 [$ M" h/ D( Q) U9 L' L8 K- ]
  4. >>> math.sin(math.pi/4)
    0 r. K4 k0 ]8 A
  5. 0.7071067811865475
    . ~5 d% _4 _9 o) \  K9 ?! ?
  6. >>> math.sin(math.pi/2)
    . ~0 o# U' z6 Z
  7. 1.0
    & }# G' |3 ]5 ~) Q
  8. >>> math.sin(math.pi/3)) F' M( k9 Q3 \# w
  9. 0.8660254037844386
复制代码
, N& A9 b% ~5 ^' {! N2 y
math.cos(x)  求x的余弦,x必须是弧度' h' o! g  t6 i4 ]5 K0 g- Z- x/ e/ Y
  1. #求x的余弦,x必须是弧度. o! j5 R5 l# z/ ?3 t" q/ c6 s+ `
  2. cos(x)
    , K$ \+ a  R9 W8 `
  3. Return the cosine of x (measured in radians).
    % V  V6 ]5 Y" A! E1 _- p
  4. #math.pi/4表示弧度,转换成角度为45度
    - Q/ ~* `, P$ O9 D/ i8 m
  5. >>> math.cos(math.pi/4)# [7 @& B( O, n+ d/ |
  6. 0.7071067811865476; z. m/ l; b# U- P$ Y
  7. math.pi/3表示弧度,转换成角度为60度
    ! R4 e) m7 Z' T9 A) h
  8. >>> math.cos(math.pi/3)+ l. H+ K1 Q, w* T
  9. 0.5000000000000001& \9 O" K+ y9 k4 [, m
  10. math.pi/6表示弧度,转换成角度为30度4 W0 q0 ?9 F) g
  11. >>> math.cos(math.pi/6)& J0 H+ |2 y: ]+ `0 O! `
  12. 0.8660254037844387
复制代码
' y+ ~0 {% J) c; c5 r# A
math.tan(x)  返回x(x为弧度)的正切值7 A0 R1 c& M& m) ~& d  c7 i) u# \
  1. #返回x(x为弧度)的正切值
    , A3 L4 |5 }$ F2 I- E: E  x
  2. tan(x)/ w; T2 N8 @6 O: M" B0 }3 S
  3. Return the tangent of x (measured in radians).3 k; g2 S' W4 E! O- P2 @
  4. >>> math.tan(math.pi/4)7 X7 U) ?& w; `% o5 J" ^, j6 G
  5. 0.9999999999999999
    & i0 O9 x5 d7 V8 P4 @5 y
  6. >>> math.tan(math.pi/6)
    & T  ~& Q: J) y8 Z: r! l
  7. 0.5773502691896257
    $ p3 Z; W' r3 }' h4 d2 ^
  8. >>> math.tan(math.pi/3)
    % I/ c4 N" r9 N7 ?+ W
  9. 1.7320508075688767
复制代码
6 ]/ J% Q6 X) ]0 Z+ m0 T% ^! F( k
math.degrees(x)  把x从弧度转换成角度" z$ l! r. a3 @8 |
  1. #把x从弧度转换成角度
    / C! J' U4 ^% Z2 w1 d! q1 I1 u7 l
  2. degrees(x)- h; ?, R: @' R& ^
  3. Convert angle x from radians to degrees.
    , q, n6 O6 S+ X. P
  4. , p+ w0 ]% V1 v, e8 `% e9 G6 R
  5. >>> math.degrees(math.pi/4)
    1 y; c$ @5 l$ m3 I6 ?2 _2 w
  6. 45.08 T2 y5 S# {3 U
  7. >>> math.degrees(math.pi)1 b" A" r2 @5 n( \4 k/ O6 w
  8. 180.0& B" J- m. H- N( F
  9. >>> math.degrees(math.pi/6)
    9 P/ |0 y: k7 O1 y& \
  10. 29.999999999999996& `2 `* T! J2 x0 Z, d
  11. >>> math.degrees(math.pi/3)# p# t  H$ i6 g' A0 t0 C$ i
  12. 59.99999999999999
复制代码

  A+ u; \" F+ gmath.radians(x)  把角度x转换成弧度
+ g4 r: G6 @7 }" ?5 n7 v
  1. #把角度x转换成弧度
    ( D( V- b9 I  O6 M
  2. radians(x)
    8 c& e& y; p8 `5 @
  3. Convert angle x from degrees to radians.
    1 Q3 j% E% A2 \/ a! y
  4. >>> math.radians(45)
    2 c4 T, l/ A: ~+ a$ j4 Y) r0 K
  5. 0.7853981633974483
    9 Y# x% }1 S# v2 }+ ~2 n2 y
  6. >>> math.radians(60)
    - q2 E" B% E4 x) D9 [4 n
  7. 1.0471975511965976
复制代码

% ^5 n. o' Y" g% g! }1 O. c- dmath.copysign(x,y)  把y的正负号加到x前面,可以使用0, R$ G% }6 v$ o8 L8 i4 G
  1. #把y的正负号加到x前面,可以使用0
    - A, ^  P" i. U- b, D) m
  2. copysign(x, y)" z7 ^" E/ t" i5 f- a% C! A1 P7 l
  3. Return a float with the magnitude (absolute value) of x but the sign
    6 y4 R, `; V- B) s! A! q
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    % B0 P: A, X( E/ j1 g, H/ t
  5. returns -1.0.1 X& \/ w, |! W8 s
  6. ) W; ?" p. j1 X' n/ t% a
  7. >>> math.copysign(2,3)9 G& w' [' a; Z( c) @( R- |/ T
  8. 2.0- T# O. y7 A# e/ ]7 k, U; a
  9. >>> math.copysign(2,-3)5 \- P# Y3 Q' {
  10. -2.0
    5 y3 P  J" b1 c  f9 e% U  w3 B* G
  11. >>> math.copysign(3,8); v# l: i) p' [6 s) G: A% A: M- D
  12. 3.0
    4 f6 |, U: ?  w: w$ T7 k
  13. >>> math.copysign(3,-8)
    3 D- b1 z# V5 |6 P9 r
  14. -3.0
复制代码

( H3 a$ P" |% [math.exp(x)  返回math.e,也就是2.71828的x次方
1 Z) U! E% d$ \& |7 H! ]  y/ K
  1. #返回math.e,也就是2.71828的x次方
    / r2 q' |: V3 a. p
  2. exp(x)
    $ B% x* w; {' W7 V2 d
  3. Return e raised to the power of x.
    : |% ], B0 R+ H' m
  4. 6 Q/ H( @* l! s$ C
  5. >>> math.exp(1)
    + W. x, G7 g. ]4 I+ y. e7 U
  6. 2.718281828459045. K2 R  L" Q- Q0 k3 q7 L
  7. >>> math.exp(2)
    2 r! k1 P/ s; U0 _! j8 e
  8. 7.38905609893065
    * i1 g* p/ O* T3 F) k
  9. >>> math.exp(3)- [7 K; I) O7 p6 g
  10. 20.085536923187668
复制代码
' ~& u; k+ R, X& _$ w
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( f4 n' C4 U" y& Z+ U
  1. #返回math.e的x(其值为2.71828)次方的值减1# G, d$ _- ]( ~
  2. expm1(x)
    6 `" c, M- b6 u  \0 a4 ?9 Y
  3. Return exp(x)-1.) k7 ]- @1 h# w; A- O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.# Y  }, S, c6 P) ]

  5. $ c6 [* }! J1 b0 ~# R6 n! N
  6. >>> math.expm1(1)( X$ b! d8 u" H% I6 ^" Q# n$ u
  7. 1.718281828459045. t* Q. L& h3 d# L8 }& T
  8. >>> math.expm1(2)' B8 J  _7 Y6 W& V
  9. 6.38905609893065
    $ E- N4 J7 d) C! w$ ]
  10. >>> math.expm1(3), h7 G* x! @* R( g' }( G5 J$ b
  11. 19.085536923187668
复制代码

8 c- _  W0 }5 r( ^$ G7 Z7 F& rmath.fabs(x)  返回x的绝对值
. h5 Z6 e9 @" v
  1. #返回x的绝对值/ Y0 ]( ]9 _# B  ?
  2. fabs(x)
    # i" i5 P6 b6 R3 V& L
  3. Return the absolute value of the float x.( O2 Q* B8 Z) v: o9 j# J

  4. 1 I0 B: _9 R7 ~9 }9 y
  5. >>> math.fabs(-0.003)
    6 e# f5 R0 z4 ]
  6. 0.003
    * e! T8 f* x$ `. ?; H" ^. D
  7. >>> math.fabs(-110)
    & j0 Z. G9 l8 E' M$ C
  8. 110.0
    . s6 |6 K* r. A; t
  9. >>> math.fabs(100)- T: w4 e1 ]6 ?
  10. 100.0
复制代码
! F& d3 _* a1 n# d) X
math.factorial(x)  取x的阶乘的值
. H3 x% q/ o7 p
  1. #取x的阶乘的值
      _8 _/ E, a; u
  2. factorial(x) -> Integral# M; O! B* O' s( T
  3. Find x!. Raise a ValueError if x is negative or non-integral.3 z/ n) Q% L$ s( @
  4. >>> math.factorial(1)
    # a3 s  Y( \! Z  B6 p
  5. 1
    3 Q" f' ?6 ?3 O( S3 U. O
  6. >>> math.factorial(2)
    ; w, M6 S  A# d: h0 f; q3 @
  7. 22 `; O4 A# a  v( ?/ U  p& n
  8. >>> math.factorial(3)( M3 C6 L5 h& J, B4 O5 J
  9. 6
    1 w0 q) S& x( r, r, i. Q
  10. >>> math.factorial(5)
    & j1 F! q1 i6 ?: j
  11. 120. o) L. y# s9 W" Z( }3 m
  12. >>> math.factorial(10)
    0 b: c" V# |7 Y/ x/ \
  13. 3628800
复制代码

$ r% O. O" U0 U% `2 bmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
1 @6 _( }" o' q
  1. #得到x/y的余数,其值是一个浮点数
    * {) Y' |; Y7 o/ d4 x  y
  2. fmod(x, y)! l) L: ?2 W3 O7 }: d
  3. Return fmod(x, y), according to platform C.  x % y may differ.. @% Y; [5 d& n/ y0 T
  4. >>> math.fmod(20,3)8 P+ f9 J& r+ Q* u% m! C8 N/ M
  5. 2.0' x3 P* m2 P# l/ m; F, ~2 E
  6. >>> math.fmod(20,7)
    # [: X, d9 Z* \' o
  7. 6.0
复制代码
8 y0 O# A. z) e, z8 y/ l
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; N0 z! Q; i! a& T3 r3 [( S
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ( e! P& R8 L* u: a3 f
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    * l2 U5 D4 |( r7 w( ]
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ; h) q0 k: a+ T" ~. z
  4. frexp(x)
    + x  ^7 s. i1 D/ A$ W: X3 D) m
  5. Return the mantissa and exponent of x, as pair (m, e).
    ( Z/ z9 ^# f8 B& N, F
  6. m is a float and e is an int, such that x = m * 2.**e.
    " f9 k! u5 g7 j
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    ! ^5 F6 D- [& }2 x: c
  8. >>> math.frexp(10). ]  N4 y' q2 v5 `( `, ^
  9. (0.625, 4)( N6 u% N; ^. a; h( F
  10. >>> math.frexp(75)
    8 }8 W" A. F  _- p
  11. (0.5859375, 7)2 `  U6 n1 W, F" G/ A5 l: n
  12. >>> math.frexp(-40)
    ! E4 G: P! w% z3 V% i8 |; Q9 L
  13. (-0.625, 6)
    8 \) r$ i6 L6 ?# ]' o; \9 R: n
  14. >>> math.frexp(-100)
    9 `% a  |" S- I5 W$ X) q
  15. (-0.78125, 7); a1 U% o) p  z0 U0 o. C0 Y- I3 c
  16. >>> math.frexp(100)
    8 X; L1 L% t- H  g% ~
  17. (0.78125, 7)
复制代码

$ h5 K; T$ F7 p$ c3 kmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
' B# o: z8 e) i2 t0 |; J  r, k
  1. #对迭代器里的每个元素进行求和操作
    5 Y. A1 K. Q: Y: h
  2. fsum(iterable)& |8 P2 a( D6 C$ ]  w. n
  3. Return an accurate floating point sum of values in the iterable.
    4 d; ]% |( T* ~9 H5 n
  4. Assumes IEEE-754 floating point arithmetic.
    4 X& _0 [" \+ Y: j# c
  5. >>> math.fsum([1,2,3,4])' Z5 P* ^; z& z# {# f( o
  6. 10.0
    + O1 W( d5 w6 L6 l1 T
  7. >>> math.fsum((1,2,3,4))
    % K# B. [8 Z0 A5 K1 {
  8. 10.0
    0 v3 W) n# s% H- z) r% t3 |. `  }
  9. >>> math.fsum((-1,-2,-3,-4))
    " D: N9 L, _5 p( J" t3 m
  10. -10.0) V3 r- H1 U# d7 g3 h
  11. >>> math.fsum([-1,-2,-3,-4])! n5 P$ l9 A( D7 n( H1 ~2 \
  12. -10.0
复制代码
# K4 n4 `9 v# [2 C
math.gcd(x,y)  返回x和y的最大公约数. ^, W: v/ s6 Z
  1. #返回x和y的最大公约数
    . ?2 o2 ]9 `6 u1 g
  2. gcd(x, y) -> int0 M  ], e" j% M# G4 n
  3. greatest common divisor of x and y1 m) D( z( ~( o& R  o5 x- A: s
  4. >>> math.gcd(8,6)) {6 q) R4 W% W+ p4 s3 \
  5. 2/ T) V! F$ X6 @# R6 ]1 u+ u
  6. >>> math.gcd(40,20)
    8 q9 h) n7 u5 A$ S+ H* A# r
  7. 200 T2 g, X# q: Z
  8. >>> math.gcd(8,12)- H) M' |& a; q+ h5 W: k
  9. 4
复制代码
& n- ?" g+ {7 m8 O$ \
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False; M& m8 q# v3 f1 \
  1. #得到(x**2+y**2),平方的值! i+ v% [" S5 `5 s5 L" @) G5 S9 Q
  2. hypot(x, y)
    - B$ P* ~  a- s# a" p) b
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ! {. n! R$ p" A" U
  4. >>> math.hypot(3,4)# |) [* u! @3 U& ~- K: s# \/ U9 v  o9 O
  5. 5.0
    / N2 H6 F, V: U( q* b0 N, u
  6. >>> math.hypot(6,8)
    , ]& i, [0 H4 [' z9 A7 ]) z, F
  7. 10.0
复制代码
! k5 g( T+ h; X, o1 b0 A6 R7 L. }; @
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
- l( Q) J+ U* _6 C# I, ]' G( Z$ ?
  1. #如果x是不是无穷大的数字,则返回True,否则返回False7 b3 D6 @. T6 U+ c! O" U
  2. isfinite(x) -> bool9 y. V. U. W% v1 g+ s0 R
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ) Q& \9 K$ S/ P) O
  4. >>> math.isfinite(100)
    & r4 A0 }9 k7 ?+ r0 r9 U5 k
  5. True3 j: x+ N/ }, R. T9 w
  6. >>> math.isfinite(0)! m" D7 ~( b6 }9 q1 O
  7. True. I: O/ H# C5 {: e& m6 j/ h3 p- h
  8. >>> math.isfinite(0.1)
    ) i5 J$ H# M3 j& S8 B
  9. True( m  t  Y& o3 E0 K. E2 Q/ ^, E
  10. >>> math.isfinite("a")) ^& `8 o; Z* u8 \
  11. >>> math.isfinite(0.0001)! \$ W$ ]2 P  \+ I: u" B0 m
  12. True
复制代码

% k" D& e2 \  [1 f9 _* s4 Bmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 I7 h& \7 {; C( }5 H+ L+ d
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    + Y5 U5 `9 ^* y$ x  k
  2. isinf(x) -> bool6 e! w* c( Q0 y  O% r% C
  3. Return True if x is a positive or negative infinity, and False otherwise.
    8 \# e  u( C6 V1 t
  4. >>> math.isinf(234)
    & C. _0 F! r  p& f, D# Q
  5. False
    * w! W% t8 N$ ~* q3 M' |4 I
  6. >>> math.isinf(0.1)
    ( H3 Q9 [* \* t7 [& L
  7. False
复制代码
0 \9 X; V  @/ ?; M* }/ T
math.isnan(x)  如果x不是数字True,否则返回False2 q' f7 Q4 u! r# [9 i* q" G
  1. #如果x不是数字True,否则返回False
    * l' t: O9 a9 ~9 T' P! t& k
  2. isnan(x) -> bool- H* d/ _. a5 {2 _, `& Z
  3. Return True if x is a NaN (not a number), and False otherwise.
    , ~9 O& \" a8 [$ T; T: X5 A
  4. >>> math.isnan(23)0 X. k7 H7 `) N3 R" u0 f5 B
  5. False
    : t9 D6 N! }' s9 K8 X& a
  6. >>> math.isnan(0.01)
      @# i) v" Y% g
  7. False
复制代码
; u9 i0 J0 S; Q1 a
math.ldexp(x,i)  返回x*(2**i)的值
1 {) W1 c" I# _* F9 G
  1. #返回x*(2**i)的值6 m) c" q1 E/ ?% {* e
  2. ldexp(x, i)
    5 n% K6 q% [' L% }% f3 i! ^
  3. Return x * (2**i).) c! t) j3 F# G+ r6 H
  4. >>> math.ldexp(5,5)( B: F6 Y0 \) H3 K
  5. 160.0
    6 G) {5 d- R2 u5 K
  6. >>> math.ldexp(3,5)2 w* E% @+ D, {
  7. 96.0
复制代码

! H8 \. ?- A! f( l' Zmath.log10(x)  返回x的以10为底的对数
1 }' f  o( \5 l" T
  1. #返回x的以10为底的对数
    ) F6 q2 |2 i4 Q: {1 ^  z
  2. log10(x)3 ]- a( `- W1 Z2 q; v
  3. Return the base 10 logarithm of x.. H, ]* ~0 t# e8 b: r! D
  4. >>> math.log10(10)
    4 L5 Y, g, L8 K5 E% `! k
  5. 1.0+ J! _; k& v! o4 j, n' {2 \2 @8 h( j
  6. >>> math.log10(100)
    / b5 @# C5 S. H3 I5 {: ^! K/ b8 w7 }
  7. 2.0
    " T! @+ U* S6 C& E6 |: ^
  8. #即10的1.3次方的结果为20
    * E% L/ k5 v9 o2 ~+ r9 ^( }& K
  9. >>> math.log10(20), c4 I" `+ `$ Z
  10. 1.3010299956639813
复制代码

! K$ e) \* X  A' V5 o+ V& mmath.log1p(x)  返回x+1的自然对数(基数为e)的值
* e' S2 W& X! g- e$ i0 X
  1. #返回x+1的自然对数(基数为e)的值+ |/ s9 k* a0 w0 B, V' m# v+ D
  2. log1p(x)# E2 _  e! w( x2 |5 d+ u$ R# L
  3. Return the natural logarithm of 1+x (base e).
    5 c) S, `1 c+ T+ {& ^0 z  e
  4. The result is computed in a way which is accurate for x near zero.; ?6 }  C" ?/ y; c/ K
  5. >>> math.log(10)
    3 z) j- C* ?' p) h+ A3 V  f
  6. 2.3025850929940466 @) ?) \# b: t0 o4 j
  7. >>> math.log1p(10)* q2 n1 f+ H5 E8 V- W! V
  8. 2.3978952727983707; t# e4 j( K, x- z, k
  9. >>> math.log(11)
    0 X6 ^3 L& B+ _/ I5 A- v
  10. 2.3978952727983707
复制代码

- n9 [# T5 S6 wmath.log2(x)  返回x的基2对数: J1 w5 R( _- \0 n6 F
  1. #返回x的基2对数5 J- c' M, f" w: w
  2. log2(x)
    ; P  I( q+ Q1 S$ r! C# |9 G
  3. Return the base 2 logarithm of x.  |; m8 u- g' i9 M& G
  4. >>> math.log2(32)# ]7 C- Z2 h* ^: A! W
  5. 5.0
    0 {' y. P4 }* w8 B
  6. >>> math.log2(20)
    3 j! f2 D% C) U2 I: X+ q
  7. 4.321928094887363
    2 b+ Z, [7 G3 U* l
  8. >>> math.log2(16)+ Q9 |5 _) X) u! g9 c. y5 V9 Q
  9. 4.0
复制代码

$ W$ m: P, G7 K% v7 r4 Q8 Umath.modf(x)  返回由x的小数部分和整数部分组成的元组8 C1 M$ F+ U6 n9 Z
  1. #返回由x的小数部分和整数部分组成的元组4 p7 ^% k! j% g5 L
  2. modf(x). B2 K& \9 t7 i- q( ~
  3. Return the fractional and integer parts of x.  Both results carry the sign
    + o# s7 E9 D6 U( x& h  W6 B- g
  4. of x and are floats./ R; ]7 e3 n# f) y7 r3 z
  5. >>> math.modf(math.pi)) w! {" d* E% s6 j" l0 U7 g4 q
  6. (0.14159265358979312, 3.0)
    * {2 V" J* r$ K3 X3 p' j# s
  7. >>> math.modf(12.34)4 Z! q, r% h! Z" k8 }  z
  8. (0.33999999999999986, 12.0)
复制代码
! b9 H; ^/ K4 \
math.sqrt(x)  求x的平方根8 l' Y- U$ p7 q  O8 N2 P2 a0 O$ `- i
  1. #求x的平方根
    ) H& x5 m1 @/ }: y$ X. ~$ {+ C
  2. sqrt(x)2 \+ _) s2 \4 D) T4 B% q" I) }1 m
  3. Return the square root of x.
    & `8 u5 a4 L! X& u: N4 L3 }
  4. >>> math.sqrt(100)  F0 f/ m! o8 J" y# X2 p1 {/ g
  5. 10.0
    5 R, e6 s' m1 l7 d. Y
  6. >>> math.sqrt(16)" E% d/ v# o7 H7 o
  7. 4.0
    3 u7 ?; v& b% Z: R+ }
  8. >>> math.sqrt(20)+ D& O: B7 R( j- Q) X! h+ ~
  9. 4.47213595499958
复制代码
( X' n" S7 w! z) P. R
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    5 T3 U7 s' Z7 [% |. x+ t3 N
  2. trunc(x:Real) -> Integral7 e1 i7 P/ g) B, {5 ~8 H& d
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: r/ }) ~# E! R- X. ~/ c4 ~
  4. >>> math.trunc(6.789)6 h8 S# }5 f- Z4 l$ W; h
  5. 6
    5 |  m: i3 X7 ^* u
  6. >>> math.trunc(math.pi)
    + w3 T. y$ U% n) R! s
  7. 3
    " E7 K" S: ]6 _5 m
  8. >>> math.trunc(2.567)  h; R! \# f' |4 Y
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-25 23:53 , Processed in 0.068410 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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