新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1924|回复: 0

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

 关闭 [复制链接]
发表于 2021-7-24 10:21:24 | 显示全部楼层 |阅读模式

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

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

x
0 R5 ]/ C. c1 k5 U7 |* ^$ O
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
. d5 |" M5 ]! ?" u

* T" Y* ?! H2 b4 z; i7 R方法1+ V1 X9 o1 I, n
  1. >>> import math0 V9 R& X( i- q& ^
  2. >>> math.sqrt(9)
    ! c  v) _1 J8 r9 I. `
  3. 3.0
复制代码
方法2
- x" m$ a( u, R! n6 T6 z0 ]
  1. >>> from math import sqrt
    4 `5 _: j9 J) C$ Z$ n  ]: f7 F$ {
  2. >>> sqrt(9)7 x% Q3 F7 r2 n! K$ I
  3. 3.0
复制代码
6 I" Z- n3 S6 t9 G2 ~" S2 z


8 @$ ?& H7 ?/ k/ ^8 K3 x3 e5 u1 V
math.e  表示一个常量
* \) R5 V2 k* P& U4 P
  1. #表示一个常量
    ( u4 I$ n6 W2 k$ i1 Q* Y+ t4 f/ ^5 x
  2. >>> math.e0 w+ V6 F8 c  t, Q, t. x$ e- Y
  3. 2.718281828459045
复制代码
8 o8 R3 p" t  N0 C0 A$ i1 p  Y2 m! h
math.pi  
数字常量,圆周率
' d# Q; N9 t6 R1 |2 J, h! k, ]
  1. #数字常量,圆周率
    6 ^( X. C5 K5 a9 u# M0 K7 S3 p
  2. >>> print(math.pi)/ m. |) }  @$ l2 C! f- N( Y9 S% I5 W
  3. 3.141592653589793
复制代码
% `, N# z6 \8 g
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

# |+ C/ {7 ]3 L) ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    2 }8 M3 G5 P  n; t  d2 i# q
  2. ceil(x)  I4 X" e; u9 H$ C' Y
  3. Return the ceiling of x as an int.
    * m1 G! P! Z% s5 @; Z
  4. This is the smallest integral value >= x.
    5 D4 h; U4 f3 e4 V" k7 M9 Z

  5.   t9 |$ z# W1 E
  6. >>> math.ceil(4.01)
    ( }8 q0 v# `  `" T! Y4 i% o
  7. 5' v/ U4 L  f5 L' H# u( |$ z" n9 \
  8. >>> math.ceil(4.99)/ z0 X$ q: K8 L; V
  9. 5
    6 I( |1 O" e3 ]) ?& X! w
  10. >>> math.ceil(-3.99), o$ e$ z- l# @# D8 \
  11. -3
    ! B/ W) [' n' ?1 \' Z- f* s
  12. >>> math.ceil(-3.01)
    + M+ K8 X# y5 v
  13. -3
复制代码

' {2 }" E% k; X$ Zmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' }' K# R* f% C0 p7 B' V. l4 Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    3 K% l( o. F$ W/ D. @. y5 b7 w$ e7 W
  2. floor(x)8 [9 X% j8 q; s3 v/ s6 p2 z
  3. Return the floor of x as an int.6 j9 m, Y1 @6 ^. g1 K& O
  4. This is the largest integral value <= x., s3 @: O0 G" W/ {) o4 }
  5. >>> math.floor(4.1)
    6 u& j+ L7 p9 I
  6. 4
    $ c+ x- n5 J* y
  7. >>> math.floor(4.999)4 G7 y) |  f4 P' X
  8. 4( j4 }9 e' L8 ^
  9. >>> math.floor(-4.999)$ T, C/ w7 E/ F$ k# N
  10. -58 ^3 T( _) j. z
  11. >>> math.floor(-4.01)+ x9 @" v+ c4 R; L
  12. -5
复制代码
* O* }- e3 h8 _5 m) Q
math.pow(x,y)  返回x的y次方,即x**y" V% z) c+ B/ E$ r) z
  1. #返回x的y次方,即x**y) P9 }& }8 T% |8 h' G0 W1 D
  2. pow(x, y)# U2 Y' K* s; y' ^  @
  3. Return x**y (x to the power of y).
    ; I* N! g* Z$ b) G+ P
  4. >>> math.pow(3,4)
    2 D7 ?7 i  m+ @9 y, C6 v7 W; u
  5. 81.09 Y" [+ m9 _- E5 L8 J& m0 V0 }
  6. >>>
    ! k6 v2 c7 Q- q8 {
  7. >>> math.pow(2,7)
      ]' O  Y5 [, a; J% R/ m
  8. 128.0
复制代码

% m# G+ {8 k3 G/ n6 q8 Z5 _math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% E) F8 l5 T/ F. ]) D5 {* X' z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    9 a9 c, Y. [' o2 {
  2. log(x[, base])5 X- I, W5 C/ t# o0 W/ E
  3. Return the logarithm of x to the given base.- H+ n8 p! K% T; g0 C
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ( w& _3 H+ [3 v$ s" K
  5. >>> math.log(10); z9 H7 m9 e" w5 D' z' F; ?8 f# t
  6. 2.302585092994046
    ( e( o+ f4 d! B3 J; |5 m& v1 `
  7. >>> math.log(11)  D6 n3 P- m4 N! A! X; Z# ?. n/ O
  8. 2.39789527279837078 r/ X2 R5 A/ Z' G/ K  _: y" c
  9. >>> math.log(20)( ~! j: M  F9 q
  10. 2.995732273553991
复制代码

% N+ t$ ^/ N& ^* d% i: Z$ [9 Ymath.sin(x)  求x(x为弧度)的正弦值
8 d9 P/ U& W2 l. u: p
  1. #求x(x为弧度)的正弦值
    7 a% b! R9 V9 u
  2. sin(x)
      o8 B) m5 ~, G0 p( ?
  3. Return the sine of x (measured in radians).1 y. A) ~! z; c6 `, i) ~6 @
  4. >>> math.sin(math.pi/4)
    4 y, O  y3 \& {# O3 }: J' ?: D
  5. 0.7071067811865475
    + q; U7 }+ f# ?6 q' e
  6. >>> math.sin(math.pi/2)
    2 Q/ c5 T$ R! M! V5 }
  7. 1.0* @' ~4 K: L  {) L! {- m& O( |
  8. >>> math.sin(math.pi/3)
    7 S. ~( b$ B9 [2 X+ v. F6 p
  9. 0.8660254037844386
复制代码

" H# v& r/ r3 ]2 emath.cos(x)  求x的余弦,x必须是弧度, r' v1 m! {* \3 N: |1 e  {5 z$ F! I
  1. #求x的余弦,x必须是弧度
      c0 q0 F4 f& O9 m3 [! r
  2. cos(x)
    5 t5 G, S: }* I1 _( u2 e
  3. Return the cosine of x (measured in radians).+ F1 G, d& J/ Q
  4. #math.pi/4表示弧度,转换成角度为45度
    3 g7 k# X8 }- a, w8 n4 w) M
  5. >>> math.cos(math.pi/4)
    # P/ {% i: F8 x" n: i
  6. 0.7071067811865476
    2 p# H5 w" P/ ]! ~7 [7 Y8 x9 y& n4 X
  7. math.pi/3表示弧度,转换成角度为60度
    ( \. O/ r$ u2 a" O/ K
  8. >>> math.cos(math.pi/3)& E- `; y& E3 B3 O" o
  9. 0.5000000000000001
    1 m& E" X1 |* S1 z
  10. math.pi/6表示弧度,转换成角度为30度
    ' R  B. Z3 ^: l" u1 D. c
  11. >>> math.cos(math.pi/6)
    . b& Q9 n2 D6 q6 I! S8 l% k
  12. 0.8660254037844387
复制代码
$ C+ q$ k; I6 u. @
math.tan(x)  返回x(x为弧度)的正切值
2 p, j* ]5 l7 }1 t3 j7 t, X$ v
  1. #返回x(x为弧度)的正切值
    - r1 @2 l9 F  q4 F
  2. tan(x)' |. _- a, C) G5 g5 d0 e9 c
  3. Return the tangent of x (measured in radians).
    4 {( S* P4 D6 e2 `" C
  4. >>> math.tan(math.pi/4)
    2 x- Z. w( W( \+ o2 u+ T
  5. 0.99999999999999999 {6 a' D# h+ B( b: u
  6. >>> math.tan(math.pi/6)
      {8 t# ^$ y5 R
  7. 0.5773502691896257
    ! X( @  Q4 u# Y9 Y: o8 X- A, Z, x
  8. >>> math.tan(math.pi/3)
    ) M& Q$ ?3 s3 i
  9. 1.7320508075688767
复制代码
- r$ I' l7 U# A$ B  w
math.degrees(x)  把x从弧度转换成角度0 T4 {% U' b, N1 y9 p/ M- J" t
  1. #把x从弧度转换成角度6 R: u* S! |- r% z9 t& i
  2. degrees(x)
    ( j; X: r8 h, j) q: b; d& g
  3. Convert angle x from radians to degrees.* g# q: v# T' Z$ P* f. @* j
  4. * Z9 w/ D- l- g/ u
  5. >>> math.degrees(math.pi/4)+ x& @) f) r9 l. d0 {
  6. 45.0' Z) N. W" D% G% w) Y/ X& z
  7. >>> math.degrees(math.pi)$ q! ?3 y% D% o; V( n% N4 d
  8. 180.06 U  ?4 o. C# \6 ]
  9. >>> math.degrees(math.pi/6)4 |7 ?( @2 k. Q0 @. D" h
  10. 29.999999999999996
    4 F, [% R, k' t- j
  11. >>> math.degrees(math.pi/3)- N8 }3 t7 T! w, G  |1 R
  12. 59.99999999999999
复制代码

, @" v4 l1 L: amath.radians(x)  把角度x转换成弧度/ I8 o. \3 p+ W, }7 s; J3 D
  1. #把角度x转换成弧度
    : ?# J2 Q5 r! w4 W% A( j
  2. radians(x)" @6 y2 X5 z- ~4 o1 d+ {/ Y8 L
  3. Convert angle x from degrees to radians.
    2 d% t- j/ P- D5 I5 N7 b/ [
  4. >>> math.radians(45)
    * W3 g* w. c7 o+ z0 M* {
  5. 0.7853981633974483
    0 }  v4 j0 T) Z; e6 h1 E
  6. >>> math.radians(60)
    " a" }% t- ~; |# e2 n
  7. 1.0471975511965976
复制代码

' _6 h; X; K& x9 h1 R8 Emath.copysign(x,y)  把y的正负号加到x前面,可以使用0
" @- l% X' z7 b$ m: T+ D2 [/ s
  1. #把y的正负号加到x前面,可以使用0
    . K5 f. e2 P5 e) S- p3 ]( C: P- ~
  2. copysign(x, y)5 C8 M3 t3 s' E8 s# b5 t8 J0 g
  3. Return a float with the magnitude (absolute value) of x but the sign   Y' S5 Y- F* o7 v7 ^0 c* W
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & \; e0 b- ~7 }' U4 R
  5. returns -1.0.. l+ n' V4 {& o0 q

  6. 3 h* J3 q9 _  Y
  7. >>> math.copysign(2,3)+ w# T, `# Y; F9 S! N
  8. 2.0
    9 f; }2 L; M  J& o5 Y: F
  9. >>> math.copysign(2,-3)
    ; n+ t) X* H6 o8 c! i' ]% g$ g
  10. -2.01 Z. O8 [$ D+ }( B
  11. >>> math.copysign(3,8)% L. _8 E! ~  y4 k
  12. 3.0
    * L( n8 G( H$ x
  13. >>> math.copysign(3,-8)& Q, V$ m4 B: F- Z' F, _' f
  14. -3.0
复制代码

' C2 }5 f/ E8 l1 t( lmath.exp(x)  返回math.e,也就是2.71828的x次方
  t- Z+ t9 k- {! S0 q! [5 X1 K
  1. #返回math.e,也就是2.71828的x次方
    4 P& s+ D& D& |3 ]1 F0 @$ E
  2. exp(x)' _& m( k; y" s: c! b5 a1 S
  3. Return e raised to the power of x.
    3 U8 o; c2 q- u9 L5 N1 g
  4. $ \( h# E8 f. {5 w' k* d4 U+ m) g
  5. >>> math.exp(1): l9 L. L1 P% _$ J: }
  6. 2.718281828459045! r) x# i/ |% r. t( Y- K
  7. >>> math.exp(2)
    ; \# x7 w) Q: ^5 Y4 ]( i3 E9 T
  8. 7.38905609893065! D& S; L! z  o  l
  9. >>> math.exp(3)% X3 X; ?/ l9 N  w0 t9 u
  10. 20.085536923187668
复制代码
2 f+ v$ j, `5 S# S
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
9 F7 K. q7 C, U: w9 E
  1. #返回math.e的x(其值为2.71828)次方的值减1( @0 ]4 _( e" A* }8 e5 U# y
  2. expm1(x)
    : L6 c, C( g  c' R/ Q! y, N7 Z
  3. Return exp(x)-1.: G- A# S4 _; D  q, P5 U
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    3 I6 A& m+ N% k. p' e! a
  5. " p. i( r9 ^1 I0 k; F
  6. >>> math.expm1(1)
    $ G- n0 P' [$ w  A
  7. 1.718281828459045. ~# _# X5 n, p
  8. >>> math.expm1(2)% ]6 P* d" T! ^6 [% M6 P; ^
  9. 6.38905609893065" y8 X+ v+ c/ `. x" b
  10. >>> math.expm1(3)9 M' u. I$ L; `. @: O; j2 O* ]
  11. 19.085536923187668
复制代码

- }, X* |# U" R: C, {; hmath.fabs(x)  返回x的绝对值
* v) A6 W8 _. E8 ?5 j  p
  1. #返回x的绝对值
    - ^4 p5 s1 P6 J; T. \
  2. fabs(x)9 Z/ ~6 |. r: x# U& T* W: {
  3. Return the absolute value of the float x.
    . n& l- q) `; C. X. ^

  4. & P; U- N+ E9 ]$ x7 X. I
  5. >>> math.fabs(-0.003)9 \$ Q' p/ c% \& G/ I3 @3 T8 Q
  6. 0.003) B5 o( I. _7 H5 \0 W$ C( ^
  7. >>> math.fabs(-110)
    % v; W9 d7 p: Z) n& h0 P3 a
  8. 110.0
    1 e6 H1 j8 L$ ~& b2 M
  9. >>> math.fabs(100)
    + P3 x7 |) N& u0 \
  10. 100.0
复制代码

. Z1 C3 v6 S% j% G9 ~math.factorial(x)  取x的阶乘的值
9 y+ g. i" ~/ z. h! |
  1. #取x的阶乘的值
    . Q* @& K( Q+ ^
  2. factorial(x) -> Integral" h2 O. v+ _7 Z1 ]( U, c$ L
  3. Find x!. Raise a ValueError if x is negative or non-integral.: `8 S. J/ E0 I
  4. >>> math.factorial(1)
    ( y6 N3 V( T; M- X
  5. 11 R/ R. n/ A5 t; E+ g
  6. >>> math.factorial(2)8 p" B( q& M$ r1 u5 }
  7. 2
    & L5 L0 b5 D; N
  8. >>> math.factorial(3)2 ?. ?4 |- h0 Q7 \
  9. 6
    * f3 C) @6 f: _, V) q+ B
  10. >>> math.factorial(5)
    ) i' f2 g- C9 U5 S
  11. 120
    9 c& b" Y) U+ p7 W$ d
  12. >>> math.factorial(10)+ S" ?* n8 v, S- s& I* O
  13. 3628800
复制代码
7 p& \5 I. b5 s2 |& M
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数0 p( j  n; P# c' N
  1. #得到x/y的余数,其值是一个浮点数
    ; P8 l- ]( N' s) m1 b$ V7 A
  2. fmod(x, y)
    : p8 O. e$ G% L  T+ Y( \- k) w
  3. Return fmod(x, y), according to platform C.  x % y may differ./ g; Y: B7 k4 G0 h
  4. >>> math.fmod(20,3)3 Y% x3 Z0 D2 O: Y' S( Z2 a1 ~9 j8 ]
  5. 2.0" W- ~6 V( `/ n8 o6 E
  6. >>> math.fmod(20,7)3 s8 ]  V1 c' v# l, {( r
  7. 6.0
复制代码

. d4 I; M5 L' Z: i# F4 ^math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 N- D: X" Y% t: E) B
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ) Z0 \$ f" X1 w% ^6 M
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ; u$ U9 |& O/ o+ @, o& |* @
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1  w) ?+ c6 M1 ^, B
  4. frexp(x)* ~1 _$ g0 Y; a8 i
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 d, K% q# K$ U2 A2 j8 O
  6. m is a float and e is an int, such that x = m * 2.**e.
    7 J  M$ _' E3 u
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0., V' i' G+ g8 _) R/ X; n0 k
  8. >>> math.frexp(10)1 j2 o2 l$ Q7 `; b& B
  9. (0.625, 4)6 T- t1 F2 U" g7 c" F
  10. >>> math.frexp(75)
    : Z* r( M% Q% a' \, G* f
  11. (0.5859375, 7): n9 G  I9 N9 L% Z  C
  12. >>> math.frexp(-40)0 D( q2 O) M' Z/ W
  13. (-0.625, 6)5 K; Q8 `( v) g# s- o
  14. >>> math.frexp(-100)
    ( j: B1 ~) W" L
  15. (-0.78125, 7)! D7 K" @' f; ^  ]
  16. >>> math.frexp(100)
    % q, q. A0 S) K, k3 L& F. y
  17. (0.78125, 7)
复制代码
, E$ u" M, r9 i' w" d
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
: w; n3 y# S# E* F
  1. #对迭代器里的每个元素进行求和操作
    5 S" k3 C1 u% P2 r7 @6 C3 V# A
  2. fsum(iterable)( c" j! m6 ]4 c* [
  3. Return an accurate floating point sum of values in the iterable." C) Y- r7 ^: |- }/ {1 W- B( g  {
  4. Assumes IEEE-754 floating point arithmetic.
    8 ^" }" s  b; N0 }! o5 T& Y
  5. >>> math.fsum([1,2,3,4])
    ( P! @$ b- n( Y8 H8 O) x
  6. 10.0+ C% t  Z; E" s4 @; O/ v
  7. >>> math.fsum((1,2,3,4))+ l% i6 z3 M5 `, ^
  8. 10.08 n# `! S) p, v2 O
  9. >>> math.fsum((-1,-2,-3,-4))3 A! B4 \& K8 K7 K0 R
  10. -10.0# A: o7 _9 K- ^7 l  W0 @: h
  11. >>> math.fsum([-1,-2,-3,-4])
      b4 {% G. F; z
  12. -10.0
复制代码
# ?0 O  `% i. [: L) k5 V. E
math.gcd(x,y)  返回x和y的最大公约数9 W1 Q* {: V% z1 d" k' T
  1. #返回x和y的最大公约数. _+ H8 q5 V% `) x3 R- D# u
  2. gcd(x, y) -> int
    " X7 J: j7 }1 a- V( r
  3. greatest common divisor of x and y
    ; E* L, i4 t& G: @) k7 w7 V
  4. >>> math.gcd(8,6)& z) y# `2 K$ w5 q' v
  5. 2
    ) U, P! @4 q, M. v9 s
  6. >>> math.gcd(40,20)
    2 {+ j; P) U+ v& W+ B
  7. 20' Z0 U1 [) ]  h
  8. >>> math.gcd(8,12)
      _- }0 \; |* q. H2 ?3 A7 W% o
  9. 4
复制代码

' F( ~6 x7 C9 }3 z2 omath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False- q2 L+ u. g! b( B9 H7 S6 W* ~
  1. #得到(x**2+y**2),平方的值
    5 ~% v. I6 j. c  I# W) [  D6 Y
  2. hypot(x, y)6 K- p; l! ^# |& d; d2 U! ^
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    2 t) G* j$ m" Z7 @5 g$ O+ t% I6 I
  4. >>> math.hypot(3,4)) g! `- B& V1 f* D
  5. 5.0
    8 L4 @$ [( C+ ^; M2 Z1 l% E+ ~
  6. >>> math.hypot(6,8)
    : A, F% N" R- w  a( x5 e, e
  7. 10.0
复制代码
& o: X( u! ]$ [$ l* d, E
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 w% }( t% g4 x5 Y
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    . |1 m# C$ n# F
  2. isfinite(x) -> bool
    * e$ Y$ N7 N8 I5 w& o0 V) R
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    # V2 x% {' d- A' M- P0 @
  4. >>> math.isfinite(100)
    3 H6 O, b1 K5 T3 l0 F
  5. True
    $ f5 b: h( z8 r6 j
  6. >>> math.isfinite(0)  @" m  r& p9 s
  7. True9 q. i  j6 R, r; V
  8. >>> math.isfinite(0.1)) J( _/ ~5 j  N" S4 {
  9. True% {) l& m9 D1 e
  10. >>> math.isfinite("a")
    ; b( A% y, g. t
  11. >>> math.isfinite(0.0001)
    / ], e, i  j, H, P
  12. True
复制代码

1 H" t1 ^* v: W; F+ T3 zmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False5 V2 S7 P  L6 `# b! R$ E$ h8 K
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    : u* \0 {8 M7 d  p
  2. isinf(x) -> bool) l$ {  R% @. t5 }3 i6 y
  3. Return True if x is a positive or negative infinity, and False otherwise.
    : S/ f% h  z/ @
  4. >>> math.isinf(234); p! }1 e8 T+ J( y: l( K2 z3 h
  5. False
    * S8 @# C; V+ H3 z* C
  6. >>> math.isinf(0.1)
    # d, w3 v5 e+ w: {
  7. False
复制代码

. h% R9 B' |8 W7 d& w2 gmath.isnan(x)  如果x不是数字True,否则返回False/ i* L9 N" [$ T: f1 ]
  1. #如果x不是数字True,否则返回False
    9 T- ~/ S+ i3 s5 G6 |
  2. isnan(x) -> bool
    ) H6 J" Z% f. P$ T8 Q  J, R
  3. Return True if x is a NaN (not a number), and False otherwise.
    4 ^% x* o; O1 G/ D
  4. >>> math.isnan(23)9 j! e& }) r4 D4 ~9 ~2 ?- ~
  5. False
    ' I4 n) @4 a7 l9 s: M1 T
  6. >>> math.isnan(0.01)9 e" |& U# c' l6 r( S! J
  7. False
复制代码

9 l& P4 A9 }- z. Nmath.ldexp(x,i)  返回x*(2**i)的值1 U  L2 O2 f* N  \0 U% K
  1. #返回x*(2**i)的值
    ! e- l( \! J9 N  U
  2. ldexp(x, i)
    ' |6 B) x, v3 l. ^, m
  3. Return x * (2**i).
    $ t1 W1 i0 k4 G. Z) ~$ A
  4. >>> math.ldexp(5,5)
    3 M3 p. I0 T' q4 d! K
  5. 160.0
    ; b+ U" ?2 h* I8 F7 d% }
  6. >>> math.ldexp(3,5)' T6 ^3 o$ Y9 N  t
  7. 96.0
复制代码
8 [4 ?2 D8 x" J1 T% M0 f. C3 k5 d
math.log10(x)  返回x的以10为底的对数" A' W  h. ^' @; Y  A3 Y8 T
  1. #返回x的以10为底的对数
    ) u# J& d: i" ]
  2. log10(x). X" N/ O2 T$ t! D7 T! E
  3. Return the base 10 logarithm of x.# _4 i/ N' g: x! |, i- i6 z
  4. >>> math.log10(10)+ f% H! u) {+ U- j/ x
  5. 1.0
    ; m. @' Q* [5 v' q/ Y( T
  6. >>> math.log10(100)) o* P3 i4 Q" W: @) P! ~6 C. |
  7. 2.0* I' n0 @: c; q
  8. #即10的1.3次方的结果为20" M( D+ g5 ?# j% R8 A! w
  9. >>> math.log10(20)
    * X6 }* X2 e# q; u
  10. 1.3010299956639813
复制代码
( x4 U" ?! _& g6 ]
math.log1p(x)  返回x+1的自然对数(基数为e)的值; F7 w$ p  g  ]: N
  1. #返回x+1的自然对数(基数为e)的值3 Q; W1 Y0 K0 X6 ]& C
  2. log1p(x)
    ) i$ u1 \# h! s& H: b! N3 L
  3. Return the natural logarithm of 1+x (base e).
    $ [$ P; L$ L4 N7 d) z" C
  4. The result is computed in a way which is accurate for x near zero.1 ^# \6 w/ W* N: \' J9 A& P3 U9 E
  5. >>> math.log(10)' r; D0 q" p  r( \: ]6 W- }+ w% B/ x  x
  6. 2.302585092994046
    0 W  w% K! ?, ^# L0 C
  7. >>> math.log1p(10)& e2 q; h* f( Y+ M2 }- X
  8. 2.39789527279837076 G9 {0 T! F7 m, }$ V9 w5 M/ J
  9. >>> math.log(11)1 _7 X) c+ ]3 R
  10. 2.3978952727983707
复制代码

# F1 r% u) \; V2 J4 J, y- tmath.log2(x)  返回x的基2对数# j/ Z) Z' z2 R# S! G5 f
  1. #返回x的基2对数
    ) X, \' p4 i7 r" M6 q2 l6 {
  2. log2(x)
    . s; B3 G: V7 S+ o2 @' n; i/ A
  3. Return the base 2 logarithm of x.' R, P# R$ J" f4 O4 k! M3 Z+ Y
  4. >>> math.log2(32)
    ' Z7 h& [# `; X4 G- f. i! l4 m
  5. 5.0
    5 J7 U5 w- K7 @" G
  6. >>> math.log2(20)
    ; Q6 X  O+ ?4 T( R
  7. 4.321928094887363) u) m4 \# \' r$ F8 O
  8. >>> math.log2(16); P9 S2 a  K. P8 x* u
  9. 4.0
复制代码
: v& {3 C( o2 T5 u# ~
math.modf(x)  返回由x的小数部分和整数部分组成的元组& }4 R0 J! v4 V+ g! S+ c  t
  1. #返回由x的小数部分和整数部分组成的元组/ b: D+ f: f/ x% ?) J
  2. modf(x)
    / l8 }4 z+ N) ~  W0 f+ X- z+ Y
  3. Return the fractional and integer parts of x.  Both results carry the sign
    5 l! O6 Q* a9 ~2 C. X) N
  4. of x and are floats.
    4 x8 p7 g. [0 R+ Y
  5. >>> math.modf(math.pi), T; [+ D( w, V3 f2 }7 i, ^- O" F
  6. (0.14159265358979312, 3.0)0 b7 T: O$ ]- c" l% q! B
  7. >>> math.modf(12.34)
      M4 I1 v) Z0 K! o1 O* ^
  8. (0.33999999999999986, 12.0)
复制代码

! W6 b5 |/ o' u$ x4 u* O- Omath.sqrt(x)  求x的平方根
" `. {/ t# J+ i+ Y  K8 U1 X7 s
  1. #求x的平方根
    ' S4 I/ H; c' b, `
  2. sqrt(x)
    ) P6 D. ~/ ^" P1 p% w
  3. Return the square root of x.4 c' e' ?9 ~, y1 O1 H
  4. >>> math.sqrt(100)9 V" P, b( z( K' A+ j
  5. 10.0+ y# x% ?+ |: \) E$ O$ R9 K8 {
  6. >>> math.sqrt(16)
    , D- S+ U( u: l
  7. 4.0- @5 j, u2 N- ]' u3 v8 q
  8. >>> math.sqrt(20)
    5 x5 n2 i. F' k! V# s9 }6 N
  9. 4.47213595499958
复制代码
" o& s) r, i1 {1 N
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分) W# A+ R' }9 U2 ]' w" D
  2. trunc(x:Real) -> Integral/ `1 [( v1 X  I; h# L' r& Y) X
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    # h* B- r& [$ i5 G; v
  4. >>> math.trunc(6.789)
    + v) A: S. V$ k7 l+ i7 K+ \% F
  5. 6
    & B8 C5 {2 B8 C" D
  6. >>> math.trunc(math.pi)3 G0 [" @1 Y/ Y3 p6 H7 J$ n  t
  7. 3
    0 X: W& {5 _! t6 [( L
  8. >>> math.trunc(2.567)' M1 A& G7 v7 u
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-20 13:13 , Processed in 0.076582 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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