新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

! f9 B  B; z. Z$ T, R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' q; g% E+ X3 l8 P" O- k2 W9 b1 {( N

  K! C+ [8 q) z( f方法1
: [7 Q; Y1 G2 O/ M9 A
  1. >>> import math
    6 ~0 C0 u3 _) c- a' S  }
  2. >>> math.sqrt(9)
    " \0 N3 \$ R2 b- i) B6 c: ?
  3. 3.0
复制代码
方法2  E6 s- ]4 _6 O3 `
  1. >>> from math import sqrt; z. D4 p; |5 I# `$ V* D& @1 t
  2. >>> sqrt(9)
    * V, x' B* u$ X! a: W" r
  3. 3.0
复制代码

/ z8 X: C- Q/ P$ v
. |6 H# B( f7 J0 g% l2 E
math.e  表示一个常量
) I) \5 B- N8 z/ J4 Q3 z( E
  1. #表示一个常量
    . W+ c# H5 Y5 c/ \" R  |0 a! F" c# m
  2. >>> math.e
    - d) W; U5 o) z' c
  3. 2.718281828459045
复制代码
5 I% t. r* {3 a( z) t$ B
math.pi  
数字常量,圆周率

* Y& O7 y* d  ^( |+ |* }
  1. #数字常量,圆周率( L( p8 l: Y) Q+ s% [( l; f
  2. >>> print(math.pi)
    6 X& U- ~' k  K8 Y8 z  N
  3. 3.141592653589793
复制代码
, s9 [7 r% b8 B
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

9 z% J% ~  b/ k, z6 b/ m6 V
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x% F. Q3 T9 q. A  }# j& T
  2. ceil(x). @9 M  b5 `8 M. e- p  G: p; H
  3. Return the ceiling of x as an int.6 l8 G% C+ {" q( W7 j" |
  4. This is the smallest integral value >= x.
    $ c' N& ~: `/ O9 T# H0 \

  5. , O  U. @4 ~; D# ]6 @
  6. >>> math.ceil(4.01)& }* G6 A1 s( z3 H6 Z* b
  7. 5
    & D0 Z6 |4 z+ Q+ K, \$ f: F
  8. >>> math.ceil(4.99)
    4 k( L$ H# _$ n9 @5 D/ [8 W
  9. 5
    & z/ {' ^' z  m  \$ a1 o
  10. >>> math.ceil(-3.99)
    ) D* S" q4 C9 x* K* T
  11. -3
    ' V* x  Z% q7 o* \% u4 L
  12. >>> math.ceil(-3.01)4 g# A# p3 S5 l4 C: d
  13. -3
复制代码

, m# x3 s& _  N5 G! f, emath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 P( H7 f5 P9 h" b1 N  T4 I/ s' r" @
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 P% [: E3 q& }$ [2 E- i
  2. floor(x)' d6 b" X, A2 t
  3. Return the floor of x as an int.
    ! y3 R0 b- _! M; Z2 _; B& S$ E8 y
  4. This is the largest integral value <= x.
    / R/ ]8 ~* M* D6 Q1 d5 y
  5. >>> math.floor(4.1)
    0 O7 b1 |% G4 ?$ T+ P( E
  6. 4
    8 D$ ^& l! k# ^, `; ^1 ]
  7. >>> math.floor(4.999)
    2 V- h2 [8 r2 l. o* T4 f9 @
  8. 46 l" `% L2 C2 M
  9. >>> math.floor(-4.999)8 `/ X8 h) ?) b  `: ?; T; r5 m
  10. -5
    6 Q+ |1 D7 X, w; X3 l3 Z! B
  11. >>> math.floor(-4.01)
    7 {9 b4 V+ s3 F/ V* g4 |
  12. -5
复制代码
" [" D. }3 P! y8 X2 D7 |3 _( n. n
math.pow(x,y)  返回x的y次方,即x**y
( [6 Q! v% U9 O+ l. a& K* u
  1. #返回x的y次方,即x**y
    ) [0 T( D; E4 m; C% M1 C
  2. pow(x, y)
    9 n; Z0 U# [( u  @6 }% E( [
  3. Return x**y (x to the power of y).& \0 |. j' \% f( q7 J" V
  4. >>> math.pow(3,4)
    2 T+ x  U. b5 s1 S; D) K  P
  5. 81.0
    2 H% M5 u! u2 s. x0 W9 \
  6. >>> ( S9 j% Y: a8 h3 e
  7. >>> math.pow(2,7)
    , M4 c' {7 ?* G! \/ f5 [/ i
  8. 128.0
复制代码

+ _% O4 [3 ?/ H9 K- P$ tmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# F1 L4 N& `' P7 L8 N
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 G! K* t' k8 C5 s
  2. log(x[, base])
    5 r; j. B7 ~; \, B
  3. Return the logarithm of x to the given base.
    6 ?, |$ M4 a' v( K" I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    2 a4 ?7 D0 a' q& U$ ?3 o
  5. >>> math.log(10)5 w2 i" X* G- X) v5 L
  6. 2.302585092994046/ y* W8 x0 `& {+ ~
  7. >>> math.log(11)1 d$ C+ @- ^) P, K
  8. 2.3978952727983707
    7 T! F/ z5 N0 q* K- A+ T
  9. >>> math.log(20)
    / H, r( R; |: M  J& @% Q
  10. 2.995732273553991
复制代码

5 E- P% C. [' p1 l$ w1 T; umath.sin(x)  求x(x为弧度)的正弦值. t1 K, c$ |+ T2 j0 @% r* H/ q! Y0 D
  1. #求x(x为弧度)的正弦值
    $ C" ~$ y7 V" G1 n/ M5 o5 s8 H
  2. sin(x)0 B/ A5 e+ k0 P1 ?! ^
  3. Return the sine of x (measured in radians).
    ; J9 B$ G; J) [0 f9 y; \, G
  4. >>> math.sin(math.pi/4)
    + B, o, n* P, R/ Q
  5. 0.7071067811865475# W2 k/ k; Q/ V2 O' G: m
  6. >>> math.sin(math.pi/2)
    , B9 C0 L+ N0 d
  7. 1.0+ b5 O4 a4 B. J3 \2 u; O
  8. >>> math.sin(math.pi/3)
    ; z4 g: x0 V: u! p4 n
  9. 0.8660254037844386
复制代码

/ \2 u$ u# d) S/ u& Fmath.cos(x)  求x的余弦,x必须是弧度% J- k3 F: }. @& y$ ?  J
  1. #求x的余弦,x必须是弧度
    0 ?4 y' i' l' }" k
  2. cos(x)0 w4 t  A' c2 r% Z/ D  y
  3. Return the cosine of x (measured in radians).
    , s3 ~0 y) M0 R& H5 v8 o  @5 h
  4. #math.pi/4表示弧度,转换成角度为45度# \* R8 l4 g3 t( c7 `9 W
  5. >>> math.cos(math.pi/4)
    : j! O" ?7 @7 S' N' `
  6. 0.7071067811865476
    . w2 I) [) J% Y" X( q3 B
  7. math.pi/3表示弧度,转换成角度为60度. L4 V) P. _+ X3 M# y
  8. >>> math.cos(math.pi/3), q8 w9 s* r: \' ~
  9. 0.5000000000000001
    - n; P& d" k1 @: y& c
  10. math.pi/6表示弧度,转换成角度为30度2 h( a$ X/ C+ v& [- A; |# A
  11. >>> math.cos(math.pi/6)
    , X8 `" Z1 n: n& W0 a, p
  12. 0.8660254037844387
复制代码

4 O+ ^0 r+ f9 L2 jmath.tan(x)  返回x(x为弧度)的正切值3 ~# K$ t2 U- D9 o) B
  1. #返回x(x为弧度)的正切值1 P* k0 V  q+ o
  2. tan(x)1 I+ B1 P1 V1 m; _
  3. Return the tangent of x (measured in radians).
    ( g% H7 G5 F% b* r: G& {
  4. >>> math.tan(math.pi/4)' F* L4 Y% C5 Q: f/ V
  5. 0.9999999999999999! v& A; X: c" L9 j! V8 g5 {/ c) x) o" j
  6. >>> math.tan(math.pi/6)$ x/ i) U4 l/ Y2 i* d
  7. 0.5773502691896257( P# o/ W* l4 r
  8. >>> math.tan(math.pi/3)
    - }8 ^! S2 C. G# f0 W) W4 d, _0 v4 b
  9. 1.7320508075688767
复制代码

( k! J/ K* ~3 c# g2 Amath.degrees(x)  把x从弧度转换成角度) ]* v2 J/ h3 W: {6 e
  1. #把x从弧度转换成角度
    $ |& |: `9 w) ~" E$ e- r$ ?' F
  2. degrees(x)* P  j) v3 e3 z$ s( e
  3. Convert angle x from radians to degrees.
    % a9 ?0 \/ g. a& X; I0 u  Q  A
  4. . t" ~4 q  F6 Z3 \
  5. >>> math.degrees(math.pi/4)4 b/ w: v& W" E7 h& a( ]8 O# {: u3 E% l
  6. 45.0
    ! x8 `: ]5 E4 c5 d, v
  7. >>> math.degrees(math.pi)& t( o8 p$ S& Z) _% a  p5 _+ ~
  8. 180.0
    ) ]/ b# R5 \, G, ~
  9. >>> math.degrees(math.pi/6)
    7 ]. C9 s9 q9 A; w) k" n& S
  10. 29.999999999999996
    3 k7 Q6 @, |: \; \8 D1 |0 d
  11. >>> math.degrees(math.pi/3)
    1 ~& z) x, {3 f2 u" X  m
  12. 59.99999999999999
复制代码
+ g, b/ `8 Q& s' o( D/ _# P8 U
math.radians(x)  把角度x转换成弧度
' L: g" a- z# u3 p* @/ e3 x
  1. #把角度x转换成弧度1 [4 A7 ]% `- _6 R
  2. radians(x)
    : i& G7 P8 K5 P& D, y) ]1 e# H  w
  3. Convert angle x from degrees to radians.
    / N/ \: t9 j; y
  4. >>> math.radians(45)7 D. k- o+ t8 _
  5. 0.78539816339744839 q8 M" t3 A" {; k# W" n
  6. >>> math.radians(60)1 K, f6 L+ Y5 C8 I- \. ?
  7. 1.0471975511965976
复制代码

  y' k5 r8 K& J, Pmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
9 S6 R) |) m" [! Q* `, I) R, U
  1. #把y的正负号加到x前面,可以使用08 h! k" F% H6 N( H( g
  2. copysign(x, y)
    " O+ a+ d* |. h+ t. L( E9 O7 p
  3. Return a float with the magnitude (absolute value) of x but the sign
    0 ]) f# R% [* ^2 _
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    % A" \) o. b" f+ O3 m+ j" I0 A
  5. returns -1.0.
    + N8 X/ L' x6 F: g
  6. / H/ H. Y2 S- E, Z6 x5 n' t
  7. >>> math.copysign(2,3)
    7 M  A* K' D4 m& e
  8. 2.0. W; W; `% R3 Q( N
  9. >>> math.copysign(2,-3)& ^+ i2 M- S8 _5 B; Y3 z8 X* t
  10. -2.0
    ) H- A7 J2 g( \0 t5 D% J* E
  11. >>> math.copysign(3,8)
    & R! X) ]) W0 y2 L( j
  12. 3.0
    3 o4 }" h5 D# e* F
  13. >>> math.copysign(3,-8)
    , c4 X& C1 D# ?" m' |3 b
  14. -3.0
复制代码

; D3 o8 b" o  N# B# l6 P8 \7 S6 ^math.exp(x)  返回math.e,也就是2.71828的x次方0 X9 F/ H8 A9 Q" _
  1. #返回math.e,也就是2.71828的x次方- O" V7 \3 N7 w' G9 z
  2. exp(x)
    9 Y- \2 o  s# S+ D8 V" T
  3. Return e raised to the power of x.
    1 D& D6 Y# T) ~5 T) u* C$ x5 Q
  4. ; z: G! E# @( v. K: }
  5. >>> math.exp(1)% \9 {+ U) U$ L- O! o! D
  6. 2.718281828459045  e6 i8 X& V# H
  7. >>> math.exp(2)
    9 G& D/ R+ t$ B% E; ?  Y
  8. 7.38905609893065
    - `3 j9 [" t# C, t2 c2 h
  9. >>> math.exp(3)
    2 t, H! \  O+ }) \1 {. h& M0 ~
  10. 20.085536923187668
复制代码

/ E" S3 i+ W+ W5 ~  `math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
. X) J4 r- h) ^; M; L# I. P# }
  1. #返回math.e的x(其值为2.71828)次方的值减1. A. f8 b4 y1 q/ Z: i
  2. expm1(x)5 Z% c- }4 G; q5 E
  3. Return exp(x)-1.
    0 G* _5 ]9 s' ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 G1 j, \0 r. \/ C" B
  5. ! [; L% }- |& M
  6. >>> math.expm1(1)
    6 y% I/ e) O* {4 [# ?, Z* ^
  7. 1.718281828459045
    + W, u! ^  Y$ f9 @
  8. >>> math.expm1(2)0 _( Y/ |. ?  U8 L
  9. 6.38905609893065
    ' y; Z/ [6 ~/ R& e- A
  10. >>> math.expm1(3)! D1 a0 B" {( E; B0 K4 u
  11. 19.085536923187668
复制代码

: s8 ?$ r4 ?: D, X! j1 }: Imath.fabs(x)  返回x的绝对值
9 T( |, l" B( d: j3 s6 w
  1. #返回x的绝对值  a0 T' u1 `( f5 b5 {
  2. fabs(x)& @7 S# L" b4 E! G8 v' T
  3. Return the absolute value of the float x.
    3 b$ [3 H7 U+ x3 P
  4. , l/ O% _* P& v+ J  Z# K
  5. >>> math.fabs(-0.003)2 a% t7 U* ?3 l( r# Q2 A
  6. 0.0037 k# }, q' X+ o
  7. >>> math.fabs(-110)+ {5 }2 S: U- L- @* z) h! C3 X4 Q
  8. 110.0
    4 U% E2 _1 [1 L1 G, d1 {
  9. >>> math.fabs(100)# @3 Q8 t. x/ P  h8 X( ?
  10. 100.0
复制代码

, c$ a% a( c8 x% D# L" {2 ^math.factorial(x)  取x的阶乘的值# F& U  X( F' Q# s# A. ^
  1. #取x的阶乘的值# D1 j2 Z8 G% z# @
  2. factorial(x) -> Integral' {( P& X" ^3 h/ ]
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    7 G+ j5 f. A2 M( M; M1 W5 y3 J. O
  4. >>> math.factorial(1)& d! W/ `6 y, r8 U: W* J% ]$ ^
  5. 1. t1 a1 D4 O! V! U6 y$ w
  6. >>> math.factorial(2)
    + |3 L% @7 i1 f
  7. 2
    ' B3 a' I# P% {8 z+ ^
  8. >>> math.factorial(3)
    " I  a! D& y( K5 ?  a
  9. 6
    + v  Z# N8 ^& @' l0 k) Y: K
  10. >>> math.factorial(5)
    1 i+ T7 N( B& ?7 l/ \
  11. 120
    ! W4 v' o. M. B( R
  12. >>> math.factorial(10)/ Y/ Y, X7 V' M& _. H: l
  13. 3628800
复制代码

# d# |/ ~( y3 m$ rmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
2 a0 W8 t/ ]4 K2 U& L
  1. #得到x/y的余数,其值是一个浮点数
    - @% g$ y6 C8 m4 j9 _) x7 Z% O
  2. fmod(x, y)
    * k6 j5 p) m/ r5 }; b% y3 V) j
  3. Return fmod(x, y), according to platform C.  x % y may differ.% X9 h1 a$ t7 ~$ d
  4. >>> math.fmod(20,3)
    + d* v6 J) W4 j
  5. 2.0
    4 d  q% j# t, g9 [- x
  6. >>> math.fmod(20,7)
    9 {3 x5 t  B5 x
  7. 6.0
复制代码
+ b  D: ^8 u' X1 E- g* Z8 D4 k- x
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围# j- ]6 V3 g; g' A' R
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    & @9 r5 P" W1 S/ d% v7 q; V
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    $ U+ c# |& K# r) i/ B4 }  ]2 U( K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    , B3 I- m  _5 l7 g1 F5 K; q
  4. frexp(x)
    ; d: t9 B/ e4 R# ?: v
  5. Return the mantissa and exponent of x, as pair (m, e).
    * N) J/ G2 \$ |- C9 V" p
  6. m is a float and e is an int, such that x = m * 2.**e.
    3 S% N8 S6 v) P6 ~. j
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.# V# [: Q4 D! \5 d
  8. >>> math.frexp(10)
    + w, z- ^, ]! Q2 W
  9. (0.625, 4)
    : I3 K7 W% h: w, n- ?$ q! X- i
  10. >>> math.frexp(75)& d  C8 e2 N: ]6 W
  11. (0.5859375, 7)
    - D, Z$ z9 O# C% N) t: A! F
  12. >>> math.frexp(-40)! M% G; R5 q  P4 f1 M$ q- Y. \( ^4 n
  13. (-0.625, 6)' `% R4 K% X7 K( R$ L+ s) [
  14. >>> math.frexp(-100)- r. @( [4 X! s' ?5 \
  15. (-0.78125, 7)
    / a, U! ^( d8 h+ ~* {5 J, j/ B
  16. >>> math.frexp(100)- z; X9 ~$ E4 g
  17. (0.78125, 7)
复制代码
" g  Z  g. G3 Q
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
" n% H0 X8 U  V( u& Z
  1. #对迭代器里的每个元素进行求和操作
    : l! U0 I! C. l+ `0 j9 h; F
  2. fsum(iterable)
    8 F) _+ }1 c. X- I5 S/ q
  3. Return an accurate floating point sum of values in the iterable.7 u, V4 K0 q7 a* F4 a/ \; g6 v
  4. Assumes IEEE-754 floating point arithmetic.
    $ J3 e* p0 W$ v  B  o  X- b
  5. >>> math.fsum([1,2,3,4])
    ' a8 K0 \) v/ E* K: Q( k
  6. 10.0+ n3 |8 z, M0 b! {# I# ?
  7. >>> math.fsum((1,2,3,4))
    9 K  D. m& f9 q% D; i/ m0 K
  8. 10.0  H7 H9 W0 N4 ]/ R  @0 X
  9. >>> math.fsum((-1,-2,-3,-4))
    8 q4 C$ p( u9 w
  10. -10.0. S7 G$ Y; m  U; f+ p2 B
  11. >>> math.fsum([-1,-2,-3,-4]); F2 S" z  _0 |6 ~
  12. -10.0
复制代码

) h0 w4 d- ]0 K# |" E1 D" }math.gcd(x,y)  返回x和y的最大公约数2 L. A9 |3 M* x' I
  1. #返回x和y的最大公约数
    1 r$ ?( ]9 X/ b8 g( q, y
  2. gcd(x, y) -> int
    / d; _9 T7 X8 b
  3. greatest common divisor of x and y
    ) U$ d* V, Z$ Y/ v5 q. S9 e- F& u! m6 r" H
  4. >>> math.gcd(8,6)
    6 f6 t  f4 m, c* q, P) W
  5. 2
    9 o* f1 V0 ^) n% N9 R2 |" m+ B* i8 Z
  6. >>> math.gcd(40,20)
    5 Q: o# z0 P& B0 G8 o  ~
  7. 20
    , W" K! {2 J: A8 n
  8. >>> math.gcd(8,12)
    : X5 K# g. E7 t$ w) |, t! n
  9. 4
复制代码
1 F3 Y% M. |) {" k' ~1 v/ `5 M* G
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
: H9 A4 P( a- e( g6 y
  1. #得到(x**2+y**2),平方的值
    : |! V) t. L' @; \$ e
  2. hypot(x, y)- _  O- B. n( x3 @
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    3 H) R% s& f; s* A" H. ]
  4. >>> math.hypot(3,4). B; g. J( i; K% G8 R
  5. 5.0/ l7 m' g" E: a
  6. >>> math.hypot(6,8)
    * D6 y1 U. T0 j% N' g& D
  7. 10.0
复制代码
: j, U3 F/ w2 E1 [
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
% c0 q- o2 j( U! a5 q* G( M+ P
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ! [& g5 H. i- E$ C9 I- E
  2. isfinite(x) -> bool
    ) k+ U' ^; u1 k' m
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
      D$ O3 |# h* t) o
  4. >>> math.isfinite(100)3 f( N3 Q+ q" D3 i% g# O6 C$ q
  5. True
    8 P" h7 r& r! c" R0 J
  6. >>> math.isfinite(0)
    6 f4 j2 T4 e3 |
  7. True
    6 R; m3 [2 Z0 R3 l8 K& I3 U
  8. >>> math.isfinite(0.1)4 J# b4 O1 Q/ f2 y. }$ @2 k* x
  9. True
    6 T% c  l0 X! j/ d: q
  10. >>> math.isfinite("a")
    , e. L2 _2 ]* h! o
  11. >>> math.isfinite(0.0001)
    5 r) \! J; X4 m' r
  12. True
复制代码
3 D" b2 w; q0 w
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False  M/ y4 b2 r9 z& V; D7 `1 T9 C
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    4 o9 Y1 G6 o9 d  {6 U
  2. isinf(x) -> bool
    ( q0 S: |: x! D# m( N
  3. Return True if x is a positive or negative infinity, and False otherwise.: o6 Z/ m0 b: h5 v
  4. >>> math.isinf(234)7 m; w9 p3 Y/ A
  5. False# w, Z) O7 O2 c6 M7 d1 ^: F
  6. >>> math.isinf(0.1)4 ^: C' N2 u! M+ P* K5 ?
  7. False
复制代码
( Q7 ~3 }7 K8 O$ L6 v/ _% o
math.isnan(x)  如果x不是数字True,否则返回False
% x) i! c! ]3 \3 y" l5 l7 |
  1. #如果x不是数字True,否则返回False! M9 Z/ A+ M- G; ~* K
  2. isnan(x) -> bool# N5 Q5 ~6 K  [# Y
  3. Return True if x is a NaN (not a number), and False otherwise.% M. Q2 [: V7 X; V  D  O- A& `0 Y, }
  4. >>> math.isnan(23)
    * b- W- ]+ E5 U: b1 D9 [# ]3 l
  5. False
    7 M4 S) O1 {  ?/ K/ K5 b
  6. >>> math.isnan(0.01)6 {" E5 i  H3 [
  7. False
复制代码
6 A3 m6 k) ?. n( p6 ^7 ~
math.ldexp(x,i)  返回x*(2**i)的值
* X. Z8 `: b% }! J! ^3 R
  1. #返回x*(2**i)的值
    9 _% t6 r; w5 n; B
  2. ldexp(x, i)$ o( l0 q% s3 O3 ^3 k; Q
  3. Return x * (2**i).' v9 k; |1 ^. a. B" ~, [
  4. >>> math.ldexp(5,5). P. h2 d4 j( l3 o' k
  5. 160.0& k1 B/ b4 i9 v
  6. >>> math.ldexp(3,5)) G8 B! K0 [  E5 m+ s
  7. 96.0
复制代码

( Q# p+ U1 u8 J2 Imath.log10(x)  返回x的以10为底的对数7 ~3 v; c1 ~7 @9 J- F
  1. #返回x的以10为底的对数) q! U5 d+ P  u' Q. m6 v
  2. log10(x)
    + m& Z2 k' S$ z4 H
  3. Return the base 10 logarithm of x.
    7 \) C+ L: n% c0 L. R# \8 N% n
  4. >>> math.log10(10)
      _: C; b- C# a6 S8 M' x2 ?
  5. 1.0
    9 F% t/ b0 z* b, B" s, l
  6. >>> math.log10(100)6 Y% o' K5 z1 u2 X/ J
  7. 2.0
    : I( s8 u  t- e6 @9 _. f
  8. #即10的1.3次方的结果为20* t/ p  N/ T  o3 K! K
  9. >>> math.log10(20)
    ) O/ g! g* Q, P( W' U; F# K( x3 v5 u
  10. 1.3010299956639813
复制代码

, a  x  U. H( [) c2 R, d4 g- i6 \* bmath.log1p(x)  返回x+1的自然对数(基数为e)的值
; o$ G$ i1 u  [0 z9 o  ~
  1. #返回x+1的自然对数(基数为e)的值" M/ J7 P5 o8 b9 B0 ~! {
  2. log1p(x)% K/ H+ E) W0 s8 s* Z1 |6 q( Q, `
  3. Return the natural logarithm of 1+x (base e).* B* |9 f! V) v% w# V, b( s6 U% l
  4. The result is computed in a way which is accurate for x near zero.# t6 l. u# `! w
  5. >>> math.log(10)1 T6 }' I* S! w) q
  6. 2.3025850929940467 }7 d+ y5 N8 X5 O
  7. >>> math.log1p(10)
    0 s9 U/ F. h2 [; l
  8. 2.3978952727983707
    ! M) x7 _) Y1 D( u
  9. >>> math.log(11)
    / W8 w, X1 R4 {. v2 @
  10. 2.3978952727983707
复制代码

- K6 v% S' Q) @! Q$ amath.log2(x)  返回x的基2对数
' E. m: `5 [* @
  1. #返回x的基2对数2 a& u/ Z/ Q1 T
  2. log2(x)$ H! N/ D4 F" Q# d" E! k2 Q# o1 L
  3. Return the base 2 logarithm of x.
    3 f' p8 p8 i4 O% E. i1 s
  4. >>> math.log2(32)
    4 X, }2 C/ a9 g/ s3 x9 u+ q
  5. 5.06 D4 @0 O/ ^5 e+ @0 z9 B# l9 z
  6. >>> math.log2(20)
    # h( o# g& N& m
  7. 4.321928094887363; X! S% S8 @0 _. c4 @& E- Y2 G
  8. >>> math.log2(16)
    + y8 g7 d4 ^4 m1 n
  9. 4.0
复制代码
& J9 ?' P" h; G) ?. [
math.modf(x)  返回由x的小数部分和整数部分组成的元组/ b2 S6 s6 i, q
  1. #返回由x的小数部分和整数部分组成的元组* \' t. y7 @  T
  2. modf(x)
    9 X, X1 c0 P0 Q( y3 R. g
  3. Return the fractional and integer parts of x.  Both results carry the sign& t& i1 m- E! L: }
  4. of x and are floats.% x, J0 N& M/ V7 A
  5. >>> math.modf(math.pi)
    * V) G- G% t8 G3 c+ {
  6. (0.14159265358979312, 3.0)5 `* E. o* ~, I% E* {5 z) A
  7. >>> math.modf(12.34)
    # [: b" I( J% r+ J. x
  8. (0.33999999999999986, 12.0)
复制代码

2 P% D7 C' P* v0 R* smath.sqrt(x)  求x的平方根
* Y8 M- \3 _$ e% r5 v! k* j. G
  1. #求x的平方根
      z- C7 ?& z" S. W
  2. sqrt(x)3 T) C  z  a0 u) H# ]& U" f) X
  3. Return the square root of x.
    ! b4 Z- V: U; ~; ~; C
  4. >>> math.sqrt(100)* A9 z8 d4 H1 _' n* t! ?
  5. 10.0) _( u# V9 T+ K1 [; i9 F% W
  6. >>> math.sqrt(16)8 e# }$ T6 v6 d% t6 U
  7. 4.0# `" Y8 E2 L" ?  B/ W' R6 [/ ~$ ^
  8. >>> math.sqrt(20)
    ( `  u/ [7 ~( e. |9 f0 ~9 c0 v
  9. 4.47213595499958
复制代码

6 D, S# H; D) p0 L# A9 V+ U7 kmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    , z, M& z. e( Q. \
  2. trunc(x:Real) -> Integral
    $ _! ^4 W5 a' t
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ( G8 Y6 e- O/ t! O
  4. >>> math.trunc(6.789)! e% c% y. v5 A! Q# O- j
  5. 6
    0 z/ X! c* F' V& G/ I
  6. >>> math.trunc(math.pi)- |0 y7 ], z' }: V
  7. 3+ s& m, P  e# R: s
  8. >>> math.trunc(2.567)! u  L* W$ {. u; n3 ]2 y
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-2 20:35 , Processed in 0.087103 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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