新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

4 t6 s+ H6 B) R8 w  O  s【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。5 n+ c; j5 p- d. X! m7 `. i  j
8 {9 a( x) ]* L
方法1
. m& T2 @+ M1 ~
  1. >>> import math
    - c, g4 \5 U8 B4 t
  2. >>> math.sqrt(9)
    0 Q6 F2 w3 z0 k
  3. 3.0
复制代码
方法2% F& r6 l1 M" u* h" U4 b: R4 r
  1. >>> from math import sqrt
    $ p+ a3 X! y# L2 `  l* X/ u; f
  2. >>> sqrt(9)- `9 f4 w# d( w; |; ~+ {
  3. 3.0
复制代码

, N% g1 Z4 h% }4 s. `3 d

0 H, p) z$ I6 L! s; S7 ~
math.e  表示一个常量
6 v4 n4 V3 b1 Z9 P1 B4 E& R
  1. #表示一个常量. G, ]" Q& L. ~. R2 h+ w+ s
  2. >>> math.e
    1 b. o. q3 l$ Z0 h. S
  3. 2.718281828459045
复制代码
5 [6 i7 b; K8 n6 R8 d
math.pi  
数字常量,圆周率

9 j  Y8 J: w/ g9 a8 S
  1. #数字常量,圆周率5 Z! R6 p6 I5 k& u3 u  a" W2 z" \
  2. >>> print(math.pi)2 t4 H% ?, U3 N' S6 S& O; F
  3. 3.141592653589793
复制代码
4 `9 i, G6 ]" }1 J4 O, e; \0 M$ U
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
, i7 L7 b5 {" o6 P. Q1 K
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 X$ ^( s( f7 ?. F1 }; D) `
  2. ceil(x)
    + w8 @+ _. a! ]; T0 r4 ^0 {
  3. Return the ceiling of x as an int.4 @8 P. P  q$ _6 g
  4. This is the smallest integral value >= x.' {4 g2 B- F1 z4 |* _3 N

  5. + B2 U9 w! T) G, e- v1 p; p# ~
  6. >>> math.ceil(4.01)
    # y5 _. L+ j# W: \# y
  7. 50 v5 o' e1 o& O. t. w$ D
  8. >>> math.ceil(4.99)
    " C+ S8 `# P7 W2 h
  9. 5
    - X, c+ q+ o5 ?' `1 K! _- k4 B5 B
  10. >>> math.ceil(-3.99)
      p0 y% c" b- Z% h5 ~; a
  11. -3; [" W$ g* x0 @- R0 y4 q: V
  12. >>> math.ceil(-3.01)' b; p0 \1 k1 w% S
  13. -3
复制代码
, i0 A4 J* }0 ]  f& I' X
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身. Y+ A) o  b  D% @* R1 X3 H
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    & X. t" o; X; q$ e
  2. floor(x)
    5 X# q6 y3 {5 J. |6 T0 d% f& c
  3. Return the floor of x as an int.
    5 J& g6 q9 {$ M3 O% n/ E' P+ I
  4. This is the largest integral value <= x.
    ( y, a0 _/ {4 j1 ^2 k
  5. >>> math.floor(4.1)$ F! J. G# s& K
  6. 4
    * W4 a+ D2 L( ]* b! K
  7. >>> math.floor(4.999)
    0 _; R4 Y; s" a3 H+ K, k
  8. 4
    " K; z- U1 g# }+ g
  9. >>> math.floor(-4.999)4 J  _8 m* O& I# \: A
  10. -5
      V0 L$ X8 x3 w/ L4 `  q: i' e
  11. >>> math.floor(-4.01)) z4 O. u9 A6 \! Z- Y6 _
  12. -5
复制代码
- I2 w+ U0 P- @0 A% u
math.pow(x,y)  返回x的y次方,即x**y
# \. x8 o9 t: @+ q. ~6 C8 }
  1. #返回x的y次方,即x**y
    . j% Q. o2 M6 ~' D( @/ O+ E& k
  2. pow(x, y)
    % V) V: j+ I( o0 [& H" z# L
  3. Return x**y (x to the power of y).
    % h; H4 {6 x9 }( i$ W
  4. >>> math.pow(3,4); V. b8 W5 L; J* o$ e# V
  5. 81.07 }5 k. E& k4 z6 D
  6. >>>
    7 `. T$ b& G- q. V! E: j
  7. >>> math.pow(2,7), Q* ]2 b0 V0 F
  8. 128.0
复制代码
& G5 ?" u# s, C: f( G
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) l* W1 Y; H# u. d; ?) K0 K/ e
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    & N0 Z8 t- x7 D9 b' X
  2. log(x[, base])
    + {7 t! I* r, Y3 _* O( E1 Z
  3. Return the logarithm of x to the given base.4 a+ A! d( V. ^4 g) Q# s
  4. If the base not specified, returns the natural logarithm (base e) of x.. N3 B+ y; B2 n$ r4 G, X1 Z- v; ~( Z! V
  5. >>> math.log(10)
    # x& Y$ Y$ Y; I
  6. 2.302585092994046' L: C* m4 t9 n& {$ ~
  7. >>> math.log(11), f; J0 x  l9 Y- v6 w
  8. 2.3978952727983707: E( [. t# L3 ?& k
  9. >>> math.log(20)
    6 w. m1 Q+ e6 @6 b- i0 c( a0 x
  10. 2.995732273553991
复制代码

- P- s1 `: r$ E2 }; M+ q) ^' Cmath.sin(x)  求x(x为弧度)的正弦值3 X$ A4 r" W" U$ T" s! l' o. K
  1. #求x(x为弧度)的正弦值
    : X) @# ]) D4 Z# l- E5 X" P/ c/ H
  2. sin(x)
    + K$ d$ ]9 L$ m5 i
  3. Return the sine of x (measured in radians).& q8 R: J. t; K6 X& }
  4. >>> math.sin(math.pi/4)
    & h2 c: _( @) n. \
  5. 0.7071067811865475
    , j+ f  D9 a  e9 d# k
  6. >>> math.sin(math.pi/2)
    + p1 p: N' N8 Q+ D/ z
  7. 1.0  ]6 U4 b) ?# D2 Y( }: u# C8 K
  8. >>> math.sin(math.pi/3)! y. l# K; P, s  d3 I2 R+ ?4 O5 [
  9. 0.8660254037844386
复制代码
4 d! A  m- ?: P3 P: E
math.cos(x)  求x的余弦,x必须是弧度* |) T3 q/ p) @0 w/ F
  1. #求x的余弦,x必须是弧度
    , n7 [' D3 O, M1 O6 m3 L$ C( I6 v
  2. cos(x)& R& c7 }# y( v
  3. Return the cosine of x (measured in radians).
    $ H7 o: b6 ^% N# K' V! t  @$ P$ f
  4. #math.pi/4表示弧度,转换成角度为45度
    4 _  ]' a! V5 s, s" ]1 a" f
  5. >>> math.cos(math.pi/4)
    $ m$ l7 q4 N0 c( q9 ^
  6. 0.7071067811865476
    8 a% E6 j& r9 s; p
  7. math.pi/3表示弧度,转换成角度为60度. `$ b6 p6 a; s" l/ I) I; J) r
  8. >>> math.cos(math.pi/3)1 J& Y; C  Z- y8 t# q1 G
  9. 0.5000000000000001
    $ M7 s+ x1 O! |
  10. math.pi/6表示弧度,转换成角度为30度
    8 z8 U' A, C! C
  11. >>> math.cos(math.pi/6)
    0 G9 E+ K/ ?' [
  12. 0.8660254037844387
复制代码

4 }  Z' @& j* N2 K" ]0 W: V0 hmath.tan(x)  返回x(x为弧度)的正切值
1 `1 C+ \7 D$ Z) d# y5 E
  1. #返回x(x为弧度)的正切值
    - j9 B+ C/ m8 e
  2. tan(x)
    9 P7 R( ~' u% j0 B7 d! u* M
  3. Return the tangent of x (measured in radians)., `2 R& f% ]6 t* T
  4. >>> math.tan(math.pi/4)
    ; l" [3 r/ ^9 A' _" ~: ]# }; O
  5. 0.9999999999999999
    & Z3 X3 _( }" L9 g; w% ]
  6. >>> math.tan(math.pi/6)) ?6 N1 n+ p1 @
  7. 0.5773502691896257
    7 n  b" ]- T4 H5 f+ `" S. d* O9 W; b
  8. >>> math.tan(math.pi/3)
    * \. m8 w8 k) f* f, a
  9. 1.7320508075688767
复制代码
3 i5 P5 |* Q" i9 ^6 t
math.degrees(x)  把x从弧度转换成角度" l% m5 r1 p3 S7 D0 T1 h6 B" d
  1. #把x从弧度转换成角度. X4 i' f0 ~( a& C( H7 z
  2. degrees(x)
    1 O9 X% A: j2 Z! y( s9 \* Y
  3. Convert angle x from radians to degrees., |4 M- n" ]# ^# H2 b2 }
  4. ; y* {" S: R+ z& Q- f( t) w. {$ \
  5. >>> math.degrees(math.pi/4)/ {+ s2 q  v2 n8 G4 `
  6. 45.03 K1 k' m5 _8 H) N( [
  7. >>> math.degrees(math.pi)2 _" O8 c( I. K  E* [/ \. h; q1 m6 Q
  8. 180.0' i  \9 X/ e2 y+ s3 K3 W4 ?
  9. >>> math.degrees(math.pi/6)
    8 @  d, r( a$ L* o5 g6 j- J4 `
  10. 29.999999999999996
    2 w) t2 l0 _5 M, b4 w
  11. >>> math.degrees(math.pi/3)# g9 D  `( n- d
  12. 59.99999999999999
复制代码

' n* G- B" A. {" s+ I6 J1 emath.radians(x)  把角度x转换成弧度
$ h, I; _7 {3 i9 L$ G/ U
  1. #把角度x转换成弧度% f: B- g, X- r! w9 o
  2. radians(x)
    5 l/ D) o. u5 f; S9 R/ X
  3. Convert angle x from degrees to radians.
    5 ]) {3 d0 P. a- w/ X( z
  4. >>> math.radians(45)
    ; M5 t/ V$ o4 v) E" B, ?; Z
  5. 0.78539816339744838 g! w, q8 Z, Z6 R! w8 o
  6. >>> math.radians(60)
    4 M5 e$ O- N2 l  Z/ A
  7. 1.0471975511965976
复制代码
3 W: Y8 Y' i9 D3 L
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
0 G3 E5 ]7 b2 b3 z  \2 H7 I3 m
  1. #把y的正负号加到x前面,可以使用0+ K: H2 f! b! c4 O" `& _9 G% a
  2. copysign(x, y)
    0 R# X" N6 c" L- k" p; ]" |
  3. Return a float with the magnitude (absolute value) of x but the sign 3 w, E4 v/ l1 G" x& i2 N
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    1 q* a1 h1 C" j& w, G2 ?0 C$ D
  5. returns -1.0.
    ! r' f  m: }! q- S7 o9 W" L
  6. & d9 q4 y7 E5 p/ M/ n
  7. >>> math.copysign(2,3): x) G6 \1 [0 G# \
  8. 2.0+ G3 A1 R$ x3 L; G( @
  9. >>> math.copysign(2,-3), C; k; I- q1 _: w; m
  10. -2.0/ F! j( R4 d5 y- N# x5 J
  11. >>> math.copysign(3,8)
    0 E8 ?$ c! Z" b2 m5 V! W% _
  12. 3.0
    3 W/ P3 y5 D: X9 d
  13. >>> math.copysign(3,-8)/ f* u5 L6 U! h1 m' F# \6 b
  14. -3.0
复制代码
/ O& `" E2 @$ f
math.exp(x)  返回math.e,也就是2.71828的x次方$ x" |6 J$ L6 K! [; `; V
  1. #返回math.e,也就是2.71828的x次方; g0 @" a5 {; ?+ N- P$ A! q2 B
  2. exp(x)
    5 v& y+ i! L/ w7 i0 y2 _
  3. Return e raised to the power of x.
    ( L# p. w3 s# a- ^1 I

  4. 7 c. |% U: @- j9 [4 z- S
  5. >>> math.exp(1)7 s& X5 ?! o$ o$ j( }6 s( B' w
  6. 2.718281828459045/ T, {  ~  k: ?" i% B
  7. >>> math.exp(2)
    + w! P% A" K: T! p9 v% e1 |
  8. 7.38905609893065
    2 }1 a" b3 M2 @2 Z4 L/ Z, k8 Q5 p" W
  9. >>> math.exp(3)
    : s# r+ {. g  y
  10. 20.085536923187668
复制代码

/ F" F9 K! e3 a7 {& Emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1; f. ~) ^9 O/ m* R! Z
  1. #返回math.e的x(其值为2.71828)次方的值减1) O# [2 V( B7 S. i: y2 x
  2. expm1(x)& v1 v( w1 V7 S0 A. T7 c3 C3 H
  3. Return exp(x)-1.8 U2 r' e- M$ @# ]% ^. e1 g" S
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. p. K- e# W5 K* U9 {

  5. 8 S2 K5 {7 {5 c
  6. >>> math.expm1(1)
    ; E: t) ?2 R4 t0 ]
  7. 1.718281828459045
    ) [$ P9 f* g' `9 g" x. W. t
  8. >>> math.expm1(2)
    ! {& ^8 L. j5 y8 `4 x) E* L: ~
  9. 6.38905609893065
    9 V' m; I' Z* C
  10. >>> math.expm1(3)
    ' ~, c' t7 m2 L
  11. 19.085536923187668
复制代码
+ p( d7 X8 q. a" q
math.fabs(x)  返回x的绝对值
( @$ f# R0 s5 ?" x" z; ]2 D5 }
  1. #返回x的绝对值6 Z+ x6 R# W, y
  2. fabs(x)7 M: g  ~0 G! z' f
  3. Return the absolute value of the float x.. l  q9 m+ V+ G4 `

  4. . u0 k1 f! W. z3 n
  5. >>> math.fabs(-0.003)
    - O6 c1 w' I2 i& k$ n( S1 `
  6. 0.003) y- q6 u8 {7 y1 U. c
  7. >>> math.fabs(-110)
    2 B8 v0 [! l, H( D$ \2 f& ^. y' Q$ K0 K
  8. 110.0% b/ Q5 W! l9 r/ `# h
  9. >>> math.fabs(100)
    4 r& m6 C6 J* n
  10. 100.0
复制代码
3 J  \4 a, _% `/ Z/ |% ~. V6 L
math.factorial(x)  取x的阶乘的值
; Q; T8 \: j/ `; B
  1. #取x的阶乘的值
    ' a% ~: W5 G2 y% `) J! p
  2. factorial(x) -> Integral) N3 Y2 e! i3 {2 l% b
  3. Find x!. Raise a ValueError if x is negative or non-integral.: q4 _8 t& X1 C0 e0 u
  4. >>> math.factorial(1)
    6 J  p% F9 @5 ]6 q  z! e; c
  5. 1
    + U. s+ n, A3 n8 G: P
  6. >>> math.factorial(2)
    6 }/ {9 i7 H4 `5 n) K- |
  7. 2
    ; U/ q' Q7 u+ H" e$ J7 ~
  8. >>> math.factorial(3)
    ) x9 t- v& k% F' Z* `
  9. 6+ b  w+ a- ]8 \1 b( T( w( D8 r& Z
  10. >>> math.factorial(5)
      X* M2 V& v- B% ]. ~
  11. 120
    / W$ g) e9 {$ }# u2 |
  12. >>> math.factorial(10)
    + u; f1 b8 c* Z+ Z% o2 h$ v
  13. 3628800
复制代码

# Y: S1 {# y, W" M! |( Umath.fmod(x,y)  得到x/y的余数,其值是一个浮点数% b: ?' [4 N# v8 }
  1. #得到x/y的余数,其值是一个浮点数
    , [3 F* B/ D0 P0 o: R: d) n
  2. fmod(x, y)
    # c5 o4 D( f* Q6 ^% c
  3. Return fmod(x, y), according to platform C.  x % y may differ.5 M  F0 e* W: z# J, F& U, [5 D- {
  4. >>> math.fmod(20,3)  K$ b: o( F1 a. U
  5. 2.0( h) ]( f8 E% I0 c8 Z
  6. >>> math.fmod(20,7)+ m, F5 _  ~. G+ E0 e, i
  7. 6.0
复制代码
" Z4 g+ W" E6 u! e
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
0 m$ x; e( n2 x# F* z8 E
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,  U) `7 e- Y- @( {3 T" v
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值* @: ?! W$ u- a  p: d! E1 v! t
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和15 W( {( p3 {2 r& D$ V& F8 Y
  4. frexp(x)
      q6 Q7 x9 i8 s( E& j/ o0 [% W" K: A' [
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 j! j! n2 Q; Y
  6. m is a float and e is an int, such that x = m * 2.**e.# v3 |. ~+ I* y4 g- U) v
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.7 ~$ x3 N, g: [
  8. >>> math.frexp(10)
    0 r% u# U% Y; n0 B
  9. (0.625, 4)
    3 K  l$ X9 i) Y1 o% J2 u8 z9 D
  10. >>> math.frexp(75)
    ( c+ f3 T) B/ A5 U; ^1 J
  11. (0.5859375, 7)6 R: C# Q! ^+ F1 j
  12. >>> math.frexp(-40)1 C: Z. H* Z) e( d( a( @5 L5 c1 w: f
  13. (-0.625, 6)
    ' y7 n/ |% L, f/ t8 O7 U; N. u
  14. >>> math.frexp(-100)0 b; p; r! w- T, D8 C
  15. (-0.78125, 7)! v7 t& p' r' l) V
  16. >>> math.frexp(100)+ c' H4 T5 k2 f& ^
  17. (0.78125, 7)
复制代码
7 j8 h1 }. O" }% A: H: _" T' K' D
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 }/ q8 S* k$ m* n' X( C+ z+ G3 n
  1. #对迭代器里的每个元素进行求和操作% _+ ?# {( \! A7 {; a
  2. fsum(iterable)5 Q$ ]" ?9 z9 k
  3. Return an accurate floating point sum of values in the iterable./ f! X% l" |% z8 Z( M4 ~* B% _
  4. Assumes IEEE-754 floating point arithmetic.3 G  W/ {: o8 K
  5. >>> math.fsum([1,2,3,4])3 O, q0 i% ^' ~! e( l
  6. 10.03 X) _" d8 E3 Y# e) p) E
  7. >>> math.fsum((1,2,3,4))2 n7 y0 T% P# G
  8. 10.0
    1 E) ~6 d# w5 W1 `
  9. >>> math.fsum((-1,-2,-3,-4))
    5 N, |) z5 G" K# B4 s
  10. -10.0# O  D0 S+ u3 j. X
  11. >>> math.fsum([-1,-2,-3,-4])
    + e3 b& B( [3 ~1 K8 c( h% ?( F
  12. -10.0
复制代码

+ R6 [/ B7 g( |8 l. G* Lmath.gcd(x,y)  返回x和y的最大公约数
% V: {! h  ]8 g! I
  1. #返回x和y的最大公约数8 C, ~( v* o0 A5 C
  2. gcd(x, y) -> int, _$ j; ~* q3 m. p. ~" f3 |
  3. greatest common divisor of x and y5 g, ^: i' b/ L) }# o7 E
  4. >>> math.gcd(8,6)
    3 r1 n3 g, X9 [( v" Z0 ?2 y
  5. 2
    6 L2 i: s2 J1 h' K8 [
  6. >>> math.gcd(40,20)9 Y, o- w  x* v& {. S) _- E9 x+ L
  7. 208 _3 i3 e! y9 ~% m% P0 C, B* x, j
  8. >>> math.gcd(8,12)
    1 S3 W7 o+ ?* V
  9. 4
复制代码

6 H9 \* a. G4 I1 T& Tmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
6 X  d2 q) Z# ]9 A& x4 l" U
  1. #得到(x**2+y**2),平方的值' b* [  X4 s& s  ^/ g8 s. Q; D9 T' n
  2. hypot(x, y)) `6 l$ t- @. r
  3. Return the Euclidean distance, sqrt(x*x + y*y).( W# P, p& U9 W, l# s) f+ j7 E
  4. >>> math.hypot(3,4); j5 ]1 {" q2 {, |
  5. 5.0
    9 _# e$ g( R/ G5 Z6 Y  m" J4 V
  6. >>> math.hypot(6,8)
    6 f4 J: w; k" e- Q  N4 F8 _( W
  7. 10.0
复制代码
4 A0 @. ]+ r( D& R/ Q" Q( U  q8 n4 C
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
) E7 B) y& g& C
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    * w9 |' u9 k  @  \: }6 K. T
  2. isfinite(x) -> bool# c1 d6 @" M- {, N" f2 _
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    & M# \2 T; `$ U  `
  4. >>> math.isfinite(100)4 S  v2 D0 c5 d" m7 ]
  5. True4 i6 T, A" s+ p1 {( W
  6. >>> math.isfinite(0)
      B4 j0 R+ i( x0 j! m9 I
  7. True! `# f' X1 k, |! g
  8. >>> math.isfinite(0.1)5 T7 A/ `; z7 a. U" [
  9. True
    4 L+ a+ @6 N7 N
  10. >>> math.isfinite("a")
    2 O/ C3 H/ X7 a6 f: t
  11. >>> math.isfinite(0.0001)
    0 Y: A' C8 w- \: v" X
  12. True
复制代码
' e- J8 _) K( u0 z/ g
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
! W6 m1 W6 U6 l( u+ i: M3 a( j5 c
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False; M9 g, N' ~1 O: i1 H& O
  2. isinf(x) -> bool
    & ]1 P6 p9 J! Q5 }. Y! T
  3. Return True if x is a positive or negative infinity, and False otherwise.+ L" o& V3 Q+ F+ s. t
  4. >>> math.isinf(234)
    # Y7 j0 |: p0 {0 Z1 q8 t
  5. False
      k8 h+ J4 G8 K% ^" K5 @5 N
  6. >>> math.isinf(0.1)" p1 c& f/ [* v$ t
  7. False
复制代码

8 T" Q3 C3 L: ?& Xmath.isnan(x)  如果x不是数字True,否则返回False
3 c2 Y! s; }7 V0 D. s
  1. #如果x不是数字True,否则返回False
    ) l% Z3 ^8 p! B. [. D# M
  2. isnan(x) -> bool
      V3 Q# P( d- \  v
  3. Return True if x is a NaN (not a number), and False otherwise.
    2 t9 W" I5 d# R0 I0 `& Y) Z
  4. >>> math.isnan(23)" N9 U, A3 c0 [" l. r6 w! D
  5. False
      o4 a8 o. {7 }1 A  y6 A9 G$ H. e- {
  6. >>> math.isnan(0.01)
    5 y! }5 ~8 C% H
  7. False
复制代码

7 `' B; _/ R) I0 V5 b0 N5 K' \math.ldexp(x,i)  返回x*(2**i)的值3 L1 N6 K9 I# z/ G7 h$ ]
  1. #返回x*(2**i)的值
    8 @+ Q# k, v8 D3 J4 q4 a, k
  2. ldexp(x, i)5 o! T; T3 b- N* `" p
  3. Return x * (2**i).
    . J) q9 F7 q/ C+ A3 B$ @1 t6 n
  4. >>> math.ldexp(5,5)* g/ p8 v0 w8 l- E
  5. 160.0
    ; W9 J/ `+ W6 y
  6. >>> math.ldexp(3,5)+ }. W' }+ h9 |5 \7 }5 `
  7. 96.0
复制代码
+ C7 H# Z/ \) b$ k, v
math.log10(x)  返回x的以10为底的对数4 h" H5 r6 d. ~6 V/ N
  1. #返回x的以10为底的对数' V" ]" N- {' e+ m4 }# I* b$ A
  2. log10(x)
    1 }( D7 {6 C$ ]! r) X$ w6 n
  3. Return the base 10 logarithm of x." j* m* R  T! C2 q, _
  4. >>> math.log10(10)% A. z; I- |( V, i3 a
  5. 1.03 F' `# l4 K2 c7 T+ s
  6. >>> math.log10(100)
    , }( Y1 N1 g0 n* c& y/ P4 _
  7. 2.0
    ) W/ r$ s% ^4 a9 @4 D
  8. #即10的1.3次方的结果为20
    4 h& W- q4 X/ J4 N
  9. >>> math.log10(20)
    , p2 k: |6 D) r0 B$ I1 v" S; G2 j
  10. 1.3010299956639813
复制代码

  M6 J5 T3 v3 l+ ^math.log1p(x)  返回x+1的自然对数(基数为e)的值- p- t( @; ?+ q/ {
  1. #返回x+1的自然对数(基数为e)的值
    7 j7 R# S3 L0 B! I
  2. log1p(x)
    1 q- s) c# B0 f5 X
  3. Return the natural logarithm of 1+x (base e).6 j+ P+ W6 H' y# O- |! k
  4. The result is computed in a way which is accurate for x near zero.4 f/ S# w( q. ~
  5. >>> math.log(10)" L" W4 |$ \9 F( d' G  L
  6. 2.302585092994046
    & g' B. H% \9 `9 k+ y0 ]3 z
  7. >>> math.log1p(10)
    ; `+ B2 n4 t$ V+ W0 u+ H
  8. 2.3978952727983707
    & o1 l4 x$ n. B9 U
  9. >>> math.log(11)
    3 t8 u6 G1 A* J/ K
  10. 2.3978952727983707
复制代码
8 o8 `4 R: `; o: f$ J( |* V
math.log2(x)  返回x的基2对数
' H# d* e# C. R
  1. #返回x的基2对数
    + ^: f; H4 X6 [1 }& v# G" o
  2. log2(x)
    5 G. g9 b$ L; i
  3. Return the base 2 logarithm of x.8 Y, V$ X, K8 F' M# Q
  4. >>> math.log2(32)& [: O# g) G, s5 ]; P
  5. 5.0  y7 y; |/ B* o0 q4 \) b) d
  6. >>> math.log2(20)" u; w8 D7 k7 \  }5 i; n) K
  7. 4.321928094887363% }/ c# C& y2 w" I
  8. >>> math.log2(16)
    + z7 t1 t" o7 N7 W8 y$ x/ t3 y
  9. 4.0
复制代码
. e3 d3 }8 U& v! a
math.modf(x)  返回由x的小数部分和整数部分组成的元组
% X, Z! Y% e  ^9 w
  1. #返回由x的小数部分和整数部分组成的元组
    5 x; b& \; a" Q: }- n
  2. modf(x)! I4 n! u; V- E' l
  3. Return the fractional and integer parts of x.  Both results carry the sign& j- j) u5 ?; f, n/ |- H- n
  4. of x and are floats.
    ; F" ^: |; }/ y/ A
  5. >>> math.modf(math.pi): l( C1 U% j( u% r5 G. g7 S
  6. (0.14159265358979312, 3.0)& b, L5 \# ]. \
  7. >>> math.modf(12.34)
    + s, R5 G" V/ k9 M
  8. (0.33999999999999986, 12.0)
复制代码

2 w  H( F2 s8 A9 Qmath.sqrt(x)  求x的平方根
, C2 x& o: I6 C, q6 U& Z' G5 {
  1. #求x的平方根7 V5 y# Z1 i* W: h8 c: ^
  2. sqrt(x)8 K3 u6 d1 }8 O* y* T/ Y' S  Y
  3. Return the square root of x.5 j# I5 A8 m8 f
  4. >>> math.sqrt(100); t0 B1 }: u! K
  5. 10.0
      A7 L% y1 K2 A  j, Y
  6. >>> math.sqrt(16)  C9 }  q4 ]1 Y0 ?8 ~
  7. 4.0. Q6 J6 e" X& U; P1 \4 }, K
  8. >>> math.sqrt(20)+ r. T, P9 g# e% G1 ^! d+ X8 ^
  9. 4.47213595499958
复制代码
  j& L$ f( z. n7 a0 O- j! w, U/ n
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分( m3 h- x# L1 y
  2. trunc(x:Real) -> Integral0 e1 k+ Y; y7 U% \" m- [* u+ }
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.  y- I% g) ]& O+ i/ p  B
  4. >>> math.trunc(6.789)
    , w6 H4 V5 x4 f: O5 I) a# h/ Z1 q) V
  5. 6
    9 B( k& r# O8 ^6 a! c5 r7 z
  6. >>> math.trunc(math.pi)
    2 N9 h* ?4 Z6 N' w; `: u
  7. 33 Z  i: _2 M2 r! v, c. ^
  8. >>> math.trunc(2.567)
      }5 Q- ?5 ]" Y9 k- _3 ^: l( g1 U' I
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-9 14:46 , Processed in 0.104525 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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