新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
" A& N. W* J3 F* T- J  N: t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- U& W( G; L3 k% |+ R

5 r6 |$ `  ~# X$ {- m6 K( Z方法1
5 |0 v' {$ A1 j3 c  s
  1. >>> import math
    0 V- k# Y# k8 \1 F
  2. >>> math.sqrt(9)
    - S" S5 L# N! F; s% i% k7 M
  3. 3.0
复制代码
方法2
$ H) X+ k+ \9 I4 a* {7 ~
  1. >>> from math import sqrt0 p7 S' ~' q/ [
  2. >>> sqrt(9)
    " q2 [5 i* E) Y+ w; x. X
  3. 3.0
复制代码

# K' F" L. l$ A

8 P' e$ z+ v/ Z8 _6 |1 q
math.e  表示一个常量* b; ^9 h1 M+ y% j  }3 X% Y8 r3 _
  1. #表示一个常量8 }" Y* f1 m$ b' Y3 a
  2. >>> math.e# N& Y# [9 ], z, Z
  3. 2.718281828459045
复制代码
9 r1 h  ~5 B3 y8 r
math.pi  
数字常量,圆周率

/ s, w, D1 S: h
  1. #数字常量,圆周率
    ( `) P2 k; T. u. I
  2. >>> print(math.pi)
    7 {6 ], F$ Y! Q; ?; B+ S
  3. 3.141592653589793
复制代码

2 [8 K" d) T* `, s# y0 ymath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

4 [: X9 m8 n% F1 b" s
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, B9 D9 W0 Q; o" N! y4 A0 n
  2. ceil(x)
    - j3 B+ c: D9 V4 h2 \4 U
  3. Return the ceiling of x as an int.: g) F) P" K2 Z' Y& `
  4. This is the smallest integral value >= x.
    * K  \2 m9 n: b0 h: F
  5. + N! ]' e# ~9 u5 N* y- [
  6. >>> math.ceil(4.01)
    : [& B. d% ^8 u: z
  7. 5
    . C, U8 h- M2 i
  8. >>> math.ceil(4.99), @" L0 F( k7 K: _. b
  9. 5( M+ a: I7 v9 `1 h; r( D; C! i; U
  10. >>> math.ceil(-3.99)
    ( S" s" l7 D! F$ o$ Y! P
  11. -3
    4 f8 }5 {+ d7 C& Y
  12. >>> math.ceil(-3.01)
    0 x3 x+ H5 w, `& K0 O' z
  13. -3
复制代码

  a, D+ b4 _6 l. V3 R; i5 Nmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 z0 g% Y* L3 T4 E( v/ k
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 W) r; @: G, g: o8 g" ^7 s
  2. floor(x)6 p; f7 v$ k, T" w9 U
  3. Return the floor of x as an int.0 O7 r5 D0 |1 h. R2 u: M. y( k
  4. This is the largest integral value <= x.$ e5 ~: U; j9 R
  5. >>> math.floor(4.1)4 g. G7 X: x- U- k3 Y* u3 b8 c( ^
  6. 4' U9 s% ~2 ?4 f6 G3 V3 i: U
  7. >>> math.floor(4.999): V- n0 G# r/ G  W1 n4 Q) g
  8. 4
    . a/ g' @; M# u: e& C* _9 s+ P$ w  e
  9. >>> math.floor(-4.999)/ c* Y8 c$ Z! I& b8 G# x+ ?
  10. -54 |& I, X" h/ Z( y  y+ l1 N; y
  11. >>> math.floor(-4.01). q* r+ w4 J# ~; J
  12. -5
复制代码

* s- n" s' E( [- }4 H3 p4 A# Y$ Hmath.pow(x,y)  返回x的y次方,即x**y
4 f! Y$ ^7 X2 H4 U0 `, T
  1. #返回x的y次方,即x**y
    9 n2 n; P5 p8 `7 P$ h# L
  2. pow(x, y)
      O! v7 F  Z8 L- Y
  3. Return x**y (x to the power of y).
    % F8 F( p* s! m
  4. >>> math.pow(3,4)
    8 }. z8 D  c" X; w8 a: }; Y
  5. 81.0# _8 |6 N+ T- E; n$ i, j
  6. >>> 8 F1 S! K; ?2 c/ p
  7. >>> math.pow(2,7); n$ f& t: O+ a0 x
  8. 128.0
复制代码
: S& H6 ~8 v0 x
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) m0 h" c% M+ a% W# |, q
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    & B- A* g0 M* s5 L/ R" @
  2. log(x[, base])6 s* o2 t& J# V2 i" i7 s8 o
  3. Return the logarithm of x to the given base.
    * X' r0 K' b; |- N1 ^, \
  4. If the base not specified, returns the natural logarithm (base e) of x.
    8 v3 J7 E1 F# L' ]0 A. z
  5. >>> math.log(10), ]0 N8 j# A  J- z
  6. 2.3025850929940465 g: R9 `2 h( g' X" p( I
  7. >>> math.log(11), y. Q+ W* J$ _
  8. 2.3978952727983707
    ' p/ n# u' |9 K$ d% F  J$ {
  9. >>> math.log(20)
    1 B6 g$ A  G" L. |; A$ y9 y* A
  10. 2.995732273553991
复制代码
6 @+ I4 r* R0 n! B2 l  X! e
math.sin(x)  求x(x为弧度)的正弦值
% k3 x% X  w! W; X4 x# b4 E1 n5 |
  1. #求x(x为弧度)的正弦值
    % z* g0 Q) y! B& s7 k
  2. sin(x)6 W/ {& [2 F- T! {1 u
  3. Return the sine of x (measured in radians).' x4 n# N8 c2 S/ H; l. }
  4. >>> math.sin(math.pi/4)& A8 D; i; c7 q* U% O9 P
  5. 0.7071067811865475% @" N6 U5 [/ G- X4 G
  6. >>> math.sin(math.pi/2)* b5 u+ O% V% |0 m2 k+ s# X9 J
  7. 1.0
    4 K! R( u4 k2 V4 Q' J. [, Y
  8. >>> math.sin(math.pi/3)' N( \0 R! O3 ~  c9 ~6 f! F$ n
  9. 0.8660254037844386
复制代码

7 L! r) X" S2 F9 C) x/ T6 Q5 wmath.cos(x)  求x的余弦,x必须是弧度
7 U7 E! Q$ \: u6 r$ \; U
  1. #求x的余弦,x必须是弧度
    : z. R2 H3 s. S) b: {% `. c
  2. cos(x)
    , u0 x$ V. ^: T- q4 {) G5 J+ T2 b
  3. Return the cosine of x (measured in radians).
    ! t! u$ d2 w- l+ ?
  4. #math.pi/4表示弧度,转换成角度为45度% R# R  r7 W0 }3 y5 X
  5. >>> math.cos(math.pi/4)" I- B* p5 b% P
  6. 0.7071067811865476" Q! c3 Q5 H3 z, C
  7. math.pi/3表示弧度,转换成角度为60度% D4 q/ r  P, e# i
  8. >>> math.cos(math.pi/3)
    + V* G, ^5 X1 O
  9. 0.5000000000000001
    # V8 z1 Z( ^) ?6 J6 k
  10. math.pi/6表示弧度,转换成角度为30度
    , o, M$ n/ J4 s$ G
  11. >>> math.cos(math.pi/6)
    $ E5 Q. k) W6 Y% z2 C3 M
  12. 0.8660254037844387
复制代码
" f0 @) ^! C6 l& j. X
math.tan(x)  返回x(x为弧度)的正切值
! @' N: M1 V& I
  1. #返回x(x为弧度)的正切值
    ( W& }, F$ v2 z4 V3 J" _
  2. tan(x)
    7 e4 P" O4 y* G& O) \1 V
  3. Return the tangent of x (measured in radians).
    + W) Y8 n9 h) [" p1 }& r, o3 q8 r) r
  4. >>> math.tan(math.pi/4)
    ; x- }/ ]9 p3 q- N
  5. 0.9999999999999999
    ( j% R. \0 Z" E( w* |
  6. >>> math.tan(math.pi/6)
    1 K, @; R* t6 Q& D2 `
  7. 0.5773502691896257
    3 F' d; d. B# E
  8. >>> math.tan(math.pi/3): o3 T6 f* P* ~- `8 F
  9. 1.7320508075688767
复制代码
7 [9 E7 T* ?' Y( b
math.degrees(x)  把x从弧度转换成角度8 a- y/ K: W+ \) F7 o  Q
  1. #把x从弧度转换成角度
    1 r% w9 K; p) ]  T# r4 A. R. `+ o8 g0 @
  2. degrees(x)
    0 ]/ ]9 Q. V5 d
  3. Convert angle x from radians to degrees." B8 Z' _4 P6 W1 \/ x5 z
  4. . c8 \3 ]8 L9 I/ J5 i  k
  5. >>> math.degrees(math.pi/4)
    # a8 V$ }% u6 p& x5 b& l3 J) T" a4 x
  6. 45.0
    * @' w0 d! e- e! m
  7. >>> math.degrees(math.pi)1 S( L4 v& o7 C5 r6 d4 o8 L( l% U
  8. 180.0
    1 j! O3 T4 x# o0 p  @* ~% g
  9. >>> math.degrees(math.pi/6)
    ) Z/ |) c* n( k: `. Q+ i: l) y
  10. 29.999999999999996
    & u$ ^- b" t1 {' v0 E& u/ I
  11. >>> math.degrees(math.pi/3)
    9 R- P3 d: a9 w6 Q
  12. 59.99999999999999
复制代码
( a% D& w* [/ T- e
math.radians(x)  把角度x转换成弧度: ^$ @8 Q/ J$ i% h$ E; T$ o: t8 `
  1. #把角度x转换成弧度( E  [) o; Z* ^# g6 v
  2. radians(x)- n$ h, [5 T* T6 U9 x7 b% n1 u
  3. Convert angle x from degrees to radians.
    % g6 C  w$ ]( R: f' P+ g* a5 M
  4. >>> math.radians(45)
    % h, R( B. l8 J% t" R/ U
  5. 0.7853981633974483- n3 l# D* Z# m+ A3 ^
  6. >>> math.radians(60)
    5 d* t! W( l7 {3 v, `
  7. 1.0471975511965976
复制代码

3 e3 h1 S0 M% W) F5 {# J4 \" _( ?( zmath.copysign(x,y)  把y的正负号加到x前面,可以使用0: q+ x# V8 W7 ]5 h5 H' d6 L
  1. #把y的正负号加到x前面,可以使用0- o; a8 L8 Z7 o: b! l& Q% {
  2. copysign(x, y)
    : ?) ], ~8 U2 a$ Z+ H. c
  3. Return a float with the magnitude (absolute value) of x but the sign
    5 d" [  G* l4 o/ E1 o& u
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    + H6 `/ z+ y0 I# a* g
  5. returns -1.0.
    ) [$ Z0 N# `; _; B9 p+ P: a  z

  6. * A( U" |/ W- O; W
  7. >>> math.copysign(2,3)
    8 J9 Y, V: h: \% J6 T
  8. 2.0$ ~2 _1 T/ U/ ]  p" v- L0 O1 r
  9. >>> math.copysign(2,-3)8 S+ E0 }. _: |0 O/ W& z4 D( [
  10. -2.0
    $ |0 B6 t* V$ N$ Y" X! M9 U, ?3 j, z
  11. >>> math.copysign(3,8)& z7 w1 H% Z. j  M
  12. 3.0
    * m7 R- G9 w# p2 w; q
  13. >>> math.copysign(3,-8)! Y1 k2 f* f2 `. ~3 W% W3 k* ~
  14. -3.0
复制代码
( W) `" ]% f& V2 I5 S/ t; g( [" y
math.exp(x)  返回math.e,也就是2.71828的x次方
4 Y$ v* m/ G' L+ K; P$ d
  1. #返回math.e,也就是2.71828的x次方4 {/ s8 g" w3 g( `1 H6 g" F' D, M
  2. exp(x)# j/ j, _; p  O% Y6 x6 X; X
  3. Return e raised to the power of x.
    . ]3 S4 w. ]. h& N: S
  4. 8 l. `! L4 P, u
  5. >>> math.exp(1)4 X3 n1 v, A3 R; f+ J) v
  6. 2.7182818284590456 ~/ ^. X2 G! K0 M" s' w
  7. >>> math.exp(2)" S5 f! r2 @: G% Q! p& H
  8. 7.389056098930653 p% _$ m! X% P3 x9 L  [
  9. >>> math.exp(3)
    7 w3 m) Y# v3 S7 J' Y$ Y) q7 ^2 A
  10. 20.085536923187668
复制代码
# }4 G% Q/ G) q/ g* n& i  u
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减12 B: G  ^; I% q7 x
  1. #返回math.e的x(其值为2.71828)次方的值减14 r# q6 Z# E; ]3 p6 `5 n
  2. expm1(x)
    0 ^! N0 r3 L# W
  3. Return exp(x)-1.
    2 b. j+ K) r4 C' a( {# `8 D
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.5 O4 ?" O$ C- @6 I

  5. # O; ]) }6 U& u/ `
  6. >>> math.expm1(1)1 K5 L$ m  R, B8 ^
  7. 1.7182818284590450 }& c+ B) q; u. }) L% W: V
  8. >>> math.expm1(2)' }+ \' f% M0 F* T$ r, K, ~/ Q: O
  9. 6.389056098930653 T$ j6 b, y! L" Y  `' a
  10. >>> math.expm1(3)
    $ g! C' Q' ^4 W- T
  11. 19.085536923187668
复制代码

" n. t% J. p5 ~; y; q" w4 Hmath.fabs(x)  返回x的绝对值
* o; R! l5 c6 A! l) H! x
  1. #返回x的绝对值( i) f! m) q9 d5 E3 D8 P" T
  2. fabs(x)
    . G  Z4 h% S: u6 p
  3. Return the absolute value of the float x.8 A# e; V, \2 f% Q3 R
  4. 6 Z; S  J+ C( J* F
  5. >>> math.fabs(-0.003)0 E  w1 Z1 ~& A0 `3 Y
  6. 0.003
    4 C: E2 t) ^: F2 \0 J- H
  7. >>> math.fabs(-110)" u: ]8 j' a$ J- P# U- O
  8. 110.0: ]$ b! b% x! v2 ]
  9. >>> math.fabs(100)
    , B7 e) q: L) w+ `! @
  10. 100.0
复制代码
  U$ @3 |$ Z) R3 e- w; u/ N
math.factorial(x)  取x的阶乘的值/ K/ O3 b& c& a# K1 W. Y1 F' x* b
  1. #取x的阶乘的值* w/ W- @, H/ @( N/ |1 f! K
  2. factorial(x) -> Integral3 m# z- k" \4 C2 o' g+ D
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    7 v# F' l, ?1 H4 Y$ N' L& b6 v
  4. >>> math.factorial(1)( s# K" `* u! ?% I
  5. 1
    & J; B( F: @  b1 |% L$ ?/ u
  6. >>> math.factorial(2)9 s& n4 s( X# U; y# m, A1 f
  7. 2
    ) w$ g3 `7 z% N4 g& A9 b8 V) r
  8. >>> math.factorial(3)
    & D" O( U' O4 D. C3 \0 T+ c7 J9 y
  9. 6  [; t! M6 m% j. o% Z
  10. >>> math.factorial(5)& d% |# y1 D0 V' m
  11. 120
    $ E. c! K/ e8 X8 ]8 Y/ l
  12. >>> math.factorial(10)
    - Y. F' U6 S3 Z0 U
  13. 3628800
复制代码

" r8 y9 F. s" B# pmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数9 E3 s/ X4 x3 y
  1. #得到x/y的余数,其值是一个浮点数+ ^& c$ H, N  Y2 W/ S! ~0 b4 @
  2. fmod(x, y)' E; j9 }% }! l
  3. Return fmod(x, y), according to platform C.  x % y may differ., l) r( a+ k7 D6 j: T  Q4 R+ X
  4. >>> math.fmod(20,3)
    $ Q" j+ h4 g8 {- c
  5. 2.0; e) H: S; S3 ]  y% C5 A( a
  6. >>> math.fmod(20,7)' w. n7 Q: Z. ~5 g: @- W4 p
  7. 6.0
复制代码

7 G" D* Z1 v* k5 x" Tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% H0 w" B! d1 u, }9 ]$ C
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,0 N3 h3 Q7 l+ o. F- n
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值8 h5 ~4 a! D5 ~8 |. U, o( C
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    $ G+ M9 c' F2 `/ L9 K# G0 {
  4. frexp(x)
    % D$ C4 ]; j# M) h% k" f
  5. Return the mantissa and exponent of x, as pair (m, e).
    ! |/ U* g3 W/ h, Q
  6. m is a float and e is an int, such that x = m * 2.**e.7 ]* S2 t% @9 h# [& b
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    $ ?( g: H9 L! l9 K) L5 p" Y
  8. >>> math.frexp(10)
    1 C! D; I6 U/ k
  9. (0.625, 4)
    / }5 _/ S8 {! N, s# Z: h: P* d# d
  10. >>> math.frexp(75)  `* f+ V2 j  R5 l; R. a8 v- ?
  11. (0.5859375, 7)  e7 n- I/ R0 M% L
  12. >>> math.frexp(-40)
    $ W* ?5 I" H1 o* M
  13. (-0.625, 6)
    & m8 k2 S. `8 j0 k
  14. >>> math.frexp(-100)$ C/ M& q% O7 ^. j
  15. (-0.78125, 7)
    8 o7 N2 c6 O5 b% X  t+ L( k* x% ]" a0 S
  16. >>> math.frexp(100)
    / H0 S  v4 H. ~0 @; Y. M
  17. (0.78125, 7)
复制代码
0 V( r" t0 \4 s2 S0 T9 o
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
* b: Z* C5 V7 C" d; I7 ]0 n0 d
  1. #对迭代器里的每个元素进行求和操作
    . T5 s0 y; x( B9 O9 }1 L" H
  2. fsum(iterable)2 ]7 I. }; {. y  P
  3. Return an accurate floating point sum of values in the iterable.
    & ]6 R; K. `! V7 Z
  4. Assumes IEEE-754 floating point arithmetic.
    , ^& d. ^$ h$ M$ y7 s( z7 e- O
  5. >>> math.fsum([1,2,3,4])2 |3 [  x2 w6 t" N
  6. 10.0# m8 t; X$ ]* w# U/ a$ j2 i' J
  7. >>> math.fsum((1,2,3,4))
      m1 H+ f  z' ?3 I9 \
  8. 10.0/ s2 n1 e' l8 e& x& c/ j
  9. >>> math.fsum((-1,-2,-3,-4))
    / k, ^0 i, v( x/ c( ~* M& ?  z/ q
  10. -10.00 w0 a8 e1 q( P, f$ h  z
  11. >>> math.fsum([-1,-2,-3,-4]); y8 v, ~( x' J
  12. -10.0
复制代码
1 x) e: z  G- D$ \8 D' z
math.gcd(x,y)  返回x和y的最大公约数: e' A' w% X7 V6 n+ l: G
  1. #返回x和y的最大公约数8 v6 x. S! X# q1 p1 @" J, X9 \
  2. gcd(x, y) -> int! o/ N: ^5 R* h8 t" y; e0 h5 F
  3. greatest common divisor of x and y
    ' f& |9 x% f' I, l
  4. >>> math.gcd(8,6)1 z/ X4 f, O% i% J
  5. 2
    6 H+ b% J# a2 l" s8 Q' n# L
  6. >>> math.gcd(40,20)% ]8 k9 Y& q, ]* A$ A+ o2 V/ n
  7. 20
    ! d" c; `& e: _/ {2 |
  8. >>> math.gcd(8,12)
    $ K9 E- _% m& T, q
  9. 4
复制代码
+ h/ E8 Y- X0 U8 h; C" @8 @
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False. M/ `, c' x) d1 I6 A& v
  1. #得到(x**2+y**2),平方的值
    " t% H. g  l4 |+ e$ x9 b
  2. hypot(x, y). ~& V" \, ~- x9 H
  3. Return the Euclidean distance, sqrt(x*x + y*y).7 l8 E& Y! S4 U3 v6 v' O
  4. >>> math.hypot(3,4)
      g5 B( W( K6 }- e% o2 n
  5. 5.0/ P. c9 J- y) s- ]. ?$ v6 X& g
  6. >>> math.hypot(6,8)
    ' Z( \! D) K# W, {  {1 q
  7. 10.0
复制代码

6 T3 I# R2 p: Z: N0 P" ]6 b7 }5 K3 Smath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False/ }8 L( C- b7 e
  1. #如果x是不是无穷大的数字,则返回True,否则返回False$ K7 f5 n# k( s, w" b8 r
  2. isfinite(x) -> bool
    % S2 d( R9 w9 M+ }. ~8 Y# c9 c
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! l  H3 k  S+ H! |' v6 E* t4 P- E: |1 A
  4. >>> math.isfinite(100), B3 W7 d. Y3 W% Z; m  C
  5. True6 K/ R. @5 `( \/ t) S$ h  l! U
  6. >>> math.isfinite(0)/ P3 L3 G8 K- S8 G, H4 Q+ e
  7. True
      u6 i% l' ~* p( G0 ?
  8. >>> math.isfinite(0.1)
    ; l' H# ~) z& [8 K& L; S
  9. True, U' w* p; c# K) Y4 T4 r5 |7 ]
  10. >>> math.isfinite("a")
    * d/ d1 x6 Z/ z" n8 e! ^
  11. >>> math.isfinite(0.0001)) j" v! Q0 _! v5 w2 e
  12. True
复制代码

! J. P+ l! U( P) i% g: _math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 l" a: q& d5 H% W4 T% g
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    6 `) J& m4 N6 x6 A
  2. isinf(x) -> bool
    % d; Q2 e8 L  J; F" k
  3. Return True if x is a positive or negative infinity, and False otherwise.
    . F5 R8 F( q4 e; }/ H+ R* f; N  R
  4. >>> math.isinf(234)8 i9 Z9 q$ ~5 `, n+ c
  5. False( w2 h5 {5 Q& z: y8 }5 h4 `
  6. >>> math.isinf(0.1)5 q+ l7 \& k) N
  7. False
复制代码
/ Z' V- Y* N; L
math.isnan(x)  如果x不是数字True,否则返回False
5 Z, I7 I) d8 Y6 g. G
  1. #如果x不是数字True,否则返回False) c; a2 x" b4 Y; g
  2. isnan(x) -> bool
    $ @  [; c& ^3 ~8 R3 |! e" E) Y: _
  3. Return True if x is a NaN (not a number), and False otherwise.) l; k/ C$ j5 k5 k! w1 Y
  4. >>> math.isnan(23)
    + z# y* u! G1 f1 ^- Q  `2 W. [* Q
  5. False; ~% z: j- v' n( _4 M! R& i
  6. >>> math.isnan(0.01)
    ; e+ ]) s+ r+ I# r0 Q- x
  7. False
复制代码
3 \: p( e' ?2 S
math.ldexp(x,i)  返回x*(2**i)的值" Y5 |* O, }! y: q3 n' [
  1. #返回x*(2**i)的值, z2 Q) a2 \7 I+ O1 j% p( U
  2. ldexp(x, i)
    7 l1 N1 E& E: [4 Y! Y3 ?# [! D
  3. Return x * (2**i).
    * O3 }- ?, f& }" s/ t
  4. >>> math.ldexp(5,5)6 ], Y& A' N. j9 L+ d& X4 b8 v# J
  5. 160.0
    + o* G$ M& i2 j3 p4 Z5 }
  6. >>> math.ldexp(3,5)
    . M# W/ L# d; K7 j: T- H5 e
  7. 96.0
复制代码
3 l; S5 H3 n0 p- \, \& m
math.log10(x)  返回x的以10为底的对数
2 p5 T7 F$ ~1 F5 T" |
  1. #返回x的以10为底的对数5 A  A/ Y: v1 L1 K, F! _5 _
  2. log10(x)
    : C$ P) g7 p, C9 T
  3. Return the base 10 logarithm of x.
    $ K" ~. K# {) ~( ]6 x6 }
  4. >>> math.log10(10)$ e% k$ P; ]1 d. i* J  z& u
  5. 1.0
    ; g( m3 D0 `# l" p) p) w
  6. >>> math.log10(100): _5 E, R3 l- W9 J2 s  }% x
  7. 2.0
    1 D+ Z) C" @/ T, Y
  8. #即10的1.3次方的结果为20  ^2 W2 }  F6 O0 W3 c5 I/ N! ~
  9. >>> math.log10(20)* r1 ]0 B( W9 X- ~8 ?  [
  10. 1.3010299956639813
复制代码
7 \/ \6 y3 y7 u* r" O
math.log1p(x)  返回x+1的自然对数(基数为e)的值0 u1 v$ p1 `) n0 w9 [& M" h
  1. #返回x+1的自然对数(基数为e)的值
    + h- F6 Q9 K% \) T7 N
  2. log1p(x)" I- \+ e/ [/ L: ~0 K% `
  3. Return the natural logarithm of 1+x (base e).6 Q0 m' d5 @8 b5 X5 J+ r9 V
  4. The result is computed in a way which is accurate for x near zero.
    9 U' Z( f1 q( F( ]1 r9 f& p! G
  5. >>> math.log(10)
    & Q) E6 Y- j8 R1 W, x& I
  6. 2.302585092994046: H) K9 i& J' l
  7. >>> math.log1p(10)
    / F* X& K$ ]; d: q% ]
  8. 2.3978952727983707" v% X% G& \1 k1 W6 G$ o
  9. >>> math.log(11)
    + e9 ]. l2 ^: {7 R' b
  10. 2.3978952727983707
复制代码

3 z+ S: w8 S$ j* V2 qmath.log2(x)  返回x的基2对数4 y% w) ?% q# B5 B8 W( k
  1. #返回x的基2对数( C: u& F9 ^0 Z# s- o
  2. log2(x)
    6 b& i' M: q) w
  3. Return the base 2 logarithm of x.
    7 }. Y9 w+ W: m# V; Q6 Z$ U, C
  4. >>> math.log2(32)' z: Q0 z0 `. t6 e" I
  5. 5.0
    . N* R( [( m1 c7 ?0 b
  6. >>> math.log2(20)/ q2 M8 H$ C& l% k( H% A
  7. 4.321928094887363
      ?2 \' r% ~3 h, G9 h, ]
  8. >>> math.log2(16). e1 I* m  P+ L/ l6 b
  9. 4.0
复制代码

3 v& T, B( ]: C, lmath.modf(x)  返回由x的小数部分和整数部分组成的元组0 l; A$ y" J7 L+ Y8 q9 [, b( g
  1. #返回由x的小数部分和整数部分组成的元组
    " M7 y# y- W% {6 |; ~
  2. modf(x)
    5 h$ b# i; c& T; c- a1 W& e% P
  3. Return the fractional and integer parts of x.  Both results carry the sign+ D) @5 p; S4 M  H9 _6 @
  4. of x and are floats.9 P/ f) C+ G! A4 [
  5. >>> math.modf(math.pi)
      d$ S0 i7 x8 T
  6. (0.14159265358979312, 3.0)6 A" B4 ?5 e/ V" R  J
  7. >>> math.modf(12.34)
    # {5 g; }5 O3 B3 o
  8. (0.33999999999999986, 12.0)
复制代码

0 i5 S5 l6 G5 Z7 M5 z" C+ kmath.sqrt(x)  求x的平方根
3 Q. b7 f6 N: {/ H0 W3 q& l# M
  1. #求x的平方根
    4 t  ]; a) U, a% Z2 R' {, m4 u
  2. sqrt(x)+ L; ~0 p( `, _- ^5 ~: o& {
  3. Return the square root of x.
    4 \: E. n/ O5 @2 q1 y
  4. >>> math.sqrt(100)
    * o2 ?9 g  ?9 p9 K/ n$ M
  5. 10.0* A& F' D9 T* T$ d9 W
  6. >>> math.sqrt(16)
    - d3 [1 J+ R7 V7 U( i5 y$ h- S
  7. 4.0
    + [* _0 _+ m! V; ~1 e$ R% ]) n+ y
  8. >>> math.sqrt(20)  R) V2 n! t) U( }' B& Q3 i" ]% j
  9. 4.47213595499958
复制代码
/ @; ?, U& A: U( Q( P9 m
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分2 p; }2 b: j; U" n& l: F/ T
  2. trunc(x:Real) -> Integral
    7 F  R+ \' h8 b- e- v; o4 p
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ b) [7 l3 \) m' b/ @" s$ {4 C
  4. >>> math.trunc(6.789)
    9 R" `9 Z) T6 f: q
  5. 66 H( m8 s5 b6 Z
  6. >>> math.trunc(math.pi)$ p) L+ Q& l( b) I- b* f
  7. 3; m8 |) }: G7 u  K9 H
  8. >>> math.trunc(2.567)
    6 M, o% y6 Z7 e5 `; D- v6 [# o
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-25 00:27 , Processed in 0.075224 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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