新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 2031|回复: 0

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

 关闭 [复制链接]
发表于 2021-7-24 10:21:24 | 显示全部楼层 |阅读模式

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

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

x

& ]2 Q' s6 |8 Q. m9 J. a- E! W) o9 |【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 w; F' ]# O( Y$ |* v

( o6 V9 O# D% Y8 m) W方法1
8 b/ l" ?8 U+ }% h5 |$ i
  1. >>> import math
    / u: N& \6 I! A' @+ S1 Z0 `' q) }
  2. >>> math.sqrt(9)
    ! [/ z, D# R5 z; D3 l" R
  3. 3.0
复制代码
方法2
# w; ~! h8 b' c
  1. >>> from math import sqrt3 e# d8 W$ D3 p1 w
  2. >>> sqrt(9)
    * d8 S; u2 X7 x4 s  T
  3. 3.0
复制代码
+ ^& Q9 b; f# E- K. D& t! F

& o/ A+ i: N( \: ]; n
math.e  表示一个常量
& I, h8 V2 O$ k# d" J
  1. #表示一个常量: d1 E8 A! x  \: C  T
  2. >>> math.e
    " A) p3 W% K9 }. o: m% j
  3. 2.718281828459045
复制代码
- G6 ~$ N/ p' G" w$ F
math.pi  
数字常量,圆周率
2 g0 V0 J9 c6 N& T5 s$ R
  1. #数字常量,圆周率& ]9 W  A% ]; s
  2. >>> print(math.pi)
    3 A3 i* O: _$ b- I* Y4 }; G
  3. 3.141592653589793
复制代码

& l  g' N8 S; c; v) z5 F4 A. U# Rmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

, i8 |/ |' z+ U# S0 E& ~( L2 T- j
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    : l/ N- \% P; j1 b& [8 I  k3 l  c
  2. ceil(x)
    ; m" \. s( ~! p
  3. Return the ceiling of x as an int.
    " K2 r5 t+ s  R. y2 h8 Q
  4. This is the smallest integral value >= x.5 l4 }7 l9 c1 S. r* S8 O! ~

  5. : {  |3 W: u6 T* @/ R
  6. >>> math.ceil(4.01)- n5 x# j6 p1 k- E* L0 O' u  R
  7. 5
    : T3 e  ?; g4 t8 E4 ^- `  c
  8. >>> math.ceil(4.99)6 q5 R& U1 `1 }' H: Y
  9. 5" ~) Z! K! z9 A
  10. >>> math.ceil(-3.99)
    % w7 I' F4 }7 n. v' K6 C% d5 ]
  11. -38 z( d: h1 |5 {# z+ @- u% ]
  12. >>> math.ceil(-3.01)
    6 W, ~, B: I1 R$ G
  13. -3
复制代码

( D" c" e$ u9 s5 Y4 xmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' o  j0 n+ N6 Z3 ~
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ I7 ]4 O. Q+ W! G4 T9 P, o; b
  2. floor(x)
    ) {/ C2 _  v. k# _$ y% U
  3. Return the floor of x as an int.; @$ D! d3 J. B* ?+ h" ?8 a( l
  4. This is the largest integral value <= x.
    ! Z: _/ [! U- X* ~) ^
  5. >>> math.floor(4.1)
    * h, i& l" V6 f3 ?$ s- N9 I
  6. 4
    " x$ ]; a- Q( c& o! i9 D
  7. >>> math.floor(4.999)
    $ b- ]  [( n, _8 a
  8. 4
    % V4 M3 _3 m9 s6 {% C' m
  9. >>> math.floor(-4.999)% g; K* O3 S" {% ~
  10. -52 j1 P, a2 I- v1 Z$ Y- `9 u
  11. >>> math.floor(-4.01)/ P. C9 \/ [( U
  12. -5
复制代码
0 y1 p, r1 A% b" u. u
math.pow(x,y)  返回x的y次方,即x**y; J" s2 W9 V6 b6 p- B
  1. #返回x的y次方,即x**y
    2 u8 y6 M  E1 m- ^% I2 [
  2. pow(x, y)7 M- k, k* ^1 P& Q6 r8 w) i
  3. Return x**y (x to the power of y).- @+ A) \+ H; g: e
  4. >>> math.pow(3,4)
    * q2 F5 [. ^1 R( v$ @8 Y6 X
  5. 81.0  E9 [0 r& P7 L2 }, d$ a
  6. >>> - J" b) ~, c. V5 E
  7. >>> math.pow(2,7)
    6 @$ a- z# h7 r3 {: P
  8. 128.0
复制代码

" u  W: b$ B) U% V/ n. omath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 l/ G2 A% L. H3 {" n
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    6 h+ T; m2 r9 B0 ~( R) d
  2. log(x[, base])
    0 o7 t# L8 ^; s9 ~+ h& x
  3. Return the logarithm of x to the given base.
    $ w4 X$ ?) p3 Q, P( q! Y( N/ t
  4. If the base not specified, returns the natural logarithm (base e) of x.& o0 u# H. Y; v1 x# _2 ]% J7 q
  5. >>> math.log(10)
    3 z& i  h& A8 Q2 w' I
  6. 2.302585092994046
    " `8 i2 A$ `2 V2 f
  7. >>> math.log(11)$ U3 Y* Q/ K1 C. A
  8. 2.39789527279837078 \: @6 C3 B5 l0 A/ o6 H
  9. >>> math.log(20)7 x, ^- l0 [9 e, x
  10. 2.995732273553991
复制代码

( H  F6 ?7 t# Z& g4 Vmath.sin(x)  求x(x为弧度)的正弦值
% E4 H1 j$ Z9 b6 b: u3 [9 [
  1. #求x(x为弧度)的正弦值
    9 y3 e  s* H+ ^
  2. sin(x)
    2 G5 I7 ]6 I1 F
  3. Return the sine of x (measured in radians).! r: \# ]. A0 b. f- ^
  4. >>> math.sin(math.pi/4)( u4 e7 a! S2 I7 ?; s
  5. 0.7071067811865475
    1 L& ?3 ?8 [( P' f) @
  6. >>> math.sin(math.pi/2)
    6 Z, x1 K$ u, J0 z& T3 W
  7. 1.07 j6 v. t9 ]& B" R1 R! T) H
  8. >>> math.sin(math.pi/3)
    4 t; k( Z! s- ?
  9. 0.8660254037844386
复制代码

% l- a+ N2 b" C" y% Pmath.cos(x)  求x的余弦,x必须是弧度
! q# ]4 {# r; X: X6 f- z/ r
  1. #求x的余弦,x必须是弧度" f1 ~9 j( |& s8 a6 X+ B
  2. cos(x)' A" L) R  c, O1 A2 n
  3. Return the cosine of x (measured in radians).: s1 _0 {+ D5 H: T0 p' A8 c
  4. #math.pi/4表示弧度,转换成角度为45度
    6 ^* X# k3 D" z( R4 u- {$ L
  5. >>> math.cos(math.pi/4)
    9 i# Z+ h, D1 _5 h9 p8 L! J
  6. 0.7071067811865476
    " s6 B0 J' ^1 Z/ h* p" g
  7. math.pi/3表示弧度,转换成角度为60度
    ; d" O; ]9 A8 t8 G6 f
  8. >>> math.cos(math.pi/3)) p4 _: h. z! N
  9. 0.5000000000000001. Y9 P0 {& i2 C+ G; ?, e
  10. math.pi/6表示弧度,转换成角度为30度
    6 {: i; K6 g' m
  11. >>> math.cos(math.pi/6)
    9 w0 ^1 i! [, \, J/ x! e' p3 ~
  12. 0.8660254037844387
复制代码
  z" }3 n, ^6 j
math.tan(x)  返回x(x为弧度)的正切值. B9 h& Q& Q6 Q/ S
  1. #返回x(x为弧度)的正切值; h* D5 z. A# W; P* R
  2. tan(x)
    6 b2 q+ u: [. v) U* ^. i% z
  3. Return the tangent of x (measured in radians).) y( d: _+ ?( C- z8 N. y' T: f
  4. >>> math.tan(math.pi/4)
    - R- z  F: h- \' E" b3 M
  5. 0.99999999999999991 N) m( A+ d, ]6 I* p
  6. >>> math.tan(math.pi/6)- A; u5 O/ z! R+ v
  7. 0.5773502691896257
    - k6 H0 Q6 x& ]( f# ^. y' v
  8. >>> math.tan(math.pi/3)
    ( B) W$ s5 ~6 t6 l# p, o% H& F
  9. 1.7320508075688767
复制代码

5 }) J& J  ]9 ]2 t1 a7 Umath.degrees(x)  把x从弧度转换成角度$ c3 `: u( t3 W9 o6 z; v- ~
  1. #把x从弧度转换成角度! r: q: k: @* I; q
  2. degrees(x)
      C; j- I$ U1 s4 h$ u* c
  3. Convert angle x from radians to degrees.
    1 l) }+ Y) c: }7 v5 E& U
  4. & l+ z2 `0 [4 a2 ]" B2 ~
  5. >>> math.degrees(math.pi/4)' W* y& V9 ]* S/ J
  6. 45.08 @, v4 b0 R6 ?
  7. >>> math.degrees(math.pi)+ c/ z6 r5 g4 G( i
  8. 180.0
    * Q9 z  n5 ^0 h& Y) G+ I# B+ n, M
  9. >>> math.degrees(math.pi/6)
    3 u6 Q; v5 k$ X/ C% J9 L1 R1 }
  10. 29.999999999999996
    , S4 R- T" ^! j, c8 q7 y& o+ Z0 \* H
  11. >>> math.degrees(math.pi/3)$ \7 R7 ]( n# k. F0 S
  12. 59.99999999999999
复制代码
& Q8 |5 G5 Q+ u" P4 \+ h3 H
math.radians(x)  把角度x转换成弧度
& {5 w1 t+ H5 Q$ o1 p' |
  1. #把角度x转换成弧度
    " Z$ z+ u, L$ e' ]
  2. radians(x)2 h5 O4 ?3 v/ j
  3. Convert angle x from degrees to radians.$ S+ Q: ^2 p7 \% r: {2 y4 W& n/ U
  4. >>> math.radians(45)
    6 B. c1 J! s6 P6 T% N% L
  5. 0.78539816339744831 [( |, k( i+ x& s& W
  6. >>> math.radians(60)
    2 s$ o  k& Z2 K# v( I) b* s
  7. 1.0471975511965976
复制代码
: U  J, Z0 ?9 D, u
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
5 H7 E8 L3 J1 F0 e, I" q2 \
  1. #把y的正负号加到x前面,可以使用0% ~, A- V9 W+ h
  2. copysign(x, y)
    ! k- ]3 d& x% b" a4 z
  3. Return a float with the magnitude (absolute value) of x but the sign
    3 t5 `5 m) ~( [1 D" l4 X8 ]6 E
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) / h. s. x9 T! @
  5. returns -1.0.0 I- p4 E- F. C0 A6 {% s, B
  6.   ^2 q9 W! L$ Q6 J* j' F+ d
  7. >>> math.copysign(2,3)# w* T* {- C3 T* e* r- P) u
  8. 2.0  ^5 h5 j. V" P. _3 o+ e2 [: V0 o% j+ }. B
  9. >>> math.copysign(2,-3)2 T, c. u/ ~# g$ N6 `- `. m% z
  10. -2.0
    ' r# V4 T7 N6 I
  11. >>> math.copysign(3,8)
    - Q& }  h' z' v; ~# a) t, Y$ A% a
  12. 3.0
    ) @( H% `1 I& T2 j7 g
  13. >>> math.copysign(3,-8)# l; a8 c, e8 p% {
  14. -3.0
复制代码
- h9 A  c# @% G( i, n
math.exp(x)  返回math.e,也就是2.71828的x次方
, _# T* M9 R) G7 d2 H/ ~9 W2 T
  1. #返回math.e,也就是2.71828的x次方% Y& ]7 q# x. g9 M1 q% V- K9 u/ f
  2. exp(x)# X2 x* O7 z: ?2 J* J
  3. Return e raised to the power of x." a# x2 F- u, g- z
  4. 8 {. P6 {' j1 {
  5. >>> math.exp(1)5 {% n  _, ]  z' B$ J1 Z5 N
  6. 2.718281828459045
    ( _( W$ |: |; u
  7. >>> math.exp(2)
    ! O& h4 A  v/ `5 D9 g# f
  8. 7.38905609893065
    $ H: q3 Z6 W+ k$ ]
  9. >>> math.exp(3)! ]$ p$ L9 [/ S* ]
  10. 20.085536923187668
复制代码

( S, J& S2 D3 i( z, I/ b" }math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1* U8 B1 G) @/ N, e% Q, ^4 ]$ h
  1. #返回math.e的x(其值为2.71828)次方的值减1
    . l# r$ h2 D- l# g2 y
  2. expm1(x)
    # V6 b; j7 r1 ~! v
  3. Return exp(x)-1.! D4 G8 Y( ^& l
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ( G$ r0 l+ a$ x7 ?) y; {
  5. * p1 z( F7 A# u0 n/ O- v' e
  6. >>> math.expm1(1)
    ) Z3 l/ D) Z6 }5 ~. O$ Z* j
  7. 1.718281828459045
    8 j5 p- U6 G" q2 y7 h" h5 S, `
  8. >>> math.expm1(2)# P/ S& J% T, }- j
  9. 6.38905609893065
    # `- c6 ?1 F; s$ m. }& [( M
  10. >>> math.expm1(3)
    8 }: x0 ?& Y( B8 H" d8 u! K, b
  11. 19.085536923187668
复制代码

0 m: O+ p2 h/ ymath.fabs(x)  返回x的绝对值' s! V3 o( k) Z% R* j$ P
  1. #返回x的绝对值" Q; `. O( L$ Q7 ]7 r" s
  2. fabs(x): G9 d, G) N- E! A6 w: N
  3. Return the absolute value of the float x.
    $ i2 W8 Q& c  v
  4. - G7 j# m4 }% l, L/ M1 H
  5. >>> math.fabs(-0.003)4 A, r: m) z) @! u+ D# q$ X# z( H* Z5 A* ?
  6. 0.0036 I) _1 {, F: G* `8 b
  7. >>> math.fabs(-110)2 H1 K4 O$ E" Q3 r1 Y$ B
  8. 110.0
    ! Q* E& y9 V) L9 f- K* D" \2 Q
  9. >>> math.fabs(100)
    " A1 a) |8 Z4 i8 g6 W" F
  10. 100.0
复制代码
6 T7 d$ y  _* G4 b8 L( b) r
math.factorial(x)  取x的阶乘的值$ A3 X# X" C$ W4 r
  1. #取x的阶乘的值
    % T5 _9 S2 {. q
  2. factorial(x) -> Integral& T- X) e$ h( f3 ^6 _% b
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 y& n- X' k8 C
  4. >>> math.factorial(1)
    8 k8 ]6 j1 V. e) D) ~5 k
  5. 18 b0 b' ]! T' }: p+ m. A3 ^
  6. >>> math.factorial(2)
    9 S2 Q! R9 E- h1 g& [
  7. 2& Z$ A% j" W9 u( F! c
  8. >>> math.factorial(3)
    , ]: u5 D9 ~2 r/ U8 R$ ?1 e
  9. 6
    " s7 ]$ w1 F4 ^& \$ U
  10. >>> math.factorial(5)! M8 b5 R& H+ m2 x5 R, ^4 R
  11. 1203 I" H* L* T8 ?. p: A; u# s% C
  12. >>> math.factorial(10)% x/ W3 Z% T4 ?1 E/ m3 b  n
  13. 3628800
复制代码

0 V# p  e' {7 `6 A! P' j" Hmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
: m# e( E8 e2 D) o0 _# d7 }
  1. #得到x/y的余数,其值是一个浮点数: I$ Q8 p' K+ A3 x. g
  2. fmod(x, y)" X! }" z% f6 y% F
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    , z* `: N3 o  [+ f: l4 R, |) S5 q
  4. >>> math.fmod(20,3)
    . I! n& L: B* p  w4 F
  5. 2.0; P. N: f/ H" u8 l& G7 l
  6. >>> math.fmod(20,7)
    9 [3 Q4 G2 M! u/ w! u
  7. 6.0
复制代码
4 q/ z7 {1 V) B8 N/ ?# {
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 m, c/ T3 s5 J4 a. n) B
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,  M* O' o9 f, s
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    7 o7 I& S6 ?# H  t# f( p  [/ K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和15 \; z. _4 R" N$ D9 y* `- I* ?
  4. frexp(x)
    # p  E& u4 t0 Y+ l
  5. Return the mantissa and exponent of x, as pair (m, e).; D, H# D) a8 E$ V' o7 n
  6. m is a float and e is an int, such that x = m * 2.**e.
    1 L! b+ ], U1 k2 ~* Z8 @
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.: e) s+ C. I# ]7 ^( a& S. P, v1 o8 {
  8. >>> math.frexp(10)
    ( {8 X5 j/ }3 e% ?0 {5 j" R/ c
  9. (0.625, 4); _; n# _- ]7 K/ |& D5 g; A
  10. >>> math.frexp(75)$ \) s" k: s, D# b/ W/ R
  11. (0.5859375, 7)3 j3 i# Y% z3 H' F3 t! F
  12. >>> math.frexp(-40)+ Z2 _; W# |- u$ I  N7 i
  13. (-0.625, 6)
    , S1 Y2 s5 y/ ^
  14. >>> math.frexp(-100)
    " d+ E& |. e: P! c; ^
  15. (-0.78125, 7)
    + i6 i# s- x. s7 p7 E' }
  16. >>> math.frexp(100)
    + ^# t' ^. b* ^$ v* G
  17. (0.78125, 7)
复制代码
, J# p9 M( L4 R5 B+ H) w' x( L
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, O# E% c8 k1 w/ o$ k+ |6 c
  1. #对迭代器里的每个元素进行求和操作
    ! ]) }& l0 @1 m3 D8 t% W
  2. fsum(iterable)9 G+ s" a( f+ D
  3. Return an accurate floating point sum of values in the iterable.9 D3 I! |' R* x, E
  4. Assumes IEEE-754 floating point arithmetic.
    + R5 p+ v0 q3 u9 l' {6 H( N
  5. >>> math.fsum([1,2,3,4])
    , A5 ~& S3 u; K  s% U
  6. 10.0/ O& S' ~7 _( V0 K5 I
  7. >>> math.fsum((1,2,3,4))! M/ _7 O/ f; x1 Z
  8. 10.0( X8 {2 {! ~7 {; ~  b0 l; t/ L+ p# U2 _9 F
  9. >>> math.fsum((-1,-2,-3,-4))
    9 e0 t; ?& u; I" i$ F2 A) \" J5 u" z
  10. -10.0
    / _. P7 c- r4 y" }, n; O
  11. >>> math.fsum([-1,-2,-3,-4]). |, P* i. z6 b2 `
  12. -10.0
复制代码
+ U2 ?# G  L9 b  O- S( l2 Y
math.gcd(x,y)  返回x和y的最大公约数
. c5 q' b- i( ^
  1. #返回x和y的最大公约数2 U& e$ s) T& _/ A; ?5 h9 T
  2. gcd(x, y) -> int% W  ?4 E% G0 }3 X4 H
  3. greatest common divisor of x and y
    3 m8 B: }% L" f# o5 J5 p
  4. >>> math.gcd(8,6)% D+ ?* J) h, `9 H7 W% l
  5. 2& N8 S( F6 a9 Z9 p* b' R
  6. >>> math.gcd(40,20)$ A: J: B3 a3 z: t2 d
  7. 20
    1 [% S+ g8 F. u  b; C" m3 q: H
  8. >>> math.gcd(8,12)
    * I, L9 |6 E$ I% }
  9. 4
复制代码

" E+ K7 K5 W2 {# g7 Tmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False: E. X& N6 }9 E4 G* j& t' ?
  1. #得到(x**2+y**2),平方的值$ b5 e9 y6 Q. n! }6 O# d
  2. hypot(x, y)$ {% Y! w# ~3 E* C3 x2 \4 ?
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    $ P/ ?% I# w% v  f7 q9 Y
  4. >>> math.hypot(3,4)0 K7 q8 F9 m9 r7 b6 z
  5. 5.0
    ) ]$ ?4 v$ O  |; e6 E; j7 C
  6. >>> math.hypot(6,8)
    $ Z; m1 n. E2 [* L; R
  7. 10.0
复制代码

2 G& O) v  [2 y+ F  e( T. H: nmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False+ H6 P& d2 W# G# s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False; C2 G7 w, Z$ Q; p+ G7 N- K
  2. isfinite(x) -> bool: p6 h  E) _& u
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    / v  y# z6 G! ?0 H$ ~
  4. >>> math.isfinite(100)2 T& K' S" D# t8 R, S" p
  5. True; x6 g; s  X7 _
  6. >>> math.isfinite(0)
    / `, J: U7 L; C6 \; d
  7. True
    4 i4 N  A, o* P: ?- q0 Q1 Y
  8. >>> math.isfinite(0.1)
    5 O' M8 v0 S( X6 }( o
  9. True* u' n2 g# y$ d9 v' ]4 M
  10. >>> math.isfinite("a")/ d: M& S) Y8 E& u
  11. >>> math.isfinite(0.0001), D; x; I9 w1 P8 v+ j% h
  12. True
复制代码

: h" K+ y% F) o. d, ^math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 T' u$ k2 w; X* b: ~; L
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    8 O8 v9 P* K3 _% Q' E
  2. isinf(x) -> bool4 S/ l8 _! s4 ^0 Z& J
  3. Return True if x is a positive or negative infinity, and False otherwise.
    # B* }5 _8 F. K% {2 Q
  4. >>> math.isinf(234)
    4 s( w/ O6 z% c3 i( h
  5. False# a* Z# U; |) h  o1 x
  6. >>> math.isinf(0.1)
    * T8 h, K. A$ z4 J! m% ?$ Y: R
  7. False
复制代码
; i2 g6 p7 v2 ]$ n3 G5 f( A  }8 b
math.isnan(x)  如果x不是数字True,否则返回False, h8 a+ a, T3 b. P2 O+ O
  1. #如果x不是数字True,否则返回False: _( h6 l: K& k  D% r
  2. isnan(x) -> bool0 L+ ~2 C: P. w9 Y3 ]! e
  3. Return True if x is a NaN (not a number), and False otherwise.* E4 r+ Y- D- L' U5 l
  4. >>> math.isnan(23)  I- y, M+ ]: @3 \; z% f! C
  5. False8 A; S5 U" R# K8 R( M$ k
  6. >>> math.isnan(0.01); O6 d: ?, f* t; {6 Q  Z! B
  7. False
复制代码
7 Y: J3 `2 I, [( o, b0 i
math.ldexp(x,i)  返回x*(2**i)的值
. a; j2 v+ L/ l- {
  1. #返回x*(2**i)的值
    - p- v8 o% y, ]
  2. ldexp(x, i)% C: p2 A  l( ?
  3. Return x * (2**i).. Y4 l; S6 M! @& s) Q( X5 {. K
  4. >>> math.ldexp(5,5)' Q3 T1 z+ V: X+ b# \: @, f6 a
  5. 160.03 n+ ~7 n, X  X( P! @3 q
  6. >>> math.ldexp(3,5)
    2 O& z/ e* D. M) @
  7. 96.0
复制代码

9 P8 V1 Z5 B! X- ?% T# |- smath.log10(x)  返回x的以10为底的对数  q: m/ V" }" I+ z- ]
  1. #返回x的以10为底的对数" C: z) ~( }) u0 R2 ]3 Y
  2. log10(x)- s, m: H- W3 m4 ?5 c" Q9 o
  3. Return the base 10 logarithm of x.% G( |) u" m, ^8 X6 X2 W
  4. >>> math.log10(10)7 K% l7 W0 p2 w) e7 h- ]$ D& i
  5. 1.04 x! y5 v; m5 g% G: n2 u! ^
  6. >>> math.log10(100)
    5 J( ~9 A4 P( |2 r
  7. 2.0
    - U$ U  C# z' J3 }/ A1 M9 w0 F: Q
  8. #即10的1.3次方的结果为200 k2 M1 Z9 E4 T, u) j) Y
  9. >>> math.log10(20)
    9 E9 m) I, h; F; w8 e  [
  10. 1.3010299956639813
复制代码
# B6 [! [- W. ~( ~6 c
math.log1p(x)  返回x+1的自然对数(基数为e)的值
, L4 E0 H( q2 }8 h" ?" v0 h
  1. #返回x+1的自然对数(基数为e)的值8 k! \5 _" `# r7 ?9 u% K
  2. log1p(x)+ v' ?1 G5 g$ T
  3. Return the natural logarithm of 1+x (base e).$ @1 |& A$ B# d) n# g# g* t
  4. The result is computed in a way which is accurate for x near zero.! N- H% v) R9 f: V
  5. >>> math.log(10)
    8 }: x/ u0 }" ^) W: s6 q
  6. 2.302585092994046
    2 O3 r! T6 [1 s6 o6 e
  7. >>> math.log1p(10); n! ^9 b5 }4 @
  8. 2.3978952727983707
    % Y7 n9 v3 k$ L8 A3 `3 o" a; q
  9. >>> math.log(11)
    5 T  |* V9 P) W( p! Q0 |  z( J  c% T
  10. 2.3978952727983707
复制代码

. u: ]& e3 s  P6 z8 t. L% \( Dmath.log2(x)  返回x的基2对数
& a# Z! t& @- M" n0 Z
  1. #返回x的基2对数" f$ m# H8 A5 R/ A
  2. log2(x)
    0 h; @9 Y' M" f# v& ~, c( v
  3. Return the base 2 logarithm of x.. W) k0 }  z% n' o  M$ w  u( \3 E
  4. >>> math.log2(32). d: w% X% z! V1 _; @" ]7 n5 N
  5. 5.0
    9 @) a) t7 L1 |* ^% p
  6. >>> math.log2(20)
    ! F, \+ t, m" m* p, R
  7. 4.321928094887363
    : ^. p, K) x5 f5 |; T0 j9 [) N3 x
  8. >>> math.log2(16)
    1 j' {6 l* L, q/ N. m
  9. 4.0
复制代码

5 E) w! F, K' Bmath.modf(x)  返回由x的小数部分和整数部分组成的元组4 H7 s7 f, {7 h1 m" a( m. q: |
  1. #返回由x的小数部分和整数部分组成的元组
    - J1 p: v" _# h! `
  2. modf(x). H* q% ?, P( \  w/ B
  3. Return the fractional and integer parts of x.  Both results carry the sign8 D: s/ L' w* _6 U, K3 I+ r& G! W- n
  4. of x and are floats.
    . t8 e$ }0 n2 d7 u5 \  w3 w0 ]
  5. >>> math.modf(math.pi)2 t. C* C' k! H7 r
  6. (0.14159265358979312, 3.0)
    6 G& q# h) a! {2 u/ A1 F
  7. >>> math.modf(12.34)
      u4 B7 `6 i+ @
  8. (0.33999999999999986, 12.0)
复制代码

9 Z" |# Z* s7 F4 h& `$ jmath.sqrt(x)  求x的平方根
1 B8 Y) r6 I2 j3 ~' V
  1. #求x的平方根
    , h2 G4 ]3 W% M
  2. sqrt(x)0 s( [& Y. L- E! z4 T
  3. Return the square root of x.$ z# t7 |: J5 b. O- K; i7 K; t
  4. >>> math.sqrt(100)
    4 a1 Z) N. p# r2 Y
  5. 10.0
    6 c8 h; \& h0 E9 \
  6. >>> math.sqrt(16)# x' l5 K; m/ B$ X
  7. 4.0* t( v7 u" Y- G% z
  8. >>> math.sqrt(20)4 ^, v& f9 M' f3 m
  9. 4.47213595499958
复制代码

3 {" ^3 n; G& ^) Gmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    - r! d) W# l' E; w2 c% r
  2. trunc(x:Real) -> Integral
    1 Y  [) w: u% L: A
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* P2 S4 i7 E0 P' Z2 R; h; h
  4. >>> math.trunc(6.789)
      J2 Z1 s6 }2 G# e7 u
  5. 6
    ( Q2 k+ |0 y  t
  6. >>> math.trunc(math.pi)
    . F$ |1 l; b6 _  T
  7. 3, A7 `5 r; L  w
  8. >>> math.trunc(2.567)$ l! z3 B: w2 U+ |" |% t& w
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-30 15:50 , Processed in 0.083889 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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