新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
/ n7 Y, z0 \6 O* c9 u
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& K) p) l) p" u
% O% c( U* R& K
方法1
- [4 B$ c% {5 T. U2 u, v* T
  1. >>> import math
    6 v0 T! X3 s0 D% N
  2. >>> math.sqrt(9)
    & }: L" a5 M  b
  3. 3.0
复制代码
方法2
3 S% @# o0 Q8 T* t" o
  1. >>> from math import sqrt
    " F/ ]. t4 X0 z6 _( U2 I% p
  2. >>> sqrt(9)
    + I4 x- {, A. T- q/ r
  3. 3.0
复制代码

8 Q4 G0 N4 @/ {* e

2 ]. E! {& I$ F
math.e  表示一个常量4 `) j4 A$ x( R; L
  1. #表示一个常量
    1 u$ @" c4 P6 ^, P
  2. >>> math.e7 f# z3 B; p4 _5 ^
  3. 2.718281828459045
复制代码
% R+ g+ _) ]* ^" s# Q: a1 {
math.pi  
数字常量,圆周率
2 F7 Z8 _; ]( }; u* ?
  1. #数字常量,圆周率
    . O$ t, a2 M$ }- j) @
  2. >>> print(math.pi)0 Y! r* c3 \. _8 M
  3. 3.141592653589793
复制代码
3 D/ m3 {9 [/ C- A$ i$ h3 M
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
, M3 N6 l7 x7 Y# V# x# X8 w, b: j8 Z
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ' v3 w, ?/ f* [. h
  2. ceil(x)
    ) z) q8 R7 k' O* t1 C
  3. Return the ceiling of x as an int.
    . p3 H/ b5 U2 G% I( L. W
  4. This is the smallest integral value >= x.
    & r" E, b7 F0 A
  5. 1 k: b9 U) d1 ^% `& o, n. E! m0 G
  6. >>> math.ceil(4.01)
    7 v: v$ P( J0 x3 R( H' d. m
  7. 5
    ; m0 I; f5 U+ e5 c1 G" ~' |; g
  8. >>> math.ceil(4.99). f, C' l7 B1 X7 R& d- w
  9. 5$ [9 Z$ L# ~- ]: x' y
  10. >>> math.ceil(-3.99), @" ]' H! I/ ]: Z. A6 m, X
  11. -3
    & G+ W' U9 N) d
  12. >>> math.ceil(-3.01)
    - r5 |$ k0 z: o, A
  13. -3
复制代码

  {5 x4 y# t8 x0 E0 pmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ V9 k3 u& `3 T
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    2 g$ e- h) H# Q) g, i9 v- o
  2. floor(x)4 ^$ j+ I5 f, f
  3. Return the floor of x as an int.$ M, Q0 a- }: D
  4. This is the largest integral value <= x.
      V. k# `4 Z5 _2 I2 k/ V
  5. >>> math.floor(4.1)
    4 g. p% S& v" ^
  6. 4
    % H# J' a$ {! s4 z- \
  7. >>> math.floor(4.999)4 h% r) t7 J6 X1 ]. {, d* A
  8. 4
    2 w8 x' k; y: w- m2 S% W( C
  9. >>> math.floor(-4.999)
    2 a" [9 l# C. B
  10. -5- s+ A; t9 z6 ?. g* w
  11. >>> math.floor(-4.01)
    5 y+ e- G% K; x
  12. -5
复制代码

$ a- {$ _8 T' q; P, @( T; Rmath.pow(x,y)  返回x的y次方,即x**y
; ]0 B7 r+ i+ u
  1. #返回x的y次方,即x**y* J' o  n: c0 o- @0 e. @8 h
  2. pow(x, y)
    ! R: {- l2 h- N7 ^/ H! m; X
  3. Return x**y (x to the power of y).7 {5 X4 q% v) D; s8 ?/ A
  4. >>> math.pow(3,4)
    8 w* }: Q: Z- J2 d  N) Z* q
  5. 81.06 L$ h! M+ ~) v* n) N6 L
  6. >>> . L9 j+ T8 z' I" u" ]$ r: i1 f
  7. >>> math.pow(2,7)$ Q, c( ]6 K8 y. J
  8. 128.0
复制代码

" |' r) }) `# @5 T3 smath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ `) I+ D) |% C: E( ]# n' ~
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% K0 T% X/ p1 M4 V8 G4 B+ B
  2. log(x[, base])
    # m1 Z7 a- p% O! ^# B
  3. Return the logarithm of x to the given base.4 |6 g2 }( S  j9 X# }! l
  4. If the base not specified, returns the natural logarithm (base e) of x.! q# w  [9 u5 b+ @6 r2 L
  5. >>> math.log(10)
    / H% _$ D1 S3 y3 x1 K/ }3 v
  6. 2.3025850929940460 q6 c9 ?" h7 p. o8 W* P) o! B8 o
  7. >>> math.log(11)
    ( o" |1 ~6 k  {, f5 K3 Z2 W
  8. 2.3978952727983707
    . C2 q, ?* x1 m$ }% K
  9. >>> math.log(20)
    ( ?) Y5 U6 A) H4 j+ o2 e+ d
  10. 2.995732273553991
复制代码

% c- h9 v% i: `. X% G- _: Wmath.sin(x)  求x(x为弧度)的正弦值
7 k% U( G! W/ N) V
  1. #求x(x为弧度)的正弦值3 h. W2 t& O( \8 d2 j
  2. sin(x)4 s! _# m8 a1 N
  3. Return the sine of x (measured in radians).; t/ j& a; q9 }- [; `  q$ n  f' m# _( ?
  4. >>> math.sin(math.pi/4)0 c- i6 `3 E$ ?
  5. 0.7071067811865475
    $ O8 r& o% `+ W: L
  6. >>> math.sin(math.pi/2)
    ; [/ ?6 w8 ^2 f/ r/ ?0 h, J/ Z5 j
  7. 1.00 o: t3 i* b" r# `. ]$ j, C
  8. >>> math.sin(math.pi/3)
    " n0 @& h$ f. M1 M
  9. 0.8660254037844386
复制代码

" a, P6 H& |$ `6 _: B4 fmath.cos(x)  求x的余弦,x必须是弧度& N! w6 k# S% X- A4 N# _
  1. #求x的余弦,x必须是弧度9 v1 ~% x' c( D5 U- z& ]$ q, W
  2. cos(x)
    4 _! l, k+ F, f. G& b6 c* H- u
  3. Return the cosine of x (measured in radians)." i0 v* k! P! H/ J- `. S2 U
  4. #math.pi/4表示弧度,转换成角度为45度
    , _& {9 B& `# e/ p6 y. O
  5. >>> math.cos(math.pi/4)
    # \+ X& k$ I1 G5 z+ ^
  6. 0.7071067811865476
    2 Y5 C& M6 X! R- G, _" L- F. F
  7. math.pi/3表示弧度,转换成角度为60度2 u5 ?8 }! k& D! t0 ]& J
  8. >>> math.cos(math.pi/3)
    $ G! y- E1 y; }4 Q/ h
  9. 0.5000000000000001/ J9 b6 }% i) i8 n3 M$ |
  10. math.pi/6表示弧度,转换成角度为30度
    5 Z) ?# c: W3 e
  11. >>> math.cos(math.pi/6)
    3 V6 e* z4 d6 F
  12. 0.8660254037844387
复制代码

$ P. V+ p  z# I  {' Hmath.tan(x)  返回x(x为弧度)的正切值
8 x5 j9 P5 R! C* m5 b
  1. #返回x(x为弧度)的正切值# e* o/ }. P# O) r6 f& B' F
  2. tan(x)
    - c# X% ?! v$ M' o4 @
  3. Return the tangent of x (measured in radians).
    1 H& B$ }# f0 \% B+ @- W; u" N
  4. >>> math.tan(math.pi/4)
    1 c& B  {. S- j/ P' B2 T: C
  5. 0.9999999999999999
    3 V  G1 Q- d9 W+ p5 }1 X
  6. >>> math.tan(math.pi/6)
    & Y5 M. [2 A( l6 m1 s2 L* \
  7. 0.5773502691896257
    ( _, @  g# C" s8 A6 b+ n. [4 d' J
  8. >>> math.tan(math.pi/3)6 i* p/ M3 C" Y: h" d4 [; Z
  9. 1.7320508075688767
复制代码
6 o( Z# K% R( a# r$ z+ a
math.degrees(x)  把x从弧度转换成角度
8 Z* e. f, z; V' B( [, D! i
  1. #把x从弧度转换成角度1 k( z- i; o" f* R. Y
  2. degrees(x)7 h: h5 x% \# `3 Q
  3. Convert angle x from radians to degrees.
    . o' G5 D, ]7 T1 D! U# \% z, g

  4. ! k6 U, Y7 c: Z  X2 {, R# r0 e# z
  5. >>> math.degrees(math.pi/4)
    5 t5 U! A4 p1 o6 r& ^
  6. 45.0
    2 S2 _( i) h; _2 X( K; h
  7. >>> math.degrees(math.pi)7 d5 s  j% N' l! A" f6 }; M  @
  8. 180.0. c% R) ?+ a# z: Z9 ?2 q
  9. >>> math.degrees(math.pi/6)
    - X0 ~, k7 R% q9 B8 b4 H$ p7 w4 L( F
  10. 29.999999999999996' o. [, g! l& }( a+ B
  11. >>> math.degrees(math.pi/3); T( @( ]3 e9 ]3 M, x( w- p
  12. 59.99999999999999
复制代码

: X5 h' ^& }0 i% l* X9 qmath.radians(x)  把角度x转换成弧度
7 e& c' @* E0 i$ R4 B2 n* }
  1. #把角度x转换成弧度5 U# l% L3 B& t, w3 i% F
  2. radians(x)
    8 H) I9 S6 r" E6 f- b/ u
  3. Convert angle x from degrees to radians.8 B" U8 k. k6 |( A+ Y: c4 w
  4. >>> math.radians(45)
    . j" a9 M( {5 v; A. \- t
  5. 0.78539816339744837 Z* r6 F( E- o
  6. >>> math.radians(60): S# e' D. p! C$ E6 ]  [
  7. 1.0471975511965976
复制代码

' i+ H! c! r9 W4 t2 g  j& Wmath.copysign(x,y)  把y的正负号加到x前面,可以使用0# i% [1 g5 n- ]7 H
  1. #把y的正负号加到x前面,可以使用0
    ; ^7 |' k3 N% K- z2 }3 y* L8 i4 g' K
  2. copysign(x, y)
    ! ~: {5 G& d, {9 t/ a' w: Q# N- b
  3. Return a float with the magnitude (absolute value) of x but the sign
    6 t4 }$ W2 L0 y) T& @
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) % g' @/ v2 t7 F! J! m; A+ X
  5. returns -1.0.9 f3 e9 f! R0 R! `$ C: c; `
  6. 5 \: M' R7 d4 c
  7. >>> math.copysign(2,3)
    # _$ w; k" X7 a' m' f
  8. 2.0
    1 V; A; b, }# P5 b% h5 P2 q
  9. >>> math.copysign(2,-3)
    7 V5 R1 E4 `( t, J
  10. -2.0
    5 [2 e( x7 z, j4 j
  11. >>> math.copysign(3,8)
    0 w, M( m1 q! q! m
  12. 3.0; o& h9 L# R" O  P, q
  13. >>> math.copysign(3,-8)0 K& d7 W; j7 s! p3 f
  14. -3.0
复制代码
6 I, n2 I, C$ Z
math.exp(x)  返回math.e,也就是2.71828的x次方
  {$ _: F. f1 P- p0 M8 l0 B
  1. #返回math.e,也就是2.71828的x次方
    ) f0 b0 d/ W" e. G7 g$ [0 P  d
  2. exp(x)& W4 q# e2 q2 z8 a) Y# d# u
  3. Return e raised to the power of x.
    4 Z  T% b% L" K( v  s/ }3 Z

  4. % f# y" E. F' e8 f, }2 I  J3 k. z  \
  5. >>> math.exp(1)! Z( U) J& d4 d/ f$ H7 Q
  6. 2.718281828459045
    - ~- \! ^" I/ t, i
  7. >>> math.exp(2)1 K* @/ F5 {% q0 j
  8. 7.389056098930657 p$ @1 @% e) ^5 C2 r" e, G
  9. >>> math.exp(3)% m2 |4 B8 b% o$ C1 t
  10. 20.085536923187668
复制代码
4 O( m  H  c! g  J( z" S9 }
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减16 [- C. W5 J1 j5 o( K$ q" M
  1. #返回math.e的x(其值为2.71828)次方的值减1% \$ }: a( b; K6 @8 y. m5 D/ J
  2. expm1(x)8 I  L* O% t5 R: k* }; d" G
  3. Return exp(x)-1.! A0 J# r) F: p9 f: z
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.% y7 g7 P0 m' M1 g

  5. 4 W) x: Q8 p! s% B! w) e
  6. >>> math.expm1(1)6 L( ~+ I" i1 s, w
  7. 1.718281828459045
    8 D9 ^: [$ `5 M8 ~- N: n' G
  8. >>> math.expm1(2)7 Z, k2 Z7 d0 v# _
  9. 6.389056098930653 m' x. }- Y' y9 s% \8 @
  10. >>> math.expm1(3)
    : g! k7 k9 s" h3 ~1 ?% R; y
  11. 19.085536923187668
复制代码
. W5 n# J- T- J. O5 K% R, Z* Q
math.fabs(x)  返回x的绝对值
  c* W4 }+ _0 N) e
  1. #返回x的绝对值; M& o: C6 d3 V
  2. fabs(x)
    - {9 C* e2 h! s: U
  3. Return the absolute value of the float x.
    / O) ]( D( W8 k
  4. 1 d. w& [: ~, S8 o: ?
  5. >>> math.fabs(-0.003)3 O: I% Y/ V# i/ \
  6. 0.003% [0 [: f* G7 o, I6 i$ H( L& m
  7. >>> math.fabs(-110)
    ( _/ B$ l5 u* u- l& l5 F2 n
  8. 110.0% ^) Q4 I% G- \% e, G) g5 z; Y
  9. >>> math.fabs(100)- I' q: P( f9 U( [; @3 i
  10. 100.0
复制代码
: h3 j  G3 V4 K* p, m
math.factorial(x)  取x的阶乘的值$ Q; ?) n$ ~" E& g
  1. #取x的阶乘的值! M8 ~7 S+ `6 X
  2. factorial(x) -> Integral
    . g8 a/ p$ D$ D! P
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    2 E% Q. s, |0 i
  4. >>> math.factorial(1)
    1 C, v2 C/ `( \# _' S# X& ?
  5. 1
    - v- }- i/ j& w) @
  6. >>> math.factorial(2)8 H% s. h! M, y9 ]' }9 U
  7. 2
    - t& m- a& }& u7 W( Z
  8. >>> math.factorial(3)5 `" k/ C* ~9 ?( `  G% a0 O5 P
  9. 6
    ' e: s$ @8 x  w. A5 [1 @
  10. >>> math.factorial(5)
    6 J1 u( A$ L3 f. G- L) f* ?
  11. 120
    6 u0 h/ y. R5 {2 F9 O2 M0 e
  12. >>> math.factorial(10)) g  k- P4 L% F; K; x* |0 _
  13. 3628800
复制代码
2 w" c) S- `" r) A; N; E- K& \: K
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数+ q* c  H/ h/ ?/ O2 ?, O
  1. #得到x/y的余数,其值是一个浮点数
    3 j# n4 h5 T& R1 X: [# V
  2. fmod(x, y)0 [  c! `9 P9 v
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ( Y) H0 r+ K9 S; C
  4. >>> math.fmod(20,3)
    1 W5 d5 M6 ?; Y- U
  5. 2.0
    , V6 G- _( `+ M  U
  6. >>> math.fmod(20,7)# Q7 @! X* W' K7 ~
  7. 6.0
复制代码
& U: h0 a' p9 }( b
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 b* l" G! b: A/ b2 z
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . ^, P7 C1 L+ l% _' v3 k
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 K- k3 h8 l4 B. c% f4 U9 c
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    0 U1 u4 e% P8 P( G* K
  4. frexp(x)
    1 D! c) H2 N' ]+ R7 {5 B
  5. Return the mantissa and exponent of x, as pair (m, e).+ k; t$ Q0 \7 p- e6 e% y
  6. m is a float and e is an int, such that x = m * 2.**e.
      o3 O$ O+ V& K- L" O' ?* U9 p
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.$ D/ R- i$ K! M% R
  8. >>> math.frexp(10)
    5 ?. ^& {4 B2 g  B) O
  9. (0.625, 4)& P6 Y+ F3 Q0 W2 `  |3 O
  10. >>> math.frexp(75)6 K6 M% R8 c$ f" u( [5 n7 d
  11. (0.5859375, 7)1 I% \* S  W8 Z
  12. >>> math.frexp(-40)' @) V: t9 J3 q6 S
  13. (-0.625, 6)
    ; X5 m6 F- y' ]/ @; K
  14. >>> math.frexp(-100). D$ ?# V2 U! M% `( R3 e
  15. (-0.78125, 7)
    - y: j! C/ \! Y! Y) \2 n
  16. >>> math.frexp(100)
    0 U* s* h0 f) [* c1 s5 z
  17. (0.78125, 7)
复制代码
6 F0 q# {0 @% H7 R2 s( q1 n
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
) n' h" D1 W: t/ K) R- n, {. a1 I
  1. #对迭代器里的每个元素进行求和操作
    $ O+ @+ K8 j& h) B% x
  2. fsum(iterable)/ d( {0 F) R. b6 V$ s
  3. Return an accurate floating point sum of values in the iterable.- C" Z# T' Y; z. @. _
  4. Assumes IEEE-754 floating point arithmetic.% b+ u* M0 S/ Z1 K5 @& H6 O
  5. >>> math.fsum([1,2,3,4])5 C! U: K- {. W* i
  6. 10.0
    ( W- W( `. X2 E% y- l
  7. >>> math.fsum((1,2,3,4))
    % j! q3 e- _1 O( K
  8. 10.0
    : D0 J* W0 U4 f& q. o8 T& o
  9. >>> math.fsum((-1,-2,-3,-4))) J1 w1 Z0 Z) k
  10. -10.08 L3 f" E* ~( b9 f; p+ X$ j
  11. >>> math.fsum([-1,-2,-3,-4])
    & g5 J( M% h4 W/ K+ C- f9 a& ~* E& ]
  12. -10.0
复制代码
/ P; |$ z) K) x. k* I% y
math.gcd(x,y)  返回x和y的最大公约数
! c) f+ i5 y% Z; E3 n0 _& i
  1. #返回x和y的最大公约数. H( O# @6 ?9 j$ d2 a8 A6 J
  2. gcd(x, y) -> int7 O% t/ D0 B; Q. K3 C0 I
  3. greatest common divisor of x and y/ F+ g  X6 o& \  N: Z5 J
  4. >>> math.gcd(8,6)
    & z6 n* |  u9 ~% _* H' r, o
  5. 2
    $ k; t; b- M8 L5 d  d
  6. >>> math.gcd(40,20)
    " Z( j2 g* ~. U# F; F% C
  7. 20- D+ O3 e, F8 @) h
  8. >>> math.gcd(8,12)& N- n! G/ g) ~" s  V4 n
  9. 4
复制代码
% e$ c0 U* `- a
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
1 t( {" d( K8 R8 |' W0 d$ ^
  1. #得到(x**2+y**2),平方的值  e" ~- o6 Z* M) C
  2. hypot(x, y)
    ! I% c3 W) C7 T5 J+ }% J9 A! ]
  3. Return the Euclidean distance, sqrt(x*x + y*y).5 s+ Z" d# t3 }2 w9 @
  4. >>> math.hypot(3,4)
    3 B3 B1 ^* K& m- z! T, w
  5. 5.0+ t/ @+ \5 w' d/ G! `* h8 G
  6. >>> math.hypot(6,8)
    # B) c! M; W/ m4 g9 L" b
  7. 10.0
复制代码
# Z: G, `+ q- j
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
& k0 y2 r" G. o, t7 v
  1. #如果x是不是无穷大的数字,则返回True,否则返回False( r6 S+ `, o! E: \: T# ?
  2. isfinite(x) -> bool8 t( v1 ~5 a' l$ A4 u' h0 M
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.+ \$ _: e0 A+ W
  4. >>> math.isfinite(100)
    $ s1 d6 N& a- B& E& b& x% d
  5. True! O# }9 F6 `7 J% m  @
  6. >>> math.isfinite(0)8 x% u7 E) R( G" O% N- [
  7. True  c+ k7 F* {( {; j: J* B, {& `
  8. >>> math.isfinite(0.1)- t7 a! s0 V9 \3 n2 K2 N3 b' l$ _% |& D
  9. True
    & r2 B& X; Q/ `" q/ ?
  10. >>> math.isfinite("a")
    3 m/ S" y7 s0 U
  11. >>> math.isfinite(0.0001)
    , h% l) _- d" n, O7 p0 o  l
  12. True
复制代码

: a9 s8 g0 }/ r, _# n: Rmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
( m1 t5 R9 ?& Y, b& f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    8 ^5 W8 b$ m% Z3 P% T$ V0 u
  2. isinf(x) -> bool0 `5 X. T) F- m6 V0 ]) f# F
  3. Return True if x is a positive or negative infinity, and False otherwise.) [4 ~  _( l* ^# [& S3 _/ t
  4. >>> math.isinf(234)& k' j  S+ F! D% ~( i! l
  5. False- @( `1 i# M9 t" S# s$ H* `/ A, Q
  6. >>> math.isinf(0.1), t3 G$ @; Q4 B1 x0 Q- y
  7. False
复制代码

% K/ ?  l8 y( }! y& W: Umath.isnan(x)  如果x不是数字True,否则返回False! Y& E' e+ N, t0 R5 s  }0 w
  1. #如果x不是数字True,否则返回False0 b3 r) c4 ~3 b+ Y6 o
  2. isnan(x) -> bool1 ^7 K& s& d( Z
  3. Return True if x is a NaN (not a number), and False otherwise.
    5 T0 Z- F* C: V& a$ Q- c
  4. >>> math.isnan(23)9 M3 z7 Z* P# x5 c5 i$ `
  5. False1 ~) Y- c! a# j  J+ G: k
  6. >>> math.isnan(0.01)
    : e. ~5 f# Q( C7 e7 n: v
  7. False
复制代码

8 D' D0 a; O6 ~1 f; q6 @" w: s. Ymath.ldexp(x,i)  返回x*(2**i)的值
9 i5 l; x5 k8 q0 p# j# x
  1. #返回x*(2**i)的值
    : _, t' p# R. V
  2. ldexp(x, i); T5 k4 s. ?6 i+ E/ H! j
  3. Return x * (2**i).0 l+ w4 x0 w2 J! t. d
  4. >>> math.ldexp(5,5). l( j6 R: y- q# A
  5. 160.0
    ! _5 W0 {! \) N7 ?# j+ }3 A
  6. >>> math.ldexp(3,5)
    4 l3 o" S, D" G/ }% v
  7. 96.0
复制代码

, T& C- V/ Y: \- _math.log10(x)  返回x的以10为底的对数
' s9 H. R4 c1 g, E; y
  1. #返回x的以10为底的对数
    7 {$ E- w, ?" `+ h
  2. log10(x)& h1 M4 S9 @5 p! ?* n* f1 Y
  3. Return the base 10 logarithm of x.- Z" ?0 t" m7 G2 R3 s
  4. >>> math.log10(10)1 ~# f# n# g8 ?6 S3 ?' j+ z
  5. 1.0
    ) ~& }) d& J# Q5 }5 w* x
  6. >>> math.log10(100)5 W9 b! t2 Y. T- @
  7. 2.08 m& H9 S) {5 k  e
  8. #即10的1.3次方的结果为20/ I2 k1 Y) x4 ~9 K( Y
  9. >>> math.log10(20)
    8 F0 R$ I% V9 N# d4 @; O
  10. 1.3010299956639813
复制代码

1 O1 O. }( S8 ?6 l: J; Q$ J: nmath.log1p(x)  返回x+1的自然对数(基数为e)的值
& R* P  ^4 t# r5 e# O" M
  1. #返回x+1的自然对数(基数为e)的值
    ' `% H) o& E* S, J, [$ B9 g" b
  2. log1p(x)4 z; Y) Z3 f: _
  3. Return the natural logarithm of 1+x (base e).# c3 G9 l; x; W" k8 x8 H
  4. The result is computed in a way which is accurate for x near zero.
    - q4 z$ o9 y: m0 ^
  5. >>> math.log(10)
    ! V* N+ b! j) C0 j$ w
  6. 2.3025850929940468 r& F( {, i& Q! j/ ]
  7. >>> math.log1p(10)
    & `9 [  C( l1 ~9 f
  8. 2.3978952727983707
    9 A7 Z; k: g0 g( t( {
  9. >>> math.log(11)2 W! [! m  a' v" z7 h6 F
  10. 2.3978952727983707
复制代码

6 e) w' M9 R; cmath.log2(x)  返回x的基2对数/ B5 R+ C, Q1 L* u+ T
  1. #返回x的基2对数% g" u4 w: R: y
  2. log2(x): j; q% f, x/ X; t% Q4 C
  3. Return the base 2 logarithm of x.% O- {8 K- x( \+ }# H5 X2 `
  4. >>> math.log2(32)
    - _! d) [, j9 J4 L
  5. 5.0
    / N# e0 b, W9 V- O1 w
  6. >>> math.log2(20). W# P  `. _0 M2 W
  7. 4.321928094887363
    9 Q4 j0 B& Z6 c( A6 l
  8. >>> math.log2(16)) m) f. A% k8 H) z
  9. 4.0
复制代码
- S! a# o0 M) O/ ~* {/ q
math.modf(x)  返回由x的小数部分和整数部分组成的元组! E9 T) K* J, q4 ^  S
  1. #返回由x的小数部分和整数部分组成的元组
    0 s& f* W) d' _" c$ B
  2. modf(x)7 X( N5 Q  q6 P1 K# v1 m6 R# Y$ f
  3. Return the fractional and integer parts of x.  Both results carry the sign; }! E( L; K. F7 p: q8 h
  4. of x and are floats.
    , q& n! e) I0 ?( ~6 B3 G
  5. >>> math.modf(math.pi)7 }/ C  T, F& i2 T& Q# u5 L
  6. (0.14159265358979312, 3.0)9 F" v$ O; D% T1 s% M& ]7 n7 i- o7 l, u
  7. >>> math.modf(12.34)0 J) r: P; @7 l3 X
  8. (0.33999999999999986, 12.0)
复制代码

; l' _! m1 M9 a% Cmath.sqrt(x)  求x的平方根7 }  K: ?. S( F2 J5 g
  1. #求x的平方根6 G' B! q5 b$ i# Z
  2. sqrt(x)
    & ~1 U+ |% M/ L; n( N
  3. Return the square root of x.% H7 o( h2 U9 W! K1 @; v2 M3 D) }
  4. >>> math.sqrt(100)- K. X: C# J+ D% N% U
  5. 10.0$ I0 s( ]- e( v% R) w* H
  6. >>> math.sqrt(16)% A# p7 S! v# F8 V
  7. 4.0
    : }, {( ]4 ~0 Y) [+ i# N9 @
  8. >>> math.sqrt(20)( Y. h, `2 n3 a% e8 f8 D( K
  9. 4.47213595499958
复制代码
+ \2 s0 _1 ?' |4 A/ z% [: N) R& M5 g
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分& R6 O; Z% r: T
  2. trunc(x:Real) -> Integral) @1 ^4 t3 }% L4 T% ]5 s: m2 u8 @
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.& K& f# g; F/ x
  4. >>> math.trunc(6.789)
    3 E6 C' N: \  N3 T" y% {
  5. 6
    : w9 R) s, j- f9 [' i8 L
  6. >>> math.trunc(math.pi)- C4 T1 d& y( m( H; s/ y
  7. 3- \& K. Q6 x, u
  8. >>> math.trunc(2.567)
    : K. M+ X/ A, J- G' H" _% M
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-23 23:34 , Processed in 0.081947 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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