新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
' K5 w  L7 P7 j9 l6 @6 g: M$ V( Q
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。( \" }9 G  w4 u
+ ]3 r" n+ |7 k. z9 G/ K- d
方法12 A6 t) Y$ V4 b) ?. u  q
  1. >>> import math
    7 g' G+ h  B8 W  A3 m! |" O" V
  2. >>> math.sqrt(9)
    # c$ D0 u7 q  `& ]
  3. 3.0
复制代码
方法2
1 `$ W) G, g" H2 z" {2 X! [4 P
  1. >>> from math import sqrt* v% q, |: n; L' [1 A; ?5 k
  2. >>> sqrt(9)8 }$ I' ~8 B4 v" f  b0 R" x
  3. 3.0
复制代码

3 K" k; B5 X. H8 n  n

$ d: p, g) Y" k# i
math.e  表示一个常量
- E! R! U3 x, u3 H2 J
  1. #表示一个常量
    5 Q' T2 h) \5 {+ y( ^
  2. >>> math.e
    % K& L8 f6 K) j
  3. 2.718281828459045
复制代码

4 A# n3 h2 T' C. Emath.pi  
数字常量,圆周率

/ M# P2 y' T% U* K7 ]7 f" p! ^1 H
  1. #数字常量,圆周率% J; E  l3 s  |$ \: W
  2. >>> print(math.pi)0 g1 |; s, T2 F& R; V: z+ k+ U0 j
  3. 3.141592653589793
复制代码

* m) F5 E. ]) U2 O: J& Hmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 N, k* j& U! Q2 o. l; h) T& ^
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x. A  h1 k6 ^# z  F& q: B
  2. ceil(x)3 V; K& W2 d# C: J  E. G: v9 ]
  3. Return the ceiling of x as an int./ Y7 Y6 r' o' H, x; {0 u, Y& N; y
  4. This is the smallest integral value >= x.
    2 G; f5 L% E' m( J& z: |

  5. ; p2 y& s: }! I: Q
  6. >>> math.ceil(4.01)) F% _+ `2 U0 A( ?8 B- m" j
  7. 55 y0 _2 N$ S- E0 |1 z4 u+ r
  8. >>> math.ceil(4.99)+ J4 {% @$ s8 g0 {8 x- T+ R+ ?
  9. 5/ e5 r8 p9 Q# T7 A5 c
  10. >>> math.ceil(-3.99)4 }, o. v  f8 c4 k
  11. -3
    # b* F% g6 x7 g# J: j
  12. >>> math.ceil(-3.01): T4 D- G6 a4 [  S& k4 m8 P7 w- e
  13. -3
复制代码

3 [6 o7 v4 z' v$ \+ [" n+ `) y0 fmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身& z9 h& p- D) q* a
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    " b# {9 G4 ?( Y6 d- H
  2. floor(x)$ b$ Q% O2 _% {0 t! R
  3. Return the floor of x as an int.# R" f0 l2 B% Q% ^3 r/ U2 x. N
  4. This is the largest integral value <= x.; m( d; r2 X& r3 ~' C
  5. >>> math.floor(4.1)6 u0 h# s% a+ y8 k) ^
  6. 4
    & L% Z/ S# N, {) M
  7. >>> math.floor(4.999): i3 d3 Y9 ]1 H
  8. 46 r4 c, W2 h9 i4 E
  9. >>> math.floor(-4.999)
    ) }$ ?) w/ J0 e( Q8 Z" _0 S% c
  10. -5" V5 `; d0 g1 V3 _( ~! R# k
  11. >>> math.floor(-4.01)
    5 E( [* q/ K4 |; x
  12. -5
复制代码
' _" K" V% X6 ]7 k
math.pow(x,y)  返回x的y次方,即x**y6 a: ~7 [4 u1 D! |( q" j& d) S) K
  1. #返回x的y次方,即x**y
    ! }% G, |, d2 q4 p9 H& C
  2. pow(x, y)
      J5 u! G( v/ S0 e' Z; ~1 {% P  \
  3. Return x**y (x to the power of y).4 x& J: U% T% Q
  4. >>> math.pow(3,4)- l$ \1 l# V, H/ S: n
  5. 81.0; S' W0 T  z" J6 O. W/ y5 G
  6. >>>
    ) z- h# |6 Z7 |7 d) A; K! O  S% o1 B
  7. >>> math.pow(2,7)
    ' b) M0 ^0 _: I+ q& Q1 o: P6 _
  8. 128.0
复制代码
/ D3 N9 T) n+ @8 l  w( ^2 }* s
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( Q) U" T" g2 X8 d5 K$ u
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 X$ {7 W/ t" P1 x, O" C: m5 v
  2. log(x[, base])
    + ]( w; N" D, Q' A- z
  3. Return the logarithm of x to the given base.2 q( C+ u9 c' M* W+ i, Z- \; l
  4. If the base not specified, returns the natural logarithm (base e) of x.
    / [* L, k! E5 D; y
  5. >>> math.log(10)
      M" y' ?) S6 _* `; G
  6. 2.302585092994046' Z; F/ c9 J0 p7 h8 I$ }
  7. >>> math.log(11)
    $ T; p. C1 [, x! p$ Z
  8. 2.39789527279837075 m0 l, _9 a4 y8 y
  9. >>> math.log(20)- N9 G7 U5 @/ A* X( v3 j
  10. 2.995732273553991
复制代码

" `# \- O7 ~% ^1 q0 c" @: b8 K! Lmath.sin(x)  求x(x为弧度)的正弦值
& ~0 X1 A* y9 y- n+ v
  1. #求x(x为弧度)的正弦值1 v3 g( H/ G- T8 o9 q
  2. sin(x)
    ! x: I2 A) m7 V  V1 p5 z. o
  3. Return the sine of x (measured in radians).- A: R4 Q5 N+ W
  4. >>> math.sin(math.pi/4)
    1 v0 M0 \$ A7 C( m+ @, G
  5. 0.7071067811865475
    " F" }  i7 L, v/ n9 C& r/ I
  6. >>> math.sin(math.pi/2)( E: q8 ?8 m, r3 @( O+ |+ f/ I
  7. 1.09 S' U; \! J3 F2 [* t7 C
  8. >>> math.sin(math.pi/3)* H. U. A$ T0 a% S2 Y0 f
  9. 0.8660254037844386
复制代码
* W4 }8 @) n3 d) K, Y
math.cos(x)  求x的余弦,x必须是弧度- o& `+ w4 N; a. Y: @1 ^. L
  1. #求x的余弦,x必须是弧度
    $ m; C* Y8 J7 H& p" A
  2. cos(x)6 a4 U( s% ?  r2 B0 C# |2 B( O9 _' `
  3. Return the cosine of x (measured in radians).: w' y. g' c; L8 Z; |
  4. #math.pi/4表示弧度,转换成角度为45度( I$ g+ ?4 W# F- A
  5. >>> math.cos(math.pi/4)% t$ I* Q6 x+ }( W$ u* J' }- O4 A
  6. 0.7071067811865476
    $ W, n. T3 {- w! r- N
  7. math.pi/3表示弧度,转换成角度为60度
    , b8 r% j/ N/ o' I( ?
  8. >>> math.cos(math.pi/3)
    / e* @) `6 B9 ]' e8 Z8 F; G
  9. 0.5000000000000001
    ! o. I! @/ f. o: E& \5 P
  10. math.pi/6表示弧度,转换成角度为30度
    2 C. b/ t& b/ ]' ^9 v; v, ?4 `
  11. >>> math.cos(math.pi/6)8 F: ?* m& y8 P5 Z9 H. l- x! F
  12. 0.8660254037844387
复制代码
, y! N0 ^; |8 d
math.tan(x)  返回x(x为弧度)的正切值: [0 l3 b9 [" w$ `
  1. #返回x(x为弧度)的正切值
      ~* ~* ~+ }' o+ H5 M
  2. tan(x)
    % c( I9 c" O8 a. l/ V
  3. Return the tangent of x (measured in radians).* j! o8 V; _' Q! f% d
  4. >>> math.tan(math.pi/4)
    " G9 ?4 v3 n' m4 y% D, o# _
  5. 0.9999999999999999
    * L2 Y, O7 _" |* E- h
  6. >>> math.tan(math.pi/6)- p( n0 _. r2 U0 M$ z6 {
  7. 0.5773502691896257
    % g, j* W* L* ]: @3 q
  8. >>> math.tan(math.pi/3). \5 s( W7 k8 R
  9. 1.7320508075688767
复制代码
- T( d& U; R- x9 f4 |
math.degrees(x)  把x从弧度转换成角度/ P: A- D* g' L
  1. #把x从弧度转换成角度
    4 h% B# i% C6 c% i3 \0 w
  2. degrees(x)
    ! b8 W6 X3 r# h" O7 z0 V: C
  3. Convert angle x from radians to degrees.
    ( o% p6 L3 ^2 C
  4. 0 E, z% u, x$ e
  5. >>> math.degrees(math.pi/4)/ j6 X* I" H2 M9 V' y) T
  6. 45.09 P4 J7 Y; [& _, [+ \& `* R' Y
  7. >>> math.degrees(math.pi)
    " D+ R- A+ {8 x6 }' R! X( w
  8. 180.0
    ; s% I. h3 B& o
  9. >>> math.degrees(math.pi/6)& c* K) c+ W1 Z0 W1 \  p
  10. 29.999999999999996
    & X6 n1 W5 Y; V% t: S, G
  11. >>> math.degrees(math.pi/3)
    $ U$ l9 }) ^% f, b6 f% S, V$ |$ h
  12. 59.99999999999999
复制代码
5 @! E! ~% X% R1 Y; l
math.radians(x)  把角度x转换成弧度
  Z* C8 \5 Z- a4 S5 Y  V9 }7 x) ?
  1. #把角度x转换成弧度
    0 X& h! S) e8 x
  2. radians(x)
    ' ~( }+ |! T5 [; E
  3. Convert angle x from degrees to radians.. D: ?0 C7 J+ `; x9 j
  4. >>> math.radians(45)
    ' o" J5 t9 i& A2 B& J/ ]
  5. 0.78539816339744839 ?( M9 E: ]) E* T: P
  6. >>> math.radians(60)$ ]* H+ h7 s! W, U! T8 f. w
  7. 1.0471975511965976
复制代码

! B4 |! r& Z* J# Rmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
% R1 F7 D" ?: b" G5 V" F4 B
  1. #把y的正负号加到x前面,可以使用0. \2 ?" x. {$ A; A
  2. copysign(x, y)
    8 ?- B4 R) w+ R5 X  Z/ S# h) n
  3. Return a float with the magnitude (absolute value) of x but the sign # ~: g8 i/ A$ j
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 V% p0 |, Y5 c+ w% o
  5. returns -1.0.
    2 [  K$ n& s2 M1 F4 ]& g
  6. $ o4 s/ N! g' K1 @
  7. >>> math.copysign(2,3)
    7 a2 q& ?8 M2 Q0 V
  8. 2.00 ?# F* Y$ h5 J+ n2 L+ n* N$ e
  9. >>> math.copysign(2,-3)
    0 p' l6 k, E" `0 T* p/ ]
  10. -2.0/ v: ^  o6 G& a3 |( T
  11. >>> math.copysign(3,8)! E: V# I8 V8 Z0 t9 i
  12. 3.0
    * h/ {. o7 J, |$ V) f  h
  13. >>> math.copysign(3,-8), Z- P0 k2 f( H$ q
  14. -3.0
复制代码
$ x: S2 ?, \( p. `
math.exp(x)  返回math.e,也就是2.71828的x次方
3 x5 a* h: D( n( _7 R+ v
  1. #返回math.e,也就是2.71828的x次方5 ]. V* m) a. F1 n0 g- w1 z
  2. exp(x)" I, R) g( w9 Q" U' a$ V; u5 z
  3. Return e raised to the power of x.+ ]" z8 _/ R6 I+ W
  4. & I5 ^3 \! D; z- a% z
  5. >>> math.exp(1)
    ; u: x! j9 J) n# a4 G( w! S
  6. 2.718281828459045& i1 [4 h9 y0 M
  7. >>> math.exp(2)) ^9 }( ?5 v/ r+ H
  8. 7.38905609893065" a/ ]# O6 P. k9 j$ i
  9. >>> math.exp(3)
    # Z% w; O. M# s& |
  10. 20.085536923187668
复制代码
1 T% Q( u  v% D
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
8 A1 ?8 W' L9 d6 U( _$ I
  1. #返回math.e的x(其值为2.71828)次方的值减15 o. e( T$ w, p8 \) b2 a
  2. expm1(x)7 I3 W2 o' `7 ]% L) o
  3. Return exp(x)-1., v2 {0 r/ N! H4 f# u
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    4 B# }/ U3 T* K
  5. ; C/ \5 S3 B4 ?) f3 V7 n9 K% v2 j  _
  6. >>> math.expm1(1)
    / D! n. c  L! Z% S4 `, j
  7. 1.718281828459045. w8 S' K% G# G( o
  8. >>> math.expm1(2)
    * P" E0 `  ~4 U! a% |7 i; s: F
  9. 6.38905609893065
    ) a/ g% I! N. F4 ?# A) ]/ e
  10. >>> math.expm1(3). {2 F1 ~: X( l9 f* q
  11. 19.085536923187668
复制代码

) ?# N) b/ |4 `math.fabs(x)  返回x的绝对值- ~% S1 n9 k$ h# n  _4 w5 }
  1. #返回x的绝对值3 F9 n, k" ?! L% X* E( `
  2. fabs(x)* E' r4 S) }$ f- A( k9 Z4 i# ?
  3. Return the absolute value of the float x.
    9 v( l5 K) Z8 O
  4. : Z# W8 N( Y! H& c8 Z7 R& a
  5. >>> math.fabs(-0.003)
    " W; R3 b/ ~' ~
  6. 0.003/ T( @1 r. T9 l  W, a( k! i
  7. >>> math.fabs(-110)
    0 b0 Z4 n% E9 X2 u9 t$ O2 @
  8. 110.0
    ) ^/ h7 r1 v1 G
  9. >>> math.fabs(100)
    # u6 y0 E; k0 \( j4 J5 Y
  10. 100.0
复制代码
  h9 J( z- j/ @1 ~( Q
math.factorial(x)  取x的阶乘的值
3 H. Z8 [* Z9 V5 S  n
  1. #取x的阶乘的值
    6 ^9 }5 W3 l+ g. {% s
  2. factorial(x) -> Integral
    / J( H8 b. k! t% N$ c5 \
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 Z8 ^$ ]7 p' D% w  ]
  4. >>> math.factorial(1)
    & t. M! j- ^  \, e# T& y
  5. 1
    ! A* b8 j# v  r# ]6 L: q0 Q8 U
  6. >>> math.factorial(2). u& f8 l5 |& G6 [4 T% B
  7. 2
    6 `+ B' s4 Q2 Y3 @& i
  8. >>> math.factorial(3)
    3 J' V; `* O, e: d0 D9 I2 t
  9. 6
    , }) Z* o  P4 ]4 o
  10. >>> math.factorial(5)
    ; ]4 \, _% [7 u
  11. 120
    7 a/ m. N4 O# H7 C: O+ l% y9 i3 x
  12. >>> math.factorial(10)+ t8 M: _4 h5 L! J$ U: r
  13. 3628800
复制代码

& Y% Y* \) Q  a5 omath.fmod(x,y)  得到x/y的余数,其值是一个浮点数& K5 {7 V, [$ }
  1. #得到x/y的余数,其值是一个浮点数6 W& K% W/ V3 Z5 z, c! ~9 R7 z
  2. fmod(x, y)$ |; K$ B2 Z, F3 R
  3. Return fmod(x, y), according to platform C.  x % y may differ.' M' v+ S% G) d3 S/ r+ v
  4. >>> math.fmod(20,3)' m* h) \& c- S0 X) i3 |6 Q; W
  5. 2.04 S2 [* J% G' n9 D  g) ~/ \
  6. >>> math.fmod(20,7)
    4 P. s4 [; ?6 B; K5 v% g. n0 A# @
  7. 6.0
复制代码

  W. J0 f4 V4 K4 R2 w# Mmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围( j+ P+ W2 d$ K, Q) a
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . Q9 }) h8 [) d( Q  @7 `2 r& J& m
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
      n+ C& s4 b2 q4 u
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ( T; k* \0 {/ H; ~' E
  4. frexp(x)
    $ {. O8 U& n4 A+ M& _" z  \$ C- C* Q
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 P  m% q) C9 \# e
  6. m is a float and e is an int, such that x = m * 2.**e.
    7 \% z3 c' H; _$ T" ?* `3 g
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 q4 S: M% Q8 h/ E6 L: @  b
  8. >>> math.frexp(10)
    % d1 X5 i+ w' ]8 O3 `, P1 e( ]8 W
  9. (0.625, 4)# M, H" o5 p2 ]
  10. >>> math.frexp(75)- Y, O+ i0 u: H+ D3 R( K2 z9 ~, b
  11. (0.5859375, 7)
    $ L: }9 d( G2 F
  12. >>> math.frexp(-40)
    6 L3 C9 c4 X" O- I5 v! w8 K
  13. (-0.625, 6)7 a7 ?9 D: @9 m( V/ \
  14. >>> math.frexp(-100)
    , ~$ ?( v! x! k5 U4 S' ^$ d! \
  15. (-0.78125, 7)
    ( e+ R4 \5 k+ @! V6 r$ T3 `
  16. >>> math.frexp(100)
    / m5 P7 _; r3 K
  17. (0.78125, 7)
复制代码

2 _5 W8 n) p7 z" Omath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
/ N8 k# W- ?6 a
  1. #对迭代器里的每个元素进行求和操作- @0 r) ]8 @; O# f
  2. fsum(iterable)9 C% S! S* T9 ~. d/ i
  3. Return an accurate floating point sum of values in the iterable.7 O+ n* x" k" e7 l) S; v. P: {& c/ N, b
  4. Assumes IEEE-754 floating point arithmetic.. t6 i6 m/ G- _4 \5 ?6 P) a0 e; @( m
  5. >>> math.fsum([1,2,3,4])* \& Y, P! i, z: y: k1 M; i* ~
  6. 10.08 y  ]" t5 O1 c# U# [) U
  7. >>> math.fsum((1,2,3,4))
    " V: s3 M! ?: v
  8. 10.0
      a. x: }$ k: v# ?
  9. >>> math.fsum((-1,-2,-3,-4))+ D$ L' r# r" z9 T4 F
  10. -10.0
    5 b& v8 J; n2 g1 ?+ t% ]2 E8 z% @
  11. >>> math.fsum([-1,-2,-3,-4])5 d. W: ~* l2 o& H; G# s; u& X- x
  12. -10.0
复制代码
3 x3 x; H. ]* N+ n
math.gcd(x,y)  返回x和y的最大公约数: D' d9 G- `/ k/ \3 o) G+ D! n
  1. #返回x和y的最大公约数; x6 J. E2 j$ {$ D5 }. J
  2. gcd(x, y) -> int: {4 p8 l( T: |! d  ~5 `% v
  3. greatest common divisor of x and y
    / d) B1 e9 m: P+ r/ i
  4. >>> math.gcd(8,6)
    % f- {% @1 D" Q* Q  N7 P
  5. 2
    - t  C. M: Q; B
  6. >>> math.gcd(40,20)
    ( C- ^' X1 K. ~( b  i
  7. 204 G/ u9 f0 @% x0 q! ~
  8. >>> math.gcd(8,12)
    8 c8 K5 x4 V9 W
  9. 4
复制代码
& G& ~. G+ o( y1 a
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
: K5 P. E8 X6 _5 N
  1. #得到(x**2+y**2),平方的值7 P6 ?& V- {' }& g9 P- R
  2. hypot(x, y)* K5 q1 G7 p( [. d9 P' F
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    , P5 _" C# l7 h& m
  4. >>> math.hypot(3,4)0 u  [$ Q7 z; q' O$ |5 E! S' x
  5. 5.0& a* T4 c" U$ f/ x( q1 }
  6. >>> math.hypot(6,8)( b& O# l9 f# B8 W9 z  ~7 k
  7. 10.0
复制代码

- F; f8 H7 Z! }! K$ t0 j' [7 amath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False4 w" I- ^0 g) b, Y3 E7 d
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 v, a2 }, U+ z  \/ u; W+ t5 \+ f
  2. isfinite(x) -> bool
    7 F+ P1 [' C- i/ ^# ^4 c# w. n
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.% I) Q/ d# G" I8 M3 V7 i( s
  4. >>> math.isfinite(100)
    $ W; l7 I( c, f" [
  5. True
    : k9 ]5 X% T, D5 V( P# d0 ]
  6. >>> math.isfinite(0)4 l  |+ T2 ]* e7 x9 X4 r
  7. True" R6 k4 |( S$ A9 F' K
  8. >>> math.isfinite(0.1)
    ! _+ a" E& P4 i
  9. True! s6 N7 C' j2 `& k3 o5 C2 k4 I
  10. >>> math.isfinite("a")% m7 e2 U+ S$ v) A( Q/ G9 ?
  11. >>> math.isfinite(0.0001)
    + k. U/ h  B7 J
  12. True
复制代码

& B% c" y, T, omath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 L' T$ d  T4 O8 l
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False6 S) x4 y. u( G
  2. isinf(x) -> bool
    - x- z6 w" R" k. c9 r* m
  3. Return True if x is a positive or negative infinity, and False otherwise.
    , I' o7 V( E) y2 J* E( e( {1 B" N" n
  4. >>> math.isinf(234)0 _: F( I2 s5 s6 h: T
  5. False
    * }6 r% }, k. }; n* ~" ]) O
  6. >>> math.isinf(0.1)
    8 y1 ~4 V+ Y( M) E
  7. False
复制代码

. ~4 J+ D8 n6 |9 H( K9 K( C" Tmath.isnan(x)  如果x不是数字True,否则返回False
% m/ h& y2 }3 @6 J; b
  1. #如果x不是数字True,否则返回False
    & b$ ?' p0 ^2 \  V; S
  2. isnan(x) -> bool. ^/ }* d/ P7 e2 L0 ^- L
  3. Return True if x is a NaN (not a number), and False otherwise.) h$ d4 Q' M' ~' b4 m
  4. >>> math.isnan(23)
    : Q2 u6 Q0 n9 R$ i, U
  5. False- r3 Q* f1 M$ V7 f3 e' B, s
  6. >>> math.isnan(0.01)* T9 X/ A  c0 X/ M% \
  7. False
复制代码
5 W3 T6 e( P( W3 |1 `2 n/ b# P9 ]
math.ldexp(x,i)  返回x*(2**i)的值
& d4 K" [% R( y+ E* ]
  1. #返回x*(2**i)的值
    : w2 W6 o% E6 f
  2. ldexp(x, i)
    " I# w" L; E1 v
  3. Return x * (2**i).8 Z7 c) [. J& J) |" O! k6 F
  4. >>> math.ldexp(5,5)
    % L9 |5 n6 W7 ?6 j
  5. 160.0- R  ^* T' d8 m. t$ Y4 g8 B
  6. >>> math.ldexp(3,5)$ P( a6 i2 x" k% _! D6 x
  7. 96.0
复制代码

  H9 C9 X: Q5 _math.log10(x)  返回x的以10为底的对数' h' V2 I$ `/ s5 ?' @+ K% Z0 g3 P- j
  1. #返回x的以10为底的对数! F6 x% K3 @; H% o4 m
  2. log10(x)
    # g; c8 }' ]2 m! f3 ~
  3. Return the base 10 logarithm of x.* `6 s, H( P3 f( ?
  4. >>> math.log10(10)
    # G5 C8 a8 L" V% x
  5. 1.0
    0 n0 P) O  J3 n1 Z. l
  6. >>> math.log10(100)
    $ ?6 r* a# r; U$ Q* D/ Y- r
  7. 2.08 _( T7 K' T+ T- W; C. k2 z$ r
  8. #即10的1.3次方的结果为20& d; _' C0 T' R9 G( ~
  9. >>> math.log10(20)
    6 D. |1 s. h2 Z% V2 {' F' Z- @% \
  10. 1.3010299956639813
复制代码
; ?  [! C7 i( I5 O  A* v
math.log1p(x)  返回x+1的自然对数(基数为e)的值2 T0 s# y& v2 J( l+ _$ p) M
  1. #返回x+1的自然对数(基数为e)的值9 l8 M- x% M" y
  2. log1p(x)
    1 {2 e4 t& D* }% Q: k
  3. Return the natural logarithm of 1+x (base e).0 I& ?" Z2 J3 K$ \9 T1 T8 M' @: W
  4. The result is computed in a way which is accurate for x near zero.
    ! E: K0 w* }( i* h
  5. >>> math.log(10)
      r. J6 I9 `1 q/ D3 W- M! q
  6. 2.302585092994046: \! i7 g  f  X0 V+ l7 `+ _
  7. >>> math.log1p(10)
    * u$ c' N5 g1 n# `! x+ l& i( }( ?. F
  8. 2.3978952727983707
    8 j+ W# D1 i0 Q$ |& W
  9. >>> math.log(11)
      s0 B0 q: O* `
  10. 2.3978952727983707
复制代码

! [) |3 ?* M, r# Fmath.log2(x)  返回x的基2对数
4 E, u) H  M! x/ |  c& V" A0 X& Q
  1. #返回x的基2对数
    : `0 ]( H; Q2 ~) |7 N3 k
  2. log2(x)2 d! B- X. K: X! E! N( Y+ `
  3. Return the base 2 logarithm of x.
    ) F  T6 w$ L; o8 z) ?  `, z5 Y
  4. >>> math.log2(32)* d) Q6 M; J; O: P
  5. 5.0
      {  I: b0 }( q7 ^  [5 l
  6. >>> math.log2(20); y3 e7 k1 @3 l# }4 _; S( s* o4 _
  7. 4.321928094887363
      e3 j- B5 t$ ]6 r1 u7 c
  8. >>> math.log2(16)
    3 m& ]! U- Q0 x9 H! U& r) B
  9. 4.0
复制代码

0 w. b  ], J  y% m+ amath.modf(x)  返回由x的小数部分和整数部分组成的元组
& b7 u* A# A1 i, V% I8 h
  1. #返回由x的小数部分和整数部分组成的元组9 _* y+ L$ E6 N& s: ^# h8 z" \
  2. modf(x)
    ) f6 }& a" \; \; V- j7 D
  3. Return the fractional and integer parts of x.  Both results carry the sign8 g9 n! r! P* Q6 C
  4. of x and are floats.
    ) |' P5 L, D1 Q- R4 ~6 ~+ f, n
  5. >>> math.modf(math.pi)
    / d. j( y' \3 m# w
  6. (0.14159265358979312, 3.0)
    1 j. J8 `+ ^5 r' L: C8 e
  7. >>> math.modf(12.34)4 C: |( F2 S4 V
  8. (0.33999999999999986, 12.0)
复制代码
* A4 h/ y  i9 @6 p+ H
math.sqrt(x)  求x的平方根  x" T! z6 ~: E. \" l
  1. #求x的平方根! b! I9 G6 m7 ?6 J! k
  2. sqrt(x)% X: p) w2 ]: r5 U% T& g, {. g
  3. Return the square root of x.6 o' c! p. t; s5 a
  4. >>> math.sqrt(100)
    3 q/ i$ b! e! X  @. P
  5. 10.0$ G$ D( k" |+ `) s' v: l- S
  6. >>> math.sqrt(16)
    ' \' [6 R! }5 l
  7. 4.0; A0 C7 d+ P7 u9 w# V" O
  8. >>> math.sqrt(20)) r9 T) R; v  f) _! |+ D
  9. 4.47213595499958
复制代码
. V* C) T5 e: o7 r" s7 |
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分- N4 A( |( F/ N- Q' |. P
  2. trunc(x:Real) -> Integral, E! ~0 q- L# Y2 v
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    # ~0 e* }  v8 R
  4. >>> math.trunc(6.789)5 ]+ |$ s$ G2 G  c: M$ B3 `- A$ J
  5. 6$ f% |& N4 z7 v1 Z% c7 d- \5 H
  6. >>> math.trunc(math.pi)
    2 O' H! A; |, A" r6 K# `: F
  7. 3
    , X6 G9 R/ P9 x# l' Z5 X
  8. >>> math.trunc(2.567)( L% k6 \9 `; t% r" w& @# y) Z# \
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-28 14:33 , Processed in 0.098441 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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