新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

& E" y  W' d: E; q) P# \2 |【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; U, \+ i8 q- h2 _* m
" H! Z! k3 E- T+ [7 H
方法1
9 E$ d! R) i6 h9 z6 x; }
  1. >>> import math% W4 z0 O8 U( i; E+ F6 q
  2. >>> math.sqrt(9)
    9 J3 |0 b' B6 d( Z" R, s) t4 U
  3. 3.0
复制代码
方法2
3 |! e9 b8 p- a$ z! I. \
  1. >>> from math import sqrt
    ) A# }- V: ^" g/ w/ Z
  2. >>> sqrt(9)) o/ `' k) V+ B' c# h9 o
  3. 3.0
复制代码
0 u9 G; r0 u* o+ Y; Y


4 @# b* f2 H) g/ f. @1 k% ]8 s% {
math.e  表示一个常量
# `  G6 V& ^# S: O! {# L7 r
  1. #表示一个常量
    " j/ s8 {* }/ T0 U& O1 l
  2. >>> math.e
    8 I- C4 r' a0 Z+ e1 R9 Q, }$ f* `; b
  3. 2.718281828459045
复制代码
0 p- r$ ?  @9 [6 l5 i: ~
math.pi  
数字常量,圆周率

: J) R+ a7 b  `/ q
  1. #数字常量,圆周率! K1 z' [( C* S: e; Z; Z
  2. >>> print(math.pi)- k- Y3 P7 g! [3 r! c- E
  3. 3.141592653589793
复制代码
6 l% P) n0 b" c& D0 k" |" e$ r
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

' z- H# v. C3 V+ n2 x$ ^8 d
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 }' C3 o* _% M' r) S
  2. ceil(x)
    2 U6 }* t" t5 ?" @
  3. Return the ceiling of x as an int.
    ; f# ?! S) r, r7 x, A( B, Q5 n
  4. This is the smallest integral value >= x.' ~- p$ b! B& N0 T

  5. ' ?0 [0 h6 g( i1 X3 P+ w8 i" i9 R
  6. >>> math.ceil(4.01)
    $ B/ Y. g- K0 k
  7. 5
    ; P) J# o9 R; q) i! H( M
  8. >>> math.ceil(4.99)& K& i3 p  |% \; G
  9. 52 P$ T6 Q# b9 x$ i5 Z
  10. >>> math.ceil(-3.99)
    7 Z3 U& F) P9 O8 X
  11. -37 Z0 y6 P6 j8 W+ K
  12. >>> math.ceil(-3.01)
    / D5 V$ C" B" Q+ R
  13. -3
复制代码
* A5 h. |" A8 F
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. J9 w2 a) W3 Q9 f3 a4 [% v
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 z5 l* O) l/ ]! d7 K6 X, Q! n- Q
  2. floor(x)8 x4 w- l  d( v; D( V0 X/ A1 O2 K
  3. Return the floor of x as an int.
    . d$ c$ j' ~2 S- ]8 d
  4. This is the largest integral value <= x.: E" d5 q9 v( C4 A0 `9 ~. `' S
  5. >>> math.floor(4.1)
    9 S% w/ W2 O" ]6 D8 Y" R
  6. 4
    % N2 ~: h$ s) o) B% o
  7. >>> math.floor(4.999)) M4 s! E* V' L$ k# L1 @: S
  8. 4; R8 ?" v8 [3 d9 e* B6 M# Y
  9. >>> math.floor(-4.999), S' j' N. z  I# T
  10. -5' V  E6 i* S, A# Y5 m% z
  11. >>> math.floor(-4.01)
    6 q( a4 g# c1 C& ~& T6 D: b7 A
  12. -5
复制代码

. T  i1 K" k1 `1 imath.pow(x,y)  返回x的y次方,即x**y5 f: j5 r' i% e7 V$ e3 E7 v
  1. #返回x的y次方,即x**y
    " r  p5 E& ?* L( J( W9 n& X
  2. pow(x, y)
    0 w2 b3 `. U7 _  e" c# c
  3. Return x**y (x to the power of y).
    : b3 ^# h# _$ \+ V  x3 D
  4. >>> math.pow(3,4)
    2 [6 d+ Q  A, t  K
  5. 81.06 k) _" k3 ^) M# ~. m, t
  6. >>> / C# m% W5 m* g6 V+ L
  7. >>> math.pow(2,7)
      i  d9 W# k( M# ?2 ?) e
  8. 128.0
复制代码
; ]/ a) s8 @0 [3 m) i2 t0 E
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 P  Q/ W) y" d7 J! h+ ~( S. p5 j" j
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  ^2 h* {) ^7 }! C& w4 I
  2. log(x[, base])9 q5 w8 t( J& Y6 D: \
  3. Return the logarithm of x to the given base.8 v) H$ m$ [- @3 Q+ G+ L$ y
  4. If the base not specified, returns the natural logarithm (base e) of x.
    2 p" G  @. b) `7 }4 |3 |6 ]) g
  5. >>> math.log(10)9 P: u$ u0 n2 m8 E7 r4 O8 i
  6. 2.302585092994046: \. K9 j' d  Y; Z. ~1 r
  7. >>> math.log(11)
    $ M. M, n3 Z6 x) M0 @. h
  8. 2.3978952727983707
    9 o2 ^2 z2 e$ ^  m4 t1 n$ r
  9. >>> math.log(20)
    9 B) S, I9 W; W& d
  10. 2.995732273553991
复制代码

7 L: B. ^) ]! }$ smath.sin(x)  求x(x为弧度)的正弦值, |: i! m( q: q+ k, b
  1. #求x(x为弧度)的正弦值, }9 {& N# R$ e3 ~! Q( b
  2. sin(x)
    / ~' F' C& [+ p( e7 N
  3. Return the sine of x (measured in radians).9 j3 ?# T% e- e+ u# D& p
  4. >>> math.sin(math.pi/4)( K8 N+ S; t( F
  5. 0.7071067811865475# t  f$ X6 g  h* p$ O5 ^" L8 T# a
  6. >>> math.sin(math.pi/2)
    ( M3 O' C! @: L; q
  7. 1.0' x1 L1 {* V. t: H/ ]; E
  8. >>> math.sin(math.pi/3)  i$ B( Z' R( G! i
  9. 0.8660254037844386
复制代码

4 P$ P  \8 s5 i' U6 v. q3 a  V* Umath.cos(x)  求x的余弦,x必须是弧度9 H9 B+ H* W1 ?) G
  1. #求x的余弦,x必须是弧度
    . d) j9 f! K* T! ^; V$ t& y' ^
  2. cos(x)4 a, P6 [! @+ B8 n; r
  3. Return the cosine of x (measured in radians).
    9 ^, ~+ D, s, ?2 d
  4. #math.pi/4表示弧度,转换成角度为45度
    3 I, N7 R: J( M  y
  5. >>> math.cos(math.pi/4)! M+ @8 g' k# ]# w, m  A
  6. 0.7071067811865476* g4 q4 G0 y4 b" z; E) C9 @. C6 U
  7. math.pi/3表示弧度,转换成角度为60度& ?5 e: \$ i) u3 {, f8 L
  8. >>> math.cos(math.pi/3)
    ! d9 q; O* ~7 i& X# J7 e- t
  9. 0.5000000000000001
    6 J8 s2 ?' S7 @: s
  10. math.pi/6表示弧度,转换成角度为30度
    9 g& W. g4 ^! d% _: F7 W" y. ^
  11. >>> math.cos(math.pi/6)
    ! `3 _% S& u  t) Z6 ?- w
  12. 0.8660254037844387
复制代码
9 `+ V3 m& J6 H# b/ _
math.tan(x)  返回x(x为弧度)的正切值
1 x- R/ g+ l4 O( W  c. a
  1. #返回x(x为弧度)的正切值+ n2 L, D& q6 ^6 V3 U; t" d* v
  2. tan(x)
    7 @9 f# u" g/ y
  3. Return the tangent of x (measured in radians).( b1 N9 K/ y0 V% ?
  4. >>> math.tan(math.pi/4)* e5 b$ @. k; g1 S
  5. 0.9999999999999999
    $ A1 ^# @; V) _
  6. >>> math.tan(math.pi/6)" ~& Y3 e  V. ?  E+ L. ~3 a
  7. 0.5773502691896257
      o% f7 i9 ?/ S/ K2 R4 n6 G; q& l
  8. >>> math.tan(math.pi/3)% z, s& Q3 A# A4 _3 G  {: j
  9. 1.7320508075688767
复制代码
2 @7 W! Q2 ~0 }4 d5 W
math.degrees(x)  把x从弧度转换成角度
/ |! k* ~/ [6 F
  1. #把x从弧度转换成角度
    8 F+ r$ S" C' R' R
  2. degrees(x)
    8 o% O" N0 |& N
  3. Convert angle x from radians to degrees.. p# d: L& l; q; j1 E4 y
  4. ' g2 g, X  ~+ b
  5. >>> math.degrees(math.pi/4)
    4 M2 C# i' q/ U  j8 ?
  6. 45.00 d5 H4 G( t6 m; Q# |
  7. >>> math.degrees(math.pi)7 Y. s6 D: Z" M5 m; h' T5 D8 w; Q
  8. 180.0
    6 k/ ~' J# n7 z1 L& ?6 e
  9. >>> math.degrees(math.pi/6)
    $ [' K+ w0 ~6 m
  10. 29.9999999999999963 k' B! }' F9 Z5 f. f
  11. >>> math.degrees(math.pi/3)
    9 i' q& m. S4 m9 e* l  Z  B; U
  12. 59.99999999999999
复制代码
) B0 |1 P5 |# y3 V
math.radians(x)  把角度x转换成弧度
0 h: L, e# t1 `  _/ g
  1. #把角度x转换成弧度* @5 E: I: f" ?, k% x. s, L
  2. radians(x)- {  j$ Z" ^9 k4 U
  3. Convert angle x from degrees to radians.4 c# J6 `: {8 V* F/ l4 O3 f8 Y! u
  4. >>> math.radians(45)
    ' y1 E$ Z6 V. g( j* K* `
  5. 0.7853981633974483
    ! s/ ~1 B4 W3 @/ i5 M) b
  6. >>> math.radians(60)
    ! I6 i$ V2 C" ^6 ~# r+ j7 n
  7. 1.0471975511965976
复制代码
+ `4 _, n6 g- R: Z: I/ _8 f
math.copysign(x,y)  把y的正负号加到x前面,可以使用0% t# A7 D' Y' B/ j
  1. #把y的正负号加到x前面,可以使用0* s* K8 c" K1 ^& ^' o. _
  2. copysign(x, y)4 P1 q* R) N! q/ r7 t. ]
  3. Return a float with the magnitude (absolute value) of x but the sign
    & A( T9 n1 D0 M' B1 d+ C* H4 `
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 i1 Q2 K$ n7 r  F% O4 y9 M: R
  5. returns -1.0.9 O  ?! A8 z! A4 x

  6. 8 B! @# f7 z2 [
  7. >>> math.copysign(2,3)4 Q; Y+ f7 l' D3 a6 s
  8. 2.0% A7 A7 g$ Q2 X1 B8 N8 [
  9. >>> math.copysign(2,-3)7 J6 c/ M; F' n6 x$ C3 }2 T: w
  10. -2.07 V4 I7 q  p+ p* d* U  g2 F5 A
  11. >>> math.copysign(3,8)" X) e' b8 I# U1 Z( R8 j
  12. 3.0
    5 O( `, X; y  A: l) _; b" y
  13. >>> math.copysign(3,-8)/ M8 F1 K! ~0 Z/ z: `) i  W
  14. -3.0
复制代码

8 O5 }: ]: v5 n( Xmath.exp(x)  返回math.e,也就是2.71828的x次方
& z( }& i( d% i9 V* G
  1. #返回math.e,也就是2.71828的x次方4 y' Z3 L7 x) b& E" P
  2. exp(x)
    ; E3 S- Y( E: e& C. T! P+ Y7 _
  3. Return e raised to the power of x.
    " _4 n% O, [/ Q/ ]
  4. & |, e/ K9 N& ]6 w& U6 T
  5. >>> math.exp(1)4 F& r2 I9 S- \
  6. 2.718281828459045
    ! c+ g( W- a2 N. D; u5 O
  7. >>> math.exp(2)
    - O4 m7 U( z0 ]" x' O
  8. 7.38905609893065# o! |% N0 h5 T- x& u0 [
  9. >>> math.exp(3)
    % d/ N) I: a+ y4 v
  10. 20.085536923187668
复制代码

$ E8 E  L! H# w4 a8 Emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
* l1 `0 ]( J: ~. R) @7 R
  1. #返回math.e的x(其值为2.71828)次方的值减1
      P  w8 q( @2 q+ K6 m
  2. expm1(x). R0 z% c6 R$ w+ p  U
  3. Return exp(x)-1.; l4 e# O" G0 Y& u  M3 b) k
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
      A1 b% Z4 h) F$ E" B
  5. + @5 I; K, a' b9 X
  6. >>> math.expm1(1)4 ~8 z$ F) `" c5 N. @
  7. 1.718281828459045
    ' c, P! |4 t1 f$ b. e$ ^! f" O
  8. >>> math.expm1(2)+ i9 S# z1 K4 k+ c2 N9 I9 G* ~  o. I+ R
  9. 6.38905609893065& O% `. O, l) u
  10. >>> math.expm1(3)5 D, g& q; T5 p, Q% ?
  11. 19.085536923187668
复制代码
- F8 o+ N" l; I3 ?& r2 ?9 A
math.fabs(x)  返回x的绝对值& g. A" R: m; f# I
  1. #返回x的绝对值
    : P% u$ U) D8 O0 X3 _2 G
  2. fabs(x)
    ' z  R, ^2 `) _7 _  ]
  3. Return the absolute value of the float x.
    1 I# W% l5 J1 d0 k/ i9 n" s
  4. 4 h9 b8 F" h/ \7 Q8 ]
  5. >>> math.fabs(-0.003)
    : B& V# N5 w0 `5 l
  6. 0.0033 e8 P) E. r* l# N# H
  7. >>> math.fabs(-110)+ W4 n: [$ J  Q
  8. 110.0; Y5 U4 a# ^0 Q0 |/ w% L# ~
  9. >>> math.fabs(100)
    4 [8 S# d! y1 S( U0 d
  10. 100.0
复制代码

* x) E* x1 R8 z. {' I# n3 Z- z9 t' Amath.factorial(x)  取x的阶乘的值
( v+ w/ |7 ?( V- I" @/ n) Z  K
  1. #取x的阶乘的值; j% z' i, E0 Q/ N9 v
  2. factorial(x) -> Integral" t$ R6 w: e4 J6 ~
  3. Find x!. Raise a ValueError if x is negative or non-integral.6 Y* x( P5 E; X$ I' F$ N
  4. >>> math.factorial(1)' x& Q# C  Y! u9 t5 |
  5. 1& P" Z9 l! s* p: L6 X& W0 M" Q# e
  6. >>> math.factorial(2)/ x/ z6 k: ^7 J" S( b
  7. 2
    - V4 r  J/ n; g3 M2 {$ L
  8. >>> math.factorial(3)
    # @! D+ [5 V4 v$ D( i
  9. 6
    9 J7 h) @/ \: E
  10. >>> math.factorial(5)
    . H8 C. U9 L& A1 s  U* ]
  11. 120. G4 s( E/ h8 B1 D+ U) `
  12. >>> math.factorial(10): \$ b1 O& J% ~: ]# ^7 b% Z7 h
  13. 3628800
复制代码

2 O' Q( X5 V$ F  Omath.fmod(x,y)  得到x/y的余数,其值是一个浮点数0 o( d( f( r% [  O7 k
  1. #得到x/y的余数,其值是一个浮点数4 g3 p/ {( m' R* i8 U
  2. fmod(x, y)
    ; A' D, R! L) A- `* l) e) E& @
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    5 w; u7 ~; V1 ^8 @3 x9 h9 m& v5 v
  4. >>> math.fmod(20,3)
    5 G/ J$ o2 ]% b9 R9 r
  5. 2.0& z* E! V2 n; q1 W
  6. >>> math.fmod(20,7)
    4 _: y4 K' |! \* I' q# k
  7. 6.0
复制代码
2 f' M/ {4 Q1 z  Y' Y7 R6 H  F
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% S' Z7 Y2 y( P9 X" c, D9 l9 u1 c
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 Q! E" M# T! Y+ s+ _: E% `
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值# Q5 A9 v, w8 _, ^% c+ r6 W; R- U
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    , x; Y# Y  a/ e) u
  4. frexp(x)
    ! B, `) A) V% j0 D8 L) `
  5. Return the mantissa and exponent of x, as pair (m, e).
    ; s) V3 w' p: g& L
  6. m is a float and e is an int, such that x = m * 2.**e.; o5 {8 \3 S" i& Z$ Y& l. f: n& d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    # Y5 k% q& j6 {4 j5 N% M
  8. >>> math.frexp(10)0 {8 [6 X( S0 {+ Q
  9. (0.625, 4)5 Q. g; X- l, |; L4 W1 w
  10. >>> math.frexp(75)6 V( {) L, H: P6 j
  11. (0.5859375, 7)
    4 ?) N4 v, y; W# H8 ~) ^" v
  12. >>> math.frexp(-40)/ i" C, v$ X9 v; @0 ]
  13. (-0.625, 6)
    8 [( b" f) R9 T) \
  14. >>> math.frexp(-100)
    ( U& K. m1 i. H- Q3 |
  15. (-0.78125, 7)
    * x/ Q2 c$ Z) ~$ G6 v$ n; D
  16. >>> math.frexp(100)0 W9 A+ G' C  h
  17. (0.78125, 7)
复制代码

1 c9 T, B9 ^0 M- y4 h% @math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
/ i6 L; v# D5 {. ]2 x& K- S1 o
  1. #对迭代器里的每个元素进行求和操作9 b6 y# F0 t  g/ A
  2. fsum(iterable)( n8 p1 U8 {# L7 v
  3. Return an accurate floating point sum of values in the iterable.
    & }- r, Z) B8 Z% ?2 P
  4. Assumes IEEE-754 floating point arithmetic.; D/ G/ S: p; _( g; O+ C& b
  5. >>> math.fsum([1,2,3,4])4 y5 \$ M4 h3 O: T( ^: A- W6 n
  6. 10.0. ~  U( f7 K4 V  E* p
  7. >>> math.fsum((1,2,3,4))# E" W0 }4 ^3 T$ R* x3 J
  8. 10.0
    ) k1 T' Z' A4 G2 H' J' ^
  9. >>> math.fsum((-1,-2,-3,-4))/ k8 Z6 k5 I* I0 ~/ X( u4 j3 O
  10. -10.09 d. X/ c' A8 [- i
  11. >>> math.fsum([-1,-2,-3,-4])$ v5 ?9 |6 L  h4 \9 i8 u
  12. -10.0
复制代码
5 l8 ?2 i6 D2 M1 f
math.gcd(x,y)  返回x和y的最大公约数( V  [" p( K- u. N% ~/ _
  1. #返回x和y的最大公约数7 p0 z3 X, k* H
  2. gcd(x, y) -> int
    6 L0 A) c7 f- @' {, }
  3. greatest common divisor of x and y/ \4 f$ y7 c  Z" Q% s' U& b
  4. >>> math.gcd(8,6)
    1 H, H! w* `- m% W0 b* D
  5. 2
    - a. _; N6 c, P6 y1 a
  6. >>> math.gcd(40,20)  J  k; s9 U+ Y
  7. 20" j9 z* P. A0 ~, F3 e2 b5 `& j/ W
  8. >>> math.gcd(8,12)
    ) g% k$ e2 c; ?, A8 g7 [
  9. 4
复制代码
4 K/ ]4 i- W- f) f8 f( o! @4 [3 Q+ g
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
( k9 j! J/ _8 [$ G7 @- p3 @$ W
  1. #得到(x**2+y**2),平方的值8 p9 Z; u! |+ i" s; D3 }
  2. hypot(x, y)
    ' P4 r& e# Z# {. B2 Q
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 o+ [. d8 N" K# q2 u
  4. >>> math.hypot(3,4)
    1 ?  W9 T' B. h
  5. 5.0; h- C8 I/ B! D# c
  6. >>> math.hypot(6,8)
    + [/ h5 j* G1 E% `# j3 I
  7. 10.0
复制代码
4 V0 m% p! B4 i4 L- Q& h) i
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False* \* j8 n& s' M! n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False. B$ \1 z- d2 o' }0 M
  2. isfinite(x) -> bool$ O6 \9 B9 d$ p4 b* [( M/ S( T4 U
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    # @0 t7 B0 {" _$ ^" G7 i4 V5 B
  4. >>> math.isfinite(100)
    3 o0 }) n) \' w$ H; p/ h2 x, R
  5. True5 U& l" L7 N  G) ]. s) Y
  6. >>> math.isfinite(0)
    / t& i( O: [1 V" B% L
  7. True
    : z) \# R, |' |# K% l
  8. >>> math.isfinite(0.1)1 ?6 i9 w( ]( ?, d% k! ?
  9. True
    ! V1 ^/ N+ P+ T6 {: \/ [2 R
  10. >>> math.isfinite("a"). W/ d. P9 q' t6 B4 d$ D5 @
  11. >>> math.isfinite(0.0001)
    9 e# t" C  G: x: A( d7 a" U
  12. True
复制代码
" ]' P, w+ c8 Y8 C# Y% \
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False9 q2 ?7 N8 P0 r4 F6 M* Z
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 f; e# r& b; x# q
  2. isinf(x) -> bool: J1 z' j' y) M+ M  e5 h
  3. Return True if x is a positive or negative infinity, and False otherwise." ]( l% E* y2 `( |# \8 h! Y
  4. >>> math.isinf(234)
      l; b8 g3 |4 T) L. \8 e5 Z! A3 ?6 R
  5. False
    & }( Q) [% M% V7 ^9 K
  6. >>> math.isinf(0.1)- E0 F' {$ t4 R& T
  7. False
复制代码

: s; _- L% a/ |; ^9 O! ^: E9 B1 ?math.isnan(x)  如果x不是数字True,否则返回False8 M: a4 X& S, f! i
  1. #如果x不是数字True,否则返回False
    + X! h: m0 `, {- _' r
  2. isnan(x) -> bool% @+ a; ?5 a, i/ T! \9 Y
  3. Return True if x is a NaN (not a number), and False otherwise.
    ( [# N# D) R# b, u; X$ M6 T- h6 E
  4. >>> math.isnan(23)& A9 g2 O) c3 U- `+ G! @1 s
  5. False, U9 y+ @  I/ d  M: Z8 g. M
  6. >>> math.isnan(0.01)4 i7 u" n/ I, P6 i
  7. False
复制代码
# j5 F0 ], x, C& M4 L3 N
math.ldexp(x,i)  返回x*(2**i)的值
- P  W6 C4 \/ u
  1. #返回x*(2**i)的值! q; o) d! V- S/ ?3 Z
  2. ldexp(x, i)0 L- g$ W& W) o3 a
  3. Return x * (2**i).6 d- ]# n9 I( ^- Q: [7 v
  4. >>> math.ldexp(5,5)& d6 V" r8 g5 n" l
  5. 160.0
    ) h. N# l6 U  B- G$ H' P  N
  6. >>> math.ldexp(3,5)
    : N: {; H$ Q2 G
  7. 96.0
复制代码

( |) n, m& C9 z' y- s) u5 gmath.log10(x)  返回x的以10为底的对数. @5 D1 p" N6 V) i( J9 y, L7 {: F
  1. #返回x的以10为底的对数5 J% x+ H* e, K
  2. log10(x)6 Q% K, ~6 O7 `
  3. Return the base 10 logarithm of x.
    # i) S) G- Q* t$ i7 P( K2 v' ?
  4. >>> math.log10(10)
    / f% N% N9 {4 x* L. z! }, b1 K& a
  5. 1.0
    2 f: S) X4 \1 u: y) w5 H
  6. >>> math.log10(100)
    # V! T" l* Y* Y
  7. 2.0
    0 R8 I0 i8 b) B: _
  8. #即10的1.3次方的结果为20
    ! o1 Z, e2 N) @
  9. >>> math.log10(20)/ Z  b8 w% L" C, _; W) ?
  10. 1.3010299956639813
复制代码

6 D+ x* `6 r/ C) ]& ~3 amath.log1p(x)  返回x+1的自然对数(基数为e)的值
  m7 v. m- ^( d& l3 j6 u
  1. #返回x+1的自然对数(基数为e)的值0 Y8 ^; A) y% i* w9 s
  2. log1p(x)8 X- \4 [9 L" Y1 i6 H
  3. Return the natural logarithm of 1+x (base e).
    3 O# A! A3 D( d& Q7 g6 K4 M8 h5 u
  4. The result is computed in a way which is accurate for x near zero.
    & r* l7 @( R6 N! E# h4 d9 M5 g
  5. >>> math.log(10)" d% l: D; V: A( A" P1 R6 J
  6. 2.302585092994046
    0 G/ }4 f! ?; {, ^
  7. >>> math.log1p(10)  v8 s) k$ g6 M& F$ d- {! [
  8. 2.3978952727983707
    & p- W* Z& S( l7 o+ m
  9. >>> math.log(11)
    & C3 M% M6 p! H4 h. }8 s: x
  10. 2.3978952727983707
复制代码
* m9 ^2 e3 b5 h. z
math.log2(x)  返回x的基2对数
3 R1 G4 K: e3 t
  1. #返回x的基2对数6 E/ p8 P  _" V, X* G
  2. log2(x)5 J3 _+ D2 U4 l- @! Z/ }7 n
  3. Return the base 2 logarithm of x.
    & Q. M. L6 ^( ?, I* N" l  P
  4. >>> math.log2(32)
    ) K* p# G' G! ]. U4 }% {
  5. 5.06 @4 \. W& j1 u3 `, C
  6. >>> math.log2(20)" w. B+ B) Z) `/ U4 ^9 q- c, C
  7. 4.321928094887363; R9 D9 W# G! k! Q3 _, z
  8. >>> math.log2(16), B; H+ p+ I8 i; G$ |9 {4 A) N1 c
  9. 4.0
复制代码
/ v, K( H. k7 l) E  @7 L
math.modf(x)  返回由x的小数部分和整数部分组成的元组* Y7 W, q. E1 Z% w; V1 B8 ~' ~7 h. f
  1. #返回由x的小数部分和整数部分组成的元组
    # C  r  e- Z; G0 T% ?- U
  2. modf(x)
    , T5 Z+ P! S/ X/ G7 \& G( r' _
  3. Return the fractional and integer parts of x.  Both results carry the sign
    8 w: L% ^& N; M8 |; K
  4. of x and are floats.
    & b4 q; p1 e% V% H! C$ ?. {
  5. >>> math.modf(math.pi)2 X4 [; ~2 b: H+ v1 i
  6. (0.14159265358979312, 3.0)6 s' X  u4 I9 q
  7. >>> math.modf(12.34)
      [+ s$ ~0 D& s& a$ s2 Y1 Z
  8. (0.33999999999999986, 12.0)
复制代码
' H* P( F* X7 ^( N9 R3 ^
math.sqrt(x)  求x的平方根
. I7 a* n$ Y2 }" W4 h; r' a7 V! t
  1. #求x的平方根1 Q. f& J7 ^0 E$ s) n* V! a  Y( l
  2. sqrt(x)2 w6 B; d  y4 ~5 r- K
  3. Return the square root of x., f9 j* e- L: `
  4. >>> math.sqrt(100)( p) z0 B" `. |/ K3 I0 Z
  5. 10.0* k, U. I6 l' r5 m( d  a
  6. >>> math.sqrt(16)
    ! E; d& k) ^7 `& k; J6 i
  7. 4.0
    - t/ B* {4 o6 h) T
  8. >>> math.sqrt(20), x) M% q. _* ^) P
  9. 4.47213595499958
复制代码

4 L9 {) I4 n) }, M2 h" imath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    9 t9 s  o  z7 @8 L& t6 o5 p7 }
  2. trunc(x:Real) -> Integral: _- m  a; K' x) I
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! P# f5 ^: |% S/ x
  4. >>> math.trunc(6.789)7 U1 u. S8 N$ B5 v
  5. 6: r" k; M' B- _" A1 p2 p6 ~
  6. >>> math.trunc(math.pi)
    0 R0 K( N7 j* }" n( p. ]
  7. 34 @  A% Y! G/ h
  8. >>> math.trunc(2.567)3 L# b3 t8 S6 c( X
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-10 16:04 , Processed in 0.120166 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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