新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

. g( @# e0 b0 S; T0 }% n, P; e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# Q* Y7 n5 T' j( U$ _0 ]: b

8 K/ a* T# }8 J. r3 f. f+ ^' |! B7 v6 y方法1' |2 y5 k! k( n  M2 a% k+ \0 R
  1. >>> import math
    ' z  W  \( k* ~% v0 i
  2. >>> math.sqrt(9)1 J; i5 t6 r3 {) X& y, M
  3. 3.0
复制代码
方法2; t1 z+ _* P8 q9 F  |
  1. >>> from math import sqrt
    1 o+ P) D" g" c( {4 Q$ p! l/ V! l
  2. >>> sqrt(9): i; {# K2 h* G! F& U+ s% Q
  3. 3.0
复制代码

- ^# y* E3 s* x& g& p

. u2 k) i1 R# X( k' d4 l
math.e  表示一个常量3 B8 |, k. I) V
  1. #表示一个常量
    2 d, A; A1 n8 M4 V7 g8 H4 S) W* \
  2. >>> math.e$ \5 z6 j, r. o; N$ h8 W, ^" [% n% h3 w
  3. 2.718281828459045
复制代码

% F% p0 m6 u; d( q8 y" Rmath.pi  
数字常量,圆周率

! N9 D, y) K$ j( T2 f. f2 y
  1. #数字常量,圆周率
    9 I, S' Y4 q# r* p
  2. >>> print(math.pi); K# j2 \5 p5 @1 U
  3. 3.141592653589793
复制代码

' z' x, p6 M4 w# k& \math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 T& \' n$ {. b' O! W7 e2 P
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x  f4 j2 E0 a4 c' a3 I
  2. ceil(x)
    & m6 r8 s: g2 m1 W0 C% f7 Y6 f$ b
  3. Return the ceiling of x as an int.
    3 c, p( X# w- Y% c7 K
  4. This is the smallest integral value >= x.* X0 t' Z: \0 v1 {& `6 W1 w1 M
  5. & @! D7 I- n& M
  6. >>> math.ceil(4.01)
    1 b! Q- g# ]; P
  7. 5) n9 W. y, D% ?# m! L
  8. >>> math.ceil(4.99)8 [; y  f0 Q/ ^( a3 H
  9. 5( }( z- J1 [8 i
  10. >>> math.ceil(-3.99)
    ! \+ l( b6 S( d  Y$ w+ u/ V( b
  11. -30 a! ?$ _6 x, h# H
  12. >>> math.ceil(-3.01)
      k% m% V" X& w& M9 x
  13. -3
复制代码

/ b0 `% w3 x' s! Y# Zmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
2 ]- c- c1 l; c0 j4 w# B( w2 a
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      W" s* u6 Y0 f- \
  2. floor(x)/ J" y5 N) F6 n3 F( n' Y" V: [8 n+ [
  3. Return the floor of x as an int.
      e# l* q: ?1 e( D# {/ A2 Y! X
  4. This is the largest integral value <= x.1 R) O- T$ G& e7 B7 L) R
  5. >>> math.floor(4.1)* \3 [: q$ N/ A3 a
  6. 4
    % r$ n/ X7 A* B) M7 P+ ?* T
  7. >>> math.floor(4.999). O$ c* m# S  T
  8. 4. I, D' N1 f0 v6 W$ Y
  9. >>> math.floor(-4.999)- S1 F& Q; d! @, k) w
  10. -5- @: g# ~7 n5 H, X) H! \
  11. >>> math.floor(-4.01)) U+ `- I7 x9 E1 ~! V2 p
  12. -5
复制代码
5 ]; `% w# q" j
math.pow(x,y)  返回x的y次方,即x**y9 `1 O$ c) |, G2 j& X% s+ R: ?7 x/ a
  1. #返回x的y次方,即x**y* }5 ]+ |6 r$ A7 [9 Y$ K
  2. pow(x, y)
    0 D7 c5 z/ W- Q' j7 J
  3. Return x**y (x to the power of y).* N& D- }  L& w
  4. >>> math.pow(3,4), T8 m* h2 ]3 ^- Z% S+ l: D
  5. 81.0
      w# u' c5 a' _; x7 s
  6. >>>   @! r' z) E' @" a. p- {8 A
  7. >>> math.pow(2,7)3 [8 m9 o/ I2 _6 X9 H% \. e
  8. 128.0
复制代码
" V9 B. q9 N; p" `/ g+ ^+ {
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 U+ `% u7 E* f! C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ F' }$ y& H* q$ Q( A7 P
  2. log(x[, base])
    ( B. V" \: b& h1 }4 G
  3. Return the logarithm of x to the given base.
    ( L+ a+ a7 {, u% o, F( d
  4. If the base not specified, returns the natural logarithm (base e) of x.
    : E- \  B9 I7 J' z
  5. >>> math.log(10)
    " h; S" C3 d% q: \$ Z7 G7 ^- e
  6. 2.3025850929940462 ?! J) l$ x5 j( t
  7. >>> math.log(11)
    : ?% o( }5 x: [: p
  8. 2.3978952727983707  W9 ]& i3 j% X7 u9 }7 A. F! u) K. x
  9. >>> math.log(20)
    4 g) n  e2 V2 f) `" y
  10. 2.995732273553991
复制代码

4 d7 n+ U' u4 n9 z1 W* V$ L& wmath.sin(x)  求x(x为弧度)的正弦值
. i4 t4 n% M8 f, e% w, `8 K  V7 ]
  1. #求x(x为弧度)的正弦值
    ) e' f1 L' q5 t  B. p# t
  2. sin(x)- R& S/ s; z, {) j* j# L
  3. Return the sine of x (measured in radians).
    : T2 P$ n$ A! h8 v( b; p9 C# c
  4. >>> math.sin(math.pi/4)
      o5 K3 Q2 i$ n( C3 r6 S
  5. 0.7071067811865475: M' K( F7 M- Q; P6 n
  6. >>> math.sin(math.pi/2)
    0 m7 z! |* \! r3 D5 b
  7. 1.0) u; z. G% {0 C% H/ e7 O# A4 l" {
  8. >>> math.sin(math.pi/3)9 b$ R5 A2 }' n6 a
  9. 0.8660254037844386
复制代码
; ]% o+ k7 r1 w0 V; L  U0 G
math.cos(x)  求x的余弦,x必须是弧度
, }& y+ T/ x& e: L& ~
  1. #求x的余弦,x必须是弧度
    , f. H& H6 ~9 I% R6 G
  2. cos(x)* c: H: i! Z. Y
  3. Return the cosine of x (measured in radians).
    5 @& V. E. R2 p3 Y
  4. #math.pi/4表示弧度,转换成角度为45度$ j( L) A- O8 r5 m; h
  5. >>> math.cos(math.pi/4)
    / K+ ^4 d9 y8 b6 U" d7 O# |
  6. 0.70710678118654762 N% O% u9 l. n$ m6 g7 t- \8 B
  7. math.pi/3表示弧度,转换成角度为60度
    1 t2 E0 G( S0 u9 \- L& D
  8. >>> math.cos(math.pi/3)
    4 O6 G2 v0 l" ]; p
  9. 0.5000000000000001- U: c! o& c6 E; g2 v1 V
  10. math.pi/6表示弧度,转换成角度为30度9 h" u9 A8 L* V% I/ l: c* c# R
  11. >>> math.cos(math.pi/6)
    7 S. M5 F8 i' s3 e8 c0 E- G
  12. 0.8660254037844387
复制代码
0 U" z  m- F, b) M6 d+ ]
math.tan(x)  返回x(x为弧度)的正切值) w  N2 |/ i8 O; {# M
  1. #返回x(x为弧度)的正切值
    : |3 t0 O" b2 ]2 ^# r: L
  2. tan(x)7 x, J3 z' j+ T$ d5 Z
  3. Return the tangent of x (measured in radians).' }: x2 I4 w% [" g
  4. >>> math.tan(math.pi/4)4 b+ I/ W/ c' ~3 V
  5. 0.99999999999999998 x8 j! z: e# t
  6. >>> math.tan(math.pi/6)
    % d2 o+ [$ O9 Q# E) b
  7. 0.5773502691896257
      D. y7 Q( C5 n& A3 z' D, U
  8. >>> math.tan(math.pi/3)8 y9 S7 L3 P* H% @: u1 K9 v" P. H
  9. 1.7320508075688767
复制代码

7 s& H2 a1 z+ m- gmath.degrees(x)  把x从弧度转换成角度6 b9 j( I7 Q, @7 S) B+ b  M/ k: A
  1. #把x从弧度转换成角度; i( d9 h2 o' Z6 e" p
  2. degrees(x)
    2 S6 C1 [/ V( o
  3. Convert angle x from radians to degrees.! C( w; z0 u; ~- N/ O' z

  4. 2 c" r" \3 u' B9 c- r
  5. >>> math.degrees(math.pi/4)+ C# q0 S: u1 w# j* M4 i. ~0 O. E3 W
  6. 45.0
    9 X- n# M0 ?# V  b/ c# @
  7. >>> math.degrees(math.pi)& [- k6 }. `6 @. u
  8. 180.0' H; {% y3 |6 |# h
  9. >>> math.degrees(math.pi/6)
    : f4 p4 p3 ^0 N) x4 t; u
  10. 29.999999999999996
    : |; H/ R4 S7 W
  11. >>> math.degrees(math.pi/3)
    " o7 b0 f7 o# \% Z3 [
  12. 59.99999999999999
复制代码

: g) D6 d# L4 t3 M3 Omath.radians(x)  把角度x转换成弧度
" Z* [2 w, _( C
  1. #把角度x转换成弧度
    ' z0 V6 E' C/ B# J6 P
  2. radians(x)
    2 Y) T( B$ {  Z! G: Q
  3. Convert angle x from degrees to radians.! F$ [9 A, [; i! L( O# p1 N
  4. >>> math.radians(45)6 f) M4 e( g9 P- Q* M6 y
  5. 0.7853981633974483/ S/ n2 c/ X/ L! ?1 r8 Y
  6. >>> math.radians(60)
    1 N( d: T. C% E, ?; W) g
  7. 1.0471975511965976
复制代码

" J# a# v$ n6 jmath.copysign(x,y)  把y的正负号加到x前面,可以使用00 i- q0 q' t  s( T$ C0 V
  1. #把y的正负号加到x前面,可以使用05 W8 F" ]" v- O  |6 V/ e
  2. copysign(x, y)
    2 w* J0 q! b0 ]3 a* d# ^% W
  3. Return a float with the magnitude (absolute value) of x but the sign   z6 I/ n! P9 y8 V: d! V' T3 B6 r
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( J. h& j1 j! H. d7 ]1 Y
  5. returns -1.0.
    : g9 _6 c$ ~' Q& C. V

  6. & ^$ b: t5 t& w# i
  7. >>> math.copysign(2,3)7 B8 p; N$ J2 [6 Q7 G2 g
  8. 2.0' {1 o9 x( J0 ~2 w: E
  9. >>> math.copysign(2,-3)6 F! L% e6 g: _5 q+ A
  10. -2.0
    0 ]# c- e3 ^; [- o( h
  11. >>> math.copysign(3,8)
    1 B( {: p( _8 L5 V" P6 H# b: R
  12. 3.06 ~2 c  G' b/ B, B, Z, `
  13. >>> math.copysign(3,-8)' }8 ?) W% V3 V3 d& A2 E! R
  14. -3.0
复制代码
8 }, w8 r4 m( w- N2 k: ^$ n
math.exp(x)  返回math.e,也就是2.71828的x次方8 `: _' T" R! h! P& v
  1. #返回math.e,也就是2.71828的x次方$ L8 k% x' ]: p: f$ X4 ~0 l" c
  2. exp(x)
    0 W1 h: Q' f- R5 Y' ~, R/ ~$ a
  3. Return e raised to the power of x.7 d# H. K$ k0 i. R$ M( q7 B0 J: H& G
  4. - {# ^! T! s, m# v2 [
  5. >>> math.exp(1)
    . `9 P0 v: f2 `$ r
  6. 2.718281828459045
    6 k) D" T6 x; i6 ?
  7. >>> math.exp(2)) k# c) n3 N% o% u3 L2 m2 J7 F/ ?3 y
  8. 7.389056098930655 m: w8 s: F$ C
  9. >>> math.exp(3)
    $ n& b& F) Y8 s  b1 F
  10. 20.085536923187668
复制代码

) w( K  W$ n- d5 h" r3 umath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( n! u; f# M, k3 ]
  1. #返回math.e的x(其值为2.71828)次方的值减1/ B- ~. B" [- T9 l
  2. expm1(x)9 ?2 S$ m* _4 Y4 l) T6 F; f6 o* r
  3. Return exp(x)-1.
    7 h2 l) }# _8 u* g3 ?
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    & m1 p/ ?: _' n, ~6 k; U- L! m/ C
  5. . _  |( D( B! m4 }
  6. >>> math.expm1(1)
    9 F4 S% H$ p8 }2 ]! A8 l: n
  7. 1.718281828459045
    7 X+ h1 G3 |0 r1 E* V1 A5 G
  8. >>> math.expm1(2)" w4 M5 h( [  |5 e
  9. 6.389056098930654 C; `( D0 m  ~
  10. >>> math.expm1(3)- \2 b; i6 e6 i, |1 r5 ^$ ]
  11. 19.085536923187668
复制代码
% X; [: k  g3 \- D; e! T" ]& l
math.fabs(x)  返回x的绝对值( M# y8 k1 E  \. p7 D
  1. #返回x的绝对值
    ; y& ^8 K( A+ _+ C
  2. fabs(x)
    - w0 t4 Z- \& d1 I# N/ t. U
  3. Return the absolute value of the float x.0 a! K% ~$ ^3 k3 K2 ~; `
  4. # Z$ b) Z# O: P! P, g2 J
  5. >>> math.fabs(-0.003)$ p9 z' F% M9 y" F) D7 n1 @# \
  6. 0.0032 a* t, y+ m% r: E. H, h% v# z
  7. >>> math.fabs(-110): J0 O6 T: a7 Y" a8 Y
  8. 110.0# q5 Y( g2 I* Z3 s, H$ R
  9. >>> math.fabs(100)
    4 T  i* c+ i) s
  10. 100.0
复制代码

: C. _$ Q3 H6 Qmath.factorial(x)  取x的阶乘的值/ `# ^# Q! Q) A. @( u; m
  1. #取x的阶乘的值
    2 N+ Q+ \0 \3 N1 o6 L9 t0 d. a
  2. factorial(x) -> Integral
    ' ]$ r- {" l- o+ L, r
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    4 u8 Q/ N) J- o
  4. >>> math.factorial(1)
    3 X' `  A9 e# D+ j/ _0 b$ t7 D# _9 E7 N
  5. 16 F9 o1 p' y5 _# [, x
  6. >>> math.factorial(2)  `3 z6 ~8 h! g9 f4 R" H
  7. 2& F/ X" x) s% _8 y; l+ V5 r7 c
  8. >>> math.factorial(3)
    ' q% ~& a! |* Z
  9. 61 u. X9 j4 J. c* c# y) R
  10. >>> math.factorial(5)7 V5 b. K& c, O) r3 j2 c9 n$ Z! {
  11. 120
    / r! Q- }0 i$ R0 L6 Y
  12. >>> math.factorial(10)
    ! x, a# P0 }  x8 o% u& }) ^
  13. 3628800
复制代码
8 T1 g" {0 R/ p  c# B1 s
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数% J% P4 E% m# v4 j& i7 c8 B
  1. #得到x/y的余数,其值是一个浮点数5 z$ `. k' }: A( ?/ }
  2. fmod(x, y). a2 `. a+ [% r6 p: c% i0 h
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    5 K6 ]$ ^2 [' D$ ^+ l
  4. >>> math.fmod(20,3)
    . f3 G* Q# u5 X
  5. 2.0. g3 U9 b8 p! A7 l& n8 z
  6. >>> math.fmod(20,7)
    . H. U1 ]# Q* u1 E4 z
  7. 6.0
复制代码
3 ~3 u+ l0 a; D. v' W2 {
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; m$ E6 d+ j$ x# C3 l
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 i8 o+ `/ G1 l& i& _% i
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ) a+ c) e. a& @5 J3 f
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; O& \2 D% s) ]4 w
  4. frexp(x)
    : t+ ~) d, J  r9 N! A8 w0 j
  5. Return the mantissa and exponent of x, as pair (m, e).
      c6 O: I3 M* ?! A
  6. m is a float and e is an int, such that x = m * 2.**e.
    2 G) N; S) L$ ^) U9 E- S/ e
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    3 j3 T) E8 D: F" i, m" [$ X
  8. >>> math.frexp(10)3 D/ l9 h. @6 J- M) S
  9. (0.625, 4)+ m7 B0 R/ e- N3 W9 E
  10. >>> math.frexp(75)
    % u% M0 I/ `1 ]) e0 x3 X
  11. (0.5859375, 7)
    $ f1 }" D* J3 P7 h/ w5 }
  12. >>> math.frexp(-40)- n% B: L7 r0 H6 _4 s1 n7 ?5 \' @
  13. (-0.625, 6)
    # h' O0 C7 S! r. W
  14. >>> math.frexp(-100)
    8 Z0 g$ }, z# F0 H9 `3 V
  15. (-0.78125, 7)
      s7 g/ U- q& [  l- a0 k2 r. `
  16. >>> math.frexp(100)2 F) e# B5 I" g( t& L
  17. (0.78125, 7)
复制代码

% M  A: K5 D% ^  c; r/ lmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 R  ~) [' L1 c7 q/ D& x3 k' h
  1. #对迭代器里的每个元素进行求和操作
    $ ]  L8 Z: s& J- w: b
  2. fsum(iterable)
    . J' Q3 o+ g4 q) g
  3. Return an accurate floating point sum of values in the iterable.* b5 m: p4 E4 g2 [' s" N! l$ b( Y
  4. Assumes IEEE-754 floating point arithmetic.5 E, }8 u$ i9 w7 i- t6 S  s
  5. >>> math.fsum([1,2,3,4])
    : G$ Q5 C! o& v; o! f' e- ]
  6. 10.0
    $ {8 R& c* ]: }# a
  7. >>> math.fsum((1,2,3,4))2 L7 U9 i3 h# y' W1 M3 X2 R# g
  8. 10.0: i" P* w$ p/ s) n$ |
  9. >>> math.fsum((-1,-2,-3,-4)). h" f$ n0 Z# W  W
  10. -10.0- e. _3 {1 p  B. J
  11. >>> math.fsum([-1,-2,-3,-4])5 S4 Q! f/ o: A, t+ u# V3 }
  12. -10.0
复制代码
: y+ t9 ~# s& F% J+ G! f7 z( Z
math.gcd(x,y)  返回x和y的最大公约数$ _! U, I3 }3 ^
  1. #返回x和y的最大公约数
    % {/ v& j) Q3 C3 U% g6 h* h% T
  2. gcd(x, y) -> int- e7 t! y1 f# y, I# ~- e5 c! k$ V
  3. greatest common divisor of x and y. h9 B$ L, k; @- ?
  4. >>> math.gcd(8,6)
    5 z1 t3 W4 p* j! t
  5. 2) X2 F3 ?' P- [3 `4 i* [" w% L* k
  6. >>> math.gcd(40,20)
    4 ^1 @8 d+ M9 R
  7. 20
    $ e. a6 \5 Q" d
  8. >>> math.gcd(8,12)
    " S3 [' ]# q; ?" ]# R: I! r
  9. 4
复制代码
" B7 n: K* q/ C7 V' l
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False. f4 i! w7 @  B9 q: T# H+ e# D8 s+ y
  1. #得到(x**2+y**2),平方的值
    + H/ P6 M* u/ ]( v! V
  2. hypot(x, y): O! Y5 @7 {6 |, H: b0 g
  3. Return the Euclidean distance, sqrt(x*x + y*y).8 q/ |1 a% F, P* h
  4. >>> math.hypot(3,4)
    " r7 M0 t2 w( h$ N
  5. 5.0% v( G. W0 }. P8 Z% }
  6. >>> math.hypot(6,8)2 F7 s0 O& N: J
  7. 10.0
复制代码

) r+ c6 L) D8 ~3 }7 |8 t' P: }math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False$ o: e) j' A: n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    4 j) o& r& [' A) g- v1 s! M: r
  2. isfinite(x) -> bool
    1 X* m4 O2 Q: |9 K
  3. Return True if x is neither an infinity nor a NaN, and False otherwise./ [! m$ m9 Z1 _9 F. B% ?0 r3 Z
  4. >>> math.isfinite(100)
    8 f) b0 \( T' }( J  u
  5. True
    8 [; P9 \9 Y: V8 q7 W) }
  6. >>> math.isfinite(0)
    4 N: p; b9 v2 f% d
  7. True
      v3 V) h( ~% C6 Y- b) m' P
  8. >>> math.isfinite(0.1)3 c5 a8 T# v/ h" g( Y
  9. True; J3 q2 E. F# m- u7 u0 }; ?- T, P5 s
  10. >>> math.isfinite("a")- y- b9 v: J. h' |% t% k$ g/ n
  11. >>> math.isfinite(0.0001); j5 y7 X! K  _7 m6 O
  12. True
复制代码

; C% j" R5 i- ^1 H% \/ A$ Nmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
: b9 [, ]# @; y3 D' D8 V) \$ z
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    % @1 }% W2 J# Z2 Y: @
  2. isinf(x) -> bool
    7 [& R) A% q+ H  x: g4 _8 [  c
  3. Return True if x is a positive or negative infinity, and False otherwise.4 {& ?1 l! ]6 J. ~. H' v! d2 I
  4. >>> math.isinf(234): y2 Z& B( j: k  ~& b8 c: {
  5. False3 V; u0 @& b  {2 B- x9 V' _
  6. >>> math.isinf(0.1)
    0 L& r+ E6 s" t) {4 v2 Z( J
  7. False
复制代码

6 w4 T! p' {  j  h4 J- imath.isnan(x)  如果x不是数字True,否则返回False
* D+ S3 X- B: }/ [: W( }1 O0 B
  1. #如果x不是数字True,否则返回False
    " _1 E& R4 x0 @! f, s, M
  2. isnan(x) -> bool) Q; L' e4 e- J4 F
  3. Return True if x is a NaN (not a number), and False otherwise.9 \3 X1 Q  P4 k: \4 V& K
  4. >>> math.isnan(23)! a" o  ]. }4 x
  5. False
    + v( Q. ~8 k. J& |3 T5 I
  6. >>> math.isnan(0.01)3 T' ~) r' n! z
  7. False
复制代码

5 T: _0 W) [0 V" m( J: s6 |math.ldexp(x,i)  返回x*(2**i)的值" |1 ~6 ^9 r4 H) A
  1. #返回x*(2**i)的值
    ' ~9 C8 U' ^, L# X0 U/ t) U
  2. ldexp(x, i)0 x% i: i& g# e7 t% {7 c4 Y2 _; W
  3. Return x * (2**i).' ?. l2 `8 S$ a( I. |4 v+ X
  4. >>> math.ldexp(5,5)$ Z1 p0 z% c: Y4 I2 o6 r
  5. 160.0
    $ f. O/ w# b3 a2 x! v) q
  6. >>> math.ldexp(3,5)
    2 _. |* t+ P" n( U
  7. 96.0
复制代码
* Y0 l7 N* f2 t9 U
math.log10(x)  返回x的以10为底的对数
$ K( M& E; h% X1 U+ U, w$ I6 A
  1. #返回x的以10为底的对数
    5 M# e& o5 M$ |8 Z
  2. log10(x)
    4 {/ ~* [. T& v+ g+ J' `: z+ R4 E# r
  3. Return the base 10 logarithm of x.+ E$ }5 n: S* T
  4. >>> math.log10(10)) W' \; W3 K+ b0 @2 H& [3 ~$ V
  5. 1.0
    , Z* Y* \. i5 x% ]) s5 a) j
  6. >>> math.log10(100)& \$ }3 l5 z, J/ ^8 z7 {; a
  7. 2.0
    ) z6 h$ Z$ L& }2 j+ a
  8. #即10的1.3次方的结果为20. U. j1 E' @- l& z3 `  g# O& W  j
  9. >>> math.log10(20)
    1 M8 k  f  U6 P$ t% u* V& d% R
  10. 1.3010299956639813
复制代码

4 K) u' H% `# L1 v2 y) Gmath.log1p(x)  返回x+1的自然对数(基数为e)的值' c& ?# b$ X  T" P! f& J8 m8 J
  1. #返回x+1的自然对数(基数为e)的值8 Y6 ?! g) Z. B! W- ?
  2. log1p(x)
    6 x& [+ b+ b# ~9 ~$ L/ G' H
  3. Return the natural logarithm of 1+x (base e).
    $ V- s$ Z* y0 C% u
  4. The result is computed in a way which is accurate for x near zero.
    6 I! w" Q. M1 F  {- E" g( Y
  5. >>> math.log(10)
    1 Y9 h2 D# p. H' e
  6. 2.302585092994046; ~2 Z4 e; O( R
  7. >>> math.log1p(10)5 R, @' f2 F6 }+ c8 b0 {: M
  8. 2.3978952727983707
    & h. l( r) f3 \# f
  9. >>> math.log(11)6 }% k: P& s- [1 v& L
  10. 2.3978952727983707
复制代码

0 O- ]! ^. {1 R/ x, W- L4 _! s0 s( emath.log2(x)  返回x的基2对数% W1 b0 e' M7 D- r' y7 m2 ?
  1. #返回x的基2对数
      f8 I# V5 j7 v4 Z  ?% |
  2. log2(x)
    9 H) D8 K5 m3 n" u" q
  3. Return the base 2 logarithm of x.$ S, D. r1 x9 c4 ~
  4. >>> math.log2(32)- V3 y! }0 J5 V* C  V5 G4 u* Q
  5. 5.03 _( n6 y3 K$ Y6 I0 n" l
  6. >>> math.log2(20)3 d8 W4 m! M3 b
  7. 4.321928094887363( w7 b( C8 p/ f- I# x5 k( Q
  8. >>> math.log2(16)
    , j# j& r! a7 i$ _' G
  9. 4.0
复制代码

# y7 w* J6 P6 Tmath.modf(x)  返回由x的小数部分和整数部分组成的元组, W# g3 ^' U' h) d# T- }
  1. #返回由x的小数部分和整数部分组成的元组
    4 p( |3 D3 f) |. A+ r
  2. modf(x)! p2 q, |' i8 N4 M/ s0 C+ ]
  3. Return the fractional and integer parts of x.  Both results carry the sign* W8 ]5 b; c- L
  4. of x and are floats.0 v2 L) d7 Y* G- N' W
  5. >>> math.modf(math.pi)
    * @- H: e# n# L4 U! x
  6. (0.14159265358979312, 3.0)
    ; s8 C4 s: `! F4 J/ L
  7. >>> math.modf(12.34): I- s' ?& W! d, q
  8. (0.33999999999999986, 12.0)
复制代码
; S3 Y% V8 ?) E2 s/ J
math.sqrt(x)  求x的平方根
9 ]0 G2 j. G" P% e1 x; [2 [
  1. #求x的平方根5 t! s9 T8 A0 s
  2. sqrt(x)
    ) O7 H5 e" M: _1 }
  3. Return the square root of x.) i  w* G6 O& U& W& N: W$ M
  4. >>> math.sqrt(100)8 g  d8 @; j3 S: t1 \
  5. 10.0( @2 J: e1 {/ G! ]
  6. >>> math.sqrt(16)
    * ?5 h" f, f7 i
  7. 4.0
    + {9 f. @6 }2 ~0 K2 g: f( N" @1 k
  8. >>> math.sqrt(20)
    7 M5 d9 }# y# H2 J: x  b  n+ h! C
  9. 4.47213595499958
复制代码
  ]' ~2 H9 s1 e& J9 f/ j
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    + R# l. v" p* b( X& ~
  2. trunc(x:Real) -> Integral
    * S: [2 ?. }/ M
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    * d, E# F9 m9 b5 M  ^; e) \& N
  4. >>> math.trunc(6.789)
    , D9 M* a( e' ?8 D
  5. 66 |3 W/ }# m. D- ^: p
  6. >>> math.trunc(math.pi)" p9 e3 E- t/ z. ^& I
  7. 3
    , E, L6 r8 C3 d; L
  8. >>> math.trunc(2.567)
    ' v0 R7 J7 w: o9 `2 `
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-22 08:06 , Processed in 0.088317 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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