马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 @# k8 a! G9 }6 E* q |$ p" { L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。4 Z, N+ `, y H6 x
5 o3 C3 D# y+ z6 J
方法1:) W4 u6 n5 X E
- >>> import math
. l4 O9 x: M- U - >>> math.sqrt(9)! a& E! t5 G k' k% {
- 3.0
复制代码 方法2:
! s' h s9 k' B# ]9 r$ }- >>> from math import sqrt
" A* l/ k0 H, Z) |8 O( ~ - >>> sqrt(9)/ L9 j. }3 _& Y" O" S( p! c) @
- 3.0
复制代码 0 o& w0 Z2 s- y5 R# h: W( P
# g3 q% K) U- m( J$ ^
math.e 表示一个常量
, t# U: P1 C( B- #表示一个常量
" D& \4 V; x8 r9 o( H; t( u - >>> math.e
! f# U" ^9 Q; b4 J6 \' s/ m - 2.718281828459045
复制代码 2 L- F8 x! h$ d
math.pi 数字常量,圆周率
1 l! c3 q: x1 }6 M- #数字常量,圆周率0 X' B( E6 n& C* h
- >>> print(math.pi)
* ?% Q' d% ?9 C! K0 R - 3.141592653589793
复制代码
1 K2 E& n% }' K+ o: S8 umath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x# L. D$ S9 y8 }9 u: @+ j8 B& U
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 N9 h% H3 ^ Y - ceil(x). S% c& \9 i3 P9 W j
- Return the ceiling of x as an int.
$ h5 _$ h* l" T- i4 c - This is the smallest integral value >= x.4 Y8 C( v: p4 k* R
- ' R- E" G( e0 \( Z, _+ C5 g- u
- >>> math.ceil(4.01)' Q+ A" w; K8 X* b1 l. m
- 5
, F* Y6 f. [3 |4 r, D- \ - >>> math.ceil(4.99)
* ~) x4 _/ _. g' ~) Q* t: C1 g( B% G - 5) |" M& n3 o& B
- >>> math.ceil(-3.99)# H( U; [; b2 u* d
- -3
4 U) Z* m0 i" b- q$ |7 Y - >>> math.ceil(-3.01)/ B7 L* Z7 |2 L a; L) {& E# ~
- -3
复制代码 9 ~8 ^" b0 S9 M) @2 s, {% q1 G* j
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; V/ ]% D D+ ~8 I- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 P% H! D$ b) ^5 K, N& [8 O- _
- floor(x)
- V* p# a' _5 P: B+ P - Return the floor of x as an int.
2 `7 o" w8 {- k4 q+ T6 F - This is the largest integral value <= x.8 ?5 ]& W( [; b
- >>> math.floor(4.1)) H$ D3 E D* V6 g) n
- 4
4 l0 o- e# D6 n0 w2 r - >>> math.floor(4.999)
% \: H0 k1 \& [* J - 4" Z) x8 R1 ]8 x5 B
- >>> math.floor(-4.999)3 o* V/ A& f5 D/ ^6 H# z
- -5' k* V) K7 x( Q
- >>> math.floor(-4.01)
1 i& M5 h: E9 Z - -5
复制代码
" @, l' x7 J& s2 G Lmath.pow(x,y) 返回x的y次方,即x**y1 x5 L* {! ]8 f" \5 M$ Y
- #返回x的y次方,即x**y
8 X' e+ z& F& O3 }- c' c1 w - pow(x, y)* N8 z* M# Z0 m9 P# B
- Return x**y (x to the power of y).
+ d- N$ I- V6 b. U - >>> math.pow(3,4)
- U5 `% u( g8 E- t6 Y5 P. j: | - 81.0
/ V5 k) I7 c/ {* Z - >>>
6 @3 j+ H7 T, S6 ~* G: x - >>> math.pow(2,7)) C6 x/ q# D) ^" T9 Y/ N, M
- 128.0
复制代码
% x. c4 q) b: K$ ?) jmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( _7 C. X& E5 t, j. |4 h
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, q% U# o) j( U9 g( l" p1 v - log(x[, base])
/ p' @% I5 x/ z9 W5 F' r0 l - Return the logarithm of x to the given base.
' `' p1 |& {$ }* v U6 y - If the base not specified, returns the natural logarithm (base e) of x.
. i9 ^( Q" F1 o% | - >>> math.log(10)" N, ^; j6 d8 W6 w
- 2.302585092994046! @3 Q2 F* D H, e0 u( t
- >>> math.log(11)
: u1 ^! O, n. g8 ?8 o. @& f k - 2.39789527279837076 p4 M6 N% M& W. a
- >>> math.log(20)
! c8 F" B% ~/ d7 U* ~8 y - 2.995732273553991
复制代码
$ J; h$ a: d/ z/ @* @$ Q3 D0 M, emath.sin(x) 求x(x为弧度)的正弦值) ^ f3 t) @3 i, n4 v- c
- #求x(x为弧度)的正弦值
6 E# v0 d X6 e - sin(x)0 W% q H5 D: J) s& W8 F% R
- Return the sine of x (measured in radians)." _8 R( U5 F- g. z
- >>> math.sin(math.pi/4)
7 g1 k' u/ I( v6 o) C - 0.7071067811865475. c" W/ E( G, L/ i5 |% i, Q# v
- >>> math.sin(math.pi/2)
% l/ G5 P1 o m } K - 1.0
5 _. Y+ U F) ^! d - >>> math.sin(math.pi/3)- A- @% z. P! l4 }! R) L: M
- 0.8660254037844386
复制代码 ( ?9 k8 z; O) C, s* Z4 e! M9 d
math.cos(x) 求x的余弦,x必须是弧度
* o2 v1 n" ?7 Y r: I, ]/ A- #求x的余弦,x必须是弧度
/ ~4 O F* p3 ~ - cos(x)
. W8 H9 @3 q) ~) y - Return the cosine of x (measured in radians).
# f+ ^; P( }* ~0 c- x - #math.pi/4表示弧度,转换成角度为45度
' V* X: K6 Z e& \( X4 R! T4 x - >>> math.cos(math.pi/4)9 }3 m- d* D/ l; Z
- 0.7071067811865476
$ H7 b) i+ P6 ~- p& r - math.pi/3表示弧度,转换成角度为60度
& Y* q. s8 V7 `" }7 K - >>> math.cos(math.pi/3)' F ~" c. M+ {6 {. v I9 d6 ~5 ~
- 0.5000000000000001
% H8 o6 _6 K h* f - math.pi/6表示弧度,转换成角度为30度- M" \% ]% k# ^( e% l
- >>> math.cos(math.pi/6)3 `# l* f5 x/ }- l3 t
- 0.8660254037844387
复制代码
9 w; d* A% K: dmath.tan(x) 返回x(x为弧度)的正切值2 H% R9 x( R8 [+ A$ t) K& c' O
- #返回x(x为弧度)的正切值- ?9 U. A' q) h
- tan(x)! A; s; k/ ~% V7 b! {% J
- Return the tangent of x (measured in radians).
# h2 p& I/ v' b - >>> math.tan(math.pi/4)
: v) f3 h6 b" I5 N7 W4 j) G - 0.9999999999999999. K' v8 z0 J6 U
- >>> math.tan(math.pi/6)
/ p) |3 V2 y5 q8 y6 n8 y5 T- a - 0.5773502691896257
; T1 `% E' G# ~- A) j* j0 e, Y, l' |8 W - >>> math.tan(math.pi/3)3 a2 b% L7 Y3 U# S+ q
- 1.7320508075688767
复制代码 5 R8 d" Z* n$ H+ ]" R4 z
math.degrees(x) 把x从弧度转换成角度5 `3 y1 ^3 J# E4 u' T; N* u1 a; _3 b
- #把x从弧度转换成角度
" l) r* p+ C' B# i' B - degrees(x)2 P8 ?: |; ]( X( l4 h& v; b
- Convert angle x from radians to degrees.- D. _% _+ r4 J
9 u! k7 t1 C b: U* Z( P/ h) V* m- >>> math.degrees(math.pi/4)
8 ?( j* y" f& O6 V, _4 F - 45.0
& R2 J& y2 `. \8 |) ` - >>> math.degrees(math.pi)
. }8 b- F, j* a! ~ - 180.0+ X8 O- G; B0 w/ u9 P: a( B
- >>> math.degrees(math.pi/6), |0 `- [7 U0 u' A+ H
- 29.999999999999996* e7 i3 w2 } P) j
- >>> math.degrees(math.pi/3)) V3 ^" E+ \# B( @: C4 h7 G
- 59.99999999999999
复制代码 % {6 Z$ {0 W8 L
math.radians(x) 把角度x转换成弧度; e8 R& L" n0 K. ~- t
- #把角度x转换成弧度
7 B; P! j! i4 M; J) g - radians(x)
9 a3 e/ u* N& { - Convert angle x from degrees to radians./ C) V9 ]$ B. g0 j
- >>> math.radians(45)
& }% W* F) L6 X7 r' b% H - 0.7853981633974483* f1 K% l0 U4 s7 r9 }+ u
- >>> math.radians(60)
: Y0 M/ v, S# t8 ]+ c% o4 r1 P - 1.0471975511965976
复制代码 7 ]7 R2 z" i$ m8 G) F
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
/ J: L% U+ C, o9 u0 Q- #把y的正负号加到x前面,可以使用0- K4 Z8 B9 V% q3 x0 T5 T4 R) w
- copysign(x, y)* F9 m% B g p8 f- z
- Return a float with the magnitude (absolute value) of x but the sign ! Z! A0 ~6 X& r3 D$ q" T
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
$ w& i r; _, T( I) ] - returns -1.0.7 q6 H" r7 P4 Z8 M A/ G2 \
- 2 |! R) u' f1 n( k0 u2 p% D' G, q
- >>> math.copysign(2,3)
+ ` t: _4 d% p9 g: Q: y8 J f - 2.0
/ C- X( k% R a! a* U. P - >>> math.copysign(2,-3)
' T5 r- |6 L7 ~: ~7 ` - -2.0
$ t7 I5 T% m) A! n- x0 v! @0 t - >>> math.copysign(3,8)
( `, M4 G/ A2 u% q' y; Z - 3.06 s" D! a+ x3 l% H
- >>> math.copysign(3,-8)
# A3 C* d9 {) @" H3 { - -3.0
复制代码 4 k' u8 w2 E f: s8 E/ N/ \
math.exp(x) 返回math.e,也就是2.71828的x次方4 T% ?0 f+ H. \+ g/ }- J
- #返回math.e,也就是2.71828的x次方
8 a2 @" b. r% J% a5 O( P4 ~7 d2 O# _( G - exp(x)
# `7 d& `' n. W! k$ h! H5 i% b - Return e raised to the power of x.
, A# q6 o( V: h7 A4 U - 9 X n; L0 y5 u
- >>> math.exp(1): j# s# U% D+ b9 d& O, }$ H0 u% J
- 2.718281828459045+ C4 {( @9 b% C. V
- >>> math.exp(2) O9 `. }7 w% j
- 7.38905609893065
9 G+ W9 L' K1 W0 H - >>> math.exp(3)
/ D) p7 r$ A& \" \! ^! ~+ M - 20.085536923187668
复制代码
; g6 u9 W& S9 p, ^math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减13 G/ i. K. c& K2 ?( V, F2 Q/ H
- #返回math.e的x(其值为2.71828)次方的值减1
|' y3 b6 f. h7 L8 U" g( { - expm1(x)
. T7 X% A$ E% v4 U - Return exp(x)-1.
- T3 X7 z* i, p* D3 ]$ A$ N2 G - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. U. z8 P4 W" E3 \
- + u$ @$ |5 @- H# ]
- >>> math.expm1(1)
4 } v) o* M! _7 q - 1.718281828459045* j1 d' p( |# i
- >>> math.expm1(2)9 q! ~8 |( n6 R3 P$ ?
- 6.389056098930658 g! ?9 ^5 A5 k4 h% l
- >>> math.expm1(3)
- \6 ?0 y, W& O* `$ B5 ^, w1 p - 19.085536923187668
复制代码
6 F$ n& g( ~ ^math.fabs(x) 返回x的绝对值
0 A: J2 e, g! f1 d' G- #返回x的绝对值2 X, a) ?6 P+ s5 ?. x
- fabs(x)
@. E; |- ?( |; [ - Return the absolute value of the float x.
& `6 T( q/ M, W& T' G
/ Z/ G! a4 s* f) Q+ \- >>> math.fabs(-0.003)
. b4 M# I, R& p9 D0 @4 A - 0.003
) }9 g K) U3 x - >>> math.fabs(-110)& K; D% J% C( j' U" n1 V% X* v5 F
- 110.0
. R, L% Z5 M" L; L - >>> math.fabs(100)% O; m; Y' E# V" X! { d4 A5 ]
- 100.0
复制代码 4 n) \0 k! }5 Z- P8 h
math.factorial(x) 取x的阶乘的值; ?, ^+ [/ n# H" t. E3 U
- #取x的阶乘的值
' J; o7 Y. I! W: c7 q# c9 g# L - factorial(x) -> Integral
7 Y# B2 m+ T( f" L" q - Find x!. Raise a ValueError if x is negative or non-integral.
% _0 \8 n5 K1 i' {' | - >>> math.factorial(1). u/ K/ B+ b u2 B
- 1
: w; c* y/ R" W! _ - >>> math.factorial(2)9 t- P# K# u9 ]7 }0 E) m3 r
- 2! B; U2 z$ u+ A3 Q: T
- >>> math.factorial(3)- b. U$ H& e t
- 6
4 ?) O( ?4 `* [; f/ j - >>> math.factorial(5)
# ?3 f; a7 X ?% {" J - 120
1 M; `9 E, t6 \9 ~8 ?$ `7 G - >>> math.factorial(10)
% `7 R, U" X# P, U% @ - 3628800
复制代码 * x$ M# Q' I: G3 \
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
% I/ J f0 q" m. F" H9 a- #得到x/y的余数,其值是一个浮点数
# \2 K8 ^6 P7 @& | - fmod(x, y)+ Z. \3 \& g8 J6 `# J
- Return fmod(x, y), according to platform C. x % y may differ.1 C% m: X$ t) D
- >>> math.fmod(20,3)' m+ y; m7 z# ?5 L0 ^
- 2.0
* i& s9 O; g8 V& a - >>> math.fmod(20,7)
( \! N) _9 w: Y: I - 6.0
复制代码 3 k6 ?, @: A2 w$ u F
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
4 H+ q" F* V9 i* l { c- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
$ \# h& p# n$ c- P* u5 x - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" y- d9 v- g5 i, }7 g3 }0 a S& \' p
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 k6 _, t7 y% g* ?8 w
- frexp(x)! o7 J8 {; d$ w* D* c
- Return the mantissa and exponent of x, as pair (m, e).
9 `, E$ U+ [7 m - m is a float and e is an int, such that x = m * 2.**e.+ @; h3 ~ a% G, h; V" k2 \5 r
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.2 H% ]# i2 L9 y5 q& ]/ k
- >>> math.frexp(10)$ {# w5 ]2 U7 ]; \
- (0.625, 4)- S. f( K( Z% \: Y$ t) O5 Z
- >>> math.frexp(75)
1 o9 W' a' p- o$ `* j, U0 _ - (0.5859375, 7)
7 L: y7 R1 J+ K5 F. o8 B( j& R* a - >>> math.frexp(-40)
' u1 p: L' n- Z3 p! ?+ M - (-0.625, 6). [& X# L, N* |; x1 `1 A! P
- >>> math.frexp(-100)
$ P9 L! o y4 b z* [) g - (-0.78125, 7)+ q+ T2 N3 n: M! U
- >>> math.frexp(100)$ l9 Z( t* @$ T
- (0.78125, 7)
复制代码
; k- \) g7 A5 K$ gmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
6 a" ^, K; X4 Q" f- F- #对迭代器里的每个元素进行求和操作
4 a7 _/ H( ] [7 ^$ l - fsum(iterable)
1 E! U3 `9 W' ~% D - Return an accurate floating point sum of values in the iterable.
3 R* k6 C6 Q0 B% }* u$ S - Assumes IEEE-754 floating point arithmetic.
" ?) }6 G8 t7 a* M5 ` - >>> math.fsum([1,2,3,4])3 J5 G- |# x4 v2 C/ Y' _
- 10.05 L- J3 x! e& J; P7 _( e) G
- >>> math.fsum((1,2,3,4))
1 y+ w" h3 [ u, l; F e - 10.0
. t* o' c7 z0 }: r! L - >>> math.fsum((-1,-2,-3,-4))
, u% h) {/ _+ X6 ]' f" Q5 _ - -10.0: S$ l8 ~8 n* I! M2 i
- >>> math.fsum([-1,-2,-3,-4])
% H2 v( h: P& e! a, S; D: P9 q+ M - -10.0
复制代码 ^% X7 [4 f i7 Y) e7 ~7 ~
math.gcd(x,y) 返回x和y的最大公约数
2 X% L+ @9 _+ Y: ?' [- #返回x和y的最大公约数
7 |. Z7 C8 t6 ~9 l5 q - gcd(x, y) -> int) Z/ B: K2 Q$ t: \6 @
- greatest common divisor of x and y
: O% L+ E+ `- O - >>> math.gcd(8,6)
6 U6 p" W& v# f - 2# |9 A+ f' R7 Y
- >>> math.gcd(40,20)
" |0 Q5 n1 F% ^1 c5 r$ A - 20
3 K, g8 V/ o( x9 m - >>> math.gcd(8,12)4 k/ ]( ] n7 k8 Q. O; m
- 4
复制代码
% x' L9 I1 W0 ?% w3 Imath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
( S8 M0 R- O1 A; e1 @- #得到(x**2+y**2),平方的值
) C$ o3 N# l0 h1 e% T0 S - hypot(x, y)
# T8 `& j0 ^0 [$ L& r$ p - Return the Euclidean distance, sqrt(x*x + y*y).
# m$ Q; l& j+ v M/ ]- S - >>> math.hypot(3,4)
2 d# G. Z6 b# x2 t1 T0 a - 5.0
* y4 L2 l/ m9 z* o; \9 d: n - >>> math.hypot(6,8)# [2 i) E3 k& M1 L) E. v* P) h4 c3 F
- 10.0
复制代码 6 T" Z7 T$ @* `
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
, }9 l% V& h9 L) a( n- #如果x是不是无穷大的数字,则返回True,否则返回False/ u1 J, V/ F3 g( \
- isfinite(x) -> bool
. N+ z; N7 t1 Q5 k8 s+ w - Return True if x is neither an infinity nor a NaN, and False otherwise.
# R3 E/ o# m; R* Y- p: Y* w - >>> math.isfinite(100)# G3 E; `, K+ T5 I& L1 h1 S0 o
- True$ d4 N$ `) z0 I6 I
- >>> math.isfinite(0)
( m8 R8 q1 |0 W - True" ^5 h1 E3 Q" f. B$ x6 ?: S1 p: t9 o' {
- >>> math.isfinite(0.1)
+ R% e# d1 s4 D - True
& t) R7 C2 [1 m; r5 Z# J+ i - >>> math.isfinite("a")
& e' U+ k3 E% U1 V% a. v - >>> math.isfinite(0.0001)9 b, P e' M- Q9 P
- True
复制代码 9 `5 z) H/ p$ E; L1 }) G
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False! ~# i; h5 N6 S# f
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
- \* R) B9 y% \ - isinf(x) -> bool
2 {1 P$ _) s( h& y - Return True if x is a positive or negative infinity, and False otherwise.
' y; h( e/ Z% F% Y* D& Y) g - >>> math.isinf(234)
, z4 |) ^$ l! S! k0 q2 P0 J - False
, z/ E, g/ }& N) f' G4 e" F3 m# g- ^ - >>> math.isinf(0.1)
. o& d2 L" X9 o n, m, B7 x - False
复制代码 + J) N% q1 J: A
math.isnan(x) 如果x不是数字True,否则返回False
( U/ n6 ~9 |7 D8 p) T- #如果x不是数字True,否则返回False4 N( o$ K8 G8 Q# P$ k8 ?2 d" u2 f
- isnan(x) -> bool, E: ]9 {- P9 ]. p7 ^
- Return True if x is a NaN (not a number), and False otherwise.. G W" j- p$ T! }
- >>> math.isnan(23)
; L! [ @: }- f7 O% s0 V/ w( V8 e - False
# W5 p+ s4 L$ I$ l$ ]4 V! p - >>> math.isnan(0.01)
# F8 Y" L& E Q% F$ U0 o+ i: Y: u - False
复制代码 ' ]( `6 K5 [+ Z# A9 d4 g
math.ldexp(x,i) 返回x*(2**i)的值
4 Z( w) ~. ^* Y- #返回x*(2**i)的值
( M( d4 v6 r0 ]) d0 B - ldexp(x, i)* F( {: P |5 [) q3 t- e# J
- Return x * (2**i).0 {: D" s$ I2 Z8 G$ s; d2 F
- >>> math.ldexp(5,5)
7 F% X1 f$ f: r+ ^- \2 ~+ S9 h - 160.0
4 C7 ` J3 W8 _; c - >>> math.ldexp(3,5)* Z" ?& `5 I5 p8 P# Y! W
- 96.0
复制代码
+ t9 z1 m/ M {5 k+ u& Pmath.log10(x) 返回x的以10为底的对数/ a( t; H, D9 ]0 g+ [
- #返回x的以10为底的对数& S; u* ~, c* Y+ h1 C- [
- log10(x)
3 X9 C5 W8 `5 ~8 A$ } - Return the base 10 logarithm of x.* r- K# R+ m3 i, `9 H8 Y7 W2 b
- >>> math.log10(10)
* m" A }) a# u - 1.0) j# z1 {& C9 ?
- >>> math.log10(100)
" J7 X, l# V5 y - 2.0
! ]1 [( S) y' i' ^+ } - #即10的1.3次方的结果为20
; g' I) o* x- w - >>> math.log10(20)
2 r% S$ z. P( A - 1.3010299956639813
复制代码
5 G& G D; U d: |math.log1p(x) 返回x+1的自然对数(基数为e)的值
/ \! h* Z8 G% d# p' k# w- #返回x+1的自然对数(基数为e)的值
% Y& C9 ]; |1 w n, [; `. L - log1p(x)6 t7 G/ c6 ^0 l/ p, p% x# u
- Return the natural logarithm of 1+x (base e).
7 r% ^! T; O1 h% B3 C7 J% } - The result is computed in a way which is accurate for x near zero.
G* c" x4 ~% F - >>> math.log(10)$ h9 k/ j4 [& w0 a
- 2.302585092994046
0 X4 ?2 Z$ ^4 s* o - >>> math.log1p(10)
2 G# ?% w! [% Z) I - 2.3978952727983707
/ r v& p) h. D1 a* y8 M - >>> math.log(11)$ Q) S9 \+ V5 | M8 Q' b! s1 r
- 2.3978952727983707
复制代码 ( N0 m$ g* D$ X; i; S- z
math.log2(x) 返回x的基2对数 R, d7 O, @' _# [$ ]" }3 r
- #返回x的基2对数
; z5 `; |! V- C) d" b) M5 W - log2(x)
" }- b! ]* }2 h8 `% E - Return the base 2 logarithm of x.
3 j7 O. ?; T" H: g; P - >>> math.log2(32)' U+ A% N7 g+ E9 F x/ h
- 5.0
, t+ H# l1 a9 y' i - >>> math.log2(20)- N+ g* X5 S1 k7 l
- 4.321928094887363
" b1 t4 {7 o) y6 c" z8 D' T - >>> math.log2(16)( g% v: u( U1 o- j6 ?1 n! p
- 4.0
复制代码 $ V. v$ f& E( @: i6 G% F" M
math.modf(x) 返回由x的小数部分和整数部分组成的元组
" T3 k5 ^; n, A# U( p4 B. D- #返回由x的小数部分和整数部分组成的元组/ X. R* a/ C. y( N& x+ R
- modf(x)( X1 ]9 {4 [) c! c! q; G2 R
- Return the fractional and integer parts of x. Both results carry the sign
( w% K% j8 b/ p8 _6 c, ^ - of x and are floats.
2 |' \) o3 I' [; O6 p4 G$ p7 G - >>> math.modf(math.pi)
" L$ m! Q3 G: J - (0.14159265358979312, 3.0)
' f3 \1 U! Z, n4 Z3 l! y4 | - >>> math.modf(12.34)
+ w4 l* p( j1 u8 _, W' O: f8 h - (0.33999999999999986, 12.0)
复制代码
3 H5 {& k3 T& Y m4 H4 G3 K3 G: wmath.sqrt(x) 求x的平方根
( u6 P( _& b1 D- #求x的平方根" d$ j2 r- M. s P1 f" @. U
- sqrt(x)
; z i; K! W) e# V3 ~! P2 y0 I0 J - Return the square root of x.; }% u( V. E2 j8 H& L
- >>> math.sqrt(100)
! I5 o2 E7 x* v% c4 A - 10.0
1 r& b/ b2 r4 i! }# k+ `5 n. w - >>> math.sqrt(16)
' U7 Y; U# k5 m: H9 J, h - 4.03 [6 |6 g7 w y9 C3 O; U( d4 u
- >>> math.sqrt(20)
+ W7 i9 X& t4 w. m* o: Q8 U - 4.47213595499958
复制代码 # v# r2 \7 J& @6 b6 V" m* R' a
math.trunc(x) 返回x的整数部分- #返回x的整数部分
: W7 p: s( Y/ w8 N - trunc(x:Real) -> Integral) z6 ~5 |+ G9 p$ l4 m
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
0 N! K2 o! U1 c4 a6 o$ _ - >>> math.trunc(6.789)# _7 l6 A' y- c! @
- 6
3 @/ [* [! B2 i% g' } - >>> math.trunc(math.pi)1 U1 W* x% @* U
- 3
+ u6 L! z. Z$ R, g' t8 x/ M - >>> math.trunc(2.567)1 d* x5 X% _; M; }
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|