马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
( k9 U n' T" d9 p3 F【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 Z- H4 F. g. v0 m
5 e; W/ W/ r5 T& x
方法1:
6 x% W, C, s' R: }% b- >>> import math* [& J9 {6 U+ @0 s; n2 u
- >>> math.sqrt(9)0 Q3 r6 n7 P6 ~/ A
- 3.0
复制代码 方法2:6 k5 i) |2 q, S/ i
- >>> from math import sqrt8 Y4 P2 H* J4 G; c& c5 ~$ ?
- >>> sqrt(9)* u0 W/ A; r( r
- 3.0
复制代码 0 F/ U0 S* N- Y2 x6 V9 W
/ u X/ |4 n- G6 |- Y; k- }
math.e 表示一个常量3 B, R' _" g d& [5 [$ U$ b k! ]; h" q4 f
- #表示一个常量
) y) t: p9 I8 p: Q* C - >>> math.e) m6 z6 p! O' Y1 x
- 2.718281828459045
复制代码 _" R" B7 J3 S6 i) V2 N. Y& c& n0 w
math.pi 数字常量,圆周率
! m% o0 Z G# V- W: }* g; U, ]8 U- #数字常量,圆周率% R9 ]2 ]- Q; q8 Z, b( a" ]0 ]1 D
- >>> print(math.pi)/ h- v+ N1 I! [2 m4 K
- 3.141592653589793
复制代码
8 J& |$ K7 @- f: rmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
6 F' q4 A. `- s$ Y& N& b8 q- #取大于等于x的最小的整数值,如果x是一个整数,则返回x* b* r7 ]! O) u8 O
- ceil(x)
0 q: t) I' X$ ~; P- T$ i - Return the ceiling of x as an int.
9 D% A1 c( _/ X$ r& w) W - This is the smallest integral value >= x.
# a0 C0 Z% C( C2 V t
4 v# d0 a9 K0 B/ l, B- >>> math.ceil(4.01)
2 s& v& r0 D+ J S - 54 k0 T* v+ [( r; \! ~6 C9 d
- >>> math.ceil(4.99)9 y/ J' ~1 d. n
- 5* A$ p! v' \7 e. U/ r7 E }
- >>> math.ceil(-3.99)9 `* {& h: Y6 L4 i6 n
- -3
) X# j& F9 K7 O9 y8 ]# | - >>> math.ceil(-3.01); O3 _% M7 b! d% ]: V# z; B
- -3
复制代码 . Y, G9 T; b. q) [3 N+ g& ?! J
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 u* A8 u" R* s+ w- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% I* j' n/ o- j6 e4 b; z/ o
- floor(x)
& z5 g1 i o2 ]6 M$ ]/ O. e - Return the floor of x as an int.! W- B. d/ D5 e7 }) B
- This is the largest integral value <= x.
, ^; W% ?' R6 f - >>> math.floor(4.1)
$ \2 D! i: Q1 D: w - 4
, i! O' S: w: F! D# ]* Q; ^; x - >>> math.floor(4.999)! V3 _( |3 j( \+ N- ?" U% L
- 4! o- s+ i# f* C- l @) h
- >>> math.floor(-4.999)0 c7 ~7 v9 O& O ?% ~# _; B' W
- -56 r! s5 h, g, ~4 X/ F6 E# d% R
- >>> math.floor(-4.01)0 n( r$ _, n% ?! B) F8 r
- -5
复制代码 . w" @% O& j" A6 S! S
math.pow(x,y) 返回x的y次方,即x**y$ ?$ R+ k. H; U5 L' F
- #返回x的y次方,即x**y" B7 G( ^- b8 P# ]
- pow(x, y)* t( S9 h2 B, ~( D/ Q
- Return x**y (x to the power of y).1 ]- b, B, M; w2 R: [
- >>> math.pow(3,4)2 p3 U, {/ R2 [( o: S5 s
- 81.09 x6 h r3 ^' G' O; P9 Y: \
- >>> ' i1 J2 W( ]4 Y* r
- >>> math.pow(2,7)0 j4 U5 h/ j$ t
- 128.0
复制代码 G4 t1 L$ l+ T( Y
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): v4 g, v' u6 P8 e* x6 F+ _* w( r
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base) o1 z+ t8 S) z6 H7 }
- log(x[, base])
7 C6 G1 g& l; Z/ q - Return the logarithm of x to the given base.
) u6 n0 l4 \* n* u, w/ P* m) ]+ f* _ - If the base not specified, returns the natural logarithm (base e) of x.; @! ]6 a, k6 z9 k( p
- >>> math.log(10)
3 H- [$ V3 y# K' w% K' { - 2.302585092994046
. d8 }: Z @; N1 X! P ^# l - >>> math.log(11) X( g: n4 H5 x2 J* r
- 2.39789527279837074 R* b- E5 T9 e/ A& z
- >>> math.log(20)
) z7 Q! ^. L7 K0 e# f - 2.995732273553991
复制代码 - J. a2 B( @$ E0 _: r) m
math.sin(x) 求x(x为弧度)的正弦值: C4 [ G9 d1 Z+ G& S3 J1 D
- #求x(x为弧度)的正弦值
& P0 I @" u) v, m; {9 V - sin(x)
: ?/ y% k7 f: w- y - Return the sine of x (measured in radians)./ i* k& j$ z- l9 V
- >>> math.sin(math.pi/4); i3 H7 Q1 I: Z& x9 a) V" M
- 0.70710678118654755 {$ R% p- L9 |, b
- >>> math.sin(math.pi/2)
5 `. ^! \% v2 c% `/ N - 1.0/ Z; N$ q0 F* q; [
- >>> math.sin(math.pi/3)
; I& m1 ]: T Y3 Y# H" v/ { - 0.8660254037844386
复制代码 9 g, _4 J+ y- X! g/ w2 _% O
math.cos(x) 求x的余弦,x必须是弧度. T) s) D- Z( j1 O
- #求x的余弦,x必须是弧度
1 C! v" [6 g: A" q - cos(x)" ]! O7 A( S7 o7 Z
- Return the cosine of x (measured in radians).7 @& t# w5 u0 b
- #math.pi/4表示弧度,转换成角度为45度
! k. u. Q( I e - >>> math.cos(math.pi/4)
- D" O: M0 j7 F7 x4 g7 f: E8 X& l - 0.7071067811865476
* p* L3 [- j2 T' D' A% w - math.pi/3表示弧度,转换成角度为60度
5 a* K4 W2 A5 U& K - >>> math.cos(math.pi/3)7 }5 x' X( J' e }; _, [$ d
- 0.5000000000000001% N; w/ U2 ]; ]6 m; P! Q5 B* X5 r
- math.pi/6表示弧度,转换成角度为30度
* K1 ?( m; p& Z* a - >>> math.cos(math.pi/6)8 @9 k# `4 w- @: a+ t# y
- 0.8660254037844387
复制代码
3 c, S, ^% k4 N/ i9 g1 y4 K8 Xmath.tan(x) 返回x(x为弧度)的正切值
: y1 h( w+ f h- #返回x(x为弧度)的正切值% G- V2 k- a# `3 C! U- Y) r2 \8 V
- tan(x)
! ]: l! |. U0 u0 N - Return the tangent of x (measured in radians).2 [ a: r* B* e; }2 t
- >>> math.tan(math.pi/4), t5 l" f' d) R* x+ N: l
- 0.9999999999999999+ l& e) H: r3 a7 m" N3 c7 V, R u
- >>> math.tan(math.pi/6)3 f$ P0 S \& k6 [, _, T9 `! j! e
- 0.57735026918962577 n% E% C1 u2 Y& T
- >>> math.tan(math.pi/3)
& I6 C0 ?0 M/ S1 r6 q# g4 _ - 1.7320508075688767
复制代码
# n; H& Y' t' fmath.degrees(x) 把x从弧度转换成角度
' x# m) o! |- c, K- #把x从弧度转换成角度
. O/ j8 \" K3 I Q2 |+ Q - degrees(x). d# J0 J3 J- T, H' E7 u
- Convert angle x from radians to degrees.9 f$ S8 ]- [4 C% [* H" _$ F
- 4 X& a1 y( l# }1 x. M
- >>> math.degrees(math.pi/4)- j" _8 j$ u& {0 `' q2 W% Y( d0 ^- C
- 45.0
: c- p7 E: |2 k. h - >>> math.degrees(math.pi)
0 M0 ?9 D2 _5 |0 | - 180.02 C4 M! }1 _+ @' k8 B7 Q7 N
- >>> math.degrees(math.pi/6); j" a! J1 p) V
- 29.999999999999996
; L# M( S$ Q" ?$ n) a - >>> math.degrees(math.pi/3)- W& ^. Z6 e' k e! ~
- 59.99999999999999
复制代码
, P1 a! V9 L. _% s: }math.radians(x) 把角度x转换成弧度2 `9 b& f1 W* L" Z4 @
- #把角度x转换成弧度+ ^& T3 N* X5 l
- radians(x)5 d$ S2 W+ M' Z. W: T; y1 ]8 n
- Convert angle x from degrees to radians.
1 s" p! e7 H! C4 [ - >>> math.radians(45)
! |- ]9 M/ ~; g/ f" h l - 0.7853981633974483
7 }8 O- u# Q6 ~8 z! d& y - >>> math.radians(60)
+ V3 _! Z" F3 [" \* A- ]$ H - 1.0471975511965976
复制代码
- u+ y6 d! e" Y& _0 O# l) @! n$ m$ pmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
" G* E- E% ~! X& v% ]7 w4 \- #把y的正负号加到x前面,可以使用0
* h+ A7 A9 W1 N. N - copysign(x, y)/ {7 ~5 |$ E; `
- Return a float with the magnitude (absolute value) of x but the sign & F+ x n& {/ J$ e
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
1 `6 _7 n! S: U - returns -1.0.
$ G* A0 {. M' p* Q' j
& i& _5 |' I# m0 J$ M- >>> math.copysign(2,3)- c* e- I2 l! V) J+ _5 D
- 2.0
. S! g4 R2 }0 @9 H$ s+ o - >>> math.copysign(2,-3)
. x- b7 J) v: O8 X( ? - -2.0" C+ T# i Y5 g! Y' z0 q& \: a
- >>> math.copysign(3,8): F+ F7 `0 Z( d5 l
- 3.0" _3 c6 ?, Z! W8 i; H
- >>> math.copysign(3,-8)" Y' U' J4 ~, v. L" ]
- -3.0
复制代码
$ J% |- M; Q* n' `, ?* O Mmath.exp(x) 返回math.e,也就是2.71828的x次方, w X0 P; S* U9 n
- #返回math.e,也就是2.71828的x次方9 i) c: I, ^- _1 ?
- exp(x)
$ k" O2 o$ z- s3 z M2 {& ^ - Return e raised to the power of x.! y0 f4 u% } i( M
( i) Z% N) ?; j, @% q- @ {5 \# H- >>> math.exp(1)
6 X/ Y% U& X5 H" V/ K5 ` - 2.718281828459045 [. r/ ]8 u4 Z5 `: N
- >>> math.exp(2)- @% W0 R, K$ a$ e- C
- 7.38905609893065
6 K, m$ x. y. l - >>> math.exp(3)
& J* e4 i. k$ O - 20.085536923187668
复制代码
N/ o7 |! Q8 D+ Bmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
3 X# U, H" ?& z1 K- #返回math.e的x(其值为2.71828)次方的值减19 g) |: h8 g& C" l) y' K
- expm1(x)
, F( d' P4 q) J/ x - Return exp(x)-1.
) k# l2 i* p) f2 Z$ x4 M3 C2 J ?# l - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
# \- j( c6 C$ C* G5 m
* O8 D. u% p6 m$ J; [2 i- >>> math.expm1(1)+ T! \4 T& x( U6 M1 m& A2 y0 p
- 1.7182818284590459 W, a5 Y! t# V/ x
- >>> math.expm1(2)" g, \5 E/ m' u/ w/ H1 F
- 6.38905609893065
& ^ N* l: \# x- Q - >>> math.expm1(3)! H& x# T5 `: o5 }( f; K( g
- 19.085536923187668
复制代码
( ]2 _! h9 Z- L# l, A) V! |math.fabs(x) 返回x的绝对值
9 Q6 p1 @3 W, g4 U, ^/ B$ @- #返回x的绝对值
" f1 q r* Y. U# F! ?4 F - fabs(x)0 P$ a1 K* \4 O, z' g0 z) {, x
- Return the absolute value of the float x.
' m- v, `3 f; M1 H, v% H - K0 x0 u7 P9 w
- >>> math.fabs(-0.003)
4 V2 ^ D1 ] b9 W5 S0 d& Y - 0.003 i1 E% {' N$ i7 p. K; N" H
- >>> math.fabs(-110)8 j+ t9 e, L) X
- 110.0
- i& a1 r7 e! h( D Y0 x - >>> math.fabs(100)
. ^1 { o) u; v$ Y! [5 w - 100.0
复制代码
N( |1 l& s9 a( d, g# Umath.factorial(x) 取x的阶乘的值: d$ ^+ A# B& K
- #取x的阶乘的值2 W9 \ m u l) A# U
- factorial(x) -> Integral
( Y5 e! p, ]0 z& B5 B/ K/ N - Find x!. Raise a ValueError if x is negative or non-integral.# n3 K5 H# g, W! Z6 a% F- M
- >>> math.factorial(1)' u( C% V1 K# K2 S1 q$ v
- 1* r, g4 s1 A/ p; l9 g X
- >>> math.factorial(2)
/ A% o8 j+ T5 B# x - 2
6 ]2 d+ p/ {1 ]$ ^& x - >>> math.factorial(3)
. ^- U3 C( S% ]$ t! e - 67 f4 L7 Q4 f$ x6 F( h
- >>> math.factorial(5)" H+ x F1 `) Y* F7 x
- 120
* L- Y6 t( F i - >>> math.factorial(10)
! M( t7 e1 ]! P- O - 3628800
复制代码 * i: Y0 X% @( N- E% i6 [9 z
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
2 d# ]/ C; X6 a. Z- #得到x/y的余数,其值是一个浮点数& L; ~3 f" Y$ ~ y+ v- H
- fmod(x, y)
, |( T7 O3 |4 X - Return fmod(x, y), according to platform C. x % y may differ.$ L' L( W9 o. l- M3 P
- >>> math.fmod(20,3)
# `3 P9 a: L+ M# D2 R2 T - 2.0% [) n. U" c/ I" W3 a
- >>> math.fmod(20,7)
* S3 X1 q# s: T6 _1 N - 6.0
复制代码
/ g, n2 w+ s. g7 z$ U- emath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& `! ^& _6 m R# e3 U/ q0 K
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
( i. G, e0 g; C2 _% B+ w- X1 ?0 M1 T - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
" n/ U4 H9 f2 n9 F - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( `5 f7 r+ ~! S/ R& l( r! }$ q
- frexp(x)* \' M( Z2 ?* R1 M& q
- Return the mantissa and exponent of x, as pair (m, e).
" o& v' q8 U6 y3 _ - m is a float and e is an int, such that x = m * 2.**e./ C R8 p( e1 _, [* s
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
' M0 } ^5 v; @' D - >>> math.frexp(10)! h4 H7 c* L; L a8 J
- (0.625, 4)
/ ]# V o7 ^7 ?( k - >>> math.frexp(75)9 ^9 X$ B& H3 J) @+ S+ u) [
- (0.5859375, 7)
% O9 m4 W' Z" ~3 C# e - >>> math.frexp(-40)
: ^( [6 J1 N8 |, w' p) x - (-0.625, 6)
, Q: z# S* r( C2 i! L - >>> math.frexp(-100)* Y+ |) F, P5 V, Y2 j( z
- (-0.78125, 7)( {: s& V! Z Q- \& ]) P6 q. R
- >>> math.frexp(100)
" T9 z7 n0 K2 x- ~3 o- E! v2 w - (0.78125, 7)
复制代码
. u5 P1 C* m4 A2 {math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
: S3 t5 x" J M( a u- #对迭代器里的每个元素进行求和操作! N: M2 l& B4 @9 _0 x
- fsum(iterable)
2 d- x* D' ^8 Z( v; Y/ a+ ] - Return an accurate floating point sum of values in the iterable.& J) C2 ~ x' ^6 n1 v6 y) |+ d( E
- Assumes IEEE-754 floating point arithmetic.7 f1 t$ Q; v5 G' q
- >>> math.fsum([1,2,3,4])
+ E) s g$ V% T' j! E" `, M - 10.08 Y X/ i# ]9 f% O
- >>> math.fsum((1,2,3,4))7 h) `$ D! z: V) H
- 10.0! x+ R4 X; b7 z4 w
- >>> math.fsum((-1,-2,-3,-4))
4 F( D* k1 e2 M9 t o4 _0 u, Z, {9 G - -10.0& ]( @5 a% {% {) M! b5 [0 T
- >>> math.fsum([-1,-2,-3,-4])
+ S" I( J& w: z% s( A2 y - -10.0
复制代码 4 m' ]6 f- f) R: k5 `
math.gcd(x,y) 返回x和y的最大公约数
7 ~# @4 |& J& p; ]- #返回x和y的最大公约数% a, d( p/ d9 `3 u3 X, ` j
- gcd(x, y) -> int
" A; o- b' S2 f" o2 m+ w5 f1 | - greatest common divisor of x and y" X3 T v! R1 v
- >>> math.gcd(8,6)5 _( v# d) W D. ~) ?' K) d
- 2
. w) X+ n/ L, x- H& Y - >>> math.gcd(40,20)
5 h- R: C1 w- r9 S# A' j8 u - 201 n5 H) Y1 l/ h
- >>> math.gcd(8,12), `, H3 p! n% g+ E" h' R) s: T
- 4
复制代码 ) I1 F) _) Y8 C* N4 E' I ?! G/ |
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* \4 O. `/ }: u- n, f* o$ y- #得到(x**2+y**2),平方的值
% P, w5 m2 w& i, G' F( Z0 f2 J - hypot(x, y)0 X: i! ~5 ~/ d' ^$ I! X& E
- Return the Euclidean distance, sqrt(x*x + y*y).
) y. C) p& F2 v2 b+ S - >>> math.hypot(3,4)% }+ P" o; q7 Y
- 5.0
% f# ]; C5 N5 T. f - >>> math.hypot(6,8). P( k8 H/ P, `, S+ o( E3 G
- 10.0
复制代码 & y. L8 h, l- b0 n
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
6 _5 J8 Q- @1 @. l8 E- #如果x是不是无穷大的数字,则返回True,否则返回False, u' `/ `# `2 K. R- a
- isfinite(x) -> bool1 l& Y: x4 R# v0 f
- Return True if x is neither an infinity nor a NaN, and False otherwise.
2 w: [/ f6 Z; C$ H3 A- T) f1 m8 b - >>> math.isfinite(100)
) |' R2 I, c# F5 W9 i, | - True0 N' E2 C# W3 A; n# l- o
- >>> math.isfinite(0)$ e# [0 i" m$ }% _" N: V* v! F) J& P
- True
' ?- d' K+ M. M0 N# b3 c - >>> math.isfinite(0.1)
% u, `5 u5 L- m" O0 Q8 r. E - True$ e4 z9 J4 ]" T2 @
- >>> math.isfinite("a")
6 r9 E" U, s R/ c9 E) k - >>> math.isfinite(0.0001)
3 {* Q; R5 v8 ?6 R' m% v$ a - True
复制代码 % {3 n' S7 b, y) F4 ^" R
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False {. x. H8 z8 i' n( b1 ~
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
! t) I% j N% f; L - isinf(x) -> bool
; Q2 }. O) }7 K5 y8 { - Return True if x is a positive or negative infinity, and False otherwise.
% z4 S. U& N' N$ k( ^( o$ i5 i - >>> math.isinf(234)7 u/ O4 W1 N7 m2 [7 T, L
- False. k# q+ m* V8 i
- >>> math.isinf(0.1)
# P P0 ^# w/ F' B0 R - False
复制代码 6 J, h) ^+ F0 o B* E% x/ t
math.isnan(x) 如果x不是数字True,否则返回False3 q4 F/ L3 x: {
- #如果x不是数字True,否则返回False
0 y, C/ I# Y5 m) C' D2 z - isnan(x) -> bool! q0 }4 }( F0 r2 a$ s2 E
- Return True if x is a NaN (not a number), and False otherwise.
! T) y4 x2 v3 O1 u$ c - >>> math.isnan(23)
. p1 k8 V) q4 J: }! [ - False% M- \1 V, z; x/ x/ z) _+ b
- >>> math.isnan(0.01)5 C: m6 e4 J* E- `/ M
- False
复制代码 _8 C/ S |: [0 o
math.ldexp(x,i) 返回x*(2**i)的值8 A# n- u0 ?1 K5 w4 }
- #返回x*(2**i)的值 v' U# ~- i8 @4 q, n
- ldexp(x, i)% R5 S* x& G9 j( ?# A0 o
- Return x * (2**i).' \- Y% W: B7 W0 U9 F5 i
- >>> math.ldexp(5,5)
0 K% f0 K/ _0 g4 H* D$ z5 f - 160.04 W( C4 D/ y. T% ?
- >>> math.ldexp(3,5), F* a$ D" r6 h: t ]
- 96.0
复制代码 ( m1 S4 c/ {4 w. F- I
math.log10(x) 返回x的以10为底的对数
& u& @0 N: j5 k- #返回x的以10为底的对数. ^5 u8 {4 ~" i% {: G
- log10(x): U! |" k: }/ S y& c( b @
- Return the base 10 logarithm of x.
3 _* s6 f+ Y& w) \: h - >>> math.log10(10) d( }5 }" H$ W) W. S5 z. z
- 1.0
9 I/ A- b1 f Y' f; t - >>> math.log10(100)
6 r& g* Y( i- G7 U - 2.09 X; p# B! W0 o+ J: M0 o7 V4 r
- #即10的1.3次方的结果为209 G& ]6 j- [5 G7 }0 j- |7 n9 _
- >>> math.log10(20)
- i" R8 Z# n. P( b - 1.3010299956639813
复制代码
% J' p. |; u' ]- M+ W! x5 R. x% n/ M* Bmath.log1p(x) 返回x+1的自然对数(基数为e)的值
- Y, U2 M* y+ U8 _' {6 T# p- #返回x+1的自然对数(基数为e)的值
; P5 y H! l- c( f) x - log1p(x)
; O3 l' f" g0 ~ - Return the natural logarithm of 1+x (base e).2 B3 m8 `2 X/ \9 p; ^. Y0 W
- The result is computed in a way which is accurate for x near zero.
|- S' S! p' l0 e L - >>> math.log(10)7 D/ J, }! m) G0 t# T4 V
- 2.3025850929940469 w2 ], }7 _' [/ T& y$ P
- >>> math.log1p(10)
6 u8 D0 H3 k1 M4 F" r - 2.3978952727983707
/ j1 i% b! h0 R% W0 `1 S2 M - >>> math.log(11)" ~! m' g5 [" W$ h( F' f
- 2.3978952727983707
复制代码 7 y/ ?& [- ^/ O5 t4 \+ ]( E
math.log2(x) 返回x的基2对数0 J. s3 S; g7 N: t1 s% f$ q# l) |# k
- #返回x的基2对数
: j/ S) F5 w. R& w) N! m+ T - log2(x)# N, i; `: @ ^
- Return the base 2 logarithm of x.; u( x0 O) j+ t, q h
- >>> math.log2(32)% X" }" a& @! s4 N
- 5.0
. v- T+ q/ k$ E2 g - >>> math.log2(20)4 |; Y4 w! o3 }4 u% i' Q
- 4.321928094887363! `; B9 p# S" q
- >>> math.log2(16)
" j: a2 v6 h8 G$ I; k - 4.0
复制代码 6 K- U! A& u1 v; k. _
math.modf(x) 返回由x的小数部分和整数部分组成的元组4 f. f& J9 S" l) A3 s* B; ~
- #返回由x的小数部分和整数部分组成的元组
! v' w& K, n/ @ - modf(x)6 r' p$ Q1 q5 u" @ W
- Return the fractional and integer parts of x. Both results carry the sign
Z7 y4 c5 q Z- ?1 d6 J - of x and are floats.: G) B6 ?7 b# Q. A2 I% g
- >>> math.modf(math.pi)
& A- S# S7 C1 r5 v E+ G7 \! t% E - (0.14159265358979312, 3.0)
9 y: c1 I5 P; T0 W; c$ A - >>> math.modf(12.34)
4 C2 q* A* X' m' l0 f- u: g - (0.33999999999999986, 12.0)
复制代码 5 w) K/ H: k& d; B
math.sqrt(x) 求x的平方根
9 T+ w/ `! u% I) w( L- #求x的平方根" p: _5 f" i# M' K! g, F
- sqrt(x)
/ g3 d4 k6 z! K' x% ` - Return the square root of x.
3 [9 m8 Y5 @- ~# ], [# ~ - >>> math.sqrt(100)! s9 d) S2 L0 J& P
- 10.0. G% o8 r% u! d* S; c. G: `
- >>> math.sqrt(16)6 e' w/ |( X0 ~
- 4.0
; J& U3 @! j6 d) h9 @) | - >>> math.sqrt(20)
7 M3 W0 `; c; D% J. ] - 4.47213595499958
复制代码
1 W% n) Q6 v. J( gmath.trunc(x) 返回x的整数部分- #返回x的整数部分
9 [- E4 i- l; S1 M! e) i/ D, u. K - trunc(x:Real) -> Integral
' k8 b8 a- Q( x+ i - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ d! k# O1 [* `3 U" J' b, D
- >>> math.trunc(6.789)5 j% n7 }' s( {0 o$ c' t' ^7 k
- 6 F2 R* H- T* o# K5 P1 V
- >>> math.trunc(math.pi)6 a8 _0 ~ T* v, `
- 3
3 I- _+ v. d1 r8 \/ J. ^ - >>> math.trunc(2.567)
( Q0 J1 V1 J& `3 g. W - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|