新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

2 T$ g2 c/ x9 g- Q9 j- l【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" d4 E) i5 s( ~# A; q
) A+ s' d/ I. B$ w
方法1+ M. \0 b* P2 S& G9 B2 y* `4 T. j# O
  1. >>> import math, K, @4 k1 q+ M; V' k
  2. >>> math.sqrt(9)0 k4 R2 S5 K2 f3 e7 [
  3. 3.0
复制代码
方法2$ f, i; b1 E8 W; E
  1. >>> from math import sqrt, K& \' F& T& t% K
  2. >>> sqrt(9)
    . N( N9 w9 K, {6 d3 o: g
  3. 3.0
复制代码

3 r& |' o7 g4 w; m3 ]3 P$ W: u
2 Z% o1 |! g) ^! S1 b) S2 w0 X" s8 {
math.e  表示一个常量
( h1 W& T* {; p. p( j) D5 e
  1. #表示一个常量
    ' C' z/ h7 S# t. S. a
  2. >>> math.e# k! J  E2 c" O+ ^
  3. 2.718281828459045
复制代码

. `9 O" K6 N1 m# B/ B; bmath.pi  
数字常量,圆周率
( s( X" o% a4 i
  1. #数字常量,圆周率  [1 p5 ^) l; w) ^  h$ E
  2. >>> print(math.pi)3 ?$ N! m0 [* g( x" `+ ]
  3. 3.141592653589793
复制代码

) P4 z) v9 n" L  d3 Pmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

5 y0 l; a) e8 K. H4 V: X
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ( `$ S! J9 v9 y' \- O) a" {. B5 X
  2. ceil(x). j8 K0 @! Y7 X) D
  3. Return the ceiling of x as an int.; A- u3 a8 J9 Q7 t+ P
  4. This is the smallest integral value >= x.
    4 R) `6 ~$ X0 U( I

  5. 3 }' `; U' C2 t( d* K8 p/ q
  6. >>> math.ceil(4.01)
    " p4 j0 U  X; [' o3 m5 Z% }
  7. 58 S1 _8 R& h! T0 @4 Z3 H
  8. >>> math.ceil(4.99)
    # D7 A! \5 ^3 n3 I
  9. 56 j' z; _! q+ E" G( Q) h8 S3 D0 l
  10. >>> math.ceil(-3.99)$ K& z( `( @* Z
  11. -3
    % f+ J2 z) L0 G
  12. >>> math.ceil(-3.01)
    : G1 N0 }( B  w2 h' K3 \
  13. -3
复制代码

4 `) T- H4 Q$ c; \math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
  B1 {4 t" Z/ E. R$ O! \
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    : `- [# c! I! x' \4 B% @
  2. floor(x)
    1 V% J9 ?* L! q. d: r
  3. Return the floor of x as an int.
    $ b8 k2 a$ H1 _: I
  4. This is the largest integral value <= x.$ k! i7 k/ y( q4 G
  5. >>> math.floor(4.1)
    - x0 a' ]' j7 G( D' `5 h
  6. 4# @/ Z3 ]& h$ x0 e, _. h$ Z! \
  7. >>> math.floor(4.999)$ _4 D' R% C5 W. H; \; z' V
  8. 40 Y: z' d* \  Z, Y+ T
  9. >>> math.floor(-4.999)
    - j8 `2 F/ K: s3 v
  10. -5
    9 ]+ j1 N9 z4 }; `  W( o
  11. >>> math.floor(-4.01)5 w0 |" J0 D6 q* O3 C. b# }
  12. -5
复制代码
% P- d+ p  t2 R% S7 |) E
math.pow(x,y)  返回x的y次方,即x**y
3 Y& J3 f2 z, B, j
  1. #返回x的y次方,即x**y
    . u( k& p) N1 C5 ?0 \* w
  2. pow(x, y)
      f3 |$ G' X$ `- ~6 j5 k
  3. Return x**y (x to the power of y).) m$ _0 ~  |' c1 Q2 e
  4. >>> math.pow(3,4)4 E  W: j4 x! x! D' ~2 w
  5. 81.0
      M/ ]. T6 r7 k0 C4 {
  6. >>>
    ( `% E7 _" \( c% y8 p5 F: t# U& R
  7. >>> math.pow(2,7)2 O: L  F9 z( J2 o# n) G: d
  8. 128.0
复制代码
0 u+ @. q( X1 H4 P2 q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 r+ b; ^, ?8 E
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    # {1 @- u) u' r
  2. log(x[, base])' t( Y8 |/ J7 J& e8 a' v
  3. Return the logarithm of x to the given base.
    $ v4 _$ o6 x$ @4 V9 W/ G! U; t
  4. If the base not specified, returns the natural logarithm (base e) of x.
    % i0 B3 ^) P& Q3 J2 Y& D4 ?/ g+ G: A
  5. >>> math.log(10)% P1 [' O7 K8 O- }
  6. 2.302585092994046) o1 a1 C* H" [5 W* ~
  7. >>> math.log(11)8 U3 i9 M9 D% d( j& c
  8. 2.39789527279837074 [8 R) }6 M# `  X7 P
  9. >>> math.log(20)2 A/ B5 I  [( ~
  10. 2.995732273553991
复制代码
6 }6 y3 e* p* G) A
math.sin(x)  求x(x为弧度)的正弦值$ G4 p# r7 h- T9 j0 w( ]( E
  1. #求x(x为弧度)的正弦值
    / ~) v! K/ H" ~! {% o9 x" \& O* j
  2. sin(x)% ]+ l1 B- f: {. @
  3. Return the sine of x (measured in radians).
    / X( U8 I: ]! M$ G* R
  4. >>> math.sin(math.pi/4)- F- Z3 }# h; x* G
  5. 0.7071067811865475; B/ R; n9 Q! Q+ V; D( m' m1 C
  6. >>> math.sin(math.pi/2); {, p* ]# c& D$ H
  7. 1.0# }6 O' ^0 P$ V* v, W% @, y3 K# @
  8. >>> math.sin(math.pi/3): g  z0 ?1 G3 H- h5 w- h
  9. 0.8660254037844386
复制代码

/ @' Q8 ^$ A' M* M; \math.cos(x)  求x的余弦,x必须是弧度. R) I. N) W0 G* ?$ k& D$ ?  Y
  1. #求x的余弦,x必须是弧度
    0 b5 m  |* G) }6 ^, j4 S' `, u
  2. cos(x); a1 w0 k3 f% p1 r2 w% G
  3. Return the cosine of x (measured in radians).: t7 t1 O9 D% W8 k1 n+ D2 B  m
  4. #math.pi/4表示弧度,转换成角度为45度
    & X1 Q4 m2 P3 h: p1 ?
  5. >>> math.cos(math.pi/4)' |; D) R+ S- x* G
  6. 0.7071067811865476
    & P% a0 E# @3 y' a5 ~
  7. math.pi/3表示弧度,转换成角度为60度$ Z/ D5 T" m; q+ q& h
  8. >>> math.cos(math.pi/3)
    . U* L: u1 j3 J! q! d7 q
  9. 0.5000000000000001; @0 }+ M0 _6 q
  10. math.pi/6表示弧度,转换成角度为30度( G/ z+ z; t6 [# J- J0 _1 L
  11. >>> math.cos(math.pi/6)
    $ _3 Y0 ]) w6 J" m. E, r" g2 _
  12. 0.8660254037844387
复制代码

; }$ [% B  j1 h/ t9 A0 s6 Imath.tan(x)  返回x(x为弧度)的正切值
# Z* ?# l2 T/ P) k& P% y
  1. #返回x(x为弧度)的正切值3 V! _, N/ h6 `, l7 ^( n
  2. tan(x)
    . s9 r* S" s0 m5 R  x
  3. Return the tangent of x (measured in radians).
    * ~/ F3 ^  r; j( M, ^/ c9 O$ ?( q
  4. >>> math.tan(math.pi/4)7 \3 A, V; p' q
  5. 0.9999999999999999
    * z+ I& w3 V  l: J+ @
  6. >>> math.tan(math.pi/6)
    / `6 R! h7 U, m7 a& {. W
  7. 0.57735026918962575 O2 Z  p. E3 \8 J. n. B4 }
  8. >>> math.tan(math.pi/3)% n( Z2 L$ C! A. G( G  |( E* u& S
  9. 1.7320508075688767
复制代码

5 b  ]) a$ {' Y( \3 lmath.degrees(x)  把x从弧度转换成角度
+ Z0 T) R) N  k. n! X
  1. #把x从弧度转换成角度3 M# H& M: d6 K/ f% b- `
  2. degrees(x)
    2 c% s6 o+ I/ ]. g
  3. Convert angle x from radians to degrees.
    ' F2 Z, ^! R+ i! w9 y+ ^' L- y6 U* c
  4.   h) J/ E% N+ {) B
  5. >>> math.degrees(math.pi/4)
    * h# n9 Y$ x9 j' {5 v% Q8 t
  6. 45.0
    / N/ O, Q# E4 O" \/ ?7 i) v
  7. >>> math.degrees(math.pi)  p0 f3 o) j9 p+ R
  8. 180.03 \- |/ h% j5 x2 m
  9. >>> math.degrees(math.pi/6)
    ! l9 S# I  M; ^, x) ?  t
  10. 29.999999999999996
    ( _5 o+ B: s5 V  O3 e* M2 a6 S" b
  11. >>> math.degrees(math.pi/3)  d! l- Y8 D8 B$ ~: [2 ?3 u- G
  12. 59.99999999999999
复制代码

0 W% p: I$ ~% g" r, F: dmath.radians(x)  把角度x转换成弧度
0 U/ Y* T# ?. u0 ~7 N# p
  1. #把角度x转换成弧度
    3 n5 l/ T  [$ f$ S+ i/ K! _2 }
  2. radians(x)
    4 R, x1 a4 @+ V" b( |" h
  3. Convert angle x from degrees to radians.# }5 p. W* t+ f. ^- v! |
  4. >>> math.radians(45)
    / e: X/ B% q9 H( s# j1 I* a
  5. 0.78539816339744830 R6 j0 {0 t; ^/ @; j
  6. >>> math.radians(60)
    . B2 c2 T% N8 _5 i& r" P$ ]
  7. 1.0471975511965976
复制代码
3 r& m+ e7 E' X" B8 I9 b
math.copysign(x,y)  把y的正负号加到x前面,可以使用0  `- T0 g3 Z% L6 r
  1. #把y的正负号加到x前面,可以使用0
    * y( ~' i2 ?  S- ?7 X
  2. copysign(x, y)0 d: n8 ~1 x4 p3 t
  3. Return a float with the magnitude (absolute value) of x but the sign
    : F% G8 P$ X" m
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 v. Q- X- f1 `+ R9 b% L
  5. returns -1.0." s5 r2 ?* S! Q0 ]  a
  6. 2 R- D4 ?2 Q6 D5 {9 d8 x1 m
  7. >>> math.copysign(2,3)
    4 h: w% Z9 R/ m  @; m4 T9 l
  8. 2.08 q8 ^4 G5 @& K% U: ]+ A/ U8 L
  9. >>> math.copysign(2,-3)* }3 t0 U1 K# T) p% z" P! N
  10. -2.0
    4 D" c: [$ S. f2 k
  11. >>> math.copysign(3,8)
    5 }# N7 v, l# r3 b5 d( b
  12. 3.0
    ( |2 Z( s5 `% c. b
  13. >>> math.copysign(3,-8). {# |  T+ ~# c$ e7 l
  14. -3.0
复制代码
# c) [2 y" @4 ~. j! H, D& D0 ^
math.exp(x)  返回math.e,也就是2.71828的x次方) D+ X& Q2 Q' `; r$ y
  1. #返回math.e,也就是2.71828的x次方& p' h* o4 c; _7 N/ G6 _) p5 c
  2. exp(x)
    1 ~7 V6 P9 o8 W! A
  3. Return e raised to the power of x.  a' U' p' t7 B9 w# z6 ~7 O
  4.   y2 @( E6 z( R7 i1 w. o. e& Z. ~
  5. >>> math.exp(1)$ h7 }0 V% @' t) E3 ]
  6. 2.718281828459045
    ' |! h- E/ Q, C: H+ f
  7. >>> math.exp(2), R3 k0 j! y) t! U" P
  8. 7.38905609893065
    6 ]7 k& `# b0 A9 _5 j- G) U
  9. >>> math.exp(3)
    1 A: }5 o* z9 e" \
  10. 20.085536923187668
复制代码
' @+ R  p, [* E4 a. S! i
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减18 |2 M; c1 C* e/ b$ g
  1. #返回math.e的x(其值为2.71828)次方的值减1
    + t" n* j. Y2 }5 L2 D5 {/ K
  2. expm1(x)
    1 g$ j3 P% v' U/ S6 n2 m5 D. T
  3. Return exp(x)-1.
    . P* V2 y2 `, X7 v% Z7 i: t. `/ ]+ a
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    4 X0 N: b. M- q# l

  5. 5 D( ]$ ]. V7 f* z$ D
  6. >>> math.expm1(1)# m6 P$ @! D2 a3 D4 k: j
  7. 1.718281828459045
    1 X7 C! z: K+ _# n" y* h
  8. >>> math.expm1(2)
    7 K& e$ S6 G: H& L" Z' W' z7 x
  9. 6.38905609893065, r) }1 L8 q. O8 a1 B+ Q
  10. >>> math.expm1(3)* ]/ v# |; t: z' s
  11. 19.085536923187668
复制代码

5 D5 L) ^3 Q/ C1 R# Q9 L2 lmath.fabs(x)  返回x的绝对值2 v" e: s8 W$ V" ]5 I# a: B
  1. #返回x的绝对值
      r- t: G& i; ~9 J: A
  2. fabs(x)
    ) w1 Y0 x4 N. T8 P
  3. Return the absolute value of the float x.
    + `6 e" S- q' Y2 w* R

  4. 1 q8 g& K- `5 k- O
  5. >>> math.fabs(-0.003)4 _' y) U/ m; F7 A. b
  6. 0.0035 E) M1 K* [9 e9 Q  R- B
  7. >>> math.fabs(-110)  Q: l8 r$ e5 ~
  8. 110.0
    , Z" R# {- c4 V! {
  9. >>> math.fabs(100)
    ; d  Z" g% B2 ]; A; _, y/ _2 ^
  10. 100.0
复制代码
* t4 l9 h2 }9 u# ?1 D% s9 Y0 `
math.factorial(x)  取x的阶乘的值
. a9 s- H$ I) ~- M
  1. #取x的阶乘的值
    2 D0 s" l. x$ f3 O! Y
  2. factorial(x) -> Integral
    5 p2 c: K' P  v. d# P# N
  3. Find x!. Raise a ValueError if x is negative or non-integral.4 P( z7 X/ F2 V% i7 X0 I6 S9 `
  4. >>> math.factorial(1)# z% V0 X; V- m
  5. 1
    2 j( r% v2 z  {5 S+ l3 L7 h
  6. >>> math.factorial(2)
    " n7 Z9 X, r% Y& I2 {
  7. 2: e/ r1 A, G8 f( w& Y) C
  8. >>> math.factorial(3); v& q5 ^% ?& ?! N$ w
  9. 6- S4 d, O8 N, `4 J& R4 D4 g% k  g
  10. >>> math.factorial(5)5 y2 c! Q! }* l* Q% F
  11. 120: m) \  d5 p. k* m  X3 f9 z9 }4 K
  12. >>> math.factorial(10)1 `( s" ^" ~) |9 c6 r
  13. 3628800
复制代码
" O2 w3 c7 ]+ C( U
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
" m) k" ]- f: J# I' v) Q
  1. #得到x/y的余数,其值是一个浮点数
    ( j- T. w8 T# O
  2. fmod(x, y)
    5 {, `% f2 @( x& J
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    7 S1 ]. e4 u' A3 \7 s
  4. >>> math.fmod(20,3)& L, y3 F0 B1 b( a: a  J/ _, N0 y; ~
  5. 2.0
    7 F6 s. N+ k* J' E# S/ s8 S% B1 w5 \
  6. >>> math.fmod(20,7)
    * Y# ?& a% H! r- i" n: e( m1 B( C0 e
  7. 6.0
复制代码

* `! I% j. I, L* O5 E" I' Smath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
* }3 [* ?0 d) {) j& R- M% c+ d
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    , V! S+ T2 X/ j
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    * s! c7 {: |3 j3 A( B0 ?  k; l
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 y3 y% q0 u$ l' d; U
  4. frexp(x)
    ( {, B2 h8 p; X6 U  m- o5 \$ A
  5. Return the mantissa and exponent of x, as pair (m, e).# b& P2 x& l% w5 D( I
  6. m is a float and e is an int, such that x = m * 2.**e.
    4 {6 u4 G% E) U7 h+ n
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    8 \" e* Y: `$ {- o
  8. >>> math.frexp(10)
    ; r; w+ ?9 C3 U* G/ h, V( ^0 H' x
  9. (0.625, 4)/ j* R2 E/ u2 {9 j" _' T
  10. >>> math.frexp(75)# a; b2 W: o& k# @5 I" k
  11. (0.5859375, 7)
    8 `& f4 I6 M! k7 h  x' z
  12. >>> math.frexp(-40). o- O/ g' w: k8 d) F
  13. (-0.625, 6)
    % H4 E7 y& Y; p7 ~
  14. >>> math.frexp(-100)0 |7 @( Q0 L% ^. e+ \' ^# \5 T* C
  15. (-0.78125, 7)+ Q( c/ {3 U2 V) r1 b! t& D$ U% i
  16. >>> math.frexp(100)5 F2 G! B& v( I( |- q) O
  17. (0.78125, 7)
复制代码

7 \8 C7 Y* N/ C* Jmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
6 L8 M7 x$ N  y- d& M1 z
  1. #对迭代器里的每个元素进行求和操作
    4 H- b! c, h: L* I, _8 F* m; U
  2. fsum(iterable), p; M; a3 v- [" k& E+ ]7 F
  3. Return an accurate floating point sum of values in the iterable.6 e! G( U( z0 g
  4. Assumes IEEE-754 floating point arithmetic.3 B( T3 R: `( ?6 e& ^
  5. >>> math.fsum([1,2,3,4]); l+ ^" X6 o. `( C
  6. 10.0
    4 h) F" I& l2 w! T, V/ @, G
  7. >>> math.fsum((1,2,3,4))1 y; A. {5 J, r' i! V1 m
  8. 10.0
    ' V4 \! p' m# @% ?1 ^
  9. >>> math.fsum((-1,-2,-3,-4))
    9 x5 d8 a# p# \1 @
  10. -10.04 f% l& N2 P' U6 w- w
  11. >>> math.fsum([-1,-2,-3,-4])
    " {; l) [) o7 l5 f: {
  12. -10.0
复制代码
. K' ^! a6 z  o5 _: B5 f4 V8 K
math.gcd(x,y)  返回x和y的最大公约数
2 ?5 H8 U- ~5 A$ L! G1 d0 T4 J
  1. #返回x和y的最大公约数" V- s* D" G( Q% n
  2. gcd(x, y) -> int" k. N+ s& a0 i" I" ], U' i% O
  3. greatest common divisor of x and y
    : F$ N9 `( @& U! z7 N2 M% K1 u
  4. >>> math.gcd(8,6)+ D& w) Q( `$ t3 `# _
  5. 2( I' i5 q; B8 k& t( O6 E) J+ U
  6. >>> math.gcd(40,20)% Q9 e9 O# A$ L2 D  o
  7. 20; N- O' H' T# v6 r6 \! g
  8. >>> math.gcd(8,12)
    % \4 |0 C& \3 A
  9. 4
复制代码
2 Y) U" a4 A5 p' ~; m+ a
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False: ?; H0 z# B3 W' H7 X
  1. #得到(x**2+y**2),平方的值
    0 N" P! d7 }+ ?) Z6 V+ x
  2. hypot(x, y)
    1 f( j9 l7 R/ L0 Y# n" K7 {2 O6 t
  3. Return the Euclidean distance, sqrt(x*x + y*y).6 l" Y7 ?- |; m, I
  4. >>> math.hypot(3,4)
    * E) H! e5 ^1 I* w" ^
  5. 5.0
    - |5 R) Y3 q  }
  6. >>> math.hypot(6,8)
    5 M9 ^* V) ?7 A% |+ P
  7. 10.0
复制代码
8 X) @# v( f: ~% t7 k
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False  m# h  z1 Z8 i. d) Q% @( ^6 T
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ! x; `, E1 Y" c- _+ V  K
  2. isfinite(x) -> bool' A3 i" ^! B  u- C8 ?
  3. Return True if x is neither an infinity nor a NaN, and False otherwise." b3 g- |- i: U8 E$ I
  4. >>> math.isfinite(100)
      O/ @: D1 c% r* r: t4 o
  5. True
    ) w1 C4 |5 A/ S) o) v6 W- `
  6. >>> math.isfinite(0)6 ~* H& v+ |, x' s$ Z* R) t
  7. True6 e7 C" H3 i/ }9 i2 D- B6 b
  8. >>> math.isfinite(0.1)
    3 V1 q# s6 @" S" X" M# c0 v
  9. True
    1 W" T' Z+ U9 P0 S, }( ?( l
  10. >>> math.isfinite("a")3 T5 h  `9 e$ l; f9 r4 k0 [0 ^1 p
  11. >>> math.isfinite(0.0001)
    # \9 T$ T/ r; N2 _
  12. True
复制代码
& v3 g  C+ Z3 z* {, i9 @; o9 Q6 Z! z
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
# a1 L/ o& H6 U' \. K4 \
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    * V9 c& j, [6 ?, U; j, ?: A
  2. isinf(x) -> bool  W6 S" Q5 ]7 [
  3. Return True if x is a positive or negative infinity, and False otherwise.' d$ T: X% h; E& ?4 A# W
  4. >>> math.isinf(234)
    2 B. e6 j5 z) S. H  v* \9 ~" n$ A7 U
  5. False* K" j# q$ Q+ }& _, ^" X
  6. >>> math.isinf(0.1)
    & m+ _& f2 }/ F- _
  7. False
复制代码
& Q$ R! j/ e% `
math.isnan(x)  如果x不是数字True,否则返回False2 q2 K# r7 ~8 Z: [$ K
  1. #如果x不是数字True,否则返回False
    - Q5 X) E7 V0 }1 s
  2. isnan(x) -> bool* m0 p! q6 s( u% s% G( u- k2 P
  3. Return True if x is a NaN (not a number), and False otherwise.
    5 Q4 s' |" \! y& R: h  K& z
  4. >>> math.isnan(23)1 o5 y& o: w; F6 ?$ W
  5. False" v  @& D+ _9 ]' e% Y4 S! e1 U
  6. >>> math.isnan(0.01)  E0 Z  v) `& ~% Q$ E  E
  7. False
复制代码

+ |  f( e1 _- W8 d/ ymath.ldexp(x,i)  返回x*(2**i)的值, Y8 _0 U0 G- Y4 M
  1. #返回x*(2**i)的值) T- u. j3 P1 V: s/ q" U- N
  2. ldexp(x, i)
    ' J6 A2 u- {* A# B
  3. Return x * (2**i).& m$ d' X- p; W+ u4 ?
  4. >>> math.ldexp(5,5)$ q* I+ U  V! c# I) c
  5. 160.0
    ' C" W" w2 T5 f+ v8 K6 j
  6. >>> math.ldexp(3,5)5 ~7 d: M% u% O; s/ U
  7. 96.0
复制代码
8 Z3 Y1 y- q8 M: s& x0 x: |9 Y
math.log10(x)  返回x的以10为底的对数; ~" ?, b7 d% n- y" n
  1. #返回x的以10为底的对数
    : d+ ?5 y4 m9 H' G% H% ?4 j" ]
  2. log10(x)  i: [. w  T/ d% C# Z
  3. Return the base 10 logarithm of x.* _1 X  M/ v/ N9 g5 G
  4. >>> math.log10(10)
    % Y4 b% W* X* w/ U% ?
  5. 1.0
    3 @( x3 T; d; ^7 I# ?% \
  6. >>> math.log10(100)
    1 F, W# ^: B! a% D! d
  7. 2.07 N) F! E) ^1 V+ W/ L; \
  8. #即10的1.3次方的结果为20
    4 g: X) M2 }: M8 Q/ Y  b- |
  9. >>> math.log10(20)) {3 o5 ?. m8 H) I' i+ ?+ J- s' q
  10. 1.3010299956639813
复制代码
; A# Z. V% V$ R$ K8 d
math.log1p(x)  返回x+1的自然对数(基数为e)的值
) w6 v0 e  S, [5 n* K1 s' j/ R3 P
  1. #返回x+1的自然对数(基数为e)的值5 ^4 M$ \) ?4 H6 \/ i0 W
  2. log1p(x)
    3 r, s0 {1 {+ m& h
  3. Return the natural logarithm of 1+x (base e).+ f3 a- E% @7 |& `9 k" T
  4. The result is computed in a way which is accurate for x near zero.
    6 N, l# q3 F% m6 Y% o) `4 C
  5. >>> math.log(10)
    + c$ Q& _" P* D+ s4 m. R' d, m  b
  6. 2.3025850929940463 s0 H. f2 y: f
  7. >>> math.log1p(10)
    1 I, J" I( H+ z6 f
  8. 2.3978952727983707
    ! N( Y5 @% b# f# t( |
  9. >>> math.log(11)# q) l" s# P8 I# e
  10. 2.3978952727983707
复制代码
6 Z; e. X, }3 l3 v% Y1 r
math.log2(x)  返回x的基2对数  u4 ^- N/ Z) m  U
  1. #返回x的基2对数& e' w, @3 ?8 f4 _  K- N+ L
  2. log2(x)' K: b) }7 M4 A. ]( v
  3. Return the base 2 logarithm of x.
    2 ?7 h: {; _6 e" O' N
  4. >>> math.log2(32)0 U6 V1 W& K1 q* {+ H0 z3 E
  5. 5.0) y# K4 Y( ?* N" X! h6 M
  6. >>> math.log2(20)9 m1 h3 r" N2 A! T$ k0 B
  7. 4.321928094887363
    1 G& o+ n! B% I* ]2 z* B: g/ G
  8. >>> math.log2(16)' ~& k7 J8 F+ _5 ]) N  M
  9. 4.0
复制代码

5 {9 f$ _2 g: Z& `! t# t+ t; \+ j6 _. Gmath.modf(x)  返回由x的小数部分和整数部分组成的元组
, g; s  ^; A1 H
  1. #返回由x的小数部分和整数部分组成的元组
    0 \6 X4 ?$ L' s. _3 E
  2. modf(x)" k8 M' n! M: z" t# l
  3. Return the fractional and integer parts of x.  Both results carry the sign- M6 m6 A% |3 x: m! B+ t/ y0 B
  4. of x and are floats.9 k  y2 j' r% Y% d& _
  5. >>> math.modf(math.pi)
      i% x1 i8 v+ q8 I7 W; w
  6. (0.14159265358979312, 3.0)4 ~$ b2 ?% ^& e+ F
  7. >>> math.modf(12.34)% a3 [: {( z/ u: }+ w" W9 E
  8. (0.33999999999999986, 12.0)
复制代码
6 \/ `  N7 b; A5 L  B* N  V
math.sqrt(x)  求x的平方根
- i# J% u0 I# @
  1. #求x的平方根! {9 j4 |: K4 W, _( Z! ?7 @( m  \
  2. sqrt(x)- H1 x% _* S  H5 D- c) o
  3. Return the square root of x.
    + Q# e/ Q- D/ J! k
  4. >>> math.sqrt(100)7 K" s7 r2 L; d+ Y/ S3 y% C/ z
  5. 10.0
    8 m- _9 S6 P- p% s. p. @
  6. >>> math.sqrt(16)
    5 \0 r4 S- N' P' K) x& `1 K6 I6 u6 L
  7. 4.0
    ' A& G$ {- G: Z. d
  8. >>> math.sqrt(20)
    ! m" M7 n, {% R' E- F6 O7 B. h% Y
  9. 4.47213595499958
复制代码

) y" f; O) Z& imath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分# d4 r0 O0 ?! X# i- U" W' ^: `
  2. trunc(x:Real) -> Integral7 F) w, V' C& i. b9 j! k# r
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.+ i+ Z% v0 A4 l8 e- v
  4. >>> math.trunc(6.789)8 R2 X8 a9 h" B1 H2 s
  5. 6( y- f6 F' @. u" S3 W
  6. >>> math.trunc(math.pi)" R! }% d3 {, f& f) b  S( M0 @
  7. 3
    , [- a& Q0 w; X) x/ f3 V% e. n% b, y
  8. >>> math.trunc(2.567)- n' o, F/ E! f" q, w
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-18 20:35 , Processed in 0.083081 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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