新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

" |, o% F1 N6 m0 v【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ [& e9 n4 O+ k; m: n( p5 }
  s  B+ F6 A0 s* n. _$ L  ~. G
方法1/ y* t$ Q. E! K" G8 p
  1. >>> import math/ e+ D% N& ]* r4 E1 A7 \; I
  2. >>> math.sqrt(9)
    . \( o9 y4 w* z7 C8 a2 A1 `4 X3 g
  3. 3.0
复制代码
方法2
2 {" e2 R# x- O+ x, e% r
  1. >>> from math import sqrt3 w: w9 |- _. T2 g1 n
  2. >>> sqrt(9)
    ) b; B! R9 U8 |0 |
  3. 3.0
复制代码
5 r5 C& w3 ?* J: K- U" q* t2 l3 e


- ~4 L5 V' S/ _4 E$ p9 n' ?' n
math.e  表示一个常量
. P! E3 R& u4 I. y2 w& Y
  1. #表示一个常量
    / F* Q* p' p2 m# i6 d: c1 @" E
  2. >>> math.e' c3 u+ |0 X- j' d, V6 ]1 R
  3. 2.718281828459045
复制代码

: J1 N3 B0 c8 s* bmath.pi  
数字常量,圆周率
" W& C5 C% J! c% O
  1. #数字常量,圆周率1 t1 I% J3 b, r* G
  2. >>> print(math.pi)
    , F7 x& k% }' D7 A' F
  3. 3.141592653589793
复制代码

7 Q8 n% I4 J; Z5 ]+ ~8 l& z, ?& [math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

" I8 e0 Z6 f; `! O8 @$ R+ u" g6 M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x; f7 j* i* h( T8 T
  2. ceil(x)
    % `; n4 ~1 I" V" {$ @) m9 n! @
  3. Return the ceiling of x as an int.% e1 w5 B/ u" q1 u& R* o7 \- B
  4. This is the smallest integral value >= x.
    + z  T. `. L" _2 ~4 D7 B/ I" S
  5. ' s: A5 ^- W+ p2 N
  6. >>> math.ceil(4.01)
    $ p- t* I, k5 h. n
  7. 5
    * ^  ?5 N2 b! E$ N1 h0 m
  8. >>> math.ceil(4.99): K9 x' @  k8 H8 H- r( ]+ {% |
  9. 5& ^0 T: G5 a4 n
  10. >>> math.ceil(-3.99)
    + `8 y$ W; u3 E% l" R
  11. -3
    + {6 O7 ]7 q& R) |; W; k" M
  12. >>> math.ceil(-3.01)
    $ d" h4 F8 l6 A' V! `5 V
  13. -3
复制代码
% @/ B% j7 p& D9 s6 H6 B
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) N1 X6 d2 K( {  v1 {7 Y
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 q' R( U6 f$ z! |, l
  2. floor(x)4 f3 Q; i, z  n1 D3 q7 n* {  _
  3. Return the floor of x as an int.
    + }" j5 n4 ]' O- b
  4. This is the largest integral value <= x.
    6 S; X! |! }1 f
  5. >>> math.floor(4.1)
    ' h. F1 T- }5 }1 T/ \3 Q" U
  6. 4
    $ E. o* |; L' [7 @  c
  7. >>> math.floor(4.999)+ G; U; L* ?. d4 d
  8. 4. i1 `1 l, t( {9 t1 O
  9. >>> math.floor(-4.999)! i4 F& w0 H  c) b( J0 f
  10. -5
    & p1 ~- x) c8 P4 x5 p
  11. >>> math.floor(-4.01)
    : S9 u! ]% O: q6 k! J5 w" ~2 k
  12. -5
复制代码
& O% ^: L; k! [$ A
math.pow(x,y)  返回x的y次方,即x**y
0 ^  I4 ?% w8 ~% B. Y. M8 D
  1. #返回x的y次方,即x**y  Q) Q! l: `& l$ A- k+ D
  2. pow(x, y)3 Q2 [4 k% x7 e
  3. Return x**y (x to the power of y).2 k  c+ {" a, \: u8 i6 l+ j: E; t
  4. >>> math.pow(3,4), |7 [* `% i; H/ ^: r0 q( d4 q8 P
  5. 81.0
    9 R) R9 M- P# G6 Z
  6. >>> , m  F# i% A# R1 L9 m+ u: e
  7. >>> math.pow(2,7)
    / j; C1 v( Y: z) T7 P5 Z& |& _
  8. 128.0
复制代码
# H- h9 d$ E/ s- q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# B1 O$ Z' B) ^5 J8 Q4 c) J+ I) p
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 U9 ?/ @: r' Y
  2. log(x[, base])6 W4 a( R/ J, i/ ]  P# H; P
  3. Return the logarithm of x to the given base.( B* Z7 j& Y7 c6 y2 x
  4. If the base not specified, returns the natural logarithm (base e) of x.
    8 w. H% X5 z  X0 {$ @; k
  5. >>> math.log(10)
      ]" w& i3 ?- l8 E2 Q
  6. 2.3025850929940462 G1 [  I- L8 ?( _
  7. >>> math.log(11)2 b& O+ U; q+ _* i$ V% K. @/ {
  8. 2.39789527279837074 Q3 \  W5 X. R
  9. >>> math.log(20)
    5 h; l4 w: K7 O) m) O* H
  10. 2.995732273553991
复制代码

  [, G: x7 o- ^! E# f' O. h* xmath.sin(x)  求x(x为弧度)的正弦值: u  d' X2 D4 |) b+ I( g
  1. #求x(x为弧度)的正弦值
    - {; y) ^, e3 D
  2. sin(x)
      i1 f, c, s" g# C& }
  3. Return the sine of x (measured in radians).
    ! b/ l4 D0 u+ |+ \
  4. >>> math.sin(math.pi/4)
    + j5 f% Q% ?, Z& R! w. n
  5. 0.7071067811865475
    - `- q) V# d; s; P- k
  6. >>> math.sin(math.pi/2)
    7 t3 D; |' g5 C9 K, q1 s
  7. 1.0
    % i: o9 H; {3 i' ?. o
  8. >>> math.sin(math.pi/3)
    $ p: ?3 y( d5 _
  9. 0.8660254037844386
复制代码
' F7 |% f4 d3 S
math.cos(x)  求x的余弦,x必须是弧度7 z. k3 A0 C# [
  1. #求x的余弦,x必须是弧度
    . G  m; @* G. q. \, R7 r3 W4 k! ]
  2. cos(x); s5 k' F: u6 v* Q1 K5 ]3 s
  3. Return the cosine of x (measured in radians)./ F/ S9 K! I" B% P2 r
  4. #math.pi/4表示弧度,转换成角度为45度. y% Y0 @5 W/ R3 ^# X
  5. >>> math.cos(math.pi/4)
    2 {, W7 _- @2 ?
  6. 0.7071067811865476' \2 k+ h' c# r
  7. math.pi/3表示弧度,转换成角度为60度
    6 K( _1 x; R) y, x
  8. >>> math.cos(math.pi/3)
    1 A/ y+ X$ h4 p; s5 y) ?) K. ^& |
  9. 0.50000000000000010 [3 \% ~1 N+ k  ]4 l: O% ]  Z& [
  10. math.pi/6表示弧度,转换成角度为30度* @+ C9 L  W" F( B2 f% g' s
  11. >>> math.cos(math.pi/6)8 z* M- N; l  G" ~
  12. 0.8660254037844387
复制代码

: O+ e$ }0 l; j) _1 n* N. a* s' Qmath.tan(x)  返回x(x为弧度)的正切值. q2 F9 P# N/ j& G9 ?
  1. #返回x(x为弧度)的正切值- Y2 A6 K. k  J+ C
  2. tan(x)
    1 d! C6 ]9 G/ u( n9 w7 b" l! n# h6 K
  3. Return the tangent of x (measured in radians).
    5 \0 V+ m1 k! G0 H, r
  4. >>> math.tan(math.pi/4)/ i6 P* f8 X  Q
  5. 0.9999999999999999
    + M# T, R# H( B$ R. l
  6. >>> math.tan(math.pi/6)
    ! U% q- Y8 h5 b* U( g
  7. 0.5773502691896257* W) u% Y; _$ @4 M: q: ~. l
  8. >>> math.tan(math.pi/3)0 c, L- k2 e8 p6 M7 `2 g$ e: W
  9. 1.7320508075688767
复制代码
+ T) ]; @7 d0 _8 [. Y- G( ~
math.degrees(x)  把x从弧度转换成角度; `3 C/ x$ _/ f9 v
  1. #把x从弧度转换成角度
    ' i$ {) U. g* y. R
  2. degrees(x)
    / O/ p; c2 W( N" r2 l# t: q7 M( |
  3. Convert angle x from radians to degrees.! X% z) [* ]% \/ w6 F4 t' k

  4. 9 j8 k2 z# m( A$ s4 D# M7 {7 h- e
  5. >>> math.degrees(math.pi/4)+ R! B* M- Z+ _3 R
  6. 45.0
    : B$ O! H) A  K3 v/ b7 f
  7. >>> math.degrees(math.pi)- D/ m! S0 n' a" I3 c
  8. 180.0  x( r7 w6 d( U. m
  9. >>> math.degrees(math.pi/6)( Q# Z0 A( p) M- ~5 A5 O% M
  10. 29.999999999999996
    2 G0 \2 b; i9 B2 ~! t
  11. >>> math.degrees(math.pi/3)
    * C1 Y- c1 z  `3 v) f+ j7 D0 j5 x
  12. 59.99999999999999
复制代码
$ d+ k) }. i- o' g6 ?
math.radians(x)  把角度x转换成弧度
4 s( T- B; u' k$ ]1 ~
  1. #把角度x转换成弧度
    # K% M4 l" ]8 y$ M! ^2 l9 f
  2. radians(x)
    4 Z% r7 @8 t3 v$ q. W
  3. Convert angle x from degrees to radians.$ Y) p) O% z  L
  4. >>> math.radians(45)
    , d6 _) S1 q6 _4 B. C# A
  5. 0.7853981633974483+ Z8 N( b* j# W) _' u5 {* a
  6. >>> math.radians(60)
    & V7 P3 o/ [& {* F
  7. 1.0471975511965976
复制代码
3 ^/ P$ |5 b' _" l0 Z3 X6 J
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
  z- }0 ]! ^1 K
  1. #把y的正负号加到x前面,可以使用0
    # y4 \; }( M' l% V! w  P$ U: J
  2. copysign(x, y)
    - S+ O; m; |  k, a/ g$ ~
  3. Return a float with the magnitude (absolute value) of x but the sign
    * |. v9 [% A# [8 Y1 V' V! |
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) , G, X/ z3 l- J; V1 e" G2 M
  5. returns -1.0.7 ]' [, _8 W5 ?5 e; a2 [. Z+ v

  6. , o) _7 ?0 A9 y! ?( {
  7. >>> math.copysign(2,3)
    4 z3 J, x. F. V8 R* s8 [
  8. 2.00 Z, f, _/ M  f0 @6 G( Q5 m
  9. >>> math.copysign(2,-3)
    + D; f8 I4 ^6 s; n
  10. -2.0, E) m8 \' O& c# ~( T2 [
  11. >>> math.copysign(3,8)
    2 x: @8 G( a0 _6 X
  12. 3.05 |9 x: R4 U5 Y$ \1 q+ N5 f
  13. >>> math.copysign(3,-8)
    0 t3 a! P, [# w' x2 H
  14. -3.0
复制代码
! @. l5 [- g5 m& m
math.exp(x)  返回math.e,也就是2.71828的x次方. y! ]5 {3 L: L- k
  1. #返回math.e,也就是2.71828的x次方
    ; H; w# f2 ?" ?( K, l( l4 D2 Z
  2. exp(x)  A" R# @6 R7 o/ N$ e# L
  3. Return e raised to the power of x.
    9 F& ^' K) Z: Y
  4. & O& z% H' u' b/ u% b8 Z" x6 w
  5. >>> math.exp(1)
    7 M( n  z$ X1 E  [, x( p5 ^
  6. 2.7182818284590451 Y+ T+ S( N1 w/ S2 A- u- b* L5 @
  7. >>> math.exp(2)9 z/ w* E. S2 i' C6 K4 r$ N
  8. 7.389056098930655 T* b& s0 L; t( {- T$ @
  9. >>> math.exp(3)
    * I0 m. I( `" e0 l7 @: @6 T$ {8 ], e
  10. 20.085536923187668
复制代码

2 }* n; Z, s/ [math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1- D- x% H: w3 G* P' i8 R
  1. #返回math.e的x(其值为2.71828)次方的值减1# |5 y- j* Q; I+ D- D
  2. expm1(x); X) d  u- n3 [+ L
  3. Return exp(x)-1.! E2 L3 [: g& z* P3 z+ G# c- a
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    8 S+ D9 I+ z( p( J  T9 b* ?  O
  5. ) K3 s' _' M) N- L4 ~) m! x
  6. >>> math.expm1(1)& q) z  P1 l5 n
  7. 1.718281828459045
    , ?4 x7 E7 f0 |( n4 r5 I
  8. >>> math.expm1(2)- O4 O0 @' o" [+ g% V7 e# `
  9. 6.38905609893065# H- f# y$ {9 W5 m% P) z& h
  10. >>> math.expm1(3)$ {8 S& X2 t* X
  11. 19.085536923187668
复制代码
8 k* \; K+ O+ q4 Q! ?
math.fabs(x)  返回x的绝对值
+ U1 S  b  K* W6 ~( H- |- H
  1. #返回x的绝对值
    , a7 t5 Z/ W. d& v' @- p4 D! L
  2. fabs(x)
    # X5 D1 Y3 Q- c: g. P
  3. Return the absolute value of the float x.0 ~1 L% I$ |: X, D5 g3 i

  4. 6 V+ b3 O+ v' R4 W6 M
  5. >>> math.fabs(-0.003)
    0 @* Y! B8 N5 V5 v! ?% f7 `
  6. 0.003
    4 C, X' }; g( _1 X2 U
  7. >>> math.fabs(-110)5 y) X7 X: x  [' ]
  8. 110.0
    # @9 t, Q: S# c+ s
  9. >>> math.fabs(100)
    0 E2 v7 G5 A" S* y4 Z, g
  10. 100.0
复制代码
& B- @" z! C% d7 ~: ^
math.factorial(x)  取x的阶乘的值; t0 C: i* q( G3 U# Y! T! S$ q! `
  1. #取x的阶乘的值
    ! b9 Q6 z2 Z8 D6 j+ a. z
  2. factorial(x) -> Integral- B( `% F- H* \7 `" I' i/ p
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    . m0 |( k8 v4 Q1 x. h( D  [
  4. >>> math.factorial(1)$ O5 ?/ o2 s: U1 m, F. k: K1 a) p
  5. 1( K  H6 B: m7 Z* A, S, m9 b
  6. >>> math.factorial(2)
    * S$ d2 u) g" n0 O+ \6 m$ D
  7. 2$ x% l9 Q7 E6 k+ s. A6 `' J5 C
  8. >>> math.factorial(3)! P; L4 G0 {- N% {
  9. 6. Q  x2 e& [+ \5 u
  10. >>> math.factorial(5), \+ R! i$ p& l9 ^# {2 u3 d
  11. 1201 E$ X* i: u( F4 O% @6 A( r
  12. >>> math.factorial(10)" U3 z8 b$ L  s8 Z
  13. 3628800
复制代码

5 v1 P: n; t% u$ |math.fmod(x,y)  得到x/y的余数,其值是一个浮点数/ h4 s! t6 W0 q$ D4 j, I
  1. #得到x/y的余数,其值是一个浮点数
    + H6 i2 D4 A! s9 g0 M) y5 t
  2. fmod(x, y)
    1 b* t2 w3 c, j1 w. k1 f/ {
  3. Return fmod(x, y), according to platform C.  x % y may differ.' @4 V5 d/ i6 ^* v
  4. >>> math.fmod(20,3)
    7 P0 d$ L& F: W
  5. 2.0
    " @3 H! V' @( d+ t' z8 \  g
  6. >>> math.fmod(20,7)/ ]  O2 _3 k9 b% C3 O
  7. 6.0
复制代码
6 i& Z1 `) S4 h4 ]7 k% V
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
  D4 V0 E3 |# F1 Z8 t
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( M4 M3 K6 m, X& O% S& x2 t6 S
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    & j9 T6 d2 n$ ^
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    0 |( J$ ^: i; ?+ e0 [  j
  4. frexp(x)
    4 \- N3 M9 m2 U
  5. Return the mantissa and exponent of x, as pair (m, e).
    3 c% @$ A3 C$ H0 Z
  6. m is a float and e is an int, such that x = m * 2.**e.
    2 a5 D0 x4 U' q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    : v3 G) @; i4 s. c' [
  8. >>> math.frexp(10)
    & T3 X' v4 J: x3 h/ K; x1 s& t& T% _
  9. (0.625, 4)
    & _: X- Q3 S5 R; r- ~( f/ ~  X
  10. >>> math.frexp(75)3 Z1 S* Y% Y. F: _5 Y
  11. (0.5859375, 7)( V; P6 Q; M( m. ^" G; K" c
  12. >>> math.frexp(-40)& ~9 t7 @, U+ v% q/ r% V& _" Z& o
  13. (-0.625, 6). d; F5 g0 G, \6 j
  14. >>> math.frexp(-100)
    9 O( q# X# m( |, O: d$ w
  15. (-0.78125, 7)1 S% a6 \5 u1 K) v9 z) P" a
  16. >>> math.frexp(100)
      }0 a! @+ A, k2 l( T
  17. (0.78125, 7)
复制代码
% s( m! h, k* ^# W
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列  o2 t9 ~* w) @3 I4 h( ?
  1. #对迭代器里的每个元素进行求和操作
    2 D/ u$ e3 O( e( b8 x
  2. fsum(iterable)
    : M, n! h6 G& A: Y) m; t* L, N
  3. Return an accurate floating point sum of values in the iterable.+ @. g& u. t' x0 \5 J; \. J
  4. Assumes IEEE-754 floating point arithmetic.5 l. y# i. Y  H6 s
  5. >>> math.fsum([1,2,3,4])
    - j5 B+ w5 c7 m) T! n* _. ?( k
  6. 10.0
    - T) c- [  [7 n7 F8 C$ @
  7. >>> math.fsum((1,2,3,4))4 Y; f4 n6 G. Z# T! @, ?' @
  8. 10.0
    + L$ G4 n  ]4 m- |; P
  9. >>> math.fsum((-1,-2,-3,-4))7 f: M% r* g$ J# R6 q* K: p: i
  10. -10.01 X$ V4 d& ]4 x# A  |( D" t: T
  11. >>> math.fsum([-1,-2,-3,-4])
    , ^2 t& E% H4 ^; C2 K
  12. -10.0
复制代码
0 T2 K: t. B1 K1 m, D
math.gcd(x,y)  返回x和y的最大公约数0 H. c' A! E1 {: N. Q
  1. #返回x和y的最大公约数* z  R8 g# A$ x4 F+ ^
  2. gcd(x, y) -> int
    % |% g" A3 T) l/ _! K/ t: H! z7 ^( ~) x
  3. greatest common divisor of x and y
    6 b/ [0 [2 C* K1 |9 q
  4. >>> math.gcd(8,6)
    , x  V% d' f0 t
  5. 2& x0 _: Z) u/ c7 h2 q! ~
  6. >>> math.gcd(40,20)( m: l5 \: P& t7 O
  7. 20* t6 d, b( s: f3 s% j4 D! m
  8. >>> math.gcd(8,12)
      R* [1 u/ i; G9 w" T( k  [+ {* L
  9. 4
复制代码

7 I2 v" B  ^  [6 g$ Wmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
6 O/ s. U& m) O4 Z
  1. #得到(x**2+y**2),平方的值; z! M5 U2 ~4 w, v3 q% }4 `
  2. hypot(x, y)
    $ m1 O) s! E8 j7 `& ~
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ) U7 e6 n8 J1 |2 A
  4. >>> math.hypot(3,4)
    : u. W3 R$ ~! e" d
  5. 5.0
    # C; X% O& H+ e# T! K
  6. >>> math.hypot(6,8)
    * }* E5 _4 |4 E/ W
  7. 10.0
复制代码
, q  a6 t# z4 H6 S$ P
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 H' F. ]0 ?" H2 n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    / O( ^2 t1 U% D  r9 d$ K7 `
  2. isfinite(x) -> bool3 R8 t8 D/ x* C- ^# E  }- l
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.4 I6 m; O5 U' `6 i2 l
  4. >>> math.isfinite(100)
    6 E. m, h' Q4 G7 a; N
  5. True( ?4 [( n( O: O! T$ C
  6. >>> math.isfinite(0)
    / o$ H4 }. i3 S! s
  7. True
    * B9 I8 Y* i# j
  8. >>> math.isfinite(0.1)
    + P8 q5 T. f9 x, p! Z
  9. True2 I: }* A3 i. o+ P; m5 p/ `
  10. >>> math.isfinite("a")
    $ q8 m7 F+ {, N, o5 n
  11. >>> math.isfinite(0.0001): f, k( l$ F" T) u
  12. True
复制代码
- W$ m6 }2 f, C2 N
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False$ A, `' [% s# b0 e
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    2 j0 [: `0 I- {/ _" i
  2. isinf(x) -> bool, `* O6 i8 x. i5 w: R5 B$ A/ k
  3. Return True if x is a positive or negative infinity, and False otherwise.
    4 ~' D+ H/ ?. m/ K
  4. >>> math.isinf(234)$ s, |  O' l& F) c( \* b
  5. False
    5 v' L; U3 _8 Z3 S/ I) n: V
  6. >>> math.isinf(0.1)7 K% @, u% i0 `9 ~1 X( _
  7. False
复制代码
2 g! c, ?. I$ S9 ~9 r
math.isnan(x)  如果x不是数字True,否则返回False& ?; b) G2 s# J3 G5 v
  1. #如果x不是数字True,否则返回False
    8 @5 ]/ ?7 z  o: X: K* P
  2. isnan(x) -> bool# U4 l/ \- d- J  v
  3. Return True if x is a NaN (not a number), and False otherwise.+ z, Y2 j* x% Y
  4. >>> math.isnan(23)! _& s$ z& ~! e9 [" _' Q! c7 E
  5. False
    : s4 L) N# a8 Q& d/ |6 Q
  6. >>> math.isnan(0.01)4 F1 H+ [% x. |8 e4 @
  7. False
复制代码
& v+ W- y. y* F$ M5 E
math.ldexp(x,i)  返回x*(2**i)的值! V5 Z9 x- B1 w: h0 X8 v, P' N
  1. #返回x*(2**i)的值
    ! V. M! }) `9 k% f' Y
  2. ldexp(x, i)7 b/ q" ]6 M' {, U. B! B
  3. Return x * (2**i).
    ( ~5 p7 B4 f! Z( v7 q( u+ p
  4. >>> math.ldexp(5,5)
    ( Y. A5 e! @$ d7 I& u, d) }, O( Y
  5. 160.01 U2 r, [  s* N& u$ ]
  6. >>> math.ldexp(3,5)
    $ Y6 c# l+ ^+ l/ o
  7. 96.0
复制代码

/ J! ?6 a6 o9 ?! m, o! Umath.log10(x)  返回x的以10为底的对数* n! O+ D4 M9 p  ]! S3 E5 ]4 f
  1. #返回x的以10为底的对数
    2 B1 k9 m6 M: w% S0 ^
  2. log10(x)* b* I' f6 Z! H% @0 p* B
  3. Return the base 10 logarithm of x.
    8 K0 T* v* w( k' r: [, A
  4. >>> math.log10(10)
    " M$ V5 {, N% `
  5. 1.0
    " s5 Z* B" S' s4 ^2 N  V- m2 k
  6. >>> math.log10(100)' G: p3 |# u7 @
  7. 2.0
    0 V) E8 @5 v+ D' ^4 I% B& c
  8. #即10的1.3次方的结果为20
    * E3 U  c7 s+ b# J
  9. >>> math.log10(20)2 t" r8 \8 a2 p. W/ u
  10. 1.3010299956639813
复制代码

. r; G% K! b' v2 D) y2 Fmath.log1p(x)  返回x+1的自然对数(基数为e)的值8 m$ f, v( r! ^5 x& O
  1. #返回x+1的自然对数(基数为e)的值
    ! N+ l9 w! ]' K" w! O0 a) f0 i
  2. log1p(x)
    , }( o/ U/ x# z4 z) q/ f
  3. Return the natural logarithm of 1+x (base e).
    & n" m8 t) m; v; r' U8 X" R
  4. The result is computed in a way which is accurate for x near zero.9 d9 y) P. R! I9 b2 G7 o' N
  5. >>> math.log(10)
    1 z6 p% C. K9 x9 c6 u" w' N4 h
  6. 2.302585092994046
    9 r, l7 E( i! R) t
  7. >>> math.log1p(10)
    9 ^3 `" I) K/ @
  8. 2.39789527279837079 t  |  r4 m% P( d
  9. >>> math.log(11)- K7 t$ Z/ v, {# i% Z# Z4 j4 ?
  10. 2.3978952727983707
复制代码

1 J- r, L2 t& Y2 |9 G4 Vmath.log2(x)  返回x的基2对数* p$ }5 A/ m1 V$ G$ s( u- L9 S; T
  1. #返回x的基2对数- ?1 @0 Q" P! f0 Y% ~5 Z( P
  2. log2(x)
    ! r7 c2 v' P" |" H( @# x
  3. Return the base 2 logarithm of x.
    7 Y' m* t( z  p9 t
  4. >>> math.log2(32)3 F. o+ Y, }6 ?! S) M+ N
  5. 5.0: ?0 @. F0 r! {+ z
  6. >>> math.log2(20)! y! V, C1 o7 B' N8 x" n! n
  7. 4.321928094887363
    ; S  j$ |" v* u$ D3 v
  8. >>> math.log2(16)
    ' ]" w' ]# j8 n. M
  9. 4.0
复制代码

; `/ |  t: W4 f& C& w# tmath.modf(x)  返回由x的小数部分和整数部分组成的元组
6 y2 p$ e- X& |
  1. #返回由x的小数部分和整数部分组成的元组+ D8 J6 H' _; M3 b, H( U
  2. modf(x)7 Y3 G$ d+ }# q* s  I& N
  3. Return the fractional and integer parts of x.  Both results carry the sign. E. i* {; s4 O3 J
  4. of x and are floats.
    . B7 K0 A) o% L+ A* l0 C* S) O  ?+ s
  5. >>> math.modf(math.pi)
    ) B9 l( T5 ~0 L) J
  6. (0.14159265358979312, 3.0)
    ' k6 @+ K) d6 o* I
  7. >>> math.modf(12.34)
    3 L3 v! O2 E/ S' O8 C5 B
  8. (0.33999999999999986, 12.0)
复制代码
% {" F! o) s* g3 L; x7 O5 p
math.sqrt(x)  求x的平方根+ A! z& \: N9 N. ~1 g
  1. #求x的平方根$ C% h( ]! Z  t1 O
  2. sqrt(x)4 `' [& ^* h- L- c! g/ w
  3. Return the square root of x.4 L  z6 O' u! @# p) Q7 U& c
  4. >>> math.sqrt(100)
    0 e. Y! L* u8 T& d  m  C
  5. 10.0' D. S8 F2 l# P- V+ U+ [
  6. >>> math.sqrt(16)" {, `$ ^# F% m/ g/ d8 u2 z
  7. 4.0
    : p/ A* C3 F; R/ L& d# R- g! b) @
  8. >>> math.sqrt(20)
    7 [: V; @3 w3 H" |
  9. 4.47213595499958
复制代码

/ |* {( v6 t% x' [, z$ h, K, n- U! Bmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分: r3 y0 A! x, K/ p: C  }( J$ t% i
  2. trunc(x:Real) -> Integral, W* {3 {1 X5 v% r$ ~' Z3 \- d% q
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method., K4 C. v0 Z# J6 W5 |
  4. >>> math.trunc(6.789)
    : a* q6 N6 U$ p
  5. 61 ^$ V# y3 M1 l8 O* R
  6. >>> math.trunc(math.pi)$ A6 K2 c6 w5 p8 ?: t$ ?
  7. 3, G+ ^. Z6 s6 X1 Y. O! X
  8. >>> math.trunc(2.567)% j) o* q" J3 }$ Z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-4 18:57 , Processed in 0.107668 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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