新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
  o, T) ^$ Z- v6 q' c9 M
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ z7 F5 S+ b  B4 A( x" O/ B( V
6 q4 c" ~; I" Q6 V2 \
方法11 D" c, g5 |% r6 B" Q
  1. >>> import math( f# j; o5 F3 C+ ]' q, C: l: t0 B
  2. >>> math.sqrt(9)
      f3 i5 y- e* B
  3. 3.0
复制代码
方法2
$ B, ]# ~) v5 L( h" a5 I3 a
  1. >>> from math import sqrt2 G' L/ y' a3 D+ l9 ~
  2. >>> sqrt(9)$ D( U) l& s! A; s' o& m
  3. 3.0
复制代码

: T$ ~7 k# i* M

  E0 A8 ^( |, s% l
math.e  表示一个常量
. p* P9 }% X3 b5 m- a
  1. #表示一个常量# W/ b. ]5 Q$ S) w+ q
  2. >>> math.e
    9 R) m# w0 I/ p' a9 g: z8 t
  3. 2.718281828459045
复制代码

% d7 v+ y1 \2 l; ~: W! qmath.pi  
数字常量,圆周率
0 [+ i4 I  j$ V
  1. #数字常量,圆周率$ y/ i* d  @2 Z
  2. >>> print(math.pi)/ G2 L* Y% _6 @* j  N
  3. 3.141592653589793
复制代码
9 u# d7 c( [3 ^# @7 ?# g
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
, q  b- b" b4 A# d9 r5 \
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    , j) N; r4 P+ ]- G  z9 {" n; v' R' q
  2. ceil(x)0 O! @5 v8 r# W- n. \, u4 z
  3. Return the ceiling of x as an int.
    : H8 y- z5 ^1 c) [8 y& [/ R! n* K6 s- N" {9 q
  4. This is the smallest integral value >= x.
    ! O7 t% G5 m! |+ y
  5. 4 M7 s+ x2 B5 z4 Z" [% g5 v8 I
  6. >>> math.ceil(4.01)
    " r8 j+ d  e+ s8 e/ @
  7. 56 y1 \* c! _% |  R# d0 Y8 E
  8. >>> math.ceil(4.99)6 t  ?% r1 a4 z0 _" Z
  9. 5
    & `& g% a$ {, |
  10. >>> math.ceil(-3.99)
    # p/ k) G& G! D0 F
  11. -3. z: p/ K; t* }0 ]( o
  12. >>> math.ceil(-3.01)
    2 l3 K  i1 ?* O) m6 s3 b) t  F) B
  13. -3
复制代码

7 S0 j7 m: L/ o& jmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 z  x3 \7 G' ~3 Q3 W9 w6 |
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ' X% t+ o  w  z  U3 V
  2. floor(x), ~8 H. x  {/ G' o
  3. Return the floor of x as an int.1 W' p/ V$ Z4 p5 k
  4. This is the largest integral value <= x.0 e6 \+ ?7 W+ t
  5. >>> math.floor(4.1)
    ) `9 f8 f2 Q  D& t
  6. 4) M5 P- z5 Z. ~4 E( \
  7. >>> math.floor(4.999). t2 }- @2 H6 ]( U( z) V
  8. 4: \: S0 ~+ q3 g
  9. >>> math.floor(-4.999)
    & r9 a' z* U% E' `  k
  10. -5
    & h6 @) q, x/ m0 W; y' p/ m
  11. >>> math.floor(-4.01)2 e! A# d8 m! O, f, T. u8 t
  12. -5
复制代码

. F+ U" e) f# `* j* Bmath.pow(x,y)  返回x的y次方,即x**y
0 M  Q3 d# A0 K1 |/ m" A5 _
  1. #返回x的y次方,即x**y; R1 F, |' }  P8 L1 }
  2. pow(x, y)  c" ~3 Y# @& Q: Y
  3. Return x**y (x to the power of y).8 O4 c$ Y# C8 A( D* Q
  4. >>> math.pow(3,4)
    7 t. V  A7 i2 u9 v$ @" \
  5. 81.01 ]8 x6 V5 l( \) i+ b" c) S
  6. >>> * g3 s) l' B0 k. ]# n9 j
  7. >>> math.pow(2,7)9 `% n4 ^, d0 j% f7 y7 |
  8. 128.0
复制代码
; @/ q8 f- g. |
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
) K7 S: M4 O. u$ E5 @% w0 t
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). M/ j9 B% W8 f7 z
  2. log(x[, base])# [- Y7 {) W7 p; {) G
  3. Return the logarithm of x to the given base.
    1 l$ r: V6 F9 |( e
  4. If the base not specified, returns the natural logarithm (base e) of x.5 Q- b3 O8 J; Z0 V
  5. >>> math.log(10)1 |8 `& C! E0 \" p
  6. 2.3025850929940460 P5 k3 U* D$ H7 T9 h
  7. >>> math.log(11). Z$ y1 |2 l3 \8 s+ u
  8. 2.39789527279837077 t6 B# ?( i, X" ?0 t0 [
  9. >>> math.log(20); n5 L0 u& B: E9 {$ d$ M1 p* h
  10. 2.995732273553991
复制代码

  K9 |- Z; z8 \( Emath.sin(x)  求x(x为弧度)的正弦值
2 g! w5 T# D, i( b" I/ [
  1. #求x(x为弧度)的正弦值" l3 I; H% _$ n9 V, e/ K
  2. sin(x)
    , W: B1 a0 V3 g
  3. Return the sine of x (measured in radians).& U+ N0 k; Y# r8 W4 P
  4. >>> math.sin(math.pi/4)
    - x0 B2 a0 Y$ ]6 S/ S
  5. 0.7071067811865475
      L& j- V  \0 C8 m* y" {( o
  6. >>> math.sin(math.pi/2)1 M) X5 _$ O: ^8 @3 M( d! t
  7. 1.0
    9 b! ^: `! p- B: i5 Q
  8. >>> math.sin(math.pi/3)& v- k7 b3 J  e7 X
  9. 0.8660254037844386
复制代码

2 d4 N6 o; O; Y/ |; o9 m' I# w0 Qmath.cos(x)  求x的余弦,x必须是弧度2 V& f: k' J" ^/ t" D& j
  1. #求x的余弦,x必须是弧度
    . F' U' O* h& {1 J
  2. cos(x)
    3 B9 ^' e* ^; H7 g; H  M& i
  3. Return the cosine of x (measured in radians).4 y- C5 \# \8 W  B
  4. #math.pi/4表示弧度,转换成角度为45度: a0 j, v2 n1 n1 g9 ~2 |
  5. >>> math.cos(math.pi/4)
    " }6 Y" s- B9 t
  6. 0.7071067811865476$ X% X: g. A, y+ O7 |
  7. math.pi/3表示弧度,转换成角度为60度
    ; E  n0 @( `( K: Q8 A; m+ j
  8. >>> math.cos(math.pi/3)  R  G) N" t7 ?, J: [
  9. 0.50000000000000016 a. J+ y+ R! Z' j& [, ^, Y. y
  10. math.pi/6表示弧度,转换成角度为30度
    7 `$ V, P$ l% U- ?  p
  11. >>> math.cos(math.pi/6)
    ! h4 p: ^3 ]3 Z5 ~" L
  12. 0.8660254037844387
复制代码
+ j1 {$ w! ~7 e- T- s
math.tan(x)  返回x(x为弧度)的正切值
- e! h2 p, u; C% J2 ^1 B
  1. #返回x(x为弧度)的正切值; P. \& P' K: D) F) v3 g6 C
  2. tan(x)0 q) E+ `# e* _! |6 \: {  [
  3. Return the tangent of x (measured in radians).
    - e2 y3 j: m  D! \" m; _9 o
  4. >>> math.tan(math.pi/4)
      \* b2 l  _2 z& |/ I& @
  5. 0.9999999999999999
    & @* a3 [' O4 X3 f' i
  6. >>> math.tan(math.pi/6)" C; Z% P  b7 L2 D  I( m5 f
  7. 0.5773502691896257# Z8 g/ B$ v% R% }( q
  8. >>> math.tan(math.pi/3); ~9 Q" z$ ]$ M2 N3 J
  9. 1.7320508075688767
复制代码

1 n) ?! k+ W' Wmath.degrees(x)  把x从弧度转换成角度
% y6 ^/ N8 Z. V6 n" C; l* C2 w9 k
  1. #把x从弧度转换成角度0 K( `  @- N- f. _( h+ @6 Y
  2. degrees(x)4 M+ E$ S  F# S2 @( _* w
  3. Convert angle x from radians to degrees.* D2 N# w6 \3 v8 U3 @& u! q4 w
  4. : f& L. L" @5 [
  5. >>> math.degrees(math.pi/4)" P" X, S" G" C0 z( F6 X
  6. 45.0* c' m' H4 N* E8 ^# @& ^; ?% ~
  7. >>> math.degrees(math.pi)
    ' R, @0 n& o+ Y" ^2 w+ T' u- Q
  8. 180.0
    ! v+ d5 l( v0 o3 h9 x4 Q# H3 I
  9. >>> math.degrees(math.pi/6)1 i! @/ j6 }6 K
  10. 29.999999999999996
    8 x: ~$ D7 k. C4 @: H! X, o7 Z
  11. >>> math.degrees(math.pi/3)
    9 }! j) z8 R( {+ B
  12. 59.99999999999999
复制代码
# C( h3 E7 t$ v7 O
math.radians(x)  把角度x转换成弧度8 W  @3 }5 ^4 |5 s. }5 k
  1. #把角度x转换成弧度
    3 P! G) v# r- ?9 v5 p" ]
  2. radians(x)4 L% Y( E3 R9 }; D) o
  3. Convert angle x from degrees to radians.
    + D: B. A: S9 j' W% h0 B
  4. >>> math.radians(45)
    ! c! R4 p0 k% q: \  E9 B
  5. 0.78539816339744834 D' w5 R4 b- K6 ~
  6. >>> math.radians(60)
    3 l8 `1 M, ]' e7 x
  7. 1.0471975511965976
复制代码

% S3 Y' _) w; a  Tmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
' Y, |1 @) `7 k; Y
  1. #把y的正负号加到x前面,可以使用0
    ! ?6 f- `% F0 N* g, X) E
  2. copysign(x, y)
    , M  ]  o: h) ^8 K6 [8 N
  3. Return a float with the magnitude (absolute value) of x but the sign " n6 y) d5 T5 C" S1 |# y
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    4 C6 b: \. @3 J3 F- J
  5. returns -1.0.$ h) W6 `+ n1 D2 B6 u( Z6 _" G
  6. , j0 r* s- e7 q, b5 g. v
  7. >>> math.copysign(2,3)
    4 x+ }" C3 {) ?; k' _7 h
  8. 2.0- G( k7 \& H/ S" n; ]8 p2 [$ I0 P
  9. >>> math.copysign(2,-3)/ N, R' S; H' t6 m
  10. -2.0' `4 K- M: ^( p8 Q
  11. >>> math.copysign(3,8)
    & L) ~1 P! n- P1 ~
  12. 3.00 V: K" f& O& a* T- x
  13. >>> math.copysign(3,-8)
    6 ]  J( y$ \' x& g. `
  14. -3.0
复制代码

1 B# B3 s& I: [, U$ r% U1 p3 t" Hmath.exp(x)  返回math.e,也就是2.71828的x次方3 K2 T. n+ ?( l- e8 r" N" P8 s$ x
  1. #返回math.e,也就是2.71828的x次方+ s6 K3 W3 w8 i/ l
  2. exp(x)2 ]3 S8 G4 C0 [- ?3 s6 V
  3. Return e raised to the power of x.5 [& R1 V9 x0 X2 y/ v

  4. ( H, E  H/ Z) p
  5. >>> math.exp(1)8 o3 F8 Q2 b/ w. q) i% {" f6 H  t$ R
  6. 2.718281828459045/ z* O8 I& f& t# H, e
  7. >>> math.exp(2)
    , M% z% x8 ]. g% S9 A, m+ o
  8. 7.38905609893065/ ]  Q3 t$ e% M1 e% a
  9. >>> math.exp(3)
    7 w8 I: c3 O7 O% r
  10. 20.085536923187668
复制代码
5 o( O4 {7 _/ C. d/ L' m6 a
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1) ?# M% W2 R! Z) K" s
  1. #返回math.e的x(其值为2.71828)次方的值减1
    3 z3 u& c6 y, K$ u4 G) {
  2. expm1(x)- t. t$ X  y; e: i; ]. z7 M2 U) ?8 ]
  3. Return exp(x)-1.- [2 u! X- e- ~. y: y4 I3 \- i3 s
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ! N4 a/ U, T7 @' E( b* N& l" f: U' Z
  5. 7 a5 p" s" Q8 r6 S  ?4 u
  6. >>> math.expm1(1); @$ l( F6 T' q) ~( `
  7. 1.718281828459045% J! ~& K" M/ f3 j/ s/ X6 O9 u
  8. >>> math.expm1(2)
    0 y. q+ A3 N7 W4 I/ K  ~  K0 o7 L
  9. 6.38905609893065
    2 g. P. a( ~5 W! x; w
  10. >>> math.expm1(3)
    & D$ ~. ]8 Z+ G! |6 a3 N) e
  11. 19.085536923187668
复制代码
" I" L1 v: n  M1 ?) x1 j: G
math.fabs(x)  返回x的绝对值
7 i$ [+ A; u. w
  1. #返回x的绝对值
    3 R, |. A7 t" R. ~
  2. fabs(x)6 m+ v9 b5 M% f6 ?2 k" M( `
  3. Return the absolute value of the float x.8 b8 L5 q0 p7 C9 J, q

  4. " G5 Z! ^  k9 s* o7 @* s, O. x6 i
  5. >>> math.fabs(-0.003). P5 t/ N. ~' E) J8 z' `
  6. 0.003! ~0 y1 V+ {& H4 X4 i  K' r! W
  7. >>> math.fabs(-110)
    ' e1 }( h2 X/ d7 P9 e1 w; y
  8. 110.0, t+ k$ R! I2 {
  9. >>> math.fabs(100)# ]: P0 u: n8 e; ]9 T0 ~
  10. 100.0
复制代码
( Z  H  S6 k" L6 k4 ?& [
math.factorial(x)  取x的阶乘的值
' S) A1 F- n5 _( ]8 h+ e6 ~9 S
  1. #取x的阶乘的值# s9 R  {% l7 h! I
  2. factorial(x) -> Integral, v% f2 t2 k, c9 ?) z9 ^9 I
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    1 t* o; E* r' C; m( ^" J* E: l
  4. >>> math.factorial(1)
    8 [3 V- D# O- p7 B2 V" ?/ S
  5. 1
    ; \" t# p0 o8 q5 F: ^4 [9 j
  6. >>> math.factorial(2)
    / I0 L4 p0 E- M( Q3 z! G1 o
  7. 2$ T2 o! p4 S0 }) \% \8 t8 h" P
  8. >>> math.factorial(3)
    - Q# \8 I! y$ O2 P" u- ~% ]- Q
  9. 6
      m# e- S$ B6 z, B
  10. >>> math.factorial(5)
    & t2 X1 Q- x3 \" Z. U
  11. 120
    ) k7 E1 G+ x: p+ I, [) D& d# `' e
  12. >>> math.factorial(10)
    % C2 g1 S' D+ b
  13. 3628800
复制代码
% I/ J- _& `( D# A
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
! b& d' r, e7 j9 i
  1. #得到x/y的余数,其值是一个浮点数$ u7 d8 G6 P7 `. l7 U
  2. fmod(x, y)
    6 D: E' b9 G* y8 K: B- V
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + Q+ B8 v5 {, R+ G+ I) i' M' U9 _4 _
  4. >>> math.fmod(20,3)
    7 d0 @8 S/ ?4 A, N# u  L
  5. 2.0
    ! _: g5 P% r  y# B* O
  6. >>> math.fmod(20,7)
    3 Y* C$ a4 D1 h6 s- p! H
  7. 6.0
复制代码
( S$ h9 y% e" \+ ^1 a8 s
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) v6 U5 Y) e  ~$ N. `- w
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; |4 D, Q/ Y6 X( x% f
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    4 i$ q  c1 Z9 c$ r# j0 @( L9 K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    & B9 k: ~& k" h# q0 J( E- O
  4. frexp(x)
    ) K* v- w, X" t; P7 R" s; F: t
  5. Return the mantissa and exponent of x, as pair (m, e).- B+ Z8 Y- Q2 a- b: y4 R
  6. m is a float and e is an int, such that x = m * 2.**e.% g3 c7 S; l2 c+ |4 }; |! l5 e
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.( x9 e0 w4 R7 T0 w, }5 W* i3 F( {; G
  8. >>> math.frexp(10)
    9 }5 v( }! E* m; `; K# D% w
  9. (0.625, 4)$ d& z' E6 R1 E- L" A& W% Z
  10. >>> math.frexp(75)3 R$ L  v0 ~; ^# D
  11. (0.5859375, 7)6 ^' X+ C, W( L  _2 f* E
  12. >>> math.frexp(-40)6 p6 t3 \* \/ n0 f& t1 L5 b
  13. (-0.625, 6)  ]9 s6 N" g; i- S4 C  a+ }
  14. >>> math.frexp(-100): j. L. M6 `, P( `4 o
  15. (-0.78125, 7)/ [# M  h8 n* I4 }' {& v
  16. >>> math.frexp(100)7 Z2 W( j/ m* o) G
  17. (0.78125, 7)
复制代码
# t: ]) ^. ]' h! |
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, F& F: d9 g% G! V
  1. #对迭代器里的每个元素进行求和操作; n- x: o, a9 f# J7 I3 U# y4 `
  2. fsum(iterable)
    ! Y7 w3 E+ F/ b$ F$ [% r* x
  3. Return an accurate floating point sum of values in the iterable., N% P  |, A) `( |1 l7 o
  4. Assumes IEEE-754 floating point arithmetic.6 L' T& r5 {; w+ y/ l) S( S' d. ~6 n6 f
  5. >>> math.fsum([1,2,3,4])9 q6 E4 }6 V5 k; B+ {9 |0 b' U; \
  6. 10.0
    , ?! l9 l/ y* r
  7. >>> math.fsum((1,2,3,4))
    4 l2 @; W0 K/ U) ?$ c# s  f; O4 [
  8. 10.08 y+ P0 S, Y/ M# a) |7 O
  9. >>> math.fsum((-1,-2,-3,-4))( {% L8 N5 ~4 N( N% [, [7 P4 i& W5 e
  10. -10.0
    " m9 ^/ e( l6 U8 p3 N5 e# i
  11. >>> math.fsum([-1,-2,-3,-4])8 N2 C3 B! |+ i- D% l
  12. -10.0
复制代码
; [4 Y5 ?; V7 b9 `
math.gcd(x,y)  返回x和y的最大公约数
6 }; D# ^1 n( x2 c# J
  1. #返回x和y的最大公约数
    " n; T& B$ U4 F7 Q! R
  2. gcd(x, y) -> int- h9 b3 V; P  S- v% l2 d
  3. greatest common divisor of x and y$ O3 X  W, |# {) V5 z
  4. >>> math.gcd(8,6)* ?9 j- n! Q) R; [( X* x. t- q
  5. 2
    + ?0 v2 d, d/ }, v
  6. >>> math.gcd(40,20)
    # }7 K1 Q5 p: A1 R
  7. 20
    7 o. E' A( J! F) _* `5 p
  8. >>> math.gcd(8,12)- ^; @. R, b8 s7 T
  9. 4
复制代码

' p- R& P! M( Y6 Tmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
# o7 ~$ F1 x: F3 I9 G: T
  1. #得到(x**2+y**2),平方的值
    + {' b1 t# o7 F- l. S2 Y# z3 w
  2. hypot(x, y)) x) k8 M0 \' I0 N- Y5 V+ m! |
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ( O9 Q" Y* E1 ]  R8 k4 K* J* ~
  4. >>> math.hypot(3,4)
    $ s& Z( ~. U2 E! ^2 h4 A/ Y- T* A- Y/ m
  5. 5.07 n! z( G, f- r5 C! O+ s0 L
  6. >>> math.hypot(6,8)% Z( F+ Z) O+ L2 B
  7. 10.0
复制代码

, ~6 j; r( F0 cmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
1 D) S8 {  Z5 i2 X' a
  1. #如果x是不是无穷大的数字,则返回True,否则返回False8 l- R+ K! J/ k9 m- i. o3 U7 e
  2. isfinite(x) -> bool
    & F$ g+ T) |/ _; ~
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.; e5 E9 Z. O  a2 M* D4 d; H
  4. >>> math.isfinite(100)
    " m2 m9 T1 W1 r3 T
  5. True
    % {8 X9 e1 m6 ^' t% e7 S) F5 D
  6. >>> math.isfinite(0)
    . g' [6 p& p+ z  _5 C- y
  7. True3 e1 f* ^: R* z6 ^
  8. >>> math.isfinite(0.1)$ U: ~6 F: w% Y& q7 C# j6 E
  9. True
    9 P2 s3 p0 E" Z0 u
  10. >>> math.isfinite("a")
    / W" k1 T, h$ Y0 \; V2 O4 r' w) i
  11. >>> math.isfinite(0.0001)" l: K0 z" g/ u; J4 N; M3 m
  12. True
复制代码
, p7 b/ O/ z7 _2 Z, i
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
6 P4 p! T% t) ^* W# q- \
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    3 b; `7 [' H* p! @" w  }- z6 Q8 y; }& _
  2. isinf(x) -> bool
    ! {; ?$ ^& P  ~# [8 _- W- I5 _! l- Z
  3. Return True if x is a positive or negative infinity, and False otherwise.
    # r2 x) X) O- `
  4. >>> math.isinf(234); v" s7 [$ H& z9 n0 i
  5. False8 N- k# ]- n3 n7 w2 m" ^
  6. >>> math.isinf(0.1)
    ( G7 G( e9 Q) `5 \" W6 p' o0 A, j9 r
  7. False
复制代码
3 a& A; r2 i' s9 |" Y; P9 U
math.isnan(x)  如果x不是数字True,否则返回False2 n# P4 u7 h. h! G5 g$ k7 q
  1. #如果x不是数字True,否则返回False
    ( v  N5 [  D) N, l
  2. isnan(x) -> bool
    3 L- O' @6 g  [, r, B0 X* |4 j& U
  3. Return True if x is a NaN (not a number), and False otherwise.6 Q6 ^$ b& Z( `7 Y, Y
  4. >>> math.isnan(23)
    3 H) l2 Q6 M! N$ O( V5 _
  5. False' O5 I" ^4 Z: B. ?4 F
  6. >>> math.isnan(0.01)
    % }9 x& M; g* B4 d% D* y7 U
  7. False
复制代码

  b8 J& ~* P# e9 y- smath.ldexp(x,i)  返回x*(2**i)的值
; r6 l6 l7 n0 C* x" o
  1. #返回x*(2**i)的值; A$ {/ o8 m  O
  2. ldexp(x, i)
    % B) O9 n: N9 l# H" L
  3. Return x * (2**i).
    , g7 M0 S2 }3 [
  4. >>> math.ldexp(5,5)0 Z5 C# `$ w2 d2 b0 p
  5. 160.0
    1 ^3 E/ t" |4 k; d: @$ ~0 ]# T
  6. >>> math.ldexp(3,5)
    5 P8 z6 |/ L1 w2 P& J
  7. 96.0
复制代码
! X! B7 d  D& e* a6 }) C
math.log10(x)  返回x的以10为底的对数
2 s  k, u& g' \! [3 e+ V* i
  1. #返回x的以10为底的对数
    / ^6 e  F. p$ m
  2. log10(x): W: P) d; F' H1 v) M% A4 c
  3. Return the base 10 logarithm of x.
    8 O* @3 m5 F3 x
  4. >>> math.log10(10)7 F4 N/ I1 R0 K0 g5 `5 F
  5. 1.0
    0 p3 i1 x- i: Z# D+ q+ E4 d
  6. >>> math.log10(100)5 `8 z. Y3 A* X. V; a3 S+ d
  7. 2.01 O/ F4 e( }, ~  X
  8. #即10的1.3次方的结果为20
    # d; b0 o- T/ }+ }# T5 a' u5 ^
  9. >>> math.log10(20)
    7 m2 d7 J% J+ R4 f; [6 ^1 r" x5 Z) A
  10. 1.3010299956639813
复制代码

) b+ Q* n0 ^8 S9 R3 R# V, q5 tmath.log1p(x)  返回x+1的自然对数(基数为e)的值
. o2 X/ n! Z0 e9 }. H/ ]  ^3 f. l
  1. #返回x+1的自然对数(基数为e)的值
    2 e/ ~8 \; }- ]
  2. log1p(x)
    0 E& @/ P' T4 R
  3. Return the natural logarithm of 1+x (base e).
    . \/ |. {" W; \5 Y0 W+ I2 j
  4. The result is computed in a way which is accurate for x near zero.) W% ^# t* n& T) T  H( u( q9 Y/ Y+ P
  5. >>> math.log(10)1 k) E% t* a2 a( X8 |4 ^
  6. 2.302585092994046
    0 i3 H/ e) i( P3 W& }6 k2 x
  7. >>> math.log1p(10)
    7 V- Z% }8 T5 z3 `8 P1 V
  8. 2.39789527279837072 o, d4 e; s* C9 L8 l
  9. >>> math.log(11): Z& V5 @3 I, p; b' P- e
  10. 2.3978952727983707
复制代码
7 W9 u" N6 K3 g
math.log2(x)  返回x的基2对数, J4 y) q, ?5 u$ C4 Y+ Q4 r( s
  1. #返回x的基2对数
    : B# u3 z: {$ u0 f' }
  2. log2(x)- \4 ~1 ]! A! E( e* M& P
  3. Return the base 2 logarithm of x.7 |5 t! E6 U1 P5 C, k
  4. >>> math.log2(32)
    % ^* f+ J( M) h/ O3 W: P
  5. 5.0
    5 t  o' W, ?: L- C) V% X
  6. >>> math.log2(20)
    - G8 e0 t( f7 g% o
  7. 4.321928094887363% T/ p5 v% V. I6 m1 {- x1 q9 I
  8. >>> math.log2(16)1 N" z: T# J* D, H1 v/ T
  9. 4.0
复制代码

0 J' \! s) M3 }7 ?& ]math.modf(x)  返回由x的小数部分和整数部分组成的元组
5 X6 q2 y" ^$ M2 j3 d( c0 c
  1. #返回由x的小数部分和整数部分组成的元组
    7 z% j4 y* g) k
  2. modf(x)) I1 P$ M* W8 N- e0 c2 _
  3. Return the fractional and integer parts of x.  Both results carry the sign& F9 l' E. `) c* v0 r3 [
  4. of x and are floats.
    2 S3 ]: w) ^6 ^" h& T" U/ M. m: z
  5. >>> math.modf(math.pi)
    3 _+ W6 O2 {, t7 K" d
  6. (0.14159265358979312, 3.0)" b3 z2 i+ B' k
  7. >>> math.modf(12.34)
    4 r9 }. S9 v7 v: j
  8. (0.33999999999999986, 12.0)
复制代码

" L0 b9 S) j# p8 _! Q8 qmath.sqrt(x)  求x的平方根' O- Q9 [5 M7 T/ Y# f
  1. #求x的平方根
    % ]: \- y- D7 l
  2. sqrt(x)4 ~( V# d0 E% j% X5 X" ]1 ?
  3. Return the square root of x.+ G) B" A4 ^; L* r" p& _) ^
  4. >>> math.sqrt(100)0 @; I! f" f# H
  5. 10.0" K  |0 D6 b5 j
  6. >>> math.sqrt(16)) \) H1 i# i$ n: R. ?$ b" s
  7. 4.02 ?3 V3 }1 I9 c/ g  n4 u* Y
  8. >>> math.sqrt(20)
    ) g9 U' K/ _& V8 }  `
  9. 4.47213595499958
复制代码
  {$ `9 S! V$ G( u
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分, m7 V+ Z9 f' E! h7 o, O
  2. trunc(x:Real) -> Integral3 U, f* `8 j5 V( Z
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    0 z5 d6 F' N& ^8 m; q
  4. >>> math.trunc(6.789)
    * @0 |2 O: _, M* r# x2 s
  5. 6
    # P0 o8 U" T! t5 S+ S/ v9 Y5 H. O
  6. >>> math.trunc(math.pi)7 b( e& k) J3 G5 }
  7. 3. w3 f' o5 N+ i7 \+ d, g
  8. >>> math.trunc(2.567)7 @5 ^2 q: y" L. b
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-22 15:26 , Processed in 0.082443 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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