新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

$ b( o8 c% S7 l9 G, b【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。+ w( U7 y& Y1 @7 b. i% i; U( M
( N3 N! Z. F1 v  V% l4 _4 S
方法18 c0 f# O! E9 I5 M) ?
  1. >>> import math- a8 _+ G! g2 W  N" m. T
  2. >>> math.sqrt(9)
    ) e' M' F, W3 Z" `/ r
  3. 3.0
复制代码
方法2
9 C& U& O& q" D& H
  1. >>> from math import sqrt
    ' c, N- N9 M/ _% E. R7 B
  2. >>> sqrt(9)7 O' f/ G. g! v* z; y# U$ Q: r: s3 @
  3. 3.0
复制代码

2 {! b- i' Z2 Q# c9 B0 t

1 p  E9 {9 U" Q4 X5 B1 X7 N; ~" W7 C
math.e  表示一个常量
; p$ X3 E' N1 C+ E) w0 h
  1. #表示一个常量
    5 H' O1 d+ T1 w0 x
  2. >>> math.e( y( t5 Y1 f# U: D" e
  3. 2.718281828459045
复制代码

- A5 x# F# j' K! Gmath.pi  
数字常量,圆周率
7 \8 N) x- ^# n! U" F
  1. #数字常量,圆周率: E% R' U" b8 {! T7 \2 {; e" f
  2. >>> print(math.pi)
    ( b. V. M6 q2 V6 ?; r* p' z- ]
  3. 3.141592653589793
复制代码
6 |4 \/ m3 S- z# e! X
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 R2 T0 p. t+ X: P: O4 \! U
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 F- y' K) ]2 V7 P5 a" S7 i
  2. ceil(x)' G" y, P5 A2 H$ w, I7 ?6 f
  3. Return the ceiling of x as an int.( T; M; d/ `+ ^4 B" F4 e' o
  4. This is the smallest integral value >= x.( B% T0 j1 f  u

  5. & [1 M7 A+ v8 z# J0 t) q' x  Q
  6. >>> math.ceil(4.01): x: u% o8 i* B
  7. 5- G2 G% v7 n$ \0 R; M  l
  8. >>> math.ceil(4.99)
    4 M( Q$ A' o* r( }/ _5 Z) U' x
  9. 5( m6 j/ D" M# \( {
  10. >>> math.ceil(-3.99)+ g) U) F9 ~% q$ e8 a; B* V# C& _; z
  11. -3& T* {3 A, ~! A6 s( i! o2 r* Z2 H; A1 x
  12. >>> math.ceil(-3.01)1 I+ d' Q: g# w6 m% U7 ^
  13. -3
复制代码

; e3 j/ Z$ _$ X+ q* ]2 n( n' tmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: Y+ X, c1 n) ~' P$ X
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    2 n3 d% A6 J9 U
  2. floor(x)
    9 f+ j, D7 L+ F; X) j* k* C
  3. Return the floor of x as an int.
    6 Y% B9 }" u8 H2 ~
  4. This is the largest integral value <= x.4 R% f, y6 w  a% _
  5. >>> math.floor(4.1)/ c$ C1 Q0 M3 g
  6. 47 _, ^+ k8 f' t
  7. >>> math.floor(4.999)
    ' S' B# O1 `8 P1 _, K# I
  8. 4
    9 e% m: R2 k% s( K
  9. >>> math.floor(-4.999)
    4 Z$ d- e( R5 q, L1 T
  10. -55 \" D: ~2 Q5 ~$ ?, F
  11. >>> math.floor(-4.01)
    : [# Y( K2 x$ C9 }7 H$ z
  12. -5
复制代码

! h  W5 x5 B- Q4 D+ Jmath.pow(x,y)  返回x的y次方,即x**y
2 {: X  Y# g( [; j0 [. \
  1. #返回x的y次方,即x**y
    ! f/ G' Z0 o8 }9 r4 k
  2. pow(x, y)7 k3 b  w2 f! e3 C8 ?
  3. Return x**y (x to the power of y).
      L# W" |( ]% g! Q2 S
  4. >>> math.pow(3,4)
    + \9 B6 [) N2 r6 `, y4 t4 T  L
  5. 81.0
    3 @/ ^& ^; k' ^' n4 S# F
  6. >>> * i* @3 F# ~, ]$ l' _/ C- ~5 F# N
  7. >>> math.pow(2,7)
    ; N4 }$ C2 N0 F. y, K6 _
  8. 128.0
复制代码

* I# C$ B1 D3 d2 p4 u0 zmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* T3 v2 U5 [/ T  w; G! u  B6 n8 i/ p
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 S1 ?! `- @# f# N* ^3 |
  2. log(x[, base])
    $ i, w3 u: C, {+ [' T4 V- L
  3. Return the logarithm of x to the given base.  @, U& l5 V& Q! k$ g/ X2 R
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 d9 j- h+ `. k3 F
  5. >>> math.log(10)
    ' _8 o; h( y1 |! @; x7 J" \
  6. 2.3025850929940464 V! n3 ?$ o0 Q7 F; s8 t+ ^( g. n, k
  7. >>> math.log(11)
    3 D' P5 h# g& B2 O' S+ K) w# d
  8. 2.3978952727983707# O3 r  j4 h  ~5 {: ?
  9. >>> math.log(20)
    2 S* f5 [/ M& @- p0 C& ^, Q- d
  10. 2.995732273553991
复制代码

' [6 d% Z: j- X$ ]- r) s6 nmath.sin(x)  求x(x为弧度)的正弦值
6 W8 m8 V2 Q; s9 u3 ]
  1. #求x(x为弧度)的正弦值% _: a2 r3 ^* [1 G! }
  2. sin(x)
    . T0 L; P" x8 J/ q% m
  3. Return the sine of x (measured in radians).
    ' Q; |$ K, M! B: K
  4. >>> math.sin(math.pi/4)4 Z. W4 a3 p4 k2 [! r
  5. 0.70710678118654752 s& O2 O$ F4 f7 D3 c' h
  6. >>> math.sin(math.pi/2): L3 c5 O; s& T, h
  7. 1.0
    ; H- V6 U. G8 g7 [1 ~2 y- {, ~( l
  8. >>> math.sin(math.pi/3)
    " |; }' c0 \/ s
  9. 0.8660254037844386
复制代码
9 a4 s' j" d; m; w+ q+ j& l3 |
math.cos(x)  求x的余弦,x必须是弧度
+ j2 u& C7 X) e1 ^2 o4 S
  1. #求x的余弦,x必须是弧度
    ( Z5 X$ e0 c, m( A" }7 V" d4 ~! R2 ^
  2. cos(x)
    4 \+ B- t7 Z/ u5 _9 G% S
  3. Return the cosine of x (measured in radians).4 m- w: w- z4 C. H+ ]( o" H
  4. #math.pi/4表示弧度,转换成角度为45度
    7 D; q( @9 Q7 x+ i3 B4 ~
  5. >>> math.cos(math.pi/4)
    4 B$ ~% W0 A) |
  6. 0.7071067811865476
    8 k* \& h% l- r3 ~9 V
  7. math.pi/3表示弧度,转换成角度为60度- d: Z% m8 o. v  s
  8. >>> math.cos(math.pi/3)
    / @3 P$ U- Y: T6 Y- I, [
  9. 0.5000000000000001
    ) Y+ Z7 P7 Y& G# l& i7 R& r
  10. math.pi/6表示弧度,转换成角度为30度
    / ^; @, e3 l" m
  11. >>> math.cos(math.pi/6)
    , W6 @6 s9 v1 K7 P
  12. 0.8660254037844387
复制代码

2 B! {% [3 ?3 E& ^math.tan(x)  返回x(x为弧度)的正切值+ M4 K% u# B& b% `$ N  y
  1. #返回x(x为弧度)的正切值
    8 T" n4 d1 @. w4 U, N! s
  2. tan(x)
    % m3 Q' f* G% d: U( g% ^. W
  3. Return the tangent of x (measured in radians).$ Z4 l5 Z6 d( {& T- Z" M  d% D3 N
  4. >>> math.tan(math.pi/4)% m  Z% [+ ?. A3 y
  5. 0.9999999999999999. C% _' L9 w  r# F  D) ?/ l7 ?0 R
  6. >>> math.tan(math.pi/6)
    ! |, g& w( ^% w; J& M& e
  7. 0.5773502691896257
    $ }- z0 [; k% ~
  8. >>> math.tan(math.pi/3)' A3 n' ?) N5 M6 f$ \
  9. 1.7320508075688767
复制代码

) B/ L& Q( b0 umath.degrees(x)  把x从弧度转换成角度% S, L: m* r- E- T# {
  1. #把x从弧度转换成角度/ H" l- B0 G; I% R1 Y7 z! g; I& W
  2. degrees(x)% J8 J. J* r. \" y1 B* h0 y
  3. Convert angle x from radians to degrees.. Z( P* I% |& I2 @' H3 c

  4. $ `6 `2 `# z5 l$ M9 h6 k
  5. >>> math.degrees(math.pi/4)
    2 o% O9 w& ]: a  M% o
  6. 45.0
    - f5 {. O7 Y  V5 V( x9 Y6 ~0 s. x
  7. >>> math.degrees(math.pi)$ D  X6 n  V: B. W# ^. W# [2 K
  8. 180.0
    , T/ B0 H# r! D
  9. >>> math.degrees(math.pi/6)9 D  x, p7 B8 V4 ]" P% R, w) H- J
  10. 29.999999999999996+ v& c7 E/ ]( h% O! O
  11. >>> math.degrees(math.pi/3)+ b$ m; @% o# x* b
  12. 59.99999999999999
复制代码
5 Y4 i1 C, f$ _/ H2 q! F
math.radians(x)  把角度x转换成弧度) j) J4 Q6 Z! d2 o  Y2 b) U' L, U
  1. #把角度x转换成弧度
    & z+ I" J  v& @; i
  2. radians(x)$ Y  z* f; G! w- G* I
  3. Convert angle x from degrees to radians.: q+ W- x( g" x  F( w
  4. >>> math.radians(45)
    ! z7 U, x: A0 c9 U
  5. 0.7853981633974483; R, f" a$ |; \. B2 f# i1 p4 Q' |
  6. >>> math.radians(60)% Q: m* E5 }/ U' Z: l' B3 s5 T
  7. 1.0471975511965976
复制代码
/ d- v6 D1 H$ T
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
. _0 |) i& o$ c2 D/ N( Y
  1. #把y的正负号加到x前面,可以使用09 \# u' |9 [( f6 }$ j: ^' n! b; d
  2. copysign(x, y)5 e8 f5 Z. k. w" v3 x, _: @
  3. Return a float with the magnitude (absolute value) of x but the sign $ f3 r% T6 }& K- T4 c# @' \6 A; u: [$ C4 U
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 P, J5 U, K# X; e" _5 {4 Z
  5. returns -1.0." X) e& i7 y$ r  n6 i
  6. 3 |6 r" s7 e' U, Z
  7. >>> math.copysign(2,3)7 o' y; r! h& H  Z1 ~
  8. 2.0
    9 W* c, B; j) h% u
  9. >>> math.copysign(2,-3)& u. C& L' N* L5 e* _3 B: w; j
  10. -2.0
    ( h" M0 C) f5 ^7 a+ X+ ~
  11. >>> math.copysign(3,8)
    5 o/ F+ W+ I3 }' M* E7 b( R
  12. 3.09 e5 B* F# L- }9 O1 F; a2 j
  13. >>> math.copysign(3,-8)
    2 g" s/ g( X! @- m4 o' T/ T
  14. -3.0
复制代码
$ R, }- y2 d3 @4 g0 j0 _# E
math.exp(x)  返回math.e,也就是2.71828的x次方" G3 _: c3 q$ _2 ^6 {! Q. _& |
  1. #返回math.e,也就是2.71828的x次方/ r7 l* h7 @) h6 x7 I& ~3 W
  2. exp(x)! N' H- f1 b; l! a7 G
  3. Return e raised to the power of x.
    7 q+ r" o5 S$ f% y/ p4 {
  4. ) W. I, x' _/ g$ ~8 j9 c
  5. >>> math.exp(1)8 s" {2 ]: J: y
  6. 2.7182818284590458 n  n% [8 G4 \! I, b7 R9 [
  7. >>> math.exp(2)" B% I4 [8 `3 k
  8. 7.38905609893065" w: `3 P3 b+ F7 N
  9. >>> math.exp(3)
    3 S9 H# O. P- P6 \9 i3 `) N
  10. 20.085536923187668
复制代码

# ]; i# f  [6 x1 e$ D" {( n4 gmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
. ~) F; v& x- x2 ?
  1. #返回math.e的x(其值为2.71828)次方的值减1' b0 b' [" X3 U' K# Z+ H
  2. expm1(x)
    ( k6 y* Q# I3 X4 S& H4 G
  3. Return exp(x)-1.' H% H. l" J! Z* Z* g8 |  {
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.! ]( _1 g' {& M+ }6 T+ r( j2 G
  5. 0 {. Y  x0 O, L" S  G' u1 x
  6. >>> math.expm1(1)
    , ?1 r. V  B6 J3 N
  7. 1.718281828459045
    8 b, \3 g) c" i. Q
  8. >>> math.expm1(2)
      W8 c1 e( x; G1 `/ N0 E. N
  9. 6.38905609893065/ o+ Y3 U6 S2 M" P1 ]" S
  10. >>> math.expm1(3)
    $ b1 e" }5 d! n8 g5 O  t
  11. 19.085536923187668
复制代码

& X5 O. O- n, m: B, Q. L( t8 imath.fabs(x)  返回x的绝对值. h/ K* o' D' A8 I$ b, m
  1. #返回x的绝对值
    7 G% c% d& L% l9 c  C" h; |# l. D( J
  2. fabs(x)
    * s& ^1 x1 x# Y  T7 ]; J% T# ^3 X3 [
  3. Return the absolute value of the float x.3 ?9 I# W# J% k
  4. ( {2 l  Q& \5 V+ h6 ~5 H( R7 y' U
  5. >>> math.fabs(-0.003); l9 I5 }, ?3 V+ G  t5 U: G/ B
  6. 0.003
    1 D: W/ w& \! O& E7 s# I- @! s
  7. >>> math.fabs(-110)
      P0 P" p2 m+ r1 |$ f! U3 Q
  8. 110.0
    ; D3 l- b3 D( T; N5 [
  9. >>> math.fabs(100)
    % i" V; _" U9 \  @+ b& x! K
  10. 100.0
复制代码

$ Q0 o$ s: }# n) c% }math.factorial(x)  取x的阶乘的值  H. m1 }+ U1 F8 I" K6 q
  1. #取x的阶乘的值6 ^% a6 m; G/ T# e) C# j6 d
  2. factorial(x) -> Integral
    2 @! ]5 Q0 a* C* Y5 i+ x
  3. Find x!. Raise a ValueError if x is negative or non-integral.1 S  s# m. X8 w$ g8 [" x7 I
  4. >>> math.factorial(1). D8 a$ r' ]$ L* M( R# B8 p0 i
  5. 1
    . d6 B) T; K9 ~, J! i/ O$ T
  6. >>> math.factorial(2)
    , k6 k0 U( y8 E; X, R; O& E. ?
  7. 21 N: `6 J- Z; R5 Q! q" t
  8. >>> math.factorial(3)
    ( Q+ T4 {% {& r$ L
  9. 6
    : O2 J( n3 I, p9 h
  10. >>> math.factorial(5)' t2 }5 F, d4 A% r& Y
  11. 120
    6 L" q1 j6 ^; x9 B/ m1 R( f
  12. >>> math.factorial(10)
    * F  \' l0 k' D7 r/ P- E
  13. 3628800
复制代码

, I2 H2 c0 f' c( J9 f0 Fmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
( l1 u- e9 s* B+ e0 [2 Y. \/ \
  1. #得到x/y的余数,其值是一个浮点数
    $ `0 Y* n) D! g: ^  y3 @: a
  2. fmod(x, y): K& X' w  K, n% `1 U
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    6 K. u$ \. U$ P# {
  4. >>> math.fmod(20,3)
    9 H) z' S) I! H
  5. 2.02 M! j) j. P" D( q2 `2 B" m
  6. >>> math.fmod(20,7)/ s& ^( y& C2 R( C9 [" u: e, x
  7. 6.0
复制代码
4 t$ N/ @3 l5 j  k% A
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2 D4 n; @' |8 G& h  c; M8 W' n
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; h1 M' C& L3 G; D! ~  q
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 \8 j* n: j' K$ J! `; x
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    9 h' M0 R9 {. n' K
  4. frexp(x)" u+ d. ~; j1 ^* ?; o7 d( S. _+ j0 p
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 c8 l' x9 }7 D  _7 d: [
  6. m is a float and e is an int, such that x = m * 2.**e.
    5 L- U- `$ E3 h6 S4 v/ R& @
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    : s7 d( Z) }4 ~% h0 V0 i6 X
  8. >>> math.frexp(10)5 s/ P: N! V9 i1 W
  9. (0.625, 4)
    + M# V  W6 m+ E# g/ u
  10. >>> math.frexp(75)
    + [! R9 j% v6 [6 U# Z) U1 O
  11. (0.5859375, 7)
    ) }; S+ l, n; S$ g$ `& c; L' C
  12. >>> math.frexp(-40)
    7 H  [8 P! k! W8 n( w
  13. (-0.625, 6)( G7 A9 h2 W  E! D# Q/ q
  14. >>> math.frexp(-100)
    8 p8 C/ Q6 T( w# Z4 m5 M
  15. (-0.78125, 7)1 \  B  V% ]; |8 ]; n
  16. >>> math.frexp(100)$ h1 z2 E; p' U6 C8 ]: u: ^. o# s& C
  17. (0.78125, 7)
复制代码

- C$ l% M& J9 Qmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列) t9 |+ s0 D- O9 O. D: l( {
  1. #对迭代器里的每个元素进行求和操作# |) t3 H5 f  a! n) t  C. C
  2. fsum(iterable)1 k9 l( w# R+ m% V9 f9 p! g- l
  3. Return an accurate floating point sum of values in the iterable.! c: L, y8 g, B! V! ?( K
  4. Assumes IEEE-754 floating point arithmetic.  e& i7 ?/ T8 E9 c
  5. >>> math.fsum([1,2,3,4])
    / \2 n4 a! R; y! y/ m# T
  6. 10.05 [6 J( ]: [$ U' ?  j  X4 o
  7. >>> math.fsum((1,2,3,4))
    3 k4 x. P& p+ z( C4 `: ?
  8. 10.0
    ! l! |, I- v' l' E1 ^$ W' C) K* |
  9. >>> math.fsum((-1,-2,-3,-4))
    2 [6 W/ W. c, a8 O* O' x* J
  10. -10.0
    : J% }* U& O6 K, {
  11. >>> math.fsum([-1,-2,-3,-4])
    & ?( s6 Y# f. w" z, F- M5 y
  12. -10.0
复制代码
, e( w- f/ b! x; ^% \+ W* q/ N& @
math.gcd(x,y)  返回x和y的最大公约数3 b+ y4 ^' q3 P5 ~3 t/ v% B
  1. #返回x和y的最大公约数
    4 q8 f5 A1 }2 o' g; ~/ {( s% ?8 T
  2. gcd(x, y) -> int
    8 Y( A0 p8 d3 [7 z  _
  3. greatest common divisor of x and y, i" |# |9 u+ R: T3 {
  4. >>> math.gcd(8,6)
      f* L6 j+ F# ]+ q' U
  5. 27 R0 o5 r2 u3 F
  6. >>> math.gcd(40,20)' F' u6 m' J" s: B: Z
  7. 20) B' e! h' n7 ]6 A$ H4 ~6 u: Y
  8. >>> math.gcd(8,12)
    7 s1 h6 `5 R) U8 p( K0 ^' Y4 Y
  9. 4
复制代码
( r0 @! a8 p+ C$ N  ~$ I7 L* c6 V
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
4 ?9 _3 u- G0 b( _* D5 s
  1. #得到(x**2+y**2),平方的值- [2 u7 G! J; e4 y7 m
  2. hypot(x, y)
    - E9 E2 d' T1 _% L
  3. Return the Euclidean distance, sqrt(x*x + y*y).- b3 g# L" I3 z/ z' ?+ ]8 r0 k0 A
  4. >>> math.hypot(3,4)4 p9 I* L5 z: T3 U
  5. 5.01 ^+ f1 g. |  [) P
  6. >>> math.hypot(6,8)
    # n/ Q% @/ C3 {" I  X. J0 o
  7. 10.0
复制代码
% ~% Y$ e# }) f+ D# O
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False9 B& S' ~, X- u! b, `
  1. #如果x是不是无穷大的数字,则返回True,否则返回False5 e; K- t* W4 D/ L. b/ t
  2. isfinite(x) -> bool- v' f% u( a4 q1 d2 X
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    . @' D* J9 O, p) G1 o+ B
  4. >>> math.isfinite(100)# d. a; t7 F: S. E# g
  5. True
    ; v% I- [% g) @. f% e) ?
  6. >>> math.isfinite(0)# V$ I6 s+ D, I+ C( J2 a
  7. True1 m1 b- d7 H5 {
  8. >>> math.isfinite(0.1)
    ' H% P3 G$ V/ ?  [" {/ o2 F8 ]0 x
  9. True7 Y& u& H4 a, F$ p1 e1 q/ N) [
  10. >>> math.isfinite("a")
    4 p; Y  l$ `# {5 ?4 P9 w7 N
  11. >>> math.isfinite(0.0001); i+ B  B: r" h! |7 [, r: P% q8 ?, E
  12. True
复制代码
2 w1 m' I# d& h5 y) u
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, Z, X+ O7 H' w* Q- ^
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False0 J6 a* Z& Q6 I8 l; B
  2. isinf(x) -> bool
    & h" P6 c0 `( h2 D( t% r) s4 t
  3. Return True if x is a positive or negative infinity, and False otherwise.& j; w, l& x' I2 |! P7 ^
  4. >>> math.isinf(234)/ W/ P* ]! H2 b9 q
  5. False8 z3 X% ^7 R, n" _1 u* M! v
  6. >>> math.isinf(0.1)+ ^2 G: ~* N# @; J+ |$ u/ ^; P; |
  7. False
复制代码

* D2 n+ j1 C* u" o, y6 s: q, Emath.isnan(x)  如果x不是数字True,否则返回False1 a& e, O$ y  j; D7 F
  1. #如果x不是数字True,否则返回False
    + [0 d; f4 P; E& E2 h& n! t1 O) r
  2. isnan(x) -> bool
    0 Z) o, q9 M9 T) A( J
  3. Return True if x is a NaN (not a number), and False otherwise.# i- @- c- O4 f
  4. >>> math.isnan(23)
    " W4 R& z3 \5 A" b. A% F
  5. False
    6 D3 y  U$ S; G8 ]) H; }
  6. >>> math.isnan(0.01)4 h8 N: N8 c4 P5 f+ p
  7. False
复制代码

7 ~( i, a2 _3 xmath.ldexp(x,i)  返回x*(2**i)的值
/ n: [$ K+ w" Y' u( p& f2 U
  1. #返回x*(2**i)的值4 p0 X0 y: S6 [, h: u
  2. ldexp(x, i)
    # p. b3 |* @# F0 `  e
  3. Return x * (2**i).4 {7 g1 t; ?- A$ y8 F
  4. >>> math.ldexp(5,5)2 K6 d* T4 v8 b' |8 V
  5. 160.0
    4 u5 w* j, e- ~$ n  h! Q* `
  6. >>> math.ldexp(3,5)
    + _( G6 m; o9 w1 a9 E, G
  7. 96.0
复制代码
. v9 w/ G3 h/ b6 U( R: S+ R+ u
math.log10(x)  返回x的以10为底的对数  n% M. g. B7 v, Q, a9 G% G
  1. #返回x的以10为底的对数
    % }5 \9 E/ K9 T( {8 M
  2. log10(x)$ j" v, s/ \4 f( b, [! `" j
  3. Return the base 10 logarithm of x.
    3 o# n. O; f" B& x; ?0 w# ^% j
  4. >>> math.log10(10)4 F8 ?- w0 s9 Z# @1 V/ B% G
  5. 1.05 i0 r% {. d; L3 M7 ]6 l1 i/ L
  6. >>> math.log10(100)' r$ R* J8 j. s6 `3 d) |# o$ Z4 Z/ ]
  7. 2.0" G5 d6 s8 s) f! L7 r* i1 e7 D8 ~) E
  8. #即10的1.3次方的结果为20( u7 Z& Z4 s* J/ ~; j! x/ A
  9. >>> math.log10(20)! _. u& A& G- A6 ^
  10. 1.3010299956639813
复制代码
$ m2 N" y" ?1 E8 @8 Z& r* @
math.log1p(x)  返回x+1的自然对数(基数为e)的值4 p" F: F) |: o6 l
  1. #返回x+1的自然对数(基数为e)的值
    8 W4 u& Y( z) Y& r4 Q6 c7 g
  2. log1p(x)% e  u7 x$ g( l# `) F  y
  3. Return the natural logarithm of 1+x (base e).# ~+ p$ C$ c, C
  4. The result is computed in a way which is accurate for x near zero.
    ( R1 {& k4 }1 |3 p0 Z- \4 a- N
  5. >>> math.log(10); ~% k5 @% f5 Y! \
  6. 2.302585092994046
    - Y. J; b3 e: l5 J7 E+ c4 A7 ^+ u
  7. >>> math.log1p(10)9 G4 p/ w3 Z3 U5 u
  8. 2.3978952727983707
    5 D8 w$ w) j# b5 o% M0 B& C
  9. >>> math.log(11)
    , k, L. {5 j1 A' |) k
  10. 2.3978952727983707
复制代码

# Q* A$ n* `, ~8 mmath.log2(x)  返回x的基2对数
1 z" w: _# D, Y3 O, p3 B
  1. #返回x的基2对数
    2 s) l$ ?( G) ]) r, c: ?
  2. log2(x): s: N4 Q) X' k" s8 L
  3. Return the base 2 logarithm of x.
    3 I: I4 M; F" y4 e& T2 f3 G( t
  4. >>> math.log2(32). q- {9 I: I* Z, U
  5. 5.0
    ! D' z5 {  h- z  T0 x4 _. ]5 W
  6. >>> math.log2(20)
    0 F3 a% Y4 T' P
  7. 4.3219280948873637 v  b; n+ [* F7 O3 I
  8. >>> math.log2(16)5 f$ B$ [" _9 g0 E$ [
  9. 4.0
复制代码

$ }, V' m% h& z; A& ymath.modf(x)  返回由x的小数部分和整数部分组成的元组. [$ x0 s7 D! o  b# E% l- l% S$ ^
  1. #返回由x的小数部分和整数部分组成的元组
    7 J9 S. U, ]: K9 S( D
  2. modf(x)7 y$ z. k8 ~( t0 B3 a8 z
  3. Return the fractional and integer parts of x.  Both results carry the sign- w0 G3 M" F9 C6 d9 i) E; d
  4. of x and are floats./ L3 H8 X7 V% ~: i, h$ p$ a' ?9 B
  5. >>> math.modf(math.pi)
    % w7 {# Q; ]. X7 {$ E* b* Q  j- w
  6. (0.14159265358979312, 3.0)
    ' Z. N- u' [8 C) f5 ]% D  g! U9 Y
  7. >>> math.modf(12.34)
    8 {& E$ `9 `3 `; N" s9 x- s
  8. (0.33999999999999986, 12.0)
复制代码

2 g% _5 W, `' m% P) Zmath.sqrt(x)  求x的平方根) c' X! M5 A. q
  1. #求x的平方根% B( V% U0 Q- ~! {9 W* B
  2. sqrt(x)3 V7 s  ~) t1 N+ s3 h& W
  3. Return the square root of x.1 T& S5 g8 j& c- j& v( `6 `
  4. >>> math.sqrt(100)
    % @, c$ D+ D6 D5 e' t
  5. 10.0
    3 n1 P( k' Q7 V0 [1 B
  6. >>> math.sqrt(16)
    ( k/ Z6 R$ c' q$ S8 W" h
  7. 4.0
    ' c7 q% E# T$ s! n- _
  8. >>> math.sqrt(20)
    $ ~3 u# Q3 r# |2 a8 D
  9. 4.47213595499958
复制代码

( d$ S+ S  t( x' `; B- m; W& qmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分' c  m1 `( k* r; l
  2. trunc(x:Real) -> Integral0 ?" T- s: B2 I$ G/ B7 K  y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    5 i) |: L5 z( z1 ?; M
  4. >>> math.trunc(6.789)  N3 r; S' F+ [5 \1 e: s
  5. 69 F3 y& o: |9 Z
  6. >>> math.trunc(math.pi)! g1 Q4 A5 e5 e' }4 R: Z! L; c/ F4 F
  7. 35 A- L1 |3 m% t8 ~3 Y
  8. >>> math.trunc(2.567)
    ! C4 D; }: W* p4 f1 V+ ~' E9 S
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-21 11:06 , Processed in 0.092782 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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