新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

$ T* W2 @$ _: a  {* s" I' |【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ @8 }) d  W. q$ n" i

  O" I6 P6 T* Q8 n+ G, z8 \& F  f方法1
' E1 f6 G) S) D8 f3 p
  1. >>> import math5 l9 ^6 w4 d' u( q/ Y
  2. >>> math.sqrt(9)4 D) b& L) W/ r) @5 e) k
  3. 3.0
复制代码
方法2
' v8 _; o  [& U' ?* ~1 u5 ^
  1. >>> from math import sqrt9 _+ {+ Z7 W# O1 I4 x6 u
  2. >>> sqrt(9)
    " T0 w0 W, X2 u% G' n' G
  3. 3.0
复制代码

% |- D& \: U! P' R" d. P
6 W; _" F9 |3 y9 W
math.e  表示一个常量! e3 A3 L7 W' l: g1 \9 s
  1. #表示一个常量+ p  s/ w. q/ F3 d( w4 G/ D! w
  2. >>> math.e+ ?0 T* _* g% U( \2 K
  3. 2.718281828459045
复制代码

& a/ q, y' j, `. jmath.pi  
数字常量,圆周率
/ I! {; a0 M& c
  1. #数字常量,圆周率; [4 g- ^' N+ L1 Q0 T1 j
  2. >>> print(math.pi)2 f; \8 g3 I; f4 o1 s9 Y
  3. 3.141592653589793
复制代码

' T9 y8 T/ |( vmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

, c& p2 Q' s* j% f2 g7 D
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    & u8 O4 {6 [+ n
  2. ceil(x)0 {, _: f( T4 s) R* [: i" |
  3. Return the ceiling of x as an int.
    4 \$ P# _+ i, j7 F" z" \1 M; q- K
  4. This is the smallest integral value >= x./ X% n5 o- D2 B. `6 S0 o' L
  5. 7 \" s) @% x3 q9 C
  6. >>> math.ceil(4.01)
    & M0 p$ J' H1 D" O
  7. 52 \+ p+ S4 d) S1 S% \2 R( B1 W
  8. >>> math.ceil(4.99)
    ! H: L( I: i2 ?, s  l/ Q
  9. 55 u$ u5 C: X( {6 d  t1 m
  10. >>> math.ceil(-3.99)0 c: s2 ]$ G" C- w" a6 u- |( O3 B
  11. -3
    2 `* U+ M8 M, w5 H1 d
  12. >>> math.ceil(-3.01)
    ) h) d9 I+ E8 L' g: Y
  13. -3
复制代码

3 I$ w$ `6 L0 z2 m- k1 hmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& F% N$ m$ }2 @6 r
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身; c6 N! N0 A# l/ I! V4 E
  2. floor(x)/ ~6 J; V% E  h1 y! Z2 c
  3. Return the floor of x as an int.( v' o/ G; w- j* B
  4. This is the largest integral value <= x.
    7 z0 X# l9 p- `; {8 R' a- [' S5 Z4 Q
  5. >>> math.floor(4.1)
      p2 c9 ^+ U# e. l+ J  t
  6. 4
    # W+ L* |( o: C/ v0 s5 m( _
  7. >>> math.floor(4.999)
    4 ?) s* R. r$ g2 A# m. R& X5 U
  8. 4
    7 ]# m  b, Q8 F% ^
  9. >>> math.floor(-4.999)
    6 e1 x  N( Q; p, T' v# j
  10. -57 D8 ~/ k5 h2 U! w
  11. >>> math.floor(-4.01): @. {  B, i- ^9 g
  12. -5
复制代码
9 l8 c- G- ?5 {+ }
math.pow(x,y)  返回x的y次方,即x**y) x- t4 z# Y! F: n$ T. }
  1. #返回x的y次方,即x**y
    ! E- f& E  ^- h% r4 s; u1 Q
  2. pow(x, y)
      c0 j! i" X) u
  3. Return x**y (x to the power of y).0 V' |7 u3 S4 O. @2 L# B* r' f5 S3 w
  4. >>> math.pow(3,4)
    ) W7 E: v9 X5 q4 G; ^$ h) P8 P
  5. 81.0% Z- j! E! ~" w/ X! i
  6. >>>
    , M/ u$ D' ]' e- }! C4 a$ L7 \4 I
  7. >>> math.pow(2,7)% t( s0 J* U* v+ h+ I. S  _5 _: `
  8. 128.0
复制代码
# ~! E  S$ z7 ?+ q0 i6 c
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 y+ ^( w* L/ v/ K
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    4 h( X6 B: _- R0 q% p
  2. log(x[, base])
    5 d# E5 e: D  K2 K3 X+ ~: g6 R
  3. Return the logarithm of x to the given base.
    " S% ]2 i; h" L; X9 V% d) @
  4. If the base not specified, returns the natural logarithm (base e) of x.* ~% c; x7 M- {! w! F# u! g6 i
  5. >>> math.log(10)/ ^5 a+ s6 b+ ?1 _8 k# }
  6. 2.3025850929940464 \$ a. _) J# U4 ?3 h
  7. >>> math.log(11)
    " c5 K0 }8 |& K% L
  8. 2.3978952727983707
    1 s  Q" c6 l  i
  9. >>> math.log(20)# w3 {* }9 T* ]& N( f6 m3 D4 w3 _2 V0 l% e
  10. 2.995732273553991
复制代码
# d; P2 P, w4 p
math.sin(x)  求x(x为弧度)的正弦值+ V9 m4 Q3 _; N: Y5 q1 A3 @
  1. #求x(x为弧度)的正弦值
    : w9 E' p8 h& r2 r) p6 C
  2. sin(x)
    ) T+ u& c  @) c! Y$ D; ^
  3. Return the sine of x (measured in radians).
      ]( t! z, u) v7 n
  4. >>> math.sin(math.pi/4)
      d3 \8 q0 }) e( H
  5. 0.7071067811865475
    , s2 B7 w2 L: Q  Z
  6. >>> math.sin(math.pi/2)/ _1 w0 T  \" o9 Z
  7. 1.0- U' Y9 @6 e3 N6 H+ t; `" G
  8. >>> math.sin(math.pi/3)
      Z3 ~# P, e+ ~
  9. 0.8660254037844386
复制代码

2 C4 v  L. _; G2 C! Dmath.cos(x)  求x的余弦,x必须是弧度
5 Z( }' H, v1 Q/ C) _' C9 B. C, l6 e
  1. #求x的余弦,x必须是弧度+ w1 Y  X/ x" h" d/ O; `% p' u( S
  2. cos(x)
    : V" ?* C  w* [0 f1 g2 w2 C
  3. Return the cosine of x (measured in radians).
    2 Q1 P* l  X  R1 w( _3 O
  4. #math.pi/4表示弧度,转换成角度为45度4 C- m9 h0 f1 Z! X
  5. >>> math.cos(math.pi/4)* o' Z* t1 \1 Q% ]: o  q
  6. 0.70710678118654768 b. {- U: ~+ m( O5 ^8 m
  7. math.pi/3表示弧度,转换成角度为60度
    " {7 ~% _: Y2 `+ w# ^, n, a! s; S7 h1 L, E
  8. >>> math.cos(math.pi/3)
    6 d. q6 q' X  i$ w; Q
  9. 0.50000000000000011 B5 h: @7 ~9 _
  10. math.pi/6表示弧度,转换成角度为30度
    ) e* N2 f- I5 ?
  11. >>> math.cos(math.pi/6)
    ) m  n2 v2 C2 M" ]3 i
  12. 0.8660254037844387
复制代码
! B  M4 g; Y7 A3 L
math.tan(x)  返回x(x为弧度)的正切值% c4 W9 ~8 L5 d4 ^  v- ^" C
  1. #返回x(x为弧度)的正切值" D- _7 R& u2 t. a
  2. tan(x), _( }: B) N- o2 _, I& Z/ Y
  3. Return the tangent of x (measured in radians).
    9 n# Q8 D2 X: ]+ `. m
  4. >>> math.tan(math.pi/4)+ A4 R$ `7 e  e/ R
  5. 0.99999999999999996 d8 d" h- `, Q% n" v+ y
  6. >>> math.tan(math.pi/6)/ B! f7 p9 I7 h. O6 l% F8 M
  7. 0.5773502691896257
    1 z  Q' l) O' P; c* f" Y
  8. >>> math.tan(math.pi/3)
    , T5 W! N6 y, a% E  g$ }2 X+ S
  9. 1.7320508075688767
复制代码
' G7 B6 G& R. Y* H1 L* I
math.degrees(x)  把x从弧度转换成角度
8 u7 z: f7 h% P7 v! E7 Y- G
  1. #把x从弧度转换成角度7 L; v2 A' D) {1 {. K
  2. degrees(x)( g7 U3 T; J2 ?: _. a
  3. Convert angle x from radians to degrees.# g$ {) K+ v; k9 ~

  4.   Y% c2 w  M8 d% m- U
  5. >>> math.degrees(math.pi/4)  B1 \5 `1 }3 ^% k9 q* E7 o
  6. 45.0
    - j: l7 D4 {% X; `
  7. >>> math.degrees(math.pi)8 @2 f% q. @6 m
  8. 180.0
    2 d% N* ]2 f. d6 }
  9. >>> math.degrees(math.pi/6)+ D% A% R6 l' L5 ]) k. p3 t. ~% k
  10. 29.9999999999999969 S0 ~" e2 ]8 Q' T' H$ S9 K8 H8 O$ w3 Z
  11. >>> math.degrees(math.pi/3)8 ~4 L8 c0 T3 b, H  y3 b% R0 ^+ o* m& l
  12. 59.99999999999999
复制代码
- X* y* o# T* y6 U$ `4 E
math.radians(x)  把角度x转换成弧度
4 Z  `) J. p% f* k9 I
  1. #把角度x转换成弧度
    7 \$ F% `+ S7 H+ c3 }6 ~+ \" _
  2. radians(x)
    3 R6 J3 }3 J. D( J: |4 o7 m0 C7 S* A
  3. Convert angle x from degrees to radians.
    0 Y( U1 K( Y8 n' N5 U
  4. >>> math.radians(45)! g: q: C1 D/ O* o) n8 V8 e
  5. 0.7853981633974483
    ( Z  }/ ]' a3 Y. ^' Q0 ]
  6. >>> math.radians(60)
    % y4 F4 J& J( k- ]9 ?
  7. 1.0471975511965976
复制代码
) j8 l, `; `1 s
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
+ K% d7 u: A. O% r" E
  1. #把y的正负号加到x前面,可以使用08 d3 g3 _9 p! g8 A# l
  2. copysign(x, y)) k5 L( A( l) z+ `+ h, K7 }! M+ D
  3. Return a float with the magnitude (absolute value) of x but the sign
    2 w  Y  g1 S" Z7 R; a, ]
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' ~0 y8 i* n$ g# T
  5. returns -1.0.
    3 K; R- u8 S$ h

  6. 6 z4 ~5 Y6 a8 ~3 f$ r3 ^4 p8 ^
  7. >>> math.copysign(2,3)% p1 X1 f8 a# Y+ q1 G
  8. 2.0
      b8 \% L( B( i
  9. >>> math.copysign(2,-3)
    ! X; W7 L, K8 ^9 A
  10. -2.0
    + Q1 b+ L% q- l3 ^( f
  11. >>> math.copysign(3,8)
    : H7 B) g& D& B
  12. 3.0
    & {, L2 W: B0 r& `1 L6 [
  13. >>> math.copysign(3,-8)+ N, _6 x# r. P7 V* i/ b8 Y
  14. -3.0
复制代码

& Z$ d' q; Q* H0 @4 M; n) {' Qmath.exp(x)  返回math.e,也就是2.71828的x次方' p3 c% ^1 ^7 j& }/ x# k
  1. #返回math.e,也就是2.71828的x次方
    8 S  C8 \6 S2 I9 b
  2. exp(x)
    5 M! s0 U0 I+ @0 f+ q& h
  3. Return e raised to the power of x.( j8 ^6 c/ r5 Q; q" f3 \. g

  4. * ]0 e3 r  r7 X4 s& L
  5. >>> math.exp(1)
    / b  w/ K% g6 W  j7 U. ]( o
  6. 2.718281828459045
    # |2 [! k: s# H; z
  7. >>> math.exp(2)
    ' X3 o* q6 b+ D
  8. 7.38905609893065
    0 f0 g' y( B" I! x0 O
  9. >>> math.exp(3)8 G" `, [; @& V; @
  10. 20.085536923187668
复制代码

8 @, z' g4 j- ?math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1: o4 ?; ?' P4 ^4 w
  1. #返回math.e的x(其值为2.71828)次方的值减16 g2 J& T& {1 f- f
  2. expm1(x)
    7 }$ f+ I( r; L1 ]5 _  [* w+ w4 l
  3. Return exp(x)-1.. b; H- A* f8 i1 i
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 n4 l# V9 E8 D
  5. * Z  F; n9 p5 A  p1 S% m
  6. >>> math.expm1(1)
    + q6 ?# u0 t0 i/ c( [  N
  7. 1.718281828459045
    4 p7 l/ U7 N: \
  8. >>> math.expm1(2)7 I$ F' ]+ ^) }1 ^
  9. 6.38905609893065
    " J* W) K: _* Q/ J6 f1 q
  10. >>> math.expm1(3); B, c- H" r) y! c& y0 N6 n, e& N
  11. 19.085536923187668
复制代码

0 _" @/ _4 ~; S/ Nmath.fabs(x)  返回x的绝对值; q1 K% j; V$ O' A" V
  1. #返回x的绝对值
    , C5 Z- }# O7 s0 J3 f* {
  2. fabs(x)
    . ^: s0 ]9 c' I1 j8 D) F
  3. Return the absolute value of the float x.
    - i8 ?+ N9 s( y- h& J0 C( p" ?  Q

  4. * @0 S- e7 _! A5 K  }% o
  5. >>> math.fabs(-0.003)
    3 L  P* [, X4 t3 H7 P7 g7 f) k$ u
  6. 0.0034 E- p# B7 C! B
  7. >>> math.fabs(-110)0 `& ]# e; n: \- R& w
  8. 110.0, ~6 _: f' F% S  N; u
  9. >>> math.fabs(100)
    . F' H' f- w4 b! x
  10. 100.0
复制代码

, ]1 Z* g7 Q( O7 V# Mmath.factorial(x)  取x的阶乘的值
+ c' J: j5 S4 ^2 \5 l
  1. #取x的阶乘的值
    6 u0 w) _2 _; G: G$ t
  2. factorial(x) -> Integral, a( G: D4 p1 p
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    $ Y+ D2 v- X& E& t' X
  4. >>> math.factorial(1)! v6 L  a$ \. U- u1 l! ?& J
  5. 17 }$ [2 f; U# y" O7 P: ~" B* I
  6. >>> math.factorial(2)
    6 ]2 A  z# Y, N+ w
  7. 2
    : W9 o0 I" w/ ~. M' m! K
  8. >>> math.factorial(3)% O% }- u4 E! H. i( T3 s
  9. 6/ W. G& _" x& U4 a, |
  10. >>> math.factorial(5)
    / _& g; {! v* b9 G5 }
  11. 120( X2 s4 \+ M7 L1 q- h
  12. >>> math.factorial(10)
    0 y9 w* M) s3 R: z6 e
  13. 3628800
复制代码

  j* z- }! U7 K" ymath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
! S; T( ]/ z4 Q( E& G& o/ I. n
  1. #得到x/y的余数,其值是一个浮点数) B. i; @! g  r. J3 o
  2. fmod(x, y)" Q  X8 @$ S2 S$ X' K8 v; N
  3. Return fmod(x, y), according to platform C.  x % y may differ., f) Z6 B+ C# x8 L$ D3 u* h% l3 U
  4. >>> math.fmod(20,3)# m5 h1 |( w- C' g# I% {
  5. 2.0. b, V7 m* e4 @! X" S
  6. >>> math.fmod(20,7)
    0 v& L) r* M; z, i  ]
  7. 6.0
复制代码
6 a9 O, d* J5 h+ W$ T, `
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
, E+ Z3 U6 u' k. q% Z- g1 i
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    / @( `4 C! Q% @1 Q
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 v7 @, E1 L2 |9 C+ A: ~( i
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1  ^4 C. v. E, _) y8 P7 d
  4. frexp(x)
    " f2 L. w# O6 ?" E# o* I
  5. Return the mantissa and exponent of x, as pair (m, e).
    : l( e* w- i, d! Q% S
  6. m is a float and e is an int, such that x = m * 2.**e., N) C9 \9 t8 `& [1 i7 Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.$ t/ x. X9 B, b1 E8 ^9 k: y, G
  8. >>> math.frexp(10)
    & H4 c, j$ E( R0 N: f/ j  |
  9. (0.625, 4)* l5 @  V0 E  m- j5 w; p
  10. >>> math.frexp(75)/ `: l- o& S4 S+ q" h+ d) {
  11. (0.5859375, 7)
    # h, Y+ E& c; t, ~" O6 R
  12. >>> math.frexp(-40)
    0 P$ }/ [$ e/ Y# K- P$ e
  13. (-0.625, 6)! G$ T; L, l# f8 H$ V  Z, a, y  ^
  14. >>> math.frexp(-100)% s& b9 j" U, R2 Q
  15. (-0.78125, 7)" h/ B$ U! p# l% W9 N( s* `4 v
  16. >>> math.frexp(100)
    $ N1 K5 k$ i8 y# v7 y
  17. (0.78125, 7)
复制代码

1 O( n9 w( A7 V# Emath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列3 A" W2 _, j& Z. [
  1. #对迭代器里的每个元素进行求和操作
    " C  V  d( d/ q/ ~
  2. fsum(iterable)
    1 M+ n* q$ t* N6 G3 {
  3. Return an accurate floating point sum of values in the iterable.
    " b% ~! }& @5 d
  4. Assumes IEEE-754 floating point arithmetic.
    0 ]9 E' K; @; z* r- m
  5. >>> math.fsum([1,2,3,4])
    0 J, K0 C. ^$ K% _+ M" x2 y, F
  6. 10.0$ y' Q" u7 E0 U2 [- x
  7. >>> math.fsum((1,2,3,4))
    ) [: q8 H' x* a8 M. g' s6 D
  8. 10.0
    7 h2 S# d8 }' P* w, r/ ^3 J
  9. >>> math.fsum((-1,-2,-3,-4))
    3 n& Q9 L* |6 |) ?' ?5 {5 u
  10. -10.0  i7 D( n+ [! g2 {! Y; B
  11. >>> math.fsum([-1,-2,-3,-4])) L! b% _( F2 [1 ]
  12. -10.0
复制代码
5 I0 k8 C2 P& `' w- v
math.gcd(x,y)  返回x和y的最大公约数
8 W: g' i- U" }1 [6 g/ `
  1. #返回x和y的最大公约数
    0 d# g  c8 x' h
  2. gcd(x, y) -> int2 u9 h8 _) C$ d. K0 t; [' |% `+ N/ c
  3. greatest common divisor of x and y
    + s' ~- ~; ]" H
  4. >>> math.gcd(8,6)) z, I( U' t: w/ \+ P( {
  5. 2
    " S* q4 m% n$ R$ _- `/ M
  6. >>> math.gcd(40,20)
    7 ?( W) J1 P9 o2 f& |2 v
  7. 20  r- d# t2 {' w: F+ h0 }4 V) z# j
  8. >>> math.gcd(8,12)
    + @7 {, j5 v. t; L) A+ C; u/ O7 U
  9. 4
复制代码

1 l1 n3 r+ ], lmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
# J9 l) P6 _) X) G4 ^- Z% F$ @
  1. #得到(x**2+y**2),平方的值! y% j/ U5 A+ }. e* F1 X
  2. hypot(x, y)
    ! l, M- S# u3 b
  3. Return the Euclidean distance, sqrt(x*x + y*y).1 w0 Z, {! s. ?1 r* M2 t( v" J3 a
  4. >>> math.hypot(3,4), z; f( V0 E( i% k0 }; J6 a: d
  5. 5.0) ], e9 q0 L# r
  6. >>> math.hypot(6,8)- h' U, Y3 R$ z. y
  7. 10.0
复制代码

9 J  ?- I5 {: C4 a* f) I4 imath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 r/ u" j# B; a# P0 O8 W# V( D& A
  1. #如果x是不是无穷大的数字,则返回True,否则返回False; G5 u/ E. l$ O+ {! Z7 I) w
  2. isfinite(x) -> bool
      [8 e# P" G# X. J1 R0 M7 U
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! p4 w# ~8 r! B' Y
  4. >>> math.isfinite(100)" x  v% t. J6 g, i' x' ?+ x
  5. True: t5 m0 y, T8 S0 d/ W/ a
  6. >>> math.isfinite(0)
    8 R+ }' v0 K, A. L
  7. True2 s/ ]6 _7 R' u5 e8 `% b
  8. >>> math.isfinite(0.1)
    $ D8 {& ]# y# y' t: ?. d
  9. True
    1 `5 L9 |0 O7 f$ c5 @' z
  10. >>> math.isfinite("a")6 j+ K* ]/ m) n' q, P
  11. >>> math.isfinite(0.0001)
    2 A7 Q& \& d& S# m* P/ N% g
  12. True
复制代码

9 E2 o  ?# G% d/ B0 B; B# fmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
  z+ j4 e+ {8 c# b: a% i. S
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False8 _: N6 i: M! f- \* t
  2. isinf(x) -> bool7 R9 _% h. E' n1 X
  3. Return True if x is a positive or negative infinity, and False otherwise.
    8 V" }! A) l) F1 W: l
  4. >>> math.isinf(234)7 ^& m+ g$ `. o4 U
  5. False
    / B8 _; D% r7 h2 q
  6. >>> math.isinf(0.1)0 j: I4 _# O% E# v4 j' r) `0 u
  7. False
复制代码

! c6 F( A4 T' {; l. imath.isnan(x)  如果x不是数字True,否则返回False9 s3 l# B7 Y  `
  1. #如果x不是数字True,否则返回False: y# a$ [6 {9 P& s% T, N
  2. isnan(x) -> bool9 u0 x& o! z* K6 R' o
  3. Return True if x is a NaN (not a number), and False otherwise.  t2 R) \0 x' t3 K/ `4 ?
  4. >>> math.isnan(23)2 a1 k2 N/ \) x7 l8 r& ]
  5. False
    % \" z$ R4 ^/ ~( L
  6. >>> math.isnan(0.01)
    7 h5 m, `  D( `- q. P  h4 B
  7. False
复制代码

! q8 p) |- O! ]6 _math.ldexp(x,i)  返回x*(2**i)的值
6 K8 i- m/ z! f) c2 G
  1. #返回x*(2**i)的值
    * v4 d; ^& @* _4 D# X! E5 p
  2. ldexp(x, i); B2 D- E9 S! ?+ Y( K7 o
  3. Return x * (2**i).
    1 U6 F/ |1 f+ ^
  4. >>> math.ldexp(5,5)5 u% F% H. M: A8 M1 |; g$ K
  5. 160.0; O, G, B1 }/ P0 h" H8 ?5 \$ B! Q
  6. >>> math.ldexp(3,5)! e, Y3 N% u+ c4 y: _
  7. 96.0
复制代码
# _) {% ^5 D9 Q4 Y" c2 Q- f
math.log10(x)  返回x的以10为底的对数5 U# F  P2 O4 _1 g. N& s
  1. #返回x的以10为底的对数7 F+ s+ C/ R+ n, e  h0 @/ o8 z+ H0 X
  2. log10(x)9 H4 j4 X1 `# P  P0 s
  3. Return the base 10 logarithm of x.
    % j% v9 Q- w3 O% F- W9 c* V: F" f
  4. >>> math.log10(10)1 M! i6 r2 s; I3 b1 N
  5. 1.0: O/ {9 h6 i& d1 n  S* R- m* \
  6. >>> math.log10(100)
    1 X* I# b$ O- d
  7. 2.01 M" {; [4 S4 \& {  b2 Q$ b5 f
  8. #即10的1.3次方的结果为20
    ) E* F) T. M  ^4 \/ e
  9. >>> math.log10(20)7 N% w! _/ d, ~9 P2 n
  10. 1.3010299956639813
复制代码
' G# Q2 I; i. a4 b3 U0 F
math.log1p(x)  返回x+1的自然对数(基数为e)的值
* x4 @) v: z+ c$ J
  1. #返回x+1的自然对数(基数为e)的值& l8 V8 i& K. U: |! @- L' Q( O
  2. log1p(x): |' b7 g; i$ q) H/ P1 b% J
  3. Return the natural logarithm of 1+x (base e).8 Y3 N- t# u: ?$ O& k
  4. The result is computed in a way which is accurate for x near zero.% t0 U& t& O5 \/ j2 k2 W
  5. >>> math.log(10)
    6 \% c5 d* Q' X; h* Y
  6. 2.302585092994046
    " ^% u- b4 _% o9 u: @/ @( y8 h; |
  7. >>> math.log1p(10)  S5 y% b& {0 ?: y9 p3 I. O# N
  8. 2.39789527279837079 V! p8 _4 E. G' [7 l2 j
  9. >>> math.log(11)
    & i6 g( d6 V; E% ^
  10. 2.3978952727983707
复制代码
; D6 [/ g3 W% I' q+ A2 O1 R" q  V/ Z
math.log2(x)  返回x的基2对数5 y  S$ L% {% D
  1. #返回x的基2对数. L$ Y) j8 M: A9 Y8 F) N. g
  2. log2(x)8 `# z, F: \3 c$ b  j2 @$ B* A( g
  3. Return the base 2 logarithm of x.
    9 \& @# K: b) p1 O, [
  4. >>> math.log2(32)
    * X: D8 d1 u5 A% E
  5. 5.07 l3 G; J' ]8 q
  6. >>> math.log2(20)2 v' @& O5 n  A
  7. 4.321928094887363
    * Z, k0 G1 |3 _$ G; _& h' E
  8. >>> math.log2(16)
    & l/ W4 C: e# |) u
  9. 4.0
复制代码
1 a; W! O4 E& y/ \0 r0 `
math.modf(x)  返回由x的小数部分和整数部分组成的元组( S, q. y) ?: w
  1. #返回由x的小数部分和整数部分组成的元组
    5 C! A2 C) S( ^: l: ?' q
  2. modf(x)) h1 V/ W( `6 j$ e3 s3 t
  3. Return the fractional and integer parts of x.  Both results carry the sign
    * E3 R6 _) s' j" `
  4. of x and are floats." e- c9 b3 ]( o( r1 g; Q7 c/ y4 W
  5. >>> math.modf(math.pi)
    % a) g8 `$ c# W
  6. (0.14159265358979312, 3.0)
    + e  }; c( g) K  V% f( ]5 p
  7. >>> math.modf(12.34)' F) G1 h+ u+ K
  8. (0.33999999999999986, 12.0)
复制代码

6 Y; d; X' _- K9 v2 _! S# wmath.sqrt(x)  求x的平方根" m; Y4 K- ]/ [; C9 U# Q
  1. #求x的平方根9 D, H8 ]  a( ?1 G  V/ V8 g
  2. sqrt(x). ]+ v7 _* m# g4 [- C& ]: @# O, G
  3. Return the square root of x.0 f8 V  e% A% q) [7 J( J5 V; t
  4. >>> math.sqrt(100)
    0 i8 m) |5 P! m" T; O9 c
  5. 10.0
    8 r& z; ?, V" B+ u
  6. >>> math.sqrt(16)
    4 n$ h0 d2 o9 r! t/ R4 }5 Z" W4 H
  7. 4.0
    5 Q' N- a& K7 l
  8. >>> math.sqrt(20)
    + {9 t% c5 l& h& }& R5 `  @
  9. 4.47213595499958
复制代码
. q2 a- k; }" `; i. Y( e' x: K' I
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分2 W; _  H; H+ j0 O/ }; D8 ]
  2. trunc(x:Real) -> Integral3 k( o: R4 [1 ~
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! K: E/ @/ j8 ]/ t1 U! P4 v8 _
  4. >>> math.trunc(6.789)5 d. |) T4 @8 {
  5. 6
    2 _# h) L( P1 n9 k1 H( a: _
  6. >>> math.trunc(math.pi)
    : B! }  ?0 e' S/ Q5 J7 K6 O% C4 |
  7. 37 [7 _% f, @9 j1 S  U- a
  8. >>> math.trunc(2.567)
    * V9 s4 m/ o% ?3 _
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-24 18:08 , Processed in 0.089360 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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