马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 ~1 Z/ f; T* b5 E1 u* e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。/ A- r: u* U5 C: ~" E! A. V
6 V& G% a+ ` K. ~9 N
方法1:
+ ^* S3 I X& C/ Z8 f3 r- >>> import math0 i v. G: q0 `1 F
- >>> math.sqrt(9)# {% t! J( C2 t/ C `
- 3.0
复制代码 方法2:# Q. i- K1 n. {/ ~
- >>> from math import sqrt, P4 x# m( t" a
- >>> sqrt(9)
0 k. l8 J% @+ t9 G. B3 U) { - 3.0
复制代码
, h: }# M2 {* |# Q) H
) u' o7 X4 E0 e% ymath.e 表示一个常量
: c: x) \7 R, A5 y7 f- #表示一个常量# n" B( |- m9 u2 z
- >>> math.e. W. B I6 e) P! _0 X& P" L/ _+ s
- 2.718281828459045
复制代码 u; q7 Q" G% ~* `
math.pi 数字常量,圆周率8 H7 n- U$ u3 K& K$ b7 C/ \3 L
- #数字常量,圆周率
- g6 c) I- k, x4 P. d4 e9 y - >>> print(math.pi)5 M' i4 M! J0 U. `5 N+ \
- 3.141592653589793
复制代码 # }, s8 m" v. H
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 G! Z( w+ [. L- #取大于等于x的最小的整数值,如果x是一个整数,则返回x- g1 E( Z; N' i1 L" \
- ceil(x)
8 G! r. H5 a- R2 G" y/ f0 D! O; p - Return the ceiling of x as an int.+ {- D+ }6 x) `
- This is the smallest integral value >= x.9 R2 P6 |2 B: q! ^. L) b
3 T. W6 d! O. ~- Z- F ?) v8 Y- >>> math.ceil(4.01)
# Y; ?) e, h8 c8 N( L7 _6 O' O+ I - 50 q) ]/ u; @: t) E a1 ~( \4 c
- >>> math.ceil(4.99)& b1 F3 ?, n. e) r, t* ], K
- 55 {" M9 ?$ E+ {% W9 ]
- >>> math.ceil(-3.99) s3 Y6 ]% u& G( P( h
- -31 _( f3 ?1 f5 j) O% e$ w
- >>> math.ceil(-3.01)
0 x1 I1 e5 y- T - -3
复制代码 / b6 {2 |; ^/ f) V- {
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" R3 {/ f1 m& G$ a E4 A! Z- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 Q0 J2 M5 X3 I( G5 @
- floor(x)- d2 E" \& C/ ~! \ V- _& Q3 |
- Return the floor of x as an int.; k( w4 M6 d' @* l \
- This is the largest integral value <= x.* n& f# w: O A
- >>> math.floor(4.1)
+ y, |( @* b4 ^. c% w+ F - 4
- l% q7 ]1 Q$ e - >>> math.floor(4.999)5 x3 U7 ?# Z5 ~ A# C: q' A
- 4
9 q( Q+ q; B5 ^4 ]8 S1 C - >>> math.floor(-4.999)1 N! y( v( z1 T1 {
- -5/ f0 H$ E* r7 u: o
- >>> math.floor(-4.01)
9 R) c0 i% S/ u3 ~# [" n - -5
复制代码 3 n& g- a* d' u0 u: p
math.pow(x,y) 返回x的y次方,即x**y
; m' w" K" j; Z- t9 i- #返回x的y次方,即x**y
" ?/ r) x y7 D6 D% H- | - pow(x, y)" ~) a" C- T. L" x' A8 w
- Return x**y (x to the power of y).1 n6 p0 P8 c$ G8 ^
- >>> math.pow(3,4)$ L2 ^* d; O8 |$ b
- 81.0; _6 {; h1 Q9 x+ a
- >>>
: X0 t* B) e$ u, |% C( H - >>> math.pow(2,7)+ k3 L! w3 N- B+ ]0 o
- 128.0
复制代码 " A' c, }8 r5 }7 Q8 s! F0 ^7 q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)& k/ V3 Y: r) y- A
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 |/ d7 y( W- i* P! X
- log(x[, base])7 ~0 }. @. ]4 W1 v% q% K
- Return the logarithm of x to the given base.
! X/ s. T9 z; i4 a A& r - If the base not specified, returns the natural logarithm (base e) of x.
( u! t' ]. j. h8 U - >>> math.log(10)
& l+ r0 C3 f" N: K; _( S - 2.3025850929940464 ]' `7 \ y+ W; G
- >>> math.log(11)
K* y; e" z" }" Q! q3 k& N* } - 2.39789527279837078 C# }) [5 ^ c. S7 h; N8 b
- >>> math.log(20)
8 F) P9 _/ x) v/ _ - 2.995732273553991
复制代码 - x V- f+ x* ~- X2 V% [0 k
math.sin(x) 求x(x为弧度)的正弦值0 Z- N: } S) M5 \3 r% @* B! j
- #求x(x为弧度)的正弦值( A; Y! G- w6 }: d" w) y
- sin(x)7 U. _- B9 G" M, q& y
- Return the sine of x (measured in radians).
# s/ i& y# p8 E' Y- ^1 N5 P - >>> math.sin(math.pi/4) ?: y% F* t/ }9 }. \, r
- 0.70710678118654757 R8 v4 {8 }3 g
- >>> math.sin(math.pi/2)
( F! [! A/ N6 P; W; E - 1.0
) v. N) b+ e% R2 j A - >>> math.sin(math.pi/3)( u6 a1 |$ r( j' p9 x
- 0.8660254037844386
复制代码
$ D2 q6 n2 J; @$ p8 X, L/ \math.cos(x) 求x的余弦,x必须是弧度
- J7 C6 R3 Z. \, G2 ^5 Z- #求x的余弦,x必须是弧度
5 B" d, j- v$ u - cos(x)" F( x6 y( ]( T1 h5 y* r3 t
- Return the cosine of x (measured in radians).
! E4 h& L3 p* i0 Q( T7 w% s - #math.pi/4表示弧度,转换成角度为45度
' s# \% u0 w0 k7 ~- Q! k a - >>> math.cos(math.pi/4)
) M5 h M; J# ~( Y: O. I - 0.7071067811865476 u" ~5 l, Z! f2 Q+ b
- math.pi/3表示弧度,转换成角度为60度- Q5 f/ V5 t% o2 j
- >>> math.cos(math.pi/3)- Z. Q( K- ~* K' Y& g
- 0.5000000000000001
" K6 B& E6 g6 n' q9 g - math.pi/6表示弧度,转换成角度为30度7 j& W5 M \0 z# B! p( e+ |, `
- >>> math.cos(math.pi/6)+ O$ t' f, U1 Z
- 0.8660254037844387
复制代码 6 O+ h; p* p/ h8 o c, b2 W
math.tan(x) 返回x(x为弧度)的正切值
5 f' b* r' z" i: P' H! i6 u- #返回x(x为弧度)的正切值
( J9 c# x: u5 r* d/ H U - tan(x)1 }$ H! n* z( U Q1 S
- Return the tangent of x (measured in radians).
^) @# i7 E# v/ D - >>> math.tan(math.pi/4)
6 ^4 p! I1 d: z' x0 a4 Q - 0.9999999999999999
b+ M8 i2 @6 e/ t4 q - >>> math.tan(math.pi/6)
) u6 U* B# d* ?& ^4 ]3 o# W - 0.57735026918962575 ~& u3 _& l/ K6 N6 W
- >>> math.tan(math.pi/3)
; l( R5 L8 F; Q7 W - 1.7320508075688767
复制代码
( _ v$ ]* f! |; L3 Fmath.degrees(x) 把x从弧度转换成角度
+ x5 r, b0 ?2 [( ~8 H; h- #把x从弧度转换成角度
6 L: ]# h# v$ v: C2 i - degrees(x)! `! ~) Y3 U$ ~
- Convert angle x from radians to degrees., t# i, U) r# [9 B
% e3 P1 \% \& h& x1 D* C# f$ ~0 J- >>> math.degrees(math.pi/4)
: z' s* l0 n5 d- W+ O# \+ D - 45.0
, V* e2 O7 l0 N, V& K - >>> math.degrees(math.pi)
+ Q2 O$ K: U2 w# {. p$ t - 180.0
T2 s4 G3 i8 Y0 q - >>> math.degrees(math.pi/6)
2 I7 H3 A. ]# _/ O8 B: T - 29.999999999999996
6 X: I4 R7 d5 e( L; \: f - >>> math.degrees(math.pi/3)5 c6 [8 z* a9 G7 f% R7 ^ K
- 59.99999999999999
复制代码 / I* N& J4 ]! A; q
math.radians(x) 把角度x转换成弧度
+ G& U- W; k+ n- #把角度x转换成弧度
* r" r0 X/ p& J8 ^( g/ J: g - radians(x)* X! j" `+ X1 A5 ~
- Convert angle x from degrees to radians.9 w1 q$ b6 F! ^) e: a) [1 V
- >>> math.radians(45). U* W+ @0 N8 v G5 I
- 0.78539816339744836 w8 F- n# t* i+ g- }3 R3 u
- >>> math.radians(60)' Q! B3 K' Z8 p! p1 Z3 ]
- 1.0471975511965976
复制代码 4 r& [, `# k2 G. x7 M
math.copysign(x,y) 把y的正负号加到x前面,可以使用04 u. g( R: \* j
- #把y的正负号加到x前面,可以使用0# P8 _5 K4 U! l2 J
- copysign(x, y)5 f3 v" d6 f* D; j- ^' M. Z' `
- Return a float with the magnitude (absolute value) of x but the sign
; y: m9 h W" B$ ^4 Y' n: D - of y. On platforms that support signed zeros, copysign(1.0, -0.0) + p' N0 r0 a: Y
- returns -1.0.: T* I: k8 \2 s& u! p) I% R$ f
+ T5 Q* T! ?7 x- >>> math.copysign(2,3)
0 M$ O. @+ Q% { }& Q0 _; {0 Y - 2.0
; H: C" ]6 ~( p - >>> math.copysign(2,-3)
( w. `, V" e6 F( N - -2.0
' i* J& q/ I7 C8 C, Q J - >>> math.copysign(3,8)
6 F7 t0 E% |, I7 n - 3.03 ]8 |- G. r) h0 f0 [
- >>> math.copysign(3,-8): q9 A/ g3 I7 E7 ~
- -3.0
复制代码 : A) W W" E, \1 _* B
math.exp(x) 返回math.e,也就是2.71828的x次方: t: n* x8 A& F$ } ^: \; s+ m
- #返回math.e,也就是2.71828的x次方0 Q. h. a, ]( S+ n3 O/ x
- exp(x)# O0 h0 ]% A" G# t) A& S m* g) K
- Return e raised to the power of x.
; T& J; M3 u$ V4 d, a7 P0 ?
& R. k0 F: W: [( d6 l* W- >>> math.exp(1)
& }6 n; D" ~( _, Z1 Q# ` - 2.718281828459045 H* e" u. u$ ~ |
- >>> math.exp(2)
. C5 d1 o* _* N% i8 y - 7.38905609893065& `- S+ q5 Z9 ^. B( k
- >>> math.exp(3)
$ |0 ^3 v: ~, Y P$ q: ^' [2 {" e - 20.085536923187668
复制代码 ' |3 X7 T! @( w- J
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减10 H, }- j9 E5 Z0 i
- #返回math.e的x(其值为2.71828)次方的值减1( _4 U) {' b& F
- expm1(x)
. h8 N- _6 Z3 v- ~* c# u p - Return exp(x)-1.$ t6 I% K) G7 f2 a" a+ p+ a) O* \: d
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ t# B) u* b2 `: Z3 l4 I: L2 s
+ N4 I' J. W7 P" f& u6 z$ M" j$ i- >>> math.expm1(1)
& ~. l9 b% G& g0 r8 r2 W, \ - 1.718281828459045# O% a7 I) J" j9 r0 q5 y
- >>> math.expm1(2)8 ~$ [) N" C; @6 Q0 k: ^8 m. H+ _, N
- 6.38905609893065. E5 H9 R3 ^/ h# p5 |& A
- >>> math.expm1(3)$ `9 d8 h- M( e" A* n7 m4 o
- 19.085536923187668
复制代码 4 q9 x3 G& h6 ~" y9 Q- `8 `+ k8 z1 \
math.fabs(x) 返回x的绝对值7 _# P! O) O9 K4 }5 S" f( Q
- #返回x的绝对值9 S: R9 g4 H' `
- fabs(x)
. f$ E5 X4 | Z7 A8 \2 f7 P& k# `$ c) A - Return the absolute value of the float x.
0 W! Q1 ?1 i+ N8 z9 y - * `; Y( ]% q/ M# @/ @% p) S
- >>> math.fabs(-0.003)
/ j3 M- J) v' D - 0.003, {4 n3 e3 V' l& K* \
- >>> math.fabs(-110)
" ?% I7 \# g; j - 110.01 d& _2 w' K1 U# Z
- >>> math.fabs(100)! Q# V# R% X+ U& _* ~
- 100.0
复制代码 # k, Y5 }" b! R. t
math.factorial(x) 取x的阶乘的值
' e2 i* V' t9 P! I/ n/ U- #取x的阶乘的值
( H3 Z4 a2 V0 i - factorial(x) -> Integral- Y8 B4 \0 D# n" l
- Find x!. Raise a ValueError if x is negative or non-integral.
# p8 f$ y+ w: x: u J& N: N - >>> math.factorial(1)' a. }: I) @" l, c
- 1
4 X! \( K5 V' W. Q - >>> math.factorial(2)) d4 R0 b) M- X& y' L' {: v
- 2) f9 r- J4 `. ^8 g; s& V/ m5 H+ k a) ~
- >>> math.factorial(3)
4 M# _" i: N5 |4 I+ q) m1 x - 63 C+ y+ ]0 k# t2 P# a
- >>> math.factorial(5)& V, `2 Z! E, [
- 1205 A# N J6 J- a$ c6 R4 r
- >>> math.factorial(10)8 b/ U v2 o5 a1 f8 d9 K
- 3628800
复制代码 1 P* i$ A; v1 R& o+ ?1 q
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
. _/ q4 d3 B3 @4 Y3 m- #得到x/y的余数,其值是一个浮点数* z( Z0 a# |- S/ X
- fmod(x, y)
" _2 O" f! D* A8 L( Z/ l( G6 X - Return fmod(x, y), according to platform C. x % y may differ.
$ c$ b' P4 L8 X) a5 d - >>> math.fmod(20,3)
6 H, V4 ?) F0 r( |/ p! K' a" J - 2.0" T) J1 y$ N) {+ U( W& D
- >>> math.fmod(20,7)
7 I& P% p- k2 Z - 6.0
复制代码
6 X/ [: b1 p* t6 P! ?# j( c( Nmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 c9 P* E% B+ x) O, u, Q
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ F; m! ~ s$ b
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
/ b& r4 Z8 u+ G2 h0 ^4 c' \% J% a9 | - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" \3 V/ ?% {7 d; R! w5 R
- frexp(x)
; o: R" n, V* q; M2 o$ l* t) m - Return the mantissa and exponent of x, as pair (m, e).
+ Q" D! [- B7 o0 R. I - m is a float and e is an int, such that x = m * 2.**e.2 P) _3 J" D. L' |4 _: n& F
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.9 r5 q4 @4 b5 _% P
- >>> math.frexp(10)
4 }+ g T( s/ ?1 o% e- j, [ - (0.625, 4)
" l/ a( j, w4 D7 U" B, H - >>> math.frexp(75)
6 C& |; [, D9 c3 r4 ? X0 ~ - (0.5859375, 7)) M* S7 W, X9 y" y( c9 L+ c. y
- >>> math.frexp(-40)# T$ g. f6 s8 j/ I: ^
- (-0.625, 6)
! q w7 j4 w$ n- ^ - >>> math.frexp(-100)
$ ?& g* X# R7 T4 M4 j; x0 K% [ - (-0.78125, 7)
9 i1 S" T* _# p7 b* J - >>> math.frexp(100)4 o5 `1 g% S" Q- p( u
- (0.78125, 7)
复制代码
1 A. j0 K2 B1 |1 @( c, l, k1 pmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
" U3 Y) }' }( a# @- #对迭代器里的每个元素进行求和操作
( H) c7 w& L9 X2 A0 V8 D - fsum(iterable)! Z- K6 m7 u& B
- Return an accurate floating point sum of values in the iterable.% U% t# r1 S: J3 ^0 L4 h1 S
- Assumes IEEE-754 floating point arithmetic.
8 {. Y% G0 C- W6 y, N5 D - >>> math.fsum([1,2,3,4])
, a( J8 t6 C" w0 h3 } - 10.0
& V2 o$ U: B: G2 c - >>> math.fsum((1,2,3,4))2 S d; ~0 C6 x" {1 c$ ~, \
- 10.0
/ d/ W x9 r& t0 d1 x - >>> math.fsum((-1,-2,-3,-4))
7 Q7 g1 p1 g( G3 N - -10.03 h. q* j. O$ J& F, |9 |. B
- >>> math.fsum([-1,-2,-3,-4])
/ M( j8 z) B; a: x5 R4 O - -10.0
复制代码 : Z+ O4 G* C! l0 A3 [
math.gcd(x,y) 返回x和y的最大公约数: V" Q' y- h- P# h2 Z h; C
- #返回x和y的最大公约数/ f# d- ~! T/ h8 B" N( C
- gcd(x, y) -> int
0 @5 V4 ?* b3 q% k - greatest common divisor of x and y
( n; v7 Y: D& U - >>> math.gcd(8,6)- w6 b- ^' C0 g1 y, {4 X# b; G
- 2& r/ a& N- ~& J
- >>> math.gcd(40,20)
+ {7 M5 ~4 k" R0 V% k: K3 ] - 20
( ]8 f& t- j) B% a: O0 e1 Z8 R - >>> math.gcd(8,12)0 y Y& [: _ ~8 `7 K0 X! O
- 4
复制代码
! _ B2 w- @" Q4 C8 L! Mmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
7 [- m) n! J- d" v- #得到(x**2+y**2),平方的值1 t" N5 h6 y8 Z, W
- hypot(x, y)# L6 b. \% [7 W7 H& h( G6 S t
- Return the Euclidean distance, sqrt(x*x + y*y).
, R ?3 x9 Z' Q: w. H9 d - >>> math.hypot(3,4)
0 ~, T. `+ J( U9 F5 u) _" N - 5.06 K1 a) R0 Z; ?4 c2 w5 O
- >>> math.hypot(6,8)3 H" \; K% j. S7 ^4 j( p
- 10.0
复制代码 7 ?) _( E$ U; K
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
8 H( ~- |- y. `- #如果x是不是无穷大的数字,则返回True,否则返回False9 y* P$ x) }1 S# Q' M* N7 M
- isfinite(x) -> bool
" E7 c! W- G9 e l9 G% j4 V - Return True if x is neither an infinity nor a NaN, and False otherwise.
! Q1 `* k1 y4 ]9 U3 Q8 |& k; t' q. K( v - >>> math.isfinite(100): Y6 Z3 }( ?5 L* G
- True0 N! a( |3 M' f0 y; C
- >>> math.isfinite(0)) N2 g1 {* ^! H
- True
0 z4 k' \0 y! a' K- D ]! Y8 y9 H - >>> math.isfinite(0.1)( L* K( B- t( L9 _
- True
$ p! y: ~1 O9 F - >>> math.isfinite("a")
8 a- I! p5 t) {( @" E8 b - >>> math.isfinite(0.0001)
1 ~1 R: E; {) {6 T9 K - True
复制代码
- {% Q. }) G& _: \math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
; `) X. } F; a6 a. ]0 Z8 m- #如果x是正无穷大或负无穷大,则返回True,否则返回False1 O3 c8 \$ G* i6 Z! W& O
- isinf(x) -> bool
" S# Q( R! z; O - Return True if x is a positive or negative infinity, and False otherwise.
$ f5 v" f6 Q4 C$ w# U; d- \8 A - >>> math.isinf(234)
' L, v r& x9 ?! G$ ?% m( ? - False+ G# b1 j, l( U5 ^
- >>> math.isinf(0.1)6 p( c$ @9 M$ S4 x$ L
- False
复制代码 ( [' S8 `' F6 ?4 b. d+ H* o4 {
math.isnan(x) 如果x不是数字True,否则返回False
" ^8 P) j% a8 g" ?- #如果x不是数字True,否则返回False( P( p' a" n0 F6 d
- isnan(x) -> bool3 u% M3 q y( d; B! W
- Return True if x is a NaN (not a number), and False otherwise.% v0 r9 A% ]+ P& G( V1 `$ F
- >>> math.isnan(23): V* O+ e8 o6 Q/ S3 d% a! O
- False
5 S. | S. |+ ]+ ~: z" g& w! w - >>> math.isnan(0.01)
: a2 L d& [" X" | - False
复制代码
7 Z5 \- i1 D Q; U+ amath.ldexp(x,i) 返回x*(2**i)的值
9 y% ?% A0 ]/ F$ w, e- #返回x*(2**i)的值
) h$ C, l1 K8 E4 d, i; { - ldexp(x, i)6 z g9 Q' d" K! i5 [
- Return x * (2**i).
' E9 G0 D. d* z4 b" z - >>> math.ldexp(5,5)& i, p( f) c- j3 x2 p
- 160.0! E4 X9 c9 D( e- K
- >>> math.ldexp(3,5)' i! q7 m* { c
- 96.0
复制代码
4 N: i! O: b6 s* B: G! I8 qmath.log10(x) 返回x的以10为底的对数
4 t7 ]* x" V! c3 o' |- #返回x的以10为底的对数
5 H' s0 A3 T b/ S* z1 ^ - log10(x)
+ y G$ V+ n2 k% r7 L5 L! N - Return the base 10 logarithm of x.
5 S: C! G$ ]" ]$ l - >>> math.log10(10)- R, `6 j; t7 z% v8 C: n
- 1.00 C$ e: R" n$ s6 ~" f! s) M8 Q
- >>> math.log10(100)& p4 d6 O; q+ z' F
- 2.03 ~/ l. m) u8 z0 l# P! e/ C
- #即10的1.3次方的结果为20
. t# h# z9 J" d - >>> math.log10(20)
. H) K3 ?, H, m, S ` - 1.3010299956639813
复制代码 9 n( V) f, P+ J
math.log1p(x) 返回x+1的自然对数(基数为e)的值
% j' [/ m% {5 }% H1 ~! D* U% v- #返回x+1的自然对数(基数为e)的值
6 C s4 ^( Y5 x4 [! W6 q. n5 p - log1p(x)
4 N: n2 C. H, @1 N - Return the natural logarithm of 1+x (base e)." h) z+ U% Z5 z6 T! j
- The result is computed in a way which is accurate for x near zero.
* _4 u% \6 n p( i - >>> math.log(10)
9 {% b0 w7 i) u; R - 2.302585092994046
; ]* K8 @: Y5 M - >>> math.log1p(10)
# v8 U" c- k# A3 z% m$ v - 2.39789527279837076 V# H/ d) y6 h1 J3 E! g* Q
- >>> math.log(11)
% W9 p; M0 R. L6 T1 H - 2.3978952727983707
复制代码 % ~( G+ z+ O6 ]/ f5 n$ j% b
math.log2(x) 返回x的基2对数: K- Y+ F( o @% l0 N. L3 C5 X
- #返回x的基2对数
2 R6 }& Z7 c4 S! w& j - log2(x)
+ l: a9 C! i& ^% s9 A* d - Return the base 2 logarithm of x.
$ J ?: |; K0 O( @4 | - >>> math.log2(32): l, t% Q, P0 K2 Y6 j$ y
- 5.03 [- h/ w1 O- d- e
- >>> math.log2(20)
/ r/ P, I- P9 z( U8 B: F0 R - 4.3219280948873632 g. q- T+ `" l* G( f
- >>> math.log2(16)
9 Y) k0 ^$ Y5 J0 R! d2 H - 4.0
复制代码
$ L* I& j. D X; o4 Umath.modf(x) 返回由x的小数部分和整数部分组成的元组
; |, O! \3 h+ h2 r B# o6 [% L- #返回由x的小数部分和整数部分组成的元组0 `# i' J; V m, G# ?
- modf(x)
, M; T. T6 c0 E+ K4 `0 ` - Return the fractional and integer parts of x. Both results carry the sign
9 G' s! E8 ?4 Y1 _8 P% Q - of x and are floats.( P( V* z E) `% X5 y. `, C
- >>> math.modf(math.pi)3 F' S$ ^; }: a
- (0.14159265358979312, 3.0)& I/ T4 e6 c1 @4 Z# Z/ a" ~
- >>> math.modf(12.34)$ b$ X4 \0 h1 K3 w( t& ~$ D
- (0.33999999999999986, 12.0)
复制代码 9 o1 A& a4 ~* p& h( |
math.sqrt(x) 求x的平方根+ b6 ]5 V; ]* n _: R% z6 E9 N
- #求x的平方根
$ x) W$ F. ?. U( s; s% y, M - sqrt(x)8 ]: D! k3 k7 Y4 i+ H' h" c/ x
- Return the square root of x.
2 }/ k9 l" z" F1 d6 M3 B1 l \ - >>> math.sqrt(100)
: @( v2 G% h" ^# J" |/ @ - 10.06 T7 e7 @& N0 H8 r' g8 X
- >>> math.sqrt(16)7 S# y: I$ z7 A/ [$ q( e. A& U
- 4.01 X4 u8 ]& D- y7 I1 o
- >>> math.sqrt(20)7 d* j# ~. c/ a; r. X% ~2 |5 L
- 4.47213595499958
复制代码 , T# E5 J: x2 o
math.trunc(x) 返回x的整数部分- #返回x的整数部分+ h V8 E( a8 e. U: y7 d5 q
- trunc(x:Real) -> Integral
- n, E, Y; _' w) z$ K - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
. G8 i+ Z' e$ H - >>> math.trunc(6.789)
; ?$ A* F3 C! H8 u5 A/ o6 s - 6
I' m" Q8 t0 \ - >>> math.trunc(math.pi)) D5 [1 `" w- k
- 37 X5 b# ?9 f) { k# ^
- >>> math.trunc(2.567)- x/ ]& [$ h* J' L% z
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|