新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

' v% A4 G; N7 N. I# p3 S: O/ w% E【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
% k' e, k# S; j$ y- u: k

+ P  j0 Z+ g8 P1 H方法1: G) U/ B+ Q( ^; |9 a
  1. >>> import math8 v8 c/ z) p& B4 |) p- B% m
  2. >>> math.sqrt(9)( @1 w# N! F. B+ f- V( Z! Z
  3. 3.0
复制代码
方法2
: i7 W9 ^5 D" r$ p. r
  1. >>> from math import sqrt) i9 n7 V# ^; J) F
  2. >>> sqrt(9)
    ' k" N: Q1 }7 y
  3. 3.0
复制代码
$ M2 c* {! P  s6 X

4 }+ P& L' X2 h3 D4 j  X+ a
math.e  表示一个常量. l2 K, e8 ^3 z4 o
  1. #表示一个常量9 }8 ?! ?' Z" h) `* I) [
  2. >>> math.e
    % Q4 @5 _, F) R3 C% M# A) B2 |9 Q
  3. 2.718281828459045
复制代码
! t" ~+ L& g- p2 B: G
math.pi  
数字常量,圆周率
4 J3 q: c7 B2 o) K$ u
  1. #数字常量,圆周率6 ~$ ~1 }; C5 c7 U
  2. >>> print(math.pi)- e9 ?, S0 f, t$ {. M' m) }
  3. 3.141592653589793
复制代码
. X! a2 M, _  c* j6 [1 K; R% h
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 D! @5 ^$ _4 _* k* _
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 o5 H; {7 v+ R5 l" y$ D
  2. ceil(x)& c9 q2 Z, I4 J) k% l& I7 B' {% G
  3. Return the ceiling of x as an int.
    . O! ]0 f0 F4 E3 J
  4. This is the smallest integral value >= x.
    0 l9 j5 e" D4 u  h  [

  5. 0 u. ~$ k9 o' ?
  6. >>> math.ceil(4.01)4 D" w+ \9 L7 t2 q, M/ d% }
  7. 5
    7 U  S4 Z, s0 _0 ]
  8. >>> math.ceil(4.99)
    2 C. u$ d) J- M) d. V
  9. 5: H3 y& g! l  ?) i0 x( k( t
  10. >>> math.ceil(-3.99)) z' w  O# F. [* r6 t& H
  11. -3
    0 M3 A0 r% Q1 s3 P  c8 U
  12. >>> math.ceil(-3.01)' g+ G0 {8 a# l1 H3 H9 p* j# D
  13. -3
复制代码

6 i; F, e, [/ j# f0 a3 p  I* T3 h0 Amath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( {$ H5 B# e' e: q, j, _5 A6 W7 J* Y5 y) _
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 h, V5 ~/ u& Z5 `. i3 U0 b5 Y
  2. floor(x)5 ~2 Z* S* ]& L2 S# q, [5 o/ M
  3. Return the floor of x as an int.
      \9 a  K$ a0 O
  4. This is the largest integral value <= x.
    % d- _' B* T" U1 ^
  5. >>> math.floor(4.1); H1 ?  ~+ q" ?
  6. 4
    $ g/ a: X' [1 \" i' G. Y
  7. >>> math.floor(4.999)
    4 F# l( S5 t" H5 b' k3 i
  8. 4
    1 @  |7 l6 {6 d! O) v  o
  9. >>> math.floor(-4.999)
      I, S' g0 _1 N2 g( q- Y; R% q
  10. -59 f6 i% N' f, l8 D
  11. >>> math.floor(-4.01)$ |) `8 k+ ~% O2 D7 b
  12. -5
复制代码
# ?- k( }8 n# k8 M0 b
math.pow(x,y)  返回x的y次方,即x**y
0 c, m" |7 N; L- `) o! s
  1. #返回x的y次方,即x**y. v9 s, D! o( k$ \( U4 a- @/ K# G) U
  2. pow(x, y)
    7 P0 \0 Q% Y% o$ p
  3. Return x**y (x to the power of y).! }- d) x7 P  D4 o/ g
  4. >>> math.pow(3,4)) B; \+ `+ C0 P
  5. 81.05 }$ n- s+ G4 C
  6. >>>
    - y! I; j: q' U  s: ?0 l2 \1 K
  7. >>> math.pow(2,7)
    / W* h: u, f- Y4 U  x
  8. 128.0
复制代码

0 e: u! l  j9 n+ [7 xmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% H2 f- q, F* }
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    6 @' _5 |% r, z" E* I
  2. log(x[, base])* M) d7 y2 j/ Z* c
  3. Return the logarithm of x to the given base.
    & l2 [- V- }* R. i' \
  4. If the base not specified, returns the natural logarithm (base e) of x.5 n% h8 A5 t: ~/ t! v. C+ W" u
  5. >>> math.log(10)
    9 @$ o) c& U( J5 \8 P
  6. 2.302585092994046
    ! v# X1 H: ]5 z! l
  7. >>> math.log(11)
    ! u8 j! ~1 V2 a' \& z- M* O
  8. 2.3978952727983707
    + F* o1 ?# `3 N- y
  9. >>> math.log(20)
    5 q2 S2 \2 H6 F6 f! X
  10. 2.995732273553991
复制代码

& h1 I! {1 f3 wmath.sin(x)  求x(x为弧度)的正弦值, o! z! t# W5 g0 V" P+ z
  1. #求x(x为弧度)的正弦值
    4 \) ]& ?( k) z" ~
  2. sin(x)
    : d! N) X5 Z) ^" v1 s" X+ k/ o
  3. Return the sine of x (measured in radians).
    0 w  X# L, i4 w2 `7 y+ [- r
  4. >>> math.sin(math.pi/4)
    0 g. v) [* R5 V, R
  5. 0.7071067811865475( E- f$ o) \* r' c: R: B) O& G
  6. >>> math.sin(math.pi/2)+ C3 h+ B) f" d9 ^
  7. 1.0
    9 s% Z' v  J* g/ T* {9 z9 u
  8. >>> math.sin(math.pi/3)
    8 U# X+ z. {9 G; M
  9. 0.8660254037844386
复制代码

. d, \9 P- I: L0 y/ ^: ]math.cos(x)  求x的余弦,x必须是弧度
+ j% @1 Q' r( P
  1. #求x的余弦,x必须是弧度$ a& v1 A7 @' B4 ?+ R& H2 B: e+ o2 o) {
  2. cos(x)
    ' X# l0 |. ~; ~/ `8 U4 p/ v6 s
  3. Return the cosine of x (measured in radians).
    ' o% b! c" l( E0 ~( }6 x% x
  4. #math.pi/4表示弧度,转换成角度为45度3 x8 g$ J9 E; Z
  5. >>> math.cos(math.pi/4)
    0 z4 {$ \1 I1 Q" t: |' a
  6. 0.70710678118654767 b; ]( Y" e" D3 ~$ [8 F$ R0 W
  7. math.pi/3表示弧度,转换成角度为60度
    , w: v, B0 t& b
  8. >>> math.cos(math.pi/3)
    ; g! |* G8 s3 b& \' _
  9. 0.5000000000000001
    + O' m5 `* F4 `0 E" p2 }& p: K% |2 @
  10. math.pi/6表示弧度,转换成角度为30度
    1 l+ Q7 P4 d1 p' l8 p
  11. >>> math.cos(math.pi/6)* O( m! [# w) o1 M. I0 p% ?
  12. 0.8660254037844387
复制代码
3 Z/ D% F! M! S6 j) L/ s
math.tan(x)  返回x(x为弧度)的正切值4 j4 N( C/ j& j/ S
  1. #返回x(x为弧度)的正切值, Q3 b0 O2 O. S" c
  2. tan(x)' Z5 I- {2 S  ^# h" Y+ |7 u$ m
  3. Return the tangent of x (measured in radians).: ^! o2 o  r* a. e+ D6 y/ r
  4. >>> math.tan(math.pi/4)
    $ a* w! z8 y# R+ S% `# x" d- C1 D6 @
  5. 0.9999999999999999
    7 H& c4 f8 V! p8 [4 B3 K5 B
  6. >>> math.tan(math.pi/6)
    3 F$ a) g5 P" K% W" D/ `) _
  7. 0.5773502691896257
    3 i; r9 W( a" G  A- N
  8. >>> math.tan(math.pi/3)3 k( R6 P% E8 E5 T$ O; |
  9. 1.7320508075688767
复制代码

; L: {$ e- p8 f8 }9 ]/ V0 c. D3 kmath.degrees(x)  把x从弧度转换成角度
1 ?) y4 k! h3 a9 c# }/ A& R: p7 n3 Q
  1. #把x从弧度转换成角度3 o( b$ H5 f- G$ n$ ?5 ?% U2 q
  2. degrees(x)+ L/ l9 x6 T8 _6 D
  3. Convert angle x from radians to degrees.! x8 t" E( @; w

  4.   C) ^! U* F2 ~. N9 p2 @9 l0 @
  5. >>> math.degrees(math.pi/4)
    ; [% D1 u+ B/ G" a/ I
  6. 45.0$ m9 x/ v! Q/ C- A( _5 g
  7. >>> math.degrees(math.pi)- _7 }% ]' P. C
  8. 180.0( J% r; u% P& |, ~! {, ^, Z$ B/ u- X
  9. >>> math.degrees(math.pi/6)+ A; `% R  K' Z% @
  10. 29.999999999999996( v. g( Q1 B! I
  11. >>> math.degrees(math.pi/3)
    * D! ]) H" ]* z+ D
  12. 59.99999999999999
复制代码
4 W/ T' C* J) l4 ^& s4 e5 L3 R
math.radians(x)  把角度x转换成弧度* G/ u! S. U8 T+ Z9 @1 \
  1. #把角度x转换成弧度
    9 z5 k) L  K8 B( g5 q" s5 }
  2. radians(x)
    : f% ?2 v, h. s
  3. Convert angle x from degrees to radians.: ~6 G4 `) x" w; X
  4. >>> math.radians(45)
    " |6 u" H' u' o( ~; \! ]
  5. 0.7853981633974483
    1 Q  Q" ?# S  ?  |9 P7 t& {
  6. >>> math.radians(60)
    % T. ?5 z$ L6 u. L, L
  7. 1.0471975511965976
复制代码
2 F8 e: ], {8 ^
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
  j; F; v1 `6 p6 w4 ~+ O, G& a/ r9 r& V
  1. #把y的正负号加到x前面,可以使用0
    $ P# @4 R9 n" F& ~* [
  2. copysign(x, y)
    / [. x$ W& X! m3 T, ]' }
  3. Return a float with the magnitude (absolute value) of x but the sign
    5 G1 U6 y$ [* L- i. x6 v2 s
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ; f5 V/ p/ @+ b! `8 i
  5. returns -1.0.0 m+ Y* u0 s$ I% N  X) s

  6. 6 P% m5 j5 c  l9 H+ i) ~
  7. >>> math.copysign(2,3)
    8 H+ W) v& u: D) v1 H
  8. 2.0
    , N- Y5 \( S$ B. ^; a/ _
  9. >>> math.copysign(2,-3)
    ; H& C2 c8 A$ ^9 T6 Z9 ?
  10. -2.0
    % P3 k9 @6 B& F+ a
  11. >>> math.copysign(3,8)
    $ h: }3 A0 G+ w! j$ e
  12. 3.0
    1 e/ T% z; d, b0 G  p( X; o
  13. >>> math.copysign(3,-8)! W$ {! s) O: [5 F" Q$ J2 B
  14. -3.0
复制代码
- S6 {8 ]2 a, J5 ?5 ]9 R0 D
math.exp(x)  返回math.e,也就是2.71828的x次方+ _: e) B8 g! V1 m5 l1 A+ C
  1. #返回math.e,也就是2.71828的x次方& S& c: B6 ]! Z8 |  q6 Q
  2. exp(x)5 L7 }, \* p$ T8 w
  3. Return e raised to the power of x.
    9 m( g) d! G1 [: k6 i  b

  4. # X/ c2 E: z: i
  5. >>> math.exp(1)' j7 `2 q* g5 \  J* s
  6. 2.7182818284590459 m+ Z% V: f# J
  7. >>> math.exp(2)6 e* ?0 g9 ?. j* U
  8. 7.389056098930659 @$ [$ j9 n5 s. p
  9. >>> math.exp(3)
    1 J  b4 M. S' B$ u: u0 h  t
  10. 20.085536923187668
复制代码
5 Y% O% O/ T' F& }* B& y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
& E( ^# p! f* R' P- a# v
  1. #返回math.e的x(其值为2.71828)次方的值减13 Y/ B  O. g/ e, h; N/ g8 @5 Y/ j% [2 P
  2. expm1(x)3 o7 k. c9 p! g
  3. Return exp(x)-1.
    ; E/ J7 T1 O$ `  {
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; a% |4 r" `  }* ?

  5. * E: g+ r0 G( }) |
  6. >>> math.expm1(1)
    7 f6 x+ s0 ~1 h8 }
  7. 1.718281828459045! h2 D  P' z4 Z0 T5 y7 B- M
  8. >>> math.expm1(2)& Q2 `: s& r5 ]3 z* |5 F/ a/ w
  9. 6.38905609893065* P+ `2 ]% H' E3 h0 t  v9 W) ^
  10. >>> math.expm1(3)+ S: I; C! _: a0 C7 Q
  11. 19.085536923187668
复制代码

$ g$ O  o+ J4 g  w1 k; Pmath.fabs(x)  返回x的绝对值1 A* L4 [! e$ m2 i
  1. #返回x的绝对值
    % ~( \" g# x& W5 S$ M# P' s
  2. fabs(x)
    # F" Z. f$ a8 u* A
  3. Return the absolute value of the float x.
      r2 u/ ]4 T; q' V- ?

  4. 6 f7 V+ x! F3 |! V! A5 N! ?* B) y
  5. >>> math.fabs(-0.003)
    # u/ P" T) x3 |
  6. 0.003: g. _7 T! S9 n' T$ y5 T/ a
  7. >>> math.fabs(-110)& |5 h6 w" M* H+ B
  8. 110.0
    ) c) a8 v8 k, m9 `) u3 O
  9. >>> math.fabs(100)$ y; g4 P* f/ g6 A5 W9 J7 r2 n
  10. 100.0
复制代码
5 m6 ~! _2 K( r  e7 J
math.factorial(x)  取x的阶乘的值
, B2 d5 o" d+ B) P& |
  1. #取x的阶乘的值! D4 }) S2 Z- e& i- {, T. o
  2. factorial(x) -> Integral6 p- n4 o% ~- p( u% y$ ?& x. q
  3. Find x!. Raise a ValueError if x is negative or non-integral./ X# Z8 m7 m$ I& g6 x( q; g
  4. >>> math.factorial(1)5 f) S; Z' S/ @7 I  \' c2 \
  5. 1
    ; ]/ c, r$ ^2 A* e
  6. >>> math.factorial(2)' l  e8 X( b6 @4 u+ g: R3 w
  7. 2. \8 D# _+ {: s. v
  8. >>> math.factorial(3)* O+ ?7 F* r/ R1 y3 |% \
  9. 60 B* Z  a+ V. \4 Z
  10. >>> math.factorial(5)3 N" T2 A- Z% q7 d$ z& }
  11. 120: h) {0 [  F" B  @  f  B
  12. >>> math.factorial(10)" r9 s  i! |. t1 E9 S% t. t
  13. 3628800
复制代码

, |. v5 }7 D) Hmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数+ O9 u/ F0 P' u/ a3 W0 R9 n  ]
  1. #得到x/y的余数,其值是一个浮点数' D4 q6 h3 j/ ?) P: o
  2. fmod(x, y)
    8 {- _( ^/ a' R9 |/ p
  3. Return fmod(x, y), according to platform C.  x % y may differ.; B: Z; e" y' W
  4. >>> math.fmod(20,3)& T. Y+ O" L& t2 q5 g
  5. 2.0
    " |4 B2 d4 d( {( h' g
  6. >>> math.fmod(20,7)9 m' g& T1 C/ D: ]" B
  7. 6.0
复制代码

3 Q9 I- k% P- ]* w: U/ A7 u0 d; bmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
* `0 @! r$ X1 P
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 R" u3 H( \4 g, x
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 Q* }. E, v6 Q# k( o5 n: s% _
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ R+ y6 b' ^0 K+ x7 f
  4. frexp(x)
    ; p4 m$ K2 @0 o& `0 U
  5. Return the mantissa and exponent of x, as pair (m, e).
    5 `+ x8 u& v& ?* ^+ S! C) z
  6. m is a float and e is an int, such that x = m * 2.**e.5 k$ F$ @3 \# D
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.% M3 {7 N* d3 T& e( }
  8. >>> math.frexp(10)0 s2 X2 `9 P! p; h. x: N& \4 v
  9. (0.625, 4)
    + }9 l4 S7 H1 m1 Z' V  ?: I
  10. >>> math.frexp(75)
    ' r  l- Q0 D, Q  v+ G; K* [
  11. (0.5859375, 7)
      Y' k8 E* B& Z2 c) V4 w/ Y
  12. >>> math.frexp(-40)
    # i# M- D! x$ x4 D
  13. (-0.625, 6)1 x3 b  G; |0 J; f: U8 S# G! j
  14. >>> math.frexp(-100)8 @( \* w( y0 W
  15. (-0.78125, 7)
    " {* W5 k; E  O/ K+ R% h: r
  16. >>> math.frexp(100)& k) B/ l: K$ M! l  K
  17. (0.78125, 7)
复制代码
+ i4 R! g/ r/ I2 v: u# ?
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列: K% V  M& ^* ]! ^8 i4 N
  1. #对迭代器里的每个元素进行求和操作
    * i4 m6 A8 x  {% o3 c# p5 _. z* u
  2. fsum(iterable)
    6 y" e( e1 o8 |3 e$ D  y
  3. Return an accurate floating point sum of values in the iterable.- r" s- i& ~: `- |
  4. Assumes IEEE-754 floating point arithmetic.
    & c  k; E9 f+ w5 Y9 S/ n7 j
  5. >>> math.fsum([1,2,3,4])
    0 L3 u9 q' {" C7 ]7 I. k1 K
  6. 10.0; i" V# A( s% p- Y# B" Y( y
  7. >>> math.fsum((1,2,3,4))
    6 }9 [+ A/ a1 ~% A) k
  8. 10.0
    ' L# c8 j1 l- C+ w; k
  9. >>> math.fsum((-1,-2,-3,-4))/ D+ K. p$ X4 S' {
  10. -10.04 c  W) H  `2 Q0 _* o
  11. >>> math.fsum([-1,-2,-3,-4])3 H/ k6 @" c& t7 ^: l1 U
  12. -10.0
复制代码

3 x2 u7 ]7 {7 U: W: W: F. F  umath.gcd(x,y)  返回x和y的最大公约数
3 H9 G# j6 z( ]% T6 c: g0 I8 I
  1. #返回x和y的最大公约数2 ?! S% g! Z! h% M: E5 M4 J' m1 o
  2. gcd(x, y) -> int
      }2 q" x5 t5 K& [
  3. greatest common divisor of x and y1 Z  Y) T; L0 k5 T$ ?, \4 u
  4. >>> math.gcd(8,6)
    & f( P* r4 s( A- f5 M3 f
  5. 2
    3 J/ B" ]; o1 n3 x" w
  6. >>> math.gcd(40,20)! q5 ]1 R4 }9 f5 F. t& Q1 m
  7. 20
    2 P7 Q- q) U& r, R
  8. >>> math.gcd(8,12): R3 y2 R( w+ D1 I% R
  9. 4
复制代码

6 ?! M8 q) v! K$ o" J7 C% t: s/ I5 ymath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
) _; e, v9 Q+ \# B6 q
  1. #得到(x**2+y**2),平方的值% S7 u3 q1 r* a9 G# v4 t/ a; P, f
  2. hypot(x, y)
    5 w  p) {: q: H: C( |% B( L
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 X0 V" _7 s0 C
  4. >>> math.hypot(3,4)7 `: V( Q" }% F, a: T1 z8 d
  5. 5.0
    9 ]% y- S( b9 \1 s6 l! v
  6. >>> math.hypot(6,8)
    ' w% {+ c, o% |  d
  7. 10.0
复制代码
0 _4 X* b* K  ~1 d% Q. W3 U  B6 S/ }
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False( y& {- c4 Y# @  [2 _' Z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    $ @7 h% M5 U1 i  A* S
  2. isfinite(x) -> bool
    ) c1 [6 y. }! O5 m- s
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    6 r" U' p. a" ?  k4 a
  4. >>> math.isfinite(100)! ~7 D# m0 V0 m6 e. s* I/ X2 F
  5. True# v  G9 w+ ]; O1 ~' C  P' P
  6. >>> math.isfinite(0)2 ]; z7 s% d7 H+ J* n( X
  7. True
    ' ]4 a/ [) Y: i2 V( c' ^7 V2 t
  8. >>> math.isfinite(0.1)
    , ?- b8 ]" Q: k: w8 C
  9. True) d# q4 b: j2 J: W  F' Y
  10. >>> math.isfinite("a")( l; {1 n! k" e0 x
  11. >>> math.isfinite(0.0001)
    " B/ w  t0 J7 j  G% }
  12. True
复制代码

  d2 ^2 F+ }0 P$ o- L. p: _. g+ _math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
: |+ Q' u: i0 K8 h- K8 Z  @
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False' u" z& o0 G9 o2 a
  2. isinf(x) -> bool
    0 G- H7 s, R5 E. \. K% ?
  3. Return True if x is a positive or negative infinity, and False otherwise.1 @! w; Z! u1 e5 c" W
  4. >>> math.isinf(234)+ L3 Z' m7 X5 M0 m  V; H1 \$ n- I
  5. False/ k- Z: K1 z& v! O
  6. >>> math.isinf(0.1)9 S+ }& z5 F8 R# m# ~
  7. False
复制代码

7 I( U- G2 Y; I% Z( G  Pmath.isnan(x)  如果x不是数字True,否则返回False7 _: Z- I% ]' X, j# H3 o/ U
  1. #如果x不是数字True,否则返回False; b7 X( `8 o+ w' |2 c/ ?3 }; p( Q
  2. isnan(x) -> bool
    6 A* }& x! ?( L3 o
  3. Return True if x is a NaN (not a number), and False otherwise.
    ! S! t8 J: y, z( L9 A. O& [
  4. >>> math.isnan(23)
    , q+ G9 S) m: `7 Y
  5. False
    ! S3 t# X, `& j7 {
  6. >>> math.isnan(0.01)3 k! J2 q  f& A' y
  7. False
复制代码
0 t" k7 S3 r; O9 n7 h
math.ldexp(x,i)  返回x*(2**i)的值
; ?4 R% d* |1 s* m
  1. #返回x*(2**i)的值2 _* V2 w2 A& Q
  2. ldexp(x, i)
    4 D4 N" Q1 F5 ~
  3. Return x * (2**i).1 D9 A2 v( D7 L0 f! p  {7 e
  4. >>> math.ldexp(5,5)
    8 C; H+ P4 O& t/ g
  5. 160.0
    8 n4 o' f4 t5 m1 N8 j0 ]6 Z
  6. >>> math.ldexp(3,5)4 z) }  W/ E& r. H! O: v
  7. 96.0
复制代码
0 i; }' v; b' t7 G% L
math.log10(x)  返回x的以10为底的对数: B: V7 G" u7 ~, y4 |
  1. #返回x的以10为底的对数
    4 c4 R. R2 @9 M; }% S4 {
  2. log10(x)- ~/ b. \" o; i
  3. Return the base 10 logarithm of x.! I: `% ?6 w" E1 U1 i2 f1 H
  4. >>> math.log10(10)" D' `& W+ T4 r
  5. 1.0# N0 b3 f  R0 @0 x7 x1 B
  6. >>> math.log10(100)
    ! P9 {; l' V! @1 O8 W' u
  7. 2.0
    * i' i5 u9 y, c1 ?( n; U- K3 O- Q
  8. #即10的1.3次方的结果为20
    $ O9 J! K1 K, [  f7 g% ]
  9. >>> math.log10(20)
    : @* e6 ~) l0 R) V8 j5 g* y7 ]
  10. 1.3010299956639813
复制代码

8 a% t& h  f4 y) ?math.log1p(x)  返回x+1的自然对数(基数为e)的值
! S- y& r7 d# n  ?& c, d
  1. #返回x+1的自然对数(基数为e)的值( L7 ~4 K) I2 p! a" X
  2. log1p(x)
    ! V) L  t2 P3 A6 t1 X
  3. Return the natural logarithm of 1+x (base e).
    0 k- i$ ~8 F5 `/ R
  4. The result is computed in a way which is accurate for x near zero.) Y: A& i" a; Z! ^
  5. >>> math.log(10)5 |* [/ p! D6 w! }. l2 r# Y
  6. 2.3025850929940468 E9 T  @3 j3 p9 @( g/ T
  7. >>> math.log1p(10)
    1 \, `+ |" K( L$ }3 q
  8. 2.39789527279837076 Z5 h) o& \& G  i' ]! p6 E2 V
  9. >>> math.log(11)( k9 T; ^7 X6 F, D8 ^: ?
  10. 2.3978952727983707
复制代码
9 h0 ^- `! N+ I* W* B
math.log2(x)  返回x的基2对数; o( m: w9 ^2 W  ~- n% ^! H
  1. #返回x的基2对数% }8 ^, [8 L  \( Y
  2. log2(x)
    1 J$ w  d0 V, V. r$ O" X5 H5 g2 b
  3. Return the base 2 logarithm of x.; F. M( \5 r7 ?* C# P5 i0 W/ @
  4. >>> math.log2(32)
    7 {. o/ e4 g) u7 q+ d
  5. 5.0& A3 k  p4 E$ D) P; B6 N  ^8 t
  6. >>> math.log2(20)& [) Y- u" h/ u* [
  7. 4.3219280948873632 N3 o3 Y5 ~8 S) j$ P6 ?
  8. >>> math.log2(16)
    ' [/ V9 _6 H9 G7 U! c
  9. 4.0
复制代码

3 }# C4 x3 g: L2 t$ cmath.modf(x)  返回由x的小数部分和整数部分组成的元组
3 x# k% A* F2 D) y' t3 ]8 C
  1. #返回由x的小数部分和整数部分组成的元组4 r+ l/ h- u* |( J# X, `! R. t
  2. modf(x)
    9 g% p4 Y' |) ?: u7 n- V
  3. Return the fractional and integer parts of x.  Both results carry the sign
    , ^' g# p9 O* L0 |; [2 B. k
  4. of x and are floats.) `: i4 V$ c2 W) a' O/ A
  5. >>> math.modf(math.pi)# X$ J& u4 l" [; X8 }. B( K9 |$ G  T
  6. (0.14159265358979312, 3.0)9 G6 @; {, k1 K
  7. >>> math.modf(12.34)2 d: l# @8 P! o4 U+ O, E
  8. (0.33999999999999986, 12.0)
复制代码
& a6 w; m% P3 L* _7 Y
math.sqrt(x)  求x的平方根
. o* U" X' @5 |5 K. o
  1. #求x的平方根# @6 U. c* t4 F0 V! ]
  2. sqrt(x)
    % ^- p1 C( P! [1 q( o
  3. Return the square root of x.
    ! g3 N' G' W- j: @3 g1 n
  4. >>> math.sqrt(100)2 Q4 X  o  f0 y) Q% E
  5. 10.0
    ; E2 M/ M3 E0 U$ r% l
  6. >>> math.sqrt(16)' N( a  O, b( c2 j. P" ?
  7. 4.0
    ) ^4 f$ G# c8 d" H1 \& F
  8. >>> math.sqrt(20)! R9 m3 L6 v2 @8 l# Q8 s4 p" w, r
  9. 4.47213595499958
复制代码

0 k! ?8 [9 X/ R$ t" o$ i; ?math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分5 b$ Z! c  m6 O7 B
  2. trunc(x:Real) -> Integral' J6 k% f$ E' b  A
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 M4 ?& S  U  _$ y. [' X0 K
  4. >>> math.trunc(6.789)2 d7 V& e" {" ~$ ^
  5. 6
    & B/ Q+ O! j- [
  6. >>> math.trunc(math.pi)
    $ u3 h4 M5 ~' {# z/ g' `$ p+ m
  7. 3: `: h. i, K) b& P) M9 k
  8. >>> math.trunc(2.567)1 {' L8 j) J' I$ v: @- n+ h  o
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-2 22:24 , Processed in 0.082208 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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