新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
/ |' m/ H; [3 J# E+ Q6 b
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 r0 e% Q1 ?/ j$ O2 }. R& Y" B
" M9 H& a  [, C8 t- J, ]
方法1
# {3 a8 H, n2 Q
  1. >>> import math
    # {6 v3 Q7 ^$ I- S# h7 a* p
  2. >>> math.sqrt(9)
    7 i5 @1 b. t3 F; V( {
  3. 3.0
复制代码
方法27 m6 a; J) ~; l( t' U# ^6 X
  1. >>> from math import sqrt- `+ F# Q, s, i/ `) x
  2. >>> sqrt(9)8 l! Z" y1 j$ ~3 s& m
  3. 3.0
复制代码
3 F9 ], H0 b, a0 F% i2 m

/ |2 i" p  P3 w+ `8 M* b1 W
math.e  表示一个常量
# S% C- O# J# z, |( u+ p; \% O) q- ^; b
  1. #表示一个常量% ?* h" \4 H* E) e4 P& t0 E
  2. >>> math.e: d% d* B- ?9 _* B8 m
  3. 2.718281828459045
复制代码

7 h! l8 g1 k7 r' z+ bmath.pi  
数字常量,圆周率
" ]# T% s6 p  F- h- M- n# g
  1. #数字常量,圆周率' Z. F% {2 F' e: a7 q9 l/ ?2 w
  2. >>> print(math.pi)/ X0 s$ g  j) f* d8 f, l8 O& J$ H
  3. 3.141592653589793
复制代码

1 k8 I9 A* v- G0 w9 mmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

" S* I1 B) j7 X! t0 a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    $ a8 p$ ]8 P4 k: Y! T
  2. ceil(x)6 v+ f8 n) m+ q
  3. Return the ceiling of x as an int.
    : c  X$ ]' J" W
  4. This is the smallest integral value >= x.
    2 A/ e- x) B2 o. L+ E
  5. $ E+ f& z0 M- ^2 p6 X
  6. >>> math.ceil(4.01)0 A4 W, y- f. F4 \7 l1 q
  7. 5
    5 D2 H0 v* T; L9 P+ a$ \
  8. >>> math.ceil(4.99)
    + H7 L3 u5 F- X. C: [. Q6 L
  9. 5: J5 e. ]+ g: P7 e3 l: s9 ?
  10. >>> math.ceil(-3.99)" ]- N, a# `, K  s% C( Y
  11. -3
    9 s6 O; J9 L; j, k- k% i& @$ W2 D
  12. >>> math.ceil(-3.01)) z7 c) H6 _  u% @0 d" b
  13. -3
复制代码
: b6 g. ?( ^! S6 ?) p9 N+ \
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, U, |8 f) }2 _: G2 o
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    & |+ `- ?: }8 s! U
  2. floor(x)' z; e) W2 W& o' b. R) Y
  3. Return the floor of x as an int.
    2 V& u% o# d2 |) v% r% n
  4. This is the largest integral value <= x.
    : y" W, s' |# r/ ?
  5. >>> math.floor(4.1)# N- V( S) Z4 r' U$ c
  6. 4$ H& E/ k, _6 |0 W) p
  7. >>> math.floor(4.999)
    ; j, B, ?% S# R. M" ^
  8. 4
    3 S! J3 z8 a; P' @9 l4 v) e
  9. >>> math.floor(-4.999)/ l' _7 ?+ T* o( b
  10. -59 z2 l$ t+ N8 @# h* q
  11. >>> math.floor(-4.01)
    % J$ ]7 x4 K. \
  12. -5
复制代码

" h6 ]( j5 p) \math.pow(x,y)  返回x的y次方,即x**y
6 l1 T* r  q6 J9 y# a/ p
  1. #返回x的y次方,即x**y
    1 e* R: n. \8 y. \6 s
  2. pow(x, y), E9 k2 p9 W) P$ K  U' Z+ X) T
  3. Return x**y (x to the power of y).$ J6 o1 J" s7 m3 J8 q) N
  4. >>> math.pow(3,4)
    ) z3 ^! J$ Z, [" b- |  \2 \- V
  5. 81.0  s* i3 V' {+ G% ?1 w  B; p+ c
  6. >>>
    : w  X0 B# z& w% Q
  7. >>> math.pow(2,7)
    , T6 U! D2 C6 V1 K, n! I
  8. 128.0
复制代码
( @( C# x$ ]3 V! a8 Q) r. i- \
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' O6 q2 q. Y' S3 `$ b) R
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    7 R  j, [- |1 ?1 M9 |3 z8 G  l
  2. log(x[, base])+ f. }/ ~1 J3 o: c2 Q
  3. Return the logarithm of x to the given base.
    1 Q4 l. E5 J$ ^' I4 ]) H! g
  4. If the base not specified, returns the natural logarithm (base e) of x.; c  f4 b/ `7 q5 k6 y$ i& O5 A3 o
  5. >>> math.log(10)
    1 r0 [; g8 ^2 V! d3 B2 y
  6. 2.302585092994046$ ]: x+ E% @, ~, k" J
  7. >>> math.log(11)
    ( O  z  ~  Z3 R( B, n2 k7 o
  8. 2.3978952727983707
    ; y8 f0 w7 C9 x# e; B% P6 V* z
  9. >>> math.log(20)$ I  |6 M2 Q8 H, m1 Z/ C& l
  10. 2.995732273553991
复制代码

* y* |* I0 R! t# {5 p1 _$ |1 D( Hmath.sin(x)  求x(x为弧度)的正弦值
8 {; c0 g. T7 p
  1. #求x(x为弧度)的正弦值
    4 O2 G+ s; Q1 F2 Q" O1 }* o4 A; c
  2. sin(x)
    + V) y0 _* f" Z/ i8 }
  3. Return the sine of x (measured in radians).2 c$ C- Q. @& {$ m& o
  4. >>> math.sin(math.pi/4)
    + V' h2 m$ Q  s/ }  @
  5. 0.70710678118654755 k! u; K) Z( j2 n. e4 U7 R0 t  i
  6. >>> math.sin(math.pi/2)4 u& ^1 q' k9 A4 t' \
  7. 1.0* `8 Z1 G0 }( w! h
  8. >>> math.sin(math.pi/3)5 j1 g) @2 _* ~* j  k5 O- y
  9. 0.8660254037844386
复制代码

( A; r% L6 _1 c7 C* Y; X& W. R- Tmath.cos(x)  求x的余弦,x必须是弧度/ D/ y& P- i! ?" e
  1. #求x的余弦,x必须是弧度
    9 E5 l9 m" H" V
  2. cos(x)
    6 E( W& ?* y5 Y- q
  3. Return the cosine of x (measured in radians).
    / x$ g4 W+ ^7 k7 M" x2 g
  4. #math.pi/4表示弧度,转换成角度为45度
    # e6 n' `3 V5 x9 z. v
  5. >>> math.cos(math.pi/4)5 W( X3 \9 w! t
  6. 0.7071067811865476& @  W7 x* I% _
  7. math.pi/3表示弧度,转换成角度为60度
    ! l$ h/ r5 O4 t5 t) O; T
  8. >>> math.cos(math.pi/3)
    3 D. i; i4 k7 D' e+ S  g0 T1 J
  9. 0.5000000000000001
    6 U9 q- N9 G2 h2 }+ u4 y1 ]
  10. math.pi/6表示弧度,转换成角度为30度4 O3 U  [2 ]7 _! q3 J
  11. >>> math.cos(math.pi/6)
    3 I8 ]3 |$ K2 j' I! Q* x% w4 M
  12. 0.8660254037844387
复制代码

9 t! v: \' a7 V0 b6 |math.tan(x)  返回x(x为弧度)的正切值# ^& R7 f; m5 v2 e
  1. #返回x(x为弧度)的正切值& g' g5 N" E4 Y. [* h) e; E
  2. tan(x)" {0 O! |/ `8 v: K! z
  3. Return the tangent of x (measured in radians).  ]4 d9 ~; O+ W
  4. >>> math.tan(math.pi/4)
    7 L6 C  u0 t7 y+ C) Y6 d
  5. 0.9999999999999999
    + d* A0 ?& G# |7 {# w$ H8 Y
  6. >>> math.tan(math.pi/6)
    - z4 V7 G0 m6 i, @
  7. 0.5773502691896257" @' _+ O4 E& T5 V5 {: T5 X
  8. >>> math.tan(math.pi/3)8 W! J5 f* k% i9 r! a
  9. 1.7320508075688767
复制代码
" |$ F1 ]2 X0 k( ?: X
math.degrees(x)  把x从弧度转换成角度8 s' a) `/ ^7 [! I; X% ~0 S3 p  M! Q
  1. #把x从弧度转换成角度
    + H) z" e) p% N4 t
  2. degrees(x)* E: S1 N& W  w: B) Z, X0 U% S: h
  3. Convert angle x from radians to degrees.
    1 X8 w! W+ @3 k. t
  4. ( N  T* {4 a. X. k$ |5 u9 J6 C$ e
  5. >>> math.degrees(math.pi/4)4 r0 w2 a+ c( E- h5 L$ Y( W
  6. 45.0" g" C! g9 G: X. l4 n$ V! g
  7. >>> math.degrees(math.pi)- i! E4 }1 p+ t" B7 z* m0 N" {4 \
  8. 180.0
    # l: m) \) z! C/ O9 |5 \
  9. >>> math.degrees(math.pi/6)1 j4 f% C( f' c6 L
  10. 29.999999999999996, b' Z" W7 e( n; y2 y* l
  11. >>> math.degrees(math.pi/3)' f5 x! u  {4 J. _5 S8 Z  d/ E! [
  12. 59.99999999999999
复制代码
5 z5 p3 K; l! w
math.radians(x)  把角度x转换成弧度: i* M; a# k+ E' s& {2 \  g
  1. #把角度x转换成弧度3 M$ }$ r2 ~4 a; a, ~  O: N
  2. radians(x): H( q; \' Y4 ~- e5 j3 {2 E& ]
  3. Convert angle x from degrees to radians.
    $ U, [' V* p" V6 Z6 f! S1 z
  4. >>> math.radians(45)
    * j( m. q. P* \
  5. 0.7853981633974483
    5 Z) a2 g* i( x& W. U" [
  6. >>> math.radians(60)) T2 h7 B+ Y4 e* `
  7. 1.0471975511965976
复制代码
2 G5 r  r0 E8 Y9 ]: Q
math.copysign(x,y)  把y的正负号加到x前面,可以使用0/ @# k8 y( Z  B. m) o% D
  1. #把y的正负号加到x前面,可以使用00 x& \+ @5 h9 Z/ T" l2 \  |* h  R
  2. copysign(x, y)( w9 H& k$ h4 _: }
  3. Return a float with the magnitude (absolute value) of x but the sign 3 i# \6 w0 j/ }* s4 C
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    " x: D. k) w+ Z; i0 y
  5. returns -1.0.2 M0 h* z& s: T. G

  6.   `8 d# x  I/ j% \" Z2 E, f5 x
  7. >>> math.copysign(2,3)
    7 A5 G, A% b8 y9 u6 _- v: A
  8. 2.08 l" U' |: q! z) I! r
  9. >>> math.copysign(2,-3)0 o, n8 m* r3 w9 b- W+ }4 h
  10. -2.0
    1 t$ J  l  t: Y. R/ q5 e# |* m
  11. >>> math.copysign(3,8)  p3 p/ l4 m" e4 E* V2 B
  12. 3.0
    8 _8 o' ]  T8 I6 G$ j0 I2 j
  13. >>> math.copysign(3,-8)8 u1 b) c- Y/ ^
  14. -3.0
复制代码
6 ^1 {* V! T0 _8 A
math.exp(x)  返回math.e,也就是2.71828的x次方
2 E+ w$ Y$ c- }0 |$ E
  1. #返回math.e,也就是2.71828的x次方# P" K! P8 l% H- j
  2. exp(x)
    % |) V3 a4 N2 d* H
  3. Return e raised to the power of x.
    3 o" I& c( |, @  r9 @( H2 @6 K4 j
  4. 4 k) _( K' w, a
  5. >>> math.exp(1), T& m; k8 K! e2 _' J9 Q% G2 p: N
  6. 2.718281828459045
    ) v' C/ J" h; t7 h7 {1 t
  7. >>> math.exp(2)
    $ \* u) I) Z# a, V
  8. 7.38905609893065
    3 q. U5 T* w- i. a# {. i" ]
  9. >>> math.exp(3)( I: T( d1 M3 t+ m1 U" ^
  10. 20.085536923187668
复制代码
; L7 o; A0 }% s# P2 d5 x
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# `& U' r$ E; C- L( I
  1. #返回math.e的x(其值为2.71828)次方的值减1& R% t4 `0 H  H
  2. expm1(x)
    , A' d' M0 ~. e4 ], M* ?% p
  3. Return exp(x)-1.
    1 a+ h2 N/ P- z' i% n0 K5 E
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 i4 R# I+ K% `
  5. # k+ q4 _) O) M  N
  6. >>> math.expm1(1)
    $ h/ ~% D' [. `1 R
  7. 1.718281828459045* V" w/ e" R( t2 r& C+ m
  8. >>> math.expm1(2)- E* J; [2 R. R# E3 u; H
  9. 6.38905609893065& W! [% a, [; _! S% T5 H
  10. >>> math.expm1(3)
    * Y* j( z: t6 [3 D0 B* d
  11. 19.085536923187668
复制代码

: T1 p) H1 z3 S  X( L" Z* v) Nmath.fabs(x)  返回x的绝对值, G5 y" ]7 B7 E2 z
  1. #返回x的绝对值
    & B0 X; x: N8 g
  2. fabs(x)+ i( p$ l9 v3 {
  3. Return the absolute value of the float x.
    4 G8 o. I$ N# N

  4. " _" {: }' Z! b8 C( w( F8 x: E$ ~
  5. >>> math.fabs(-0.003)
    3 v; d3 g4 ^6 F/ K  T
  6. 0.003
    ; I7 v; ?- [9 j, A
  7. >>> math.fabs(-110), z0 V: n: x$ w% \9 q! o
  8. 110.0
    8 l- |3 ]/ C/ s' E
  9. >>> math.fabs(100)  ]3 U& s# \) r) O3 O, |0 t0 j8 i
  10. 100.0
复制代码
; g/ e+ h0 l0 F* i. w& m) s  M1 Q% k
math.factorial(x)  取x的阶乘的值5 Q+ \" Q2 E5 J. f( S
  1. #取x的阶乘的值( Y; X3 C6 E  G0 y- j1 Q1 W, o. G
  2. factorial(x) -> Integral
    8 c$ _9 Z* o% \: E% H1 `1 z9 s
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    - A/ r7 x5 x' b, I+ y
  4. >>> math.factorial(1)
    + r( p! {* z7 v( Q* r' y. e' E/ \
  5. 1
    / O% z1 \6 w# f$ Y1 T. l
  6. >>> math.factorial(2)
    9 W$ Q: ^; g/ r5 H
  7. 2
    5 n, u% m9 `- J
  8. >>> math.factorial(3)
    % B- t; f7 E1 B
  9. 6
    ) x5 N) Q1 l8 Q! Z3 p
  10. >>> math.factorial(5)
    $ f  p  W. R" h
  11. 120
    3 d) Q- R, m; m2 u- q* I
  12. >>> math.factorial(10)
    6 f" L5 S0 N. @! \6 L' _, ?
  13. 3628800
复制代码

8 s+ B- B, k5 V9 l3 B9 y5 Zmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
0 z- e* o0 V' V* h  ~$ O
  1. #得到x/y的余数,其值是一个浮点数
    6 M0 }7 \: n. K5 v5 l4 ]2 g4 Q
  2. fmod(x, y)
      b% Z$ P* ?& Q/ w' {8 O4 H
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    * x) U4 B$ H1 d- g
  4. >>> math.fmod(20,3)
    0 K) K- ]' u7 A
  5. 2.0" X& B! j2 z9 X
  6. >>> math.fmod(20,7): T4 u1 d' \) u5 Z$ U
  7. 6.0
复制代码

! ?5 A2 k, a) ]0 m  @math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
' c" p- G7 d( Q# {  u; Z
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' R/ N. o- U# d
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    # J5 F3 C( C" \/ W1 W5 K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    5 I7 r  X, w8 w/ [% s3 i
  4. frexp(x)
    : _0 Z, m$ D; o8 m, ?; P
  5. Return the mantissa and exponent of x, as pair (m, e).
    ! R+ _/ s" h! }3 B- @
  6. m is a float and e is an int, such that x = m * 2.**e.
    5 n1 I; O3 [8 D0 l/ C. K' T
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    2 \, H. @& ]& x0 w" s
  8. >>> math.frexp(10)
    & A8 Z) F6 R- A) \: N# ]
  9. (0.625, 4)
    + `) d! ?! W& M: T3 k
  10. >>> math.frexp(75). Q) N% K8 }' T: Y* H' i6 S* b
  11. (0.5859375, 7)8 B7 z; K: L! L
  12. >>> math.frexp(-40)) G1 U- I& a5 ~+ F) ]
  13. (-0.625, 6)9 o+ K6 ]5 q  s( ]" ~7 a
  14. >>> math.frexp(-100)# d( G$ l* g/ R, I* C& P8 k
  15. (-0.78125, 7)5 @) j8 k: C5 I& X* e0 t
  16. >>> math.frexp(100)9 g* h6 `0 _8 \: |0 {- K
  17. (0.78125, 7)
复制代码
6 y7 w* x) }; M/ D( t0 b
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
5 ], P6 ~( z6 v4 U- J3 h3 S0 v
  1. #对迭代器里的每个元素进行求和操作2 x% q8 @7 W& o, k/ v
  2. fsum(iterable)& a( C$ P7 K2 f
  3. Return an accurate floating point sum of values in the iterable.
    & i! j9 i1 K) u  L
  4. Assumes IEEE-754 floating point arithmetic.) L# J& G8 Y4 b1 P& i
  5. >>> math.fsum([1,2,3,4])! j: H4 l" R3 C9 b( P/ s
  6. 10.0
    ; s5 F* a: i% K# Z5 W4 g; R
  7. >>> math.fsum((1,2,3,4))
    . A2 X7 ], X3 ^! g
  8. 10.0
    # D* D* x0 F/ t. @/ p0 }% `
  9. >>> math.fsum((-1,-2,-3,-4)); N8 e% m* k. k+ e
  10. -10.0
    7 ?+ `; z9 O. Y" p9 l; d1 U8 j
  11. >>> math.fsum([-1,-2,-3,-4])" @4 \1 b7 U$ O) p4 z: m
  12. -10.0
复制代码

, |" G/ ^5 ]5 y1 p; L5 |1 u7 C" Omath.gcd(x,y)  返回x和y的最大公约数. [9 O; X6 X) [) @8 ?  P
  1. #返回x和y的最大公约数
    1 N$ p- A% M4 Y. S  [; e$ i; a0 U
  2. gcd(x, y) -> int% P8 k  e+ G* h5 {; P6 U: w( a
  3. greatest common divisor of x and y6 i2 o- M8 J6 B4 t# f
  4. >>> math.gcd(8,6)
    2 `# r2 g4 x. Z8 O4 q, Y: @
  5. 2; A& g9 k" q; M, e( s: ]6 v9 y
  6. >>> math.gcd(40,20)
    6 S+ `$ L7 i3 L1 Y  @& q: J
  7. 20
    7 X3 {2 v) G1 e* _
  8. >>> math.gcd(8,12)
    1 g" F" P7 a) Q# {9 a
  9. 4
复制代码
2 \! h3 P3 l1 t0 s6 M
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False' H* J/ r5 C- p4 V
  1. #得到(x**2+y**2),平方的值
    4 R' V1 L2 \" t2 {% y% z6 J
  2. hypot(x, y)1 f5 @0 [  k4 o1 G) o$ e* B2 {
  3. Return the Euclidean distance, sqrt(x*x + y*y).5 ?. g9 K7 e2 l$ B3 g7 T1 e
  4. >>> math.hypot(3,4)4 N2 B% q. k2 Z. V/ ^! i
  5. 5.02 F5 d7 p% }* c9 _- }
  6. >>> math.hypot(6,8)
    5 f& H6 L2 S0 S( F; n/ a
  7. 10.0
复制代码

8 l) i3 g9 p3 j" r/ A, B7 }6 o- rmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False+ w( C, T, O( O( F
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ( V! ^" t) x$ k& X% P
  2. isfinite(x) -> bool& z, i  e8 q! B8 \' P4 C
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    4 u' a! G- n3 g  R: e
  4. >>> math.isfinite(100)
    ! U- {, B5 O+ T7 R( p: [
  5. True
    5 G+ ~: Y! }" y0 @2 B1 G0 A
  6. >>> math.isfinite(0)0 g# m$ u6 R8 {, a6 s
  7. True
    ) W1 Z, l4 S- n
  8. >>> math.isfinite(0.1): C! j7 m  T1 i  V* A* l7 f) f
  9. True
    $ I$ Q) U* `! P  C3 S' G' H
  10. >>> math.isfinite("a")0 W( V) _4 d+ @, _" P$ M! d
  11. >>> math.isfinite(0.0001); D! g1 R) [8 x, ]2 N4 o8 T
  12. True
复制代码
( {9 d8 C# v# {2 Y9 Q
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False0 v  z8 l9 A) |5 I# C
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    " y, v( i' X3 [. q7 t3 o0 O3 m) k
  2. isinf(x) -> bool$ D- q- g4 u& t( r
  3. Return True if x is a positive or negative infinity, and False otherwise.9 p5 E+ \) z2 }
  4. >>> math.isinf(234)
    1 n9 j% H. K" o* u/ R, o. l% b
  5. False$ R) b1 V+ l1 M0 H
  6. >>> math.isinf(0.1)8 }# y# g! d7 O; z1 ]7 ^5 d0 r
  7. False
复制代码

, F; k- \! o+ L# p" Smath.isnan(x)  如果x不是数字True,否则返回False
8 k% w( O7 G6 ~! H- M: I- T
  1. #如果x不是数字True,否则返回False
    $ Q5 i4 k9 q0 v7 e/ ^
  2. isnan(x) -> bool/ h5 q5 d6 S7 X' T; H
  3. Return True if x is a NaN (not a number), and False otherwise.5 i$ g$ N6 n( Z! B9 K: q$ |: q
  4. >>> math.isnan(23)
    ! n  N3 {  h' }, b& o
  5. False* z& R! K6 J/ _
  6. >>> math.isnan(0.01)+ `" n! }- a( ~8 @. ~# a
  7. False
复制代码

5 J! O. J& r7 Y& `+ _math.ldexp(x,i)  返回x*(2**i)的值
$ U9 D! P) E$ h0 ^9 x
  1. #返回x*(2**i)的值
    % q& i! T4 a# l* {/ v0 d  W
  2. ldexp(x, i)8 j, Y8 Z  ]1 D6 o* |
  3. Return x * (2**i).1 K1 c$ n+ i6 x( x9 ~
  4. >>> math.ldexp(5,5): f* W: s' A! w+ s
  5. 160.01 y  E" d/ F) U( C" K0 [
  6. >>> math.ldexp(3,5)
    % ]4 F: w: B5 l* \) D% b
  7. 96.0
复制代码

8 @9 }% e8 B. Z9 W; u7 c& h6 tmath.log10(x)  返回x的以10为底的对数. b: ~, E- b& }7 l, z3 Q0 ?% D) W
  1. #返回x的以10为底的对数. l: C, l6 W' U; E7 x5 ^6 S
  2. log10(x)
    . x3 g" o, `; [# K# g9 O
  3. Return the base 10 logarithm of x.# [; w8 t, s% s  T2 C- ~
  4. >>> math.log10(10)
    9 @, A& T9 @% M6 f8 R) p3 L, Y* q% y
  5. 1.0
    2 F" T1 F8 n; B& E, R- r  |
  6. >>> math.log10(100)( d: D0 c$ C8 D1 d* J* T
  7. 2.0& a4 f8 W* Z+ Q
  8. #即10的1.3次方的结果为202 U+ N8 ]$ h: }( e* L2 a( J* g
  9. >>> math.log10(20)
    9 q% o  V) W" r3 S% d8 ^( P, G; F
  10. 1.3010299956639813
复制代码
, T" C$ C1 W9 H/ l
math.log1p(x)  返回x+1的自然对数(基数为e)的值
. }# i% ?. z3 \; c& B
  1. #返回x+1的自然对数(基数为e)的值; N! F+ B% s2 w! _
  2. log1p(x)
    5 T" `6 ^: B8 T9 z7 C
  3. Return the natural logarithm of 1+x (base e).
    % V  h9 q; @1 J, {7 W; x! p
  4. The result is computed in a way which is accurate for x near zero.
    / w9 e% o: R# n8 i. ^8 L
  5. >>> math.log(10)
    0 t* m: T$ m! K
  6. 2.302585092994046# Q& D- u! P- x) L, E/ q
  7. >>> math.log1p(10)
    9 R1 Y0 a$ V( u( V9 |0 o
  8. 2.3978952727983707
      j" |5 I/ T0 y" M; Q, v+ n1 R
  9. >>> math.log(11). z6 l  \; i# U" O
  10. 2.3978952727983707
复制代码
( E7 r. o- s& D# r
math.log2(x)  返回x的基2对数
6 s4 o' J( S2 w3 e
  1. #返回x的基2对数% _" h+ ~" T3 P$ N, {! f1 N4 {
  2. log2(x)
    & a& ?/ \- \3 y6 h: ?, o) m# A; h$ O
  3. Return the base 2 logarithm of x.
    6 l4 [5 Z% k5 \
  4. >>> math.log2(32)+ F' d  x' k. m
  5. 5.04 g  ]/ s; ?" _4 A1 o3 l4 \
  6. >>> math.log2(20)
    2 o' ?, m, i; O  T5 F+ U3 g9 ~# _
  7. 4.321928094887363
    ! E" p8 @3 {, M7 l' T3 _7 F) q" ]( H
  8. >>> math.log2(16)0 m( p' x2 f- w' s, k6 _0 z; \
  9. 4.0
复制代码
' m% |7 M* b. j5 d) `% _( e0 S
math.modf(x)  返回由x的小数部分和整数部分组成的元组
6 \2 |, h0 o+ E/ c0 _
  1. #返回由x的小数部分和整数部分组成的元组
    % q7 N6 s9 F) g5 j# u, P! q5 _& N& u
  2. modf(x)  ]% i. R- f( m
  3. Return the fractional and integer parts of x.  Both results carry the sign: t6 z! h" k6 x) g  V: Z& ]
  4. of x and are floats.
    " a+ J' [( {+ b$ M5 f% B
  5. >>> math.modf(math.pi)
    - \5 B% b0 R( {& {0 @7 S6 H
  6. (0.14159265358979312, 3.0)
    ) S) x( ]* V. o5 _
  7. >>> math.modf(12.34)1 p  q% T; u# K# p
  8. (0.33999999999999986, 12.0)
复制代码

/ ?% d" q; k: C% O7 Bmath.sqrt(x)  求x的平方根2 `% U, Q( M6 }2 {8 o
  1. #求x的平方根
    # s8 r% Z. a' E! W1 @4 D7 j
  2. sqrt(x)
    * ~9 a9 v- Y9 h
  3. Return the square root of x.4 E3 Y3 Z' v3 }9 p' e
  4. >>> math.sqrt(100)
    1 T/ d! ^) z( q  w6 P& i
  5. 10.05 W: I4 U9 v' d& r
  6. >>> math.sqrt(16)
    & R4 u4 V3 L( y3 O2 J
  7. 4.0
    # D$ m/ B- j: b% K6 C7 i
  8. >>> math.sqrt(20)
    . i$ t5 T; k: f
  9. 4.47213595499958
复制代码
2 E: a+ v, D# g4 F" c) W& E
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分/ y' R8 Q4 V7 y  r7 }* V
  2. trunc(x:Real) -> Integral. n( Z. _8 F9 X$ v# x! H  Y3 E3 \
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    " z- L# k3 Q) D% u! j
  4. >>> math.trunc(6.789)9 _0 K& y& ^* w2 n% O& `8 t* k
  5. 6
    $ s& v! X$ c& j) h
  6. >>> math.trunc(math.pi)' a9 e: D  Y! n, Q# d
  7. 36 P) s$ ?8 x7 k
  8. >>> math.trunc(2.567)
    0 u4 w# W: w$ a% P) E6 n/ _  M
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-21 11:04 , Processed in 0.106233 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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