新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1923|回复: 0

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

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

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

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

x

$ R( E9 d# O2 a2 i* n【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 w" g! D1 a# `
* z6 y: E, |: @6 }/ n
方法1
; S; e! W" Q( m8 ]+ a% _: i
  1. >>> import math. }0 g' n6 Z$ z2 ]1 j! J) V
  2. >>> math.sqrt(9)1 n6 h# A8 @: [; w: o' I; x# }* P
  3. 3.0
复制代码
方法29 V3 `" @7 J) v  F' k4 }
  1. >>> from math import sqrt
    4 ^) L! s) }* X# [
  2. >>> sqrt(9)
      z& ^' ^; B1 d
  3. 3.0
复制代码
, F# B* \2 g& t% b/ m- S


$ S5 i4 r" D( e) O1 Q
math.e  表示一个常量
5 m  b" V! u2 x, s! R+ a
  1. #表示一个常量
    ( r  l0 E: i- Y5 F: a8 N
  2. >>> math.e1 O& U( n$ G9 U  t
  3. 2.718281828459045
复制代码

) G# A/ _' x6 j9 ?8 ?- ~math.pi  
数字常量,圆周率

: D2 v! V; {. f* b0 C2 i, f
  1. #数字常量,圆周率( ]8 \1 B. V5 a2 z
  2. >>> print(math.pi)5 x( b4 t. W  e4 a% t2 f, ~
  3. 3.141592653589793
复制代码

# Y' s& y6 ?8 p" fmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

! `, z% i6 h- |& D9 q
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x) r6 M  t$ X0 Y
  2. ceil(x)8 s, \, T8 C: L: l
  3. Return the ceiling of x as an int.
    & f& L( f8 E& Q; t8 n
  4. This is the smallest integral value >= x.7 u% z+ |8 b" m* i) `2 C. @* |: d
  5. 4 l' B5 n/ D) {: C" o
  6. >>> math.ceil(4.01)
    0 m9 Q1 V. d' o, `
  7. 5
    8 t* S- O2 N/ Q! i2 |
  8. >>> math.ceil(4.99)' l) x; o' `; i& B) r9 d% q6 ]
  9. 5
    ( z) w4 w) V! W! A, N( I( B
  10. >>> math.ceil(-3.99)  P. M0 }( {- M0 o7 v0 E5 R# J  R
  11. -3
    9 ~6 E) `6 N- `: g1 y# I5 L
  12. >>> math.ceil(-3.01)
    $ s. n% J$ X- |6 o/ _5 r
  13. -3
复制代码
( b+ D* G" T, ]+ S2 e
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ Z$ Z, J1 c+ H: J
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身' _* E9 [3 m0 k  t# e
  2. floor(x); [# Q/ e: |  r
  3. Return the floor of x as an int.; d3 c' _- _+ b: U  N" l
  4. This is the largest integral value <= x.
    5 @8 z3 u- ^1 I
  5. >>> math.floor(4.1)0 `1 G' n# D2 t& y; h) Y% g1 I
  6. 4* t' D6 M) G, y# d
  7. >>> math.floor(4.999)
    . R) Z; x/ M. Y4 k6 q$ t
  8. 4! b4 [( A" a4 ^! h9 K/ C' [+ v
  9. >>> math.floor(-4.999)7 F8 I- S4 w* P( S, P
  10. -5: P1 J0 R9 \; Z6 p
  11. >>> math.floor(-4.01)
    ! D% u9 x9 ^& F! ]  m9 _' c3 y
  12. -5
复制代码
, g! a# X2 b& z7 D& z9 N
math.pow(x,y)  返回x的y次方,即x**y. Z9 x) |2 r- G" a! [, W! c3 _
  1. #返回x的y次方,即x**y3 V" T8 e7 Q& `3 d5 p
  2. pow(x, y)
    ! ^4 f& N. [- a5 t# C
  3. Return x**y (x to the power of y).# [# G- M+ U- L
  4. >>> math.pow(3,4)
    + t" J( b. M. d$ R% \* h
  5. 81.0
    1 ~( m! e+ O, Q2 F$ ^# b
  6. >>>
    1 R! _! W8 Y3 y+ k
  7. >>> math.pow(2,7)* b( x9 }, t; w7 Z! B, b  Q
  8. 128.0
复制代码
" R; m. ^! S" s1 D3 [
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* M7 S, ~0 x; J) A; ^5 U
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    2 j# `9 E/ q: n6 J
  2. log(x[, base]): L$ Y1 w% h7 W) m/ t1 Q
  3. Return the logarithm of x to the given base.3 D2 ?/ }& l0 T& ^! o
  4. If the base not specified, returns the natural logarithm (base e) of x.
    & v6 O# m. f: M% P+ N0 V8 W
  5. >>> math.log(10)3 ]2 U. d8 B3 M" U# j
  6. 2.302585092994046
    4 A( \6 k/ p7 Y7 ^; F/ O" g
  7. >>> math.log(11)
    & i3 `; N* K0 \0 x: ]$ s
  8. 2.3978952727983707
    , E, ~" K- o3 [  d3 t/ m
  9. >>> math.log(20)9 [5 r; g3 ]8 O8 R& _; E: m
  10. 2.995732273553991
复制代码
$ ]) R, S2 U' U7 Z& \# q0 N
math.sin(x)  求x(x为弧度)的正弦值
! l, X- T1 L- G6 A  n
  1. #求x(x为弧度)的正弦值
    4 Q4 Z8 q4 Y1 B9 ]  K4 i( e2 O
  2. sin(x)
    8 a2 L) K# }0 w$ G4 p
  3. Return the sine of x (measured in radians).
    ; }5 R1 c5 ~; D$ c6 s
  4. >>> math.sin(math.pi/4)
    8 L( Z0 `+ `9 P* v% |0 P6 c
  5. 0.7071067811865475
    3 c" c+ y1 e4 I& U5 t' b( r1 u: y
  6. >>> math.sin(math.pi/2)
    3 z" B: @  _& o8 N, m) z$ G
  7. 1.0
    " S0 F; F% k0 k( }' x
  8. >>> math.sin(math.pi/3)$ ]2 z; a. J& p3 {' |) X7 l6 e
  9. 0.8660254037844386
复制代码

" a' U5 W8 \! q8 [2 y. e2 S2 Smath.cos(x)  求x的余弦,x必须是弧度
& Y! [$ m. j: q7 b& C/ f/ s: q
  1. #求x的余弦,x必须是弧度* R) c( K9 w6 f
  2. cos(x)( j% M  y" A/ ~" g
  3. Return the cosine of x (measured in radians).
    % W0 I2 Y8 A7 ]) {
  4. #math.pi/4表示弧度,转换成角度为45度
    4 W. r/ q7 U3 N! ?9 {3 ~0 U+ A
  5. >>> math.cos(math.pi/4)
    * d" C" @5 H+ V6 k) H) i
  6. 0.7071067811865476: I! |, k7 q( j; t  f$ d" p3 e9 W
  7. math.pi/3表示弧度,转换成角度为60度
    2 g+ d0 a/ d0 v% s: |
  8. >>> math.cos(math.pi/3)# @$ i/ f) q" F5 ?) R1 s0 \
  9. 0.5000000000000001
    6 N2 W! P  @' I2 U4 N+ ^1 Q& d2 _
  10. math.pi/6表示弧度,转换成角度为30度( V4 P/ @& ~% G  ^( {
  11. >>> math.cos(math.pi/6)
    . v  G, ]% q/ z/ `
  12. 0.8660254037844387
复制代码
9 Z5 q3 x* M! |4 \
math.tan(x)  返回x(x为弧度)的正切值6 ~; x9 p2 C+ _% s" n8 F$ ~/ G9 h( e) F
  1. #返回x(x为弧度)的正切值! I: N! ~, ]. r+ K
  2. tan(x)
    7 A7 ~5 C3 U" v  ^
  3. Return the tangent of x (measured in radians).
    & L1 H: g1 j  C2 R
  4. >>> math.tan(math.pi/4)2 V7 I8 q0 N+ z% g* R: x
  5. 0.9999999999999999
    % B" }( E6 J9 W
  6. >>> math.tan(math.pi/6)
    # q9 Y" T, I: W! v! o% D& t1 c9 i
  7. 0.5773502691896257
    1 }# C' {- l  E$ M$ G
  8. >>> math.tan(math.pi/3)  |1 e, l9 n4 x
  9. 1.7320508075688767
复制代码
  h% S# r& N6 H6 ~
math.degrees(x)  把x从弧度转换成角度( U9 H. `! }: o# R( ?
  1. #把x从弧度转换成角度: z( @9 K1 I) J+ S" f( V
  2. degrees(x)
    , T4 Q' O! P# r
  3. Convert angle x from radians to degrees., K' ]$ f1 Y' r  R7 H
  4.   ?  M' i  r" E, l
  5. >>> math.degrees(math.pi/4)
    / x$ o5 _% \+ k: u# M) w
  6. 45.04 u% U- u) n8 |' v% R5 a, m
  7. >>> math.degrees(math.pi)
    2 [% [% Z- ?: Y! T0 H! B2 g# E5 F* ~
  8. 180.0$ x$ o, X* D. c" S# _1 z
  9. >>> math.degrees(math.pi/6)9 p& q) i. c$ g2 n
  10. 29.999999999999996
    9 ?0 g& o6 J6 M# t
  11. >>> math.degrees(math.pi/3)
    3 {& ?! J) F! V
  12. 59.99999999999999
复制代码
1 c5 s" K$ _, A7 U4 C: i% \* F, c$ B
math.radians(x)  把角度x转换成弧度5 x& @8 i1 G9 W" c$ ?
  1. #把角度x转换成弧度
    2 V% F+ V  r/ j/ W
  2. radians(x)& {& B; N6 I% s- e
  3. Convert angle x from degrees to radians.9 ~* _; Z/ |6 X) p
  4. >>> math.radians(45)% ^/ ~# _* `) H' L+ p, L
  5. 0.7853981633974483
    ( y, L8 l& g& L* g
  6. >>> math.radians(60)+ n; A; p- t. C6 K. R: W
  7. 1.0471975511965976
复制代码

9 x/ k: c1 j' Hmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
- Y# K' j- ^  o9 X3 U; P2 {2 X' G
  1. #把y的正负号加到x前面,可以使用0" }, n) }( l- I  g  y6 ^% h) T
  2. copysign(x, y)
    9 u! N' G. A5 D" w; z
  3. Return a float with the magnitude (absolute value) of x but the sign
    ) ?# s) k3 \- w# D$ E) ]3 G
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ' ^! h% x, X, g0 o- {. F) j% t2 y3 I
  5. returns -1.0.& ~/ a" w1 ]2 b
  6. 3 [+ I* l2 U8 u- L
  7. >>> math.copysign(2,3)2 m9 Z7 f' b  a9 v9 R" R7 ~
  8. 2.0
    + m' n1 o0 M0 a8 l! N
  9. >>> math.copysign(2,-3)2 P2 S9 [" K. H# q: [
  10. -2.0
    * r  J9 Z" p6 Y+ w& T
  11. >>> math.copysign(3,8)
    , C# P7 c1 I6 A
  12. 3.0
    " a- Q/ f! A) R0 U4 T
  13. >>> math.copysign(3,-8)* M' s* y7 {! r& [* G5 F  I
  14. -3.0
复制代码

6 S) y0 d& b( |% c$ D  Tmath.exp(x)  返回math.e,也就是2.71828的x次方
; f  A: n1 l- |* }  s$ Y$ L( r1 j
  1. #返回math.e,也就是2.71828的x次方$ {+ x( M* H. _, `1 p1 y
  2. exp(x)
    * p% l9 {7 x+ T. Y( l8 Y0 E1 x
  3. Return e raised to the power of x.
    2 o/ {. p6 g/ n6 q6 Z* o
  4. : N* h: i& P( {! i2 ?" @$ j
  5. >>> math.exp(1)
    * k2 t1 u6 |$ r( V
  6. 2.718281828459045
    , K4 b& y& w7 {5 P5 }, j" Z
  7. >>> math.exp(2)
    - u+ U2 w( q6 [: l
  8. 7.38905609893065
    7 O5 _, e; N( `% r! ~0 i% m
  9. >>> math.exp(3)7 r) ~8 [8 R* [/ G8 ?* d
  10. 20.085536923187668
复制代码
% P/ a6 T+ i) g
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1$ Z" N4 Z4 d$ [* X3 t
  1. #返回math.e的x(其值为2.71828)次方的值减1
    $ r0 @$ S4 B- W7 c
  2. expm1(x)( }+ Z6 P" E% t! S9 g! @
  3. Return exp(x)-1.- L: f' |# b! |( [- ^, H' M" i: x+ D
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 J: C4 o! Y/ n
  5. ( J9 w, P9 j  N6 M- u& |$ P; D/ Z
  6. >>> math.expm1(1)3 |  P" g; Q; \/ `5 w8 i1 M; h
  7. 1.718281828459045- r/ W% I4 B9 [1 {& U4 c9 @
  8. >>> math.expm1(2)
    * L5 `1 }) E) G
  9. 6.389056098930652 n3 a2 O- O1 j: x7 t- Z
  10. >>> math.expm1(3)
    / Y) B9 N( e7 W% C) V6 N7 l
  11. 19.085536923187668
复制代码

  _) I0 v, c- q9 F3 zmath.fabs(x)  返回x的绝对值
6 G0 {1 \/ M% p
  1. #返回x的绝对值
    6 u+ v8 D1 P1 b4 V: E' e
  2. fabs(x)
    ) v$ m% s, Z2 V) c) y) E
  3. Return the absolute value of the float x.
    7 B! G7 E/ J2 |  y3 C" v3 K

  4. ' K* z9 C2 [# n4 A3 a$ N: W; T
  5. >>> math.fabs(-0.003)0 D1 B& K6 D; z* Z3 [
  6. 0.0034 U4 m% c, b) l. g7 k* T
  7. >>> math.fabs(-110)
    $ d$ N2 h" x0 `7 W
  8. 110.0/ _( c* ?  |  ]/ s% K( X0 K; R
  9. >>> math.fabs(100)2 M+ l2 ?$ n# N+ X
  10. 100.0
复制代码
* J- o: }( P1 C0 w" H
math.factorial(x)  取x的阶乘的值; i3 w# U# S& S7 B; Q5 T% w# C
  1. #取x的阶乘的值
    0 u4 A9 l6 i+ j1 c- f+ |: ?
  2. factorial(x) -> Integral
    ( Y. H2 R5 r+ r4 H
  3. Find x!. Raise a ValueError if x is negative or non-integral.) V' g, ~, l/ f* ^0 i% W
  4. >>> math.factorial(1)
    / {( v6 v5 u% @
  5. 1
    + Q. [* J/ j! f+ E) S7 G
  6. >>> math.factorial(2)# X) s# w8 d5 V" D
  7. 2+ [% d7 i1 ?6 V2 ?+ i
  8. >>> math.factorial(3)! W* k8 _  m- ]) B( H3 L
  9. 6
    : `% i1 O, h# z* c4 E2 n3 y$ @
  10. >>> math.factorial(5)
    & o6 G: y+ n, a+ Z' u
  11. 120  N& W8 w  Z6 E$ q5 o
  12. >>> math.factorial(10): F0 I# H$ q  [" l
  13. 3628800
复制代码
( n9 K& ], q) m. Z
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
* M- N; y8 J0 x: p% O. r4 J) r, q
  1. #得到x/y的余数,其值是一个浮点数% |+ Q2 B8 A) G
  2. fmod(x, y)
    " C$ }  e2 {! O* T
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    3 Y; I2 f9 t" N! D5 g; H
  4. >>> math.fmod(20,3)
    % o% v* Y1 R# q& _9 ?3 B, g
  5. 2.0
    ; T' D9 S# }7 _3 R4 K/ L4 q
  6. >>> math.fmod(20,7)
    7 B; P% b* X) \, {* U1 |
  7. 6.0
复制代码

; ~8 f, o' p+ ?- Q9 Tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
- z+ L( T1 D. V( l& Q7 Y0 ]
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    - M$ l9 p, o; o  z4 J& v
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. ~8 \) q# d3 ~1 }8 _
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 j9 z2 \! Y, d6 ], N' [; V
  4. frexp(x)
    + p/ j. m; e3 Q  d' w
  5. Return the mantissa and exponent of x, as pair (m, e).
    ; S% N8 e: R- U& e! a- ?
  6. m is a float and e is an int, such that x = m * 2.**e.
    6 |+ ?# E2 ]2 [! Y. _
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    $ t9 C. ~) F6 n9 O- S3 h
  8. >>> math.frexp(10)
    4 c0 H* G) c3 f4 y4 P
  9. (0.625, 4)7 |- n  r$ a+ d: q6 _# a$ @- K# I
  10. >>> math.frexp(75)* X1 y  c3 K7 |4 N( s: t
  11. (0.5859375, 7)) a# z3 C: ~! h* {4 H0 ^
  12. >>> math.frexp(-40)+ P' d9 O+ I- z( I1 a1 s4 j3 K
  13. (-0.625, 6)
    & u* S& w: ^& N; b
  14. >>> math.frexp(-100); }/ R  F5 I, O
  15. (-0.78125, 7)9 r% @" A% o8 P" b, k3 R" F
  16. >>> math.frexp(100)
    ; j% ^9 q, j3 K% }* l' X5 m
  17. (0.78125, 7)
复制代码
2 r  T4 f4 }  j- k: Q, o
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 _3 ~# O5 V4 c$ A
  1. #对迭代器里的每个元素进行求和操作; e( Q/ V8 _1 k8 Q) r
  2. fsum(iterable)
    ) G* Q6 R2 l& n4 `; G
  3. Return an accurate floating point sum of values in the iterable.
    + e- a) B) D3 G/ q1 `
  4. Assumes IEEE-754 floating point arithmetic.' a% [7 f) q( p
  5. >>> math.fsum([1,2,3,4])
    0 t& k+ ~7 j( u% V$ F
  6. 10.0+ C; [3 e/ N  ^$ `4 z6 w9 r# `
  7. >>> math.fsum((1,2,3,4))
    5 s# i" C) e# h- m# i) ]
  8. 10.0' |2 a5 I8 {  E, [6 [7 a
  9. >>> math.fsum((-1,-2,-3,-4))$ o, |4 U5 i) ^0 F  u
  10. -10.0
    + O/ c7 K- X0 m; k1 m
  11. >>> math.fsum([-1,-2,-3,-4])
    2 f0 h8 F$ w& n$ X- Q; v
  12. -10.0
复制代码
& |4 s$ W! c3 e5 m5 |# k0 r& v
math.gcd(x,y)  返回x和y的最大公约数
$ @- j$ l$ ^" E; j: c- ^
  1. #返回x和y的最大公约数) C) W! c/ {6 o0 T
  2. gcd(x, y) -> int
    ( l/ p8 q1 n& ^; r. @; @/ S
  3. greatest common divisor of x and y1 q) I) y4 @. |; \& e+ F- L
  4. >>> math.gcd(8,6)
    * S7 E4 F% Q, P. F8 m
  5. 28 W+ U  l* x: k9 e8 Z% V3 O
  6. >>> math.gcd(40,20)
    9 d6 V5 Q/ K  g3 q' R; T, k
  7. 20
    & C8 \/ Z# b! W$ L3 b( t
  8. >>> math.gcd(8,12)1 x- U1 I8 g4 T' }( V
  9. 4
复制代码
) j% M& _: z4 c$ F' O0 u
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False7 q8 Y0 F, Y% k4 ^0 b& Z2 Z
  1. #得到(x**2+y**2),平方的值
    6 S! `/ y0 x; Q6 G+ S
  2. hypot(x, y), |7 M. I5 m5 E9 @3 \
  3. Return the Euclidean distance, sqrt(x*x + y*y).7 N; Y) n' `  \
  4. >>> math.hypot(3,4)2 e2 n, d% C/ ^( y0 y- x
  5. 5.0
    7 u' z3 d8 |+ A& m. e
  6. >>> math.hypot(6,8)/ V& ~- R/ @$ U  e
  7. 10.0
复制代码

# I; Q, S5 T7 m) [9 zmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
6 R3 |$ p2 U. P- S, d
  1. #如果x是不是无穷大的数字,则返回True,否则返回False3 z/ ^' l" u+ |0 n: r4 ?/ D2 D2 k
  2. isfinite(x) -> bool
    ) a6 W, T4 r& `, [! y$ c! o
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    $ p- p6 w) [) r6 X) a  k
  4. >>> math.isfinite(100), Z' a! ~3 w! d) }$ s* x
  5. True
    1 O- U) F) `* w( L
  6. >>> math.isfinite(0)
      }9 }% y" o/ n, z/ j! N) P
  7. True# y3 f' Z5 u( ?1 O1 G% D
  8. >>> math.isfinite(0.1)
    9 N7 d1 F  s% t  I6 f
  9. True- k" E- Y, R: s5 P7 {
  10. >>> math.isfinite("a"). x! X2 }& I: V! \. u
  11. >>> math.isfinite(0.0001)2 A# {6 u: P( |% G* |
  12. True
复制代码

, y9 G) R6 G7 X, x; O0 n9 R* Pmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
2 ~4 o; b8 y6 L+ _
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ; \9 S0 P8 b6 u8 V1 [
  2. isinf(x) -> bool
    4 ]7 l% @  o) S: h5 C$ y, O, z7 K
  3. Return True if x is a positive or negative infinity, and False otherwise./ V3 c3 U. S2 }
  4. >>> math.isinf(234), q( m) d" B) K* y
  5. False
    ) y4 f, k, h" @% T' ]( A% R3 Y1 n
  6. >>> math.isinf(0.1)
    ' L8 e  n% L# K6 C! [; W: ]$ t
  7. False
复制代码

# u! t, G' M8 ~5 K& K3 W8 _# f+ Z" C" ?math.isnan(x)  如果x不是数字True,否则返回False# u+ \) G. o% L
  1. #如果x不是数字True,否则返回False) {/ i2 [4 O  [) d  e4 Z4 h
  2. isnan(x) -> bool
      o, Q( Z9 t( ^0 h! |, I
  3. Return True if x is a NaN (not a number), and False otherwise.0 Y# z7 T1 L4 C3 d6 P$ R
  4. >>> math.isnan(23)& Z& w' i1 Y- H. m( E, J7 u9 z9 M
  5. False! i' F2 t0 i, c, |+ c; c, D5 t; x
  6. >>> math.isnan(0.01)
    7 m# g9 k# ]6 v  X. B, ?
  7. False
复制代码
; O" ~& n3 v! w1 [- K2 m
math.ldexp(x,i)  返回x*(2**i)的值
) |5 [% m& L: _# v  A
  1. #返回x*(2**i)的值: M. L8 w& Y0 u- e
  2. ldexp(x, i)3 U, B( z* C1 U  |7 F
  3. Return x * (2**i).  F# j1 P) I3 H8 z/ P- Y
  4. >>> math.ldexp(5,5)7 m; y1 d- _/ N
  5. 160.0
    6 w  b. T! U4 X/ ?; E9 W& l
  6. >>> math.ldexp(3,5)
    5 ~1 ]3 d  A5 x+ a- j/ d
  7. 96.0
复制代码
/ l+ X& I9 a" Y& H! m9 q5 r4 ]
math.log10(x)  返回x的以10为底的对数
  P+ i3 T' _. `7 }  T
  1. #返回x的以10为底的对数  l+ n: _( c& r0 @
  2. log10(x)
    0 j) K# p$ W* P. @6 L
  3. Return the base 10 logarithm of x." d, m- T! o, v$ Y" t1 N
  4. >>> math.log10(10); W; ^8 C& d% P6 q: ~$ a
  5. 1.0. C- A+ ^" E% m/ b1 b6 u: {! {
  6. >>> math.log10(100)& M; z3 C1 ~. M
  7. 2.0* A- f5 {$ `8 A/ F
  8. #即10的1.3次方的结果为20( A3 B  e+ {5 z5 x/ B
  9. >>> math.log10(20)
    9 _3 O9 q; B" L5 o8 C4 {
  10. 1.3010299956639813
复制代码
; i) u( ^3 t" ~! z7 U) w; b
math.log1p(x)  返回x+1的自然对数(基数为e)的值
2 U( W1 x/ G% w
  1. #返回x+1的自然对数(基数为e)的值7 l) G+ p6 A& ^3 y
  2. log1p(x): [# i; I* ]  x3 M6 H. g8 F6 g
  3. Return the natural logarithm of 1+x (base e).
    9 A! n6 B6 t8 _8 u0 `& g" W
  4. The result is computed in a way which is accurate for x near zero.
    : ~; ~! X( \; w/ E4 P
  5. >>> math.log(10)
    . t- G. g/ \2 L9 h8 e! U
  6. 2.302585092994046
    1 s, v7 P) c: Q2 \2 l( F! j
  7. >>> math.log1p(10)% Z+ O9 x/ |6 N+ T7 F
  8. 2.3978952727983707
    # u* I- S2 u8 P3 [
  9. >>> math.log(11)1 A0 [' \6 U4 f6 ?2 T0 M/ o
  10. 2.3978952727983707
复制代码
* u- x. s- k: A8 g3 M- U
math.log2(x)  返回x的基2对数
' a1 @# [6 o( @; c( O! `# s
  1. #返回x的基2对数
    0 B  Q$ J0 X" ~
  2. log2(x)
    / t4 ]/ S- z  c" u
  3. Return the base 2 logarithm of x.; r% n4 S6 U  r# b" ^. K  L( A6 h. O
  4. >>> math.log2(32)8 j! S' J" r# d1 c2 V" H( z
  5. 5.0" g$ P5 P. C3 ^/ j- F
  6. >>> math.log2(20)" @5 n8 w5 Q8 s% d0 w" }; R
  7. 4.321928094887363
    : ^* `# t7 d% T0 s6 {
  8. >>> math.log2(16)
    8 W( a) _" K# l% ?- a- M" r( D! K
  9. 4.0
复制代码

" G, [$ h( v9 s4 h4 m+ B( a# imath.modf(x)  返回由x的小数部分和整数部分组成的元组) p: \. G4 w2 n8 R1 ?/ ?/ N
  1. #返回由x的小数部分和整数部分组成的元组2 j8 W* ^, m$ n6 ~. e3 l3 Q
  2. modf(x). h7 u2 K6 c! [+ r, x4 [- D' f
  3. Return the fractional and integer parts of x.  Both results carry the sign
    6 b1 ^2 ^7 A6 Z  Z
  4. of x and are floats.
    / F; b* X( B  B5 t8 J2 U* W
  5. >>> math.modf(math.pi)7 B8 z, _7 X8 U  y+ i
  6. (0.14159265358979312, 3.0). X6 t4 C. S* Y8 P
  7. >>> math.modf(12.34)9 o, }4 J. `* U, n$ i8 v
  8. (0.33999999999999986, 12.0)
复制代码
* z" J' L+ a) w
math.sqrt(x)  求x的平方根% _$ X" H( W7 I1 Y  i; g
  1. #求x的平方根" z! c: A9 L( ^" Q$ w
  2. sqrt(x)- |5 b. C4 ^0 o# r. U9 \
  3. Return the square root of x., H% T  G! A% _- r" S- p8 E, v/ B
  4. >>> math.sqrt(100)
    * M2 h0 l" ?2 J; c# i+ U
  5. 10.0
    9 @6 L5 U% N3 A+ G
  6. >>> math.sqrt(16)& {, g( y" W# f$ I* {8 V6 ~5 N
  7. 4.0
    ! m" e9 D1 C1 E
  8. >>> math.sqrt(20)" q0 ]7 B6 O$ h. C  _2 O
  9. 4.47213595499958
复制代码

% X( V' ]) I* }2 r' Tmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    + D/ s' P4 p) `+ P% L" k' |2 G
  2. trunc(x:Real) -> Integral% o$ t+ U: {4 a  S2 j
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ y+ }4 G# N: p  p
  4. >>> math.trunc(6.789)) G# \' D: K5 U7 `1 |! k' B
  5. 6  @) ^# Y7 N: a2 [4 L; G
  6. >>> math.trunc(math.pi)- s& Z% y, z  g8 a
  7. 3; I6 U. D9 E( H. G9 @3 B7 ~! N& t  ?
  8. >>> math.trunc(2.567)
      F0 k/ U8 b; m1 L! m" m3 Z  v. a
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-20 11:30 , Processed in 0.075627 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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