新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
5 U1 m; F' f7 }$ S7 x' a3 G
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- y* Y! s! _/ {/ S6 w2 l' h
; S) @' e8 i/ T# G' Q# u- ]4 _; i; k
方法1$ q% n$ S- b2 P7 R* x
  1. >>> import math, `( p* i7 y# {% j6 J7 Z7 u/ t8 I
  2. >>> math.sqrt(9)
    2 A, z1 P3 w8 e  w( o) i& c
  3. 3.0
复制代码
方法2
& B" I) X2 X7 f) X
  1. >>> from math import sqrt
    % G$ L! Z" b0 E# L
  2. >>> sqrt(9)
    ' ~  o) }0 D" B( a
  3. 3.0
复制代码
( o2 ]: H' m! h) d  E" b( w

* Z/ \% q2 W$ j
math.e  表示一个常量0 }+ f3 T" b& T! X
  1. #表示一个常量+ E6 P( n- v) J" @. p. X' g
  2. >>> math.e
    2 d$ m( m' k9 w+ A' n! ?
  3. 2.718281828459045
复制代码

9 U1 G2 P" E( X: _0 M. Umath.pi  
数字常量,圆周率
1 R0 G% I. h. U6 `4 L
  1. #数字常量,圆周率
    % v# `% a2 Q5 @7 s7 k7 D) ]. i
  2. >>> print(math.pi)
    7 ~# Q9 k" z! J1 o6 K
  3. 3.141592653589793
复制代码

6 V6 T5 H+ L& qmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

9 O8 t# O7 H1 x. F9 T+ V2 Q
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, ]7 r# m' p, e, Q' k7 }! {/ B
  2. ceil(x)
    & P/ w% C1 I' A
  3. Return the ceiling of x as an int.  v9 y- }( e1 q4 w
  4. This is the smallest integral value >= x.
    / j, p6 E) ]7 d0 \$ B

  5. , ]* E) Q" F' C, m: @6 Q
  6. >>> math.ceil(4.01)
    8 z' h) W; Y$ q
  7. 5
    ( S3 e) K  P4 q0 t1 E0 K2 o2 j
  8. >>> math.ceil(4.99)
    ) Y% r' K0 {1 z, C
  9. 5/ B- x+ z. L! l# J
  10. >>> math.ceil(-3.99)
    0 ?  C% q) g% ^# W
  11. -3
    3 z8 N) Y$ t& j' F, q* t1 S
  12. >>> math.ceil(-3.01)- c) n; o% o9 R" X3 S) K
  13. -3
复制代码
( g6 X7 L; E# _& ?5 X$ ~
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ M( z! \! [. ?+ ^
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 f3 P$ c9 i/ Q# P. ~: N
  2. floor(x)' c, X- C: O( u, f. G- ?/ z) K
  3. Return the floor of x as an int.  |/ A% Q' ~" P; e9 R- F' V* G
  4. This is the largest integral value <= x.* y0 [+ B, n  [4 H# i2 X  C5 s
  5. >>> math.floor(4.1)
    " S5 y& @1 I9 w4 n5 W- j
  6. 4
    8 N" }( ]9 V( _+ V$ Z* X, Q
  7. >>> math.floor(4.999)  P" G" `' a5 _% M- c# w% `( b( l
  8. 49 X- o( a  b5 e3 |( j. Z
  9. >>> math.floor(-4.999)
    8 p4 Y6 k- g) s% ]. ~
  10. -5
    ; V, ~/ r3 r8 ?" |
  11. >>> math.floor(-4.01)$ ^; o! J, x: @+ c$ u! Z: u
  12. -5
复制代码

% ~& h- Q2 v; R5 K. Umath.pow(x,y)  返回x的y次方,即x**y) `- S( [- f) j: r
  1. #返回x的y次方,即x**y& }' F! k; A6 ?
  2. pow(x, y)1 t: K8 ]5 Y8 ~1 {- }
  3. Return x**y (x to the power of y).
      r  Z3 l' v: [7 u5 G' Y! k
  4. >>> math.pow(3,4)& a/ A+ u% |7 b' x  T, x" k" Z
  5. 81.0
    : j7 f, t+ ]$ h: n! o
  6. >>> ( }6 `% j. S; ]  z# K  p2 d- e7 K
  7. >>> math.pow(2,7)
    3 p  q4 M! f* @" |7 f
  8. 128.0
复制代码

- O5 W" f0 D5 p4 I4 Rmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* T/ l) n9 t  [
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ; s1 y3 d, i& ]! Q2 K5 i5 x- x8 _
  2. log(x[, base])/ ~3 G. A% ]4 r8 l
  3. Return the logarithm of x to the given base.
    ; T( b+ q6 a  B* C
  4. If the base not specified, returns the natural logarithm (base e) of x.+ u2 z; h1 h1 z4 `) {
  5. >>> math.log(10)- `) @) g' [$ y8 Y
  6. 2.302585092994046
    5 r( \: Z5 L( I1 M
  7. >>> math.log(11)! Q9 \7 `3 W) S, r! L, m
  8. 2.3978952727983707
    : x, q  p# Z( J- s8 S! a
  9. >>> math.log(20)
    ! @1 e% c& U! o* H' a. j$ n
  10. 2.995732273553991
复制代码
) d/ o  g" P9 P$ v. i( N
math.sin(x)  求x(x为弧度)的正弦值. H; ^' N, Z: i) \9 [' o
  1. #求x(x为弧度)的正弦值
    7 d6 l0 Q6 Z( C) i  L
  2. sin(x). ?4 n: W! z7 Z, r, x2 Y
  3. Return the sine of x (measured in radians).
    6 e  G* A0 u4 L/ z) g% I" S
  4. >>> math.sin(math.pi/4)% x3 f+ }) Y& S$ Y4 ]
  5. 0.7071067811865475
    " Q5 V/ x* _9 ~
  6. >>> math.sin(math.pi/2)( @3 [) J& P: e" t
  7. 1.02 C0 L) z1 e# m; R2 L0 ~: x
  8. >>> math.sin(math.pi/3)* d8 V6 Q& h! P) _, H
  9. 0.8660254037844386
复制代码

: T* G7 R# K( ^, Hmath.cos(x)  求x的余弦,x必须是弧度
/ U  o+ I/ A7 c  [, r2 J
  1. #求x的余弦,x必须是弧度
    2 e- y1 h5 V( P; g
  2. cos(x)
    " U3 ?' F+ z9 b% n
  3. Return the cosine of x (measured in radians).
    ) \6 P$ w% A1 e8 {2 ~0 {
  4. #math.pi/4表示弧度,转换成角度为45度
    / {' i$ G5 ~" t& w5 Z- _' u
  5. >>> math.cos(math.pi/4)
      Z6 V) \& L# H
  6. 0.7071067811865476
    $ k; \$ t" o7 R3 ~7 O
  7. math.pi/3表示弧度,转换成角度为60度
    / w& }( e2 a' D+ V* s. q' }
  8. >>> math.cos(math.pi/3)0 }0 b5 U7 c* t& o/ s5 E: y
  9. 0.5000000000000001. _/ b8 U, H( X" d$ |
  10. math.pi/6表示弧度,转换成角度为30度) a. G, D0 r% c1 F# X( a; r5 I
  11. >>> math.cos(math.pi/6)
    , u  M: V$ B1 F0 Q  C9 E
  12. 0.8660254037844387
复制代码

) n" Q' \' |& I7 y. q. q9 _4 x0 t$ Nmath.tan(x)  返回x(x为弧度)的正切值, e1 r2 q2 f- m( K# y& P' }
  1. #返回x(x为弧度)的正切值
    ' D& o& g2 R' G. y. g( ]6 S# C8 y
  2. tan(x)$ N5 C+ d, G1 _0 {/ O( Z8 v3 P7 k& w% Z, o
  3. Return the tangent of x (measured in radians).
    2 G! P( V* y0 J# U9 o3 r5 z
  4. >>> math.tan(math.pi/4)
    % n4 X+ \* n. U& J' }
  5. 0.9999999999999999
    % x& q) O* m" z  I1 ?
  6. >>> math.tan(math.pi/6)& n2 V  G- b+ Q" p( k
  7. 0.5773502691896257
    ' }* \: l  v+ \
  8. >>> math.tan(math.pi/3)
    % q" d1 n5 m+ u: a
  9. 1.7320508075688767
复制代码

( h7 h: b+ H  ^0 Gmath.degrees(x)  把x从弧度转换成角度  h  O4 ]: C8 u- m9 `
  1. #把x从弧度转换成角度
    6 M2 y8 G3 P/ C
  2. degrees(x)+ Y  J% `  \/ X7 I8 q7 I
  3. Convert angle x from radians to degrees.
    5 z1 w/ G1 M/ Y) Y

  4. / b1 l  @) [: {; G& O
  5. >>> math.degrees(math.pi/4)
    % j$ G7 m7 ^# N
  6. 45.0, \; J, y2 Z/ e" p; ]: p  o
  7. >>> math.degrees(math.pi)
    ) l) Q: a) v9 ?; h4 w, H
  8. 180.0
    ( n+ [/ I2 p9 N) C8 p: Z
  9. >>> math.degrees(math.pi/6)$ A; I) C$ _- M/ v' A$ I
  10. 29.999999999999996) _2 H* F, w+ T2 O
  11. >>> math.degrees(math.pi/3)
    5 N# u: e% f: P! T+ ~$ `. G4 @0 X! E
  12. 59.99999999999999
复制代码
7 M) t' }+ e  m3 ]
math.radians(x)  把角度x转换成弧度! z4 O. V: L1 _/ ?3 Z- ?( Y1 i
  1. #把角度x转换成弧度' a9 C8 C' Q: f1 W2 H1 Y
  2. radians(x)9 N- q. N0 [& B# o- b1 r
  3. Convert angle x from degrees to radians.( E2 E1 @7 F2 t8 v6 J. ]4 U1 u
  4. >>> math.radians(45)
    + V/ M) W5 o! v+ I8 [+ F/ R# }
  5. 0.7853981633974483- ?8 Q. G2 Z9 W4 V: s: [5 k2 |
  6. >>> math.radians(60)9 X, _! _$ H. J( c% r
  7. 1.0471975511965976
复制代码

9 G7 Z7 ?0 s( Y+ R9 b% gmath.copysign(x,y)  把y的正负号加到x前面,可以使用00 w2 l9 ?$ ^' @! k
  1. #把y的正负号加到x前面,可以使用0
    2 ^) e/ U4 U+ X' n
  2. copysign(x, y)0 T- Y5 |  f$ o) N8 S" d( P* U
  3. Return a float with the magnitude (absolute value) of x but the sign , u4 T. \: @. U  X$ V
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 6 W, [+ t1 Y8 v& I7 Y
  5. returns -1.0.' G; b) C% k8 x
  6. " g  E4 _; b0 y7 m4 T
  7. >>> math.copysign(2,3)
    : X# {! U+ s2 k1 x
  8. 2.0
    % t: z& @( C) b" G3 A$ e# o5 Z$ j
  9. >>> math.copysign(2,-3)
    " u7 e/ o# c# u, g  y/ u
  10. -2.0
    % z3 r5 G7 [6 g+ v( z
  11. >>> math.copysign(3,8)/ c7 T0 i% f/ F* f; Y
  12. 3.01 E# P% G9 Y  D' {* k0 w" ]
  13. >>> math.copysign(3,-8)
    ! p. a+ T1 T% n. K- y* d
  14. -3.0
复制代码

5 {. J4 i0 m: V3 S1 c) H% Pmath.exp(x)  返回math.e,也就是2.71828的x次方" u( \( j7 h1 o2 k+ K+ {5 H, t0 ~
  1. #返回math.e,也就是2.71828的x次方
    # i; x2 B$ o4 k) m2 @
  2. exp(x)
      i; S$ R0 _" c* G0 h7 D
  3. Return e raised to the power of x.' E; _7 q0 U+ k1 a+ K- S; Y

  4. 2 y% i9 y0 s6 m% b( M
  5. >>> math.exp(1); t. ^8 f6 u" v; y) a
  6. 2.7182818284590452 \/ G# }' G! H7 c" o7 s; q
  7. >>> math.exp(2)9 C( a' ?0 v, k5 c
  8. 7.389056098930657 c* S+ n. Y6 P' [5 G- N  k
  9. >>> math.exp(3)* ^2 I8 V% z) V, n3 ^5 r
  10. 20.085536923187668
复制代码
. X" P, O2 [( U5 \. a4 j
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
% L+ I/ I, n# c8 U. e, y
  1. #返回math.e的x(其值为2.71828)次方的值减1
    " r# E) _! S1 @) X  h
  2. expm1(x)) Y3 L, ]/ A5 v2 w) H3 G% ^  b
  3. Return exp(x)-1.* o" f! {$ ^3 Y+ y3 T- Q* E
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ S+ q7 ?$ D2 o0 _4 O

  5. - S8 r0 C/ t, f3 Y9 f' S: u
  6. >>> math.expm1(1)* F3 Q  X+ t' p7 i5 g
  7. 1.718281828459045) e- _3 e  |5 M# r2 O- ~  X: |. A; F
  8. >>> math.expm1(2)4 {6 K6 m/ X: X% L
  9. 6.38905609893065
    ( b4 S% Q6 U& B2 t
  10. >>> math.expm1(3)3 U  |' q+ |  h0 A6 S8 c( A
  11. 19.085536923187668
复制代码
  t* V7 n& ]% Q8 j+ w; S
math.fabs(x)  返回x的绝对值/ q1 T9 b6 e# `2 k
  1. #返回x的绝对值
    ! D: R. ]+ Y$ c9 d9 t
  2. fabs(x)" k. L1 u5 m  W% n+ T! w8 _& ]3 S
  3. Return the absolute value of the float x.7 o* h7 T' O- ^3 N" o4 u0 q
  4. % P6 D+ s6 c2 W) w4 _: D" L
  5. >>> math.fabs(-0.003)3 t) \  A* O1 [* f0 ~: T
  6. 0.003
    $ U+ V' `# [* o' ~$ n4 K; d
  7. >>> math.fabs(-110)2 S) Y) b& F8 B( B
  8. 110.0  M) o% K( x8 g: P5 C$ x
  9. >>> math.fabs(100)
    ! K; ^4 W- O! `% }
  10. 100.0
复制代码
! c, {# [: M! j" Z! t% A/ T0 e, r/ `
math.factorial(x)  取x的阶乘的值/ f' P, R9 Z% X/ ?6 f) L
  1. #取x的阶乘的值
    * @- G8 ~$ Y$ _) H$ f" ?' f
  2. factorial(x) -> Integral* L& c3 ^- q4 e& C/ N' `# ^
  3. Find x!. Raise a ValueError if x is negative or non-integral.+ @; Q* r2 W$ Q: w- |
  4. >>> math.factorial(1)% [- ]0 e; r) ^( o, I  k0 H- K
  5. 1. K/ n! B& @' @6 R* V
  6. >>> math.factorial(2)
    8 I; ]; S  ~- U( j8 d( r
  7. 2
    ! F2 p  J/ v; i- k( i
  8. >>> math.factorial(3)( M& k9 a5 Z% [  \
  9. 68 p1 }$ X( L6 @4 z# w
  10. >>> math.factorial(5)
    3 x8 T$ _. ~# p# k( B5 x3 z
  11. 120
    . a/ ^/ Y5 l1 @8 [8 j. c
  12. >>> math.factorial(10)7 Y! n+ G3 Y/ @! P/ L% k
  13. 3628800
复制代码
/ o# r/ C5 l" f* D. _! B
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
, K3 u* s! E: o& ~$ c4 o$ @# G
  1. #得到x/y的余数,其值是一个浮点数5 K) z5 L" f$ s# ?( h( O
  2. fmod(x, y)
    7 n- Z4 q9 n6 g+ K, T
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ; Y" t  G8 @: }
  4. >>> math.fmod(20,3)
    ; g# w7 E8 f. E
  5. 2.0
    8 {* {0 p# v+ W) j( \; o
  6. >>> math.fmod(20,7)
    $ y& ~& D( ~3 b  N9 c
  7. 6.0
复制代码
& j3 z/ a& _6 O% s3 Z  ]% @4 V& @
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 `: ?  |% p1 Y3 V5 p# y
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . f) U; d5 ~7 @# p# G* H
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' y# i' M' u* S& H
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 M( [0 T' o+ V3 n! }8 j
  4. frexp(x)
    % W/ f4 o' y- p4 H, t
  5. Return the mantissa and exponent of x, as pair (m, e).
    ( y. J1 p& A1 _. P9 {2 T
  6. m is a float and e is an int, such that x = m * 2.**e.
    % U2 O8 ]% ^4 O
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    4 S  [0 @4 Q% B  h$ }( u
  8. >>> math.frexp(10)
    * _) g( S# a5 I
  9. (0.625, 4)3 c6 S4 j* }6 Y- S! |( q+ p6 X/ Z
  10. >>> math.frexp(75)# v) b6 s; F, n. P$ o
  11. (0.5859375, 7)
    ( P8 I' B' c! J) u8 t
  12. >>> math.frexp(-40)
    / S/ b* R# o5 U3 z
  13. (-0.625, 6)
    - d2 k0 Z0 o6 R7 T  {4 K& `6 S
  14. >>> math.frexp(-100)
    & U# R8 ?$ J: I3 L: D0 p
  15. (-0.78125, 7)
    . Z; L# J3 R3 H2 S
  16. >>> math.frexp(100)6 |4 \' ?% E, [4 Y
  17. (0.78125, 7)
复制代码

& q( q5 c5 e- x- F: Z5 p$ nmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
6 Q) b; h, B. j* `# C$ O! U# [. w* ]
  1. #对迭代器里的每个元素进行求和操作
    / k! A) a7 b0 [+ ?
  2. fsum(iterable)
    ! D9 ~7 U( J- }. ]4 ?' Q* y7 j
  3. Return an accurate floating point sum of values in the iterable.* {& Q" E/ b% p* ]$ }
  4. Assumes IEEE-754 floating point arithmetic.5 H5 F7 Z$ B, G* f  r
  5. >>> math.fsum([1,2,3,4]). g: L+ Q/ p/ J: ]- K
  6. 10.0% T8 `7 X- f5 g' B. k
  7. >>> math.fsum((1,2,3,4))9 B+ p: C& q/ b" h! b
  8. 10.07 U+ R# L$ Z% z4 M7 J: e+ h
  9. >>> math.fsum((-1,-2,-3,-4))
    7 ?' q# @- s( a' U( W8 a. ~* G0 g
  10. -10.0
    $ [+ {" {4 N7 t. d
  11. >>> math.fsum([-1,-2,-3,-4])# D: Q% N5 T) m$ ], k! Y
  12. -10.0
复制代码

4 P' W) b$ z5 \  W  gmath.gcd(x,y)  返回x和y的最大公约数6 }" ^9 n7 e! k5 t
  1. #返回x和y的最大公约数" @8 j- V$ K6 b1 ~/ t: Z& @% m
  2. gcd(x, y) -> int0 ^9 [& T5 i$ m% @: k& t, E/ @
  3. greatest common divisor of x and y& |6 k. l$ ], g/ s5 [/ Z$ P, Y( v
  4. >>> math.gcd(8,6)% ]' q: g# c0 ^; C/ f
  5. 25 ~  |2 d- x" P( i: v0 p
  6. >>> math.gcd(40,20), u# ~* e# m. C6 @1 B4 e
  7. 20
    ) z* b5 e% K1 l8 u, k  X5 ~2 T
  8. >>> math.gcd(8,12)6 i+ p& s& a1 n4 @, f4 @
  9. 4
复制代码
( W& R1 S/ z9 I5 W
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 a+ D  ~6 \  |* z( x
  1. #得到(x**2+y**2),平方的值
    / y  B9 w' ]/ U' @/ T, Z
  2. hypot(x, y)1 m/ q$ a0 Q! z. `# o# |
  3. Return the Euclidean distance, sqrt(x*x + y*y).1 A: _) g# \7 c( k
  4. >>> math.hypot(3,4)- v0 ~8 g  P1 W2 E# ~3 g( p
  5. 5.0
    ! u2 S0 s* w. p9 _- _- X3 M
  6. >>> math.hypot(6,8)/ b' s) i. I8 b# E& X3 I, p6 _
  7. 10.0
复制代码

7 C8 R7 K: u9 v8 M/ a  }% L1 umath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False) _9 u9 f& J' k( N' H2 ]  ]
  1. #如果x是不是无穷大的数字,则返回True,否则返回False+ d8 b& @, M; M. C2 @1 D- B
  2. isfinite(x) -> bool
    9 M; {; n" ?6 ^
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    * {5 r& T/ u0 s6 e1 X0 f
  4. >>> math.isfinite(100)" f5 A- `' d) D+ k
  5. True
    6 M3 ?2 a! N+ H+ W1 x  n# R
  6. >>> math.isfinite(0)
    ; R5 v  E4 N. B! F3 |5 A
  7. True( G5 n5 r* L& _/ U: F; _
  8. >>> math.isfinite(0.1)+ v' r: p+ j3 y0 E0 k8 ]+ G% s
  9. True0 Q$ a# K' r0 w  X) s# v  g/ e
  10. >>> math.isfinite("a")1 i7 V+ S* F, V) \; @4 s9 B
  11. >>> math.isfinite(0.0001)
    8 X$ J5 b- {5 B0 e/ A/ V7 z. n1 M6 F) }
  12. True
复制代码
) z: d" Q) O- U1 ]. P6 E& m1 ?0 J
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False  L, Y% F  K3 W, U# e: y4 p
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ) s. U1 |" B9 }' v. B& _
  2. isinf(x) -> bool' H) l4 @2 _( f" D0 z
  3. Return True if x is a positive or negative infinity, and False otherwise.8 }; \- b9 R0 m8 B1 m( [* x
  4. >>> math.isinf(234)
    $ F9 p  O; y5 Q% k" T
  5. False3 R# ?4 b0 J, |4 W7 s  Z
  6. >>> math.isinf(0.1)0 ^6 P' o: F, h3 m  `8 B2 b% x
  7. False
复制代码

. x7 A8 U9 K& Ymath.isnan(x)  如果x不是数字True,否则返回False2 A$ z" L) j# z
  1. #如果x不是数字True,否则返回False
    5 a- I3 w% l, M
  2. isnan(x) -> bool6 z- H$ U7 [! P8 `" ?
  3. Return True if x is a NaN (not a number), and False otherwise.& ?1 Q1 m1 m: z* m5 w3 V* n1 \
  4. >>> math.isnan(23)1 t5 H6 e. k" A: h7 V3 E. ^! i
  5. False
    + p( C' r8 f# {& U6 e  k
  6. >>> math.isnan(0.01)
    3 d) r9 ^; E% {3 `/ x
  7. False
复制代码

9 c3 U+ Y2 c( A5 emath.ldexp(x,i)  返回x*(2**i)的值
/ n! E) |8 r9 h  l4 Q' b
  1. #返回x*(2**i)的值0 O7 o% j% ~2 D: {
  2. ldexp(x, i)' I- @! Q3 {3 w* W
  3. Return x * (2**i).2 e6 D9 ]; O$ }- G% ^$ x
  4. >>> math.ldexp(5,5)
    - [0 k7 X: ^- p
  5. 160.03 ]6 I3 W; G' J5 c
  6. >>> math.ldexp(3,5)& N# l# r, @/ `/ m2 i& W
  7. 96.0
复制代码

, O5 q( C) N; Pmath.log10(x)  返回x的以10为底的对数, Q$ X3 Z/ G1 a' c0 J
  1. #返回x的以10为底的对数' M0 p$ {4 O. e7 e3 }5 f
  2. log10(x)  l" d* v: d' |4 Q
  3. Return the base 10 logarithm of x.
    " C# Z/ J; |5 V; w5 i
  4. >>> math.log10(10)- b4 X: Q9 i; X5 @* i5 g1 Q3 j
  5. 1.0
    3 M% t' N0 H! E/ O8 N
  6. >>> math.log10(100)
    - g$ I# |; C4 s" ~" F% F: q: E# o
  7. 2.03 V; b! {5 n7 u% ~* }
  8. #即10的1.3次方的结果为20
    / ^+ ?4 x$ e* ^0 ^
  9. >>> math.log10(20)
    0 T: z+ s9 D7 b, w. \
  10. 1.3010299956639813
复制代码
2 _, N6 x1 p' f) y9 H: m7 L
math.log1p(x)  返回x+1的自然对数(基数为e)的值
$ a1 ~/ s+ }! q7 @9 p2 u
  1. #返回x+1的自然对数(基数为e)的值' g1 Y7 b9 ]8 k, X/ l/ z
  2. log1p(x)
    ' {( T3 O# O% B; p" R! K5 M9 t
  3. Return the natural logarithm of 1+x (base e).' F) n: \9 g9 a+ t3 p# r; S* @
  4. The result is computed in a way which is accurate for x near zero.
    ' G9 o, q9 Y% ~& N5 f: r+ R
  5. >>> math.log(10)
    3 m9 {. I: w1 N  w9 G6 W
  6. 2.302585092994046- \& c4 q# b3 v
  7. >>> math.log1p(10); ?( {0 l# c/ }5 k( {* g; t
  8. 2.3978952727983707$ j& r  x( Q( X7 |9 h! v; @
  9. >>> math.log(11)
    % k: t3 Z* p) F1 U2 z+ W, i' h* H
  10. 2.3978952727983707
复制代码
. i8 k& |: }$ j
math.log2(x)  返回x的基2对数
+ B3 G1 @0 G( F( {9 u3 B
  1. #返回x的基2对数
    / ?" ]4 v* s, V+ R0 g- w( j, i
  2. log2(x)8 r1 C( [2 _& q0 M% f
  3. Return the base 2 logarithm of x.4 L: v7 y. _# T! y& |
  4. >>> math.log2(32)
    . b9 b! R5 ?  F4 C( G
  5. 5.0/ z& I& u. B, T; \9 v# f
  6. >>> math.log2(20)
    5 g9 l+ M" ^9 A1 w& s( J
  7. 4.321928094887363
    % g! W9 M9 F* v$ c
  8. >>> math.log2(16)9 N3 _8 i" \) X, V: U
  9. 4.0
复制代码
5 k3 T- r3 D% e6 d; Q4 {; [
math.modf(x)  返回由x的小数部分和整数部分组成的元组) Y) P$ ^9 \5 I% N1 ~& N
  1. #返回由x的小数部分和整数部分组成的元组
    + V* J+ r9 H) I% s6 v
  2. modf(x)
    7 q, x$ {; N' e. E. x, `
  3. Return the fractional and integer parts of x.  Both results carry the sign1 X/ J# T* w' y
  4. of x and are floats.
    5 D8 z; J" q  s, E6 n
  5. >>> math.modf(math.pi)5 q- [. H3 X7 |, W3 W- A
  6. (0.14159265358979312, 3.0)
    6 |- \1 }. D6 J  a6 `5 r
  7. >>> math.modf(12.34)
    - u* S$ ^% ~9 |
  8. (0.33999999999999986, 12.0)
复制代码

! X" j- T! n6 F! ~math.sqrt(x)  求x的平方根+ I$ j/ M% V" i9 y6 t/ F
  1. #求x的平方根0 G* g2 r4 P2 @( a* l& i9 h
  2. sqrt(x)$ h" D% Y4 r, a% r! D& o9 o- f
  3. Return the square root of x.1 y$ s% |! _' f2 U
  4. >>> math.sqrt(100)
    ( }. V; u9 \9 N6 ]# f
  5. 10.0
    6 c% ?  v. Z% B" X. d( _  Y, h
  6. >>> math.sqrt(16)
    / i5 l3 `8 u( {  g" @
  7. 4.03 f% ~2 _5 d8 T7 Y
  8. >>> math.sqrt(20)0 q, q7 Y6 R) P. s( d5 Z
  9. 4.47213595499958
复制代码

! s- A7 }7 ^6 m" xmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    6 G' b5 ^, H9 H
  2. trunc(x:Real) -> Integral. ^% c; y! ?5 V  K3 ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ V; W, `$ n& g0 l0 I. X/ |
  4. >>> math.trunc(6.789)7 i8 y; Z$ ~! B- H6 `5 ^
  5. 65 A' C# Z# v* F+ D+ |2 |
  6. >>> math.trunc(math.pi). @8 Y/ ?7 [* A
  7. 3
    * o6 W) @/ p- K/ o1 p
  8. >>> math.trunc(2.567)3 p* T9 |+ U6 z8 y! J& {
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-4 21:40 , Processed in 0.085610 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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