马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 m( u6 j1 e3 T8 t- E4 u, T
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 R* _1 B u$ {
' {& G T+ U8 F9 X1 @ R3 [. j0 K方法1:
- R2 t, m% Z3 n% p4 w+ I! e: M- >>> import math
; x( \/ [# o, m8 J& @, ]& A - >>> math.sqrt(9)3 w+ O3 s/ A! R- Y4 \
- 3.0
复制代码 方法2:5 K# k, c7 h O5 `
- >>> from math import sqrt
) `2 v/ n; F/ ~; _5 @ - >>> sqrt(9)
1 o z7 K4 k. ?# { - 3.0
复制代码
4 b8 ?9 [6 G3 Z9 |/ f' h
) w# q1 H; r1 u3 [- Dmath.e 表示一个常量 h! h; ?6 g! {
- #表示一个常量
) W3 |3 @/ e) s" P* ?& M - >>> math.e" G: t! l9 G4 i- W {3 Y. `+ C
- 2.718281828459045
复制代码
1 S: v) Q$ v" q cmath.pi 数字常量,圆周率 M n9 r: w4 j' O( O! O9 F
- #数字常量,圆周率8 q4 Y( m0 Y& `1 }/ F
- >>> print(math.pi)% l1 ]+ M. z x5 p
- 3.141592653589793
复制代码 % T' m) r* D- [' _, ~% j J
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x. R/ c( I: p, }4 j
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x! ^8 d/ M' y4 Q2 v
- ceil(x)- _! ?8 f X/ ~8 A i
- Return the ceiling of x as an int./ _! W3 Q5 O; D5 l! `" P. O& i
- This is the smallest integral value >= x.! a4 @6 u/ W9 h" G# n1 x* T
& y6 m7 ~6 O3 i3 N9 o" }- >>> math.ceil(4.01)3 z7 W: D4 t% C4 R
- 51 @# `, a! D& g. r' o8 b
- >>> math.ceil(4.99)
) ~" B8 c0 t6 l2 f - 5 O/ X" U+ V( f" A+ J9 s7 D' P
- >>> math.ceil(-3.99)
! I0 \% z5 V' K0 N - -3* Y3 o" M+ b/ ~
- >>> math.ceil(-3.01)4 J5 u9 w+ a9 I4 i
- -3
复制代码 6 F2 b: [/ W0 n* R" v2 P
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
( ?% E* t$ O7 C; f2 \/ z# r* F- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: Y7 B1 D- k1 D - floor(x)
" h/ R# w- W3 c T - Return the floor of x as an int.- d! s( o9 i1 F( D4 L
- This is the largest integral value <= x.
8 O# X7 \& A- Z a/ m1 O; m - >>> math.floor(4.1)
. O! x$ {0 t6 \" ?/ D; F9 f% ]4 o - 4
" x1 r% X! L/ w; G - >>> math.floor(4.999)
. q5 ]$ t& v; N0 E8 k$ ? - 4
8 f1 z0 X" p$ y. V+ ]; i - >>> math.floor(-4.999)
. p# A5 R( F+ e* L4 F) @ - -5
' ^ G* c: R+ c. Z y - >>> math.floor(-4.01)1 }2 E& @' q: Y! d; M* F9 z% S
- -5
复制代码 " d' P) q' y$ ]" G
math.pow(x,y) 返回x的y次方,即x**y
, x6 |9 H4 D2 @. n- #返回x的y次方,即x**y# N$ ~9 ]; g0 z2 g" w# L
- pow(x, y)& {- D% P+ ]. p: m
- Return x**y (x to the power of y).- G H; C5 Y0 y8 B& f
- >>> math.pow(3,4)
' ^7 c, i; ^! e" Y% J$ S6 J2 V. s - 81.0
: s9 B" i# f- S% }+ y* s - >>>
9 [' P+ H+ l0 A$ S- i9 d - >>> math.pow(2,7)$ c" J7 [ M! W* _
- 128.0
复制代码
, F! k! g2 C/ imath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 t, M+ _( R# S# C( F5 d; k8 G
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# r1 l! [' u. h, b% Q( K - log(x[, base])+ u7 m0 e+ E4 ?) i, t6 b
- Return the logarithm of x to the given base.1 W: b. L4 z' G9 S( H2 B$ X$ }
- If the base not specified, returns the natural logarithm (base e) of x.- E. Y$ S3 n: g5 N
- >>> math.log(10)( H7 m- d! a3 X) b# G& O
- 2.302585092994046& g; Z) p4 a% R! d# ^6 p2 c7 y
- >>> math.log(11)* ~* R9 |/ c& Z! p& p/ v
- 2.3978952727983707* S( x- c0 w5 `9 {( h4 T
- >>> math.log(20)' @7 }' Q$ {. s6 J$ H. _
- 2.995732273553991
复制代码
# Q( h) N5 m) \math.sin(x) 求x(x为弧度)的正弦值6 K, S+ U+ N, C k
- #求x(x为弧度)的正弦值6 x5 y& A2 V+ h: [, o2 t
- sin(x)
& h, A! `( o: u. A% c# E! u; C - Return the sine of x (measured in radians).$ ?) O* i6 T3 [: I" {& G
- >>> math.sin(math.pi/4)
9 k( q- C/ B2 J& X0 ~" Y5 t - 0.7071067811865475
; w* \ G! t8 @: H: Q' T - >>> math.sin(math.pi/2)
e+ x) \* k- [* Y - 1.0
# _2 `2 e( P% R) ]; O0 {+ | - >>> math.sin(math.pi/3), N% W+ V% P8 {: _- F& t) K! t
- 0.8660254037844386
复制代码 8 B# n& U, j8 z# l6 }$ \9 X; ^
math.cos(x) 求x的余弦,x必须是弧度1 d% x7 E- b6 w; D p. R* i
- #求x的余弦,x必须是弧度' V& I" T. e4 ~3 H( u
- cos(x)
; C7 D- ?: D9 u# q7 f - Return the cosine of x (measured in radians).
% l) u# D' K r' s- y - #math.pi/4表示弧度,转换成角度为45度, `* G" B' Y- ^( V2 Q' ~9 ]7 {# l
- >>> math.cos(math.pi/4)
; h, A0 k! h" ^4 p1 b2 ~ - 0.7071067811865476/ S3 c* N. z0 q5 m# `
- math.pi/3表示弧度,转换成角度为60度/ I5 \" |& P* i8 x' P; d
- >>> math.cos(math.pi/3)! W" o" Z2 e! O) @' A
- 0.5000000000000001
9 \( z( s. Y- E6 c2 P- D - math.pi/6表示弧度,转换成角度为30度
5 b2 t6 Z3 K- V' ] - >>> math.cos(math.pi/6)( }2 N9 F! W4 }- {
- 0.8660254037844387
复制代码 - z& ?8 g6 u7 H, @6 a# C8 d
math.tan(x) 返回x(x为弧度)的正切值
% o4 v! f6 f1 Z- #返回x(x为弧度)的正切值" ]9 M3 w2 E' |9 ]- w% {; Q
- tan(x)
4 n, C( e9 \. c6 i+ O4 K - Return the tangent of x (measured in radians).
( ]5 d: f, y8 e# O6 o1 g8 M - >>> math.tan(math.pi/4)9 `* x i% y! W' C! {" Y [$ O; F7 e
- 0.9999999999999999
6 D- h9 X) S R( q5 X7 d& |" A - >>> math.tan(math.pi/6)* z3 Y* B4 J# v9 l
- 0.5773502691896257
* E3 F5 c! h4 |) f, T5 k - >>> math.tan(math.pi/3)/ t" x5 z& z1 } u
- 1.7320508075688767
复制代码
+ g5 `% N8 Q; z- L! a$ j# Amath.degrees(x) 把x从弧度转换成角度
5 f B N: Q$ {, s3 w6 h' O3 e. l- #把x从弧度转换成角度
! a& F, z( E5 Q! C* ? - degrees(x)
5 u" d- s+ E" w$ e# J) @+ e2 ` - Convert angle x from radians to degrees.
. d9 K0 g( N4 c8 j& | - 2 b' R2 a4 m7 ?( N
- >>> math.degrees(math.pi/4)
3 ]7 X" I# b x- N. p - 45.0) d& v# @7 B% q* T
- >>> math.degrees(math.pi)% G9 C( J: e; [) z
- 180.0) m/ ~: T3 z% G+ x) d5 T% u
- >>> math.degrees(math.pi/6)
- L6 u M+ r9 R - 29.999999999999996
. q; S" v& h5 X# R( K7 Y3 {- } - >>> math.degrees(math.pi/3)1 M$ e c5 H$ I1 q1 V: s% j1 h
- 59.99999999999999
复制代码
* i$ o" R" \* K( N1 hmath.radians(x) 把角度x转换成弧度
5 d; f! K# M' M. w2 X/ r2 M- #把角度x转换成弧度% s* |. X4 [+ r" e1 a. P" W
- radians(x)
2 B( Q& _( G5 G" P" P8 J$ @+ |, ^) \ - Convert angle x from degrees to radians.& I* a! M G) e0 T
- >>> math.radians(45)
7 }' r8 k4 x! c. ` - 0.78539816339744835 u' K U# v& n9 h2 \2 H6 W3 }9 [
- >>> math.radians(60)
/ b/ S( P! a2 a- s' e4 w" ~ - 1.0471975511965976
复制代码
7 V" q: A" w# ~! l7 b0 s, l: smath.copysign(x,y) 把y的正负号加到x前面,可以使用03 C' _, K& |9 p* h0 ^
- #把y的正负号加到x前面,可以使用0
2 B0 o( U! [4 m - copysign(x, y)
7 A9 j& X0 P# Z+ H$ G; U) e9 m - Return a float with the magnitude (absolute value) of x but the sign
; v( p6 M' I" v - of y. On platforms that support signed zeros, copysign(1.0, -0.0) - W b& z; Q1 _ w
- returns -1.0.
# _( b% a1 l5 s# m+ N* V! f
- K y) p E" `9 e- >>> math.copysign(2,3)( |7 V/ `% l/ q5 q' E; x0 g
- 2.0
/ u! r$ f% e3 R. A, A - >>> math.copysign(2,-3)
+ w' b" Z x9 I0 }2 I* o5 s - -2.0
. ]- N# g, C, T- d4 ]8 e - >>> math.copysign(3,8)( T. O9 ^8 N: ]
- 3.0( N! k* s5 e: P2 Q x
- >>> math.copysign(3,-8)
G7 [ Y* r4 ]# P - -3.0
复制代码
3 F/ M# E6 X/ F0 i mmath.exp(x) 返回math.e,也就是2.71828的x次方, f, J G' u1 v8 ] b
- #返回math.e,也就是2.71828的x次方
$ G5 l9 _+ k# k* f9 z \ - exp(x) `; S- \- f8 V4 W8 o
- Return e raised to the power of x.
/ T! k7 Y3 o. ]
0 V8 D! s" j5 P# Z; @+ X- >>> math.exp(1)
& {) J/ L# x% k6 A+ z - 2.718281828459045
/ u9 T, d9 s4 q# A# H L - >>> math.exp(2)
- ?& |1 o- U5 }' E* W, ]! s4 S - 7.38905609893065% u% e A0 P3 z4 C! I
- >>> math.exp(3)
$ t" w" B6 ?+ M+ ~ - 20.085536923187668
复制代码
' y. i+ N5 ]* O: M6 I8 Q, l1 hmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
! F4 c+ q7 T! k3 m& U& d& Z- #返回math.e的x(其值为2.71828)次方的值减1) f6 s# K. C |6 q' }8 v" ^; b
- expm1(x)5 K+ z* @: ]% ~5 I2 u, ~/ G
- Return exp(x)-1.
* v6 c2 h) ~+ i) W; Y ] - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.& p! x) R0 z9 E
0 w9 t5 x' J0 K' ]4 l- >>> math.expm1(1)
$ d) O" f, G8 ]- K3 V) \. v* w) Y - 1.718281828459045/ I5 p3 o& c, v. x+ ^3 X+ U
- >>> math.expm1(2)4 Z* D$ g8 o8 x4 b
- 6.38905609893065& M% P. m* u' m2 R
- >>> math.expm1(3)
# M6 F1 I7 k' F. h+ b - 19.085536923187668
复制代码 k2 q' L0 ]; [3 j( V& J. t1 a
math.fabs(x) 返回x的绝对值
. C. i4 Y& U/ M0 d% A- #返回x的绝对值
% ]/ o- S B* m, J - fabs(x)1 y* A. A) _) F& U. G# c
- Return the absolute value of the float x.; p4 j) a! k |7 A# u
- ' z8 I- i0 l7 Y2 K' q
- >>> math.fabs(-0.003)$ r) h3 {% ^3 N$ V* s9 f
- 0.003, w3 K( J( R( J; N6 s9 Q
- >>> math.fabs(-110)
) ^! P# j4 z2 U4 K3 S, R - 110.08 C6 z C2 z7 R9 s7 s
- >>> math.fabs(100)
8 l6 s9 ]& q: o$ y' ^4 B( q& K/ Y - 100.0
复制代码
. z* U4 @/ h) m" hmath.factorial(x) 取x的阶乘的值
1 D! M% u0 a/ |- #取x的阶乘的值& j7 Z. O+ h2 {& K$ Q2 {
- factorial(x) -> Integral
9 C. m4 P0 N' k8 v% P2 b5 a/ {% U - Find x!. Raise a ValueError if x is negative or non-integral.
% {, s+ r# }/ R8 F8 r - >>> math.factorial(1)- ?& M- ^1 u; s: N0 I" [
- 1
3 O q% E" s5 P I - >>> math.factorial(2)' Z' J/ j/ Q7 y7 U
- 27 D4 y# X" f) q* c$ l% ^2 R8 t' W
- >>> math.factorial(3)
6 b3 p X* g$ W$ f" N1 S) P - 6% v- W& l- `. Y ~. Z9 W
- >>> math.factorial(5)
, c! s8 k0 z6 n, u8 h - 120
2 t0 D9 n! X+ d* _ - >>> math.factorial(10)
- w5 K) e9 W% L8 ~) W3 Q - 3628800
复制代码 & o: c5 b$ n% o" y5 q) \7 |' G
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
8 L& y; f& c5 O% l+ p- #得到x/y的余数,其值是一个浮点数
" e" _+ b$ V, K - fmod(x, y)! H% U( @3 j- Z, `( Q$ W( Q
- Return fmod(x, y), according to platform C. x % y may differ.
' X7 j+ ~3 B. H; _ - >>> math.fmod(20,3)9 I4 I( s% i# ~" t( w `, b5 V% [( J
- 2.0
1 ]$ d8 H$ r, ~ G& l - >>> math.fmod(20,7)
$ S8 W3 U9 Y0 z" n2 q - 6.0
复制代码
- G& i* I4 D3 u4 H- ymath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
[/ i4 b0 \4 f5 [8 _$ z$ q0 l' a- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
. m8 T( I# E+ H; W3 R. w- H! G7 M - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
2 G r5 @8 l) o6 _% @2 u$ M# r - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
" i% x, Z+ x+ X. O - frexp(x); N* y3 L8 c/ n! A
- Return the mantissa and exponent of x, as pair (m, e).4 r' L+ v" l9 [7 S: W3 f0 D
- m is a float and e is an int, such that x = m * 2.**e.
9 Z R! M ~# k+ B - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.- {. P& b3 h7 ~% K; ^! b, d( s
- >>> math.frexp(10)
( h3 q; }5 K8 D, i* e! p - (0.625, 4)
7 z* E, D( K" p9 ^ - >>> math.frexp(75)8 E5 _" B( w' L% D1 \
- (0.5859375, 7)2 F- V4 ^6 B9 Z+ D+ ~4 z/ x
- >>> math.frexp(-40)) i$ m N P$ q+ s6 j; I* m# i5 _
- (-0.625, 6)1 ?6 E3 g2 s. j
- >>> math.frexp(-100)
t/ p: p# i/ a( {0 o& E - (-0.78125, 7): r* v9 C; g0 o% ~. l
- >>> math.frexp(100)7 L) Y8 r7 O& w: W9 X' j+ f$ F
- (0.78125, 7)
复制代码 9 _" f5 l/ Q0 P% Z! d
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 a) z: J+ c) _6 Q" n2 j4 x- #对迭代器里的每个元素进行求和操作
. u; s; C9 F( u) P1 _ - fsum(iterable) g) m* t% W* i8 Q4 U+ p
- Return an accurate floating point sum of values in the iterable.3 i9 s" O8 o" h9 z
- Assumes IEEE-754 floating point arithmetic.
8 m" t# b, g- ^6 m - >>> math.fsum([1,2,3,4])( m a, {2 b6 W" \& c
- 10.0, q2 }. {4 ?# z) s2 U
- >>> math.fsum((1,2,3,4))
. V1 q: r7 i# C7 \' X* ` - 10.0
1 M3 |4 d* h5 M' n$ H% l - >>> math.fsum((-1,-2,-3,-4))
. p# J" _0 p5 d9 a8 z( y0 ` - -10.0
?7 O3 n1 L- Q$ H, x# K! V8 X - >>> math.fsum([-1,-2,-3,-4])' g. e8 }/ t5 n( k! {# Q, @1 X
- -10.0
复制代码 % I! U) t# c0 Z! X3 m6 J( H/ h
math.gcd(x,y) 返回x和y的最大公约数2 I( N3 y2 @7 z7 U1 ^
- #返回x和y的最大公约数
. P- k; \. B) s/ j* k3 K [0 y - gcd(x, y) -> int2 ^, \3 f2 [4 O8 v( _
- greatest common divisor of x and y
6 ?5 j9 G! ]. M% t - >>> math.gcd(8,6)
8 J/ s, Q1 b0 v, b$ }' m1 E - 2
+ F' m! o; t, Q M2 V7 y6 h2 [ - >>> math.gcd(40,20)
9 m, ^ k% o P+ }" | - 20) \( s& P" [: |( z0 U( M( R/ o b7 U
- >>> math.gcd(8,12) B9 v+ L5 b9 k C
- 4
复制代码
* g5 J5 n6 y; x! p' mmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
9 c4 n7 y# @7 G% l- #得到(x**2+y**2),平方的值
1 n& o! F2 A4 N' [4 `$ O: X `8 T( x - hypot(x, y)
5 C1 P4 c% p# D" @+ @ - Return the Euclidean distance, sqrt(x*x + y*y).
3 ]% F- |: n3 N; T6 G. Q - >>> math.hypot(3,4)
! t5 c! `2 K! l. ~3 T! p- \ - 5.0% u2 \$ u, S9 e# x
- >>> math.hypot(6,8)2 e+ E7 V. x2 z9 Y9 M8 O( F0 ~
- 10.0
复制代码
@) z" n$ {0 r+ h: ?math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 R$ k/ c$ X3 {0 H' y2 j, f2 O9 t4 L
- #如果x是不是无穷大的数字,则返回True,否则返回False
9 g/ P; C) j2 r- e2 L- }" x8 b - isfinite(x) -> bool& h4 G( n$ n# Q* Y4 _* y
- Return True if x is neither an infinity nor a NaN, and False otherwise.$ {' x6 z' D2 H T% Q' x1 F
- >>> math.isfinite(100)8 n" l, z9 _ Q& Q! b% k! y% w
- True( |) I D; ?$ r$ R) A
- >>> math.isfinite(0)7 y, X3 k t0 @3 y$ l
- True
, s1 f4 Z2 U; j - >>> math.isfinite(0.1)3 n1 L. t8 }9 y& w" f1 f% c" X' w& g
- True
5 W3 i# M6 }+ h0 O$ y; t2 g( W$ h - >>> math.isfinite("a")# @* q, j0 ~4 Z: ^
- >>> math.isfinite(0.0001)
- k9 z. L% |3 s w$ g" H z - True
复制代码
( b- m, Z! [: r" j/ p" ymath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
* W( a8 ~8 }' Z0 ]5 p- Q, o- #如果x是正无穷大或负无穷大,则返回True,否则返回False& {1 w/ u6 y- q+ @5 m1 P" v
- isinf(x) -> bool3 W5 e q! _, T u
- Return True if x is a positive or negative infinity, and False otherwise.
! h* f3 F2 @ C, ^& e; j - >>> math.isinf(234)
3 |. }3 ?8 g" v# ?/ A - False& P# {9 E& T4 i7 S) p) e7 G
- >>> math.isinf(0.1). O* c1 L( v7 h1 q3 p# C9 F
- False
复制代码 , y1 [0 K" Y2 |0 z; ~
math.isnan(x) 如果x不是数字True,否则返回False
# A: |! P" i4 m0 a3 _- #如果x不是数字True,否则返回False
* g* \4 i3 l$ Z - isnan(x) -> bool
. K+ Z ^1 F! d$ t% g. ? - Return True if x is a NaN (not a number), and False otherwise.
0 N2 T; p& l0 j - >>> math.isnan(23)8 B, ~! _9 D! i$ n+ P
- False' a) h9 A0 `/ p4 J# ^
- >>> math.isnan(0.01)
$ L }9 h& B: K2 g( g& c* K - False
复制代码 + j* I. s( F# W0 b& |
math.ldexp(x,i) 返回x*(2**i)的值
% J- a5 G5 v+ L& t! \' E9 Y- #返回x*(2**i)的值; F* A9 _0 e. J3 n
- ldexp(x, i)7 N% [" R3 U) y9 @2 F
- Return x * (2**i).' h* ~1 ?3 h- l
- >>> math.ldexp(5,5)7 s8 s: r6 x R [, A. V4 R! S0 O
- 160.0/ T4 B( r! }, t: e. [- E$ S
- >>> math.ldexp(3,5)
' s; Q! B2 i( U% j0 G - 96.0
复制代码 4 [' l* b1 D- G& B$ W
math.log10(x) 返回x的以10为底的对数
$ k2 ?+ z! H. R4 X$ _! l- #返回x的以10为底的对数
8 P% O7 j& j+ m2 h% k0 W' ^& {! h2 @ - log10(x)
1 b0 N4 j/ p/ y" x" f7 o - Return the base 10 logarithm of x.
$ v0 e9 q( {, Y$ a - >>> math.log10(10)
4 {6 ~" v4 u2 b' }9 l - 1.0
2 |+ S3 T* Z2 f" s0 g - >>> math.log10(100)
! ~' ~0 U+ R% ]! o4 I- W - 2.0* T6 Z- Y& U0 T
- #即10的1.3次方的结果为20
" z; M# |- b7 X" l7 D1 I7 p1 N k - >>> math.log10(20) g1 d- L# J$ y0 e
- 1.3010299956639813
复制代码
7 h9 g" W1 |6 ^math.log1p(x) 返回x+1的自然对数(基数为e)的值
4 Y [# w I( j% x* S* b- #返回x+1的自然对数(基数为e)的值
! M6 p, X8 `5 M% M9 w - log1p(x)
& H! X2 x" f( P& V4 r3 p/ { - Return the natural logarithm of 1+x (base e).
: X& Y$ J$ L" j) S - The result is computed in a way which is accurate for x near zero.
; u; C, E B$ C; W - >>> math.log(10)" \% ?- T! r1 j6 n9 C
- 2.302585092994046! n2 t; [3 f3 p, A
- >>> math.log1p(10)# N6 E& N" X0 i
- 2.3978952727983707% S* P" ` N! o; Q' _4 a
- >>> math.log(11)+ K8 X$ N# y/ }& l+ }# ]
- 2.3978952727983707
复制代码 & ~! u) B) Y0 @5 ~. I1 u$ t
math.log2(x) 返回x的基2对数 Y' u7 x. u% T
- #返回x的基2对数& i3 _4 X& n3 h: i# v
- log2(x)
: P; s5 v2 j7 M8 y+ O$ Z - Return the base 2 logarithm of x.( T/ Q9 @7 R) `* U+ v% q
- >>> math.log2(32). d. {- G8 v$ j. T0 q+ t" ^- E6 S
- 5.0. Y" ~" Q0 W$ q/ {$ ~, |
- >>> math.log2(20)' x9 U4 q! P# m: p
- 4.3219280948873638 }8 s2 m5 K& a5 V& o/ z4 W7 V- I
- >>> math.log2(16)
3 i0 z/ C) K+ v# Z0 L3 ^ - 4.0
复制代码 ; H1 d- o1 ]2 ^2 r2 \
math.modf(x) 返回由x的小数部分和整数部分组成的元组
1 ?+ |' A* w+ I/ i2 ]% t- #返回由x的小数部分和整数部分组成的元组9 K3 b6 M2 \' _. k
- modf(x)# W! V" ?: | N$ i+ a* ]8 \0 `
- Return the fractional and integer parts of x. Both results carry the sign
, u. F. ~% F' l( o - of x and are floats.
; ~, }8 s4 q8 `1 ~4 U+ {# A, p - >>> math.modf(math.pi)
P( b8 z+ s, a' U( D$ B - (0.14159265358979312, 3.0)( j5 E' z' u+ M, M/ d5 H
- >>> math.modf(12.34)- v' I: ?4 b6 K5 }. g$ O; f% n
- (0.33999999999999986, 12.0)
复制代码 / ]2 {! C1 x- q, H" C+ J
math.sqrt(x) 求x的平方根
' n+ L. [' W6 @( @& ^1 L- #求x的平方根
( {: d" V# j0 j% t9 L - sqrt(x)
" I% n) I! w Y) Y" g' z$ k4 t - Return the square root of x.
' P& H" r0 h, t9 a* o - >>> math.sqrt(100)
; r" `" R3 [+ D. [ - 10.0
1 k- _7 s7 b, R' Z! r; ? - >>> math.sqrt(16)$ ^7 c! K. g- l- s
- 4.0
# d1 d2 Y% c! P* J5 Z - >>> math.sqrt(20)
6 M+ N7 O. A0 W m! I4 V - 4.47213595499958
复制代码 / A; E' q2 {6 D1 `' M' U
math.trunc(x) 返回x的整数部分- #返回x的整数部分5 e2 |" g6 \5 W E( D( N
- trunc(x:Real) -> Integral P. U' C4 M* H. B. M
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% S: p6 i) [7 @
- >>> math.trunc(6.789)
& R$ E) {" ^# ?6 N2 ^: R - 68 \ j! Y3 \; Q* F% s) X
- >>> math.trunc(math.pi)! o, `! F5 I4 i* l$ j, W1 ?
- 3
' k# J. b# ]# X: W7 G& ^ - >>> math.trunc(2.567)+ ~8 T2 p$ }- @$ O
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|