新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
4 l2 Y1 X( I: a* R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 J; B; ]# l- \# M

' ^1 a2 T& e0 \" s方法1
8 l) i) r0 s9 D
  1. >>> import math0 A1 a. Z9 Z: O9 K2 O
  2. >>> math.sqrt(9)
    8 f" z* P2 v0 T* P
  3. 3.0
复制代码
方法2
4 J9 n& T0 c3 O; h& w) Z4 z
  1. >>> from math import sqrt
    - M2 M$ j9 _3 P' K! `' j
  2. >>> sqrt(9)
    . B& `3 f! m; H7 S8 A5 i; f9 b, _2 G/ P
  3. 3.0
复制代码

' i3 S- v" [6 I: j* s8 P
# M7 V$ o5 [7 u
math.e  表示一个常量0 ^% T2 b7 t) F9 a5 P
  1. #表示一个常量
    , w* R: q: J& D1 L* |
  2. >>> math.e# n& P  u! g. p0 t7 [0 ]
  3. 2.718281828459045
复制代码

( Q# R9 L  j, F8 V# t$ \math.pi  
数字常量,圆周率

0 Q8 d: g" S7 ]" B
  1. #数字常量,圆周率
      K$ K2 M( \, }" j" G
  2. >>> print(math.pi)
    + F# ]7 j3 V! B& d$ H9 C
  3. 3.141592653589793
复制代码
7 C% e5 `/ I! Z" u! G
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

+ D* ?: Q6 J. S  w. F, ?. O& K9 M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 Z: I) Z& a. L9 Y5 E
  2. ceil(x)7 c! Z$ S; _/ w, Q
  3. Return the ceiling of x as an int.
    7 c. q2 I! ^& P3 U+ ~# K- x' P
  4. This is the smallest integral value >= x.
      h+ B* I& j5 R, h, {# Y

  5. 4 G/ i7 v2 x$ L9 T! ]
  6. >>> math.ceil(4.01)7 D! s" C( }' [
  7. 59 l/ u/ I; b8 s% P8 J- ]8 q8 E
  8. >>> math.ceil(4.99)
    5 D; \) U9 ^0 Q) k7 x
  9. 5! x/ ]% N4 p/ ], W( }8 f: o" `
  10. >>> math.ceil(-3.99). g* q4 ?6 R" @$ o; ~
  11. -3
    ' R" M0 n& i( O" C
  12. >>> math.ceil(-3.01)
    : T5 p4 c% {+ N8 K; t. J2 n
  13. -3
复制代码

: h6 E/ D$ M. d) Amath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 q4 {, q/ w/ H) z: L" Y2 H
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    1 ?; @3 j3 c+ Q* H* J7 o/ I
  2. floor(x)
    ! ?3 d& i  P! P
  3. Return the floor of x as an int.
    / O3 U% J" R4 A) E9 o* q$ J" v
  4. This is the largest integral value <= x.6 m8 s7 p! U( ?0 q
  5. >>> math.floor(4.1)
    3 M, ~9 y9 C  A* c
  6. 4
    7 U8 m5 [1 o  n# e5 Q
  7. >>> math.floor(4.999)
    2 f3 r" z" x" o. O9 f% A4 k
  8. 4
    % d1 ]+ `0 g: j2 G& L2 N# E* O5 r
  9. >>> math.floor(-4.999)% S$ f& t7 T* H: i- {
  10. -5; z& C( J" y  d8 O* ?. s6 @
  11. >>> math.floor(-4.01)
    * @# }  N* D; j+ B, k
  12. -5
复制代码

1 w* V. C- F* ^, k5 L4 u" qmath.pow(x,y)  返回x的y次方,即x**y
1 S8 _% {6 e9 t
  1. #返回x的y次方,即x**y
    : H7 Y! c6 T5 ~9 ~
  2. pow(x, y)% t. A- p1 F+ d( u+ L* [& m; M
  3. Return x**y (x to the power of y).8 `- L7 _5 Z2 v+ F
  4. >>> math.pow(3,4): l0 j! F- \+ U& A
  5. 81.08 j$ W! _0 I* [* B* V& G6 {
  6. >>> 4 \& U# U  Q  w
  7. >>> math.pow(2,7)
    0 v, }4 D, d& m9 u" Y6 L4 K" D  B
  8. 128.0
复制代码
2 K, A5 G  K0 z
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* j: o- G, h" h
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    " Z- }" l" ~( Y& \; y( z
  2. log(x[, base])
    # n( X( g2 i4 [. s" E1 [+ G
  3. Return the logarithm of x to the given base.5 p- a) C4 e/ Q0 A: q" ?" F5 v* s
  4. If the base not specified, returns the natural logarithm (base e) of x.
    7 R0 D  J, z$ r) V% z
  5. >>> math.log(10)# y8 g* }2 a3 g% `# I8 U6 }, u
  6. 2.302585092994046
    , X2 a& k8 k+ Z5 X. h+ t
  7. >>> math.log(11)3 y2 L: f+ N5 K
  8. 2.39789527279837074 y0 S2 a' |/ n
  9. >>> math.log(20)
    / ~* v) e6 W4 Z8 U6 T# K
  10. 2.995732273553991
复制代码

  ]; l; w9 ~# J; T9 smath.sin(x)  求x(x为弧度)的正弦值8 v& Q& l6 l: g2 Y, ~; a
  1. #求x(x为弧度)的正弦值
    : c' n9 C! P& e5 R6 n4 m: c
  2. sin(x)
    ( a  ~1 |, x1 a( x
  3. Return the sine of x (measured in radians).* i$ S5 l5 X2 m. t
  4. >>> math.sin(math.pi/4)5 I  ~5 [! D+ S# ?* K
  5. 0.7071067811865475
    0 S% @8 j& D. T2 Z! x$ D
  6. >>> math.sin(math.pi/2)
    6 X: Q1 n# A' f# g# q
  7. 1.0
    9 _6 I0 B$ ^- I8 d  O, y
  8. >>> math.sin(math.pi/3)
    3 P# ^+ I" {; I
  9. 0.8660254037844386
复制代码
9 T& T5 D/ v  R0 n! h
math.cos(x)  求x的余弦,x必须是弧度" O+ o0 J' [; g& Y( C. ~
  1. #求x的余弦,x必须是弧度# Z4 D, L  ~# Q4 Z" u6 N
  2. cos(x)
    , m; V+ t: J4 W
  3. Return the cosine of x (measured in radians).
      O# {- a+ P! I+ y' t
  4. #math.pi/4表示弧度,转换成角度为45度0 m" P9 O; `' S: C  B
  5. >>> math.cos(math.pi/4)
    + D* `7 ~% j) x  S/ q6 o
  6. 0.70710678118654760 n; s- ~) t- a$ [; ^7 ~3 u1 o# `
  7. math.pi/3表示弧度,转换成角度为60度3 J) P9 p; u  I- n; Q3 o, D. z
  8. >>> math.cos(math.pi/3)! R' d0 t  m7 R1 F
  9. 0.5000000000000001/ T3 ?, u; B8 e, ?4 A) j: |  w. r
  10. math.pi/6表示弧度,转换成角度为30度
    $ A5 [0 r# I& \5 e4 Q
  11. >>> math.cos(math.pi/6)# w/ A* G" c2 X6 ?8 s  \0 M
  12. 0.8660254037844387
复制代码
5 H4 b: o' s. Q5 S8 J  K2 _, E
math.tan(x)  返回x(x为弧度)的正切值
9 {" o* l: p3 m  J" r4 ~0 _
  1. #返回x(x为弧度)的正切值
    * I0 u+ s& O  V' z/ b. e
  2. tan(x)
    ) |1 }. ^4 |5 X
  3. Return the tangent of x (measured in radians).
    2 a( f; j0 J0 a- o4 b
  4. >>> math.tan(math.pi/4)9 U: @* _( a2 {; |. N
  5. 0.9999999999999999% w8 {9 M  f# ?- x2 @8 b
  6. >>> math.tan(math.pi/6)
    0 t# E6 w% v/ D9 l- J
  7. 0.5773502691896257
    % V4 w; T; c9 b
  8. >>> math.tan(math.pi/3)( M" b: P+ k( B+ I
  9. 1.7320508075688767
复制代码
! w$ v6 H+ A# ]9 S* X2 r% |! v
math.degrees(x)  把x从弧度转换成角度
1 {, m6 z% L6 c: `* e
  1. #把x从弧度转换成角度8 G9 z3 A/ s, ?, n* {2 H: v
  2. degrees(x)
    5 a, V; E! X& _5 i
  3. Convert angle x from radians to degrees.
    / P$ @6 x9 m+ Q# ?2 M0 K

  4. / L3 B$ k* F3 m* L
  5. >>> math.degrees(math.pi/4)  o  o2 S( J1 }( x0 I4 u+ O& |
  6. 45.0
    , F2 D  E3 A* F/ ?9 w2 S
  7. >>> math.degrees(math.pi)- p- ]( C% h) h  A: \
  8. 180.0
    9 \# a( }- ^: q
  9. >>> math.degrees(math.pi/6)
    : o  ~7 h$ n! f* W( ]7 y1 Q
  10. 29.999999999999996
    / i+ U" U/ w: n3 U; U# [
  11. >>> math.degrees(math.pi/3). J2 x, d0 e# ]; s
  12. 59.99999999999999
复制代码

8 ^( C. p( z3 F( i; s, hmath.radians(x)  把角度x转换成弧度4 C  u( E8 c- b: C& |/ y. T% y
  1. #把角度x转换成弧度! A7 ?4 H6 s3 O( Q1 e0 l
  2. radians(x)
    8 `- s' }' e% }1 N& R% k
  3. Convert angle x from degrees to radians.
    . ^2 O: ]+ N( @( q5 |  g' L
  4. >>> math.radians(45)- p& |$ ~2 u; F6 B5 ~
  5. 0.78539816339744833 L+ {- v0 k: g/ c8 W
  6. >>> math.radians(60)2 |9 Z5 L4 o3 A  X2 i% t1 \# g5 N
  7. 1.0471975511965976
复制代码

1 O+ |: P+ n  xmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
7 \9 p/ |& Y6 S, h& N- E+ ~: {
  1. #把y的正负号加到x前面,可以使用0
    + t$ n4 J5 Q, d8 b9 a7 v& T4 W1 _' @
  2. copysign(x, y)4 h, b9 l7 B8 i: z9 g
  3. Return a float with the magnitude (absolute value) of x but the sign
    8 c1 ?) K0 f5 k( e) y
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    3 H, y- }5 p! J) }
  5. returns -1.0.6 G' E+ ]+ q( C; x3 L( Y' [' [

  6. ! M, h, U/ A" ~/ `1 S
  7. >>> math.copysign(2,3)
    3 l# r, l. k  W* w3 x
  8. 2.0
    . E4 A$ P+ \" X$ ?
  9. >>> math.copysign(2,-3)
    ! {2 B) j% ?* Z! P/ k& z! y1 i
  10. -2.07 m- ?: j; z6 H! @1 u9 n% {) l
  11. >>> math.copysign(3,8)0 b" @3 b  W9 s2 E
  12. 3.0
    % n; r+ |8 t0 m& b; m! y
  13. >>> math.copysign(3,-8)
    7 l/ M. r* N  c5 e4 v
  14. -3.0
复制代码

1 u. [8 O# G9 Y1 p" emath.exp(x)  返回math.e,也就是2.71828的x次方; \  _7 `' A0 p: n# E; n0 m
  1. #返回math.e,也就是2.71828的x次方
    ( N, x& l* N4 B$ F
  2. exp(x)1 ~& T9 C5 F6 n' N% P. ~/ b
  3. Return e raised to the power of x." m6 v  h9 n9 c# F3 u) y

  4. ' y! \" d6 }5 S" c
  5. >>> math.exp(1), K5 w" D) v. S1 I
  6. 2.718281828459045
    , [( Y9 t7 |( l, M* ]( ]% p
  7. >>> math.exp(2)4 k$ ^0 e* O* K. F4 T) X" m
  8. 7.389056098930653 u/ F. Q5 v8 ?, A9 m. ^/ [
  9. >>> math.exp(3)
    ) J, ?, P- |% g8 L+ l
  10. 20.085536923187668
复制代码

, ^7 Z6 U8 l' J4 p$ G- D0 }/ jmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
) V+ y% {( T! n+ b/ C, L  j
  1. #返回math.e的x(其值为2.71828)次方的值减1
      O2 r' T1 G" L" X3 }2 @8 \6 u: i9 G
  2. expm1(x)& S. z4 ?# H: ?* c- t
  3. Return exp(x)-1.
    $ g( m  ~: h  m$ U5 Y# U. L' v
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 I. n) o! I# b
  5. " A: O, \1 g0 r( z% P" n' V- }
  6. >>> math.expm1(1)8 l1 K8 f/ n! s! ~1 L
  7. 1.718281828459045
    ; p, N  z6 H: \% j( q
  8. >>> math.expm1(2)
    1 T: @2 ^6 E% r0 m& g" ~
  9. 6.389056098930651 [! I2 D& Z$ y2 V
  10. >>> math.expm1(3)5 Z2 p+ ?) ~6 r, X; E$ t- p* i& i/ G
  11. 19.085536923187668
复制代码
. P! c3 U; ]; e$ S) r  u( _) \
math.fabs(x)  返回x的绝对值3 z2 W- J0 d3 G5 z! l" D6 G' U
  1. #返回x的绝对值( B! q. A0 q8 ]
  2. fabs(x)8 k5 G. i0 {/ t& \' U% I
  3. Return the absolute value of the float x.
    # a8 m1 m2 L% l& f

  4. - O' o- g6 ~: l/ E! i' T
  5. >>> math.fabs(-0.003)* e* u  U4 Y6 T% {" K; `' M
  6. 0.003) K) A' m3 ^) X
  7. >>> math.fabs(-110)
    ! h% t( w! |' G
  8. 110.0, ^+ j3 J0 E" g6 Y0 s8 K3 }
  9. >>> math.fabs(100). R: b) A! P# }8 h+ N' N6 I
  10. 100.0
复制代码
& l: T$ L* d* O% k
math.factorial(x)  取x的阶乘的值
2 k5 q9 w% ^6 a) o$ F6 b
  1. #取x的阶乘的值$ Z: b: I6 {; ]5 B0 P
  2. factorial(x) -> Integral
    " ]& g" \- K; ^, J6 U$ f  W
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    " @" c  ^  Q( ~# A. t5 @' w( K. y
  4. >>> math.factorial(1)
    ; {& H, k: h& m0 y
  5. 15 P  n4 ]5 M- \$ q6 X! b/ o+ _
  6. >>> math.factorial(2)
    ! l# C) ^5 H" Q# t* o% R
  7. 2
    * s) ~: v+ ?; c9 h9 e
  8. >>> math.factorial(3)
    + P6 h& N2 S7 A2 @: N- @% S
  9. 6" m- w5 e& d: v8 {
  10. >>> math.factorial(5)
    # O% D7 _+ M2 ?0 D  l, `2 a; {
  11. 1201 {! L8 O2 w; M0 I: f/ V
  12. >>> math.factorial(10)3 y) ]& }+ M% B0 v+ ]. v
  13. 3628800
复制代码
1 [9 r- o8 L/ f. p' ], N
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数6 N8 ]/ J5 T# ?2 u
  1. #得到x/y的余数,其值是一个浮点数7 A1 x, y/ Y4 ~7 ^  {
  2. fmod(x, y)" N0 {/ V+ E( f7 U5 e
  3. Return fmod(x, y), according to platform C.  x % y may differ.8 w$ v& E1 x2 X! T. R1 u8 F7 j: Z
  4. >>> math.fmod(20,3)* U: G5 j& Z- ~/ D+ Y. n
  5. 2.0
    6 u4 w& G! g2 ?7 b8 N/ O
  6. >>> math.fmod(20,7)5 Z2 u# k/ g  x
  7. 6.0
复制代码
$ E' b, S% r. q8 k7 ]
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围- h9 H* \7 g+ k1 ~% |
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    3 e, `3 w7 u( B+ Z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 H' D/ n2 i4 C
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 U9 X$ Z" A! A1 G0 P  w
  4. frexp(x)
    0 g% k/ J- W) u# k
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 Z& }4 R1 z. [# S  j' @; r( t6 z* D
  6. m is a float and e is an int, such that x = m * 2.**e.( x) t8 D/ c  t
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.8 d) _( U# u/ g# @: y
  8. >>> math.frexp(10)
    ! A+ [  L3 {1 Y2 i
  9. (0.625, 4)
    , F; V$ r, E7 l& I0 a0 Y4 k
  10. >>> math.frexp(75)) V/ t9 m$ C1 m, Z
  11. (0.5859375, 7)
    9 A! I% \+ }1 U5 o# J
  12. >>> math.frexp(-40)# p( W6 X% `: ?4 e; E
  13. (-0.625, 6)- m* W7 y$ r( V% G8 Y
  14. >>> math.frexp(-100)
    ; M6 ~9 F( Z- N
  15. (-0.78125, 7)0 [8 n) r: N6 y3 v9 i' q1 _" I( D, Z! d
  16. >>> math.frexp(100)9 }2 B3 n8 I* U3 L( D! `2 ^  l
  17. (0.78125, 7)
复制代码

) r+ w9 [( a/ u/ xmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
5 S4 z1 |$ m3 |7 m8 c
  1. #对迭代器里的每个元素进行求和操作/ V& x' j& ~4 R. r! G9 {3 t) a
  2. fsum(iterable)
    7 @- H" G' w. p! e" K
  3. Return an accurate floating point sum of values in the iterable.
    # c- v8 m& i+ [  m6 p. u! D! |6 _
  4. Assumes IEEE-754 floating point arithmetic.
    0 `% L. N! y* E0 ~1 L' F- J* F7 [
  5. >>> math.fsum([1,2,3,4])
    , ]3 p5 R! p! e0 }. @; v
  6. 10.0
    5 R$ L; F. V3 T# j3 {
  7. >>> math.fsum((1,2,3,4)); d1 |: `8 P" }! p3 p
  8. 10.09 y/ L5 X$ q0 J6 V+ i0 V
  9. >>> math.fsum((-1,-2,-3,-4))# u( X  a, w: t9 d* T' X$ P! p- e
  10. -10.0! J5 z: `, I0 U* L( s" v% v
  11. >>> math.fsum([-1,-2,-3,-4])! f; }. ?" E/ y  ]6 n- s5 L. j
  12. -10.0
复制代码

, u7 {7 m" z. f; `$ x" }) h& ~/ imath.gcd(x,y)  返回x和y的最大公约数# U. z$ W' G( T' A# K0 A! n
  1. #返回x和y的最大公约数
    2 l. J" g4 x# Z9 I; Q9 c
  2. gcd(x, y) -> int
    ( Y; P5 ?. y6 p: e; C$ N3 K
  3. greatest common divisor of x and y
    7 ^, f' _" W4 ~
  4. >>> math.gcd(8,6)" T1 ~# ]3 C/ ?' ]! e" F
  5. 23 s9 @. p3 J  g6 Z
  6. >>> math.gcd(40,20)
    " x# x- ^$ D' f) y
  7. 20
    ) V0 h  y3 d) K. q% p( T, }% b; z# `
  8. >>> math.gcd(8,12)
    , T9 x6 y. W. m/ X. `# s' M
  9. 4
复制代码
  e3 W7 |# h  C- Q7 @, X, P
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
) R* i5 t  P; m0 U
  1. #得到(x**2+y**2),平方的值
    % w9 ^  S6 s, N/ i1 e, z' `
  2. hypot(x, y)
    + A4 f" x# x! Q! M
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    : R5 H' \" _0 S$ }* J
  4. >>> math.hypot(3,4)
    1 K; G1 ~" Z. m8 w
  5. 5.02 ]$ c5 {+ z; q1 \
  6. >>> math.hypot(6,8)7 |& j5 y. q* k" k( i/ L8 n
  7. 10.0
复制代码

# x& X" ]9 u1 r2 qmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False. f$ J3 N# z+ R- j
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    : I! K8 Q7 S( d5 x. @
  2. isfinite(x) -> bool9 {" u. w2 W" }; h# p
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    6 y: \" f7 Z8 ^) {. G2 p# b( F
  4. >>> math.isfinite(100)( L1 B+ ^% F5 z6 j2 \: X; C* P1 w
  5. True
    : y5 d; o8 K$ @+ m0 }* S
  6. >>> math.isfinite(0)
    ! \* v. `6 C8 M# W/ m+ _
  7. True
    ' l6 m0 a; u5 M
  8. >>> math.isfinite(0.1)  X% c0 T" A, d% V+ h/ F. `
  9. True, d3 p" q& \$ q$ Q5 K* l
  10. >>> math.isfinite("a")
    4 C$ _( N4 \& x  }$ J2 B% J
  11. >>> math.isfinite(0.0001)- Y& O" y5 ~- ^- d, h: T
  12. True
复制代码
8 M- Z! w5 T( R$ V. W- C" d
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 n. T6 ]6 f7 M" M3 M
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ( d; z6 p1 X  ?$ f* ^8 u
  2. isinf(x) -> bool
    , ]+ o1 g. Z4 W. M9 f9 W% ?" x
  3. Return True if x is a positive or negative infinity, and False otherwise.* D6 a7 f' U$ r4 @# n/ H( y
  4. >>> math.isinf(234)6 {3 h$ U* Q) w; H
  5. False
    9 y1 v" c( W5 |* ~9 _/ O
  6. >>> math.isinf(0.1)
    $ i# k6 J7 h8 u& \1 R4 }" |
  7. False
复制代码
9 E* X4 F1 l* q& v
math.isnan(x)  如果x不是数字True,否则返回False+ N& L5 V2 q7 W. \8 L1 l
  1. #如果x不是数字True,否则返回False
    9 N  x. U( t& P
  2. isnan(x) -> bool7 c& `- t/ i1 i& K' `0 Q& t" R
  3. Return True if x is a NaN (not a number), and False otherwise.& h7 u# c" ?7 c4 C& v
  4. >>> math.isnan(23)# d; y/ G& b! z2 J# }  h0 c
  5. False
    5 Y) u# C( O2 U
  6. >>> math.isnan(0.01). z$ u8 R$ u: I0 q- j
  7. False
复制代码

( b9 q1 |* I& X' ^" Amath.ldexp(x,i)  返回x*(2**i)的值+ y- A4 _3 Z7 i
  1. #返回x*(2**i)的值
    : h, D0 e" P. K) X( [2 o+ u
  2. ldexp(x, i)2 L( b* C% A  g" S
  3. Return x * (2**i).
    . S" g: ]$ @' C+ r8 W
  4. >>> math.ldexp(5,5)
    # b& U* m; v9 }7 i
  5. 160.0
    ; X/ p4 X6 f# o! q! i. m
  6. >>> math.ldexp(3,5)- ^) l' u9 i' @/ ]0 d1 N
  7. 96.0
复制代码

& p: S3 W' E5 v- M5 U; Vmath.log10(x)  返回x的以10为底的对数- l/ P. R% M3 f8 c% [, G6 J
  1. #返回x的以10为底的对数; N, V; O' e% l& e
  2. log10(x)
    & C+ c* X5 t5 V# p! _! H7 d
  3. Return the base 10 logarithm of x.$ s+ R7 B, r/ ?8 [' R$ A; E1 Y
  4. >>> math.log10(10)+ f. o7 \  \+ ?: v  h
  5. 1.0  a, o* ^% s  ?8 r! t/ h5 U
  6. >>> math.log10(100)) b7 g. w3 Q5 h8 _' [, c
  7. 2.0
    % ?  M2 E9 d' F
  8. #即10的1.3次方的结果为20
    5 c$ z' T# P) p9 t3 u* p) u6 R
  9. >>> math.log10(20)
    , S$ |; _, {6 x2 [0 G
  10. 1.3010299956639813
复制代码
+ K, [8 g0 p; j/ X  h+ r' G) Z0 G- d
math.log1p(x)  返回x+1的自然对数(基数为e)的值
) D$ x- z/ B+ x, o
  1. #返回x+1的自然对数(基数为e)的值/ E; H6 U9 J  j/ g/ t% M; D
  2. log1p(x)3 [6 E! E$ a) C& f& j
  3. Return the natural logarithm of 1+x (base e).$ M- h: X8 s% }$ d0 ^
  4. The result is computed in a way which is accurate for x near zero.) B' b+ y9 E  N9 G
  5. >>> math.log(10)
    / Y- Y2 b6 q$ o$ y
  6. 2.302585092994046' q  h  G: Q. V( @3 J
  7. >>> math.log1p(10)5 ?3 ]1 E" K, r; J6 N$ J1 s. H
  8. 2.3978952727983707# z4 ^* k  ~9 Y( O4 T4 F
  9. >>> math.log(11)( o/ F: R" T, @
  10. 2.3978952727983707
复制代码

4 a3 E# t2 R& H6 I, wmath.log2(x)  返回x的基2对数/ B4 \% \# m: \0 ?
  1. #返回x的基2对数% N4 X: F, m: `
  2. log2(x)
    & s* p. C" G/ b' M8 O
  3. Return the base 2 logarithm of x./ Y( y/ [/ U1 f# _" F7 r' s
  4. >>> math.log2(32): U+ j4 D4 [# r9 ?% Z" q
  5. 5.0
    & v  Q- t/ y3 I6 C! q" x/ d
  6. >>> math.log2(20)% V% T1 K* x* y9 M) W2 S
  7. 4.321928094887363
    + k3 U: i7 u! T0 H9 W$ A/ i
  8. >>> math.log2(16); N/ V2 F+ F: L0 s/ {5 C
  9. 4.0
复制代码
+ u8 ?0 n8 `0 n8 j" L. P
math.modf(x)  返回由x的小数部分和整数部分组成的元组: \4 N3 ~. d9 K* Y, o0 B: z
  1. #返回由x的小数部分和整数部分组成的元组5 |% H1 v2 n7 `7 d
  2. modf(x)
    : \& o. d' E4 s1 ^  @2 A8 a
  3. Return the fractional and integer parts of x.  Both results carry the sign5 Z, w; ?& n7 C6 ?' K3 M
  4. of x and are floats.( A+ t2 c% a0 M, }! ]
  5. >>> math.modf(math.pi)3 N& j7 U6 `& i- A! s5 r3 }
  6. (0.14159265358979312, 3.0)
    ( k$ V/ W* ~3 B0 ?
  7. >>> math.modf(12.34)
    " E! x8 l* Q9 u1 D
  8. (0.33999999999999986, 12.0)
复制代码

9 _& _" v  U0 ]- q5 f3 rmath.sqrt(x)  求x的平方根2 |3 Z% o/ m% Z" y0 B, r
  1. #求x的平方根; \, A" F+ F+ l2 f% l; v# O
  2. sqrt(x)' W- _* H0 V4 G
  3. Return the square root of x.
    ! V7 C* w# `' V& z
  4. >>> math.sqrt(100)' X7 S* n2 a6 @! R+ t, ^  H: s+ A$ n
  5. 10.0( U0 ~! v. y! R  ]& z+ d1 Q
  6. >>> math.sqrt(16)
      m- N' O( J) f" ~
  7. 4.0$ Z  L1 M& L1 P0 U
  8. >>> math.sqrt(20)1 I" W; Y! B1 Z
  9. 4.47213595499958
复制代码

5 U# X% O1 ]# g& H9 C! Gmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分/ O+ z9 w) p2 c# E" U8 D+ I
  2. trunc(x:Real) -> Integral
    1 F) Y6 H4 j% U# _! b9 d$ D2 R, D# n2 e
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    9 u2 Q7 j8 a& `4 L
  4. >>> math.trunc(6.789)3 N' O' t# t! F) k
  5. 6
    ( l; j% z" I6 ^1 o3 |
  6. >>> math.trunc(math.pi)
    1 `7 n* _( Q; ~& A7 ~" [
  7. 3$ `) A& a0 B' E" l3 q
  8. >>> math.trunc(2.567)
    5 M, N4 N9 Z" L/ i
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-4 09:14 , Processed in 0.083892 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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