新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

4 @# k8 a! G9 }6 E* q  |$ p" {  L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。4 Z, N+ `, y  H6 x
5 o3 C3 D# y+ z6 J
方法1) W4 u6 n5 X  E
  1. >>> import math
    . l4 O9 x: M- U
  2. >>> math.sqrt(9)! a& E! t5 G  k' k% {
  3. 3.0
复制代码
方法2
! s' h  s9 k' B# ]9 r$ }
  1. >>> from math import sqrt
    " A* l/ k0 H, Z) |8 O( ~
  2. >>> sqrt(9)/ L9 j. }3 _& Y" O" S( p! c) @
  3. 3.0
复制代码
0 o& w0 Z2 s- y5 R# h: W( P

# g3 q% K) U- m( J$ ^
math.e  表示一个常量
, t# U: P1 C( B
  1. #表示一个常量
    " D& \4 V; x8 r9 o( H; t( u
  2. >>> math.e
    ! f# U" ^9 Q; b4 J6 \' s/ m
  3. 2.718281828459045
复制代码
2 L- F8 x! h$ d
math.pi  
数字常量,圆周率

1 l! c3 q: x1 }6 M
  1. #数字常量,圆周率0 X' B( E6 n& C* h
  2. >>> print(math.pi)
    * ?% Q' d% ?9 C! K0 R
  3. 3.141592653589793
复制代码

1 K2 E& n% }' K+ o: S8 umath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
# L. D$ S9 y8 }9 u: @+ j8 B& U
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    6 N9 h% H3 ^  Y
  2. ceil(x). S% c& \9 i3 P9 W  j
  3. Return the ceiling of x as an int.
    $ h5 _$ h* l" T- i4 c
  4. This is the smallest integral value >= x.4 Y8 C( v: p4 k* R
  5. ' R- E" G( e0 \( Z, _+ C5 g- u
  6. >>> math.ceil(4.01)' Q+ A" w; K8 X* b1 l. m
  7. 5
    , F* Y6 f. [3 |4 r, D- \
  8. >>> math.ceil(4.99)
    * ~) x4 _/ _. g' ~) Q* t: C1 g( B% G
  9. 5) |" M& n3 o& B
  10. >>> math.ceil(-3.99)# H( U; [; b2 u* d
  11. -3
    4 U) Z* m0 i" b- q$ |7 Y
  12. >>> math.ceil(-3.01)/ B7 L* Z7 |2 L  a; L) {& E# ~
  13. -3
复制代码
9 ~8 ^" b0 S9 M) @2 s, {% q1 G* j
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; V/ ]% D  D+ ~8 I
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 P% H! D$ b) ^5 K, N& [8 O- _
  2. floor(x)
    - V* p# a' _5 P: B+ P
  3. Return the floor of x as an int.
    2 `7 o" w8 {- k4 q+ T6 F
  4. This is the largest integral value <= x.8 ?5 ]& W( [; b
  5. >>> math.floor(4.1)) H$ D3 E  D* V6 g) n
  6. 4
    4 l0 o- e# D6 n0 w2 r
  7. >>> math.floor(4.999)
    % \: H0 k1 \& [* J
  8. 4" Z) x8 R1 ]8 x5 B
  9. >>> math.floor(-4.999)3 o* V/ A& f5 D/ ^6 H# z
  10. -5' k* V) K7 x( Q
  11. >>> math.floor(-4.01)
    1 i& M5 h: E9 Z
  12. -5
复制代码

" @, l' x7 J& s2 G  Lmath.pow(x,y)  返回x的y次方,即x**y1 x5 L* {! ]8 f" \5 M$ Y
  1. #返回x的y次方,即x**y
    8 X' e+ z& F& O3 }- c' c1 w
  2. pow(x, y)* N8 z* M# Z0 m9 P# B
  3. Return x**y (x to the power of y).
    + d- N$ I- V6 b. U
  4. >>> math.pow(3,4)
    - U5 `% u( g8 E- t6 Y5 P. j: |
  5. 81.0
    / V5 k) I7 c/ {* Z
  6. >>>
    6 @3 j+ H7 T, S6 ~* G: x
  7. >>> math.pow(2,7)) C6 x/ q# D) ^" T9 Y/ N, M
  8. 128.0
复制代码

% x. c4 q) b: K$ ?) jmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( _7 C. X& E5 t, j. |4 h
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    , q% U# o) j( U9 g( l" p1 v
  2. log(x[, base])
    / p' @% I5 x/ z9 W5 F' r0 l
  3. Return the logarithm of x to the given base.
    ' `' p1 |& {$ }* v  U6 y
  4. If the base not specified, returns the natural logarithm (base e) of x.
    . i9 ^( Q" F1 o% |
  5. >>> math.log(10)" N, ^; j6 d8 W6 w
  6. 2.302585092994046! @3 Q2 F* D  H, e0 u( t
  7. >>> math.log(11)
    : u1 ^! O, n. g8 ?8 o. @& f  k
  8. 2.39789527279837076 p4 M6 N% M& W. a
  9. >>> math.log(20)
    ! c8 F" B% ~/ d7 U* ~8 y
  10. 2.995732273553991
复制代码

$ J; h$ a: d/ z/ @* @$ Q3 D0 M, emath.sin(x)  求x(x为弧度)的正弦值) ^  f3 t) @3 i, n4 v- c
  1. #求x(x为弧度)的正弦值
    6 E# v0 d  X6 e
  2. sin(x)0 W% q  H5 D: J) s& W8 F% R
  3. Return the sine of x (measured in radians)." _8 R( U5 F- g. z
  4. >>> math.sin(math.pi/4)
    7 g1 k' u/ I( v6 o) C
  5. 0.7071067811865475. c" W/ E( G, L/ i5 |% i, Q# v
  6. >>> math.sin(math.pi/2)
    % l/ G5 P1 o  m  }  K
  7. 1.0
    5 _. Y+ U  F) ^! d
  8. >>> math.sin(math.pi/3)- A- @% z. P! l4 }! R) L: M
  9. 0.8660254037844386
复制代码
( ?9 k8 z; O) C, s* Z4 e! M9 d
math.cos(x)  求x的余弦,x必须是弧度
* o2 v1 n" ?7 Y  r: I, ]/ A
  1. #求x的余弦,x必须是弧度
    / ~4 O  F* p3 ~
  2. cos(x)
    . W8 H9 @3 q) ~) y
  3. Return the cosine of x (measured in radians).
    # f+ ^; P( }* ~0 c- x
  4. #math.pi/4表示弧度,转换成角度为45度
    ' V* X: K6 Z  e& \( X4 R! T4 x
  5. >>> math.cos(math.pi/4)9 }3 m- d* D/ l; Z
  6. 0.7071067811865476
    $ H7 b) i+ P6 ~- p& r
  7. math.pi/3表示弧度,转换成角度为60度
    & Y* q. s8 V7 `" }7 K
  8. >>> math.cos(math.pi/3)' F  ~" c. M+ {6 {. v  I9 d6 ~5 ~
  9. 0.5000000000000001
    % H8 o6 _6 K  h* f
  10. math.pi/6表示弧度,转换成角度为30度- M" \% ]% k# ^( e% l
  11. >>> math.cos(math.pi/6)3 `# l* f5 x/ }- l3 t
  12. 0.8660254037844387
复制代码

9 w; d* A% K: dmath.tan(x)  返回x(x为弧度)的正切值2 H% R9 x( R8 [+ A$ t) K& c' O
  1. #返回x(x为弧度)的正切值- ?9 U. A' q) h
  2. tan(x)! A; s; k/ ~% V7 b! {% J
  3. Return the tangent of x (measured in radians).
    # h2 p& I/ v' b
  4. >>> math.tan(math.pi/4)
    : v) f3 h6 b" I5 N7 W4 j) G
  5. 0.9999999999999999. K' v8 z0 J6 U
  6. >>> math.tan(math.pi/6)
    / p) |3 V2 y5 q8 y6 n8 y5 T- a
  7. 0.5773502691896257
    ; T1 `% E' G# ~- A) j* j0 e, Y, l' |8 W
  8. >>> math.tan(math.pi/3)3 a2 b% L7 Y3 U# S+ q
  9. 1.7320508075688767
复制代码
5 R8 d" Z* n$ H+ ]" R4 z
math.degrees(x)  把x从弧度转换成角度5 `3 y1 ^3 J# E4 u' T; N* u1 a; _3 b
  1. #把x从弧度转换成角度
    " l) r* p+ C' B# i' B
  2. degrees(x)2 P8 ?: |; ]( X( l4 h& v; b
  3. Convert angle x from radians to degrees.- D. _% _+ r4 J

  4. 9 u! k7 t1 C  b: U* Z( P/ h) V* m
  5. >>> math.degrees(math.pi/4)
    8 ?( j* y" f& O6 V, _4 F
  6. 45.0
    & R2 J& y2 `. \8 |) `
  7. >>> math.degrees(math.pi)
    . }8 b- F, j* a! ~
  8. 180.0+ X8 O- G; B0 w/ u9 P: a( B
  9. >>> math.degrees(math.pi/6), |0 `- [7 U0 u' A+ H
  10. 29.999999999999996* e7 i3 w2 }  P) j
  11. >>> math.degrees(math.pi/3)) V3 ^" E+ \# B( @: C4 h7 G
  12. 59.99999999999999
复制代码
% {6 Z$ {0 W8 L
math.radians(x)  把角度x转换成弧度; e8 R& L" n0 K. ~- t
  1. #把角度x转换成弧度
    7 B; P! j! i4 M; J) g
  2. radians(x)
    9 a3 e/ u* N& {
  3. Convert angle x from degrees to radians./ C) V9 ]$ B. g0 j
  4. >>> math.radians(45)
    & }% W* F) L6 X7 r' b% H
  5. 0.7853981633974483* f1 K% l0 U4 s7 r9 }+ u
  6. >>> math.radians(60)
    : Y0 M/ v, S# t8 ]+ c% o4 r1 P
  7. 1.0471975511965976
复制代码
7 ]7 R2 z" i$ m8 G) F
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
/ J: L% U+ C, o9 u0 Q
  1. #把y的正负号加到x前面,可以使用0- K4 Z8 B9 V% q3 x0 T5 T4 R) w
  2. copysign(x, y)* F9 m% B  g  p8 f- z
  3. Return a float with the magnitude (absolute value) of x but the sign ! Z! A0 ~6 X& r3 D$ q" T
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    $ w& i  r; _, T( I) ]
  5. returns -1.0.7 q6 H" r7 P4 Z8 M  A/ G2 \
  6. 2 |! R) u' f1 n( k0 u2 p% D' G, q
  7. >>> math.copysign(2,3)
    + `  t: _4 d% p9 g: Q: y8 J  f
  8. 2.0
    / C- X( k% R  a! a* U. P
  9. >>> math.copysign(2,-3)
    ' T5 r- |6 L7 ~: ~7 `
  10. -2.0
    $ t7 I5 T% m) A! n- x0 v! @0 t
  11. >>> math.copysign(3,8)
    ( `, M4 G/ A2 u% q' y; Z
  12. 3.06 s" D! a+ x3 l% H
  13. >>> math.copysign(3,-8)
    # A3 C* d9 {) @" H3 {
  14. -3.0
复制代码
4 k' u8 w2 E  f: s8 E/ N/ \
math.exp(x)  返回math.e,也就是2.71828的x次方4 T% ?0 f+ H. \+ g/ }- J
  1. #返回math.e,也就是2.71828的x次方
    8 a2 @" b. r% J% a5 O( P4 ~7 d2 O# _( G
  2. exp(x)
    # `7 d& `' n. W! k$ h! H5 i% b
  3. Return e raised to the power of x.
    , A# q6 o( V: h7 A4 U
  4. 9 X  n; L0 y5 u
  5. >>> math.exp(1): j# s# U% D+ b9 d& O, }$ H0 u% J
  6. 2.718281828459045+ C4 {( @9 b% C. V
  7. >>> math.exp(2)  O9 `. }7 w% j
  8. 7.38905609893065
    9 G+ W9 L' K1 W0 H
  9. >>> math.exp(3)
    / D) p7 r$ A& \" \! ^! ~+ M
  10. 20.085536923187668
复制代码

; g6 u9 W& S9 p, ^math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减13 G/ i. K. c& K2 ?( V, F2 Q/ H
  1. #返回math.e的x(其值为2.71828)次方的值减1
      |' y3 b6 f. h7 L8 U" g( {
  2. expm1(x)
    . T7 X% A$ E% v4 U
  3. Return exp(x)-1.
    - T3 X7 z* i, p* D3 ]$ A$ N2 G
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. U. z8 P4 W" E3 \
  5. + u$ @$ |5 @- H# ]
  6. >>> math.expm1(1)
    4 }  v) o* M! _7 q
  7. 1.718281828459045* j1 d' p( |# i
  8. >>> math.expm1(2)9 q! ~8 |( n6 R3 P$ ?
  9. 6.389056098930658 g! ?9 ^5 A5 k4 h% l
  10. >>> math.expm1(3)
    - \6 ?0 y, W& O* `$ B5 ^, w1 p
  11. 19.085536923187668
复制代码

6 F$ n& g( ~  ^math.fabs(x)  返回x的绝对值
0 A: J2 e, g! f1 d' G
  1. #返回x的绝对值2 X, a) ?6 P+ s5 ?. x
  2. fabs(x)
      @. E; |- ?( |; [
  3. Return the absolute value of the float x.
    & `6 T( q/ M, W& T' G

  4. / Z/ G! a4 s* f) Q+ \
  5. >>> math.fabs(-0.003)
    . b4 M# I, R& p9 D0 @4 A
  6. 0.003
    ) }9 g  K) U3 x
  7. >>> math.fabs(-110)& K; D% J% C( j' U" n1 V% X* v5 F
  8. 110.0
    . R, L% Z5 M" L; L
  9. >>> math.fabs(100)% O; m; Y' E# V" X! {  d4 A5 ]
  10. 100.0
复制代码
4 n) \0 k! }5 Z- P8 h
math.factorial(x)  取x的阶乘的值; ?, ^+ [/ n# H" t. E3 U
  1. #取x的阶乘的值
    ' J; o7 Y. I! W: c7 q# c9 g# L
  2. factorial(x) -> Integral
    7 Y# B2 m+ T( f" L" q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    % _0 \8 n5 K1 i' {' |
  4. >>> math.factorial(1). u/ K/ B+ b  u2 B
  5. 1
    : w; c* y/ R" W! _
  6. >>> math.factorial(2)9 t- P# K# u9 ]7 }0 E) m3 r
  7. 2! B; U2 z$ u+ A3 Q: T
  8. >>> math.factorial(3)- b. U$ H& e  t
  9. 6
    4 ?) O( ?4 `* [; f/ j
  10. >>> math.factorial(5)
    # ?3 f; a7 X  ?% {" J
  11. 120
    1 M; `9 E, t6 \9 ~8 ?$ `7 G
  12. >>> math.factorial(10)
    % `7 R, U" X# P, U% @
  13. 3628800
复制代码
* x$ M# Q' I: G3 \
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
% I/ J  f0 q" m. F" H9 a
  1. #得到x/y的余数,其值是一个浮点数
    # \2 K8 ^6 P7 @& |
  2. fmod(x, y)+ Z. \3 \& g8 J6 `# J
  3. Return fmod(x, y), according to platform C.  x % y may differ.1 C% m: X$ t) D
  4. >>> math.fmod(20,3)' m+ y; m7 z# ?5 L0 ^
  5. 2.0
    * i& s9 O; g8 V& a
  6. >>> math.fmod(20,7)
    ( \! N) _9 w: Y: I
  7. 6.0
复制代码
3 k6 ?, @: A2 w$ u  F
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
4 H+ q" F* V9 i* l  {  c
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    $ \# h& p# n$ c- P* u5 x
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" y- d9 v- g5 i, }7 g3 }0 a  S& \' p
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 k6 _, t7 y% g* ?8 w
  4. frexp(x)! o7 J8 {; d$ w* D* c
  5. Return the mantissa and exponent of x, as pair (m, e).
    9 `, E$ U+ [7 m
  6. m is a float and e is an int, such that x = m * 2.**e.+ @; h3 ~  a% G, h; V" k2 \5 r
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.2 H% ]# i2 L9 y5 q& ]/ k
  8. >>> math.frexp(10)$ {# w5 ]2 U7 ]; \
  9. (0.625, 4)- S. f( K( Z% \: Y$ t) O5 Z
  10. >>> math.frexp(75)
    1 o9 W' a' p- o$ `* j, U0 _
  11. (0.5859375, 7)
    7 L: y7 R1 J+ K5 F. o8 B( j& R* a
  12. >>> math.frexp(-40)
    ' u1 p: L' n- Z3 p! ?+ M
  13. (-0.625, 6). [& X# L, N* |; x1 `1 A! P
  14. >>> math.frexp(-100)
    $ P9 L! o  y4 b  z* [) g
  15. (-0.78125, 7)+ q+ T2 N3 n: M! U
  16. >>> math.frexp(100)$ l9 Z( t* @$ T
  17. (0.78125, 7)
复制代码

; k- \) g7 A5 K$ gmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
6 a" ^, K; X4 Q" f- F
  1. #对迭代器里的每个元素进行求和操作
    4 a7 _/ H( ]  [7 ^$ l
  2. fsum(iterable)
    1 E! U3 `9 W' ~% D
  3. Return an accurate floating point sum of values in the iterable.
    3 R* k6 C6 Q0 B% }* u$ S
  4. Assumes IEEE-754 floating point arithmetic.
    " ?) }6 G8 t7 a* M5 `
  5. >>> math.fsum([1,2,3,4])3 J5 G- |# x4 v2 C/ Y' _
  6. 10.05 L- J3 x! e& J; P7 _( e) G
  7. >>> math.fsum((1,2,3,4))
    1 y+ w" h3 [  u, l; F  e
  8. 10.0
    . t* o' c7 z0 }: r! L
  9. >>> math.fsum((-1,-2,-3,-4))
    , u% h) {/ _+ X6 ]' f" Q5 _
  10. -10.0: S$ l8 ~8 n* I! M2 i
  11. >>> math.fsum([-1,-2,-3,-4])
    % H2 v( h: P& e! a, S; D: P9 q+ M
  12. -10.0
复制代码
  ^% X7 [4 f  i7 Y) e7 ~7 ~
math.gcd(x,y)  返回x和y的最大公约数
2 X% L+ @9 _+ Y: ?' [
  1. #返回x和y的最大公约数
    7 |. Z7 C8 t6 ~9 l5 q
  2. gcd(x, y) -> int) Z/ B: K2 Q$ t: \6 @
  3. greatest common divisor of x and y
    : O% L+ E+ `- O
  4. >>> math.gcd(8,6)
    6 U6 p" W& v# f
  5. 2# |9 A+ f' R7 Y
  6. >>> math.gcd(40,20)
    " |0 Q5 n1 F% ^1 c5 r$ A
  7. 20
    3 K, g8 V/ o( x9 m
  8. >>> math.gcd(8,12)4 k/ ]( ]  n7 k8 Q. O; m
  9. 4
复制代码

% x' L9 I1 W0 ?% w3 Imath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
( S8 M0 R- O1 A; e1 @
  1. #得到(x**2+y**2),平方的值
    ) C$ o3 N# l0 h1 e% T0 S
  2. hypot(x, y)
    # T8 `& j0 ^0 [$ L& r$ p
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    # m$ Q; l& j+ v  M/ ]- S
  4. >>> math.hypot(3,4)
    2 d# G. Z6 b# x2 t1 T0 a
  5. 5.0
    * y4 L2 l/ m9 z* o; \9 d: n
  6. >>> math.hypot(6,8)# [2 i) E3 k& M1 L) E. v* P) h4 c3 F
  7. 10.0
复制代码
6 T" Z7 T$ @* `
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
, }9 l% V& h9 L) a( n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False/ u1 J, V/ F3 g( \
  2. isfinite(x) -> bool
    . N+ z; N7 t1 Q5 k8 s+ w
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    # R3 E/ o# m; R* Y- p: Y* w
  4. >>> math.isfinite(100)# G3 E; `, K+ T5 I& L1 h1 S0 o
  5. True$ d4 N$ `) z0 I6 I
  6. >>> math.isfinite(0)
    ( m8 R8 q1 |0 W
  7. True" ^5 h1 E3 Q" f. B$ x6 ?: S1 p: t9 o' {
  8. >>> math.isfinite(0.1)
    + R% e# d1 s4 D
  9. True
    & t) R7 C2 [1 m; r5 Z# J+ i
  10. >>> math.isfinite("a")
    & e' U+ k3 E% U1 V% a. v
  11. >>> math.isfinite(0.0001)9 b, P  e' M- Q9 P
  12. True
复制代码
9 `5 z) H/ p$ E; L1 }) G
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False! ~# i; h5 N6 S# f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    - \* R) B9 y% \
  2. isinf(x) -> bool
    2 {1 P$ _) s( h& y
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ' y; h( e/ Z% F% Y* D& Y) g
  4. >>> math.isinf(234)
    , z4 |) ^$ l! S! k0 q2 P0 J
  5. False
    , z/ E, g/ }& N) f' G4 e" F3 m# g- ^
  6. >>> math.isinf(0.1)
    . o& d2 L" X9 o  n, m, B7 x
  7. False
复制代码
+ J) N% q1 J: A
math.isnan(x)  如果x不是数字True,否则返回False
( U/ n6 ~9 |7 D8 p) T
  1. #如果x不是数字True,否则返回False4 N( o$ K8 G8 Q# P$ k8 ?2 d" u2 f
  2. isnan(x) -> bool, E: ]9 {- P9 ]. p7 ^
  3. Return True if x is a NaN (not a number), and False otherwise.. G  W" j- p$ T! }
  4. >>> math.isnan(23)
    ; L! [  @: }- f7 O% s0 V/ w( V8 e
  5. False
    # W5 p+ s4 L$ I$ l$ ]4 V! p
  6. >>> math.isnan(0.01)
    # F8 Y" L& E  Q% F$ U0 o+ i: Y: u
  7. False
复制代码
' ]( `6 K5 [+ Z# A9 d4 g
math.ldexp(x,i)  返回x*(2**i)的值
4 Z( w) ~. ^* Y
  1. #返回x*(2**i)的值
    ( M( d4 v6 r0 ]) d0 B
  2. ldexp(x, i)* F( {: P  |5 [) q3 t- e# J
  3. Return x * (2**i).0 {: D" s$ I2 Z8 G$ s; d2 F
  4. >>> math.ldexp(5,5)
    7 F% X1 f$ f: r+ ^- \2 ~+ S9 h
  5. 160.0
    4 C7 `  J3 W8 _; c
  6. >>> math.ldexp(3,5)* Z" ?& `5 I5 p8 P# Y! W
  7. 96.0
复制代码

+ t9 z1 m/ M  {5 k+ u& Pmath.log10(x)  返回x的以10为底的对数/ a( t; H, D9 ]0 g+ [
  1. #返回x的以10为底的对数& S; u* ~, c* Y+ h1 C- [
  2. log10(x)
    3 X9 C5 W8 `5 ~8 A$ }
  3. Return the base 10 logarithm of x.* r- K# R+ m3 i, `9 H8 Y7 W2 b
  4. >>> math.log10(10)
    * m" A  }) a# u
  5. 1.0) j# z1 {& C9 ?
  6. >>> math.log10(100)
    " J7 X, l# V5 y
  7. 2.0
    ! ]1 [( S) y' i' ^+ }
  8. #即10的1.3次方的结果为20
    ; g' I) o* x- w
  9. >>> math.log10(20)
    2 r% S$ z. P( A
  10. 1.3010299956639813
复制代码

5 G& G  D; U  d: |math.log1p(x)  返回x+1的自然对数(基数为e)的值
/ \! h* Z8 G% d# p' k# w
  1. #返回x+1的自然对数(基数为e)的值
    % Y& C9 ]; |1 w  n, [; `. L
  2. log1p(x)6 t7 G/ c6 ^0 l/ p, p% x# u
  3. Return the natural logarithm of 1+x (base e).
    7 r% ^! T; O1 h% B3 C7 J% }
  4. The result is computed in a way which is accurate for x near zero.
      G* c" x4 ~% F
  5. >>> math.log(10)$ h9 k/ j4 [& w0 a
  6. 2.302585092994046
    0 X4 ?2 Z$ ^4 s* o
  7. >>> math.log1p(10)
    2 G# ?% w! [% Z) I
  8. 2.3978952727983707
    / r  v& p) h. D1 a* y8 M
  9. >>> math.log(11)$ Q) S9 \+ V5 |  M8 Q' b! s1 r
  10. 2.3978952727983707
复制代码
( N0 m$ g* D$ X; i; S- z
math.log2(x)  返回x的基2对数  R, d7 O, @' _# [$ ]" }3 r
  1. #返回x的基2对数
    ; z5 `; |! V- C) d" b) M5 W
  2. log2(x)
    " }- b! ]* }2 h8 `% E
  3. Return the base 2 logarithm of x.
    3 j7 O. ?; T" H: g; P
  4. >>> math.log2(32)' U+ A% N7 g+ E9 F  x/ h
  5. 5.0
    , t+ H# l1 a9 y' i
  6. >>> math.log2(20)- N+ g* X5 S1 k7 l
  7. 4.321928094887363
    " b1 t4 {7 o) y6 c" z8 D' T
  8. >>> math.log2(16)( g% v: u( U1 o- j6 ?1 n! p
  9. 4.0
复制代码
$ V. v$ f& E( @: i6 G% F" M
math.modf(x)  返回由x的小数部分和整数部分组成的元组
" T3 k5 ^; n, A# U( p4 B. D
  1. #返回由x的小数部分和整数部分组成的元组/ X. R* a/ C. y( N& x+ R
  2. modf(x)( X1 ]9 {4 [) c! c! q; G2 R
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ( w% K% j8 b/ p8 _6 c, ^
  4. of x and are floats.
    2 |' \) o3 I' [; O6 p4 G$ p7 G
  5. >>> math.modf(math.pi)
    " L$ m! Q3 G: J
  6. (0.14159265358979312, 3.0)
    ' f3 \1 U! Z, n4 Z3 l! y4 |
  7. >>> math.modf(12.34)
    + w4 l* p( j1 u8 _, W' O: f8 h
  8. (0.33999999999999986, 12.0)
复制代码

3 H5 {& k3 T& Y  m4 H4 G3 K3 G: wmath.sqrt(x)  求x的平方根
( u6 P( _& b1 D
  1. #求x的平方根" d$ j2 r- M. s  P1 f" @. U
  2. sqrt(x)
    ; z  i; K! W) e# V3 ~! P2 y0 I0 J
  3. Return the square root of x.; }% u( V. E2 j8 H& L
  4. >>> math.sqrt(100)
    ! I5 o2 E7 x* v% c4 A
  5. 10.0
    1 r& b/ b2 r4 i! }# k+ `5 n. w
  6. >>> math.sqrt(16)
    ' U7 Y; U# k5 m: H9 J, h
  7. 4.03 [6 |6 g7 w  y9 C3 O; U( d4 u
  8. >>> math.sqrt(20)
    + W7 i9 X& t4 w. m* o: Q8 U
  9. 4.47213595499958
复制代码
# v# r2 \7 J& @6 b6 V" m* R' a
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    : W7 p: s( Y/ w8 N
  2. trunc(x:Real) -> Integral) z6 ~5 |+ G9 p$ l4 m
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    0 N! K2 o! U1 c4 a6 o$ _
  4. >>> math.trunc(6.789)# _7 l6 A' y- c! @
  5. 6
    3 @/ [* [! B2 i% g' }
  6. >>> math.trunc(math.pi)1 U1 W* x% @* U
  7. 3
    + u6 L! z. Z$ R, g' t8 x/ M
  8. >>> math.trunc(2.567)1 d* x5 X% _; M; }
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-16 13:24 , Processed in 0.089939 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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