新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
0 b4 }' Y3 K( i$ |1 E& \9 y2 y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: c0 x. ~* q* V$ G# U

4 I$ f) Q) n* O( W方法1
0 @+ L; ^! w- H' h* p
  1. >>> import math0 n9 {2 Z3 e2 @' X
  2. >>> math.sqrt(9)0 X$ M  f  ]- b3 ^5 ?, s
  3. 3.0
复制代码
方法2
" K2 P& k6 J3 ^9 P& M* i0 @, @
  1. >>> from math import sqrt
    , s7 a/ T+ \3 P
  2. >>> sqrt(9)
    ! r+ v' v" F7 E. P% b5 r
  3. 3.0
复制代码
* H0 {, D& E' h9 g$ t; E3 {& k! u; o


) n1 i! k5 l( W
math.e  表示一个常量3 O( X: u8 K; B( G! Q. g
  1. #表示一个常量" n) Z/ P  q9 c7 Q/ g' g' Q
  2. >>> math.e
    9 z6 M- Y7 T5 v- v8 D* e9 @
  3. 2.718281828459045
复制代码

! F0 {) L, x0 `0 o! Gmath.pi  
数字常量,圆周率
3 e1 L' e4 `2 K& B0 r0 Q
  1. #数字常量,圆周率4 X" [7 S. N) ]. W8 |
  2. >>> print(math.pi)$ u" b( I3 z2 H0 I
  3. 3.141592653589793
复制代码

* y! o6 w8 @6 r. G- V9 |math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

& v5 d7 A2 E" s+ f: p# N
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    5 d( X0 N9 y. s4 G. w8 S2 p
  2. ceil(x)
    8 |! r1 I) T: O6 x( n
  3. Return the ceiling of x as an int.
    . D1 B9 g) F/ b# C" d6 B( N4 B
  4. This is the smallest integral value >= x.
    1 `6 `" g' D! g7 e; R. e

  5. 0 ?  S  F: ]' ~0 y+ a+ w" R  S
  6. >>> math.ceil(4.01)
    3 C5 }! Q5 ?9 T5 B3 i, `
  7. 5' m% y5 U2 [/ K9 y; E+ n7 \8 x
  8. >>> math.ceil(4.99)5 x- w5 _1 q! `; J# T! @; {. w* \
  9. 52 {4 H6 s5 N: P# E
  10. >>> math.ceil(-3.99)
    / s2 L$ P- {" Q. K
  11. -3
    ; A, A' }$ d  k+ p& j; H4 B
  12. >>> math.ceil(-3.01)1 e/ K8 t# N$ ?- ~5 Z' J: y
  13. -3
复制代码

( C! i; g' J9 d" _2 h! K; l# I; amath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 n9 V$ ]0 R) f; m' `+ R
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 }4 q$ b: @. b8 X$ D# \( Q
  2. floor(x)
    $ O5 |3 O( B8 ?9 o0 |
  3. Return the floor of x as an int.
    1 j- _$ J* i: P9 g# \9 g; B# t: Z
  4. This is the largest integral value <= x./ D6 j+ A  z. j& @3 i% m
  5. >>> math.floor(4.1)- S# M' @' g8 j3 f. N
  6. 4- C; j8 A0 M4 n* `% L$ s+ U
  7. >>> math.floor(4.999)
      O9 P/ R# k1 v: m8 A) K* M1 q0 N
  8. 43 N5 |! R5 @8 |. ^) m" X! C
  9. >>> math.floor(-4.999)
    6 |1 g$ y+ m4 ]. H! l
  10. -5
    4 ~; f+ m$ l4 G: g7 e
  11. >>> math.floor(-4.01)
    " z& h/ L( O  l+ x# ~2 C
  12. -5
复制代码

) i" V. Y+ n( ^# ~" imath.pow(x,y)  返回x的y次方,即x**y. M# U( b9 l: q9 k# n: _
  1. #返回x的y次方,即x**y/ V$ g- H: l+ L/ F6 C) C
  2. pow(x, y)
    0 {3 @1 F! D/ d, S# f' z) A
  3. Return x**y (x to the power of y).. ~5 R8 Q" |: ^1 o0 s2 t
  4. >>> math.pow(3,4)
    & j# w) u3 q. P" z; |
  5. 81.0, T0 v2 I& f/ R5 ]6 h5 l; S
  6. >>> : `* ], W# o& J9 J" |8 b
  7. >>> math.pow(2,7)
    ; i5 u# y0 j3 j8 S" \' C3 v9 R
  8. 128.0
复制代码

" I/ N8 ]4 P+ @- x+ T+ zmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# ?9 @. N- z/ Q2 S
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    . B" T8 E: i7 W% `- v. |4 u
  2. log(x[, base])/ _. j# f( r0 K. F- M( P
  3. Return the logarithm of x to the given base.  U1 P; `6 C( c6 n
  4. If the base not specified, returns the natural logarithm (base e) of x.
    . I, Y( K$ w0 F/ z3 ^9 Y! P! x8 V
  5. >>> math.log(10)4 b. s! [8 Q( j* Y
  6. 2.3025850929940461 q. W3 O! Q7 D& ?6 w
  7. >>> math.log(11)
    % X8 J6 N8 L! m' }3 j; b
  8. 2.3978952727983707
      c7 X7 C$ b7 k% u% T7 s
  9. >>> math.log(20)
    0 `' V( r2 _6 a+ S6 o' Y
  10. 2.995732273553991
复制代码
; f5 `) X4 a5 n0 G
math.sin(x)  求x(x为弧度)的正弦值: r( @2 V2 J  K& p* r% E# m- Q, [3 W. R
  1. #求x(x为弧度)的正弦值; m: f% Y6 L, P' e$ ?' u$ f) n: E
  2. sin(x)
    1 S4 L( J/ `, L
  3. Return the sine of x (measured in radians).( k8 ]9 u) v/ g& m+ @
  4. >>> math.sin(math.pi/4)3 i  C' E# |; k' x( c) n
  5. 0.70710678118654750 G' E- W6 q% X+ R/ U
  6. >>> math.sin(math.pi/2)
    , j0 c$ F. y, I- {. `- t6 w
  7. 1.08 c' u4 y8 h; N! m" j0 K& D
  8. >>> math.sin(math.pi/3)
    . v( z% v% C% g
  9. 0.8660254037844386
复制代码

! r& w9 K9 R- Omath.cos(x)  求x的余弦,x必须是弧度
5 C, u& x5 N' x! u
  1. #求x的余弦,x必须是弧度1 J6 I1 v8 s$ C. F
  2. cos(x)
    - e6 J! |5 }. \: Z1 }- w6 {- j' M
  3. Return the cosine of x (measured in radians).& h/ O6 ?- Q6 J1 p
  4. #math.pi/4表示弧度,转换成角度为45度
    + Z- I+ ^- y; B2 R; h6 O
  5. >>> math.cos(math.pi/4)! O+ [) t3 _( N# X* t2 J9 {3 O
  6. 0.7071067811865476
    7 Y4 @& l4 F* ]
  7. math.pi/3表示弧度,转换成角度为60度
    7 B. X% p/ X! P/ `9 J8 |( }
  8. >>> math.cos(math.pi/3)0 y- _8 e9 v* J  |6 n8 C* h
  9. 0.5000000000000001+ j7 R0 A' |+ f( W/ e- O
  10. math.pi/6表示弧度,转换成角度为30度
    8 H% q0 x' a+ }
  11. >>> math.cos(math.pi/6)
    : |8 J% f# q7 D* A# O, ?3 i7 f
  12. 0.8660254037844387
复制代码
( L) m( h) L$ q# t/ E# n
math.tan(x)  返回x(x为弧度)的正切值
3 N+ e1 m# r( a0 s
  1. #返回x(x为弧度)的正切值4 m- F; I" ^; }6 ^+ H
  2. tan(x)
    $ J8 ~" h, n) a! u% M4 v
  3. Return the tangent of x (measured in radians).
    1 ~, b  E! N* N7 ]8 ]4 X8 W% A4 p
  4. >>> math.tan(math.pi/4)
    0 ]/ r& O; Z* }4 y  m, v
  5. 0.9999999999999999
    3 ^1 ^9 z' B- p5 [: T  K
  6. >>> math.tan(math.pi/6)! b# ~. v; R8 U/ [$ p1 ]3 q
  7. 0.5773502691896257
    8 W" a) y4 ^- C# R. C! h
  8. >>> math.tan(math.pi/3)  k* t1 r- R, \' g6 Q* [6 V: m2 ~
  9. 1.7320508075688767
复制代码
3 {# K9 Y' c# c4 ~9 R
math.degrees(x)  把x从弧度转换成角度/ n0 c) S& y& N3 q+ O6 s
  1. #把x从弧度转换成角度5 q2 l' C8 P8 d/ G* R; V; t
  2. degrees(x)
    & n, ^" `5 R' J% M# f8 O, i
  3. Convert angle x from radians to degrees.
    2 D- i1 X! c( M! {+ U4 [/ R
  4. ' N* W1 C  Z' i) l/ O
  5. >>> math.degrees(math.pi/4)
    ) m2 A: }; `$ e# X4 ?$ Y! M
  6. 45.0  _9 U" M0 q/ v0 l. W1 E2 f
  7. >>> math.degrees(math.pi)
      q6 _" c0 \( G( ~1 A0 N5 @
  8. 180.0# r7 f# S7 ~( ~2 n& U. c
  9. >>> math.degrees(math.pi/6)0 S" x6 r; u! ?* y# @, i1 v- K
  10. 29.999999999999996( u4 Z# g- M7 A3 B: `* Q; N
  11. >>> math.degrees(math.pi/3)# D+ \" G. E9 X5 i8 W: V
  12. 59.99999999999999
复制代码
* N* d1 K3 r5 r0 v
math.radians(x)  把角度x转换成弧度0 O' J+ w- K: S" G
  1. #把角度x转换成弧度
    + F3 n# [/ m) _; z6 v, ?/ W
  2. radians(x)
    0 S- c- d  T3 m% W1 d( `6 w
  3. Convert angle x from degrees to radians.
    0 V% C4 f& @4 C; R$ A( h
  4. >>> math.radians(45)( Q1 @) w; B1 S  b; m5 Z) X9 k* V0 a- @
  5. 0.7853981633974483
    ! ^; \: |9 T+ o6 Y
  6. >>> math.radians(60)
    - i* w7 O. z; a. `/ F+ q* _0 Z
  7. 1.0471975511965976
复制代码

, H* y, \) _, @4 F9 V1 `- A8 dmath.copysign(x,y)  把y的正负号加到x前面,可以使用00 \2 i3 n8 c# x
  1. #把y的正负号加到x前面,可以使用07 T9 I% Y" V! @9 R1 }9 R" a
  2. copysign(x, y)$ X- w+ t* R- ^, o
  3. Return a float with the magnitude (absolute value) of x but the sign
    3 @) U8 I- N) r7 m  P; m8 i
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    $ R+ ~1 E$ X3 y6 x$ e1 f& i# U- F
  5. returns -1.0.
    ) r* {3 y" Q- c* |- q3 e; |# G% |
  6. ; v& W) D' X/ c0 X) r+ D
  7. >>> math.copysign(2,3)
    % S8 Z0 ~, D: ]0 D( p4 U6 u" b
  8. 2.0
    - w; X7 x6 a$ t+ \% M% e, ~  h
  9. >>> math.copysign(2,-3)6 T0 I% N. e9 l5 n* X8 R
  10. -2.0( Q& ^9 J# @% ^' ~# ~
  11. >>> math.copysign(3,8)
    . s" \: W% \3 m- x2 ]6 |
  12. 3.0) q7 P+ Q+ u# D5 T+ w+ T1 y" u* T
  13. >>> math.copysign(3,-8)
    3 H5 f+ N1 Q9 C5 S1 l
  14. -3.0
复制代码

5 d$ _5 R3 Z9 R5 q# E9 P; ~2 E$ Umath.exp(x)  返回math.e,也就是2.71828的x次方
% w# V, ~7 u7 m6 S8 v
  1. #返回math.e,也就是2.71828的x次方
    & m% c$ i# v9 o( Y% V( }
  2. exp(x)6 @" B* H) t2 F. D/ U) R
  3. Return e raised to the power of x.5 I; h8 x6 ]: i; u. J% x
  4. . I( W: g& e& _/ ~3 a  F
  5. >>> math.exp(1)
    : k' r! s* |0 d4 I( K' k9 x. G& K
  6. 2.718281828459045
    6 o! J$ G& g+ b6 e, G# {" g$ w
  7. >>> math.exp(2)4 E) X4 e, f. P$ u0 d0 X
  8. 7.389056098930659 Q& ~. |  O  W
  9. >>> math.exp(3)- e9 D9 h: V* @
  10. 20.085536923187668
复制代码
! H  h4 B) F, b* X
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1  w- f" `9 Y/ e' P! q- n- l
  1. #返回math.e的x(其值为2.71828)次方的值减1/ C0 `( F; g6 R3 |9 O
  2. expm1(x)5 R6 k" I. Q1 y
  3. Return exp(x)-1." ^! j5 K2 D, k/ K; s  ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    , z& s" M5 r, A, x5 u" q, T9 ]" @
  5. * l5 q8 _) c1 g, [! X% Y
  6. >>> math.expm1(1)
    # k) A& Q0 s* g) i* _  Z
  7. 1.718281828459045. ]1 H, M7 u3 y: v% W
  8. >>> math.expm1(2)
      f$ b( F/ h" P
  9. 6.38905609893065/ p" A9 r0 B) R4 R3 c1 R( w
  10. >>> math.expm1(3)7 ~/ O2 [% R8 x& {! @  l# r; k1 L* S
  11. 19.085536923187668
复制代码

% a1 B* ]' ?! \4 @$ tmath.fabs(x)  返回x的绝对值
* q6 `) ~) p3 E: j7 h' l
  1. #返回x的绝对值
    1 L, w0 M* _6 k  l5 `5 k. Q4 n5 [3 }; C
  2. fabs(x)
    & D3 i0 I: K; Q4 v8 H+ L3 X
  3. Return the absolute value of the float x.$ I% b* w- H8 W& L5 D

  4. ' Q/ {; p. z' S6 m) Q" ]4 \; m
  5. >>> math.fabs(-0.003)
    : s& j2 E: B) j2 g0 e0 R, ]6 I7 a; F
  6. 0.003
    ( t/ t# Z  G+ n" x% U3 o
  7. >>> math.fabs(-110)8 a. ]8 x% ^1 r% B. Z5 ~
  8. 110.0
    5 \4 w. D4 b) Y: K* C; [/ t
  9. >>> math.fabs(100)
    7 _2 X8 j" v8 _. J/ l7 o7 t
  10. 100.0
复制代码

0 V8 S1 l2 U% K  @6 G5 i# O. Y$ [: \math.factorial(x)  取x的阶乘的值0 t  k- `: q. O2 _
  1. #取x的阶乘的值. k' W9 _* S& M5 s* }$ A& j3 i) Z
  2. factorial(x) -> Integral( M# h7 r  w) V) T
  3. Find x!. Raise a ValueError if x is negative or non-integral.! i& x9 H4 V+ q2 V1 L3 E
  4. >>> math.factorial(1)5 @7 ^1 u8 i$ k% p* P
  5. 1/ k4 ~5 x2 a! v  a
  6. >>> math.factorial(2)+ R1 q  ^/ d8 H: s! H. F4 O, s" d+ }$ Y
  7. 2
    # S  ?+ H& v! `
  8. >>> math.factorial(3)
    ! y8 |8 J9 j2 ]' W% q1 e! E* E
  9. 6
      }. S& }6 v$ Y% ]9 ]5 d
  10. >>> math.factorial(5)& M" V+ y$ }) [) Y1 Q  C1 ]3 B
  11. 120( v& y5 c! W% i
  12. >>> math.factorial(10)
    , s& R6 P/ W' C# u5 b2 S% z4 H
  13. 3628800
复制代码

4 v6 `8 D* z# Y3 [' T. zmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数. g' F/ X0 b: F$ x1 s) l( i
  1. #得到x/y的余数,其值是一个浮点数( \0 [$ b& O* M4 T% C. s4 G
  2. fmod(x, y)
    1 H9 F5 G  M; X1 k  S
  3. Return fmod(x, y), according to platform C.  x % y may differ.4 M2 K4 c2 H: R2 L0 f
  4. >>> math.fmod(20,3)( l- s  D' u  v
  5. 2.0
    . Q7 w) q2 W" H; V0 o9 s' u2 v
  6. >>> math.fmod(20,7)2 f$ x) h8 i1 e2 D* W* J; T
  7. 6.0
复制代码

  R0 u. y9 O. s3 j8 M* M( `math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, W. }0 i* W# n, I& Z6 z
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    , _. d. ]! A) d
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    5 |, N/ i0 [  j
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    4 K1 G+ k% y0 L$ [
  4. frexp(x)3 `8 O# n% V3 @- Y- O6 J$ H" K3 L
  5. Return the mantissa and exponent of x, as pair (m, e).; r( z5 }( T; A( P
  6. m is a float and e is an int, such that x = m * 2.**e.2 e& ^6 w% [2 U0 U8 J8 T
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    5 z/ m; S& s( W: w0 v; M6 x
  8. >>> math.frexp(10), j/ R- r' r& F4 [3 x  O3 V/ A7 |
  9. (0.625, 4)6 f+ i* I8 m. B; D
  10. >>> math.frexp(75)
    8 a7 ~4 n9 f3 l- `5 T; K- j
  11. (0.5859375, 7)
    ' l+ u1 u( U- r, H
  12. >>> math.frexp(-40)
    9 [' i+ s( v6 M8 b7 }
  13. (-0.625, 6)5 P$ z3 [. W5 }5 X  ~" H! E
  14. >>> math.frexp(-100)
    ! o$ U/ p  G0 X9 C
  15. (-0.78125, 7)
    1 p: v) F0 z; ?$ f
  16. >>> math.frexp(100)8 ^' v- N0 X0 w3 b! M2 [
  17. (0.78125, 7)
复制代码

  t  R4 c" q) T, m, smath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列3 A8 \% }' i( S# T8 L  w
  1. #对迭代器里的每个元素进行求和操作% _) a! i# T) T, J
  2. fsum(iterable)
    3 {( I; X7 y* S' N+ i( x$ T) R: d0 }+ g
  3. Return an accurate floating point sum of values in the iterable.; y# l8 u. f9 E6 n3 D. S9 R
  4. Assumes IEEE-754 floating point arithmetic.9 ]6 V; a, F! G2 T2 c% K/ Q+ w; F
  5. >>> math.fsum([1,2,3,4])* ?. u5 ~2 Z1 ]' {: O3 e/ B
  6. 10.0
    3 F( w  E+ W' Y# P- l6 q
  7. >>> math.fsum((1,2,3,4))
    % J3 }% a' G) ]7 u* v
  8. 10.0
    ' j- [6 Z3 m$ V/ `' O, ^
  9. >>> math.fsum((-1,-2,-3,-4))- W% ~# q% J$ W. ?: u& ?3 B( D3 B
  10. -10.0
      Z0 z, x! v" R7 R. N1 c
  11. >>> math.fsum([-1,-2,-3,-4])
    8 _3 }) B" N6 N/ d& p6 t) K: P- r
  12. -10.0
复制代码
+ N1 m* n2 q& e" C
math.gcd(x,y)  返回x和y的最大公约数3 O# X% R0 @' d) S  T
  1. #返回x和y的最大公约数
    ; W( X; Z2 ~2 Y! A) v7 |; @
  2. gcd(x, y) -> int8 J/ \( ]  ?% j- K
  3. greatest common divisor of x and y
    ' y' z5 [8 l5 R0 j  C
  4. >>> math.gcd(8,6)
    9 I$ X, X% }) Z0 V" h& Z
  5. 29 q, C6 }  q0 _/ A, ~5 w, N7 Y" ~
  6. >>> math.gcd(40,20)
    6 q0 n! g& X9 W& A
  7. 208 l- z# ~3 A8 \* E
  8. >>> math.gcd(8,12)
    , T; e* P1 |5 z% \( q. l9 K
  9. 4
复制代码

) _- l9 ?! \, c1 C: rmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
2 _6 J% G( J7 ^2 s3 c% q
  1. #得到(x**2+y**2),平方的值
    - s. F# v1 U( J) \5 Q2 Z, |# [
  2. hypot(x, y)
    ) v) g3 |, m; S; L6 d1 e% z
  3. Return the Euclidean distance, sqrt(x*x + y*y).* j3 g* y! m+ o$ V% [8 ]4 t0 Z
  4. >>> math.hypot(3,4)
    & P2 M; j: [/ q# V! l
  5. 5.0& [+ k, ]3 B0 {& R% D8 A; J
  6. >>> math.hypot(6,8)
    8 J4 s, l" F2 h& r* ~7 G
  7. 10.0
复制代码
# S7 b& s7 V4 |2 u# `
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False+ W* X) _$ F3 D( Z6 T- a$ Q9 O
  1. #如果x是不是无穷大的数字,则返回True,否则返回False( r2 @& A9 X8 M! R- V
  2. isfinite(x) -> bool; }& J/ c0 A& c4 G6 Y- B6 y
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    , [7 b( f! p7 j
  4. >>> math.isfinite(100)* p* b& ]2 @5 Z2 F# l( m
  5. True' n  n2 M' b5 }; H% {/ X
  6. >>> math.isfinite(0)
    5 O3 p. W+ B- ?0 z* r0 A
  7. True" x  H: U8 ?( e" X8 Q
  8. >>> math.isfinite(0.1). F" r8 c1 R; w& o
  9. True  f6 n! B2 l% F7 L
  10. >>> math.isfinite("a")
    1 x; h4 I7 O  r3 `6 L" j5 v' o+ P7 g
  11. >>> math.isfinite(0.0001)
    & j- {4 O" s: W$ ]8 m, [
  12. True
复制代码

* l( ?/ ~3 E5 Z' I1 m$ y: Amath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 v' R8 X0 Z: P  z* Y% `/ G
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ( S6 {. n" Y; V, K
  2. isinf(x) -> bool
    ( g  K3 t3 @1 N0 x8 T1 g5 Y& E0 `
  3. Return True if x is a positive or negative infinity, and False otherwise.
    6 A4 T& U- z# n7 ^, h
  4. >>> math.isinf(234)
    ' N" d8 H7 n9 \6 W2 c0 z  M
  5. False0 M3 z& _3 A/ r$ t
  6. >>> math.isinf(0.1)# _) r$ y+ z! h: B) K& ^
  7. False
复制代码

5 |. k4 s! ?$ c* o( K: j5 Emath.isnan(x)  如果x不是数字True,否则返回False
4 j: o5 X" I1 R$ j5 N$ n1 [
  1. #如果x不是数字True,否则返回False
    1 k/ G4 `, J) @( S5 b) S' \
  2. isnan(x) -> bool
    2 m' s- Z+ a* O" x' c$ o
  3. Return True if x is a NaN (not a number), and False otherwise.
    - h5 Z, z. Y' o6 J9 f" [/ Y
  4. >>> math.isnan(23)
    ( N. r0 H6 a  w- x2 ?
  5. False  l: @9 F" p; T
  6. >>> math.isnan(0.01)
    3 g9 ?  z8 G1 O, K5 K, j3 f
  7. False
复制代码
) \- [+ B8 e5 b5 j% v+ M$ |
math.ldexp(x,i)  返回x*(2**i)的值4 p4 r7 g' n' _
  1. #返回x*(2**i)的值4 v- N6 y! @  f! y& b$ ?! @/ x
  2. ldexp(x, i)4 L! q0 W- F' H0 L* M0 h
  3. Return x * (2**i).
    , x( ]/ f9 t& p- ]( X; r
  4. >>> math.ldexp(5,5)8 M3 K0 Q2 U* F% [
  5. 160.06 n* V% w6 Y& A. g4 x9 `6 E
  6. >>> math.ldexp(3,5)7 d+ ~" m4 z1 M( ?% B0 s3 P  O. W
  7. 96.0
复制代码
" V9 _# i/ G8 a! t1 Y
math.log10(x)  返回x的以10为底的对数4 U2 `! z& B. Q# H3 `* y
  1. #返回x的以10为底的对数3 v( U/ Q5 [6 I% i+ ^' u' t9 r
  2. log10(x)7 }. ^! r+ x# {8 y% U
  3. Return the base 10 logarithm of x.
    " G8 K. M/ ~- t  [
  4. >>> math.log10(10)2 W) H# [: [- ]2 A" s& H/ \* ]) f
  5. 1.00 W: M# x& F% ~& K* c* |! {5 ?; M
  6. >>> math.log10(100)
    3 Z9 w" `. e$ g& Y
  7. 2.0; i' y% l# f7 l- n7 t
  8. #即10的1.3次方的结果为20% ~& Y! |$ ?6 ~. a" _/ T
  9. >>> math.log10(20)- @. q! j; A9 \7 @
  10. 1.3010299956639813
复制代码
( a% y9 R1 j0 [( K! \$ ?
math.log1p(x)  返回x+1的自然对数(基数为e)的值) T; V+ l. t) Y, V
  1. #返回x+1的自然对数(基数为e)的值
    6 g8 {0 ?( S# L4 f! I2 l
  2. log1p(x)
    3 L5 @9 j  M" c1 l, g* [/ P6 ~
  3. Return the natural logarithm of 1+x (base e).' \. e  r, {% T( C& K5 f+ f( [
  4. The result is computed in a way which is accurate for x near zero.( W( O% t* W3 Z' N- a! Z
  5. >>> math.log(10)7 B+ ?. {! m  Y/ s: D
  6. 2.302585092994046  M  g/ J3 o3 Z$ @
  7. >>> math.log1p(10)% ^3 l4 L) H# B8 g2 W0 _6 v
  8. 2.39789527279837078 V# |- b3 p, W$ L6 K( ?( W- o! p
  9. >>> math.log(11)
    2 a0 |; y4 e2 }6 T, k' s
  10. 2.3978952727983707
复制代码

& R' W3 R* y( R4 L, tmath.log2(x)  返回x的基2对数5 [/ V$ g" B, X6 b# d8 M
  1. #返回x的基2对数
    * W2 c1 [1 \" d( P9 V0 t) ^
  2. log2(x); ^+ A7 b0 j& z) }
  3. Return the base 2 logarithm of x." v& \* |+ e) X  _8 x+ R
  4. >>> math.log2(32)
    ; t: e# x" ?+ e9 d( O% c( ~! N9 A
  5. 5.0% @( ?& G& i3 }6 a3 ?1 C
  6. >>> math.log2(20); i& H) \; g' W# @5 S: g/ `8 l
  7. 4.321928094887363
    * U! c+ _; Z5 g" c7 `
  8. >>> math.log2(16)$ C# s8 j7 a' s: X# p
  9. 4.0
复制代码
/ N0 U8 H. x- X; K( R6 V
math.modf(x)  返回由x的小数部分和整数部分组成的元组
8 u4 c6 N  G4 K
  1. #返回由x的小数部分和整数部分组成的元组
    : C7 a0 x" @5 r
  2. modf(x)& g1 j  s, l$ Y4 B/ g
  3. Return the fractional and integer parts of x.  Both results carry the sign) g' s  P* G$ d) y5 J
  4. of x and are floats." V* V$ Y/ H1 F7 `1 H3 k2 f; J% x2 k
  5. >>> math.modf(math.pi)
    0 L, M6 I8 p6 D, }
  6. (0.14159265358979312, 3.0)  ?5 l) N7 _& z  h  e- y
  7. >>> math.modf(12.34)1 O( q' \/ v; g5 D4 D# v0 B# j6 b
  8. (0.33999999999999986, 12.0)
复制代码

. k! }* M+ Q7 n1 P# _" q1 m9 Gmath.sqrt(x)  求x的平方根* F, P4 e& p% D' D2 o; ?' i7 f
  1. #求x的平方根+ V% d$ t8 N! v8 f5 d* N! p7 V2 x
  2. sqrt(x)
    % \6 e* z% B7 a: _6 m1 f7 M8 j
  3. Return the square root of x.8 |) h! W7 a$ v# T: ^
  4. >>> math.sqrt(100)
    # G. r0 t; v, q4 k# H
  5. 10.0' y3 Q7 _: Z2 j; }( @' w+ W3 v/ U
  6. >>> math.sqrt(16)) M; z1 R$ w: \# @7 _! Q
  7. 4.0
    ! J6 C  X3 v4 X1 W1 Y3 j
  8. >>> math.sqrt(20)9 [% a9 p3 q& d2 J8 R
  9. 4.47213595499958
复制代码

, n1 y' [* X$ G# V& h9 x/ rmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ' w6 h7 Y  L2 u* Q* Y
  2. trunc(x:Real) -> Integral
    7 y) c# C' y5 j5 m! {: T  w
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.) l- E2 F7 _) N& V
  4. >>> math.trunc(6.789)# M/ `7 [( K& Z
  5. 6% q0 v* f, Z3 `7 {
  6. >>> math.trunc(math.pi), A8 `# @. |% p: a( H! [9 _5 A
  7. 31 u( Y- L: Y& G  M: r/ n
  8. >>> math.trunc(2.567)
    ( X/ S! a" Y2 J& ?
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-6 12:59 , Processed in 0.088459 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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