新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
9 m( u6 j1 e3 T8 t- E4 u, T
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 R* _1 B  u$ {

' {& G  T+ U8 F9 X1 @  R3 [. j0 K方法1
- R2 t, m% Z3 n% p4 w+ I! e: M
  1. >>> import math
    ; x( \/ [# o, m8 J& @, ]& A
  2. >>> math.sqrt(9)3 w+ O3 s/ A! R- Y4 \
  3. 3.0
复制代码
方法25 K# k, c7 h  O5 `
  1. >>> from math import sqrt
    ) `2 v/ n; F/ ~; _5 @
  2. >>> sqrt(9)
    1 o  z7 K4 k. ?# {
  3. 3.0
复制代码

4 b8 ?9 [6 G3 Z9 |/ f' h

) w# q1 H; r1 u3 [- D
math.e  表示一个常量  h! h; ?6 g! {
  1. #表示一个常量
    ) W3 |3 @/ e) s" P* ?& M
  2. >>> math.e" G: t! l9 G4 i- W  {3 Y. `+ C
  3. 2.718281828459045
复制代码

1 S: v) Q$ v" q  cmath.pi  
数字常量,圆周率
  M  n9 r: w4 j' O( O! O9 F
  1. #数字常量,圆周率8 q4 Y( m0 Y& `1 }/ F
  2. >>> print(math.pi)% l1 ]+ M. z  x5 p
  3. 3.141592653589793
复制代码
% T' m) r* D- [' _, ~% j  J
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
. R/ c( I: p, }4 j
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x! ^8 d/ M' y4 Q2 v
  2. ceil(x)- _! ?8 f  X/ ~8 A  i
  3. Return the ceiling of x as an int./ _! W3 Q5 O; D5 l! `" P. O& i
  4. This is the smallest integral value >= x.! a4 @6 u/ W9 h" G# n1 x* T

  5. & y6 m7 ~6 O3 i3 N9 o" }
  6. >>> math.ceil(4.01)3 z7 W: D4 t% C4 R
  7. 51 @# `, a! D& g. r' o8 b
  8. >>> math.ceil(4.99)
    ) ~" B8 c0 t6 l2 f
  9. 5  O/ X" U+ V( f" A+ J9 s7 D' P
  10. >>> math.ceil(-3.99)
    ! I0 \% z5 V' K0 N
  11. -3* Y3 o" M+ b/ ~
  12. >>> math.ceil(-3.01)4 J5 u9 w+ a9 I4 i
  13. -3
复制代码
6 F2 b: [/ W0 n* R" v2 P
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( ?% E* t$ O7 C; f2 \/ z# r* F
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    : Y7 B1 D- k1 D
  2. floor(x)
    " h/ R# w- W3 c  T
  3. Return the floor of x as an int.- d! s( o9 i1 F( D4 L
  4. This is the largest integral value <= x.
    8 O# X7 \& A- Z  a/ m1 O; m
  5. >>> math.floor(4.1)
    . O! x$ {0 t6 \" ?/ D; F9 f% ]4 o
  6. 4
    " x1 r% X! L/ w; G
  7. >>> math.floor(4.999)
    . q5 ]$ t& v; N0 E8 k$ ?
  8. 4
    8 f1 z0 X" p$ y. V+ ]; i
  9. >>> math.floor(-4.999)
    . p# A5 R( F+ e* L4 F) @
  10. -5
    ' ^  G* c: R+ c. Z  y
  11. >>> math.floor(-4.01)1 }2 E& @' q: Y! d; M* F9 z% S
  12. -5
复制代码
" d' P) q' y$ ]" G
math.pow(x,y)  返回x的y次方,即x**y
, x6 |9 H4 D2 @. n
  1. #返回x的y次方,即x**y# N$ ~9 ]; g0 z2 g" w# L
  2. pow(x, y)& {- D% P+ ]. p: m
  3. Return x**y (x to the power of y).- G  H; C5 Y0 y8 B& f
  4. >>> math.pow(3,4)
    ' ^7 c, i; ^! e" Y% J$ S6 J2 V. s
  5. 81.0
    : s9 B" i# f- S% }+ y* s
  6. >>>
    9 [' P+ H+ l0 A$ S- i9 d
  7. >>> math.pow(2,7)$ c" J7 [  M! W* _
  8. 128.0
复制代码

, F! k! g2 C/ imath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 t, M+ _( R# S# C( F5 d; k8 G
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    # r1 l! [' u. h, b% Q( K
  2. log(x[, base])+ u7 m0 e+ E4 ?) i, t6 b
  3. Return the logarithm of x to the given base.1 W: b. L4 z' G9 S( H2 B$ X$ }
  4. If the base not specified, returns the natural logarithm (base e) of x.- E. Y$ S3 n: g5 N
  5. >>> math.log(10)( H7 m- d! a3 X) b# G& O
  6. 2.302585092994046& g; Z) p4 a% R! d# ^6 p2 c7 y
  7. >>> math.log(11)* ~* R9 |/ c& Z! p& p/ v
  8. 2.3978952727983707* S( x- c0 w5 `9 {( h4 T
  9. >>> math.log(20)' @7 }' Q$ {. s6 J$ H. _
  10. 2.995732273553991
复制代码

# Q( h) N5 m) \math.sin(x)  求x(x为弧度)的正弦值6 K, S+ U+ N, C  k
  1. #求x(x为弧度)的正弦值6 x5 y& A2 V+ h: [, o2 t
  2. sin(x)
    & h, A! `( o: u. A% c# E! u; C
  3. Return the sine of x (measured in radians).$ ?) O* i6 T3 [: I" {& G
  4. >>> math.sin(math.pi/4)
    9 k( q- C/ B2 J& X0 ~" Y5 t
  5. 0.7071067811865475
    ; w* \  G! t8 @: H: Q' T
  6. >>> math.sin(math.pi/2)
      e+ x) \* k- [* Y
  7. 1.0
    # _2 `2 e( P% R) ]; O0 {+ |
  8. >>> math.sin(math.pi/3), N% W+ V% P8 {: _- F& t) K! t
  9. 0.8660254037844386
复制代码
8 B# n& U, j8 z# l6 }$ \9 X; ^
math.cos(x)  求x的余弦,x必须是弧度1 d% x7 E- b6 w; D  p. R* i
  1. #求x的余弦,x必须是弧度' V& I" T. e4 ~3 H( u
  2. cos(x)
    ; C7 D- ?: D9 u# q7 f
  3. Return the cosine of x (measured in radians).
    % l) u# D' K  r' s- y
  4. #math.pi/4表示弧度,转换成角度为45度, `* G" B' Y- ^( V2 Q' ~9 ]7 {# l
  5. >>> math.cos(math.pi/4)
    ; h, A0 k! h" ^4 p1 b2 ~
  6. 0.7071067811865476/ S3 c* N. z0 q5 m# `
  7. math.pi/3表示弧度,转换成角度为60度/ I5 \" |& P* i8 x' P; d
  8. >>> math.cos(math.pi/3)! W" o" Z2 e! O) @' A
  9. 0.5000000000000001
    9 \( z( s. Y- E6 c2 P- D
  10. math.pi/6表示弧度,转换成角度为30度
    5 b2 t6 Z3 K- V' ]
  11. >>> math.cos(math.pi/6)( }2 N9 F! W4 }- {
  12. 0.8660254037844387
复制代码
- z& ?8 g6 u7 H, @6 a# C8 d
math.tan(x)  返回x(x为弧度)的正切值
% o4 v! f6 f1 Z
  1. #返回x(x为弧度)的正切值" ]9 M3 w2 E' |9 ]- w% {; Q
  2. tan(x)
    4 n, C( e9 \. c6 i+ O4 K
  3. Return the tangent of x (measured in radians).
    ( ]5 d: f, y8 e# O6 o1 g8 M
  4. >>> math.tan(math.pi/4)9 `* x  i% y! W' C! {" Y  [$ O; F7 e
  5. 0.9999999999999999
    6 D- h9 X) S  R( q5 X7 d& |" A
  6. >>> math.tan(math.pi/6)* z3 Y* B4 J# v9 l
  7. 0.5773502691896257
    * E3 F5 c! h4 |) f, T5 k
  8. >>> math.tan(math.pi/3)/ t" x5 z& z1 }  u
  9. 1.7320508075688767
复制代码

+ g5 `% N8 Q; z- L! a$ j# Amath.degrees(x)  把x从弧度转换成角度
5 f  B  N: Q$ {, s3 w6 h' O3 e. l
  1. #把x从弧度转换成角度
    ! a& F, z( E5 Q! C* ?
  2. degrees(x)
    5 u" d- s+ E" w$ e# J) @+ e2 `
  3. Convert angle x from radians to degrees.
    . d9 K0 g( N4 c8 j& |
  4. 2 b' R2 a4 m7 ?( N
  5. >>> math.degrees(math.pi/4)
    3 ]7 X" I# b  x- N. p
  6. 45.0) d& v# @7 B% q* T
  7. >>> math.degrees(math.pi)% G9 C( J: e; [) z
  8. 180.0) m/ ~: T3 z% G+ x) d5 T% u
  9. >>> math.degrees(math.pi/6)
    - L6 u  M+ r9 R
  10. 29.999999999999996
    . q; S" v& h5 X# R( K7 Y3 {- }
  11. >>> math.degrees(math.pi/3)1 M$ e  c5 H$ I1 q1 V: s% j1 h
  12. 59.99999999999999
复制代码

* i$ o" R" \* K( N1 hmath.radians(x)  把角度x转换成弧度
5 d; f! K# M' M. w2 X/ r2 M
  1. #把角度x转换成弧度% s* |. X4 [+ r" e1 a. P" W
  2. radians(x)
    2 B( Q& _( G5 G" P" P8 J$ @+ |, ^) \
  3. Convert angle x from degrees to radians.& I* a! M  G) e0 T
  4. >>> math.radians(45)
    7 }' r8 k4 x! c. `
  5. 0.78539816339744835 u' K  U# v& n9 h2 \2 H6 W3 }9 [
  6. >>> math.radians(60)
    / b/ S( P! a2 a- s' e4 w" ~
  7. 1.0471975511965976
复制代码

7 V" q: A" w# ~! l7 b0 s, l: smath.copysign(x,y)  把y的正负号加到x前面,可以使用03 C' _, K& |9 p* h0 ^
  1. #把y的正负号加到x前面,可以使用0
    2 B0 o( U! [4 m
  2. copysign(x, y)
    7 A9 j& X0 P# Z+ H$ G; U) e9 m
  3. Return a float with the magnitude (absolute value) of x but the sign
    ; v( p6 M' I" v
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) - W  b& z; Q1 _  w
  5. returns -1.0.
    # _( b% a1 l5 s# m+ N* V! f

  6. - K  y) p  E" `9 e
  7. >>> math.copysign(2,3)( |7 V/ `% l/ q5 q' E; x0 g
  8. 2.0
    / u! r$ f% e3 R. A, A
  9. >>> math.copysign(2,-3)
    + w' b" Z  x9 I0 }2 I* o5 s
  10. -2.0
    . ]- N# g, C, T- d4 ]8 e
  11. >>> math.copysign(3,8)( T. O9 ^8 N: ]
  12. 3.0( N! k* s5 e: P2 Q  x
  13. >>> math.copysign(3,-8)
      G7 [  Y* r4 ]# P
  14. -3.0
复制代码

3 F/ M# E6 X/ F0 i  mmath.exp(x)  返回math.e,也就是2.71828的x次方, f, J  G' u1 v8 ]  b
  1. #返回math.e,也就是2.71828的x次方
    $ G5 l9 _+ k# k* f9 z  \
  2. exp(x)  `; S- \- f8 V4 W8 o
  3. Return e raised to the power of x.
    / T! k7 Y3 o. ]

  4. 0 V8 D! s" j5 P# Z; @+ X
  5. >>> math.exp(1)
    & {) J/ L# x% k6 A+ z
  6. 2.718281828459045
    / u9 T, d9 s4 q# A# H  L
  7. >>> math.exp(2)
    - ?& |1 o- U5 }' E* W, ]! s4 S
  8. 7.38905609893065% u% e  A0 P3 z4 C! I
  9. >>> math.exp(3)
    $ t" w" B6 ?+ M+ ~
  10. 20.085536923187668
复制代码

' y. i+ N5 ]* O: M6 I8 Q, l1 hmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
! F4 c+ q7 T! k3 m& U& d& Z
  1. #返回math.e的x(其值为2.71828)次方的值减1) f6 s# K. C  |6 q' }8 v" ^; b
  2. expm1(x)5 K+ z* @: ]% ~5 I2 u, ~/ G
  3. Return exp(x)-1.
    * v6 c2 h) ~+ i) W; Y  ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.& p! x) R0 z9 E

  5. 0 w9 t5 x' J0 K' ]4 l
  6. >>> math.expm1(1)
    $ d) O" f, G8 ]- K3 V) \. v* w) Y
  7. 1.718281828459045/ I5 p3 o& c, v. x+ ^3 X+ U
  8. >>> math.expm1(2)4 Z* D$ g8 o8 x4 b
  9. 6.38905609893065& M% P. m* u' m2 R
  10. >>> math.expm1(3)
    # M6 F1 I7 k' F. h+ b
  11. 19.085536923187668
复制代码
  k2 q' L0 ]; [3 j( V& J. t1 a
math.fabs(x)  返回x的绝对值
. C. i4 Y& U/ M0 d% A
  1. #返回x的绝对值
    % ]/ o- S  B* m, J
  2. fabs(x)1 y* A. A) _) F& U. G# c
  3. Return the absolute value of the float x.; p4 j) a! k  |7 A# u
  4. ' z8 I- i0 l7 Y2 K' q
  5. >>> math.fabs(-0.003)$ r) h3 {% ^3 N$ V* s9 f
  6. 0.003, w3 K( J( R( J; N6 s9 Q
  7. >>> math.fabs(-110)
    ) ^! P# j4 z2 U4 K3 S, R
  8. 110.08 C6 z  C2 z7 R9 s7 s
  9. >>> math.fabs(100)
    8 l6 s9 ]& q: o$ y' ^4 B( q& K/ Y
  10. 100.0
复制代码

. z* U4 @/ h) m" hmath.factorial(x)  取x的阶乘的值
1 D! M% u0 a/ |
  1. #取x的阶乘的值& j7 Z. O+ h2 {& K$ Q2 {
  2. factorial(x) -> Integral
    9 C. m4 P0 N' k8 v% P2 b5 a/ {% U
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    % {, s+ r# }/ R8 F8 r
  4. >>> math.factorial(1)- ?& M- ^1 u; s: N0 I" [
  5. 1
    3 O  q% E" s5 P  I
  6. >>> math.factorial(2)' Z' J/ j/ Q7 y7 U
  7. 27 D4 y# X" f) q* c$ l% ^2 R8 t' W
  8. >>> math.factorial(3)
    6 b3 p  X* g$ W$ f" N1 S) P
  9. 6% v- W& l- `. Y  ~. Z9 W
  10. >>> math.factorial(5)
    , c! s8 k0 z6 n, u8 h
  11. 120
    2 t0 D9 n! X+ d* _
  12. >>> math.factorial(10)
    - w5 K) e9 W% L8 ~) W3 Q
  13. 3628800
复制代码
& o: c5 b$ n% o" y5 q) \7 |' G
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
8 L& y; f& c5 O% l+ p
  1. #得到x/y的余数,其值是一个浮点数
    " e" _+ b$ V, K
  2. fmod(x, y)! H% U( @3 j- Z, `( Q$ W( Q
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ' X7 j+ ~3 B. H; _
  4. >>> math.fmod(20,3)9 I4 I( s% i# ~" t( w  `, b5 V% [( J
  5. 2.0
    1 ]$ d8 H$ r, ~  G& l
  6. >>> math.fmod(20,7)
    $ S8 W3 U9 Y0 z" n2 q
  7. 6.0
复制代码

- G& i* I4 D3 u4 H- ymath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
  [/ i4 b0 \4 f5 [8 _$ z$ q0 l' a
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . m8 T( I# E+ H; W3 R. w- H! G7 M
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    2 G  r5 @8 l) o6 _% @2 u$ M# r
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    " i% x, Z+ x+ X. O
  4. frexp(x); N* y3 L8 c/ n! A
  5. Return the mantissa and exponent of x, as pair (m, e).4 r' L+ v" l9 [7 S: W3 f0 D
  6. m is a float and e is an int, such that x = m * 2.**e.
    9 Z  R! M  ~# k+ B
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- {. P& b3 h7 ~% K; ^! b, d( s
  8. >>> math.frexp(10)
    ( h3 q; }5 K8 D, i* e! p
  9. (0.625, 4)
    7 z* E, D( K" p9 ^
  10. >>> math.frexp(75)8 E5 _" B( w' L% D1 \
  11. (0.5859375, 7)2 F- V4 ^6 B9 Z+ D+ ~4 z/ x
  12. >>> math.frexp(-40)) i$ m  N  P$ q+ s6 j; I* m# i5 _
  13. (-0.625, 6)1 ?6 E3 g2 s. j
  14. >>> math.frexp(-100)
      t/ p: p# i/ a( {0 o& E
  15. (-0.78125, 7): r* v9 C; g0 o% ~. l
  16. >>> math.frexp(100)7 L) Y8 r7 O& w: W9 X' j+ f$ F
  17. (0.78125, 7)
复制代码
9 _" f5 l/ Q0 P% Z! d
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 a) z: J+ c) _6 Q" n2 j4 x
  1. #对迭代器里的每个元素进行求和操作
    . u; s; C9 F( u) P1 _
  2. fsum(iterable)  g) m* t% W* i8 Q4 U+ p
  3. Return an accurate floating point sum of values in the iterable.3 i9 s" O8 o" h9 z
  4. Assumes IEEE-754 floating point arithmetic.
    8 m" t# b, g- ^6 m
  5. >>> math.fsum([1,2,3,4])( m  a, {2 b6 W" \& c
  6. 10.0, q2 }. {4 ?# z) s2 U
  7. >>> math.fsum((1,2,3,4))
    . V1 q: r7 i# C7 \' X* `
  8. 10.0
    1 M3 |4 d* h5 M' n$ H% l
  9. >>> math.fsum((-1,-2,-3,-4))
    . p# J" _0 p5 d9 a8 z( y0 `
  10. -10.0
      ?7 O3 n1 L- Q$ H, x# K! V8 X
  11. >>> math.fsum([-1,-2,-3,-4])' g. e8 }/ t5 n( k! {# Q, @1 X
  12. -10.0
复制代码
% I! U) t# c0 Z! X3 m6 J( H/ h
math.gcd(x,y)  返回x和y的最大公约数2 I( N3 y2 @7 z7 U1 ^
  1. #返回x和y的最大公约数
    . P- k; \. B) s/ j* k3 K  [0 y
  2. gcd(x, y) -> int2 ^, \3 f2 [4 O8 v( _
  3. greatest common divisor of x and y
    6 ?5 j9 G! ]. M% t
  4. >>> math.gcd(8,6)
    8 J/ s, Q1 b0 v, b$ }' m1 E
  5. 2
    + F' m! o; t, Q  M2 V7 y6 h2 [
  6. >>> math.gcd(40,20)
    9 m, ^  k% o  P+ }" |
  7. 20) \( s& P" [: |( z0 U( M( R/ o  b7 U
  8. >>> math.gcd(8,12)  B9 v+ L5 b9 k  C
  9. 4
复制代码

* g5 J5 n6 y; x! p' mmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
9 c4 n7 y# @7 G% l
  1. #得到(x**2+y**2),平方的值
    1 n& o! F2 A4 N' [4 `$ O: X  `8 T( x
  2. hypot(x, y)
    5 C1 P4 c% p# D" @+ @
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    3 ]% F- |: n3 N; T6 G. Q
  4. >>> math.hypot(3,4)
    ! t5 c! `2 K! l. ~3 T! p- \
  5. 5.0% u2 \$ u, S9 e# x
  6. >>> math.hypot(6,8)2 e+ E7 V. x2 z9 Y9 M8 O( F0 ~
  7. 10.0
复制代码

  @) z" n$ {0 r+ h: ?math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False6 R$ k/ c$ X3 {0 H' y2 j, f2 O9 t4 L
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    9 g/ P; C) j2 r- e2 L- }" x8 b
  2. isfinite(x) -> bool& h4 G( n$ n# Q* Y4 _* y
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.$ {' x6 z' D2 H  T% Q' x1 F
  4. >>> math.isfinite(100)8 n" l, z9 _  Q& Q! b% k! y% w
  5. True( |) I  D; ?$ r$ R) A
  6. >>> math.isfinite(0)7 y, X3 k  t0 @3 y$ l
  7. True
    , s1 f4 Z2 U; j
  8. >>> math.isfinite(0.1)3 n1 L. t8 }9 y& w" f1 f% c" X' w& g
  9. True
    5 W3 i# M6 }+ h0 O$ y; t2 g( W$ h
  10. >>> math.isfinite("a")# @* q, j0 ~4 Z: ^
  11. >>> math.isfinite(0.0001)
    - k9 z. L% |3 s  w$ g" H  z
  12. True
复制代码

( b- m, Z! [: r" j/ p" ymath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
* W( a8 ~8 }' Z0 ]5 p- Q, o
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False& {1 w/ u6 y- q+ @5 m1 P" v
  2. isinf(x) -> bool3 W5 e  q! _, T  u
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ! h* f3 F2 @  C, ^& e; j
  4. >>> math.isinf(234)
    3 |. }3 ?8 g" v# ?/ A
  5. False& P# {9 E& T4 i7 S) p) e7 G
  6. >>> math.isinf(0.1). O* c1 L( v7 h1 q3 p# C9 F
  7. False
复制代码
, y1 [0 K" Y2 |0 z; ~
math.isnan(x)  如果x不是数字True,否则返回False
# A: |! P" i4 m0 a3 _
  1. #如果x不是数字True,否则返回False
    * g* \4 i3 l$ Z
  2. isnan(x) -> bool
    . K+ Z  ^1 F! d$ t% g. ?
  3. Return True if x is a NaN (not a number), and False otherwise.
    0 N2 T; p& l0 j
  4. >>> math.isnan(23)8 B, ~! _9 D! i$ n+ P
  5. False' a) h9 A0 `/ p4 J# ^
  6. >>> math.isnan(0.01)
    $ L  }9 h& B: K2 g( g& c* K
  7. False
复制代码
+ j* I. s( F# W0 b& |
math.ldexp(x,i)  返回x*(2**i)的值
% J- a5 G5 v+ L& t! \' E9 Y
  1. #返回x*(2**i)的值; F* A9 _0 e. J3 n
  2. ldexp(x, i)7 N% [" R3 U) y9 @2 F
  3. Return x * (2**i).' h* ~1 ?3 h- l
  4. >>> math.ldexp(5,5)7 s8 s: r6 x  R  [, A. V4 R! S0 O
  5. 160.0/ T4 B( r! }, t: e. [- E$ S
  6. >>> math.ldexp(3,5)
    ' s; Q! B2 i( U% j0 G
  7. 96.0
复制代码
4 [' l* b1 D- G& B$ W
math.log10(x)  返回x的以10为底的对数
$ k2 ?+ z! H. R4 X$ _! l
  1. #返回x的以10为底的对数
    8 P% O7 j& j+ m2 h% k0 W' ^& {! h2 @
  2. log10(x)
    1 b0 N4 j/ p/ y" x" f7 o
  3. Return the base 10 logarithm of x.
    $ v0 e9 q( {, Y$ a
  4. >>> math.log10(10)
    4 {6 ~" v4 u2 b' }9 l
  5. 1.0
    2 |+ S3 T* Z2 f" s0 g
  6. >>> math.log10(100)
    ! ~' ~0 U+ R% ]! o4 I- W
  7. 2.0* T6 Z- Y& U0 T
  8. #即10的1.3次方的结果为20
    " z; M# |- b7 X" l7 D1 I7 p1 N  k
  9. >>> math.log10(20)  g1 d- L# J$ y0 e
  10. 1.3010299956639813
复制代码

7 h9 g" W1 |6 ^math.log1p(x)  返回x+1的自然对数(基数为e)的值
4 Y  [# w  I( j% x* S* b
  1. #返回x+1的自然对数(基数为e)的值
    ! M6 p, X8 `5 M% M9 w
  2. log1p(x)
    & H! X2 x" f( P& V4 r3 p/ {
  3. Return the natural logarithm of 1+x (base e).
    : X& Y$ J$ L" j) S
  4. The result is computed in a way which is accurate for x near zero.
    ; u; C, E  B$ C; W
  5. >>> math.log(10)" \% ?- T! r1 j6 n9 C
  6. 2.302585092994046! n2 t; [3 f3 p, A
  7. >>> math.log1p(10)# N6 E& N" X0 i
  8. 2.3978952727983707% S* P" `  N! o; Q' _4 a
  9. >>> math.log(11)+ K8 X$ N# y/ }& l+ }# ]
  10. 2.3978952727983707
复制代码
& ~! u) B) Y0 @5 ~. I1 u$ t
math.log2(x)  返回x的基2对数  Y' u7 x. u% T
  1. #返回x的基2对数& i3 _4 X& n3 h: i# v
  2. log2(x)
    : P; s5 v2 j7 M8 y+ O$ Z
  3. Return the base 2 logarithm of x.( T/ Q9 @7 R) `* U+ v% q
  4. >>> math.log2(32). d. {- G8 v$ j. T0 q+ t" ^- E6 S
  5. 5.0. Y" ~" Q0 W$ q/ {$ ~, |
  6. >>> math.log2(20)' x9 U4 q! P# m: p
  7. 4.3219280948873638 }8 s2 m5 K& a5 V& o/ z4 W7 V- I
  8. >>> math.log2(16)
    3 i0 z/ C) K+ v# Z0 L3 ^
  9. 4.0
复制代码
; H1 d- o1 ]2 ^2 r2 \
math.modf(x)  返回由x的小数部分和整数部分组成的元组
1 ?+ |' A* w+ I/ i2 ]% t
  1. #返回由x的小数部分和整数部分组成的元组9 K3 b6 M2 \' _. k
  2. modf(x)# W! V" ?: |  N$ i+ a* ]8 \0 `
  3. Return the fractional and integer parts of x.  Both results carry the sign
    , u. F. ~% F' l( o
  4. of x and are floats.
    ; ~, }8 s4 q8 `1 ~4 U+ {# A, p
  5. >>> math.modf(math.pi)
      P( b8 z+ s, a' U( D$ B
  6. (0.14159265358979312, 3.0)( j5 E' z' u+ M, M/ d5 H
  7. >>> math.modf(12.34)- v' I: ?4 b6 K5 }. g$ O; f% n
  8. (0.33999999999999986, 12.0)
复制代码
/ ]2 {! C1 x- q, H" C+ J
math.sqrt(x)  求x的平方根
' n+ L. [' W6 @( @& ^1 L
  1. #求x的平方根
    ( {: d" V# j0 j% t9 L
  2. sqrt(x)
    " I% n) I! w  Y) Y" g' z$ k4 t
  3. Return the square root of x.
    ' P& H" r0 h, t9 a* o
  4. >>> math.sqrt(100)
    ; r" `" R3 [+ D. [
  5. 10.0
    1 k- _7 s7 b, R' Z! r; ?
  6. >>> math.sqrt(16)$ ^7 c! K. g- l- s
  7. 4.0
    # d1 d2 Y% c! P* J5 Z
  8. >>> math.sqrt(20)
    6 M+ N7 O. A0 W  m! I4 V
  9. 4.47213595499958
复制代码
/ A; E' q2 {6 D1 `' M' U
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分5 e2 |" g6 \5 W  E( D( N
  2. trunc(x:Real) -> Integral  P. U' C4 M* H. B. M
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% S: p6 i) [7 @
  4. >>> math.trunc(6.789)
    & R$ E) {" ^# ?6 N2 ^: R
  5. 68 \  j! Y3 \; Q* F% s) X
  6. >>> math.trunc(math.pi)! o, `! F5 I4 i* l$ j, W1 ?
  7. 3
    ' k# J. b# ]# X: W7 G& ^
  8. >>> math.trunc(2.567)+ ~8 T2 p$ }- @$ O
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-19 18:58 , Processed in 0.085307 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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