新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
; R! g5 f! T+ r: L& v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; V% {" k* q$ m( e! B3 |# S) ?

  V8 X" G, ?% L6 \, B方法1
7 C/ L4 D) W' g7 m% e
  1. >>> import math" G6 {2 W2 g7 O: W
  2. >>> math.sqrt(9)" [! d9 t/ j% K7 ^# u
  3. 3.0
复制代码
方法2
! Y3 c  ~( @$ b# A' f
  1. >>> from math import sqrt
    7 ^, A# u  K- `9 p5 m
  2. >>> sqrt(9)
    * E6 c1 s8 h1 f
  3. 3.0
复制代码

3 n) D0 O/ G& W( J7 [

6 {9 ^1 u6 y2 s+ M6 F
math.e  表示一个常量
) P; V( \" h% W6 s, a
  1. #表示一个常量
    - ~" e* a9 d/ Q# K$ e1 F
  2. >>> math.e8 K* {' V5 b  j. c( L/ H# [8 Y
  3. 2.718281828459045
复制代码
5 {, p: S( v7 a6 M2 H* m9 a, m
math.pi  
数字常量,圆周率

9 ]5 o3 b  z, b$ F2 J/ G8 W# x' v
  1. #数字常量,圆周率
    , q4 Y+ ]7 C4 ^: |* d
  2. >>> print(math.pi)
    8 g+ |+ |3 X9 }  l& U
  3. 3.141592653589793
复制代码

  M$ d  N; b5 I/ D' e8 Cmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

* \$ o+ W1 e8 P" u
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 s- _4 t, [! h
  2. ceil(x)* I( ?+ D. m( f  ^8 x! h6 h
  3. Return the ceiling of x as an int.
    3 }+ N. }0 M3 \% w. l
  4. This is the smallest integral value >= x.
    9 j: O: G" H' P& A! R

  5. 4 f$ @2 c) {1 I" [% u
  6. >>> math.ceil(4.01)& C" t# }' Y, z% c$ @9 ?/ T+ L/ u
  7. 5
    - M3 @* `" n; \- X3 K+ c
  8. >>> math.ceil(4.99)- [; a  A7 ~1 C7 d, _% v8 G
  9. 5
    ( e# n4 W; l# V) Q
  10. >>> math.ceil(-3.99), |5 V7 g6 `  y( x, b1 Y& l8 S
  11. -35 [3 d% a& N; A: b( L4 \/ J
  12. >>> math.ceil(-3.01)3 X/ `6 ?" K/ O0 Q; E
  13. -3
复制代码
$ _+ r8 Y" q! ^  D( W
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 v' c( a: s, q1 `
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 h  a- U# W" c- i
  2. floor(x)
    0 y/ I, Q: n: d! F# t% b
  3. Return the floor of x as an int.4 C1 I5 Z5 M5 o6 i6 k& u: V) A
  4. This is the largest integral value <= x.
    / a3 G, A) R3 `% }
  5. >>> math.floor(4.1)
    & W8 e1 T6 L; ]& Q6 I
  6. 4, {4 _% N8 z6 O2 M! |# X
  7. >>> math.floor(4.999)
    2 Y: F: c4 }3 `6 G0 [# }6 g
  8. 4+ Y" A4 k7 D: h! K
  9. >>> math.floor(-4.999)# c, u- M' Q6 g. U, A
  10. -56 P: r, d0 Q1 b0 u" H9 o( C
  11. >>> math.floor(-4.01)
    % p2 p! ]" `3 x
  12. -5
复制代码

) k4 X' y( U3 n9 w6 k6 Z: {math.pow(x,y)  返回x的y次方,即x**y
9 \! e$ |5 @- }# n4 l2 E. A
  1. #返回x的y次方,即x**y
    * C8 f# K. J5 T1 B) |
  2. pow(x, y)
    $ `7 }) y+ P/ k! o
  3. Return x**y (x to the power of y).' i2 g0 K7 D- N0 Q  [
  4. >>> math.pow(3,4)
    4 @1 r) \5 f/ y, M5 ?7 ^
  5. 81.0
    % l0 a5 G  R1 `
  6. >>>
    4 ]9 l+ Y, i6 E# \
  7. >>> math.pow(2,7)
    . O- B8 I: l/ F) |8 R% v* s  \
  8. 128.0
复制代码
- V- z  \7 _% d+ Q& ^, {3 ]
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) q7 T) H9 q# u' X. e
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    $ E% s( t$ U( I7 R
  2. log(x[, base])
    - W5 F, v" t" L" }2 G+ ~
  3. Return the logarithm of x to the given base.
    4 x* B+ j) v" J4 h
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ K. p7 |0 X: y* d% l$ _
  5. >>> math.log(10)
    9 c$ n" e+ j! |% ^" U3 l
  6. 2.302585092994046
    / ^- i, P# E; r
  7. >>> math.log(11)' j8 z+ H" x( P
  8. 2.3978952727983707
    5 Q0 {# p; m" O) W% y3 f
  9. >>> math.log(20)
    4 @# Q8 Y3 r' j$ G( D4 j5 d- o
  10. 2.995732273553991
复制代码
! i0 ^8 p) t; }8 k6 X+ o
math.sin(x)  求x(x为弧度)的正弦值
" w9 E7 v7 _2 h7 [% A/ d( a+ ]
  1. #求x(x为弧度)的正弦值! p9 O5 p, Y6 v5 s' `
  2. sin(x)
    6 e0 W, w) \' M' C; w% ~+ c
  3. Return the sine of x (measured in radians).  s: Z' |- b' p# O( P4 s
  4. >>> math.sin(math.pi/4)0 A9 k/ d1 H; Y7 w
  5. 0.7071067811865475
    4 o3 O+ W1 G5 ]* g
  6. >>> math.sin(math.pi/2)2 O' m" L$ O& u
  7. 1.0' r5 h/ J5 v7 x6 R7 K& H; Z4 F" o
  8. >>> math.sin(math.pi/3)' H& v4 V. {6 U/ J/ r, }. R
  9. 0.8660254037844386
复制代码
) V" N. i" W/ `
math.cos(x)  求x的余弦,x必须是弧度
+ B9 _/ a3 g; r6 Z/ L
  1. #求x的余弦,x必须是弧度6 C$ W" l  X% \2 T# C
  2. cos(x)3 V9 s5 X9 [2 p* u
  3. Return the cosine of x (measured in radians).
    ; J, y9 _4 R* T8 h% k. y& e
  4. #math.pi/4表示弧度,转换成角度为45度/ N4 Y7 A/ m! q. I( G+ Y
  5. >>> math.cos(math.pi/4)9 s& E) r+ F2 E; b
  6. 0.70710678118654768 r( y/ }' c0 i  p2 h  \
  7. math.pi/3表示弧度,转换成角度为60度
    ; d9 z5 x4 k/ k- J2 u
  8. >>> math.cos(math.pi/3)+ J6 E- L+ N9 ~7 {3 a
  9. 0.5000000000000001
    8 ^; K% R1 N  ~! q& z' k
  10. math.pi/6表示弧度,转换成角度为30度
    1 h8 w: n8 l# g! i: v
  11. >>> math.cos(math.pi/6). p; t2 |3 _3 o0 q
  12. 0.8660254037844387
复制代码

4 c: t( i! x% M" M- gmath.tan(x)  返回x(x为弧度)的正切值
5 N. `' C( L' v- X% b6 f
  1. #返回x(x为弧度)的正切值4 Q: _; I% i! P$ C, s+ Y4 k- O
  2. tan(x)1 h" X0 R7 \/ Y4 S6 Q
  3. Return the tangent of x (measured in radians).
    9 c' b) ~; d, i+ `& C
  4. >>> math.tan(math.pi/4)8 O" h; ?6 f$ {! {# Q. L
  5. 0.99999999999999999 G  R! \2 S2 b# d: t4 W" r) Q
  6. >>> math.tan(math.pi/6)
    1 f" J) M1 |- `- Q, w3 H3 h% q
  7. 0.5773502691896257  e5 R" x9 ^2 S9 w( n
  8. >>> math.tan(math.pi/3)9 E: }! c( v) o; g6 P% d
  9. 1.7320508075688767
复制代码
/ q- Q! @2 @- V% ~
math.degrees(x)  把x从弧度转换成角度
& g: W# w7 u7 @% T
  1. #把x从弧度转换成角度
    % Y6 n3 C' r0 c  V/ z4 i
  2. degrees(x)- M9 w& s; l7 X2 R, g0 _# l+ K# T
  3. Convert angle x from radians to degrees.
    & o1 C# b9 O) B; J) X- N

  4. % J' x" ~' {( _/ f4 Q4 S
  5. >>> math.degrees(math.pi/4)
    % i. D: c; i4 _4 ~# N: I# H
  6. 45.0
    3 |5 O. {. K% ]
  7. >>> math.degrees(math.pi)' Z, O' P0 v5 N9 \* P
  8. 180.0
    ) H  _4 X2 ^9 G( q1 E9 o, T! x6 \
  9. >>> math.degrees(math.pi/6)
      E) t' n8 V. T$ O# M3 Y
  10. 29.999999999999996) b$ r  M; R' ?+ q
  11. >>> math.degrees(math.pi/3)$ N2 ^7 C3 b( ?1 H$ L* @4 S
  12. 59.99999999999999
复制代码
. e) \7 w% @4 Z. B+ k% ?  T
math.radians(x)  把角度x转换成弧度
5 [* h! W6 z! g. Y
  1. #把角度x转换成弧度* J+ J8 C0 V) L1 v
  2. radians(x)
    & Z$ G7 D; n6 _% r3 Y, n3 p2 {
  3. Convert angle x from degrees to radians.* t4 m- f& ?$ v
  4. >>> math.radians(45)* U6 X" R% G. j. n
  5. 0.7853981633974483) o: e3 C" q; i3 ?; V& A
  6. >>> math.radians(60)
    0 p: {+ V3 H1 X% t) Y& W- g
  7. 1.0471975511965976
复制代码

# u" D4 X4 o, v, x) W* r, {math.copysign(x,y)  把y的正负号加到x前面,可以使用0- t( I+ v( C! ~$ b# y& P( N  I$ P
  1. #把y的正负号加到x前面,可以使用0
    0 b( Z' @: l4 m, k6 E4 R4 |
  2. copysign(x, y)
    9 _+ h: @! @, J
  3. Return a float with the magnitude (absolute value) of x but the sign
    9 o! y) C1 p* C# J$ w. p
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    7 }& d1 U; p+ B( h7 X( E' n
  5. returns -1.0.
    7 t* e' P& J$ e+ I
  6. # u, g- N! U9 Y0 b9 L5 k
  7. >>> math.copysign(2,3)6 U; c2 t, ]' F$ y- ?5 ~
  8. 2.0
    ) Q; K% z- j8 i. n/ `" I$ {4 n  L
  9. >>> math.copysign(2,-3)2 A2 F/ {/ V* w# T& q. {4 V2 R
  10. -2.0
    & Q8 @' ?2 E2 j$ K8 K2 n
  11. >>> math.copysign(3,8)6 x+ i8 [1 @" T! e. G1 D5 @0 ?
  12. 3.0! I  F. R8 @4 |) s( `
  13. >>> math.copysign(3,-8)
    2 ]! w  H- A; q5 f+ j7 b
  14. -3.0
复制代码
; v  |6 \6 Y/ j
math.exp(x)  返回math.e,也就是2.71828的x次方& e1 ?# `6 u- ?8 d8 G4 o
  1. #返回math.e,也就是2.71828的x次方' a$ b6 \& Y8 {/ F9 W, u
  2. exp(x): O! |" [; J  {6 O) Y6 g  a0 Q
  3. Return e raised to the power of x.% m6 F3 v# R' f7 y  T+ h

  4. 9 n- t- h& F& @9 m# b
  5. >>> math.exp(1)! s3 D/ p! R* R* _% Z" n6 Z# A, \
  6. 2.718281828459045
      Z7 J, B& U' @+ V0 H) \9 @
  7. >>> math.exp(2)
    * H. \+ e! I* J5 a- {
  8. 7.38905609893065" e* r- U" {+ P# Z% u
  9. >>> math.exp(3), d1 T' Q0 o7 i9 J9 W7 u
  10. 20.085536923187668
复制代码
3 @! E& Y4 n( }& V- _$ L" x
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1% _/ v# k' T  r' Z6 m  x* Q, V6 I
  1. #返回math.e的x(其值为2.71828)次方的值减1
    : e; P* V  `; X4 ?
  2. expm1(x)
    + m& ^3 Z* P) X5 M
  3. Return exp(x)-1.! R2 E6 c2 d- k+ Q* ]+ }! W7 Q4 D
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ w  J# y; o/ w0 y3 @' a

  5. 3 g. {( P1 u) w
  6. >>> math.expm1(1)
    / p% J6 H# X  h5 J& t& b
  7. 1.7182818284590454 n' g8 p& V2 b& m% i
  8. >>> math.expm1(2); V9 |$ |1 A/ r0 f1 q2 K# o7 p
  9. 6.38905609893065+ I2 t: G8 M$ o& I% o4 Z  q/ F
  10. >>> math.expm1(3)% J9 T0 D$ N5 ^
  11. 19.085536923187668
复制代码

$ s# u3 e  N. qmath.fabs(x)  返回x的绝对值" I4 b! G- a2 Y) F% @4 n) _/ T% }
  1. #返回x的绝对值
    $ d" ?3 A$ b* L7 n: c& v7 d. r$ v
  2. fabs(x)
    $ t8 Y; Y5 ^3 ~' O' z0 a' a+ ?
  3. Return the absolute value of the float x.
    3 ~8 M& o3 [6 d) W1 G6 b/ x; \) ?
  4.   A, h+ N, d+ Z* S: j
  5. >>> math.fabs(-0.003); S6 i& U1 @1 W& g/ F
  6. 0.003
    ; R! a2 D; H2 M
  7. >>> math.fabs(-110)# V: ^: w5 M8 K% M! h7 S
  8. 110.0
    - k! I- ~2 K7 c6 d7 d- F+ ~' F, t' d
  9. >>> math.fabs(100)0 g7 ^* x+ l) @
  10. 100.0
复制代码

1 j0 [" h4 y! y! s4 h; d- d# r  Zmath.factorial(x)  取x的阶乘的值
- |6 f" `0 J4 s! ^! {1 U8 S6 m! w* _
  1. #取x的阶乘的值
    : ]" C$ B9 L+ l  V1 H8 N. X
  2. factorial(x) -> Integral
    # ~' n- D8 l/ @9 o9 I+ S$ g2 w4 v: D8 i
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ( t9 i. `5 Y% O# P
  4. >>> math.factorial(1)
    : _( Q& H" H: X  `" d
  5. 1
    ; C2 O# u! g8 s7 `
  6. >>> math.factorial(2)/ v, k$ T' U; w
  7. 21 M" V/ N6 x. Z0 f0 E1 g& y
  8. >>> math.factorial(3)9 C2 B' s. Q4 e  P5 b; V" T
  9. 6# \7 S3 c7 M0 m
  10. >>> math.factorial(5)
    3 c$ R& r4 T/ F4 ^
  11. 120
    : p, ^! i: v! ]. o& P9 g9 N
  12. >>> math.factorial(10)
    7 ~/ f& n  D; x$ Q8 l; J
  13. 3628800
复制代码
- Z/ i7 t/ j' K7 h5 [: `2 a6 A  s
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
$ `% p% Q- L" z7 a# e3 I' e
  1. #得到x/y的余数,其值是一个浮点数! p2 S6 f8 _  A/ C9 f
  2. fmod(x, y)
    6 y) H8 g% j+ j- m
  3. Return fmod(x, y), according to platform C.  x % y may differ.4 w: T; O7 N% h9 r( `" u# U
  4. >>> math.fmod(20,3)) G' v& G; n9 n4 a6 `9 B2 r
  5. 2.04 {! s7 Z1 @- H: w' l3 `
  6. >>> math.fmod(20,7)
    % n- k1 r& Q3 c2 ], {+ i; h
  7. 6.0
复制代码
* B. I" d% w& o  ?
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 b5 Q/ p0 \* K6 L1 e" Y0 w
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,& D: ?: o/ V8 O7 _; D! |2 c6 \
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    # o5 x1 `$ l2 N1 f" R  D  b4 i* Q6 A9 m
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ; y7 C, M1 @) m
  4. frexp(x)3 a* i( t  Y) Q5 r9 l
  5. Return the mantissa and exponent of x, as pair (m, e).' I) D! j+ c: h
  6. m is a float and e is an int, such that x = m * 2.**e.& C, E% _) _9 T, U
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! l, l( w  y( V& @2 A! a3 {! r6 ]1 X
  8. >>> math.frexp(10)- i9 @) P/ r0 V7 t
  9. (0.625, 4): i3 w5 S; e: ~. u
  10. >>> math.frexp(75)2 ]* W, E+ f! E$ K0 B* o' m) w
  11. (0.5859375, 7)
    1 g9 C, J$ v% ]
  12. >>> math.frexp(-40)* ~" W( \4 b. l. z! S- L  T
  13. (-0.625, 6)
    9 r! r. w7 |& \+ \* @6 V  T) b; P
  14. >>> math.frexp(-100); J1 K6 X1 J' {( L& g& h
  15. (-0.78125, 7), B8 ]6 {$ R, V+ x/ s- G& z/ g
  16. >>> math.frexp(100)
    # X5 p4 d3 p5 W+ I( f" h0 @! L7 @
  17. (0.78125, 7)
复制代码
: w6 ]; y; ]6 U; {3 O' g
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列+ r" e6 k* T$ i" D* L! J6 g/ y
  1. #对迭代器里的每个元素进行求和操作' w# T: |) U1 T" w( ]
  2. fsum(iterable)6 {* ?+ }' R! ]8 d
  3. Return an accurate floating point sum of values in the iterable.
    # X( b% @0 ?8 K% q2 N( A
  4. Assumes IEEE-754 floating point arithmetic.
    . R5 Q+ n( V* b# F# J
  5. >>> math.fsum([1,2,3,4])
    - c, P# m" U9 s2 \
  6. 10.0
    . W" d# u3 d. _6 Q' }# U& k
  7. >>> math.fsum((1,2,3,4))
    * I& j. m8 X. M1 ^+ Z3 p" g: a
  8. 10.0
    , D; ]: c3 c7 y0 l
  9. >>> math.fsum((-1,-2,-3,-4))
    2 D' C, E4 `, y7 Z+ K. U
  10. -10.0( r1 Q; }8 d) @, ]% C$ H" b- l/ p& E
  11. >>> math.fsum([-1,-2,-3,-4])3 n# H8 N) D) h9 I6 H4 a% p" _; d
  12. -10.0
复制代码

. H  l( N* F* U; t" c: Kmath.gcd(x,y)  返回x和y的最大公约数4 {, h* g. W( U2 [  u" C
  1. #返回x和y的最大公约数$ e" _# ?# n$ |" o
  2. gcd(x, y) -> int+ u! l) N" ]" N: g5 W9 s8 v
  3. greatest common divisor of x and y8 S- z4 f9 ?+ u/ R7 w' n5 W+ z
  4. >>> math.gcd(8,6)
      R& _4 Y7 [3 |( \  h4 [; F! V
  5. 2
    % N. c* |, A4 \$ p5 E$ N
  6. >>> math.gcd(40,20)% |+ H9 J0 }- {4 J5 u4 {
  7. 20" ?' b6 e! C8 D, ~9 T
  8. >>> math.gcd(8,12)5 u3 q: O+ j# n( ^! U
  9. 4
复制代码
0 U% P) S( [% ~7 M$ G/ v, \) J7 H
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False! j2 P9 j  g$ K6 L" l
  1. #得到(x**2+y**2),平方的值, y3 G( G% t/ n
  2. hypot(x, y)
    ! z1 N5 F6 b! V% t
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 \7 l2 W* B1 C) {
  4. >>> math.hypot(3,4)
    - @% L& F9 C: b- M
  5. 5.0: l7 |9 F9 W+ f+ p
  6. >>> math.hypot(6,8)
    2 B4 o9 Z. s0 I2 Z; O* ~2 E' [
  7. 10.0
复制代码
, \( ?0 \+ a) a, Z! B9 \5 i
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
  g: F; W! H6 L' A- p: d  o* W
  1. #如果x是不是无穷大的数字,则返回True,否则返回False$ j8 @7 I( B) S: X6 F! A9 L: n7 G
  2. isfinite(x) -> bool
    6 \7 Y7 a2 J) a( }) w+ X$ M
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ' u' ~2 P9 t* h- k
  4. >>> math.isfinite(100)
    7 K# `$ M7 P# _; J
  5. True7 t- T7 k% K5 F) s9 K7 r
  6. >>> math.isfinite(0)
    ! \: r$ [9 @: F
  7. True
    ) _+ B. f8 ^. \3 ?
  8. >>> math.isfinite(0.1)4 _& m- G7 E0 r/ X) s  e
  9. True4 L& ]/ n3 M; L
  10. >>> math.isfinite("a")! D- d- Q. S' g2 ?/ V( z* _
  11. >>> math.isfinite(0.0001)
    1 K* o) h% e- D% Q7 A
  12. True
复制代码

% s$ r5 @! @, B0 O0 Jmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
$ t7 q8 d: C" ], G6 `
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    9 y2 ], M; t) H) G( ?8 k% Z
  2. isinf(x) -> bool
    # J- [5 V; L( y& q& J1 E
  3. Return True if x is a positive or negative infinity, and False otherwise.
    4 i- O. e9 f% D7 c; q; h3 ~
  4. >>> math.isinf(234)
    7 ~' ~0 @' L% s. l" A
  5. False
    " D+ |* M" B1 _' V2 a
  6. >>> math.isinf(0.1)
      G6 c" f/ t( W# ~
  7. False
复制代码
0 W& z3 i2 W& V, F$ F7 y0 k  E; H
math.isnan(x)  如果x不是数字True,否则返回False
! e8 F! {+ |# s" f0 v3 n0 s
  1. #如果x不是数字True,否则返回False
    9 }% c# Y8 }& i/ ?/ Z
  2. isnan(x) -> bool6 M! m' A; p0 O, v, g
  3. Return True if x is a NaN (not a number), and False otherwise.
    2 o( E- c* X5 d) o' h) q. W
  4. >>> math.isnan(23)% K  [/ R/ ^* a" `% @
  5. False
    2 M% V6 _0 \* |% K7 P
  6. >>> math.isnan(0.01)
    # _3 d/ P1 s& ^. A6 T* a+ Z
  7. False
复制代码

' }1 C; k* V' v3 `5 N, xmath.ldexp(x,i)  返回x*(2**i)的值
# C% `  A! q  b6 O5 d( m6 K/ W1 e% E
  1. #返回x*(2**i)的值
    1 l9 e% u9 a4 |. a" i0 U
  2. ldexp(x, i)/ o* W5 W4 K+ t4 Y4 P7 b
  3. Return x * (2**i)." \  j5 l! _$ O& V9 B  c* p: ]+ X
  4. >>> math.ldexp(5,5)
    : U1 T- Q* M+ H* S+ a- e% T" l
  5. 160.0
    " z) |* Y2 n" N$ m. J+ o' W$ o
  6. >>> math.ldexp(3,5)! d3 t' G% i' p; e/ ?% d
  7. 96.0
复制代码
4 s! t! m: r4 R! C5 p8 F, i
math.log10(x)  返回x的以10为底的对数; t5 D: a# d2 m" O& o6 k
  1. #返回x的以10为底的对数
    % w3 z: X; t6 ]" R6 Z- R0 i: a
  2. log10(x)1 O: E! x* s, }1 f# h- P
  3. Return the base 10 logarithm of x.0 w7 w+ \" V# a5 p) [# e& a' [
  4. >>> math.log10(10)
    - d# j- T% x( F4 T/ V( \
  5. 1.0; I) M; ^# e( K1 n6 m
  6. >>> math.log10(100)9 a, h0 [! N8 X2 b' k
  7. 2.0& Y, m# Y. _+ R  G
  8. #即10的1.3次方的结果为200 b5 v) G$ [- N4 V* t; j* Q; o* s
  9. >>> math.log10(20)5 [5 A0 r% N# O$ X
  10. 1.3010299956639813
复制代码
7 C5 K! d* t. L% w; k" h
math.log1p(x)  返回x+1的自然对数(基数为e)的值
; P" j! m# q( z& A" e
  1. #返回x+1的自然对数(基数为e)的值+ h% l/ {* ]* R& R& ~
  2. log1p(x). S* I! a+ O) I0 R( G/ k" d; ^
  3. Return the natural logarithm of 1+x (base e).
    5 U2 F% U% a9 v( Y, u+ Y; V7 p& |
  4. The result is computed in a way which is accurate for x near zero.
    , w! N, r! ^  O( z; Y5 i9 A
  5. >>> math.log(10)
    6 J* C& w' n/ Z! I8 k( `; n3 _
  6. 2.302585092994046
    4 _) ?5 |. g' @+ V2 r! G* M
  7. >>> math.log1p(10)7 I; V/ _; L! R2 Z: d3 j3 u
  8. 2.39789527279837074 E2 d! g) j/ C# c
  9. >>> math.log(11)
    - b1 M$ n6 t+ U! V; s2 m3 O
  10. 2.3978952727983707
复制代码
& o- e9 \4 J& m2 A( J: {
math.log2(x)  返回x的基2对数
8 B. T0 t$ _5 a) g8 `. Z
  1. #返回x的基2对数% q/ o* H; e5 D. E
  2. log2(x)
    4 d& G' `1 {# c0 @( j
  3. Return the base 2 logarithm of x.
    7 u/ X9 ?& E; }$ y: h
  4. >>> math.log2(32)8 S' D) W: o% [! O' }( J! F4 B4 x% |
  5. 5.0) _7 S5 k, k9 i5 Q5 O' T
  6. >>> math.log2(20)
    : P+ p% `0 Z' G/ i9 T- E" [; K6 ?6 n
  7. 4.3219280948873635 T. F3 s, s; j0 s" ]
  8. >>> math.log2(16)
    # [7 ]  P* ?3 `7 ~0 r5 g
  9. 4.0
复制代码

, i3 v3 k9 g7 Y6 e/ {math.modf(x)  返回由x的小数部分和整数部分组成的元组2 ~- b6 @# m" d
  1. #返回由x的小数部分和整数部分组成的元组
    9 Y7 B, V+ R  r1 g% s% [
  2. modf(x)5 K2 r: e  E3 m4 A( z$ Y4 `
  3. Return the fractional and integer parts of x.  Both results carry the sign
    * `% @7 \) y! N' w9 m3 r& d
  4. of x and are floats.
    0 f4 U* g( |6 i. g: [
  5. >>> math.modf(math.pi)+ x) j7 y; z+ ]- z; @2 g4 w5 v$ R
  6. (0.14159265358979312, 3.0)7 G8 ~5 \; a; X5 q8 F4 B, m5 d
  7. >>> math.modf(12.34)
    . ?, u5 I) i/ A  H
  8. (0.33999999999999986, 12.0)
复制代码
& v4 I. v' c) D. n' @; n( r
math.sqrt(x)  求x的平方根. F2 k" p$ v; I
  1. #求x的平方根+ R# @5 M, O/ h: U7 t" f
  2. sqrt(x)
    * g( G& K0 R( c1 C
  3. Return the square root of x.4 T6 Y& |1 ~$ [' B. r
  4. >>> math.sqrt(100)9 @6 i! [3 @9 ~0 ^
  5. 10.01 u' X( {% J' \; l
  6. >>> math.sqrt(16), s0 W' I: `  Y" r: X2 k
  7. 4.0
    ' B/ q) |' K( E& C" o5 O
  8. >>> math.sqrt(20)
    & O! V4 O2 W9 z* u7 [, t' S
  9. 4.47213595499958
复制代码
% U2 I. W& |) U9 d4 X# f5 k
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    & \7 a! W* }9 r& N
  2. trunc(x:Real) -> Integral
    . f3 U/ q" C0 y: [' C+ W& a
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 h8 C3 B" o0 o
  4. >>> math.trunc(6.789)
    6 O) M$ f9 h9 Z" y0 _
  5. 6! F# @8 a" c: t$ D- Y4 Y
  6. >>> math.trunc(math.pi)1 [+ ^7 \) z5 o2 W0 n
  7. 3
    : e$ T$ E4 [" k0 k
  8. >>> math.trunc(2.567)% Y/ v' y1 M) H: @1 z- }4 R* b8 b
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-6 23:51 , Processed in 0.082562 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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