新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1854|回复: 0

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

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

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

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

x
  h' p8 y( ]4 m& i' ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。( u* h4 D+ Q. P% k1 ]6 j# t
( G5 N' m/ P$ `4 L6 h
方法1$ [4 Z2 D' |, v  c4 S$ l
  1. >>> import math6 z0 y" G: ^7 {) n2 \
  2. >>> math.sqrt(9)
    ! S# l. G$ h" P8 f9 b' p
  3. 3.0
复制代码
方法2  o" f5 P$ s6 d. c
  1. >>> from math import sqrt
    $ Z" @0 G9 E  X6 }6 t1 T
  2. >>> sqrt(9)" ]* f+ U6 u" n9 T0 ^: c( y, c2 }
  3. 3.0
复制代码
  N( _' {# p; R; I3 X, m; g. M


4 {5 j9 Q4 `/ d+ G1 K( X4 J0 l0 A
math.e  表示一个常量: f6 j7 S/ f# Y( {
  1. #表示一个常量
    4 R6 @, C4 u4 U6 W2 @! a
  2. >>> math.e
    0 ?) y% D, s% ^
  3. 2.718281828459045
复制代码

) @3 T9 g: [0 m  K! h2 omath.pi  
数字常量,圆周率
+ H: O4 [: s1 ]6 @
  1. #数字常量,圆周率
    # r0 t: N. Q* e& f! K7 x) P: u
  2. >>> print(math.pi)
    + K; @! y5 `" E4 l7 ^  F
  3. 3.141592653589793
复制代码
1 X( b* n# i! ~8 ?; {
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

! V7 @6 b% X# F3 v; r& {
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    # I+ m/ z8 B! t; N$ \
  2. ceil(x)
    2 q  F# F# M; {: {. }) ~
  3. Return the ceiling of x as an int.9 C" b- p2 h; D" u$ Q+ O( I4 m
  4. This is the smallest integral value >= x.
    . y/ ?$ I- c4 }; @
  5. 6 ^2 C& E6 c2 y5 V  t- t  u" h
  6. >>> math.ceil(4.01). J7 Z% j9 n7 V: e
  7. 5
    " l" }0 r: L  o7 u0 Q1 I
  8. >>> math.ceil(4.99)7 l3 }* q$ z4 }2 i
  9. 5
    5 o$ G0 I# p( W9 Z/ b) R( I- {
  10. >>> math.ceil(-3.99)- A' {  n# k% e: y' R7 y' Y
  11. -3
    $ ~( d: m+ |, ~" E6 v% ?& A
  12. >>> math.ceil(-3.01)
    % f+ `* P% r' p( e1 o
  13. -3
复制代码

8 L9 |/ ?( v. y0 qmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 L6 h* ?- c) A# M, A6 I+ w3 @
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 H3 d6 M- m$ _7 g4 M1 w7 f
  2. floor(x)- P) k% X! f; x$ Q  X' o
  3. Return the floor of x as an int.2 `% I$ `- d9 D& r0 i# z/ \
  4. This is the largest integral value <= x.% E" x: K6 f2 L
  5. >>> math.floor(4.1)" w9 |0 @% v  J% t5 E
  6. 4- I$ T+ B8 J$ x0 I1 U& ?6 e
  7. >>> math.floor(4.999)8 j9 c. V# n$ e8 Z- s" g4 }
  8. 4
    ) V* r3 f- S$ x1 m7 q" I
  9. >>> math.floor(-4.999), {2 M, Z$ i# \  w) U3 f* L, `
  10. -5) g' A9 O& e0 Z0 E4 G6 D2 `- h
  11. >>> math.floor(-4.01)" w) U1 a2 {5 g" d9 I8 s
  12. -5
复制代码
2 y. Q% M. K- b, N7 ?
math.pow(x,y)  返回x的y次方,即x**y. t) G! f: q+ \  q; f" w6 y
  1. #返回x的y次方,即x**y- e9 r  ?, X) e8 N  Y- H
  2. pow(x, y): e$ x# R  }9 g5 z( d; [. r/ f( |
  3. Return x**y (x to the power of y).
    2 _' b8 O  S/ M! y6 g0 X
  4. >>> math.pow(3,4)1 V7 ^' _2 k  N/ O/ e0 H& F
  5. 81.0
    % i# g& }7 X& W, i4 P* g
  6. >>>
    ; q& \2 T8 D6 |7 W2 x% Q2 q! n8 l
  7. >>> math.pow(2,7)" j# x$ _+ i$ b' k, i5 W
  8. 128.0
复制代码

% d  j: m* W) e  [9 Z' E1 Cmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' k1 {& D$ q; P: p1 k
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    1 x& W$ B/ ~/ z" _% d
  2. log(x[, base])) |  `8 j5 p2 Y, y& k4 C
  3. Return the logarithm of x to the given base.+ ^1 ~0 [- I! o* p5 i& ~2 d, v
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ! ~: ~" N- Q  i
  5. >>> math.log(10)( _* @- n" B! C2 [
  6. 2.3025850929940466 ?: U- w3 T% ?$ `
  7. >>> math.log(11)* {8 U  d7 a: d
  8. 2.39789527279837076 i. d( Z. j% i! v+ `2 H
  9. >>> math.log(20)& i3 V4 e0 J( p
  10. 2.995732273553991
复制代码

/ R9 q; [& y! G* Xmath.sin(x)  求x(x为弧度)的正弦值; E/ n: i' X/ Y7 V& e
  1. #求x(x为弧度)的正弦值
    ( X3 P- B. L$ q- a( n) t1 S
  2. sin(x)* f& P* F' d" i3 q" v% E' c7 N. N
  3. Return the sine of x (measured in radians).
    % v7 C' q/ Z! ^( \
  4. >>> math.sin(math.pi/4)3 Q3 W. i+ z) n1 E+ b( ^- n! D8 L
  5. 0.7071067811865475
    - X& b3 t$ `4 n7 u) A
  6. >>> math.sin(math.pi/2)
    4 e' z3 C5 p8 b# K/ ^' z' F; s% E
  7. 1.0, c5 A+ L$ S6 O) @$ ~7 D
  8. >>> math.sin(math.pi/3)
    ( h: D& _0 q2 B7 X
  9. 0.8660254037844386
复制代码
& f0 ?7 f8 R) L  B; s
math.cos(x)  求x的余弦,x必须是弧度. [0 e& R  |6 S  q- J, i/ t
  1. #求x的余弦,x必须是弧度
    + O8 X5 C% o9 w% N* |" }
  2. cos(x)
    / S' W: _# J/ ~: f% ?' C
  3. Return the cosine of x (measured in radians).9 q! }- m1 u6 o. H" R5 p: L1 U5 X1 T
  4. #math.pi/4表示弧度,转换成角度为45度
    9 ^+ g1 J( K* ^
  5. >>> math.cos(math.pi/4)
    2 |! n: H8 t# D
  6. 0.7071067811865476+ |; Z' ~  ~' a  U, F9 L
  7. math.pi/3表示弧度,转换成角度为60度2 Q6 Z& o0 x0 J  i. p
  8. >>> math.cos(math.pi/3)
    7 v$ z# d6 [6 N+ i
  9. 0.5000000000000001
      U* W) H  l* R0 P$ v4 [
  10. math.pi/6表示弧度,转换成角度为30度
    , j! Z( a8 w' p2 M, M& J8 J
  11. >>> math.cos(math.pi/6)% I+ s  H4 d/ l% u: G, L
  12. 0.8660254037844387
复制代码

, Q7 E6 o& T1 Z+ x' ~5 n2 X* imath.tan(x)  返回x(x为弧度)的正切值4 E3 T1 K2 o! K/ O$ \9 h
  1. #返回x(x为弧度)的正切值
    : K4 R. N% @7 K. ^( _; c3 P
  2. tan(x)/ w4 @8 S0 }$ S; \7 F
  3. Return the tangent of x (measured in radians).
    : O5 _0 O, K5 M
  4. >>> math.tan(math.pi/4)1 }  f  G0 k4 b% \
  5. 0.9999999999999999
    , z! E: ^$ o# J# T
  6. >>> math.tan(math.pi/6)
    . l7 z% M, W. Q0 m1 F% d
  7. 0.5773502691896257, O! ~- I6 M% X0 b$ P. @+ t
  8. >>> math.tan(math.pi/3)5 T! i$ H. y4 ^
  9. 1.7320508075688767
复制代码
* `' G/ `) r; d6 ?
math.degrees(x)  把x从弧度转换成角度, Z: f  h6 A$ A0 D
  1. #把x从弧度转换成角度4 \: }: l4 A- g
  2. degrees(x)
    ( Y% F' ~1 T4 n2 m
  3. Convert angle x from radians to degrees.
    2 @4 a3 X5 R6 l$ ~7 k! |5 \2 ^0 m

  4. 8 X! G" e' S2 B' {
  5. >>> math.degrees(math.pi/4)
    ; q2 F$ {4 V, ^4 ~8 v
  6. 45.02 I6 |1 b, R3 f9 Z4 b+ S
  7. >>> math.degrees(math.pi)- R' }5 Z: v6 N1 T1 i3 ~# B6 {# f
  8. 180.0* ^# b! `: F' y8 F5 b
  9. >>> math.degrees(math.pi/6)
    # `& X/ P; |6 F# `; s0 O+ C+ ?
  10. 29.999999999999996# ^  K3 A4 w/ k  F0 [
  11. >>> math.degrees(math.pi/3): Q& z6 V4 c3 d3 A; N5 B
  12. 59.99999999999999
复制代码

7 O2 t, P# T7 m5 M+ lmath.radians(x)  把角度x转换成弧度
8 b: q/ {1 J2 @
  1. #把角度x转换成弧度2 j- a6 |3 r# X- k
  2. radians(x)
    4 X6 _% r3 W; z1 b
  3. Convert angle x from degrees to radians.8 @# b2 ]6 P" B/ L2 V1 e
  4. >>> math.radians(45)  c" O$ N% W! ]
  5. 0.7853981633974483
    4 s0 l. W) I2 x6 {! R
  6. >>> math.radians(60)) b9 S8 X0 e2 f7 o" v' X5 Z0 f
  7. 1.0471975511965976
复制代码

3 L: w. `* }+ M2 @, p: e# Smath.copysign(x,y)  把y的正负号加到x前面,可以使用0
+ G! R/ J6 R' }* u' V' {/ }
  1. #把y的正负号加到x前面,可以使用0; e, d  C5 v" B" J8 z5 V9 J4 |$ g
  2. copysign(x, y)2 M7 j# g9 |. u/ H$ A  U
  3. Return a float with the magnitude (absolute value) of x but the sign
    0 `. f, P, L. \( \7 J
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    3 g( C3 [  ]0 m9 [/ z
  5. returns -1.0.
    ' U9 ?& B4 G, V3 k4 e

  6. 2 o2 m6 I6 t5 _6 s6 s/ P0 w
  7. >>> math.copysign(2,3)
    5 g3 [  R" c. m% n. \
  8. 2.0
    , i+ u; }9 x( L1 W* s0 g( Z2 T
  9. >>> math.copysign(2,-3)
    % U  _- V! V& \
  10. -2.05 L$ ]7 W8 E1 G5 t. ]' f( K6 k
  11. >>> math.copysign(3,8)" x, j9 a0 u/ h' ]
  12. 3.0
    & _6 b! q0 @2 c" j0 ^
  13. >>> math.copysign(3,-8)0 y' d" U9 j2 W" {; j/ h9 O' N: a
  14. -3.0
复制代码

% a9 v' A4 H% n. }2 E3 I% ]math.exp(x)  返回math.e,也就是2.71828的x次方( u* T7 {" _2 S% P
  1. #返回math.e,也就是2.71828的x次方$ t5 a7 e$ M9 R9 }+ D  Z
  2. exp(x)
    - o4 ]3 Q% ?4 c
  3. Return e raised to the power of x.3 H# j* T& B+ j7 R9 _- q
  4. 0 r" P! K! y3 c- a+ \" B" E9 T
  5. >>> math.exp(1)
    ; m$ D1 T0 R- h5 O2 v
  6. 2.718281828459045/ L$ r; v/ ]" z: e: w
  7. >>> math.exp(2). l: l, f5 ]  L" r  L
  8. 7.38905609893065% D9 W% c5 o" `7 j1 T& L7 L
  9. >>> math.exp(3)
    $ s. n3 T8 Z5 z) n& t! T- w( C) Z
  10. 20.085536923187668
复制代码
/ j1 `7 H  Y% E! `/ C7 T4 L
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1* l" z0 u; }* x
  1. #返回math.e的x(其值为2.71828)次方的值减1; Q5 V5 q( k* a+ E! r* R) X! _/ C
  2. expm1(x): J' w3 B# I* W! j! i
  3. Return exp(x)-1.
    % _8 N$ a0 B1 w6 s
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 [8 j. X8 k) ?9 T2 C( Z9 f' `" I

  5. 8 E' |7 K/ b- K! w* M% J
  6. >>> math.expm1(1)+ k9 @2 G% r6 w$ v
  7. 1.718281828459045
    & O' S* f  i( L  I6 |
  8. >>> math.expm1(2)
    ) y" ?$ n$ v) G! \" {# v  @1 j" k
  9. 6.389056098930657 z) h7 z, y6 `
  10. >>> math.expm1(3)
      |8 @6 p# o" O5 I; A
  11. 19.085536923187668
复制代码

% J7 L/ {2 r& zmath.fabs(x)  返回x的绝对值! b1 B' R7 l$ @* }0 O, M( d
  1. #返回x的绝对值% \( ]9 ]% {3 l9 C
  2. fabs(x)
    % }. Q- ~  o" f. F% y; ^
  3. Return the absolute value of the float x.1 e. M; y4 w$ r! T) x

  4. 5 _/ k" u5 C- i( |6 |$ ~8 s
  5. >>> math.fabs(-0.003); \' J5 X3 T) h) Y+ z
  6. 0.003! B/ `& w" F" \( i) _2 b
  7. >>> math.fabs(-110)
    4 Z( w; N# u" _1 r3 y
  8. 110.0
    % `( H# M; m7 X9 s
  9. >>> math.fabs(100)
    7 g/ Z5 q% V+ o7 E: u: a
  10. 100.0
复制代码

5 ^) q! z/ }, T9 w3 V; Qmath.factorial(x)  取x的阶乘的值: M; ]# Y% L3 p* t! x5 Q( C9 X
  1. #取x的阶乘的值" Q9 o; h; U  a9 k8 H: H3 R9 r/ _7 I
  2. factorial(x) -> Integral) l4 }8 C) W5 O
  3. Find x!. Raise a ValueError if x is negative or non-integral.) a5 O- u7 R+ u. y$ ^3 v  X, u" \
  4. >>> math.factorial(1)
    + N0 W0 A2 }, K/ J6 \% i
  5. 1/ e: K7 |7 W. r$ Q/ T
  6. >>> math.factorial(2)
      C9 o0 K: g. A, v
  7. 2( L7 M1 r/ t+ J% C% L! v
  8. >>> math.factorial(3)
    3 x# u. U( J2 T1 G
  9. 6
    ; K7 M$ |/ r/ _7 Y! d
  10. >>> math.factorial(5)
    2 P, W! X1 I( I- E5 J6 b) H
  11. 120$ F2 C: ]% m0 P( ^! q" ?! W
  12. >>> math.factorial(10)' k2 e* b) r  H6 @$ @8 Z. H/ M  ~
  13. 3628800
复制代码
! K, Z+ }3 @4 O, Q- E
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数! \8 B5 m. j# @: [) |; T
  1. #得到x/y的余数,其值是一个浮点数: I9 ^; b$ c, M, B+ i# I0 X. b
  2. fmod(x, y)* Q4 P- t/ A) V: W9 \
  3. Return fmod(x, y), according to platform C.  x % y may differ.+ F1 ]# m7 f: v
  4. >>> math.fmod(20,3)- T2 @% f! g+ c0 Y$ f. x: o9 I
  5. 2.0
    ) F* P3 V) `3 v' Y
  6. >>> math.fmod(20,7)0 w& G0 R0 n! B5 s. j# J
  7. 6.0
复制代码
$ x7 c& z& [! x- q  Y) \, [3 @: h
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围: ~% r  M8 n0 v
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 t6 X; S; D2 R
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    & R0 x$ d% v: X% F2 ~3 X
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    % c  o: G- n, S6 k/ I
  4. frexp(x)9 ~! x4 X  \9 p1 h, S
  5. Return the mantissa and exponent of x, as pair (m, e).% I& R+ P4 H2 }: y- o6 H
  6. m is a float and e is an int, such that x = m * 2.**e.
    " F0 H4 b+ b% F* D) X* a
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 h1 r# \0 \! ~/ ^
  8. >>> math.frexp(10)
    . g- Q: A" e6 H, Q2 w5 u
  9. (0.625, 4)" I! ^; J; r) h, S& L" P2 A
  10. >>> math.frexp(75), M/ h6 Y* w% a8 z, K/ P7 j
  11. (0.5859375, 7)
    0 h1 a( X# P0 U+ r* n9 S$ D
  12. >>> math.frexp(-40)
    $ o. u1 k' F0 h' G8 K! Y1 d" i
  13. (-0.625, 6)9 J0 p( D  l; Y4 W  v- p/ R6 g
  14. >>> math.frexp(-100)
    : y4 f/ ?8 S- u: ?3 G
  15. (-0.78125, 7)
    6 l. v1 H$ j& f2 v
  16. >>> math.frexp(100)
    ( n# \! G6 R. P! S+ ~% g6 }
  17. (0.78125, 7)
复制代码
0 {6 h2 n! a. J- Y$ ?
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
& D0 l) D3 A; z6 J! @, J
  1. #对迭代器里的每个元素进行求和操作
      u) H9 v7 K5 z" |- S# _. a+ z7 K0 Q
  2. fsum(iterable)
    9 ?! G5 W7 [; t. I, v
  3. Return an accurate floating point sum of values in the iterable.
    $ ]& @' D* t: k/ u2 C. z/ w
  4. Assumes IEEE-754 floating point arithmetic.1 i% ]: |0 m1 Y6 d
  5. >>> math.fsum([1,2,3,4])  h; K  v8 f- H! W4 d3 B6 [2 e. x* N
  6. 10.0
    / ]; k# G8 L4 E1 ~+ `
  7. >>> math.fsum((1,2,3,4))5 P+ |+ S; W9 M$ a% G; v
  8. 10.05 ^3 R# z( d5 T& a  z
  9. >>> math.fsum((-1,-2,-3,-4))% D& t8 H( N% w# h* e7 [9 K. i+ v
  10. -10.0
    / e4 j( c  F; w' \9 n5 R" M* M
  11. >>> math.fsum([-1,-2,-3,-4])7 I0 n, f. `( j( \9 h! ?; ~3 `6 j
  12. -10.0
复制代码

: \* f+ X7 ?7 |; }; C% {* l6 P' ~math.gcd(x,y)  返回x和y的最大公约数
  a( Q- Q+ w; d: v7 \( V- J8 A  D
  1. #返回x和y的最大公约数
    / H) D& U0 p, ^- S* Q: \. Q
  2. gcd(x, y) -> int7 z0 @3 e* N; R' l0 t7 }* x
  3. greatest common divisor of x and y
    6 O' T/ h& B* y  ]/ V9 D5 k
  4. >>> math.gcd(8,6)( @' d) H- R' B9 z, C
  5. 22 Z3 ]) x9 ^2 W' n, I. c
  6. >>> math.gcd(40,20)/ V# Z) k3 |) T( d0 T% Z. v% t
  7. 20( G% n. q, t1 ?8 p. b
  8. >>> math.gcd(8,12)) c  N3 p$ G% s
  9. 4
复制代码

$ a! S3 i% i9 z4 Y# z. Pmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False# Q7 v; |3 M) q# O7 X9 b0 r
  1. #得到(x**2+y**2),平方的值
    2 c8 u$ C$ E0 w( \8 h5 _4 h
  2. hypot(x, y)
    0 H: I. O; @, z& S* F( J/ R
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    / W' h) o* w. u
  4. >>> math.hypot(3,4)/ t9 |/ Z: t  z7 ^7 w0 }
  5. 5.0
    6 V) C' y" i& F3 `8 Z7 M1 h! v% g2 ~( E
  6. >>> math.hypot(6,8)9 i9 c( H" F* t# w/ }6 w
  7. 10.0
复制代码

2 G: {, n$ ^- Y9 m# ]# Smath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
7 Q) z. ^1 h" E* J
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    * }# g6 K! T4 K9 v# [: N" M& t) x
  2. isfinite(x) -> bool
    2 f9 k& x1 \9 e$ a
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    $ d+ W7 K9 T; M1 W" d3 d
  4. >>> math.isfinite(100)
    , t- d  x! L9 k: G; [' l
  5. True
    3 L4 ~, \; s, ~4 r$ V
  6. >>> math.isfinite(0)% O+ r" W. y8 o& k" J
  7. True
    8 C) c' W* n) n: I0 U0 P
  8. >>> math.isfinite(0.1)
    ( X- t, W( T. C+ W; V
  9. True
    : l5 C8 A( Z- r7 z% n
  10. >>> math.isfinite("a")" {  _$ }# R" C% P
  11. >>> math.isfinite(0.0001)) ^6 v+ S4 M% M& k! |
  12. True
复制代码
& f/ r& ~2 m8 k0 M9 w+ x. |
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False; p% S" E; f: v
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    & n4 O  i& \& g1 H) X4 W
  2. isinf(x) -> bool
    % l0 Y( \, v) {8 Q
  3. Return True if x is a positive or negative infinity, and False otherwise.- j! M+ [5 @+ N. [" z6 l
  4. >>> math.isinf(234)
    # v, K! s" t. O+ y2 C* U
  5. False
    ( K8 R- J# G' i! }5 R) y
  6. >>> math.isinf(0.1)# P9 O( b* F6 j0 E; k2 j
  7. False
复制代码
. i( k& d( l* n5 t. f3 E% a1 c
math.isnan(x)  如果x不是数字True,否则返回False% g. J5 Y: k) x7 d9 x
  1. #如果x不是数字True,否则返回False4 c: r. @- {8 |1 }' i$ H  \8 F; Q; N  ]
  2. isnan(x) -> bool
    ) l  ?5 J6 L' ^0 `# M6 w7 r
  3. Return True if x is a NaN (not a number), and False otherwise.* ?# n; I. y  J* P
  4. >>> math.isnan(23): P  H% P5 K, h; v  k
  5. False# X$ j( `) Q4 r4 ~9 s7 |
  6. >>> math.isnan(0.01)" [! _! s1 V7 L8 M" z# J
  7. False
复制代码

4 D- S* A" g" U+ Z4 Z0 X5 h: |math.ldexp(x,i)  返回x*(2**i)的值
/ U7 z9 R7 u0 z  i
  1. #返回x*(2**i)的值
    & G' x& g6 ?0 u; E0 s6 d* _
  2. ldexp(x, i)9 A8 K) P( _4 @% ^$ }
  3. Return x * (2**i).
    & ]& Q# V( C" K
  4. >>> math.ldexp(5,5)" P4 G, u) J$ A0 c9 p4 q+ t! |
  5. 160.04 Q$ K3 D3 d- F0 {1 u3 _$ g7 J
  6. >>> math.ldexp(3,5)% p  S/ _* G+ b2 Q4 c
  7. 96.0
复制代码
) \8 I4 t% M% u9 |! s' N
math.log10(x)  返回x的以10为底的对数
3 m3 ~6 V& l( _) _1 R  J  U
  1. #返回x的以10为底的对数
    9 Q. l% _/ d6 [5 z
  2. log10(x)% }% [5 ]( _8 V( G9 ~
  3. Return the base 10 logarithm of x.
    6 ~- X3 a1 I/ A3 t! r  H+ Q
  4. >>> math.log10(10). [- Q4 |# C, [) F
  5. 1.04 w6 Q$ K  R, n$ v2 F( e# l! f
  6. >>> math.log10(100)
    # ?3 e1 ]( @( R& p* L! _
  7. 2.0: n9 q" p3 P7 P8 a
  8. #即10的1.3次方的结果为20: F) n& v- u' n3 c7 ?) u
  9. >>> math.log10(20)
    & @3 R5 P0 ]0 A$ D) ^. S) ?/ v
  10. 1.3010299956639813
复制代码
6 p0 P4 K9 J4 I
math.log1p(x)  返回x+1的自然对数(基数为e)的值
. e$ A* `. i2 o* Z
  1. #返回x+1的自然对数(基数为e)的值
    # ~2 C2 r! G7 V$ g
  2. log1p(x)/ r  z; E% {9 O1 X
  3. Return the natural logarithm of 1+x (base e).8 r+ j  @9 K7 ]* c! y: }8 ^3 K
  4. The result is computed in a way which is accurate for x near zero.
    + r) d" a$ h2 E6 Y
  5. >>> math.log(10)2 [# j  m0 y5 s1 A  @
  6. 2.3025850929940463 W$ X0 x" Z, ^6 p
  7. >>> math.log1p(10)  ?' C& _$ ?7 D- {) m: M
  8. 2.39789527279837071 T* N: b( `& U& q
  9. >>> math.log(11)
    % v# q9 F. f7 f5 G  T; W% R
  10. 2.3978952727983707
复制代码

5 `3 z1 Y' t' P3 b: hmath.log2(x)  返回x的基2对数
% l& ~" h  K# T4 W- b# ^
  1. #返回x的基2对数
    . ~" W" K9 I1 S0 q: I+ z$ T
  2. log2(x), }8 X6 Q  i1 ~+ h4 U9 u
  3. Return the base 2 logarithm of x.
    0 S5 E$ Q% ]1 ~4 m0 [
  4. >>> math.log2(32)* K  ^$ l/ W3 Y, E( K2 b) t& z+ D
  5. 5.0
    4 }9 o- r, W4 I
  6. >>> math.log2(20)
    " [# n# M/ C' C! W" y
  7. 4.321928094887363
    & O8 n5 }: w2 Y. `4 W1 i
  8. >>> math.log2(16)" s# H6 D2 E+ D4 K
  9. 4.0
复制代码

2 g; d% s/ H% T  ]  Y/ J% vmath.modf(x)  返回由x的小数部分和整数部分组成的元组
6 W& M- k' h2 M# V( [0 F! M' ~4 M: N
  1. #返回由x的小数部分和整数部分组成的元组6 R7 l5 q' q) j" s& q" G
  2. modf(x)6 Q8 B7 V- |2 C0 K
  3. Return the fractional and integer parts of x.  Both results carry the sign/ Q: i. n% i6 W0 x
  4. of x and are floats.# x5 x& M+ ^+ j$ B% S
  5. >>> math.modf(math.pi)4 w' m1 R# T- Q; X1 Q- K( K
  6. (0.14159265358979312, 3.0)" A% c6 Y6 d9 Y, q1 i
  7. >>> math.modf(12.34)5 L5 L5 O# n" O2 l. e7 O
  8. (0.33999999999999986, 12.0)
复制代码

: u4 m, I& ^$ R4 }7 ?% u3 \& W; \math.sqrt(x)  求x的平方根9 j: ]( F  p7 N" j- U# K, f
  1. #求x的平方根7 Z  n* \3 X" l4 u# T
  2. sqrt(x)
      g+ m6 n  S' N
  3. Return the square root of x.8 ~$ ~9 p" H5 J# X- D! ?) j1 P: a
  4. >>> math.sqrt(100)9 \/ {* b/ a3 g( g0 O: {9 y9 O& q
  5. 10.08 k, Z8 D# E- ^) W) n8 r
  6. >>> math.sqrt(16)
    $ L& M+ g( g+ ?1 b
  7. 4.0- P  z9 w% s$ k, z" Q
  8. >>> math.sqrt(20)2 `; s0 J8 S  g% ~7 C
  9. 4.47213595499958
复制代码
2 F) T: F7 n" P& I  U: r8 N
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分: {' W  C; v. J$ p, J1 k  D0 Y
  2. trunc(x:Real) -> Integral
    * y: y( u8 F4 v0 M; B7 ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    9 {2 j7 j4 C% |4 p  v
  4. >>> math.trunc(6.789)2 G; s0 k8 y/ y0 j
  5. 6
    * z5 L5 ?0 f) N; b
  6. >>> math.trunc(math.pi)& `# o2 |( L- Y1 q  l
  7. 35 k" d6 j: t  z7 Q
  8. >>> math.trunc(2.567)
    * b5 s2 l* e2 d% @# T
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

GMT+8, 2025-11-15 07:23 , Processed in 0.087197 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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