新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

+ G! \: i' N5 p3 y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
! N" W( y; C; W( @
( s- i) L* X8 u# k. n; n; H
方法1
8 N" \% `% l8 B& v% N- x: s
  1. >>> import math
    + y) T0 v9 r9 B
  2. >>> math.sqrt(9)
    8 b1 z1 g1 C5 t. C# d
  3. 3.0
复制代码
方法2! _* W" X$ Z/ M$ n
  1. >>> from math import sqrt$ w+ l' _" \# b7 e/ O& w
  2. >>> sqrt(9)
    % b/ @0 A" E) U/ I3 o9 a8 M
  3. 3.0
复制代码
/ ~1 Z( w: f0 }- s( k( ~. n# t7 T3 y

' U' z6 g" E& E- S1 {, j) W
math.e  表示一个常量
/ P7 e( s6 L: v# {+ o3 Y
  1. #表示一个常量
    $ I$ F9 D4 y6 O" L7 y. x; t
  2. >>> math.e/ @( z+ O9 o( k3 L$ U
  3. 2.718281828459045
复制代码
$ I/ v+ v& }+ @" n- m: `. E5 z6 j$ I
math.pi  
数字常量,圆周率
6 r* [5 \6 i; Y
  1. #数字常量,圆周率- p* t9 a% g8 V" \% T1 a# @
  2. >>> print(math.pi)
    + i6 I) H" o. Z' K* m# V
  3. 3.141592653589793
复制代码
1 {8 s- _) r$ s$ R2 |) c5 \7 r: ], m# P
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

5 X4 s9 q+ Q2 d) i+ r
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
      Y5 ?3 z% f' E, c0 O
  2. ceil(x)" J+ p% M; R% }! p3 s# M- Y" k
  3. Return the ceiling of x as an int.$ Y8 r" a. ^) l( m: Z% B7 O
  4. This is the smallest integral value >= x.  f& a, X8 i; B3 n

  5. : _+ s0 H3 ?% V; W- E+ v' F( m
  6. >>> math.ceil(4.01)4 }( q( u7 `4 J* s/ f0 M  B
  7. 5% g' `$ `- Y1 [! p6 h, ~) r
  8. >>> math.ceil(4.99)4 ?8 K, w, w$ ^. ]6 `6 t! d0 o# u8 v
  9. 5# {( p9 `8 w. \
  10. >>> math.ceil(-3.99)
    : F" J9 e. b9 A1 X4 s$ i
  11. -3
    ' E4 i  N9 y0 L7 z
  12. >>> math.ceil(-3.01)9 ^( A9 F4 h* p' h( c, D1 p6 o
  13. -3
复制代码

) o# d3 {! X# H" d! O) S- D- E; rmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' |2 R. X1 K7 L" f
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ T  @& s5 P  O# r1 Z
  2. floor(x)& `) T3 X1 T' [) x+ \+ Q3 b0 ^
  3. Return the floor of x as an int.
    9 i( S$ ?4 n$ C- ?/ w! P) E
  4. This is the largest integral value <= x.# H! q  }# G4 C$ \: p2 |" ]
  5. >>> math.floor(4.1)8 `7 `- l' ?" N  o
  6. 4, }1 O- }* e3 q6 {, W$ |% t/ q& D! t
  7. >>> math.floor(4.999)
    + Y$ a- q0 n% s+ `4 u7 X+ r
  8. 43 U% T0 ^8 {9 K& a" Y0 p
  9. >>> math.floor(-4.999), v7 s1 V) q! w9 V
  10. -5
    . S% s( N! C+ V' ^; `, E
  11. >>> math.floor(-4.01)" _; d' H) n/ p( `6 I
  12. -5
复制代码

* f9 a9 k  o4 `  Qmath.pow(x,y)  返回x的y次方,即x**y
, I# _7 a& m0 q. d" G& Z& ?
  1. #返回x的y次方,即x**y
    * p5 I  p/ q/ u& b* B
  2. pow(x, y)
    ) D+ k) w; v# U% t3 T
  3. Return x**y (x to the power of y).
    0 i& J/ c2 O- X+ x7 L0 N  H. K
  4. >>> math.pow(3,4)' h# \6 I8 ]( {5 |! u% H. r- w
  5. 81.0' W; m( i* w6 g0 ~# J# w7 Z7 s
  6. >>>
    / g( d: o, B' N% _  |
  7. >>> math.pow(2,7)
    * i; `" U/ ]" e; [% y
  8. 128.0
复制代码
& F1 {4 P2 x1 @# a* ?/ ?' C
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 M& m7 h; P: H- s
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* r4 H4 f5 c: C; k' J( ^
  2. log(x[, base])
    8 K: P. |& `. R: ^2 y- @9 v
  3. Return the logarithm of x to the given base.
    6 ~2 F) R' h/ L0 Q6 {
  4. If the base not specified, returns the natural logarithm (base e) of x.. A/ m: H) P' q+ W9 o) K2 b
  5. >>> math.log(10)) W! y! u, ]# V$ `& p; \
  6. 2.3025850929940463 a! [& r( T. d/ J& r
  7. >>> math.log(11)- Y! `! h& f# a1 A1 c1 O2 W' i% {
  8. 2.3978952727983707; x7 T$ j+ r8 y7 ~
  9. >>> math.log(20)7 P* N1 N7 O% \8 r
  10. 2.995732273553991
复制代码

# J" v2 K9 D2 V% s# h  Emath.sin(x)  求x(x为弧度)的正弦值
9 P' I3 Q) _" U1 B
  1. #求x(x为弧度)的正弦值' x; d3 _  W" \( K+ w
  2. sin(x)
    ! W$ X2 ?$ I5 n% T4 _
  3. Return the sine of x (measured in radians).
    ' L& R, o/ R$ y
  4. >>> math.sin(math.pi/4)
    - z8 j! S$ ~& J6 k6 {+ h
  5. 0.70710678118654758 S4 P# Y* M8 s* f5 {
  6. >>> math.sin(math.pi/2)5 G/ _8 @$ R2 t0 G! N2 N
  7. 1.0" [1 q. o+ N9 o1 }
  8. >>> math.sin(math.pi/3)9 E$ x& t5 K) M" Q( G8 S
  9. 0.8660254037844386
复制代码

& d1 C' |7 T$ c: B4 ~+ x! bmath.cos(x)  求x的余弦,x必须是弧度
4 q: Y9 @- a1 a+ Y
  1. #求x的余弦,x必须是弧度
    1 C) a3 o! y0 P
  2. cos(x)& ~, h1 F4 f9 f9 w3 c
  3. Return the cosine of x (measured in radians).
    ! c3 b. n+ B5 z$ L
  4. #math.pi/4表示弧度,转换成角度为45度8 l' f, {. [1 k2 `; h
  5. >>> math.cos(math.pi/4)
    0 q+ `+ |0 S1 K# }# o
  6. 0.70710678118654765 P9 A5 f; j% Q. `2 x# b
  7. math.pi/3表示弧度,转换成角度为60度" i6 s: ~8 t4 P& |+ Z
  8. >>> math.cos(math.pi/3)
    ; h% F4 r/ Z/ A/ w
  9. 0.5000000000000001; r8 ~, z5 t" l
  10. math.pi/6表示弧度,转换成角度为30度* F1 C& S" U9 d- ~" F' J& k
  11. >>> math.cos(math.pi/6): b- c$ K( h, B3 D. D$ L
  12. 0.8660254037844387
复制代码

* @( I1 K' L; C1 w( U% Cmath.tan(x)  返回x(x为弧度)的正切值* d. m$ e& R. [" m% B
  1. #返回x(x为弧度)的正切值
    ( `, e$ f1 Z3 K0 _% B. P
  2. tan(x)
    " q8 J! k7 r; k6 D
  3. Return the tangent of x (measured in radians).' Y" e3 P: u7 L( d0 k
  4. >>> math.tan(math.pi/4)
    ) d4 E8 |$ X0 s5 v& T% j4 g+ p
  5. 0.9999999999999999
    ' A% J* ]( Y+ |# ]# v; O! x
  6. >>> math.tan(math.pi/6)
    - P# V: @  [( ]1 c- F! s
  7. 0.5773502691896257
    . s! j; M' _6 _+ p1 c% f4 W$ F! t
  8. >>> math.tan(math.pi/3)4 h0 D9 Y' `* x) |) I
  9. 1.7320508075688767
复制代码
: K" O' _; w! a8 m5 |
math.degrees(x)  把x从弧度转换成角度6 Y  |1 @1 y( ~2 k4 y
  1. #把x从弧度转换成角度
    % o7 y( A1 E+ q
  2. degrees(x)
    ( v8 n4 u: r: q6 m
  3. Convert angle x from radians to degrees.7 o) ~# H, s1 H0 ~

  4. ; H) l/ K8 w" Y" H* O: D+ J
  5. >>> math.degrees(math.pi/4)
    ! t$ m, }2 G5 P( n. F
  6. 45.0
    % J& n, d6 Q/ R- L7 I8 f
  7. >>> math.degrees(math.pi)  ^9 x8 \0 r! k
  8. 180.0& p2 A1 w+ w, l: t6 j
  9. >>> math.degrees(math.pi/6)
    2 o3 w" h: g: W, W5 C& o9 Z: h
  10. 29.999999999999996
    - V$ c: S0 H: t4 g
  11. >>> math.degrees(math.pi/3)
    ( |* K/ Y# ^1 n
  12. 59.99999999999999
复制代码
# W9 p# i+ v8 I+ l8 [! |9 D, \6 Y# E
math.radians(x)  把角度x转换成弧度
! u: V) ^8 g$ Y' C& n' o, {+ k
  1. #把角度x转换成弧度
    . A* L) J0 ]. `9 d/ t, k
  2. radians(x)
    5 l5 N8 k' {1 h+ P4 h9 k8 a* S
  3. Convert angle x from degrees to radians.* ~  C0 i, D1 B  ]2 d" n
  4. >>> math.radians(45)
    ) U) o4 I* ?! G% V$ p0 B
  5. 0.7853981633974483$ D2 e' X, H  \/ i, C9 V
  6. >>> math.radians(60)
    ( ~0 p8 j( F# r) H6 O1 `/ o
  7. 1.0471975511965976
复制代码

+ w% R2 H5 a1 ~math.copysign(x,y)  把y的正负号加到x前面,可以使用0
, K! f6 k" r8 [! c" ?1 `! n
  1. #把y的正负号加到x前面,可以使用0
    . R$ _) ~, y. F9 M# R
  2. copysign(x, y)6 q% |% J5 q. X  D3 a- G# \" @
  3. Return a float with the magnitude (absolute value) of x but the sign
    3 Y8 Z5 _9 c2 V4 Z
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) / G1 q7 {6 j) l; }
  5. returns -1.0." f8 ^" ^+ s7 |9 @+ Q' s
  6.   x0 t$ i5 N2 m& M4 h
  7. >>> math.copysign(2,3)
    7 p: t0 @7 }* N! C- r
  8. 2.04 Y3 X1 m; L7 A# |1 P) u
  9. >>> math.copysign(2,-3)8 k& y4 J6 l# T: [, g
  10. -2.0; q; v5 O& O+ ~9 @2 P/ T+ ^
  11. >>> math.copysign(3,8)
    ) u; d+ w9 L+ f( c0 P0 Z
  12. 3.0
    4 w, h8 {, c, y- Y
  13. >>> math.copysign(3,-8)
    7 i& [+ W  Y( Y% A- k5 a
  14. -3.0
复制代码

- }0 Z3 M8 D9 L  Y- w, A6 ]math.exp(x)  返回math.e,也就是2.71828的x次方
1 q- I: n! R. K  e7 P8 C( a
  1. #返回math.e,也就是2.71828的x次方& C8 H3 i8 ^; V# p4 ~6 O
  2. exp(x)2 f: q2 w7 @$ R+ R1 L
  3. Return e raised to the power of x.0 ?1 a9 T) m  }
  4. ; @# j/ j% [( Q/ |! m
  5. >>> math.exp(1)
    ) g8 _) g6 ?# `  f
  6. 2.718281828459045
    2 |- A- T8 }! C
  7. >>> math.exp(2)
    2 S0 B. W/ U# F# M; a9 _
  8. 7.38905609893065
    2 q+ q& K4 k9 }- B8 T
  9. >>> math.exp(3)/ D' ]; m+ t! x: Q
  10. 20.085536923187668
复制代码

% j% j0 M  f% Omath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
" k5 G4 z  G, X+ r
  1. #返回math.e的x(其值为2.71828)次方的值减19 E2 E  ~" L  S+ n6 _# W
  2. expm1(x)
    . z6 V7 v# e/ K! F  \% k$ K: d6 M" ~
  3. Return exp(x)-1.
    ( {. V) r" i  t7 a1 a5 r; ]1 G0 i& e
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.& _" D5 A6 p. D6 H' j" D/ X* t8 w7 J

  5. ) s; T7 H% c! O' I* y
  6. >>> math.expm1(1)
    6 A5 H# m5 H' K1 o' Q
  7. 1.718281828459045
    0 H0 S2 W3 L4 A# F: `5 P3 s
  8. >>> math.expm1(2)
    9 D& [+ _( e  V- r9 i# S4 S0 {
  9. 6.389056098930654 S4 E* e0 \3 ]$ ~- W, t
  10. >>> math.expm1(3)
    8 D+ e" K. V  F. T, H( c7 A- B
  11. 19.085536923187668
复制代码
, O) m/ c4 Q3 m. Z$ j$ X
math.fabs(x)  返回x的绝对值- T; p, b* [9 [! _4 T6 s
  1. #返回x的绝对值
    $ c1 d% N6 S+ @- f8 k0 w
  2. fabs(x), R8 c# ^6 a- e
  3. Return the absolute value of the float x.& Y( K' A3 W- u/ `; u2 q, Z
  4. - O) m6 D+ \0 x3 ~
  5. >>> math.fabs(-0.003)
    1 O( C# N- G4 A: `( x% ~
  6. 0.003
    7 {$ G2 v5 v# m9 D: A" [; E
  7. >>> math.fabs(-110)& ^. M; S5 x( n4 u" r9 E+ K
  8. 110.0
    ) _9 x* B( X3 ^, H
  9. >>> math.fabs(100)2 t) s" O6 C9 o
  10. 100.0
复制代码
$ N6 l" f6 ~3 i
math.factorial(x)  取x的阶乘的值" G7 h2 r4 \) n1 I% G
  1. #取x的阶乘的值
    * H9 h- \; S5 w# r% E/ ^2 z
  2. factorial(x) -> Integral' W# l( X, `" l% l
  3. Find x!. Raise a ValueError if x is negative or non-integral.7 I" t5 f- A* S8 c+ d8 v8 \2 I1 ^
  4. >>> math.factorial(1)
    0 E  E; J/ D9 F
  5. 1
    9 h/ m+ J+ S$ s
  6. >>> math.factorial(2)4 k, {( s, {* N. {
  7. 2
    * Z0 F% h; L3 E. g
  8. >>> math.factorial(3)! S1 G+ Z- b# w1 M, n) @) w
  9. 6
    . G: C1 X3 O8 O
  10. >>> math.factorial(5)( ~0 l  K) l$ \$ g
  11. 120
    ( D: {/ f1 z* b6 \* }& H$ x
  12. >>> math.factorial(10)) ~1 Z* V1 {$ O5 L1 T3 f
  13. 3628800
复制代码
9 D& {/ n2 P% v. g
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数( V# @1 ?. {+ p6 E0 Z
  1. #得到x/y的余数,其值是一个浮点数3 X* _8 i0 q& w* @0 G9 I* |
  2. fmod(x, y)- C6 F3 u6 N/ K& ]3 p" @9 d& k7 O4 ~/ B
  3. Return fmod(x, y), according to platform C.  x % y may differ.7 `: x4 [1 g8 f$ w0 l
  4. >>> math.fmod(20,3)" ^4 E  D4 {4 f/ [
  5. 2.03 G  J; Y. w4 ?' [( R6 p! |2 n3 m) V3 p
  6. >>> math.fmod(20,7)
    ! ~1 R! d: C! p0 a6 X
  7. 6.0
复制代码

' e/ {/ V# @- V, N5 Amath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% y1 Q, A2 w. G8 P7 U: k; q
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 [) d' w6 c. O+ S
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 [' G. y- }2 l& v2 m
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) M4 N+ w2 A- C
  4. frexp(x)% _. _6 ~# B) k; u; j& B
  5. Return the mantissa and exponent of x, as pair (m, e).
    & _3 t* Z" e4 B9 j* a9 f1 m
  6. m is a float and e is an int, such that x = m * 2.**e.
    ! T- w9 z" B' u" s) c& g6 c
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- e8 \/ x$ t- w. u4 j
  8. >>> math.frexp(10). H: M6 o7 \& F4 K- j: r- z8 [4 t
  9. (0.625, 4)
    0 u7 R0 d, k9 O6 ?& P2 S
  10. >>> math.frexp(75)1 w  m* i* w$ w* s! m( V7 |6 U7 z
  11. (0.5859375, 7): t; v. ^% w/ b0 k" C
  12. >>> math.frexp(-40)
    7 \' R  }+ Y6 d# a$ u# y' h7 ~
  13. (-0.625, 6)
    / Y" c! S% p) l. e$ `+ X8 R) Q1 c
  14. >>> math.frexp(-100)7 t4 r0 D% u) W1 K0 N
  15. (-0.78125, 7)& g  N; d  ~' i0 Y$ D# o2 J% i
  16. >>> math.frexp(100)
    * d' w# h$ \3 ]2 k$ W
  17. (0.78125, 7)
复制代码
/ k3 S7 Y6 o/ U. v* ^: ]- B
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 ~' o# W, t6 j; m: t: t' K9 G
  1. #对迭代器里的每个元素进行求和操作
    - D# k& l" X& Q& N3 p
  2. fsum(iterable)
    4 a7 O. X$ p' I2 X0 R
  3. Return an accurate floating point sum of values in the iterable.
    , _: d$ ~- [, `. t
  4. Assumes IEEE-754 floating point arithmetic.
    2 E; e1 _- Z! s8 M4 y
  5. >>> math.fsum([1,2,3,4])
    - f* \% W3 \( u
  6. 10.0
    ; I4 N$ b, U1 F4 n9 N& o0 G7 l
  7. >>> math.fsum((1,2,3,4))/ O! c6 z  E2 f0 M- z( q6 k" A
  8. 10.0
    ) S; a% b- h% R- b
  9. >>> math.fsum((-1,-2,-3,-4))2 i/ D& j' n$ n6 g
  10. -10.0" d8 x$ f$ v6 j2 z! Y* j0 r" P
  11. >>> math.fsum([-1,-2,-3,-4])& j' X2 F' }5 Q" ?" |1 o" a) N
  12. -10.0
复制代码

8 U7 r, K) e; v( gmath.gcd(x,y)  返回x和y的最大公约数
3 {- {3 k( p% V$ p' ^7 g
  1. #返回x和y的最大公约数
    2 p+ @) A4 f3 E' D3 q5 _: t3 ^
  2. gcd(x, y) -> int
    . o, K6 o8 a$ s* F# W) f
  3. greatest common divisor of x and y3 Q* ~! q6 R" A9 p# ~4 M1 ]5 Q
  4. >>> math.gcd(8,6)3 g2 Y/ P+ x( T1 I9 ^3 R
  5. 2! h: k- F3 X$ R3 z- m6 e8 ?
  6. >>> math.gcd(40,20). X8 C  T" x  a6 q5 {9 e
  7. 20* K6 o2 ?0 i# o: w3 I: }* g8 H) Y
  8. >>> math.gcd(8,12)
    3 ~3 {: }2 b& J/ _) k$ g
  9. 4
复制代码
  W% r, }; n2 Z! U) S, ~
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False. Z( Y7 _$ Y9 B, t
  1. #得到(x**2+y**2),平方的值
    6 Y- v8 t. n6 _! z' w. [# `
  2. hypot(x, y)# F7 }6 g+ h3 ~" Q- e
  3. Return the Euclidean distance, sqrt(x*x + y*y).! Q1 h5 G" Y$ E& T) B( Q3 y: u
  4. >>> math.hypot(3,4)8 Q* I- j7 T. j
  5. 5.0# G7 g* O2 R$ b& ^
  6. >>> math.hypot(6,8); ?( t4 E- H. j& u. d9 y. Y
  7. 10.0
复制代码
6 Q' j% i9 l" q9 [9 H# L  k3 P) f
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False7 ^3 k( |6 X  d- L, _3 _
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" O4 N; P( b' c% J. D
  2. isfinite(x) -> bool
    ( K& g+ W! `6 h8 P' p: m' ?
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    6 q# a9 u, R  Z9 W& u
  4. >>> math.isfinite(100)7 w+ R! i0 H3 l) R: m% V
  5. True
    $ e- o5 I; ^5 O9 @! I+ C6 M
  6. >>> math.isfinite(0)  R" _' E% E/ y. |( o5 F5 r
  7. True
    1 X; T. v" u# G' R& w# r4 }3 F
  8. >>> math.isfinite(0.1)
    6 R" g7 s" a6 v  e
  9. True
    ) Z$ e% _; Z" y" D0 J3 D
  10. >>> math.isfinite("a")
    4 l  {6 S. N, J, }" G. ]
  11. >>> math.isfinite(0.0001)
    ; F. q! u: y" P
  12. True
复制代码

' L4 H8 G7 Z1 A3 F2 Smath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 ]2 U: S0 D, t, {5 X4 |
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    8 A# Y6 K+ p$ e, s6 t
  2. isinf(x) -> bool
    % ^- f; C$ w* d
  3. Return True if x is a positive or negative infinity, and False otherwise.
    9 j7 `/ K* A9 ^! Z  x. t6 r2 P% ?
  4. >>> math.isinf(234)
    ) V  Y  R3 ^, d( I$ T0 c7 V
  5. False3 y3 K+ s3 j0 @
  6. >>> math.isinf(0.1)  O0 _9 O5 s2 p  e
  7. False
复制代码

1 r. o: w& ^, E0 C3 Q4 i* r( G- _4 D; {2 bmath.isnan(x)  如果x不是数字True,否则返回False( l  p7 A5 w/ d
  1. #如果x不是数字True,否则返回False
    : p' U: {% _3 c/ v
  2. isnan(x) -> bool, M( b# _0 P) S# T8 G# _
  3. Return True if x is a NaN (not a number), and False otherwise.( P* I, p) X+ M8 q- K" A" T$ f
  4. >>> math.isnan(23)4 e5 H- H; C# x% h4 K" n# a
  5. False) @2 y* s( M+ W  W6 {
  6. >>> math.isnan(0.01)
    & }0 Z$ d' t' l" R- h0 V4 y1 |
  7. False
复制代码
; ~/ t+ E  C+ A1 D% Q& q
math.ldexp(x,i)  返回x*(2**i)的值
" a. L  @7 e, u+ J% l
  1. #返回x*(2**i)的值0 L+ |& m! z6 H9 _
  2. ldexp(x, i)
    * B- E  i$ s% Z1 r
  3. Return x * (2**i).
    0 `& {' `; w' k3 n7 n/ ?5 n
  4. >>> math.ldexp(5,5)
    7 E4 c+ S+ P2 q( {
  5. 160.0
    $ `. n6 E9 n9 ?
  6. >>> math.ldexp(3,5)4 y5 |6 x! ]+ J
  7. 96.0
复制代码

" O" P. t, l) k+ D$ zmath.log10(x)  返回x的以10为底的对数
' w8 y8 }$ r( U( Q( s  f* y3 T0 f2 W
  1. #返回x的以10为底的对数, U# C* d' M4 T
  2. log10(x)5 @* p( R/ A3 S' l: `# |9 B, Z
  3. Return the base 10 logarithm of x.* O+ ~1 U# |- H" T. ~4 C
  4. >>> math.log10(10)! r0 F$ Q+ h* J, @; q' q
  5. 1.00 f4 _% g: g) ]  w7 \
  6. >>> math.log10(100)
    7 x4 z5 H1 Z: E) r5 p# V) Y
  7. 2.0' W$ V9 W% y5 j. B" _
  8. #即10的1.3次方的结果为20
    ' X1 }( y( ?7 [# ]% D# X7 j
  9. >>> math.log10(20)
    5 l) c' U, [% [4 L; \) Y. c0 ]! N
  10. 1.3010299956639813
复制代码
+ J8 U) A9 J; h8 y* ^6 c& l" r
math.log1p(x)  返回x+1的自然对数(基数为e)的值$ s( G4 g$ K( N
  1. #返回x+1的自然对数(基数为e)的值
    , ~+ {' a6 ^, E4 G5 g
  2. log1p(x)
    & J# ^( H, b, A$ K0 G+ [7 P
  3. Return the natural logarithm of 1+x (base e).
    1 e% [9 i8 q2 ]
  4. The result is computed in a way which is accurate for x near zero.
    7 j7 N; ]. `& c8 h
  5. >>> math.log(10)$ J9 T# j/ G1 `+ [* G, ~
  6. 2.302585092994046
    3 B5 r7 i3 ?8 G9 N6 X( R
  7. >>> math.log1p(10)# Q2 o' S8 t9 J) c6 q
  8. 2.3978952727983707
    + ~( {5 n7 q' v; h: I
  9. >>> math.log(11)
    4 |9 ]" r  }; x1 ^: p
  10. 2.3978952727983707
复制代码
  `) S- X+ J$ p5 |8 F
math.log2(x)  返回x的基2对数
) j" m$ |2 w6 _- P6 e/ A
  1. #返回x的基2对数9 F5 T4 ^% Y+ ]1 v% f
  2. log2(x)
    ! m3 q  V) [% B
  3. Return the base 2 logarithm of x.; k# l, a' Y, z& H4 q$ N* `
  4. >>> math.log2(32)
    + i; o# N- X0 _, O5 o
  5. 5.0
    8 w; ?/ K% D, Z# X' I6 W
  6. >>> math.log2(20)7 J0 w$ G: z* ^/ W* v. n- N
  7. 4.321928094887363* V- {8 `" `7 b. m
  8. >>> math.log2(16)6 C6 h+ n" ]6 T  i/ E( A
  9. 4.0
复制代码

) \+ p4 @/ t2 amath.modf(x)  返回由x的小数部分和整数部分组成的元组+ N0 X0 q8 h2 c# A. G2 @6 b+ _
  1. #返回由x的小数部分和整数部分组成的元组
    & r9 }2 ^- X' Y4 `
  2. modf(x)# o8 ~" Y5 ~. L) W8 V2 G5 L
  3. Return the fractional and integer parts of x.  Both results carry the sign
    5 Q/ |  k4 z! E+ e
  4. of x and are floats.: ]# }( t+ b% S
  5. >>> math.modf(math.pi)
    - ~( Q0 d1 \5 t, s' f; [" L$ a) L
  6. (0.14159265358979312, 3.0). G! p9 e, {6 k$ y; S' f
  7. >>> math.modf(12.34): R. e( G( B6 R
  8. (0.33999999999999986, 12.0)
复制代码

; V: ?" p9 o. j' _4 Rmath.sqrt(x)  求x的平方根
- R/ k! P; I2 n
  1. #求x的平方根
    " F2 m, E: g1 B+ A% L
  2. sqrt(x)0 a+ R4 Q; L5 \; j9 y& E
  3. Return the square root of x.
    ' `' v3 B4 m" c
  4. >>> math.sqrt(100)/ H7 [2 {( I' k$ X- C2 ~, C( K4 [0 B
  5. 10.0
    # K/ o  X$ c) P4 _" B
  6. >>> math.sqrt(16)
    1 _4 M) w* v, I: x
  7. 4.0* P& e1 h% S. ]4 V6 p
  8. >>> math.sqrt(20)
    & h+ Z. m& o7 J' a" W1 i
  9. 4.47213595499958
复制代码
# S2 P: R' X  l: E: M5 T
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分0 v: m% U5 L) e
  2. trunc(x:Real) -> Integral$ P4 e/ x* x* o+ C6 y1 g
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    % s  f0 i# g* v$ a( ?; {
  4. >>> math.trunc(6.789)8 J8 H7 Z" }! T7 k
  5. 6% P; b' U4 C7 m5 [( j. Y0 O8 [
  6. >>> math.trunc(math.pi)& w& z( C6 }6 }
  7. 3; Z, a/ P& p4 j/ \8 G
  8. >>> math.trunc(2.567)
    0 T; g0 U, J. m. w& H9 }
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-20 10:00 , Processed in 0.080572 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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