马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 Q6 o. g/ \; b1 ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。" {/ h9 k3 t6 y* x% [
) Y' r+ ]) F, ?# @( ^2 V) c% E
方法1:% o, w5 R: n# F( s
- >>> import math6 Y1 i Y X+ I% I9 r- G1 C
- >>> math.sqrt(9)$ [" q! |+ |( a7 E/ P( i
- 3.0
复制代码 方法2:
; F" w7 ]" |; ^$ J, P- >>> from math import sqrt
, {0 D4 D; ~% b. S. { - >>> sqrt(9)
* t+ p8 d0 ~8 a1 n! m" V' ` - 3.0
复制代码 ; I, ` Z6 Q# c' ]( L) U8 T
+ f- x# P( ]' X1 M( m% i4 G2 emath.e 表示一个常量
) T, [- r7 N* E- #表示一个常量
1 ~) W" \! _4 N - >>> math.e' U* k$ A6 L; Z4 O
- 2.718281828459045
复制代码 9 v$ j0 ?1 h y
math.pi 数字常量,圆周率* q: X. F0 i3 g$ L
- #数字常量,圆周率/ b5 P+ y. [3 W
- >>> print(math.pi)1 }* m- A; Y7 s( E; \6 I
- 3.141592653589793
复制代码 1 T* ~5 c% t! o! V' Z ~6 s1 ^# s
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
/ p/ d2 W( t" G: D, |3 G% J6 j1 {- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
7 J0 \; r' G/ I* B" a$ X - ceil(x)! y/ n: Z( ~( b+ s# u
- Return the ceiling of x as an int.
( p$ U! b- i9 B( K - This is the smallest integral value >= x.1 r. l2 O0 G+ x5 d2 e- L6 r5 _
) ? Q* `- U: U- @- i- >>> math.ceil(4.01)
0 A- Y. l9 _7 Z' v0 i - 5
% F. l/ c5 W1 I k5 d- \8 K7 d7 t - >>> math.ceil(4.99)
7 p. t4 ?% u% V9 G2 M* q5 } - 5
5 S! p F1 a3 s# t L - >>> math.ceil(-3.99)
$ D& r! p1 M" g. ?& D6 s - -3 H! \; F2 D( \' }
- >>> math.ceil(-3.01)
# q1 e1 H! M, |9 M4 }3 Z - -3
复制代码 7 R# ?! e% F' J4 Q
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
- D5 A4 u( v; L7 ~$ ?- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) J/ t% R6 W; C8 x+ J
- floor(x)' P5 B+ z8 l9 b M* E8 |
- Return the floor of x as an int.9 j5 x' @3 g+ I8 \) W
- This is the largest integral value <= x.
3 _. P! ^. n, U8 c - >>> math.floor(4.1) Y4 ^' Z1 r& m( C3 O
- 4
6 D. s% @) M0 S! e$ j - >>> math.floor(4.999)' V; c1 z6 c( {/ h4 r
- 4
* }! O) y( X" J2 ^6 | - >>> math.floor(-4.999)& F9 w) f+ _6 E( C$ K. x
- -5" G" k# Q; h. T' C( w) c
- >>> math.floor(-4.01)* `; A% S/ [ e
- -5
复制代码 * y! p' J( [1 |, f. l
math.pow(x,y) 返回x的y次方,即x**y3 F0 \/ {# `7 h3 _' y5 \5 Q
- #返回x的y次方,即x**y
: C5 ]3 C" c' d z) @' p1 L% ? - pow(x, y)
; p; L% F* {8 n/ F2 B - Return x**y (x to the power of y).0 T3 v+ f0 x9 y2 G. n8 H8 B& U
- >>> math.pow(3,4)6 I; o( r3 s. i
- 81.0
1 x7 c7 o) L2 N0 q - >>>
1 f- x/ }% D0 R; D, z& u7 P6 w - >>> math.pow(2,7)
- t: u1 B7 n1 g2 @) B3 \ - 128.0
复制代码
* s5 N: g% @& U9 l1 `& s& Rmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# t' h/ z. G8 Q# G/ g1 [
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
+ z1 P. l( `5 E3 A - log(x[, base])* D2 O8 n5 y/ ]( A0 T# W3 k) `
- Return the logarithm of x to the given base., N+ q% s, B% p! z3 t
- If the base not specified, returns the natural logarithm (base e) of x.: q& V- B) ~2 h9 r0 h; G
- >>> math.log(10); d" a2 r3 ~$ e( b* Q* e2 B9 l( P
- 2.302585092994046
! g3 c0 o' g4 B! Q$ z; o6 E+ \ - >>> math.log(11)8 g4 Y. h, V3 l/ d* _$ U: Y7 _
- 2.39789527279837078 m+ [8 q0 m9 i" [* X
- >>> math.log(20). R& u5 V( V0 p+ F" G( U
- 2.995732273553991
复制代码 / K2 t. I1 k. Q) w; h+ u( O3 T9 J
math.sin(x) 求x(x为弧度)的正弦值
# z/ L& \2 J# s& d7 u1 U/ w" Q! ^- #求x(x为弧度)的正弦值
) ~6 _" L: i/ O8 ~. Q2 P8 ^ - sin(x)0 F+ s1 n; i% \3 D$ |, y
- Return the sine of x (measured in radians).& h2 H# p- \5 Y7 V7 k/ n8 x2 H
- >>> math.sin(math.pi/4)* Q" t- `& W, ^, h
- 0.7071067811865475
9 g- q7 q( |% O3 J% r - >>> math.sin(math.pi/2)9 M5 L, {" f3 m
- 1.0& j5 Y2 b! f! ?) T6 T
- >>> math.sin(math.pi/3)
& z( I: C; ?; @/ k$ j: q" ?/ H - 0.8660254037844386
复制代码 , J7 N- f/ [1 i) q. w
math.cos(x) 求x的余弦,x必须是弧度2 r+ L* {1 O* K# T4 U, }
- #求x的余弦,x必须是弧度' Y( I( B1 m' e7 Z+ P, e$ s
- cos(x)
3 q" ^& y3 {7 f* U6 v1 w4 F - Return the cosine of x (measured in radians)./ D/ }- P( \0 H2 j: w" B( D$ X
- #math.pi/4表示弧度,转换成角度为45度) s- |! R$ _ Q. `2 h8 W
- >>> math.cos(math.pi/4)
T( J3 m9 s0 [5 W! Z3 X - 0.7071067811865476; R0 p, H" o# F' I/ U8 i
- math.pi/3表示弧度,转换成角度为60度7 W! ?/ O9 l1 q0 \/ h
- >>> math.cos(math.pi/3)
2 @, x6 ?7 [1 P - 0.50000000000000018 O8 E. o3 q$ f2 ?. I
- math.pi/6表示弧度,转换成角度为30度0 T/ F/ t6 X- A6 B; Q. L, Q
- >>> math.cos(math.pi/6)& `# i9 S% f' ?- j
- 0.8660254037844387
复制代码 1 ^4 N, t7 Q" U/ ^9 ~
math.tan(x) 返回x(x为弧度)的正切值 q" j4 _5 T0 O
- #返回x(x为弧度)的正切值1 K6 y1 u. D, O4 W- ?7 s: A1 j
- tan(x)+ ]% {0 `# p0 ^. Y0 [
- Return the tangent of x (measured in radians).
S" R2 N2 c) }9 p4 {) j! z/ e - >>> math.tan(math.pi/4)& h( C5 ^( [0 P' V' V& I0 F
- 0.9999999999999999
7 [+ v8 E% m* J) \: K1 K - >>> math.tan(math.pi/6)3 @6 i) y; U* O C, Y! T$ @4 z
- 0.5773502691896257
8 s+ z$ i) @' _ - >>> math.tan(math.pi/3)6 Y+ X( _- q6 N/ [+ K( d
- 1.7320508075688767
复制代码 # `9 }5 _" H: D5 Q0 E
math.degrees(x) 把x从弧度转换成角度
0 J: O- R! ^" a5 I: r9 K: q- #把x从弧度转换成角度5 Q% H1 d9 {& [
- degrees(x)
& S! z3 h: n+ ^( [# e" C1 s2 u - Convert angle x from radians to degrees.$ J- _5 t8 F8 I9 X/ i
. ^9 F' R9 R9 \- >>> math.degrees(math.pi/4)
* ?( E! X) ?5 U, B; h - 45.0
. p+ A7 w3 h) ^- m1 Z$ Z; B; ~ - >>> math.degrees(math.pi)! t9 L' P* [1 p7 \
- 180.0
/ V1 e- w/ X) D - >>> math.degrees(math.pi/6)
% N1 }7 P" a6 P - 29.999999999999996
0 q" b' Z5 w5 w3 _* k8 W - >>> math.degrees(math.pi/3)
, F: H' P/ L6 u6 [ - 59.99999999999999
复制代码
" I6 m7 Y: [, C3 umath.radians(x) 把角度x转换成弧度# X4 q$ N4 c& @7 k/ s8 O
- #把角度x转换成弧度* Q! D- h# t" G
- radians(x)+ B1 \; G* k9 ~3 m. N5 L- T# m# G
- Convert angle x from degrees to radians.( b( f! f W1 W; u2 U. z0 {) v' H( G
- >>> math.radians(45)" ^! @3 q/ a; R; m
- 0.78539816339744836 d6 l/ w% Q# F
- >>> math.radians(60)
; }+ _& l W2 p; v% u0 y$ { - 1.0471975511965976
复制代码 7 D3 o1 Y0 I+ W/ C) v) m: \; `
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
7 L% F, P5 E+ J9 x- #把y的正负号加到x前面,可以使用0
) x: C8 j$ ?) |+ V8 U - copysign(x, y)
8 F$ D% ]6 s2 x3 c/ I" T5 ?( j& l3 K - Return a float with the magnitude (absolute value) of x but the sign
9 ]* y. U o( _4 ~ - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 D: P! [ T+ J6 e" x2 ^/ n y! m
- returns -1.0./ y' V6 T3 V0 U# A
- N2 n+ B4 H! l5 F V- >>> math.copysign(2,3). \- @6 @, t2 [5 u5 d. d3 P
- 2.0, T9 r! B& U x+ v* O# O. L
- >>> math.copysign(2,-3), f1 S+ B# [' d) {8 Q. P. ~ m
- -2.0$ H& a. q. g2 L6 |8 j
- >>> math.copysign(3,8)# \0 h, o; C; l0 o
- 3.0# ^0 @$ F' ^1 S' ]
- >>> math.copysign(3,-8)' W- S# ]# _7 z2 P4 `
- -3.0
复制代码 % W4 s& v5 S" h, Q1 L' K7 I. a
math.exp(x) 返回math.e,也就是2.71828的x次方
; X+ E3 C, ~2 L- #返回math.e,也就是2.71828的x次方
* y- K, K! p- _ - exp(x)
$ X: }* {& [' k" | - Return e raised to the power of x.+ A; l( D0 h1 ~, |
- 2 A; p$ p& R& Z2 n* U# E
- >>> math.exp(1); Y! ]5 F# [' b5 o( ?' d% a
- 2.718281828459045
/ q& }* ?% U( s& K, G b - >>> math.exp(2)- m5 B& l9 @1 w, R1 y" ]# }- W
- 7.38905609893065
/ z( t. D+ k5 H6 L, K; E' y: U - >>> math.exp(3)
; z3 C7 q( @, J/ Y/ w% X9 S R( _ - 20.085536923187668
复制代码
8 w* O1 B4 v) R* bmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
( \ b- H8 `: B& q U0 ~- #返回math.e的x(其值为2.71828)次方的值减10 |( B' Q: ~- k9 f# S
- expm1(x): ^3 [) h5 @. R$ I
- Return exp(x)-1.4 v0 `# s0 U( v6 d) e/ _7 E" J ~
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" Q5 a# \ _/ e1 k5 _
|0 K# w9 T4 z- >>> math.expm1(1)
, A, |) t( f: l% E% I& D- u - 1.718281828459045: X* x Q& S E. u/ `7 Z
- >>> math.expm1(2)9 x7 T" i# s* Z1 e9 }# k
- 6.38905609893065
" W4 ] w& k; M1 t - >>> math.expm1(3)
; b7 p, m- Q* A0 Y; A - 19.085536923187668
复制代码
# X5 a9 f& D* H8 Y5 {3 `8 A3 c, Mmath.fabs(x) 返回x的绝对值# L- ?2 [" f' W
- #返回x的绝对值0 t3 X. p$ n) x2 M) F5 T! q
- fabs(x)9 Z/ S& a( P$ i4 G8 B5 Y/ B: e
- Return the absolute value of the float x.
1 g+ [" R" m1 ?" S" d - 6 E+ o; Y& H) a/ v
- >>> math.fabs(-0.003): S T6 S7 k5 P' `! U8 s7 d
- 0.0036 Q' A6 P$ h X* Y6 g
- >>> math.fabs(-110)8 {* D: B& c7 ]# s# \/ Y
- 110.0& S# f5 D5 y+ G2 ^0 H
- >>> math.fabs(100)
9 v, u) a# l) P; w' c: [% v - 100.0
复制代码 6 @& \2 A/ m0 A b2 K
math.factorial(x) 取x的阶乘的值4 `/ B& E5 ?8 n
- #取x的阶乘的值
. o5 r& c! R% F( ^! g3 N1 V - factorial(x) -> Integral2 j2 u" ^% g0 W- T: _9 X- V3 P4 k
- Find x!. Raise a ValueError if x is negative or non-integral.* g! N$ i3 z8 o/ B; r$ q* E6 T* p( v
- >>> math.factorial(1)8 K0 Z7 `1 y( B0 e
- 1 i- L; e' w+ {, u
- >>> math.factorial(2)
$ l* t' e! G2 E8 Q) i; | - 2
; s/ R% P' T# V9 H% @ - >>> math.factorial(3)4 r; q9 C0 i& G, C) C
- 6
; @9 a3 w) ^" E1 \- }1 M' t - >>> math.factorial(5)
b5 ]6 v) U/ d4 i1 j - 1204 Y$ a2 g+ N7 i
- >>> math.factorial(10)
/ {2 \ L4 S# A6 M3 M* i( T. C - 3628800
复制代码 ( J3 `4 A) R7 D, n
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
! r8 O! O( \6 P1 @9 q+ D6 p8 c- #得到x/y的余数,其值是一个浮点数, \! o @2 X& g2 m
- fmod(x, y)
9 ?: {6 t! O( v {# s. J2 x2 J4 A( V - Return fmod(x, y), according to platform C. x % y may differ.
[8 [% Y |- Y: z }5 X3 g; `5 I* F - >>> math.fmod(20,3)
& v2 U1 x( U: A. K - 2.0
* I' `! R5 l$ Y( y$ J# `% L - >>> math.fmod(20,7)5 ~ }- A6 `& r
- 6.0
复制代码 * ~" a S+ y) W; u B
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 w0 ?6 R& X- M0 {" a8 k% w: X- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 F. |) Q5 C- Q2 D! y
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
) I4 g" j, k* w$ J- M - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
; j1 d% g+ @( W$ s - frexp(x)5 m! l0 z, r6 l9 H$ a) o
- Return the mantissa and exponent of x, as pair (m, e).8 [ M7 y6 n# p, }+ G
- m is a float and e is an int, such that x = m * 2.**e.+ A5 I2 f0 h" G
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
9 F( W9 x5 K6 _% p- N - >>> math.frexp(10)! M) a& B8 t$ E3 c; e; u
- (0.625, 4)7 [4 v1 {6 J: P
- >>> math.frexp(75)" L7 i( Y) k, Q; c
- (0.5859375, 7), d' h8 M( Z2 j* `/ G" Y$ K
- >>> math.frexp(-40)
m2 e D L1 h6 ? - (-0.625, 6)2 q. L# R$ q8 x
- >>> math.frexp(-100)% r4 C) Q9 `' T" [
- (-0.78125, 7)/ i" ? V+ u" ~3 r+ I6 b
- >>> math.frexp(100)
9 A5 F- y: v* C& i% k M - (0.78125, 7)
复制代码
. u) v* Q" b; L5 n2 l& h+ Fmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)0 ?& B* A8 |/ }, G
- #对迭代器里的每个元素进行求和操作# ^% Z2 i, F; a5 _0 t
- fsum(iterable)
" X( n' R$ i/ c, h5 w - Return an accurate floating point sum of values in the iterable.
4 D% U# w9 g- I" i - Assumes IEEE-754 floating point arithmetic.
& c# ?. H$ O0 L: _7 L - >>> math.fsum([1,2,3,4])
( s% j/ _. x' U - 10.0+ [. h/ g& w2 V: `! e7 {+ o( j
- >>> math.fsum((1,2,3,4))
( I0 w- E7 P3 { - 10.0
- R O% F8 h) A. p - >>> math.fsum((-1,-2,-3,-4))3 i O2 t4 i- I- C
- -10.05 X T z7 u" Q) A1 h+ |5 T
- >>> math.fsum([-1,-2,-3,-4]) w1 @! Z ]+ p# x9 p/ F
- -10.0
复制代码 % E. {6 i2 r( \6 F% |
math.gcd(x,y) 返回x和y的最大公约数
& I/ d2 C" z: [5 R! W- #返回x和y的最大公约数
! g8 |1 c6 b4 G3 T( Z+ Q$ U - gcd(x, y) -> int7 t* _6 W, E4 W
- greatest common divisor of x and y5 l6 @4 ?& n+ s4 J7 u! U. K, c
- >>> math.gcd(8,6)6 ]. F0 b& K0 @7 O
- 2
; ^+ a% w: ~9 }- G2 T* D1 S1 q8 a - >>> math.gcd(40,20)
) b2 \1 b" r5 ^6 J, a! | - 209 z2 r" j% K! P
- >>> math.gcd(8,12)1 A8 `, F/ R) I7 S
- 4
复制代码
4 h6 Y. G5 H& lmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False0 t: A; `" v: D: p' O8 M) q) X
- #得到(x**2+y**2),平方的值
* B }8 o0 K6 N* B" k { - hypot(x, y)# U. f' M. m3 |4 Q, b, y$ P2 S
- Return the Euclidean distance, sqrt(x*x + y*y).3 ~% e* z, o1 K# u; a& ^$ x+ v
- >>> math.hypot(3,4)+ N4 }3 E6 g& E2 g/ Z6 p. _
- 5.0
. [% r* z& X7 s, F+ S3 m - >>> math.hypot(6,8)+ J& I$ j ~4 C" Q u% o6 }9 y
- 10.0
复制代码 # R8 g, a& O f& A J4 j
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
3 l& z. j; S' M- #如果x是不是无穷大的数字,则返回True,否则返回False
8 ^$ F) l8 a+ g" l- t& }8 K - isfinite(x) -> bool0 V H8 @( k8 M& f
- Return True if x is neither an infinity nor a NaN, and False otherwise.* A; e5 O4 t) a( H9 E0 d" h
- >>> math.isfinite(100)
$ k$ J4 d$ F9 B5 d" r - True4 E# u; \: Z. R) S
- >>> math.isfinite(0). Q9 f& t7 u3 n5 \8 k
- True$ d+ o3 ~3 y& V# ~* x. _
- >>> math.isfinite(0.1)
3 z% u7 ?# y" @8 c4 ]0 k. A - True% B% u: N% @# c3 ^7 e, N0 @. D
- >>> math.isfinite("a")# u) n, ]# U- p. H5 q: j' q) e
- >>> math.isfinite(0.0001)
4 s' O7 A/ f& c g* } - True
复制代码
5 h3 N7 k: Y1 s! `* ?7 |5 L$ Hmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
! i% x$ H1 V6 Z- Q4 {- #如果x是正无穷大或负无穷大,则返回True,否则返回False
; H/ J# v, B* C$ U( B - isinf(x) -> bool7 d7 D. D5 h) x. D" H
- Return True if x is a positive or negative infinity, and False otherwise.
, d4 ~( |, a2 e6 x) Y& |/ d - >>> math.isinf(234)
- v5 \5 T' s* ?' W$ T1 k4 Z! j4 R - False
% q3 `: k9 `2 t+ z2 |$ Q7 ` - >>> math.isinf(0.1)
* z" W9 T8 [8 m% ^; u$ ^# L7 O - False
复制代码
" T& K9 t: D2 \8 s, ]math.isnan(x) 如果x不是数字True,否则返回False
& q& D/ n( ~; b- #如果x不是数字True,否则返回False, Z3 t, H/ p2 r3 c8 p, a
- isnan(x) -> bool9 W2 e4 V' @3 t
- Return True if x is a NaN (not a number), and False otherwise.. e" i( u: G/ K; F( _+ o
- >>> math.isnan(23)
; C: y* y# I7 Y* ], B, w - False9 k# G, h1 E( |* E3 S
- >>> math.isnan(0.01)4 d# l6 Z5 a( y# t" J- }0 Z7 j0 s
- False
复制代码 $ b5 }4 D3 I! ~* r% j
math.ldexp(x,i) 返回x*(2**i)的值
9 {% x, j; a! G5 A4 h" Q- #返回x*(2**i)的值; h5 R- ^ k* [9 t& K7 Y8 G, W q- g
- ldexp(x, i)( _) q Z% _8 H3 k0 r, g2 p1 _
- Return x * (2**i). P7 e4 h1 G ^
- >>> math.ldexp(5,5)
$ [* k. o; y9 s- P1 Q - 160.0
3 V3 s* H, C ^) i& {- L$ \ - >>> math.ldexp(3,5)
. e/ x0 c! S' ^* z, q- O9 f, A - 96.0
复制代码
0 \% d* C* g3 c) i8 qmath.log10(x) 返回x的以10为底的对数
, I7 ^+ v$ ]8 j0 y! Q* m$ `- #返回x的以10为底的对数
" Z8 J4 T% H+ \) K# e9 L2 Q - log10(x)
2 z) W* D% z( ~, v# M: J - Return the base 10 logarithm of x.
& ^3 K4 c. S! g - >>> math.log10(10)2 F5 O, w, |' z2 y k5 _
- 1.0
9 E) [0 O5 [% v L% K, i" }) Y - >>> math.log10(100); f. B5 E: T7 i
- 2.02 g% m5 G3 T% j: q# t
- #即10的1.3次方的结果为20/ E+ q5 a) y% y" M- e2 p. }
- >>> math.log10(20)
# l. I+ j2 z- x! I% |- ~ - 1.3010299956639813
复制代码
% D2 X6 a6 z& z, _6 u/ @, A ?+ `( P& L, omath.log1p(x) 返回x+1的自然对数(基数为e)的值
6 C" \' S7 }$ u% J& ^- #返回x+1的自然对数(基数为e)的值! _6 R6 G' l+ X0 r* {
- log1p(x)
: E* }+ f1 d' P1 e! _8 R: M - Return the natural logarithm of 1+x (base e).# y" X6 s1 }# K0 z& D6 P/ G" l) K
- The result is computed in a way which is accurate for x near zero.
' w# ]: b: t( m7 `* ]. H - >>> math.log(10)
( J g1 p& j- }! ` - 2.302585092994046
n: m2 i2 g* ^( H2 R* [6 \, \ - >>> math.log1p(10)4 U8 ^, j+ b0 J+ X
- 2.3978952727983707* D/ f- [2 j4 O( E% G$ c2 [% B7 j
- >>> math.log(11)( P" T% l; ?9 n/ x* i
- 2.3978952727983707
复制代码 . u+ Q* T5 x# v+ w3 K% X
math.log2(x) 返回x的基2对数
% q- j# f! q8 R0 D; ]- #返回x的基2对数, S0 H+ }0 v4 `" X7 P9 A A) q
- log2(x)8 E9 Q/ V3 ?9 L$ K$ \' B m1 Q
- Return the base 2 logarithm of x.& ?1 k$ n) ]8 B/ Q4 ~
- >>> math.log2(32)/ a" S3 ]$ a, I
- 5.07 e. t$ J. z$ ~; G0 X0 I
- >>> math.log2(20)% L% J% i0 H) U2 a- C1 j& X3 q: @
- 4.321928094887363
4 q8 k# P1 w9 ?( ~7 R1 u - >>> math.log2(16)
! x4 s$ s/ B5 l. ?: ]& J* x - 4.0
复制代码 9 h" ], E! {" C5 S8 s9 d
math.modf(x) 返回由x的小数部分和整数部分组成的元组
5 f+ r' c% L& I# e1 W+ K* }- #返回由x的小数部分和整数部分组成的元组4 r2 b4 l* D7 _9 h5 T- a: }
- modf(x)9 S1 X+ o% \" x1 O( l1 K
- Return the fractional and integer parts of x. Both results carry the sign
* @" V' m* k# c# F2 C - of x and are floats.& }! R$ Q2 c+ N. M& a8 h: c) p
- >>> math.modf(math.pi)1 ?( u% I; [8 s0 ?% @1 S- m2 d
- (0.14159265358979312, 3.0)
2 v% h& i3 `* }9 ]. W - >>> math.modf(12.34)+ [, T% l7 ~: Z" F! A/ T$ G& W
- (0.33999999999999986, 12.0)
复制代码 % k! Y. Q$ @9 O; [
math.sqrt(x) 求x的平方根
+ g1 E2 F7 H2 C5 ?- S2 x) L' g- #求x的平方根
! B6 i( c5 Z- X; A - sqrt(x)
/ \( A' L$ ?4 U$ } - Return the square root of x.% _3 b3 S# w/ c) W4 F% H
- >>> math.sqrt(100)# C7 j9 Y- n) ^9 R
- 10.07 s& ^7 v: G" o' A
- >>> math.sqrt(16)
0 A4 A( K* X3 X; z" n: Z$ K$ o- N - 4.09 U1 C; [# K0 p- t. ^
- >>> math.sqrt(20)
) p- B0 i/ A( {( L; `1 u% Q5 d - 4.47213595499958
复制代码
2 z3 J2 B6 b9 r I. C. C) E/ J4 ]math.trunc(x) 返回x的整数部分- #返回x的整数部分
: `# k0 y- W' D. a+ X - trunc(x:Real) -> Integral
# @$ K& h( F+ g$ l/ P - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 E, S' \! y+ _ - >>> math.trunc(6.789)6 R& a2 E, z `3 k \+ f6 n! k$ _
- 6
+ `- K3 Y; }5 b( {& K7 y - >>> math.trunc(math.pi)- l' U5 T% H3 t' Q2 x. b
- 3
, I. L: o$ [( p8 f - >>> math.trunc(2.567)7 @$ }8 j2 |6 u
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|