新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

" O+ V0 V% p# N* N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 O" m3 v) }! s% @1 \; D

! ?& W7 f' o1 Z' G- Z$ `& Y: D方法1
9 m  L  M1 N4 I% b8 q" t- q% E
  1. >>> import math
    " `% y& Y8 {! e, \
  2. >>> math.sqrt(9)
    8 Y+ @* N% ]. W6 g3 Z4 J7 t8 s- q
  3. 3.0
复制代码
方法24 H$ k) i" {# Y9 \( |
  1. >>> from math import sqrt" y! _- X# Y8 @
  2. >>> sqrt(9)
    . S: j2 n1 Y$ {* u
  3. 3.0
复制代码
4 E+ G9 D- E  X7 h3 S

# s2 P7 o! n7 V2 m( [+ i
math.e  表示一个常量
% ~; V$ v/ h/ M7 @3 F1 l! T
  1. #表示一个常量& @) t0 g' d8 s# ^# W- [7 V# m
  2. >>> math.e
    , Z& H- F/ o7 U' J/ a) d& v2 k
  3. 2.718281828459045
复制代码

3 l: k6 Q4 E, |math.pi  
数字常量,圆周率
8 P/ I0 g$ A8 Z$ ]8 t6 P6 n3 q
  1. #数字常量,圆周率# C% L5 \/ R1 Z: h
  2. >>> print(math.pi)
    3 S- q6 K3 `0 x
  3. 3.141592653589793
复制代码
; \1 s( K# G# F* B
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 S5 J# R7 o& v3 E# z
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    % a& I3 c# k3 k/ n4 S
  2. ceil(x)( h5 Q  g9 X1 K0 Z/ L! d+ v; z7 X
  3. Return the ceiling of x as an int.
    1 ^& g) `7 n# j
  4. This is the smallest integral value >= x.* O* n6 M- |  r5 a/ s* K- }; k

  5. % z% {# G! r% w0 W) G  |
  6. >>> math.ceil(4.01)
    7 Q% ?6 ~9 D9 j8 L6 {! [
  7. 5
    0 P" W9 G% z& r1 I6 Z
  8. >>> math.ceil(4.99)
      D  E. i. B  L7 t% {
  9. 5
    8 W/ ^0 z. I5 J: _  L/ J
  10. >>> math.ceil(-3.99)% d$ K8 x9 z, c# x- I2 S; G6 N8 X2 d: L
  11. -3
    % Z) q$ \9 A" L/ B; n
  12. >>> math.ceil(-3.01)9 _; C6 z6 Z; n! f, \  m
  13. -3
复制代码
+ \+ k% }; J5 w& q
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ d$ f4 M0 B) o- e
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    + w$ G! P2 i' F! i0 _; x
  2. floor(x)
    1 E- H+ A" J. W1 v- i9 F
  3. Return the floor of x as an int.
    3 m6 \. k; q* ~% L4 ]  J/ c
  4. This is the largest integral value <= x.8 c0 `' g) r5 S. n& {* a" U
  5. >>> math.floor(4.1)
    + x' p- n# S, d" U/ Z/ P
  6. 4
    ( a) f- L* ], w
  7. >>> math.floor(4.999)
    8 s7 V& V* L0 r8 r
  8. 4
    " r& g5 f; P& k8 q% P
  9. >>> math.floor(-4.999)
    : p* ~0 x% b) |; G7 D0 {8 g: q
  10. -59 }+ f( T- _+ {6 l6 N( r
  11. >>> math.floor(-4.01)
    3 N5 i" a' E  [8 `9 U5 `
  12. -5
复制代码
; ?! q2 O# O) Y8 b
math.pow(x,y)  返回x的y次方,即x**y1 W0 L* B4 W  ^0 @& C" x) A4 r3 _0 k+ \
  1. #返回x的y次方,即x**y  i2 n5 \" P8 L
  2. pow(x, y)
    5 D, ]4 B, X1 i" m' H
  3. Return x**y (x to the power of y)./ L! O' ?# Q# Y6 Y
  4. >>> math.pow(3,4)
    / K% P9 h3 L% v6 s
  5. 81.0& u  f$ N) _! y
  6. >>> + S; H4 D9 s9 s# h
  7. >>> math.pow(2,7)! b) o& ?* c8 [8 R8 |5 t5 m
  8. 128.0
复制代码
8 ]6 F3 h, X3 M2 G
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( K4 D7 h( t9 e3 \* @/ G- Q2 A
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ' ?9 S) @+ |# y) A; T3 w
  2. log(x[, base])% j+ E( X6 H* c
  3. Return the logarithm of x to the given base., H- V/ [: V5 D) P5 Z" z
  4. If the base not specified, returns the natural logarithm (base e) of x.
    - i) y' R8 y# _  b: w7 \+ S
  5. >>> math.log(10)* F7 S7 P* N1 U- R) P% g- t  q
  6. 2.302585092994046
    % ]* D, g) c$ ?# D5 A5 ~2 o: @
  7. >>> math.log(11)
    8 W9 R' \6 t1 `0 i; x' C
  8. 2.3978952727983707/ H$ X# |, @; |  _, {3 X, |
  9. >>> math.log(20)
      D# T0 d$ T6 J9 W: I% }$ R
  10. 2.995732273553991
复制代码

( ]9 v3 j9 ~" cmath.sin(x)  求x(x为弧度)的正弦值$ n: j  a5 g5 M$ |
  1. #求x(x为弧度)的正弦值- _5 d" ~7 L1 K. s' R, n: z
  2. sin(x)( Q6 X+ G2 a# M) R  F$ N  Q* O8 `+ {
  3. Return the sine of x (measured in radians).
    ' O( Q1 A5 \9 q
  4. >>> math.sin(math.pi/4)
    0 i7 n- _* y" |/ m5 @% p6 R! A: h
  5. 0.7071067811865475
    6 i4 U, y' J* k6 z- h, m& z
  6. >>> math.sin(math.pi/2)
    3 i* |- Y% Q0 z* E, H* }
  7. 1.0& M1 p" S; m+ h5 E0 l
  8. >>> math.sin(math.pi/3)
    + X# {4 ?$ F: D# q& I% v0 ^1 S
  9. 0.8660254037844386
复制代码

+ I0 G7 Y9 D; S( Gmath.cos(x)  求x的余弦,x必须是弧度
# Q* m8 a; `* i% U" N$ y& V
  1. #求x的余弦,x必须是弧度
    8 H' C: V* M! W, T" b% o  Q
  2. cos(x)) g6 @4 W" l" M; |
  3. Return the cosine of x (measured in radians).
      a) X/ _, F" c7 p# C- T. c6 p
  4. #math.pi/4表示弧度,转换成角度为45度' \8 o/ I5 S& X2 c
  5. >>> math.cos(math.pi/4)
    # [) l9 r' b$ @, }5 s- H
  6. 0.7071067811865476% c! P% h  F2 G" ?1 @' M' @
  7. math.pi/3表示弧度,转换成角度为60度
    " ?6 h# B  o( K8 g8 h6 r3 c2 d
  8. >>> math.cos(math.pi/3)
    + d4 \% a2 M  F2 n8 [2 h0 u" S+ P
  9. 0.5000000000000001
    3 O, l4 i6 }  l9 z' o' W' Y. R
  10. math.pi/6表示弧度,转换成角度为30度
    / S; v& N9 v! j6 Z/ Q# l
  11. >>> math.cos(math.pi/6); \3 C8 h2 z- i% @8 l3 i
  12. 0.8660254037844387
复制代码
6 ^' V+ ?2 A: c3 g. V: T& I
math.tan(x)  返回x(x为弧度)的正切值
! t- d* d& }) B; j, a( |
  1. #返回x(x为弧度)的正切值2 A" k  S/ h# t! m2 R; D+ u2 K
  2. tan(x)
    ) @2 T# `  ?8 v( e
  3. Return the tangent of x (measured in radians).5 e1 v  T9 C9 A: `+ Y
  4. >>> math.tan(math.pi/4)- K& J* ]: ^: I3 G, w% R
  5. 0.99999999999999997 |# L" V. c( d2 Z5 E
  6. >>> math.tan(math.pi/6)
    ! \% z9 r: O7 H: B  F8 T" [6 y  D
  7. 0.57735026918962570 i$ l9 Z3 D( c1 |6 q
  8. >>> math.tan(math.pi/3)2 y& p% D7 {7 y/ `$ e+ N" n  ], r
  9. 1.7320508075688767
复制代码
) z! C& E/ T" H( S0 q
math.degrees(x)  把x从弧度转换成角度6 o4 ^) m+ U) X" s7 B: a
  1. #把x从弧度转换成角度
    4 ^3 t2 c2 B4 h% B; K. W
  2. degrees(x)
      W. k" F" N/ j# }6 O% n5 I
  3. Convert angle x from radians to degrees.
    9 d$ M+ @* P, Z9 H

  4. , e/ l8 r% D  ^  z# H( P# X# S
  5. >>> math.degrees(math.pi/4)
    7 ]( v8 {% M8 X; q9 \
  6. 45.0: p7 E! c5 e4 _( l5 n9 [: {7 K
  7. >>> math.degrees(math.pi)5 ^/ [3 a: S  `. ~" M
  8. 180.04 b. g7 S4 M8 {: c# `
  9. >>> math.degrees(math.pi/6)
    / V) p) o6 s% a# M3 t
  10. 29.999999999999996! D% r; z0 u* A* Y
  11. >>> math.degrees(math.pi/3)$ U9 c0 N/ A; w) ^
  12. 59.99999999999999
复制代码
7 B7 O7 Z. {/ Y7 c4 |  k
math.radians(x)  把角度x转换成弧度
" F0 m0 m3 `! q0 A6 z
  1. #把角度x转换成弧度
    , {+ K0 S. j# u, ?* D: ]
  2. radians(x)
    ! N3 t- M+ u# x: u
  3. Convert angle x from degrees to radians.
    - S4 f. d! ^! x
  4. >>> math.radians(45)) t) u/ g% Q$ K% X5 }. j2 {4 t2 {
  5. 0.7853981633974483: U6 F2 p0 j8 b5 o% c' H
  6. >>> math.radians(60)" y: ^( H+ j* @( g) w9 v4 i5 }
  7. 1.0471975511965976
复制代码
! {  [0 s  x: A+ T7 s6 i, a; X! x3 w
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
: z0 T& p* a6 F( J$ C3 y
  1. #把y的正负号加到x前面,可以使用0
    $ m1 H* `: S: g: U0 p6 @
  2. copysign(x, y)
    / l: F& t. u* |9 t2 Z) \
  3. Return a float with the magnitude (absolute value) of x but the sign
    / k; T+ G5 u3 _: P9 Z
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    5 t% q9 M7 r) K, b. L" `
  5. returns -1.0.' F7 }' _5 U' Z7 D5 j9 |1 c

  6. . E) J' N2 ^) m$ h( B
  7. >>> math.copysign(2,3)
    " B8 H$ U' w/ p) p
  8. 2.0, b3 t/ h9 A- e# U. E
  9. >>> math.copysign(2,-3)5 o( q8 ]  }0 h6 ?. n) p
  10. -2.0
    5 n/ m1 ]8 _+ P* H
  11. >>> math.copysign(3,8)
    6 M' f" w7 `: R
  12. 3.0  a0 V7 J# }6 P
  13. >>> math.copysign(3,-8)
    ( K8 G# g8 _5 N" F# E2 ~; ]4 C
  14. -3.0
复制代码

+ B# L  S: g+ C9 e  ?7 Wmath.exp(x)  返回math.e,也就是2.71828的x次方) U4 p3 c9 h5 S
  1. #返回math.e,也就是2.71828的x次方
    5 c7 W7 _! o  ?* i) m; r: G
  2. exp(x)3 T7 L: v6 m! b. h  V5 C: x" ~
  3. Return e raised to the power of x.) d' ~; p% H2 x3 q! _9 I8 u
  4. ! S1 s. W6 A/ ?; ]& S" _4 x
  5. >>> math.exp(1)( L+ G2 j& I1 u) q! T
  6. 2.7182818284590459 k5 _* M& h( L/ _
  7. >>> math.exp(2)
    1 }: N4 k( H9 ~7 I2 a' Y& T
  8. 7.38905609893065  ?! {' w8 _2 B- X1 K
  9. >>> math.exp(3)
    * g! i. n2 j/ f- W6 a* x
  10. 20.085536923187668
复制代码

& G$ C: I; Z( U/ z! U  |4 Wmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减13 l3 a) n  P3 ~% B+ `. w
  1. #返回math.e的x(其值为2.71828)次方的值减1. |( S5 O! u3 e
  2. expm1(x)
    ' a  c. d' f" z8 F. {4 `, B3 p2 ]
  3. Return exp(x)-1.
    / F3 U% f& H5 C# \' D! J
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 `. j: u7 f2 r& v
  5. 9 X5 H. x6 X- n! M1 q7 s% }
  6. >>> math.expm1(1)9 k+ [$ k* h- W" ^" ?  g0 g
  7. 1.718281828459045
    - R, Z- x+ o. U+ S- I
  8. >>> math.expm1(2)
    7 r2 v1 Q: }/ ?8 q, a2 M
  9. 6.38905609893065+ N9 y/ e  N  b2 X4 i" D
  10. >>> math.expm1(3)
    , R# ^* Y4 K3 U, @) r
  11. 19.085536923187668
复制代码

+ `$ Q/ ~$ I% K* i0 i2 s8 Kmath.fabs(x)  返回x的绝对值: Z- o1 Y8 _$ Q% z6 k
  1. #返回x的绝对值; p* F! c' U$ h3 Q7 L/ U7 Z
  2. fabs(x)
    1 _, \8 i$ d, M& b
  3. Return the absolute value of the float x.1 k4 ^) k. ?" m
  4.   {4 l0 P( D7 z0 ^9 y) j
  5. >>> math.fabs(-0.003)8 m$ w; W8 u0 l1 |$ E5 K
  6. 0.003
    4 E; k% j" N# R- f
  7. >>> math.fabs(-110), D. ]' A; S/ W' Q  n& g
  8. 110.0
    6 J" N8 l$ s% @' X9 n3 W0 ~
  9. >>> math.fabs(100)
    - L% C2 l, T5 q  g
  10. 100.0
复制代码

. \3 ?5 b) M' W) L  L& C1 ]math.factorial(x)  取x的阶乘的值% J+ _4 D+ x7 @+ C( h2 M4 r
  1. #取x的阶乘的值
    # R1 F3 D: p; q& r. ^3 S' a) j
  2. factorial(x) -> Integral3 t; @& ~+ f8 D. c1 r$ F9 s6 I
  3. Find x!. Raise a ValueError if x is negative or non-integral.5 j; O/ }2 P" u( p3 ^- S
  4. >>> math.factorial(1)% h- |& O! t( u; h& N
  5. 10 _2 E% t* T  h5 ~- q" X6 `
  6. >>> math.factorial(2)
    ) T* U: b# p9 g6 T8 Y9 S# I# P8 C
  7. 2
    4 X# G9 y9 P) T* M
  8. >>> math.factorial(3), _, ]& X3 k/ R: c
  9. 6
    ! Y) U; |9 {' m# f0 F9 c! |
  10. >>> math.factorial(5). O+ C1 V# L$ n3 Y
  11. 120
    1 P$ Q  K+ F( I4 H! U% R+ g. R' P- R
  12. >>> math.factorial(10)' U* N9 ?8 x) ]$ p2 r: a( M
  13. 3628800
复制代码

+ \' q( f/ R5 C4 v" Gmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数4 d8 I5 ^7 t* U+ A- K; o# I
  1. #得到x/y的余数,其值是一个浮点数
      [# j/ _7 Q" i( k' v5 y/ i; {2 e
  2. fmod(x, y), U3 v; k3 h8 {; H
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    5 q) {% L8 \8 i' t. ~
  4. >>> math.fmod(20,3)
    7 v0 t7 r4 U  u
  5. 2.0
    & @. a0 P# i6 p3 }4 n  Y' T
  6. >>> math.fmod(20,7)" q7 s; [4 s( M8 c7 v% P1 X
  7. 6.0
复制代码

6 \  _5 S. I! E3 Z7 ~$ jmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ j5 F, ^& d& r8 B( j( p2 J
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,  u7 Y9 o: |/ I* l( i: V
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' ]  _8 M; M; q% k
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' K& k8 Y# B" ^+ t
  4. frexp(x)
    + w+ h$ u/ m+ z; A5 a' L' B
  5. Return the mantissa and exponent of x, as pair (m, e).
    & x9 ?  n) k8 `: G1 Q. r" r
  6. m is a float and e is an int, such that x = m * 2.**e.# \6 a" z( @9 @4 E8 v
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.# @: F) y, |) K. W
  8. >>> math.frexp(10)9 w; X; r1 H. G
  9. (0.625, 4)1 g' Y; z. x- O4 F3 L
  10. >>> math.frexp(75)& Z0 i. w! D3 W, }, V
  11. (0.5859375, 7)
    0 O' S  T3 g" [0 r( u' V5 n1 r% ^
  12. >>> math.frexp(-40)
    0 E* f/ ~0 M. Q/ C% g( R
  13. (-0.625, 6)) I% ?9 z. d- Y
  14. >>> math.frexp(-100)
    1 e2 L$ a: h2 {. Z
  15. (-0.78125, 7)
    / |2 Q, ]/ S" L0 l/ ]( B! I
  16. >>> math.frexp(100)- Z; Z: o- f5 d) |$ m8 J2 E. `5 u
  17. (0.78125, 7)
复制代码
, d$ {' Q+ U! i7 ~
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列* z, S6 t' `4 E6 M6 y
  1. #对迭代器里的每个元素进行求和操作+ V+ x8 Y1 N1 i  k
  2. fsum(iterable)* v. p% F0 j. A. s$ O
  3. Return an accurate floating point sum of values in the iterable.
    " a. {2 J) ^  g- f! b2 w: ]3 ~
  4. Assumes IEEE-754 floating point arithmetic.7 _7 e9 g2 _/ T! x
  5. >>> math.fsum([1,2,3,4])
      N! |  _  x3 y9 O
  6. 10.0) ~% f, ~& n/ ]
  7. >>> math.fsum((1,2,3,4))$ |$ k1 T) J# R: X
  8. 10.0' B0 @- v+ Z/ F2 |( J% E7 k
  9. >>> math.fsum((-1,-2,-3,-4))
    5 c6 x# i* K3 D5 P+ w
  10. -10.0
    0 ]! _( S. p& v7 x
  11. >>> math.fsum([-1,-2,-3,-4]), J+ u8 V) S8 r- b% C7 Z( _! C
  12. -10.0
复制代码
1 `0 ~3 a: v0 O% L# N4 o5 e8 b6 V$ F
math.gcd(x,y)  返回x和y的最大公约数& r( n: `8 }* Z8 Y) l" O
  1. #返回x和y的最大公约数
      S9 ~- m! ?5 H
  2. gcd(x, y) -> int8 |( G  l$ @; n5 u- G, \
  3. greatest common divisor of x and y
    . ]8 l# \3 ]* t, }- g. x
  4. >>> math.gcd(8,6)
    ! y( o$ m  t6 Y
  5. 2
    ) a! e" ]3 |- T* w# d3 g0 W
  6. >>> math.gcd(40,20)
      h" c& O  k6 K
  7. 20  {% l( {' y  O2 d, @
  8. >>> math.gcd(8,12)4 N! v8 `; {; s7 w2 a
  9. 4
复制代码

& q) r) ]8 v: B7 pmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False0 v3 ]$ I( F. H4 E2 i
  1. #得到(x**2+y**2),平方的值% y% H1 I7 ]5 [+ ~
  2. hypot(x, y)
    $ u% X" q$ K5 {# `! V
  3. Return the Euclidean distance, sqrt(x*x + y*y)./ e$ K4 `) Q- |. M/ t+ Y  r8 M
  4. >>> math.hypot(3,4)8 k9 ~9 E) b: H  @
  5. 5.0
    ) ^. z) B1 V; f5 M
  6. >>> math.hypot(6,8), }( [, m# r9 J4 _9 U( ~3 f
  7. 10.0
复制代码
  {7 `( x6 K/ E9 c2 U+ x) v, z) B
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 ^! B/ `* n. Y# H- z' u, _) g
  1. #如果x是不是无穷大的数字,则返回True,否则返回False. @0 A7 B$ j- K5 r5 K7 b6 u
  2. isfinite(x) -> bool
    # D+ `2 {0 U6 G' z5 E
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.; G4 _$ j8 F: f1 u. G& w
  4. >>> math.isfinite(100)
    + A* w7 C5 U7 |( @3 J' ~
  5. True  m* e: |& l2 `' _9 B2 a1 t
  6. >>> math.isfinite(0)
    5 D, q; y6 t1 l# o& l( c
  7. True
    , U3 D1 G! M) {
  8. >>> math.isfinite(0.1)& a+ t! x' l! q  u, c9 G/ Q: u
  9. True' j" U% G5 k2 X$ N  v6 }
  10. >>> math.isfinite("a")9 @0 f* g0 E. I, i6 i# c( q
  11. >>> math.isfinite(0.0001)
    ; {/ Y9 i6 d, [6 N& G$ [8 S1 W% E" I
  12. True
复制代码

0 I7 P' Z0 q% {math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
, n2 D2 |6 o( l! z3 Q6 c1 q
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    + C& k, ?4 ^6 D7 W- P$ u! Q
  2. isinf(x) -> bool
    ) c5 P  Y) x' S8 j- s
  3. Return True if x is a positive or negative infinity, and False otherwise.5 q) W, d7 K: e8 S6 z& c/ e# I7 n
  4. >>> math.isinf(234)
    $ V: h  A+ h1 v2 r5 W
  5. False
    8 \6 H( @% K& s4 @
  6. >>> math.isinf(0.1)
    6 ^7 @% e1 d! y- O( L# w; M. @
  7. False
复制代码

+ c# i( t. I1 p5 k1 ]# k: E2 `math.isnan(x)  如果x不是数字True,否则返回False0 N: ?7 z; C& c' U: S: V/ ^* j
  1. #如果x不是数字True,否则返回False
    9 V( W. O; T  K. e
  2. isnan(x) -> bool
    & d3 g8 ^, ?9 |# I3 S) {
  3. Return True if x is a NaN (not a number), and False otherwise.. ~: s) g1 D& {$ F# y1 ~, g! [
  4. >>> math.isnan(23)
    , ]; a, K7 L; C5 W3 d. z8 O3 [
  5. False/ h' a4 N7 X6 L3 V* y4 P
  6. >>> math.isnan(0.01)# p; D/ z0 B2 B8 [  V
  7. False
复制代码
6 ]7 J5 k  X: q7 s) ?- y
math.ldexp(x,i)  返回x*(2**i)的值
5 F" C: m# t) Z0 G' c& R9 g
  1. #返回x*(2**i)的值& N1 t  ~8 j1 j/ T: S2 `+ |
  2. ldexp(x, i)% s. S' e) q0 w
  3. Return x * (2**i).; ~+ t/ ~  {, @7 k6 a( G
  4. >>> math.ldexp(5,5)
    / M; c3 E- v7 X. B: L' {1 w" i
  5. 160.0
    ! h9 w+ z. M, n
  6. >>> math.ldexp(3,5)" W9 m3 }, m' ?4 z5 S& T" h
  7. 96.0
复制代码

1 _6 p5 B( C6 V3 a5 t( a+ jmath.log10(x)  返回x的以10为底的对数: r* ^8 w7 \/ t: A, A
  1. #返回x的以10为底的对数( }  I- e4 m( c. X! Z) V8 r
  2. log10(x)
    5 w1 o- {* \( U: m; q
  3. Return the base 10 logarithm of x.5 V- _! L% ]/ W1 I
  4. >>> math.log10(10)
    ! F$ [9 T. h. i4 B5 u% C# x" h8 |
  5. 1.0! ^1 K: p) @0 J: ~
  6. >>> math.log10(100)
    8 U8 d; H# D) H6 T; ]- c
  7. 2.0
    : i% D3 Y/ ~/ y) o9 p" e- H; e
  8. #即10的1.3次方的结果为20- c3 k2 y1 h% q* n* ~, C
  9. >>> math.log10(20)# F* S3 `4 z& O7 C! @, M
  10. 1.3010299956639813
复制代码

& z: h$ W" F% ]5 [math.log1p(x)  返回x+1的自然对数(基数为e)的值
, f) \  a# H7 z
  1. #返回x+1的自然对数(基数为e)的值
    : D$ z: T4 y# K# v/ W3 Z7 h
  2. log1p(x)
    8 n2 B9 e, F5 A& W+ I' `
  3. Return the natural logarithm of 1+x (base e).
    ( p) {( h1 \' d
  4. The result is computed in a way which is accurate for x near zero.6 s, ^5 b4 F, I! _9 n# ~$ ?
  5. >>> math.log(10)3 Q- c  n  q5 y8 s6 E6 H3 |/ N# L
  6. 2.302585092994046
      r  `" d& H7 b5 v  ^  w1 W
  7. >>> math.log1p(10)
    8 T1 p$ ^  A/ y* R& X0 q9 P
  8. 2.3978952727983707) Q+ G4 c, L: s+ ]5 X8 U; Y7 ?
  9. >>> math.log(11)
    ' W5 B# e6 x7 Y* W, s6 G! o
  10. 2.3978952727983707
复制代码

5 r+ b% ^2 Z! m  l/ ]  h1 \# [. Lmath.log2(x)  返回x的基2对数( c- J% n5 d+ A! |% x( T
  1. #返回x的基2对数
    6 \6 P- Q+ @; \1 u) U* F
  2. log2(x)) ], R; `7 g) a
  3. Return the base 2 logarithm of x.- \& Z0 A  S/ N5 ^( I
  4. >>> math.log2(32)
    7 o6 K8 W8 z6 s& H8 l
  5. 5.0
    ' m; \: T! G& e. p. D
  6. >>> math.log2(20)6 a  p' P. c6 G& h
  7. 4.321928094887363
    0 ]0 n  z5 I  v* e+ E+ {5 c4 u
  8. >>> math.log2(16)7 \2 ~8 R- O3 Z7 ~* Y' V3 Z
  9. 4.0
复制代码

0 _. ]' v" {% V2 ^math.modf(x)  返回由x的小数部分和整数部分组成的元组! P5 C# [! j- ~# Q& Q
  1. #返回由x的小数部分和整数部分组成的元组! a* m  Z$ q6 e: A9 X0 U
  2. modf(x)- |$ Z' y) P0 y3 T2 o
  3. Return the fractional and integer parts of x.  Both results carry the sign- G) [% S7 D" H% ?; H% V
  4. of x and are floats.
    / ]( O+ O+ ?8 x. O2 ?* b9 m7 U* k
  5. >>> math.modf(math.pi)$ k3 |5 h6 i2 U7 `4 x
  6. (0.14159265358979312, 3.0)
    ) o9 o% d9 r9 x; a9 M
  7. >>> math.modf(12.34)
    . M* F4 d3 Z3 V) f5 G8 u
  8. (0.33999999999999986, 12.0)
复制代码
9 ~3 ?8 p2 n: Y, B
math.sqrt(x)  求x的平方根
6 u3 |6 R2 W: h
  1. #求x的平方根7 x' N0 ?, S6 ~' D9 `! i5 @
  2. sqrt(x)
    - U% c7 k, y" g2 [
  3. Return the square root of x.
    , Q! L! K+ y% z
  4. >>> math.sqrt(100)7 \9 i' x) U9 w0 }* K5 `
  5. 10.0/ ?# i0 |: ^: T0 s) l9 Y( Z
  6. >>> math.sqrt(16)
    * }! Y/ D) Q8 y9 t
  7. 4.0$ p# n/ G4 p  B2 D* h( y
  8. >>> math.sqrt(20)
    . \# @2 n& H! b& [* ^
  9. 4.47213595499958
复制代码

- w- v& `; ~8 U) z/ e- y4 tmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分7 I4 U/ Z% B6 ~: \; t+ I
  2. trunc(x:Real) -> Integral7 B; q4 @* W" l- A# F' u
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    0 L/ f2 x! i. ?) S, `8 X1 P
  4. >>> math.trunc(6.789)/ R7 l3 t7 c, j! H
  5. 6
    4 L* A2 Z- K5 k  c8 q
  6. >>> math.trunc(math.pi)
    & G3 [' N" ]% _* H# K
  7. 3
    : B; |8 M% E: E" C6 ?
  8. >>> math.trunc(2.567)
    % g7 k* v2 p+ a# ?* x4 ]' ~
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-27 12:23 , Processed in 0.107410 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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