新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

* @9 E2 s$ T$ V0 @4 P【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。+ r& v6 m4 Y0 `5 A  e

5 ]/ g$ z; U6 p8 w, F8 M* m, A方法11 ?% U! ~; ^* p5 T/ h7 C
  1. >>> import math- T2 `+ m, Q( |7 H: |) z
  2. >>> math.sqrt(9)
    ' |# V- M  [, M: e7 ~
  3. 3.0
复制代码
方法2
7 S0 ^# [9 E! i3 M+ G  Y# U
  1. >>> from math import sqrt# l7 h* b6 ~0 o6 l
  2. >>> sqrt(9)
    9 v3 X  K" i0 J( F5 U
  3. 3.0
复制代码
) o0 c4 E; V- j! X* a0 s  u

# W6 R0 X1 u; i! c6 S; m. L2 d. E  T
math.e  表示一个常量4 Y' _0 G3 Z9 _) b9 D) F8 J) Z6 i
  1. #表示一个常量
    6 H( _1 J- G8 ]3 k1 N: x' V
  2. >>> math.e
    - d: P/ t, ~2 ]' ^& T" Z
  3. 2.718281828459045
复制代码
7 q# U. C& h$ N7 K. w
math.pi  
数字常量,圆周率
; \) d( ?, F7 f- f  a
  1. #数字常量,圆周率' I' [: p  i( H$ I0 f( Z* C- \0 k
  2. >>> print(math.pi)7 g5 o3 O5 a/ w; v, ]9 Q6 Q
  3. 3.141592653589793
复制代码
  ?, [3 f* O& O; V: V/ y
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
. b2 ?$ E! [+ l  B
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x0 j* n  r  y. {5 D4 N4 t
  2. ceil(x)
    * ?& k1 f2 \' p! ?+ M' \1 `! K
  3. Return the ceiling of x as an int.- S- ]0 u# ~: r9 c" ~2 f3 G* g
  4. This is the smallest integral value >= x.
    ; ?  [5 p. m' V1 v
  5. 5 m; I+ K, ]+ F- W
  6. >>> math.ceil(4.01)
    ' r$ P7 N! V1 s- A
  7. 5
    $ b' A8 M0 }; m. |( h
  8. >>> math.ceil(4.99)
    ( _- |6 H* O+ a
  9. 56 G/ d. ]/ ?: I& {$ Q; b
  10. >>> math.ceil(-3.99)
    " A5 K6 d  P/ a- N( h
  11. -39 x5 r# w5 K' v# {8 }$ g! U: Z/ a
  12. >>> math.ceil(-3.01)& e3 z3 I4 h( p) N# [' e5 F
  13. -3
复制代码

: X, n: V/ P. b: J: H" Pmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ H# ~" _( t- r- w! }& b
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      D& Z! T1 x6 |9 t+ M. Q% s4 P# b1 u
  2. floor(x)5 q4 g7 D# G/ {) d7 m
  3. Return the floor of x as an int., d3 `9 e5 ~5 P3 y3 y
  4. This is the largest integral value <= x.
    , t2 `2 m9 r' o; D8 N
  5. >>> math.floor(4.1)
    8 B( p5 G: `% O" ^$ G" A5 _3 X7 J
  6. 4# U. r5 h5 |9 X+ s9 H8 u3 n
  7. >>> math.floor(4.999)
    ( y* ?8 {; x4 h' M0 Z
  8. 4
    ) v9 |9 H* c: n+ f0 S4 C+ N0 C
  9. >>> math.floor(-4.999)
    ) G% ]) x, R; ^/ P4 `" i9 e
  10. -53 v7 n* u& m3 i& ]% }9 K( |
  11. >>> math.floor(-4.01)' a# t0 e7 y8 A4 L! ]5 B
  12. -5
复制代码
1 D5 B4 n1 x& U" O1 Y
math.pow(x,y)  返回x的y次方,即x**y
! q- |0 @  e9 `/ N$ e6 M
  1. #返回x的y次方,即x**y
    ) G5 f* U2 P- p! Y! M
  2. pow(x, y)
    3 I, P; d( j" X, V8 w; g+ R0 T
  3. Return x**y (x to the power of y).; ~+ B! c6 M* ~* r' W& n' p
  4. >>> math.pow(3,4)+ w5 q9 K. [: l/ G9 k0 J
  5. 81.0
    ( N$ C) f/ d, F
  6. >>> % h! I" N4 t2 ?$ Q3 j5 N6 t
  7. >>> math.pow(2,7)
    9 }' |# F. N, k% W6 e: c
  8. 128.0
复制代码

. X) f% M) y& q0 z5 s9 q$ Q, W) b5 n; Imath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 G; @# ]) P, p) b0 Z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    / c% p$ T. `$ r" p' K
  2. log(x[, base])5 B) N- _1 {& ]' f" \
  3. Return the logarithm of x to the given base.$ O+ s3 H- g6 e7 k& I% s8 a  Z
  4. If the base not specified, returns the natural logarithm (base e) of x.6 _+ i* P" U% @' t5 ]: a
  5. >>> math.log(10)
    0 L( C7 b7 ?& G5 ]! H8 R6 u5 ?
  6. 2.302585092994046% s/ s" d% g$ l
  7. >>> math.log(11)
    $ `% G9 a# V' N& ]! F
  8. 2.3978952727983707
    4 U2 N  o) E- K3 t* t$ A# J' a
  9. >>> math.log(20)  J0 h" o/ W/ u$ n3 Q
  10. 2.995732273553991
复制代码
8 B* y6 D7 J: |. y: D' |8 i' l
math.sin(x)  求x(x为弧度)的正弦值$ E9 q% h, `0 C$ M8 n' ^
  1. #求x(x为弧度)的正弦值
    $ y: x$ J% w6 D
  2. sin(x)
    " \  k: V9 @8 h
  3. Return the sine of x (measured in radians)./ Z1 M* e0 D# ?% N
  4. >>> math.sin(math.pi/4)( N3 z4 q2 w; K8 d
  5. 0.70710678118654759 h) k6 D1 [5 e! F/ ?8 A! M, y' m
  6. >>> math.sin(math.pi/2)
    0 N+ q' r# ^; c7 a2 A- p
  7. 1.0
    ; [( O+ A2 |7 {7 p% a2 n( V. R  @
  8. >>> math.sin(math.pi/3)
    1 m5 f+ C& d; x: W( V
  9. 0.8660254037844386
复制代码
; O# X9 i5 P& ]: \9 l4 i4 S* H
math.cos(x)  求x的余弦,x必须是弧度
& b2 I& }, M1 s; D: v8 N7 `  g
  1. #求x的余弦,x必须是弧度5 {& B9 I8 ^* ^; W$ [: y1 f
  2. cos(x)) g% m- S: O) `! ?6 Y; `0 b2 C
  3. Return the cosine of x (measured in radians).# E' d: ~& O6 v' N! T" h7 t9 P5 M
  4. #math.pi/4表示弧度,转换成角度为45度, v1 s2 n  H! S+ [
  5. >>> math.cos(math.pi/4)
    & C+ Q& i, {6 Q7 T
  6. 0.7071067811865476
    ! r; Y$ K/ v. d1 ^
  7. math.pi/3表示弧度,转换成角度为60度, g9 B: d$ \  @
  8. >>> math.cos(math.pi/3)
    4 ?) U7 U6 u( _1 i/ P! J# ~/ F) i
  9. 0.5000000000000001
    ! S9 }3 n" G$ T+ U' ?( F
  10. math.pi/6表示弧度,转换成角度为30度& M5 l: J0 N; ~# C
  11. >>> math.cos(math.pi/6)% O7 Q/ K: I7 [0 z0 B- f4 Q
  12. 0.8660254037844387
复制代码

9 e9 m! U  N$ I) {. @$ l; N! R$ G* mmath.tan(x)  返回x(x为弧度)的正切值
7 V# u; P0 u0 ^5 C, e3 k
  1. #返回x(x为弧度)的正切值
    . f* R. C. |. L3 m1 W
  2. tan(x)
    " U1 k: E* h- t  B* F- k
  3. Return the tangent of x (measured in radians).$ T# `  ?* I6 {
  4. >>> math.tan(math.pi/4)
    + e! Y) y1 B1 U' N* I9 l9 F2 o- q* V
  5. 0.99999999999999990 y* \( C% V8 F% Q: u
  6. >>> math.tan(math.pi/6)  s9 e: d6 q* c) U5 @% O# M( y
  7. 0.57735026918962574 D% I' N& K4 T/ E8 S
  8. >>> math.tan(math.pi/3)2 @7 |) n" m( W* y0 N  t
  9. 1.7320508075688767
复制代码
! n3 \$ [$ g' ~( w8 I
math.degrees(x)  把x从弧度转换成角度: `+ s. k* J1 v3 r, d
  1. #把x从弧度转换成角度
    % J8 c$ O, L  ?  ?9 C  `6 {! y& X
  2. degrees(x): X* i$ c5 X/ U8 D
  3. Convert angle x from radians to degrees.
    4 ~  r, T5 [4 D# P: ]) x( y' n* Q

  4. $ d% J- x# R- C# Z  j1 t
  5. >>> math.degrees(math.pi/4)
    % I$ v5 Z( S& _, m" L2 T4 B* `7 T
  6. 45.0
    4 A3 X' h  X4 w! w) C! G
  7. >>> math.degrees(math.pi)" d* X! B" H! @8 D! e
  8. 180.0+ y5 k  X& g8 |) K
  9. >>> math.degrees(math.pi/6)
    % H1 J( z/ c8 ]9 i/ h
  10. 29.999999999999996
    7 C& f4 K8 x3 M( o" k8 C
  11. >>> math.degrees(math.pi/3)
    , m  S- W) j2 ]4 p
  12. 59.99999999999999
复制代码
1 m' R% g& a5 X0 d0 ~  _$ y9 @" v
math.radians(x)  把角度x转换成弧度
& [) \; ^" Q+ X$ c# X
  1. #把角度x转换成弧度$ C5 S1 O) H( o. i) F
  2. radians(x)7 |& `- I1 y# D: E' b! y- |
  3. Convert angle x from degrees to radians.2 a6 K  E7 R6 ~" l, H% T; q
  4. >>> math.radians(45)8 J9 G4 S' f% g; k) F
  5. 0.7853981633974483
    - \& k  a; B/ G0 o, k. l. p- |4 k
  6. >>> math.radians(60)8 w4 W% U, r1 u1 P
  7. 1.0471975511965976
复制代码

+ r* u+ [6 K( wmath.copysign(x,y)  把y的正负号加到x前面,可以使用0  S6 F6 u; ]% ]* v  m# S$ q
  1. #把y的正负号加到x前面,可以使用0
    6 q% E3 `% L  l6 {' P, N
  2. copysign(x, y)
    # [3 B# @. `5 v& J" s
  3. Return a float with the magnitude (absolute value) of x but the sign
    ! E  @. W1 f# v8 ^3 v
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) " L- {' T4 @& j* C2 Y$ |) [$ [
  5. returns -1.0.  b* W; b; a; R3 q2 P, k

  6. ' n5 v5 o6 `( _; T( b
  7. >>> math.copysign(2,3)
    1 e# f) }' s0 A
  8. 2.0$ r  X( w( ?) V7 Z
  9. >>> math.copysign(2,-3)
    . N" A  x2 ~( Y( t
  10. -2.0
    1 m" i2 w; j; H. ?5 }7 ~6 c
  11. >>> math.copysign(3,8)0 O4 A9 j8 y/ n& K, B8 D/ V
  12. 3.0
    4 x+ {8 F7 C, f7 ^9 X
  13. >>> math.copysign(3,-8)4 W! i+ q) X; S. T
  14. -3.0
复制代码

) c( l- M% R2 w1 r  pmath.exp(x)  返回math.e,也就是2.71828的x次方
( E; ~8 q/ i8 \/ X  S. L
  1. #返回math.e,也就是2.71828的x次方
    $ j! i8 Z2 B$ N) S( X; ?& D
  2. exp(x)! _2 _1 O, k- `
  3. Return e raised to the power of x.
    4 [, i& y6 Z* P/ Z- X# M+ {# ]& \
  4. 5 \+ `  ~7 z: s$ ]# i  t
  5. >>> math.exp(1)& {6 y) ]# Q' `' W" R& e, A( U  B
  6. 2.718281828459045
    3 M4 r! s0 ^+ i& u
  7. >>> math.exp(2)* @1 m: c' x8 o* e/ w
  8. 7.38905609893065
    4 K# H% e1 s2 I/ L  T! n
  9. >>> math.exp(3)1 B% O/ {* u; Y! ^/ m5 j& o4 y/ W
  10. 20.085536923187668
复制代码

. h' V& c6 T+ z" d& _math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
! b- B7 C" s3 a( E. X$ a; D
  1. #返回math.e的x(其值为2.71828)次方的值减15 J- H! w1 T; |; _8 V" E- `
  2. expm1(x)
    # J3 T& _1 C8 i% M; J; q6 h5 G4 Q
  3. Return exp(x)-1.* h7 e! ^5 \# q5 s  b
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.1 l! ~' n5 R% z/ A" j; e$ N' h
  5. ) o$ q$ a0 |9 q# {  P
  6. >>> math.expm1(1)* T- Y( i$ `6 {& ^4 H
  7. 1.718281828459045
    . V3 r  F5 ^+ i0 D/ G3 [
  8. >>> math.expm1(2)1 l4 g& ]& a0 u% {8 r3 e& G, a4 i' H
  9. 6.389056098930655 w$ {6 Y1 w: v& |
  10. >>> math.expm1(3)+ g; ^5 m- j- G* B4 W, z8 X
  11. 19.085536923187668
复制代码

6 P0 _) _, C2 c5 Kmath.fabs(x)  返回x的绝对值/ n& Y- C& D# s/ a# M  c! u, T
  1. #返回x的绝对值  M. T+ N! v, P6 y
  2. fabs(x)
    0 ~0 E- Z/ D* e
  3. Return the absolute value of the float x.3 o9 H9 B1 x, ^( X9 M! Q4 A

  4. ) J2 D0 [8 C8 |9 B, K
  5. >>> math.fabs(-0.003); `, P/ M: k1 U) o. X
  6. 0.003
    2 R/ A% S0 r% e7 ~+ |! d
  7. >>> math.fabs(-110)
    $ x. r$ ?' e) x4 W5 ~, @( S( J
  8. 110.0. ?% K4 Q7 V0 O6 `) R4 L
  9. >>> math.fabs(100)
    $ e' T1 T" f  o5 k. j9 j* w( Y6 G
  10. 100.0
复制代码

2 Z5 q! K+ g& Z2 s( pmath.factorial(x)  取x的阶乘的值
: w' U0 |. Z) U2 q
  1. #取x的阶乘的值
      c9 L/ J' Y  w; l2 o9 B/ y( b- g9 m2 b
  2. factorial(x) -> Integral$ u) H* z9 f  L1 b3 C
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    5 K# y7 h& K% F% r( L' G
  4. >>> math.factorial(1)
    : T4 K1 _/ ]+ v# n( G
  5. 1$ [1 ]8 |! I7 P1 u
  6. >>> math.factorial(2)4 b! G( e: E$ v9 I# e
  7. 2
    " m! q- V$ \) K+ o
  8. >>> math.factorial(3)
    9 D/ ^: F9 X( Z
  9. 6( r# o& c5 I& E- A
  10. >>> math.factorial(5)) v; x7 y: ]0 U+ K* W! A: G. m% i* J% k
  11. 120
    & ?& b( X' h) F& @1 b* h+ Q: J  R
  12. >>> math.factorial(10)
    8 ]) S5 p6 t0 S- S
  13. 3628800
复制代码

  }' l% G, T; }math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
1 ^) r) ]. f6 N  X, d6 k
  1. #得到x/y的余数,其值是一个浮点数
    # }8 k- b; J( w; Y1 t2 p  m4 P
  2. fmod(x, y)
    7 |- E1 T. k' i
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    7 Q) v  \  y0 \, g: G
  4. >>> math.fmod(20,3)  A/ U# w$ H! ^9 @1 k$ [" m
  5. 2.0. @; n/ n2 N4 ?$ c' s2 s
  6. >>> math.fmod(20,7)
    7 d: ~1 d* a, u4 D+ @5 q6 b
  7. 6.0
复制代码
( W7 C5 z7 ?. Q
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
1 e' t( u7 ^0 {* I+ d+ K
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,, w0 M2 K5 i- |" ~* B0 ~
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    $ o2 T) k! |" G
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- ^5 K% e! [9 J. R7 o  u& \
  4. frexp(x)
    % A+ `) Z0 `2 F: G6 c' g
  5. Return the mantissa and exponent of x, as pair (m, e).2 F. s- m$ [% M$ ~/ b2 M
  6. m is a float and e is an int, such that x = m * 2.**e.6 x1 V% E5 q5 |+ v. `; q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    6 _- z" H3 s( v0 Q7 I5 h% b
  8. >>> math.frexp(10)
    4 A" L) F# K# w8 Y
  9. (0.625, 4)
    - E& L0 ?! k' k: C: f) Z5 C6 s
  10. >>> math.frexp(75)
    - [- [) J' r: }% Z
  11. (0.5859375, 7)9 }  T/ ^7 {: u. \' `" W
  12. >>> math.frexp(-40)
    . ]( M+ R* p, o2 |
  13. (-0.625, 6)1 w5 |1 X! _: q/ B
  14. >>> math.frexp(-100)
    1 ]7 ^2 U/ _9 X2 R1 Q
  15. (-0.78125, 7)
    , T8 ]& ]/ Z$ j7 a' w+ z4 J
  16. >>> math.frexp(100)+ N9 e( L: i( L3 |; \# P8 r
  17. (0.78125, 7)
复制代码
# _, Y( @$ }8 E" z8 Q1 Q; I
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
  d: L: c3 {( I7 @1 ^6 E: m3 z
  1. #对迭代器里的每个元素进行求和操作
    " E/ l+ f" x2 i) Y$ w& l
  2. fsum(iterable)& p' m% d2 t7 n9 {+ r- f) j
  3. Return an accurate floating point sum of values in the iterable.5 M: c, N8 l0 ^
  4. Assumes IEEE-754 floating point arithmetic.
    7 O( h/ P2 q% @4 r
  5. >>> math.fsum([1,2,3,4])
    ( S0 c0 F! ]+ y, e* j
  6. 10.0
    # S. s0 {- r. J) T
  7. >>> math.fsum((1,2,3,4))+ h2 d7 M) C  w/ s1 P, d! o
  8. 10.08 T* E/ a- n; s* Q
  9. >>> math.fsum((-1,-2,-3,-4))' a) V# {- m, }. {
  10. -10.0
    % S- f% h! W1 L' v" R- i
  11. >>> math.fsum([-1,-2,-3,-4])1 o# f, b0 A' W; G9 L
  12. -10.0
复制代码
% s# t, a9 T# m9 ~
math.gcd(x,y)  返回x和y的最大公约数, [- v* n  B; f9 w2 F. R
  1. #返回x和y的最大公约数) {  M# t4 f, v) E+ t. Q+ m$ Z
  2. gcd(x, y) -> int
    % `: D( |/ Z$ i  f% E: I+ r
  3. greatest common divisor of x and y
    : b9 [7 j, z. I5 j! T
  4. >>> math.gcd(8,6)
    % s8 _7 n" U  N/ }# s3 p
  5. 28 e3 l. J* O3 |& W
  6. >>> math.gcd(40,20)
    + R' e: K5 c" z, ?2 c; A
  7. 20
    2 I7 H+ b0 y) I. J6 G# b8 D/ I, K
  8. >>> math.gcd(8,12)) l  @2 e9 M+ b: P: w1 v' A
  9. 4
复制代码
% a) q9 `& ?# d5 E6 l
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
2 ]0 r4 W* _, j9 d. _  Y
  1. #得到(x**2+y**2),平方的值1 D7 c8 K- ^+ f% Q2 M9 r. T* m
  2. hypot(x, y)- F) i8 [3 x; Z5 Y( B* b3 T2 d
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    3 |; k  T8 G/ x+ w$ e9 n4 p  @. k1 M
  4. >>> math.hypot(3,4)5 t7 a/ L$ E# Z$ Q
  5. 5.0( y* s( D* v  k$ b8 r
  6. >>> math.hypot(6,8)
    9 P4 ^$ v% g  T4 o) W, f' s" ^
  7. 10.0
复制代码
# L: h! G; O! w: o- [8 \
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
! j# k& x6 n/ G6 D
  1. #如果x是不是无穷大的数字,则返回True,否则返回False) Z: H; U% i- G6 |6 k& O
  2. isfinite(x) -> bool* a" M7 @( z! V4 N7 _
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ) g. C7 w" v) d
  4. >>> math.isfinite(100)
    . l& B; r1 u- I2 G! d5 L8 Q, h
  5. True8 B9 L4 H2 u+ M: E8 ?
  6. >>> math.isfinite(0)
    " u, o1 \! L3 V; x
  7. True$ N' P8 ?% w# K0 a  L
  8. >>> math.isfinite(0.1)# t; C+ E0 P* }0 D4 s- p3 S2 Z9 x
  9. True
    $ Z1 {: b4 H# O+ T8 n7 J
  10. >>> math.isfinite("a")
    % ^/ {& I  E. F1 f
  11. >>> math.isfinite(0.0001)
    7 a4 R# z& {! b" g* D7 o6 X
  12. True
复制代码
0 W, y. B3 z8 i4 x
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
$ I( R9 ]$ B5 B. E9 S2 [- p" g2 f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ( R! ^! ?6 s, }/ ]2 u
  2. isinf(x) -> bool( a  V" u4 Q9 x6 n4 x! e
  3. Return True if x is a positive or negative infinity, and False otherwise.
    % i, Y8 X) l: Q' ^
  4. >>> math.isinf(234); v' B3 f+ G6 V% k" l
  5. False  e- F1 a$ y$ U$ v
  6. >>> math.isinf(0.1)
    " Q  U3 J" T& b: V) ?
  7. False
复制代码
9 G6 F  R! V6 ]) p; }' {
math.isnan(x)  如果x不是数字True,否则返回False
! N& q8 V, h' n- z! t& _
  1. #如果x不是数字True,否则返回False  u: E! m. p3 `4 T3 a/ A- F
  2. isnan(x) -> bool
    2 w. e+ E. @% C
  3. Return True if x is a NaN (not a number), and False otherwise.
    9 Y3 M" r, v0 H" y# O. e; }
  4. >>> math.isnan(23)* u* ]) ?3 M0 X3 n0 V6 w0 q2 i: [
  5. False
    $ g- m( {8 ?0 Z/ W1 d3 s/ a
  6. >>> math.isnan(0.01)3 q7 H+ m1 D' m
  7. False
复制代码

3 s4 [$ T7 f; J0 Tmath.ldexp(x,i)  返回x*(2**i)的值9 I: k" t9 w3 P) a. k8 ?8 |- W
  1. #返回x*(2**i)的值
    $ R: {8 C( W; V( j' m8 G% d: j
  2. ldexp(x, i)
    / I; m0 [7 t! p4 r
  3. Return x * (2**i).
    2 k- |/ l+ H; ~2 R" o+ h
  4. >>> math.ldexp(5,5)
    + R) X$ E8 x) ]! C2 s
  5. 160.03 G. W5 B; U" b  N0 |
  6. >>> math.ldexp(3,5)
      S: d. J6 s# [/ F: q
  7. 96.0
复制代码

' u. [) y6 l* b8 |: pmath.log10(x)  返回x的以10为底的对数
, v/ o; |  K- R5 f- \
  1. #返回x的以10为底的对数
    * [9 }( X) t, [
  2. log10(x)2 k: i1 c% r& j% \1 X
  3. Return the base 10 logarithm of x.
    " f& n5 z9 g3 q2 H! i7 ^
  4. >>> math.log10(10)/ w7 b( X  T$ b1 U
  5. 1.0
    ; f- d" O: M" r0 h2 \# s) `6 b
  6. >>> math.log10(100)
    4 \7 d) [* @6 w6 W5 [+ A: h6 O
  7. 2.0
    9 r2 }$ `+ w- }5 _' J; N' ?
  8. #即10的1.3次方的结果为20
    & d1 S+ o  q" s8 G0 }& M& P
  9. >>> math.log10(20)
    7 a# R; p" T/ _/ q
  10. 1.3010299956639813
复制代码

( I! x! u9 g7 p( f1 Y* r$ D# Lmath.log1p(x)  返回x+1的自然对数(基数为e)的值
+ |1 l0 x" u* G* X+ ?# J, ?
  1. #返回x+1的自然对数(基数为e)的值) F/ [; u/ I9 v8 N) r& C8 V
  2. log1p(x)
    ) z& o1 z4 L% c9 U5 ^1 j" G" q
  3. Return the natural logarithm of 1+x (base e).$ _9 \) T; u0 f' r
  4. The result is computed in a way which is accurate for x near zero." ^# j6 |. T) M- c
  5. >>> math.log(10)
    # ]2 U4 N# Q0 i# P! J3 R1 _
  6. 2.302585092994046
    6 g- [% T  B- C9 G& e8 W! V$ S
  7. >>> math.log1p(10)( N9 z) X) |+ N! r
  8. 2.3978952727983707) n+ q" i: A, w) R
  9. >>> math.log(11)- D  l/ E" i  w  @
  10. 2.3978952727983707
复制代码
- J0 r2 I6 Z8 ]$ `" g
math.log2(x)  返回x的基2对数% M9 v0 k# r# @1 W# ?! H! |
  1. #返回x的基2对数% m1 a: r( s/ X/ ~0 ^6 k) ^
  2. log2(x)7 @! J2 N: R3 N
  3. Return the base 2 logarithm of x.% [& Q+ \+ T8 B, q; g% a2 y, I+ m  H
  4. >>> math.log2(32)
    ' X7 L5 S; R) Z3 ?+ V
  5. 5.0
    5 F& e- F; h: J! F8 j
  6. >>> math.log2(20)4 p7 ?8 f- H% J. Q6 F. N
  7. 4.321928094887363
    : g1 _: P$ E; d) n; k0 [8 e7 m
  8. >>> math.log2(16)3 \# w6 O9 L' P7 f3 }
  9. 4.0
复制代码

9 n1 M8 q6 |4 X4 p4 e5 e( Zmath.modf(x)  返回由x的小数部分和整数部分组成的元组
& W( ?0 s( h: u3 c; u
  1. #返回由x的小数部分和整数部分组成的元组
    2 b. i9 t, ]" Y8 D; d9 N! m
  2. modf(x)
    7 h! l9 s9 l, I, Q0 s& L% _
  3. Return the fractional and integer parts of x.  Both results carry the sign+ A* B# ^8 `  A" i( P9 z  R
  4. of x and are floats.
    1 m3 t- ]) K5 T* r2 ]4 y& F
  5. >>> math.modf(math.pi)" O, _7 |1 I% F; @" g% O5 C2 F* ?0 L
  6. (0.14159265358979312, 3.0)3 Q* Y' |7 s$ R# u
  7. >>> math.modf(12.34)' }& u/ a# L, A, E0 i0 H
  8. (0.33999999999999986, 12.0)
复制代码

& [& r3 j# V+ m* M# y8 umath.sqrt(x)  求x的平方根
& @' b; n: w3 X& ~
  1. #求x的平方根
    ; N% O6 J8 M5 E$ e
  2. sqrt(x)
    5 @0 ]( t: q4 v6 P  ~- C% y
  3. Return the square root of x.
    : m/ {- I1 Q$ U. n5 ]2 e; ~$ q
  4. >>> math.sqrt(100); t% F( S% b+ k4 ~; ?
  5. 10.0" x! S: x; [4 p+ j5 R
  6. >>> math.sqrt(16)
    : a& J) T( E8 H( p* T, X( ~
  7. 4.09 \0 m; t' A, F6 Q
  8. >>> math.sqrt(20)1 N, h9 Q) M6 s
  9. 4.47213595499958
复制代码
  ~( q0 X9 b. u9 S6 [" |2 P, e
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ) z9 q3 ]. \* X" T6 {
  2. trunc(x:Real) -> Integral. v; t! Y* l( [3 s( d+ Z. _: S9 G9 y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ! h  x2 r$ @# [6 H2 {' p% b+ r/ @
  4. >>> math.trunc(6.789)* ]4 l- j; u0 d3 W7 j
  5. 6' f) Y, S# S* \% D
  6. >>> math.trunc(math.pi)% C  O6 T! m9 k9 N' j0 w% ]+ K! t
  7. 3
    ! O. K$ D4 `/ K  d3 q1 ^
  8. >>> math.trunc(2.567)# j& K. p  F) u( q. i& s
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-24 07:47 , Processed in 0.081276 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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