新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1895|回复: 0

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

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

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

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

x

! H- f- }6 u; {* v: w7 z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。/ _( d9 Y# f8 P0 r: ]
$ I6 J* f7 g, {. ^5 g4 Z9 H+ |5 ^
方法1
5 ^8 l( A7 ?. o4 U  U, Y3 @
  1. >>> import math
    " i1 i2 b+ W8 I% o2 J% X$ b
  2. >>> math.sqrt(9)
    ) z- {( c( ^6 t/ G: D1 {7 Q
  3. 3.0
复制代码
方法2
2 ?; P/ C2 u$ u5 P% g$ i
  1. >>> from math import sqrt
    % h. q. K3 {6 y% M+ j3 Z0 F
  2. >>> sqrt(9)) H7 R: l9 q6 U, l  Y* w" n" O+ ^
  3. 3.0
复制代码
! H7 T& K7 k  @) \1 _

  ?; `- W8 ?: q% ~9 Q
math.e  表示一个常量# h5 `4 V; j5 _  M) n. D
  1. #表示一个常量% Y0 R. I; v/ M2 [
  2. >>> math.e
    9 B$ W& x% w4 J% P( I; W
  3. 2.718281828459045
复制代码
- j( c0 {0 F* O5 u6 D
math.pi  
数字常量,圆周率
* ?' b- D9 i7 }, @2 N3 y
  1. #数字常量,圆周率
    ( u& o' v3 D8 R8 h/ J3 M" c! h! H4 O
  2. >>> print(math.pi)' p! }# p/ Y9 c# l4 I- j& _" |
  3. 3.141592653589793
复制代码
9 k1 T' ^/ Q. y
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 W) @' u: L! K
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 K' A; Q4 Y8 \- c
  2. ceil(x)+ U+ \' m* T9 y3 G
  3. Return the ceiling of x as an int.  }% R$ v6 W- R; I2 |
  4. This is the smallest integral value >= x.) ~% m) ~7 f. V' X! Y

  5. ; X7 |) ?9 v  N4 ^/ I. [
  6. >>> math.ceil(4.01)
      O" U" n! {2 I8 d# s
  7. 5+ c: \' W$ \9 r
  8. >>> math.ceil(4.99)) B) F. j( Y9 c6 a+ L
  9. 5$ t8 l# T* f# f$ F% ~
  10. >>> math.ceil(-3.99)
    % k0 ^7 Y5 \$ K# Y' _- x
  11. -3
    # T4 }9 ]3 H, U2 J" r4 E
  12. >>> math.ceil(-3.01)& J8 g  u; `6 T. `
  13. -3
复制代码
. z. }/ z+ O: z
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; M, C+ V3 H/ o
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 B+ g* z0 I+ o' |
  2. floor(x)
    0 x% A1 J- t$ P& b
  3. Return the floor of x as an int.4 I9 m% R% g8 ~: t4 _
  4. This is the largest integral value <= x.
    ) ~5 g; ]$ Z+ Q. i' F# ~
  5. >>> math.floor(4.1)
    1 x3 ~; ]( a+ [) U- B
  6. 4* s* C7 r0 \8 V& C6 E+ [8 F7 K
  7. >>> math.floor(4.999)
    % A: G% M1 Y( Q2 I8 m
  8. 42 B6 s4 P2 d5 v- U- `
  9. >>> math.floor(-4.999)
    3 D- E. |) q7 x, H! D* c: t
  10. -5$ ^! P- @& i! M
  11. >>> math.floor(-4.01)
    $ ~1 R7 _' d# _+ B9 m
  12. -5
复制代码

' K& @% R2 n: P4 I$ \6 [1 q6 Qmath.pow(x,y)  返回x的y次方,即x**y+ t- ?4 U) M9 m) z5 K
  1. #返回x的y次方,即x**y- n* X' S) L9 l" q, t2 F  r
  2. pow(x, y)
    4 J1 ?% O; k& g& @9 l
  3. Return x**y (x to the power of y).1 }# L2 t0 ~9 F& V& k$ ^# ?" H
  4. >>> math.pow(3,4)7 J1 A4 N6 t1 d  \5 |! l
  5. 81.0
    ( k$ ]7 v7 W/ J- u0 s
  6. >>>
    $ }7 S- o, s: y2 b: z
  7. >>> math.pow(2,7)
    1 P; V9 M7 q8 |5 E; x+ V
  8. 128.0
复制代码

& t/ P0 I- J$ R% i& Q9 Z. {% T- e& fmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' A' N0 y- h: G6 P
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 i. J7 p4 e9 w
  2. log(x[, base]). u1 H' v# {; \5 C
  3. Return the logarithm of x to the given base.% X2 E- B# M2 H4 O$ w6 R
  4. If the base not specified, returns the natural logarithm (base e) of x.; n2 q/ A" R# U8 i; b# o$ Z
  5. >>> math.log(10)! c" E$ K9 O& y. _' u* X& s
  6. 2.302585092994046
    6 r0 E( t: V4 }* g+ m
  7. >>> math.log(11)
    " `! x; {* O& Y$ ^
  8. 2.39789527279837070 }$ h1 |, x# F& K2 U6 V
  9. >>> math.log(20)7 Z3 X# G3 O/ N% z0 o  z( h0 E
  10. 2.995732273553991
复制代码

- W' r+ s& h% }math.sin(x)  求x(x为弧度)的正弦值
/ V$ O- _2 G# C: G# Q
  1. #求x(x为弧度)的正弦值% a) n( |/ z+ z) f4 L& g
  2. sin(x)  o7 Z8 s) ^, _: W$ I  A- y
  3. Return the sine of x (measured in radians).* Q4 q0 H. {$ e
  4. >>> math.sin(math.pi/4)2 T$ F! H2 N- v+ K7 P' I
  5. 0.70710678118654751 E: n. D% Q3 ?* l) Q
  6. >>> math.sin(math.pi/2)% m* {! {* R" S  P  x) P4 t2 V% B
  7. 1.0% D3 M- t8 P8 }! l) }
  8. >>> math.sin(math.pi/3)- O; f+ }7 v, W" N0 J4 i
  9. 0.8660254037844386
复制代码
- {0 n, I( B) i5 w) s- M
math.cos(x)  求x的余弦,x必须是弧度% a0 n7 h/ m6 P, Y  r
  1. #求x的余弦,x必须是弧度3 V" y$ _' p3 x$ ~
  2. cos(x)0 M/ O' U; [0 u7 A$ Y
  3. Return the cosine of x (measured in radians).
    2 U# P( n, B9 B: {) v
  4. #math.pi/4表示弧度,转换成角度为45度) v# A4 ]* h) d0 ^
  5. >>> math.cos(math.pi/4)9 k! h6 U, ]2 O, B' Q' v' J
  6. 0.7071067811865476) r2 a' F* ~; j( r  f- e
  7. math.pi/3表示弧度,转换成角度为60度
    1 K* E' g1 Z$ y# S7 ]
  8. >>> math.cos(math.pi/3)" d* A: H# [7 N% i, {4 ^
  9. 0.50000000000000012 F0 Y% c1 [- U, U4 L
  10. math.pi/6表示弧度,转换成角度为30度
    + r9 D4 ?+ I( ?% b
  11. >>> math.cos(math.pi/6)& L, G/ r5 j" W% |
  12. 0.8660254037844387
复制代码
; q* s4 ~1 m. Y( M" d
math.tan(x)  返回x(x为弧度)的正切值5 s6 e$ M, |- u! G" I1 z. I
  1. #返回x(x为弧度)的正切值7 h( j9 d8 G+ _
  2. tan(x)# D: T! J) N7 E& U" ?. N
  3. Return the tangent of x (measured in radians).$ f+ C* c7 o/ F5 r9 g( A; I
  4. >>> math.tan(math.pi/4)* Q0 S6 J7 L  E- X7 |% w
  5. 0.9999999999999999
      t) Y6 ^% K. I1 g
  6. >>> math.tan(math.pi/6)
    4 Z& q8 _( C6 L* N; P
  7. 0.5773502691896257. O4 R# X  u1 f5 q% {
  8. >>> math.tan(math.pi/3)3 c- ^0 Q; p- [+ Q/ l; N; ~
  9. 1.7320508075688767
复制代码
8 t) G7 u$ d, E0 l, z, O
math.degrees(x)  把x从弧度转换成角度
: H: b# w. d4 w. N/ E1 a, Y
  1. #把x从弧度转换成角度
    # T; j( f0 a4 H( x
  2. degrees(x)# F( I% C. d+ c. a- E) R1 P: a
  3. Convert angle x from radians to degrees.
    / J7 ]6 E' A% f$ v4 ~: s3 T

  4. ! e1 o/ D, r. G5 ?& v. J
  5. >>> math.degrees(math.pi/4): Y; }1 o0 d9 }/ l1 I, M) J
  6. 45.0
    + Z$ X; C. j+ e. @
  7. >>> math.degrees(math.pi)
      A8 z1 s% L5 r- i
  8. 180.0
    9 M5 a6 Y( W& j
  9. >>> math.degrees(math.pi/6)7 i% N# E. y5 M8 x) i* K( c
  10. 29.999999999999996- l5 d+ P! e& q/ l
  11. >>> math.degrees(math.pi/3)+ x* c+ w; m  `3 r
  12. 59.99999999999999
复制代码
! o$ f" }5 l) ^$ k$ H7 S- Q% m
math.radians(x)  把角度x转换成弧度
8 N; \4 a* P* j0 ]1 L( J( N4 r
  1. #把角度x转换成弧度6 g/ ~$ p7 R2 |" K8 K  n
  2. radians(x), }' @9 F& H, t# a7 y0 O! s5 o
  3. Convert angle x from degrees to radians.
    * }0 v. K: c$ {& p2 l% x, c/ X
  4. >>> math.radians(45)) w! t! n7 h4 ~
  5. 0.7853981633974483
    3 D* u; J# X% t* C1 m
  6. >>> math.radians(60)  O: O2 J8 j3 P0 X. }
  7. 1.0471975511965976
复制代码

/ Q, \3 D6 U, {; d) r; R, E, E7 Lmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
. u# t3 ~4 S& p3 m
  1. #把y的正负号加到x前面,可以使用03 d# E) w5 v  @* E' N
  2. copysign(x, y)
    # {# D8 X6 K) V8 Z
  3. Return a float with the magnitude (absolute value) of x but the sign
    . g5 {3 U, _# n3 ]6 }6 |
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & [9 @+ \5 {+ i9 K
  5. returns -1.0.6 {9 A+ v$ @. J4 B" V; ]# S! j

  6. 0 t/ p, A3 P) n0 T. F
  7. >>> math.copysign(2,3)
    + j4 L% l# E' y, I
  8. 2.00 Z  N6 r. U  y9 j, @$ |% a
  9. >>> math.copysign(2,-3)4 p- K  R1 D7 T6 h4 c
  10. -2.02 H: a+ @5 u! |& q
  11. >>> math.copysign(3,8)& d& x( ]' l/ U, m
  12. 3.05 H1 p6 e8 w, j' E* ~1 ?! [
  13. >>> math.copysign(3,-8)
    & x+ L, n. R- B& O2 Y  Y
  14. -3.0
复制代码

) A) }  l5 N/ }* o8 e3 r3 rmath.exp(x)  返回math.e,也就是2.71828的x次方/ a. N6 m5 b- S1 h% \" z% k. r
  1. #返回math.e,也就是2.71828的x次方; ]: Z, f3 y1 K2 {! S- H9 S. v
  2. exp(x)3 f! k1 j; p$ S" S6 {
  3. Return e raised to the power of x.5 C" ^1 ]9 ?3 Z
  4. 2 ]  ?6 i! v+ ~; u4 b4 ]
  5. >>> math.exp(1)
    ! M: u! O1 [9 E1 j. b/ V; Q
  6. 2.718281828459045
    $ J/ C) l, x4 v2 k! j
  7. >>> math.exp(2)7 e! E% x4 k2 j+ b
  8. 7.389056098930650 Y6 b; ]! s& k: @! H4 I; G
  9. >>> math.exp(3)
    1 R7 K( F  G. L6 R3 Z! H
  10. 20.085536923187668
复制代码

4 a3 p7 y  q4 E- v- zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
( O" w8 K( o' [- x$ t8 U% v
  1. #返回math.e的x(其值为2.71828)次方的值减19 m  |: A& P/ @& B/ o/ ^: Q5 \
  2. expm1(x)
    # x! t' Z: K1 ~- M  B
  3. Return exp(x)-1.! i; N0 ^9 J% S/ w. |$ M$ f0 P
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 F! n; T- l% ]

  5. . H( b7 q/ ~& v  j9 d
  6. >>> math.expm1(1)
    " r3 Q0 w* `1 [! T1 m
  7. 1.718281828459045( e2 c) t5 K8 C$ o9 [! y
  8. >>> math.expm1(2)+ ?7 r  [0 H$ Y" c( K9 v7 k
  9. 6.38905609893065
    0 t$ a0 g- u: m/ a; K; Q
  10. >>> math.expm1(3)8 h# A& r% }% I
  11. 19.085536923187668
复制代码

" V1 z3 G5 `" ^; S% E$ omath.fabs(x)  返回x的绝对值
5 c; z& p) Y" l
  1. #返回x的绝对值: W* j9 }" u' G. O% y7 H" ?! W3 K
  2. fabs(x). r8 F$ F+ B: g( [
  3. Return the absolute value of the float x.2 D, l( R0 P5 O4 b: h

  4. ) N3 }& X; @: P' j+ x2 w
  5. >>> math.fabs(-0.003)5 n6 c' g+ ]( [2 ?
  6. 0.003
    0 o/ z8 f1 D$ k; o
  7. >>> math.fabs(-110)
    9 e" i5 }3 u' e. ]5 q1 }8 }2 h% E
  8. 110.0" B5 E$ @" k: {/ b. I
  9. >>> math.fabs(100)
    5 z: t, W, O4 D- r# a$ B- P( s/ F
  10. 100.0
复制代码

( _( H! ]8 O( }3 ymath.factorial(x)  取x的阶乘的值
, h8 _5 @& T' S  z+ W
  1. #取x的阶乘的值% W7 |$ P: V$ @  N
  2. factorial(x) -> Integral; m2 D3 q/ G+ ~$ t* ?! i8 }
  3. Find x!. Raise a ValueError if x is negative or non-integral.+ ^4 y! e5 Z  `) U8 w8 \) k( Q) P$ r1 U
  4. >>> math.factorial(1)
    8 e8 M9 b* x5 Q- l
  5. 1
    7 [7 T$ r* e8 o7 M+ T6 {) k: n& G9 i
  6. >>> math.factorial(2)
    ) x! Z6 L1 i, k9 ]* q. v3 a* P* t+ ~
  7. 2, }4 a& K- c. n& Z! C' _
  8. >>> math.factorial(3)7 w; J9 P# w1 k# y2 y7 g% X
  9. 6
    ( d' N* h% q* Y, R0 S; j
  10. >>> math.factorial(5)
    ' A" `( \+ x* O0 A: u  D6 @
  11. 1209 Z1 i5 S+ y- m9 Z
  12. >>> math.factorial(10)/ W+ S' Q  u% v* P1 f! y( d7 C% A
  13. 3628800
复制代码

% }) u8 e) [: Z  O( ^" A1 \' ^math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
4 S( f1 Y7 n" ?4 U9 V! E1 z5 [
  1. #得到x/y的余数,其值是一个浮点数
    * @" S( a9 g$ k5 T6 z
  2. fmod(x, y)! M' I  h! D* H' S3 x) u
  3. Return fmod(x, y), according to platform C.  x % y may differ.3 a: x& K$ t# S( v3 i
  4. >>> math.fmod(20,3), ^3 J9 C3 M- v0 u
  5. 2.0" Q& [3 j4 J6 Y/ ^8 P8 G, ^  m2 h
  6. >>> math.fmod(20,7)
    " }- ?( s& s( C
  7. 6.0
复制代码
* A( o1 {: \- S) O- v7 F
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" M6 ?- T, \9 ]
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. F% a2 W- e8 Z8 j7 \
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ' f" @( |6 y5 X
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ) K8 H+ E1 U+ |( ^" E$ ^! |
  4. frexp(x)
    4 [& z/ E0 ~# Q
  5. Return the mantissa and exponent of x, as pair (m, e).! c, o% I, T% F7 }( Z4 ^
  6. m is a float and e is an int, such that x = m * 2.**e.
    4 Y( N  L: C* u! u  R
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 B- `9 `% B2 n8 M( M% q
  8. >>> math.frexp(10)* y5 _+ C- l, ^9 `$ U3 A
  9. (0.625, 4)
    + @+ Q. U, G3 i% q) l
  10. >>> math.frexp(75)0 n! K) N" ^" L# |
  11. (0.5859375, 7)
    ! D' ?" P" y" n  y+ f: M0 {. q; `' L
  12. >>> math.frexp(-40)
    1 @' O" g- ^+ F* V. n
  13. (-0.625, 6)
    4 d' e0 X8 ~6 a0 e, t
  14. >>> math.frexp(-100)
    $ Y. w$ ~0 q# S( j# \. n- w( t* p
  15. (-0.78125, 7)! |: P" h1 Z7 X  U% t& G
  16. >>> math.frexp(100)' R* B9 C! g3 ]
  17. (0.78125, 7)
复制代码
2 W$ k9 F  r( z. u0 Q6 m; e+ [. E( h
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列2 m1 k. U" X3 z8 _
  1. #对迭代器里的每个元素进行求和操作" m# W0 p; V* ~/ A* D' p) Z
  2. fsum(iterable)
    + r6 _7 p/ w2 D6 b6 u
  3. Return an accurate floating point sum of values in the iterable.! H: W/ l1 g, r
  4. Assumes IEEE-754 floating point arithmetic.9 d( N1 _/ P) }# J
  5. >>> math.fsum([1,2,3,4])3 F+ l2 V  x8 A5 D  B5 m
  6. 10.0
    2 M6 V; W% S. w% j
  7. >>> math.fsum((1,2,3,4)): w) L- B% M" g4 O8 v( Q* q4 l* x9 I
  8. 10.0
    ) a0 ?2 Y# Q2 b* q: N2 h  v
  9. >>> math.fsum((-1,-2,-3,-4))
    ' p7 e5 d2 b" O8 ?' X: O: |/ b) ^# T
  10. -10.0: W$ s1 f2 T+ ?! Y6 j7 Y
  11. >>> math.fsum([-1,-2,-3,-4])
    3 Y. B$ D5 K* m) ]: w8 [
  12. -10.0
复制代码
6 x  l3 n2 t+ Z9 C
math.gcd(x,y)  返回x和y的最大公约数2 ]; t8 v3 s/ y# `" @$ o! i
  1. #返回x和y的最大公约数
    . }5 F9 Z+ P5 C
  2. gcd(x, y) -> int3 V# G. U9 Q6 u4 v" x+ v' |" V
  3. greatest common divisor of x and y5 s2 {; v+ T! j, O! m
  4. >>> math.gcd(8,6)
    4 ?, e- g- T) W0 Q1 E
  5. 2
    2 _1 d3 x6 ^+ A5 w2 u( V4 V' C8 h
  6. >>> math.gcd(40,20)
    5 t6 z! O5 s% _) F) V' v% i- Y
  7. 20
    2 I. D  Z" z' z0 _2 t
  8. >>> math.gcd(8,12)
    8 v1 u$ l9 {8 f
  9. 4
复制代码
- s) K+ g% T0 i5 r" o
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
' m5 s; \/ h6 O. ~4 w
  1. #得到(x**2+y**2),平方的值
    ' w) c+ A9 k1 s8 y; g  b2 J
  2. hypot(x, y)% T& v) [2 y+ i' }: b2 o
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    1 c: a! v" P  ]& G# i6 w
  4. >>> math.hypot(3,4)
    * ]; B+ v& z2 F) b  f
  5. 5.0
    7 F- h& e0 r8 ~5 E* m
  6. >>> math.hypot(6,8)
    1 W. L5 v# L6 g3 `2 @
  7. 10.0
复制代码

0 O+ o  s: g( c* i1 x! l+ wmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False6 @0 Z; R% i9 z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False5 ^; y, c3 |- u3 v+ E! h0 F4 u* d5 ^  P
  2. isfinite(x) -> bool) b# N& q9 u4 M3 r
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! I: O8 L6 n: |8 X
  4. >>> math.isfinite(100)7 z( ^2 J/ g; W$ E9 w' N. E) W) [
  5. True
    % X& x+ ~" t0 L! p" y- s4 [
  6. >>> math.isfinite(0)$ @- u) s" j: T- o' w# T5 v
  7. True
    : w8 X. |4 W! j) Q" z4 f
  8. >>> math.isfinite(0.1)  L! n9 X( ]5 L% z: E
  9. True
    " y5 P! P* t5 N1 j3 V2 B  O
  10. >>> math.isfinite("a")) j# k2 ~$ I) D6 X: t0 q  D9 a% m
  11. >>> math.isfinite(0.0001)* @& b! V; a0 A: Y5 A
  12. True
复制代码

! l& B$ b& {3 X' ymath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
# w& c- b4 K! ]9 u* |
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ' s' W3 e& M% ^, J+ d9 ]" Z
  2. isinf(x) -> bool
    + y) u. y9 n2 w3 R$ d; J& O8 O+ N; W8 @
  3. Return True if x is a positive or negative infinity, and False otherwise.0 e: H* e) {2 Z
  4. >>> math.isinf(234)
    ) {9 s+ r- ~. C' e
  5. False
    6 f7 S7 V, y( W
  6. >>> math.isinf(0.1)
    " v, n* |6 B% u6 S8 h) Q
  7. False
复制代码

9 a: m6 H, b' E0 @math.isnan(x)  如果x不是数字True,否则返回False( c* K" m. b& U7 ?& b+ Q
  1. #如果x不是数字True,否则返回False
    " F- d( z" R7 l8 ]
  2. isnan(x) -> bool4 I/ v. X  ~3 k: L6 A( W+ X. q, j
  3. Return True if x is a NaN (not a number), and False otherwise.
    : b+ E1 m6 p) k2 t
  4. >>> math.isnan(23)
    1 \1 A# _- {" T0 `
  5. False$ B0 |/ D; Z8 i: M0 [- ^4 m! x( a/ R+ b
  6. >>> math.isnan(0.01)/ t% c1 {- n6 ?) f/ A$ O
  7. False
复制代码
, j: x/ ~7 S4 E; J: c
math.ldexp(x,i)  返回x*(2**i)的值
0 \  S2 Q7 m) e% u, T
  1. #返回x*(2**i)的值
    # F4 p" f" a5 ?/ g/ N8 o: U' K) b) i  H6 P
  2. ldexp(x, i)
    + V0 I2 y2 a  f7 Z# f, h
  3. Return x * (2**i).
    / m  o' E7 q2 f& ~6 u+ g
  4. >>> math.ldexp(5,5)
    . h/ H# X( W, s% O; W7 L8 V3 s
  5. 160.00 w3 h* c5 H, Q3 m0 }" M5 V9 Q
  6. >>> math.ldexp(3,5)
    2 k' E0 x; G& R
  7. 96.0
复制代码
. e$ `& }- H6 o0 n9 v* Q
math.log10(x)  返回x的以10为底的对数8 X: {2 t1 e7 f& ]8 x" Q
  1. #返回x的以10为底的对数
    4 @" v( i- u& t( b+ ?  \7 z
  2. log10(x)
    6 O! `7 P( e; h% ]4 S
  3. Return the base 10 logarithm of x.& J0 h( P7 u$ G- O& }7 i4 U- Q  \, ]
  4. >>> math.log10(10)
    # G' B! }8 L( ~9 u* u1 y
  5. 1.0; H+ S" E9 w* J: Q
  6. >>> math.log10(100)6 Z- g7 W6 a# @" H
  7. 2.0% {; a. x" A0 e' x
  8. #即10的1.3次方的结果为20
    # L' h" z6 z9 S5 k' ^3 P
  9. >>> math.log10(20)
    ) w' P" y; c8 A3 N3 i
  10. 1.3010299956639813
复制代码
- f; A# l, W8 j4 G
math.log1p(x)  返回x+1的自然对数(基数为e)的值& ]& y0 ~- Z! j! p% w, A* }: ?" k
  1. #返回x+1的自然对数(基数为e)的值
    ) E4 S% A' c# w7 h2 i9 B" S
  2. log1p(x)
    $ `; ]+ C/ u+ O! S
  3. Return the natural logarithm of 1+x (base e)." S/ d2 n2 g; S" z
  4. The result is computed in a way which is accurate for x near zero.
    ' w3 ?2 F) V: h) M6 G6 ~) g
  5. >>> math.log(10)
    6 B* p* A5 a' y4 q) y  ]( o1 a" ^
  6. 2.302585092994046& W8 ^# a; @; v5 H* M! J  E* l) u
  7. >>> math.log1p(10)% p# ~. V4 O/ {+ P
  8. 2.39789527279837073 u- b1 z% M+ N" {
  9. >>> math.log(11)
    4 R' n1 r+ ?- c7 W
  10. 2.3978952727983707
复制代码
* r9 @8 J- p" u) H. H$ o
math.log2(x)  返回x的基2对数
! s0 o- e; t! c, z4 i1 {1 R
  1. #返回x的基2对数
    & w- S: j$ Y) ]1 i* A& |& c
  2. log2(x)  P$ G' ?) Y7 l" o
  3. Return the base 2 logarithm of x.
      x, Q, k9 \, O5 r' x
  4. >>> math.log2(32), ?3 ~  q! }! y* U2 `7 q+ Y& L
  5. 5.0
    ! v  [7 w" F# _7 j! w8 U$ f7 s; b
  6. >>> math.log2(20)' J8 B. V% J$ ~
  7. 4.3219280948873633 O2 D1 {4 S7 }  j, |
  8. >>> math.log2(16)
    ) C" B7 f  \+ _8 k; r0 [9 r! @7 j/ d( ~
  9. 4.0
复制代码

- q7 A! [5 p6 a7 {$ `6 D4 \3 Imath.modf(x)  返回由x的小数部分和整数部分组成的元组; P+ J' [. j& H
  1. #返回由x的小数部分和整数部分组成的元组
    8 C/ o& Z) I, {( z) t7 Q1 G
  2. modf(x)! R5 w) H9 {" g; N* ~" q
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ) d6 t* q/ c( V- p4 o, z
  4. of x and are floats.) v, D- s! {4 z
  5. >>> math.modf(math.pi)
    7 P& n- }( |4 O- m8 |9 F( L% s3 G
  6. (0.14159265358979312, 3.0)
    5 D1 ~0 y$ f% G/ q+ |, d
  7. >>> math.modf(12.34)
    1 ]! T1 ?1 w3 c9 \! m5 b% P
  8. (0.33999999999999986, 12.0)
复制代码

* W9 d2 I* S$ {& z6 z; Wmath.sqrt(x)  求x的平方根$ X. g5 ]4 S+ m" o( h. E4 H$ u
  1. #求x的平方根
    $ {6 h' y1 S' N: X4 O. `: z
  2. sqrt(x)+ i; u" v6 L! m+ a2 m8 o
  3. Return the square root of x./ t% m$ y# W, H! m- C3 O  ?" r. I' \* t
  4. >>> math.sqrt(100)
    9 Z+ S4 `; j2 H3 Y2 _
  5. 10.0! N3 q% T& Y. K# T. j
  6. >>> math.sqrt(16)4 e. N# w( y& Q% n
  7. 4.0
    9 @7 B, P, j6 O1 ?1 _# Q4 c
  8. >>> math.sqrt(20)' z3 b4 p( ^5 ~
  9. 4.47213595499958
复制代码
( Z. y7 \1 s3 I5 |
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    & d, c& x+ C7 W: l! N
  2. trunc(x:Real) -> Integral/ o+ F4 [% W/ }; \7 p/ Q6 T4 W
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# @( h: ^3 \2 O% ?
  4. >>> math.trunc(6.789)( d4 l0 p* o( t0 _$ F
  5. 6
    ! P% \% u" G4 r- o
  6. >>> math.trunc(math.pi)
    . |8 N, E8 c' I! i3 o
  7. 3# I: L  x( ]. v1 D# I& c- d
  8. >>> math.trunc(2.567)$ m8 z4 \3 B+ ^4 r
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-3 12:00 , Processed in 0.086836 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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