新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

" `0 K$ W) ~% s' x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 U6 p. k4 i! y' ?
7 D4 [+ W/ `9 {# L" B
方法15 {5 S! ]1 {/ }7 c  O
  1. >>> import math& R% \5 {" b- A6 W: T6 J/ x8 {
  2. >>> math.sqrt(9)
    % M( j( }; L) w' o
  3. 3.0
复制代码
方法2
0 {9 ?8 \; W* R4 O
  1. >>> from math import sqrt
    1 \/ j- N2 T5 Q. }
  2. >>> sqrt(9)
    1 c8 J4 ?6 c7 m/ Z
  3. 3.0
复制代码
& D3 f1 O6 o, q4 B$ C5 e4 `3 S0 P0 J


. u! w' J% R$ e  A# q' R
math.e  表示一个常量
: E" a5 ]1 o2 I4 x
  1. #表示一个常量' F9 @+ t6 }# W5 J- ~% q
  2. >>> math.e
    6 N) d1 x1 ?8 g# z: K
  3. 2.718281828459045
复制代码
3 _" J9 b7 x1 g/ K3 n
math.pi  
数字常量,圆周率

$ l- q/ G* _+ a" D$ a0 e2 x% q
  1. #数字常量,圆周率7 l: E$ o& w$ J( N. [
  2. >>> print(math.pi)
    & i' b1 f  |1 n
  3. 3.141592653589793
复制代码

" C+ Y9 }! t# o" V! o5 Fmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
  A9 Z: O/ P$ F: e$ V7 Q! R9 \; f# P
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x7 A, c( H4 z4 |0 @7 l
  2. ceil(x)* }5 C2 H! `2 f& r0 U' \! }' z6 X* I
  3. Return the ceiling of x as an int.  d3 s* F- L: ^% y" J
  4. This is the smallest integral value >= x.
    % M* q& k( G1 l/ X* U2 ^3 h8 o
  5. : K) V' d9 b8 n( k
  6. >>> math.ceil(4.01)( Y! l  t8 }* E: F
  7. 5
    / m7 B/ K  |, d. L, c3 b/ }) t  T
  8. >>> math.ceil(4.99)- n; _+ ?  N" ?1 ?$ E
  9. 50 @5 S7 t! P% i3 ?, }9 n. u
  10. >>> math.ceil(-3.99)8 u4 ]! c% s2 E# B, [! C
  11. -3
    ; l9 {% Z* H9 M5 d7 y' m; l, T: F
  12. >>> math.ceil(-3.01)
    0 g1 _7 F9 F( Q, f# @0 J
  13. -3
复制代码
( }$ {/ l9 S7 U
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 X0 G" _& `+ s  O( b; ~
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ( R  G1 k$ m7 {# y0 A4 E$ K  p
  2. floor(x)# E( R6 W- g/ G4 n
  3. Return the floor of x as an int.) S$ M$ ?( {9 b2 v5 M
  4. This is the largest integral value <= x.
    & h4 e- U( H; o" B; w5 R: G) Y+ h
  5. >>> math.floor(4.1)& h, m+ ]% p2 P* N. Q9 ^9 C3 Q! Z
  6. 4
    $ G; q( ~1 p8 q' x
  7. >>> math.floor(4.999)3 I' N4 H) d1 S% v
  8. 4
    / H. r+ K# `# j' J- i, e
  9. >>> math.floor(-4.999)
    * m$ q6 T1 }% W7 r
  10. -55 q4 u) a, ?4 }7 f4 G6 c' E* n: [
  11. >>> math.floor(-4.01)9 P% s0 r( F# |; z+ z
  12. -5
复制代码
* `% N9 D# W1 K) w* _
math.pow(x,y)  返回x的y次方,即x**y/ Q! d; d, i' H
  1. #返回x的y次方,即x**y
    3 ?: e8 ]0 ]1 J) X" I1 I) h
  2. pow(x, y)/ s) J( |0 G$ W1 ]" y
  3. Return x**y (x to the power of y).
    # b; ?. X" }- `* I: h1 w
  4. >>> math.pow(3,4)( P" [8 t9 D) L9 h2 I
  5. 81.0
    ) j6 n  U. f/ S0 D, d1 S
  6. >>>
    7 s: g" j- [2 ^' ^# I) q# `- e
  7. >>> math.pow(2,7)  t) K+ q. g, q) q# P
  8. 128.0
复制代码
! t5 N( s  a' {5 v# ~# E
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
5 C3 D+ B. C3 r4 s  z& k& Q
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 X7 V5 z  @- o. D$ \9 q; s# [
  2. log(x[, base])- R' X! e2 U1 H/ }: v: A
  3. Return the logarithm of x to the given base.4 r3 \7 |& _) y
  4. If the base not specified, returns the natural logarithm (base e) of x.5 N: d, U  T% K% B5 W) P0 C- I
  5. >>> math.log(10); D1 u" Z! i3 N* E) Y- x
  6. 2.3025850929940465 j, W! V0 \' D5 B, q2 f1 q0 T& V
  7. >>> math.log(11)
    % D! A% }/ @# f- q
  8. 2.3978952727983707
    * f6 u" k5 r! @5 b/ [4 J) y
  9. >>> math.log(20)
    / Z( ^% S; C1 P+ K+ z  x) V9 M
  10. 2.995732273553991
复制代码

3 N: t) P$ [8 F  o7 S+ Q! fmath.sin(x)  求x(x为弧度)的正弦值
9 E+ e1 D4 z- {' T- H+ R( C
  1. #求x(x为弧度)的正弦值
    5 J  Q" s$ h7 N: M6 j% x0 L& H2 j
  2. sin(x)/ G8 C+ R  U  r4 T
  3. Return the sine of x (measured in radians).
    6 I+ v: ?& Q, g$ ^4 n
  4. >>> math.sin(math.pi/4)
    0 A( m6 K5 ?6 o% g$ I% w' x. r
  5. 0.70710678118654752 B& \6 {2 |+ g: G
  6. >>> math.sin(math.pi/2)3 T8 a9 [. ^9 e! g* G9 f+ U7 z
  7. 1.0( C4 I# g+ M+ q& w, \6 ~
  8. >>> math.sin(math.pi/3)3 D' k+ C+ u8 E; w3 [
  9. 0.8660254037844386
复制代码

: w' i  {" ]: J* v0 Z$ Bmath.cos(x)  求x的余弦,x必须是弧度
3 n2 B* e4 z4 r4 h1 G0 }( T" P
  1. #求x的余弦,x必须是弧度
    8 ^$ F6 J4 [! \0 A+ U; v
  2. cos(x)
    + P* S* X, c- a( i1 u: g* U
  3. Return the cosine of x (measured in radians).- b. m7 k# m3 [( S
  4. #math.pi/4表示弧度,转换成角度为45度8 a% N. Z- d) _  A7 H( N
  5. >>> math.cos(math.pi/4)# R5 K/ l# j+ t; P4 z% y
  6. 0.7071067811865476) {. B2 g& ]1 X. f; i* p
  7. math.pi/3表示弧度,转换成角度为60度
    . K) h  M+ R; r) S- y% }3 R, c
  8. >>> math.cos(math.pi/3)
    - ~0 Y' n1 L5 {5 v
  9. 0.5000000000000001  V. r7 l' W+ N+ ]# h: ?0 a
  10. math.pi/6表示弧度,转换成角度为30度- |& b# a' O* d8 M- |
  11. >>> math.cos(math.pi/6)
    8 K1 F6 s* A, g, [3 I7 k* h
  12. 0.8660254037844387
复制代码
& m5 c4 h; f5 N9 h: F4 K
math.tan(x)  返回x(x为弧度)的正切值% F8 P8 ~- m' A; C- y1 t
  1. #返回x(x为弧度)的正切值
    . q) K: i' ~' B
  2. tan(x)
    7 C8 O: Y/ ^; J9 ^+ N% r
  3. Return the tangent of x (measured in radians)., l+ z( W8 c* M$ `1 y
  4. >>> math.tan(math.pi/4)
    + b% h0 O" {- h/ M8 T9 u* ]1 b
  5. 0.9999999999999999
    - X; ?3 A5 _1 c8 @; x' s; D5 k% L
  6. >>> math.tan(math.pi/6)
    ; `2 B6 f  C, \. J- s
  7. 0.5773502691896257
    7 l* k4 h: T5 ^, e
  8. >>> math.tan(math.pi/3)2 n- D& I1 e. C6 q% j: R
  9. 1.7320508075688767
复制代码
0 C7 T, t8 N  n9 o! H
math.degrees(x)  把x从弧度转换成角度
4 U; P8 k8 U: F% T# u
  1. #把x从弧度转换成角度5 {7 j; z: D; F) Y  \. ]- c
  2. degrees(x)
    ' ^1 w2 v. Z" n4 r5 g* C0 d
  3. Convert angle x from radians to degrees.; W4 Q! c& J; d

  4. 2 F2 M$ C, ]6 N. X
  5. >>> math.degrees(math.pi/4)
    / @& P+ p5 q, I+ L
  6. 45.0
    3 Y  h2 d0 J% B  g2 D: ]
  7. >>> math.degrees(math.pi)3 h9 P$ B" R+ i& y% O
  8. 180.0
    : M% V; B; u3 `0 u
  9. >>> math.degrees(math.pi/6)
    4 w% A# t' |- [3 A
  10. 29.999999999999996
    ! x/ c- a+ K% i. m! B& J/ U
  11. >>> math.degrees(math.pi/3)
    9 F" d2 G' U% \! i: N; a
  12. 59.99999999999999
复制代码

( S) T7 O' P# t1 E  B) E# Pmath.radians(x)  把角度x转换成弧度6 m+ Y. e6 m; h$ r, P' a# J8 k
  1. #把角度x转换成弧度
    # a5 n; O! `7 M. m
  2. radians(x), Q$ p! r0 z! l( W  n7 @
  3. Convert angle x from degrees to radians.5 z3 S/ P4 l: u" \. N
  4. >>> math.radians(45)/ k7 m/ w% G* J/ |
  5. 0.7853981633974483
    2 O1 W/ n- J# b/ E4 I4 u% G
  6. >>> math.radians(60)& Y% t4 ?, |. P4 B! |. g1 }& a- e
  7. 1.0471975511965976
复制代码
- B, y6 s+ F- F) d
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
0 Y" y5 e$ Q/ W4 @) a# l5 P
  1. #把y的正负号加到x前面,可以使用0
    2 H, @2 T$ z) m( N
  2. copysign(x, y)4 F! Z% K" Z( E4 K
  3. Return a float with the magnitude (absolute value) of x but the sign
    7 C) W: Y  Z2 h
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 3 J( Z7 I( h% t
  5. returns -1.0.4 {% O- O  x: D4 |( B4 ~6 k( P* d
  6. ' o% ~! Y6 s4 S4 p1 \0 ]' @- C; f
  7. >>> math.copysign(2,3)
    & @; O1 W$ q8 K) Z4 L0 u
  8. 2.0* \; l; v( W3 z/ h
  9. >>> math.copysign(2,-3). M  R8 e5 w# G/ ]( m' e; i
  10. -2.0/ F! |! S% v' t/ y5 n9 C
  11. >>> math.copysign(3,8)
    % a& m" H& E( S; s3 i0 V
  12. 3.0+ }& F) r- V" E6 J
  13. >>> math.copysign(3,-8)
    2 ~, o3 B0 m' ]6 g& k/ X
  14. -3.0
复制代码
% X5 b* B, ~. X4 S
math.exp(x)  返回math.e,也就是2.71828的x次方
+ N  j- U) w# T& ]- x
  1. #返回math.e,也就是2.71828的x次方2 f5 w- |' B7 O
  2. exp(x)% E9 E( a& \% U' j" w0 I. {  b
  3. Return e raised to the power of x.$ {0 l6 X: x7 @

  4. $ A1 ?- \; W4 Q1 r1 U2 E4 b
  5. >>> math.exp(1)6 W1 Y2 _. G4 ?) ]1 g+ L
  6. 2.718281828459045
    . L5 P6 R+ V7 `0 B# v
  7. >>> math.exp(2)- _3 X, {: }( T+ F7 M1 G* W) P
  8. 7.38905609893065
    5 U8 ~/ Z- |" D) s$ m/ M% E% u
  9. >>> math.exp(3)+ J! y; g5 ^: A
  10. 20.085536923187668
复制代码
$ Y. H/ _/ }; A: z
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1  z6 ?, `3 \- |
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ; K/ w4 P" O8 h+ T8 s6 q
  2. expm1(x)
    " [4 X$ D5 l3 h6 k
  3. Return exp(x)-1.4 f4 U5 ]6 O* O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ p2 c. K  n# Q, w) t2 ?

  5. * q/ v( \& W8 i7 f" L) Q) E
  6. >>> math.expm1(1)$ V3 `$ z, B6 h% [
  7. 1.718281828459045
    2 B* f1 ^1 l0 P% e8 \8 ?! z9 A
  8. >>> math.expm1(2); N# c1 P2 i  j( V2 c1 \
  9. 6.38905609893065" G# K) X2 }5 `( M$ F2 M/ R
  10. >>> math.expm1(3)
      w5 @1 d; d/ k4 Z; j
  11. 19.085536923187668
复制代码

& D: `, f7 _7 k& h3 A3 U! |math.fabs(x)  返回x的绝对值- n" ?  G+ I( X0 q1 ?- P( u
  1. #返回x的绝对值
    8 C/ B  i# X. H$ V2 G, A6 Y) f
  2. fabs(x)" D+ W  b, X* T3 i
  3. Return the absolute value of the float x.
    4 b. G2 {: A' r* ?) s1 w- i6 B
  4. ! t+ z4 `% v! V5 o4 [  f
  5. >>> math.fabs(-0.003)% D$ r7 R1 ^; T7 x0 X% y
  6. 0.003
    & g! m, D+ M6 j1 M& m
  7. >>> math.fabs(-110)
    - \- q; _$ v' W3 s6 g
  8. 110.0
    / C1 `, S2 A, Q  D/ t
  9. >>> math.fabs(100)8 ^- Q+ b% i: F) g
  10. 100.0
复制代码

4 a) v9 c! Q% z1 ?math.factorial(x)  取x的阶乘的值9 {5 L6 t( R( d4 R! F- \+ p
  1. #取x的阶乘的值
    1 A2 r. _$ u+ w6 _8 M8 R% `) _
  2. factorial(x) -> Integral( j1 ?! s1 f1 I) V# E3 [, |/ i
  3. Find x!. Raise a ValueError if x is negative or non-integral./ k9 _; |9 Q2 D
  4. >>> math.factorial(1)6 j0 W9 `9 I9 l) A6 b# J' h& k) d2 t% h
  5. 11 E0 I) ]+ g' z' O! t% m
  6. >>> math.factorial(2)
    / I) [: _- @9 f! T( Q
  7. 2
    * D4 n7 z$ I5 B- Z4 s! W7 K0 N* j
  8. >>> math.factorial(3)
    % r/ l( K8 O8 e$ I( L+ @1 J3 W
  9. 6/ ]+ I8 J: s# e5 f( [( e
  10. >>> math.factorial(5)
    $ ~9 d' N: F1 i( L
  11. 120
    , W/ M. P/ A1 c% x8 r: z
  12. >>> math.factorial(10)
    - F3 Q- L$ {  ?( \5 U# Z
  13. 3628800
复制代码
0 o3 C) d# l! H! \
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数* J4 {: F7 N# N3 _. u" c, ^7 p
  1. #得到x/y的余数,其值是一个浮点数
    $ g  _5 @+ F+ Y' o
  2. fmod(x, y)
    ( L9 J5 J% |/ O. m3 W4 D6 w9 j8 Y
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ) z2 M- o* v: f
  4. >>> math.fmod(20,3)& X9 O3 i1 K( Y
  5. 2.04 z1 |  k& c6 f# p+ ?. p
  6. >>> math.fmod(20,7)# h. w9 W  v- Z9 b- Y0 |
  7. 6.0
复制代码

( a# J7 S+ u* n6 l/ s& O- tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) ]. S% E( K5 |8 p( u- y) K1 O9 B$ U
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 L0 K7 k5 B$ M# F5 c  z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    5 y9 V" v2 X* e$ u
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ ]( W" E! d/ j$ K4 o
  4. frexp(x)
    ) A) M. o( V5 f2 U
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 V, h% X+ }9 I7 A& g: `% K
  6. m is a float and e is an int, such that x = m * 2.**e.
    8 |1 t& T7 D, I3 \
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0./ j6 s2 E- F5 k$ x
  8. >>> math.frexp(10)# P8 r1 q0 Q& s2 T
  9. (0.625, 4)
      W' O8 H$ L6 c
  10. >>> math.frexp(75)
    # A5 {" E8 N% L3 m! P& o
  11. (0.5859375, 7)
    * |3 n; T6 y' |+ \0 M; C) r
  12. >>> math.frexp(-40)0 |# m) S6 L0 ?; E5 n
  13. (-0.625, 6)2 ~2 G  t, z7 q/ w
  14. >>> math.frexp(-100)+ N! M! [6 H3 N' o- l7 `
  15. (-0.78125, 7)$ q$ v! @; j4 d4 U) ~- y
  16. >>> math.frexp(100)# b, u( n  {4 {* m) }' M
  17. (0.78125, 7)
复制代码

$ U) u& F  C  Y& Q5 z: j4 Vmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列5 D# A7 U7 N( |0 B  _# @' `' ^3 Y* L
  1. #对迭代器里的每个元素进行求和操作6 a  p. W7 J5 z* q
  2. fsum(iterable)- V, D, b) s4 \1 k8 L6 B# P/ L
  3. Return an accurate floating point sum of values in the iterable.' [3 _4 D. i3 Q  t" N; K/ Z
  4. Assumes IEEE-754 floating point arithmetic.
    6 k( |  A5 b0 |' a  ^9 ]) P
  5. >>> math.fsum([1,2,3,4]); A, Z; c+ Q% b7 `; D% O
  6. 10.0
    ( X; M2 I# D( U1 {- U( `
  7. >>> math.fsum((1,2,3,4))7 V3 j9 D: G- \  z
  8. 10.0
    0 p8 ]& ]9 i& x! X9 R
  9. >>> math.fsum((-1,-2,-3,-4))6 T8 n2 c+ \: L& Z. }9 j+ J! ^
  10. -10.0' j1 o! j0 H- B/ U! @8 X- Z
  11. >>> math.fsum([-1,-2,-3,-4])
    6 n) h+ I4 o, q2 r! p: l+ P
  12. -10.0
复制代码
; V: t! Y+ d' O8 e# b' y
math.gcd(x,y)  返回x和y的最大公约数, P' i3 h- a, |! f" d3 f
  1. #返回x和y的最大公约数. l6 s: h6 H+ Z0 ?. G& }! Y
  2. gcd(x, y) -> int) O& Y" k5 x% z1 z) Q
  3. greatest common divisor of x and y
    # ~; |& d& }. ]/ u; ?. b4 j
  4. >>> math.gcd(8,6)
    - @5 e3 ^% ^2 W9 }$ V9 c+ `$ f4 I3 P! n
  5. 2
    * Z& Q* E* d9 w: Y  |) p
  6. >>> math.gcd(40,20)
      x' i$ Y1 }: D0 O2 e
  7. 20/ e1 Z2 Q: M# a4 _
  8. >>> math.gcd(8,12)
      f+ Z, n1 _! t! O
  9. 4
复制代码
1 l. I3 {) F/ n& _2 h4 F
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False  |& y" k2 u+ E& S* u9 ]
  1. #得到(x**2+y**2),平方的值
    9 `7 q" ]6 {) N6 e
  2. hypot(x, y)
    , R8 k/ E  e4 ~0 L4 O' i
  3. Return the Euclidean distance, sqrt(x*x + y*y).7 n) l& H3 N) m' g& c( s
  4. >>> math.hypot(3,4)
    ! Y0 r( V$ {2 X( M
  5. 5.0
    : }  d. Z% b1 B7 {8 _$ U) Y, P0 L
  6. >>> math.hypot(6,8); I* a0 t  ]  y% Y+ u* A$ d
  7. 10.0
复制代码
, Q& ?0 I8 B6 j% S  n; \
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 S6 H3 v6 k) }7 _, E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % u4 w2 G7 Q. X) y
  2. isfinite(x) -> bool5 i! H( v8 b0 A' R( Q: w& `/ I$ F# p
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    & U3 Y4 q$ c; d7 c
  4. >>> math.isfinite(100)
    ( a  X- T" n! X" g/ m
  5. True4 U: z+ O$ H  O! x5 g' w, g
  6. >>> math.isfinite(0)
    - C7 @- R8 B! G0 r
  7. True  Z$ A# E& h( F
  8. >>> math.isfinite(0.1), C4 }) ?: H8 Y$ [
  9. True
    " O. c: t& w5 x6 ~# Y% F9 J
  10. >>> math.isfinite("a")* t! r. B# T0 A
  11. >>> math.isfinite(0.0001)
    7 U7 o- L2 s+ w* g( o8 n) m/ K
  12. True
复制代码
& \' {  d: N" e) g' j
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
) r  `4 j) H  g: l3 ]3 I! S
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False8 E' Q6 `0 l2 A& c; T
  2. isinf(x) -> bool  ^" ^1 V5 J/ ^6 F% o$ ?: y
  3. Return True if x is a positive or negative infinity, and False otherwise.
    " i! t% y. [" C: N! o0 h
  4. >>> math.isinf(234), t6 C, T" X9 C( \' @  @( p  R
  5. False/ @( k3 _+ P- j: {
  6. >>> math.isinf(0.1)' |! T3 S2 L. K' D4 @5 i- O
  7. False
复制代码

, h$ x8 s" k, q7 U# ]4 B( xmath.isnan(x)  如果x不是数字True,否则返回False
; m/ M* L: o/ j$ R+ R
  1. #如果x不是数字True,否则返回False" [$ g( ?. B# i+ ~2 z
  2. isnan(x) -> bool; @1 F$ a5 o; W' g/ D8 ]+ v
  3. Return True if x is a NaN (not a number), and False otherwise.% A! d8 M1 O0 O( S
  4. >>> math.isnan(23)
    1 M) T+ Z8 V/ X' F
  5. False
    , P% c+ W- E& k- a: j. q
  6. >>> math.isnan(0.01)9 [8 p5 _, W! p6 I
  7. False
复制代码
4 A+ r5 }% ]4 w- d  Q
math.ldexp(x,i)  返回x*(2**i)的值
7 H5 l. s! ^) Z' Z6 ^0 ]
  1. #返回x*(2**i)的值; o% y) B7 b) ^. _- I' m% O( J7 E
  2. ldexp(x, i)+ X, z4 G/ [' q) O6 S2 J- p
  3. Return x * (2**i).+ H- P! u7 F5 B9 b+ Z' W  M" ?. s
  4. >>> math.ldexp(5,5)! ~& z% r/ e# ^. k" C* T) A
  5. 160.0
    & N5 \& U( n# O  j: M! D$ O
  6. >>> math.ldexp(3,5)
    * {  |; V3 Z6 W9 X+ v
  7. 96.0
复制代码

: U4 u. D# e$ H' ?; Jmath.log10(x)  返回x的以10为底的对数# N/ N, L* F% @( A  P6 I# ~! K
  1. #返回x的以10为底的对数$ E3 t+ o) S7 Y- X# q
  2. log10(x)
    1 y3 y/ o* _+ H, Z5 C  J0 r% E
  3. Return the base 10 logarithm of x.
    & T, [3 m* a9 N* }
  4. >>> math.log10(10)
    3 B  y' }0 C2 o6 S. a
  5. 1.0
    & r+ U6 Z+ V5 l$ T
  6. >>> math.log10(100)
    8 W2 k2 V9 O" X
  7. 2.02 S$ a# [0 c* U4 k! \3 S
  8. #即10的1.3次方的结果为20$ w3 N) x" I/ G# s3 n9 C
  9. >>> math.log10(20)1 J7 F/ n: x) o7 h
  10. 1.3010299956639813
复制代码
+ P# u! W% V' d3 h/ w; i( C4 x
math.log1p(x)  返回x+1的自然对数(基数为e)的值3 N+ F+ P0 y9 i, \" J
  1. #返回x+1的自然对数(基数为e)的值9 D7 I. d+ ]- i) w# a! P
  2. log1p(x)- Q8 y: E* D8 |" q
  3. Return the natural logarithm of 1+x (base e).: c2 ~$ r1 _5 r7 a; W7 b& o
  4. The result is computed in a way which is accurate for x near zero.+ T* m9 r# e3 W9 [
  5. >>> math.log(10)6 Y% f) i: J, ~9 b5 H' R
  6. 2.302585092994046
    ; A& k) @/ u' P7 j
  7. >>> math.log1p(10)3 V$ C$ s! `) b- X" w
  8. 2.3978952727983707
    & w# q# h& ?& i, J
  9. >>> math.log(11): R) ~' Z0 |# q5 d
  10. 2.3978952727983707
复制代码
" n; g# @1 Q& ~, v
math.log2(x)  返回x的基2对数
7 r* J4 c! O! l
  1. #返回x的基2对数
    8 e( i: X7 \" O, ?
  2. log2(x)
    6 N3 G0 N$ q. W& [) x/ y5 U
  3. Return the base 2 logarithm of x.
    * {* Z: v0 r6 g+ Q7 X
  4. >>> math.log2(32); A3 Y# p+ @* w9 C
  5. 5.0
    , q, }) _! m, U( [
  6. >>> math.log2(20)
    1 o. v- X  H- J. \
  7. 4.321928094887363# {$ E/ J  j; k/ }4 C3 v: `: m# a
  8. >>> math.log2(16)
    3 s2 L% L1 X' H5 U  I, C( [
  9. 4.0
复制代码

) o4 M3 c: M4 ^$ ^: T: jmath.modf(x)  返回由x的小数部分和整数部分组成的元组
( D( h, C: u- ?2 H* }2 h5 A/ t
  1. #返回由x的小数部分和整数部分组成的元组
    ) J; F, H% r' V. D' H1 M3 g; d
  2. modf(x)3 M  R9 s  v5 D( o# X
  3. Return the fractional and integer parts of x.  Both results carry the sign
    8 l8 k2 \# H/ {) j9 x
  4. of x and are floats.
    7 x! U! X4 d/ n
  5. >>> math.modf(math.pi)+ x+ u0 Z" Y3 e! _& _
  6. (0.14159265358979312, 3.0)# e) Z& p) Y' k3 o
  7. >>> math.modf(12.34)
    , H, m9 l# i5 K1 z' y" q/ w  D
  8. (0.33999999999999986, 12.0)
复制代码
; t3 i/ o& c  R% l
math.sqrt(x)  求x的平方根
2 q. K& V- F1 Z( [9 o
  1. #求x的平方根
    9 ]+ o! ]  Y0 [( k; c
  2. sqrt(x)
    8 `4 S& ]4 q$ v& y4 v8 A5 p" h, R
  3. Return the square root of x.$ |* S0 p4 U" U; [% _2 D. F9 z& F* k8 @
  4. >>> math.sqrt(100)* I( W9 l  n+ T1 E8 [* y
  5. 10.0. }; E5 a. I3 U, p  q
  6. >>> math.sqrt(16)( l6 u8 Y7 B9 y3 x
  7. 4.0: q7 q* Z. s) t  Y3 G+ r
  8. >>> math.sqrt(20)+ S. s  g1 }/ y" |" v7 M- g$ u
  9. 4.47213595499958
复制代码

2 g  y4 E* b& H6 I3 |math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分. i, j4 V/ @9 O% |- ]' g
  2. trunc(x:Real) -> Integral/ e! k  U1 ?7 a
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 b9 D, }; x# z7 o
  4. >>> math.trunc(6.789)$ I$ P7 ^0 B& t) f
  5. 6( a3 d# G! S8 s6 u: w4 j
  6. >>> math.trunc(math.pi)
    ! [: p: Q, |5 j7 h5 @- H
  7. 3
    ; B( M+ c& F9 A& _! K" z' t
  8. >>> math.trunc(2.567)% Q* o0 D% V) R- n7 D
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-24 06:03 , Processed in 0.082417 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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