新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

) k) T: G- d7 k8 G" x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, s$ }5 {3 P" I9 w

: T( `: M' X; o5 B9 h方法1
2 `9 ~0 m: _+ a9 m: i
  1. >>> import math6 W. n$ G6 f% E* b/ _& S; v" C, ~
  2. >>> math.sqrt(9)& Y6 `% C1 q) Y, u! r- w
  3. 3.0
复制代码
方法2
: j2 r; o. x4 ]4 o
  1. >>> from math import sqrt0 H0 u- i2 y2 `- [* K/ p
  2. >>> sqrt(9)
    ; r  L. V+ N# |% f' T" g
  3. 3.0
复制代码
/ \6 u0 Q1 V0 H9 {) F


# Z2 y3 i7 L* i2 J& k; N$ p5 q
math.e  表示一个常量
% c. j( t' x2 R8 S
  1. #表示一个常量: H" g" ?6 S  X! a4 ?3 P( c
  2. >>> math.e9 f6 B) S+ G; {2 A
  3. 2.718281828459045
复制代码

9 B" E! _& g6 V9 e& Pmath.pi  
数字常量,圆周率

$ Q* n$ p; |+ [1 ^
  1. #数字常量,圆周率4 K7 R/ }# S5 K$ B6 }5 p/ W) W
  2. >>> print(math.pi)2 ^2 n/ c2 s; f
  3. 3.141592653589793
复制代码

3 X5 M7 X) j/ m, |5 k1 \# V5 Xmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 u% j  ?$ K. g! E0 z3 f; Q& b$ t( \, T
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x0 G1 |  q$ h3 G1 X. t
  2. ceil(x)
    , R1 }, H* Y( s
  3. Return the ceiling of x as an int.
    1 J  R$ R# r3 ^
  4. This is the smallest integral value >= x.1 ?" j9 ?+ f% ~

  5. 4 W, a+ q8 Z: j7 h7 _; O
  6. >>> math.ceil(4.01)+ Q- C4 o, [" f# d
  7. 5
    8 r! w, `2 V  ?4 F6 s( ]
  8. >>> math.ceil(4.99)
    : O2 o3 v% L1 l2 Y% B
  9. 52 O9 U3 A0 y% E1 s) e
  10. >>> math.ceil(-3.99)
    - _% N4 {  o# U6 N8 G0 g
  11. -3# M2 x0 h3 C2 Q5 J0 W+ n2 T
  12. >>> math.ceil(-3.01)
    ! W% \7 W9 G9 p/ A, }& r
  13. -3
复制代码

0 [( ]4 U( D' |* }5 Z  H& Cmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ x3 d8 L! G" a& X2 G
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    , M/ z7 w$ r, y( J. o7 U$ A" f
  2. floor(x)
    4 b  l4 O0 s' ^1 F7 |2 c
  3. Return the floor of x as an int.
    5 e6 }/ O) q) s# L- B8 z0 h
  4. This is the largest integral value <= x.- H/ c' B9 `8 R9 K. I7 x/ n
  5. >>> math.floor(4.1)) ~5 V% |8 a$ u) Z
  6. 4. Z% y3 g! P8 [/ S) ^# u! A9 V
  7. >>> math.floor(4.999)) Q% u4 e) E5 n/ X0 s' R/ I5 L) @# _
  8. 4) @/ l5 ~, I6 q0 y8 Q: ?5 U
  9. >>> math.floor(-4.999)4 x2 F, Z( F0 o9 c- `" L
  10. -5
      k% z. h* c: r) H
  11. >>> math.floor(-4.01)
    # v1 R6 B# k; m1 o, P
  12. -5
复制代码

4 M8 R2 d5 y  e6 w6 cmath.pow(x,y)  返回x的y次方,即x**y
" }3 Y# a4 G4 V1 {& v. x
  1. #返回x的y次方,即x**y# k6 R  @% b3 A4 Z% Y
  2. pow(x, y)
    , M" y, M% Q4 k  A. a& _
  3. Return x**y (x to the power of y).$ }5 G! _: z; E2 l( f! x
  4. >>> math.pow(3,4). z/ e+ k2 H/ P# b; E. m( v( U
  5. 81.0& O- K& q; I& {) a- e
  6. >>>
    ; \9 A0 v$ U' r5 [: B
  7. >>> math.pow(2,7)# _) w$ t4 l1 F/ h
  8. 128.0
复制代码
  Z: A2 `: ]* N/ d5 c7 N0 f+ r
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 C( f, S- `2 d6 e* j
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( c! Y9 @4 b1 }
  2. log(x[, base])  O/ e2 c- |3 s$ b" ~1 S
  3. Return the logarithm of x to the given base.: x! M! `. }5 K
  4. If the base not specified, returns the natural logarithm (base e) of x.
    9 }. H$ E& c* p5 }0 V
  5. >>> math.log(10)
    9 a  c8 j) W0 |' w
  6. 2.302585092994046% ~- Z: p) j  [) ?
  7. >>> math.log(11)
    ' Q/ L3 h/ }1 s' D2 P  `( p; i
  8. 2.3978952727983707' G) D3 B* ]) |+ a4 K2 g
  9. >>> math.log(20), t5 c  {! h' g
  10. 2.995732273553991
复制代码
. `. `( U% u0 I0 A9 r. h
math.sin(x)  求x(x为弧度)的正弦值
) `2 D* K9 @# a. {
  1. #求x(x为弧度)的正弦值/ S1 [2 e. f3 O  [5 \: ]
  2. sin(x)7 [! B! K0 Z* @3 \4 K8 J& J
  3. Return the sine of x (measured in radians).2 W6 G% |& V* _  z, w! ?5 o, d
  4. >>> math.sin(math.pi/4)- j0 t/ c% B# w3 \4 M% O
  5. 0.7071067811865475
    4 u& S7 P+ F2 S+ n; u  ^+ N
  6. >>> math.sin(math.pi/2)$ J: |0 j  `1 `1 B$ p. X& ?
  7. 1.0
    0 V1 `( j: u# ~9 @" U4 J" q
  8. >>> math.sin(math.pi/3)0 B9 ^4 M5 b4 b' x
  9. 0.8660254037844386
复制代码

1 T: e" E: K+ U) W- ~7 W5 U& Imath.cos(x)  求x的余弦,x必须是弧度) \1 x9 Q# U$ F* Y
  1. #求x的余弦,x必须是弧度6 y  D$ r; P$ v1 o0 u
  2. cos(x)
    1 G( X2 q  b* o# i
  3. Return the cosine of x (measured in radians).- i4 R2 O. }, i% z, g6 U. ^! t) E
  4. #math.pi/4表示弧度,转换成角度为45度% n" n# T: _! \3 U$ p* s6 Y8 S
  5. >>> math.cos(math.pi/4)* M, F+ W: C/ Z2 E  A
  6. 0.7071067811865476
    8 |/ n1 G0 N& S! c1 x6 ~% t
  7. math.pi/3表示弧度,转换成角度为60度; B$ }& D! ~1 l) k' ]( j" {
  8. >>> math.cos(math.pi/3)
    & N- @' Y. w1 l& j' C
  9. 0.5000000000000001- S* @" U# _: E8 X, Y
  10. math.pi/6表示弧度,转换成角度为30度
    3 {  S: `) _" r- x, Q1 u6 \/ H
  11. >>> math.cos(math.pi/6)6 l* |/ ], r9 p2 Q  L6 T
  12. 0.8660254037844387
复制代码
5 g# Y8 ?: g, L- J" j: R9 {1 J
math.tan(x)  返回x(x为弧度)的正切值) S, I# E+ ~* G" v
  1. #返回x(x为弧度)的正切值
    ; I# ~0 A2 I! [; `) R1 G( f
  2. tan(x)& j5 \" x1 M1 D8 _  z: @7 F
  3. Return the tangent of x (measured in radians).8 F7 A6 x0 x( f$ z1 y- J
  4. >>> math.tan(math.pi/4)/ ^% w- O# s( I; |! t: X9 j9 u
  5. 0.9999999999999999
    ' x/ |7 U! K) A! s
  6. >>> math.tan(math.pi/6)
    3 I1 u1 ]' j0 k+ N! b
  7. 0.5773502691896257
    $ a" F" Y5 v7 `, K9 `& I# B! ~( C
  8. >>> math.tan(math.pi/3)
    $ x1 K+ N2 c! x; }
  9. 1.7320508075688767
复制代码

8 l. m. o- k! P) H0 S) |math.degrees(x)  把x从弧度转换成角度2 I5 i2 M7 @8 M* [) S
  1. #把x从弧度转换成角度, S2 r% O$ r1 u# F
  2. degrees(x)
    6 e( W( ~7 J  Z% ^4 n' q
  3. Convert angle x from radians to degrees.
    9 x9 M& ?, p; S1 ~. q$ }
  4. , q  ~' J7 ^* u% Q( d$ I7 B. r
  5. >>> math.degrees(math.pi/4)
    3 \8 U/ Z# \2 e6 ~( G2 [5 c
  6. 45.0
    8 Q6 }3 U' L4 [  W' B; e+ l- s9 D
  7. >>> math.degrees(math.pi)6 l! U0 _  Q2 C+ H3 E
  8. 180.04 o( ~5 i9 Q3 m0 y
  9. >>> math.degrees(math.pi/6)! u4 t9 D% C/ y( u8 a8 r
  10. 29.9999999999999964 H9 D7 E6 l: Z6 q0 S, X# W
  11. >>> math.degrees(math.pi/3)
    , y8 P; X/ t0 a+ l- M. u% V" J4 v
  12. 59.99999999999999
复制代码
5 q* k% U. t& g3 `, _' S5 _& j! K5 z! V  T
math.radians(x)  把角度x转换成弧度
9 Q: b5 q9 u0 B* m
  1. #把角度x转换成弧度
    % A5 y( o2 f- n" p4 g
  2. radians(x)8 A$ @5 @! B5 w4 z$ A
  3. Convert angle x from degrees to radians.7 O# m, H4 g9 ?4 _' k2 G: t5 B
  4. >>> math.radians(45)
    : E5 A" y# {- n- N. K
  5. 0.7853981633974483
    2 L/ Q. I: R: c3 H
  6. >>> math.radians(60)% O/ J( Y4 k/ `: e3 a# l" L
  7. 1.0471975511965976
复制代码

! M2 b" s: i1 h: L/ _math.copysign(x,y)  把y的正负号加到x前面,可以使用0
, w$ I2 C) S# u8 }, N, l# o$ q
  1. #把y的正负号加到x前面,可以使用04 S' \+ Z) ?3 `" t! v
  2. copysign(x, y)1 ]6 s  Q* ?; x
  3. Return a float with the magnitude (absolute value) of x but the sign
    + ?9 z- r; E3 N6 n0 C6 E% d$ o
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) , K* `* \# `8 r# s7 U% H# x  k. t" d
  5. returns -1.0.
    : E5 j+ t( x" `% E

  6. 1 W& d1 I( b# Z/ s! L) Q
  7. >>> math.copysign(2,3)+ W& Q; ~9 I3 F# m- C4 B7 B  K6 U9 D
  8. 2.0; J$ g6 b# c: B' X* i. R% k- \
  9. >>> math.copysign(2,-3)
    / o6 D: [9 \7 c( C/ a, j3 v
  10. -2.0* ?& v* _  r- Z# c
  11. >>> math.copysign(3,8)+ W% k& j% z) r
  12. 3.0
    ) t$ {% s: x1 g6 T
  13. >>> math.copysign(3,-8)
    8 e8 v( i. `  c8 p% `
  14. -3.0
复制代码

" q, _) c% _: s; U4 @math.exp(x)  返回math.e,也就是2.71828的x次方
- a% |* q7 d3 h; }  W6 K8 c
  1. #返回math.e,也就是2.71828的x次方0 `( p8 h$ u( R& Z# |
  2. exp(x)
    1 N- X9 \# P; Z6 ?0 }
  3. Return e raised to the power of x.# i4 Q3 ?& c- Y. Q

  4. # _% Y: d4 T! ?! R
  5. >>> math.exp(1), u# [( r+ g8 J! e! [1 z) v
  6. 2.718281828459045; j- c" O5 E* T9 \+ e+ U: e
  7. >>> math.exp(2)
    5 b2 g2 p) M' g8 L; K2 {7 c
  8. 7.38905609893065
    9 h* b9 z) S$ T3 H+ k5 Q
  9. >>> math.exp(3)
    ; \3 ?2 n1 V9 @4 }4 T8 X) B; i( \6 O
  10. 20.085536923187668
复制代码
5 V, Y5 |' t) k% O
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
1 ^& f3 ~' k1 F3 i, h7 }' `* M
  1. #返回math.e的x(其值为2.71828)次方的值减1( T& {/ ~: Q" _9 Z  w: y: v
  2. expm1(x). o% B+ W2 i0 h% L' j
  3. Return exp(x)-1.
    ) C4 f5 D7 u% u  _2 Y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 s- F/ H) \- w( z9 @' C5 c5 ]

  5. ) W3 A1 S$ C, z2 ]& r
  6. >>> math.expm1(1)3 f' k9 {- |% F1 B2 r
  7. 1.718281828459045
    % }" a  c+ [+ n+ c' M
  8. >>> math.expm1(2)
    * {( z  T% e% J; ^! ?+ C( B: u
  9. 6.38905609893065! I0 ?- x: u, c6 E( f% M- H/ a
  10. >>> math.expm1(3), d# j3 s% ]$ y8 J$ }4 x
  11. 19.085536923187668
复制代码

- ^, a" {( u) B; D3 T# ^' t: ymath.fabs(x)  返回x的绝对值) P& |1 o- j3 f! H: d
  1. #返回x的绝对值0 r6 H5 O1 X3 |3 u! K$ C
  2. fabs(x)5 w3 X6 e# a% T# N- ^5 E- x) b; ^, k
  3. Return the absolute value of the float x.6 V( ~1 g/ K. D) e" p( s

  4. 2 E; @# j: }. Q% u$ h
  5. >>> math.fabs(-0.003)! Q' E4 J; @$ L$ P+ Y8 X& c# a
  6. 0.003
    9 w* L7 y( B: n0 @9 k! v! ~
  7. >>> math.fabs(-110)7 F3 c2 L9 E0 R/ h4 W
  8. 110.0
    ) J! k# ?" l2 k5 V! H
  9. >>> math.fabs(100)! h5 l) i& {5 P, [8 [0 N3 C
  10. 100.0
复制代码
# S" F+ h! i. s
math.factorial(x)  取x的阶乘的值
6 y3 e- f! F( t; ]; m8 J' t
  1. #取x的阶乘的值6 X# J- ]9 O, \
  2. factorial(x) -> Integral5 }2 T$ k, o4 w+ {2 r! F
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ! j% `; }. g* a/ h
  4. >>> math.factorial(1)
    ' N/ |5 T/ g) R# M) ^, q8 J
  5. 1
    7 Y& @; l3 X8 R8 M7 y
  6. >>> math.factorial(2)5 c9 I, y4 v8 {+ H0 C, k
  7. 2
    % Q5 r4 E3 u% V; U+ I% ?6 q
  8. >>> math.factorial(3)7 b9 t! {% ~4 t) }/ k3 l
  9. 6
    1 q7 e& C5 v4 W6 R6 I
  10. >>> math.factorial(5)
    & g% z, n/ k: k, f1 m. Z% w
  11. 120
    ) J) N) r1 L$ H# _5 \- j/ l6 B
  12. >>> math.factorial(10); z% x( R6 Z; R. {: n+ s4 _) R9 s4 `
  13. 3628800
复制代码
! i' |( P+ f8 j3 j
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数7 N7 K6 v9 j# h1 T: d! y
  1. #得到x/y的余数,其值是一个浮点数
      u+ L3 g7 {! E5 h# v
  2. fmod(x, y)/ d2 Z4 {: T+ q: G, Q
  3. Return fmod(x, y), according to platform C.  x % y may differ., ]' Q7 T" d1 S1 q( w4 V4 @, N) z
  4. >>> math.fmod(20,3)2 }" a- {: e2 w7 _9 }9 z
  5. 2.0( o3 @. E. `- V1 P2 _& t1 `
  6. >>> math.fmod(20,7)
    ( P5 B( p% I! V7 G! Y+ S
  7. 6.0
复制代码

- y$ P; T  J4 M& A% Zmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ |4 {' @. ^" J- K
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ; k6 \) K: ?% |& M4 `. n* f
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. D8 a9 m4 |' [9 d
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 T! s+ _8 u9 V
  4. frexp(x)- v  l5 A- E& l. g9 [
  5. Return the mantissa and exponent of x, as pair (m, e).$ n8 F6 Z  K  k6 Y. h
  6. m is a float and e is an int, such that x = m * 2.**e.! L0 P  u# ^# m, p
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0./ a% I. W; g/ G. }. a
  8. >>> math.frexp(10)
    & r8 h3 M9 r1 _' X' f" {
  9. (0.625, 4)8 Q. p$ e+ i& }2 F" |
  10. >>> math.frexp(75)
    / ]' V3 X3 G$ K) s1 {2 J: Z
  11. (0.5859375, 7)4 Q8 O+ \  N+ ^  x; u0 T
  12. >>> math.frexp(-40)7 N3 P% F  P0 K7 S9 R" I$ A( |
  13. (-0.625, 6)
    " D4 b  H0 W! J, @  j& h  e
  14. >>> math.frexp(-100)1 q: p' `1 L6 h+ y5 S" K
  15. (-0.78125, 7)% |7 b# I" [: p# n' ~- B
  16. >>> math.frexp(100)% a5 s9 \/ E0 z5 ^- B$ o/ Y6 s# ]$ Y
  17. (0.78125, 7)
复制代码

3 g8 E9 l" V, O/ }* u/ rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
, b4 |: X5 P/ m9 W
  1. #对迭代器里的每个元素进行求和操作
    9 I$ s4 ?6 a, L3 G9 W
  2. fsum(iterable)
    ) J- q- [: a  w8 \- V
  3. Return an accurate floating point sum of values in the iterable.) x7 ]8 `1 c# X1 t) h
  4. Assumes IEEE-754 floating point arithmetic.- Z, Q6 @: ~0 O" z: v! W# C
  5. >>> math.fsum([1,2,3,4])
    3 [. a9 Z5 E8 D+ q
  6. 10.08 Y# Z: n9 R* T, S8 Z7 @4 Y& e
  7. >>> math.fsum((1,2,3,4))  k" o  X- M4 C% M0 S
  8. 10.0' X0 v* e! D. e% O
  9. >>> math.fsum((-1,-2,-3,-4))
    # @0 |3 w1 {1 v2 ]( s* t
  10. -10.0
    9 r8 l& N3 X; Y  f
  11. >>> math.fsum([-1,-2,-3,-4])
    : n6 a2 s1 a- n$ V* |8 Y
  12. -10.0
复制代码

5 f8 n$ \- r: C1 V4 U; Zmath.gcd(x,y)  返回x和y的最大公约数4 {+ [$ o( l; X$ p' S1 L
  1. #返回x和y的最大公约数- h5 Z- F6 E9 _& h3 f' u* i$ Y
  2. gcd(x, y) -> int/ ^% |0 F" \2 p+ r
  3. greatest common divisor of x and y
    ; f+ [* j7 l1 [( c: A# f4 O
  4. >>> math.gcd(8,6)  `' A1 u. @: H7 R; m8 D3 }) m* I
  5. 2
    0 v2 d5 `1 A  C- |6 z
  6. >>> math.gcd(40,20)! p, {$ v7 n1 U* U0 W  o$ d
  7. 20/ j. F- h; K0 e7 L9 w  n9 ^. H+ w2 B% O
  8. >>> math.gcd(8,12)+ |+ ^0 G# Y2 H0 Q
  9. 4
复制代码

7 v/ S6 ~0 V/ N% Bmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
1 h) A# D( g& Y! ?
  1. #得到(x**2+y**2),平方的值
    7 {, ^( Y; R0 u3 Q7 d
  2. hypot(x, y)
    + b& W7 L* _( p$ d, D+ U
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    2 R0 ?; T, t7 N2 r8 `5 V
  4. >>> math.hypot(3,4)- R' Q; z  p' T, L+ [7 l
  5. 5.0
      [' B2 V9 l; ^) V
  6. >>> math.hypot(6,8)
    $ A# P8 g+ @! o& I
  7. 10.0
复制代码

9 _$ D* c& m% \1 A# k1 h$ xmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False! H6 S& p! K) _1 P
  1. #如果x是不是无穷大的数字,则返回True,否则返回False+ Y, m) c( o7 z4 [$ S
  2. isfinite(x) -> bool
    : D) t+ y; @1 H2 `
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    1 A: @! V: }! c5 b" N; M; X8 p
  4. >>> math.isfinite(100)
    " C' m1 v. ?3 D: P* t/ M$ h
  5. True
    5 D2 [$ N/ D- r  y+ `: ]
  6. >>> math.isfinite(0)
    & j* z0 j* ?; x
  7. True! D7 f  A# G% v+ N
  8. >>> math.isfinite(0.1)
    7 r8 `) t; o. U  f% v
  9. True4 w' ~7 O& w; q; A% c0 j
  10. >>> math.isfinite("a")
    # J" G! a* @' i0 S$ {0 z
  11. >>> math.isfinite(0.0001)
    8 |6 E- U/ Y1 `4 }4 Z6 M/ E- w: G
  12. True
复制代码

2 |( o0 `: y  T- ]. v2 @8 {math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False) Z* n: d" ^6 w1 y9 u
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    , W& b2 {" o! ?. x; ]8 C/ a/ c
  2. isinf(x) -> bool
    6 p7 m4 a: o2 O- l
  3. Return True if x is a positive or negative infinity, and False otherwise.
    . y3 W2 y$ D% b2 m$ I- {
  4. >>> math.isinf(234)
    + a: K* m3 d$ f2 f" Z4 c
  5. False3 ]4 e* U6 q( q1 n
  6. >>> math.isinf(0.1)
    . [# U. y2 }" w6 p3 z
  7. False
复制代码
7 `' c  L5 ^4 Y  n* @. s! c% y3 |# Z. {
math.isnan(x)  如果x不是数字True,否则返回False
3 u$ p! N1 ]+ |/ `0 r+ C
  1. #如果x不是数字True,否则返回False7 O4 y! V) H" f
  2. isnan(x) -> bool
    ( X% X$ }! Q+ A4 e: u) G4 E& m# G) |; O
  3. Return True if x is a NaN (not a number), and False otherwise.
    + b4 Y5 c+ x5 \8 x& v- o4 C& k, h
  4. >>> math.isnan(23)1 E( r- |" @4 E- x3 C
  5. False* e' c# p! q$ |0 M
  6. >>> math.isnan(0.01)
    $ o6 X" m; w5 I* w+ l5 e  Q
  7. False
复制代码

, O7 i  E4 @- ^( {, D. k: y  ?; _math.ldexp(x,i)  返回x*(2**i)的值/ z* B. B' F6 E" G1 p
  1. #返回x*(2**i)的值
    ! Y2 B) P; x1 v) _
  2. ldexp(x, i)
    - `" e5 e6 D2 h; P0 Q2 x
  3. Return x * (2**i).+ N/ a" _4 V1 R  i" k
  4. >>> math.ldexp(5,5)" Q) g$ y1 G) t' Y
  5. 160.0
    7 f5 n2 Z# f5 |; g' `  b
  6. >>> math.ldexp(3,5)' y' S8 w. ~# {, T: u) d
  7. 96.0
复制代码

  L* E: @/ t. e. G0 `' [" `) }math.log10(x)  返回x的以10为底的对数. l. O+ d  }% \) W
  1. #返回x的以10为底的对数0 B; L5 g7 n" ^
  2. log10(x)
    ; M" `1 q% I' D4 b3 Z
  3. Return the base 10 logarithm of x.
    3 h2 c2 e/ d8 Y! ^
  4. >>> math.log10(10)
    ' }  T. a8 ?  M2 b
  5. 1.0% |. w6 L1 d# m- X9 B- ^
  6. >>> math.log10(100)
    4 m7 E- h5 K5 \, j  i* a% A
  7. 2.0
    & M* y/ o7 d4 ]3 [
  8. #即10的1.3次方的结果为20
    * ~9 d- Y( ]+ i- R1 V' P4 w! q
  9. >>> math.log10(20)2 v, P( t$ P  ]4 l
  10. 1.3010299956639813
复制代码
, M9 E# \6 n+ m  f
math.log1p(x)  返回x+1的自然对数(基数为e)的值+ I" M) Y* I( `0 n0 u  D, y
  1. #返回x+1的自然对数(基数为e)的值
    ! v* p3 L4 _4 G. B
  2. log1p(x)# r' f* D% H9 z8 f- M) `$ U$ a: h; ]# b
  3. Return the natural logarithm of 1+x (base e).
    + [3 m* W; ?2 T9 F: n. g
  4. The result is computed in a way which is accurate for x near zero.% U- X) J) k+ g  s  m# R! x
  5. >>> math.log(10)
    6 O/ v+ o" l  \; _
  6. 2.3025850929940464 t+ q* N& M5 z! ~$ T& H
  7. >>> math.log1p(10)
    / ?- p+ k) w& O' c
  8. 2.39789527279837077 ~: R# y4 O  f6 K0 q
  9. >>> math.log(11)
    ' T# V" ]0 A$ ^( K& g1 A& G/ g( b$ ]
  10. 2.3978952727983707
复制代码
& a+ d5 V& M$ T0 g  z, l* o+ ]6 Y
math.log2(x)  返回x的基2对数! d4 D6 r, m, u  U# u- ^9 C
  1. #返回x的基2对数
    2 H0 H6 K7 U! u4 D0 H1 s
  2. log2(x)
    * \4 g" S; P, K: q2 |3 h; j( ~6 z
  3. Return the base 2 logarithm of x.! y. k$ o: z( c) S' u1 S
  4. >>> math.log2(32)
    : `+ f1 u+ r9 o" p* |# x
  5. 5.06 E0 B& h& |5 c" G# X
  6. >>> math.log2(20)
    * @  i6 \: N- y1 a
  7. 4.321928094887363  v" ~9 a' N( m# X4 G6 p
  8. >>> math.log2(16)7 Z5 _( Q$ N4 M
  9. 4.0
复制代码

' ?( v- s2 p7 o" f2 o& Smath.modf(x)  返回由x的小数部分和整数部分组成的元组
$ w9 w: ^; B8 R" m% P3 m$ @5 l
  1. #返回由x的小数部分和整数部分组成的元组
    : \$ B# }* x! f' c. t  Y+ _
  2. modf(x)
    % h! N: y- J1 ?8 C; ]$ C* w
  3. Return the fractional and integer parts of x.  Both results carry the sign! i. L$ R+ }5 ]- p, y' K
  4. of x and are floats.# ]# Y' X  P" g/ z4 i
  5. >>> math.modf(math.pi)
    ! L, o" F1 q9 q9 c) u
  6. (0.14159265358979312, 3.0)% ^- G" z0 H' O0 @
  7. >>> math.modf(12.34)
    + U# [" a  W4 q& o$ l! b) T
  8. (0.33999999999999986, 12.0)
复制代码
3 \) w8 r/ |+ j$ U4 {0 G
math.sqrt(x)  求x的平方根
4 K/ W0 H+ [+ e5 t6 V# C& @7 {
  1. #求x的平方根
    9 {" b! D! P$ h- e4 ^  U' t
  2. sqrt(x)
    4 J; J# Z% y: {9 S' e1 B
  3. Return the square root of x.! V9 ?: X+ a1 T3 Z( ?: y4 A
  4. >>> math.sqrt(100)/ y" J/ S* ?6 _. C. ~8 M. y# v7 \
  5. 10.0
    ' P' U1 C/ w! s" x5 h% t+ I& j0 ~
  6. >>> math.sqrt(16)) E! ]6 M; s2 }4 ]3 C$ J! v. l
  7. 4.0
    2 N% S( o* k# [' K$ `, T
  8. >>> math.sqrt(20)
    9 w* @0 }6 ^2 ~: X) D( e
  9. 4.47213595499958
复制代码
. a; Y  m0 [8 l" M, w
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
      p1 F1 I3 y# l4 e3 f
  2. trunc(x:Real) -> Integral
    * |( z. D" k1 K$ [" h! D
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ) x6 m! }$ \+ K0 M$ }: ?2 f
  4. >>> math.trunc(6.789)$ q. m8 a. W3 J8 ~
  5. 6
    0 {* o+ G# A2 n# E( W# L
  6. >>> math.trunc(math.pi)
    1 D: R8 K4 e2 s
  7. 3
    6 `- S0 K, W, y6 e2 ~; u# Q
  8. >>> math.trunc(2.567)% [! T6 n9 ]% f/ J5 V5 |0 u
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-18 06:14 , Processed in 0.077401 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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