|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ R( E9 d# O2 a2 i* n【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 w" g! D1 a# `
* z6 y: E, |: @6 }/ n
方法1:
; S; e! W" Q( m8 ]+ a% _: i- >>> import math. }0 g' n6 Z$ z2 ]1 j! J) V
- >>> math.sqrt(9)1 n6 h# A8 @: [; w: o' I; x# }* P
- 3.0
复制代码 方法2:9 V3 `" @7 J) v F' k4 }
- >>> from math import sqrt
4 ^) L! s) }* X# [ - >>> sqrt(9)
z& ^' ^; B1 d - 3.0
复制代码 , F# B* \2 g& t% b/ m- S
$ S5 i4 r" D( e) O1 Qmath.e 表示一个常量
5 m b" V! u2 x, s! R+ a- #表示一个常量
( r l0 E: i- Y5 F: a8 N - >>> math.e1 O& U( n$ G9 U t
- 2.718281828459045
复制代码
) G# A/ _' x6 j9 ?8 ?- ~math.pi 数字常量,圆周率
: D2 v! V; {. f* b0 C2 i, f- #数字常量,圆周率( ]8 \1 B. V5 a2 z
- >>> print(math.pi)5 x( b4 t. W e4 a% t2 f, ~
- 3.141592653589793
复制代码
# Y' s& y6 ?8 p" fmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! `, z% i6 h- |& D9 q- #取大于等于x的最小的整数值,如果x是一个整数,则返回x) r6 M t$ X0 Y
- ceil(x)8 s, \, T8 C: L: l
- Return the ceiling of x as an int.
& f& L( f8 E& Q; t8 n - This is the smallest integral value >= x.7 u% z+ |8 b" m* i) `2 C. @* |: d
- 4 l' B5 n/ D) {: C" o
- >>> math.ceil(4.01)
0 m9 Q1 V. d' o, ` - 5
8 t* S- O2 N/ Q! i2 | - >>> math.ceil(4.99)' l) x; o' `; i& B) r9 d% q6 ]
- 5
( z) w4 w) V! W! A, N( I( B - >>> math.ceil(-3.99) P. M0 }( {- M0 o7 v0 E5 R# J R
- -3
9 ~6 E) `6 N- `: g1 y# I5 L - >>> math.ceil(-3.01)
$ s. n% J$ X- |6 o/ _5 r - -3
复制代码 ( b+ D* G" T, ]+ S2 e
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ Z$ Z, J1 c+ H: J- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身' _* E9 [3 m0 k t# e
- floor(x); [# Q/ e: | r
- Return the floor of x as an int.; d3 c' _- _+ b: U N" l
- This is the largest integral value <= x.
5 @8 z3 u- ^1 I - >>> math.floor(4.1)0 `1 G' n# D2 t& y; h) Y% g1 I
- 4* t' D6 M) G, y# d
- >>> math.floor(4.999)
. R) Z; x/ M. Y4 k6 q$ t - 4! b4 [( A" a4 ^! h9 K/ C' [+ v
- >>> math.floor(-4.999)7 F8 I- S4 w* P( S, P
- -5: P1 J0 R9 \; Z6 p
- >>> math.floor(-4.01)
! D% u9 x9 ^& F! ] m9 _' c3 y - -5
复制代码 , g! a# X2 b& z7 D& z9 N
math.pow(x,y) 返回x的y次方,即x**y. Z9 x) |2 r- G" a! [, W! c3 _
- #返回x的y次方,即x**y3 V" T8 e7 Q& `3 d5 p
- pow(x, y)
! ^4 f& N. [- a5 t# C - Return x**y (x to the power of y).# [# G- M+ U- L
- >>> math.pow(3,4)
+ t" J( b. M. d$ R% \* h - 81.0
1 ~( m! e+ O, Q2 F$ ^# b - >>>
1 R! _! W8 Y3 y+ k - >>> math.pow(2,7)* b( x9 }, t; w7 Z! B, b Q
- 128.0
复制代码 " R; m. ^! S" s1 D3 [
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* M7 S, ~0 x; J) A; ^5 U
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 j# `9 E/ q: n6 J - log(x[, base]): L$ Y1 w% h7 W) m/ t1 Q
- Return the logarithm of x to the given base.3 D2 ?/ }& l0 T& ^! o
- If the base not specified, returns the natural logarithm (base e) of x.
& v6 O# m. f: M% P+ N0 V8 W - >>> math.log(10)3 ]2 U. d8 B3 M" U# j
- 2.302585092994046
4 A( \6 k/ p7 Y7 ^; F/ O" g - >>> math.log(11)
& i3 `; N* K0 \0 x: ]$ s - 2.3978952727983707
, E, ~" K- o3 [ d3 t/ m - >>> math.log(20)9 [5 r; g3 ]8 O8 R& _; E: m
- 2.995732273553991
复制代码 $ ]) R, S2 U' U7 Z& \# q0 N
math.sin(x) 求x(x为弧度)的正弦值
! l, X- T1 L- G6 A n- #求x(x为弧度)的正弦值
4 Q4 Z8 q4 Y1 B9 ] K4 i( e2 O - sin(x)
8 a2 L) K# }0 w$ G4 p - Return the sine of x (measured in radians).
; }5 R1 c5 ~; D$ c6 s - >>> math.sin(math.pi/4)
8 L( Z0 `+ `9 P* v% |0 P6 c - 0.7071067811865475
3 c" c+ y1 e4 I& U5 t' b( r1 u: y - >>> math.sin(math.pi/2)
3 z" B: @ _& o8 N, m) z$ G - 1.0
" S0 F; F% k0 k( }' x - >>> math.sin(math.pi/3)$ ]2 z; a. J& p3 {' |) X7 l6 e
- 0.8660254037844386
复制代码
" a' U5 W8 \! q8 [2 y. e2 S2 Smath.cos(x) 求x的余弦,x必须是弧度
& Y! [$ m. j: q7 b& C/ f/ s: q- #求x的余弦,x必须是弧度* R) c( K9 w6 f
- cos(x)( j% M y" A/ ~" g
- Return the cosine of x (measured in radians).
% W0 I2 Y8 A7 ]) { - #math.pi/4表示弧度,转换成角度为45度
4 W. r/ q7 U3 N! ?9 {3 ~0 U+ A - >>> math.cos(math.pi/4)
* d" C" @5 H+ V6 k) H) i - 0.7071067811865476: I! |, k7 q( j; t f$ d" p3 e9 W
- math.pi/3表示弧度,转换成角度为60度
2 g+ d0 a/ d0 v% s: | - >>> math.cos(math.pi/3)# @$ i/ f) q" F5 ?) R1 s0 \
- 0.5000000000000001
6 N2 W! P @' I2 U4 N+ ^1 Q& d2 _ - math.pi/6表示弧度,转换成角度为30度( V4 P/ @& ~% G ^( {
- >>> math.cos(math.pi/6)
. v G, ]% q/ z/ ` - 0.8660254037844387
复制代码 9 Z5 q3 x* M! |4 \
math.tan(x) 返回x(x为弧度)的正切值6 ~; x9 p2 C+ _% s" n8 F$ ~/ G9 h( e) F
- #返回x(x为弧度)的正切值! I: N! ~, ]. r+ K
- tan(x)
7 A7 ~5 C3 U" v ^ - Return the tangent of x (measured in radians).
& L1 H: g1 j C2 R - >>> math.tan(math.pi/4)2 V7 I8 q0 N+ z% g* R: x
- 0.9999999999999999
% B" }( E6 J9 W - >>> math.tan(math.pi/6)
# q9 Y" T, I: W! v! o% D& t1 c9 i - 0.5773502691896257
1 }# C' {- l E$ M$ G - >>> math.tan(math.pi/3) |1 e, l9 n4 x
- 1.7320508075688767
复制代码 h% S# r& N6 H6 ~
math.degrees(x) 把x从弧度转换成角度( U9 H. `! }: o# R( ?
- #把x从弧度转换成角度: z( @9 K1 I) J+ S" f( V
- degrees(x)
, T4 Q' O! P# r - Convert angle x from radians to degrees., K' ]$ f1 Y' r R7 H
- ? M' i r" E, l
- >>> math.degrees(math.pi/4)
/ x$ o5 _% \+ k: u# M) w - 45.04 u% U- u) n8 |' v% R5 a, m
- >>> math.degrees(math.pi)
2 [% [% Z- ?: Y! T0 H! B2 g# E5 F* ~ - 180.0$ x$ o, X* D. c" S# _1 z
- >>> math.degrees(math.pi/6)9 p& q) i. c$ g2 n
- 29.999999999999996
9 ?0 g& o6 J6 M# t - >>> math.degrees(math.pi/3)
3 {& ?! J) F! V - 59.99999999999999
复制代码 1 c5 s" K$ _, A7 U4 C: i% \* F, c$ B
math.radians(x) 把角度x转换成弧度5 x& @8 i1 G9 W" c$ ?
- #把角度x转换成弧度
2 V% F+ V r/ j/ W - radians(x)& {& B; N6 I% s- e
- Convert angle x from degrees to radians.9 ~* _; Z/ |6 X) p
- >>> math.radians(45)% ^/ ~# _* `) H' L+ p, L
- 0.7853981633974483
( y, L8 l& g& L* g - >>> math.radians(60)+ n; A; p- t. C6 K. R: W
- 1.0471975511965976
复制代码
9 x/ k: c1 j' Hmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
- Y# K' j- ^ o9 X3 U; P2 {2 X' G- #把y的正负号加到x前面,可以使用0" }, n) }( l- I g y6 ^% h) T
- copysign(x, y)
9 u! N' G. A5 D" w; z - Return a float with the magnitude (absolute value) of x but the sign
) ?# s) k3 \- w# D$ E) ]3 G - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
' ^! h% x, X, g0 o- {. F) j% t2 y3 I - returns -1.0.& ~/ a" w1 ]2 b
- 3 [+ I* l2 U8 u- L
- >>> math.copysign(2,3)2 m9 Z7 f' b a9 v9 R" R7 ~
- 2.0
+ m' n1 o0 M0 a8 l! N - >>> math.copysign(2,-3)2 P2 S9 [" K. H# q: [
- -2.0
* r J9 Z" p6 Y+ w& T - >>> math.copysign(3,8)
, C# P7 c1 I6 A - 3.0
" a- Q/ f! A) R0 U4 T - >>> math.copysign(3,-8)* M' s* y7 {! r& [* G5 F I
- -3.0
复制代码
6 S) y0 d& b( |% c$ D Tmath.exp(x) 返回math.e,也就是2.71828的x次方
; f A: n1 l- |* } s$ Y$ L( r1 j- #返回math.e,也就是2.71828的x次方$ {+ x( M* H. _, `1 p1 y
- exp(x)
* p% l9 {7 x+ T. Y( l8 Y0 E1 x - Return e raised to the power of x.
2 o/ {. p6 g/ n6 q6 Z* o - : N* h: i& P( {! i2 ?" @$ j
- >>> math.exp(1)
* k2 t1 u6 |$ r( V - 2.718281828459045
, K4 b& y& w7 {5 P5 }, j" Z - >>> math.exp(2)
- u+ U2 w( q6 [: l - 7.38905609893065
7 O5 _, e; N( `% r! ~0 i% m - >>> math.exp(3)7 r) ~8 [8 R* [/ G8 ?* d
- 20.085536923187668
复制代码 % P/ a6 T+ i) g
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1$ Z" N4 Z4 d$ [* X3 t
- #返回math.e的x(其值为2.71828)次方的值减1
$ r0 @$ S4 B- W7 c - expm1(x)( }+ Z6 P" E% t! S9 g! @
- Return exp(x)-1.- L: f' |# b! |( [- ^, H' M" i: x+ D
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
1 J: C4 o! Y/ n - ( J9 w, P9 j N6 M- u& |$ P; D/ Z
- >>> math.expm1(1)3 | P" g; Q; \/ `5 w8 i1 M; h
- 1.718281828459045- r/ W% I4 B9 [1 {& U4 c9 @
- >>> math.expm1(2)
* L5 `1 }) E) G - 6.389056098930652 n3 a2 O- O1 j: x7 t- Z
- >>> math.expm1(3)
/ Y) B9 N( e7 W% C) V6 N7 l - 19.085536923187668
复制代码
_) I0 v, c- q9 F3 zmath.fabs(x) 返回x的绝对值
6 G0 {1 \/ M% p- #返回x的绝对值
6 u+ v8 D1 P1 b4 V: E' e - fabs(x)
) v$ m% s, Z2 V) c) y) E - Return the absolute value of the float x.
7 B! G7 E/ J2 | y3 C" v3 K
' K* z9 C2 [# n4 A3 a$ N: W; T- >>> math.fabs(-0.003)0 D1 B& K6 D; z* Z3 [
- 0.0034 U4 m% c, b) l. g7 k* T
- >>> math.fabs(-110)
$ d$ N2 h" x0 `7 W - 110.0/ _( c* ? | ]/ s% K( X0 K; R
- >>> math.fabs(100)2 M+ l2 ?$ n# N+ X
- 100.0
复制代码 * J- o: }( P1 C0 w" H
math.factorial(x) 取x的阶乘的值; i3 w# U# S& S7 B; Q5 T% w# C
- #取x的阶乘的值
0 u4 A9 l6 i+ j1 c- f+ |: ? - factorial(x) -> Integral
( Y. H2 R5 r+ r4 H - Find x!. Raise a ValueError if x is negative or non-integral.) V' g, ~, l/ f* ^0 i% W
- >>> math.factorial(1)
/ {( v6 v5 u% @ - 1
+ Q. [* J/ j! f+ E) S7 G - >>> math.factorial(2)# X) s# w8 d5 V" D
- 2+ [% d7 i1 ?6 V2 ?+ i
- >>> math.factorial(3)! W* k8 _ m- ]) B( H3 L
- 6
: `% i1 O, h# z* c4 E2 n3 y$ @ - >>> math.factorial(5)
& o6 G: y+ n, a+ Z' u - 120 N& W8 w Z6 E$ q5 o
- >>> math.factorial(10): F0 I# H$ q [" l
- 3628800
复制代码 ( n9 K& ], q) m. Z
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
* M- N; y8 J0 x: p% O. r4 J) r, q- #得到x/y的余数,其值是一个浮点数% |+ Q2 B8 A) G
- fmod(x, y)
" C$ } e2 {! O* T - Return fmod(x, y), according to platform C. x % y may differ.
3 Y; I2 f9 t" N! D5 g; H - >>> math.fmod(20,3)
% o% v* Y1 R# q& _9 ?3 B, g - 2.0
; T' D9 S# }7 _3 R4 K/ L4 q - >>> math.fmod(20,7)
7 B; P% b* X) \, {* U1 | - 6.0
复制代码
; ~8 f, o' p+ ?- Q9 Tmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
- z+ L( T1 D. V( l& Q7 Y0 ]- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
- M$ l9 p, o; o z4 J& v - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. ~8 \) q# d3 ~1 }8 _
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 j9 z2 \! Y, d6 ], N' [; V
- frexp(x)
+ p/ j. m; e3 Q d' w - Return the mantissa and exponent of x, as pair (m, e).
; S% N8 e: R- U& e! a- ? - m is a float and e is an int, such that x = m * 2.**e.
6 |+ ?# E2 ]2 [! Y. _ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
$ t9 C. ~) F6 n9 O- S3 h - >>> math.frexp(10)
4 c0 H* G) c3 f4 y4 P - (0.625, 4)7 |- n r$ a+ d: q6 _# a$ @- K# I
- >>> math.frexp(75)* X1 y c3 K7 |4 N( s: t
- (0.5859375, 7)) a# z3 C: ~! h* {4 H0 ^
- >>> math.frexp(-40)+ P' d9 O+ I- z( I1 a1 s4 j3 K
- (-0.625, 6)
& u* S& w: ^& N; b - >>> math.frexp(-100); }/ R F5 I, O
- (-0.78125, 7)9 r% @" A% o8 P" b, k3 R" F
- >>> math.frexp(100)
; j% ^9 q, j3 K% }* l' X5 m - (0.78125, 7)
复制代码 2 r T4 f4 } j- k: Q, o
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)6 _3 ~# O5 V4 c$ A
- #对迭代器里的每个元素进行求和操作; e( Q/ V8 _1 k8 Q) r
- fsum(iterable)
) G* Q6 R2 l& n4 `; G - Return an accurate floating point sum of values in the iterable.
+ e- a) B) D3 G/ q1 ` - Assumes IEEE-754 floating point arithmetic.' a% [7 f) q( p
- >>> math.fsum([1,2,3,4])
0 t& k+ ~7 j( u% V$ F - 10.0+ C; [3 e/ N ^$ `4 z6 w9 r# `
- >>> math.fsum((1,2,3,4))
5 s# i" C) e# h- m# i) ] - 10.0' |2 a5 I8 { E, [6 [7 a
- >>> math.fsum((-1,-2,-3,-4))$ o, |4 U5 i) ^0 F u
- -10.0
+ O/ c7 K- X0 m; k1 m - >>> math.fsum([-1,-2,-3,-4])
2 f0 h8 F$ w& n$ X- Q; v - -10.0
复制代码 & |4 s$ W! c3 e5 m5 |# k0 r& v
math.gcd(x,y) 返回x和y的最大公约数
$ @- j$ l$ ^" E; j: c- ^- #返回x和y的最大公约数) C) W! c/ {6 o0 T
- gcd(x, y) -> int
( l/ p8 q1 n& ^; r. @; @/ S - greatest common divisor of x and y1 q) I) y4 @. |; \& e+ F- L
- >>> math.gcd(8,6)
* S7 E4 F% Q, P. F8 m - 28 W+ U l* x: k9 e8 Z% V3 O
- >>> math.gcd(40,20)
9 d6 V5 Q/ K g3 q' R; T, k - 20
& C8 \/ Z# b! W$ L3 b( t - >>> math.gcd(8,12)1 x- U1 I8 g4 T' }( V
- 4
复制代码 ) j% M& _: z4 c$ F' O0 u
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False7 q8 Y0 F, Y% k4 ^0 b& Z2 Z
- #得到(x**2+y**2),平方的值
6 S! `/ y0 x; Q6 G+ S - hypot(x, y), |7 M. I5 m5 E9 @3 \
- Return the Euclidean distance, sqrt(x*x + y*y).7 N; Y) n' ` \
- >>> math.hypot(3,4)2 e2 n, d% C/ ^( y0 y- x
- 5.0
7 u' z3 d8 |+ A& m. e - >>> math.hypot(6,8)/ V& ~- R/ @$ U e
- 10.0
复制代码
# I; Q, S5 T7 m) [9 zmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
6 R3 |$ p2 U. P- S, d- #如果x是不是无穷大的数字,则返回True,否则返回False3 z/ ^' l" u+ |0 n: r4 ?/ D2 D2 k
- isfinite(x) -> bool
) a6 W, T4 r& `, [! y$ c! o - Return True if x is neither an infinity nor a NaN, and False otherwise.
$ p- p6 w) [) r6 X) a k - >>> math.isfinite(100), Z' a! ~3 w! d) }$ s* x
- True
1 O- U) F) `* w( L - >>> math.isfinite(0)
}9 }% y" o/ n, z/ j! N) P - True# y3 f' Z5 u( ?1 O1 G% D
- >>> math.isfinite(0.1)
9 N7 d1 F s% t I6 f - True- k" E- Y, R: s5 P7 {
- >>> math.isfinite("a"). x! X2 }& I: V! \. u
- >>> math.isfinite(0.0001)2 A# {6 u: P( |% G* |
- True
复制代码
, y9 G) R6 G7 X, x; O0 n9 R* Pmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
2 ~4 o; b8 y6 L+ _- #如果x是正无穷大或负无穷大,则返回True,否则返回False
; \9 S0 P8 b6 u8 V1 [ - isinf(x) -> bool
4 ]7 l% @ o) S: h5 C$ y, O, z7 K - Return True if x is a positive or negative infinity, and False otherwise./ V3 c3 U. S2 }
- >>> math.isinf(234), q( m) d" B) K* y
- False
) y4 f, k, h" @% T' ]( A% R3 Y1 n - >>> math.isinf(0.1)
' L8 e n% L# K6 C! [; W: ]$ t - False
复制代码
# u! t, G' M8 ~5 K& K3 W8 _# f+ Z" C" ?math.isnan(x) 如果x不是数字True,否则返回False# u+ \) G. o% L
- #如果x不是数字True,否则返回False) {/ i2 [4 O [) d e4 Z4 h
- isnan(x) -> bool
o, Q( Z9 t( ^0 h! |, I - Return True if x is a NaN (not a number), and False otherwise.0 Y# z7 T1 L4 C3 d6 P$ R
- >>> math.isnan(23)& Z& w' i1 Y- H. m( E, J7 u9 z9 M
- False! i' F2 t0 i, c, |+ c; c, D5 t; x
- >>> math.isnan(0.01)
7 m# g9 k# ]6 v X. B, ? - False
复制代码 ; O" ~& n3 v! w1 [- K2 m
math.ldexp(x,i) 返回x*(2**i)的值
) |5 [% m& L: _# v A- #返回x*(2**i)的值: M. L8 w& Y0 u- e
- ldexp(x, i)3 U, B( z* C1 U |7 F
- Return x * (2**i). F# j1 P) I3 H8 z/ P- Y
- >>> math.ldexp(5,5)7 m; y1 d- _/ N
- 160.0
6 w b. T! U4 X/ ?; E9 W& l - >>> math.ldexp(3,5)
5 ~1 ]3 d A5 x+ a- j/ d - 96.0
复制代码 / l+ X& I9 a" Y& H! m9 q5 r4 ]
math.log10(x) 返回x的以10为底的对数
P+ i3 T' _. `7 } T- #返回x的以10为底的对数 l+ n: _( c& r0 @
- log10(x)
0 j) K# p$ W* P. @6 L - Return the base 10 logarithm of x." d, m- T! o, v$ Y" t1 N
- >>> math.log10(10); W; ^8 C& d% P6 q: ~$ a
- 1.0. C- A+ ^" E% m/ b1 b6 u: {! {
- >>> math.log10(100)& M; z3 C1 ~. M
- 2.0* A- f5 {$ `8 A/ F
- #即10的1.3次方的结果为20( A3 B e+ {5 z5 x/ B
- >>> math.log10(20)
9 _3 O9 q; B" L5 o8 C4 { - 1.3010299956639813
复制代码 ; i) u( ^3 t" ~! z7 U) w; b
math.log1p(x) 返回x+1的自然对数(基数为e)的值
2 U( W1 x/ G% w- #返回x+1的自然对数(基数为e)的值7 l) G+ p6 A& ^3 y
- log1p(x): [# i; I* ] x3 M6 H. g8 F6 g
- Return the natural logarithm of 1+x (base e).
9 A! n6 B6 t8 _8 u0 `& g" W - The result is computed in a way which is accurate for x near zero.
: ~; ~! X( \; w/ E4 P - >>> math.log(10)
. t- G. g/ \2 L9 h8 e! U - 2.302585092994046
1 s, v7 P) c: Q2 \2 l( F! j - >>> math.log1p(10)% Z+ O9 x/ |6 N+ T7 F
- 2.3978952727983707
# u* I- S2 u8 P3 [ - >>> math.log(11)1 A0 [' \6 U4 f6 ?2 T0 M/ o
- 2.3978952727983707
复制代码 * u- x. s- k: A8 g3 M- U
math.log2(x) 返回x的基2对数
' a1 @# [6 o( @; c( O! `# s- #返回x的基2对数
0 B Q$ J0 X" ~ - log2(x)
/ t4 ]/ S- z c" u - Return the base 2 logarithm of x.; r% n4 S6 U r# b" ^. K L( A6 h. O
- >>> math.log2(32)8 j! S' J" r# d1 c2 V" H( z
- 5.0" g$ P5 P. C3 ^/ j- F
- >>> math.log2(20)" @5 n8 w5 Q8 s% d0 w" }; R
- 4.321928094887363
: ^* `# t7 d% T0 s6 { - >>> math.log2(16)
8 W( a) _" K# l% ?- a- M" r( D! K - 4.0
复制代码
" G, [$ h( v9 s4 h4 m+ B( a# imath.modf(x) 返回由x的小数部分和整数部分组成的元组) p: \. G4 w2 n8 R1 ?/ ?/ N
- #返回由x的小数部分和整数部分组成的元组2 j8 W* ^, m$ n6 ~. e3 l3 Q
- modf(x). h7 u2 K6 c! [+ r, x4 [- D' f
- Return the fractional and integer parts of x. Both results carry the sign
6 b1 ^2 ^7 A6 Z Z - of x and are floats.
/ F; b* X( B B5 t8 J2 U* W - >>> math.modf(math.pi)7 B8 z, _7 X8 U y+ i
- (0.14159265358979312, 3.0). X6 t4 C. S* Y8 P
- >>> math.modf(12.34)9 o, }4 J. `* U, n$ i8 v
- (0.33999999999999986, 12.0)
复制代码 * z" J' L+ a) w
math.sqrt(x) 求x的平方根% _$ X" H( W7 I1 Y i; g
- #求x的平方根" z! c: A9 L( ^" Q$ w
- sqrt(x)- |5 b. C4 ^0 o# r. U9 \
- Return the square root of x., H% T G! A% _- r" S- p8 E, v/ B
- >>> math.sqrt(100)
* M2 h0 l" ?2 J; c# i+ U - 10.0
9 @6 L5 U% N3 A+ G - >>> math.sqrt(16)& {, g( y" W# f$ I* {8 V6 ~5 N
- 4.0
! m" e9 D1 C1 E - >>> math.sqrt(20)" q0 ]7 B6 O$ h. C _2 O
- 4.47213595499958
复制代码
% X( V' ]) I* }2 r' Tmath.trunc(x) 返回x的整数部分- #返回x的整数部分
+ D/ s' P4 p) `+ P% L" k' |2 G - trunc(x:Real) -> Integral% o$ t+ U: {4 a S2 j
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ y+ }4 G# N: p p
- >>> math.trunc(6.789)) G# \' D: K5 U7 `1 |! k' B
- 6 @) ^# Y7 N: a2 [4 L; G
- >>> math.trunc(math.pi)- s& Z% y, z g8 a
- 3; I6 U. D9 E( H. G9 @3 B7 ~! N& t ?
- >>> math.trunc(2.567)
F0 k/ U8 b; m1 L! m" m3 Z v. a - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|