马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 X! N1 O* [% R1 e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ e; K4 b; z0 M2 X2 u E0 h7 K9 H
: ^/ q/ z# O; M5 ?4 S! B3 \1 d方法1:
7 Y8 B8 d) g9 M$ P; S [- >>> import math- W7 X, v0 a( Q
- >>> math.sqrt(9)
+ ^% X( {- T3 p s - 3.0
复制代码 方法2:
, X, G* | N& B- \& `/ H7 Z* }- >>> from math import sqrt$ A* P+ r: S2 e, p
- >>> sqrt(9)
: m% u( Q4 S1 O- f1 W7 @! M* A( ^ - 3.0
复制代码
$ h! k! }# l$ S0 l! | ! T! A# N% @0 M9 \5 Z5 T
math.e 表示一个常量
# G5 ~7 j; J0 s% `6 O& K ?- #表示一个常量
4 z+ z" [9 Q, X% p1 E9 Z. V& n; H - >>> math.e& C/ w: B- v4 Y8 c& O
- 2.718281828459045
复制代码 . t) V+ L' m' B. M/ o
math.pi 数字常量,圆周率
) B9 g6 E2 D+ l9 E- #数字常量,圆周率* K$ a3 l3 U6 C+ H; a
- >>> print(math.pi)# b0 k, D. G# m8 h# E2 }7 d8 c' X
- 3.141592653589793
复制代码 , n5 R2 g) ]' F
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
Y6 y& k4 P3 A! `- #取大于等于x的最小的整数值,如果x是一个整数,则返回x* m0 I' s- Y2 |6 l2 [
- ceil(x)
5 U6 [0 k$ r& } - Return the ceiling of x as an int.! x, \8 O0 A, Z
- This is the smallest integral value >= x.
, M9 {1 K0 w7 D! S( g$ a1 \ - ( S8 x. T& o* g: L: v5 n) ?
- >>> math.ceil(4.01)6 X# P! u2 M& N/ ?3 T& q/ C
- 53 t: D4 c# e3 d
- >>> math.ceil(4.99)) X5 W/ I' E6 H' c/ \# F2 ~* i3 s
- 5
8 w" Z3 [0 s- u) l* q b$ r+ I - >>> math.ceil(-3.99)
: Y6 J9 i/ L& V* W) Y - -3
) l5 }9 J: E$ _' k - >>> math.ceil(-3.01)9 G7 \+ e Z* n" F* Q2 K2 `
- -3
复制代码 4 Y0 n% h" g' M
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ w' A% |- Z) ^' ?& @
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- w4 X' i2 w7 N2 i3 u
- floor(x)2 C% a. G: M2 c
- Return the floor of x as an int. M9 I4 \6 e+ Z9 z' p, P. _
- This is the largest integral value <= x.6 @3 V" m3 Y# w" q/ {, h% [) ?% k9 A
- >>> math.floor(4.1)3 i) R7 m3 ?$ h% z0 A' b
- 4' p7 D _4 L% N8 |1 k5 q9 U6 i
- >>> math.floor(4.999)
: p* B/ k+ C W - 43 k# D; Z" F( ~
- >>> math.floor(-4.999): q1 x( E% J% F; r6 K
- -5
2 q8 A7 v, ^" q( }1 y) x( J) L: \. O - >>> math.floor(-4.01)
% C3 t! l+ W' m2 z - -5
复制代码 * V1 [0 ~4 L0 I" z- c
math.pow(x,y) 返回x的y次方,即x**y7 z# T! I- V& S# O& [' ~) H
- #返回x的y次方,即x**y
8 R# |7 i/ \2 e3 w3 Q - pow(x, y)0 e) D: ?- O) n; z
- Return x**y (x to the power of y).& z3 F7 H7 q% g3 Z
- >>> math.pow(3,4)
' [9 [8 u7 p r# p' Z7 b" A - 81.02 ^" a* k* k% ~( c; p
- >>> ! S& r( {# o8 q" ?
- >>> math.pow(2,7); w! ?4 J+ `; G$ P2 I+ g
- 128.0
复制代码
% R% ~+ Y4 B6 F- F& {math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); f; M V0 M- I/ R( l& {/ k
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 I) L+ H$ d0 b' i" b
- log(x[, base])# J! a$ W4 f7 `9 p& _( i7 u x
- Return the logarithm of x to the given base.+ [: e, Z) H# m3 F) W9 J
- If the base not specified, returns the natural logarithm (base e) of x.
, k- q1 _: C8 M% U4 o. V - >>> math.log(10)6 Q; h8 k! G- [+ M/ ?
- 2.302585092994046% F5 t/ G2 t5 b _! j
- >>> math.log(11)( w# J# \8 J3 Y6 D
- 2.3978952727983707
% a7 d# X Y# r - >>> math.log(20)/ C% U% Q' Z1 @. ~! Y
- 2.995732273553991
复制代码 5 V$ b- L6 b1 s, R
math.sin(x) 求x(x为弧度)的正弦值
& h! \+ O* k; A" B" V, J. R- #求x(x为弧度)的正弦值
; Q; u5 z; o9 \ - sin(x)) h8 j2 o. d* x# ]
- Return the sine of x (measured in radians).
, G2 i/ j/ d" t+ _3 J& ?4 N6 o- ? - >>> math.sin(math.pi/4)
' z. b+ l b: N" R: ~- D# T - 0.7071067811865475
0 l- P0 O& \7 h1 H/ S2 }' R - >>> math.sin(math.pi/2)
% a) d$ r8 N0 Q# {: u - 1.0
$ j: E- v k' x( U! O! s( [( K0 S - >>> math.sin(math.pi/3)8 F. B& a# L7 `8 z& ^# R
- 0.8660254037844386
复制代码 ' X4 I3 D/ z" D2 X# ] a
math.cos(x) 求x的余弦,x必须是弧度
8 H2 e# G( j1 i- l, t0 Y4 i/ d- #求x的余弦,x必须是弧度5 U+ b, j5 s) g' F) u6 G- g
- cos(x)6 X7 P! p. l% [8 F$ m& M' S/ T
- Return the cosine of x (measured in radians)., a& I6 S1 ]8 R2 z
- #math.pi/4表示弧度,转换成角度为45度
, r4 ?$ k6 N8 l# ^ w+ p+ `( } - >>> math.cos(math.pi/4)3 n z+ \. n1 h% o O% S6 @" L
- 0.7071067811865476
$ f1 J4 O9 c3 N5 s! f P7 n" b - math.pi/3表示弧度,转换成角度为60度( K( Z3 J7 j7 n. G3 |4 ?
- >>> math.cos(math.pi/3)
9 _" W+ E2 J0 L4 v2 L( Q4 H! L - 0.5000000000000001
. r8 a2 }! ~5 W2 H! B$ b& ^3 P - math.pi/6表示弧度,转换成角度为30度
3 h) D' w7 I8 t% F I; D6 V( S - >>> math.cos(math.pi/6)
- Y& i+ b/ D8 B: ?+ D5 n: v( i1 t - 0.8660254037844387
复制代码
& m j: H/ I& T& c: ?, Emath.tan(x) 返回x(x为弧度)的正切值
0 c( j& k8 e6 L& F6 I5 I0 x' |( T$ c- #返回x(x为弧度)的正切值
$ y+ L" ]9 Z1 r1 O - tan(x)0 m( @% E% R ~5 R
- Return the tangent of x (measured in radians).
( e( V C2 d i) x- t& s - >>> math.tan(math.pi/4)
$ t9 O- V; k: B2 J1 ^" H - 0.9999999999999999
$ j1 h; {; W* x5 V) B - >>> math.tan(math.pi/6)9 W( N# @! t9 l& w8 j H7 ^
- 0.57735026918962575 m" ~5 I- X4 G
- >>> math.tan(math.pi/3)
$ x' t H2 G$ Q; O - 1.7320508075688767
复制代码 1 P* R9 S6 d2 u
math.degrees(x) 把x从弧度转换成角度) K+ F$ P2 }$ b3 ~! r5 z1 r
- #把x从弧度转换成角度
$ ]% g( R C0 z$ n - degrees(x)
" ^8 G& ~& P& z - Convert angle x from radians to degrees.- l' @) |. J( v+ q- `6 z
% s/ n# h9 c5 q+ a5 J4 ?- >>> math.degrees(math.pi/4)
/ T0 L) {$ V0 Z' w! s! J - 45.05 ~. Z4 @3 k; d+ P, E0 X
- >>> math.degrees(math.pi)2 _5 o& L$ o1 r0 o0 {) n% k; _
- 180.0
; @/ N: [# z3 v" M3 V! X, S8 F- x - >>> math.degrees(math.pi/6)
, b0 b* u4 K6 X7 z) _8 C( H" U - 29.999999999999996
+ y6 T+ F8 Y3 J1 K3 ? - >>> math.degrees(math.pi/3)
' r- P! F; t/ [) l: N: w - 59.99999999999999
复制代码 1 ^! a1 V- b6 }4 l6 x ]
math.radians(x) 把角度x转换成弧度
9 K; S; k5 L3 B+ j' v- #把角度x转换成弧度
3 P1 z6 P- q5 j% Y& m* x - radians(x)% d: a2 Q; I6 X4 \$ `0 m
- Convert angle x from degrees to radians.5 `6 l* g+ w6 h* s1 ^9 S
- >>> math.radians(45)
& q# d1 s9 R5 q9 e4 M7 U - 0.7853981633974483
9 i6 Z+ K* a$ c7 P/ G - >>> math.radians(60) I4 Y* J" H0 R W g4 L
- 1.0471975511965976
复制代码
; s0 x1 `. d- E `( ]; cmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
$ G/ `0 |+ {: @) Q- #把y的正负号加到x前面,可以使用0* T- E; _9 F) F- ], u/ V, g
- copysign(x, y), ~7 Q! x& `1 k) y4 ?
- Return a float with the magnitude (absolute value) of x but the sign % {* R9 q4 H( M2 X
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 W- ^% L" E' @$ v/ ~
- returns -1.0.! j l; N$ Z, \7 [% Z0 ?0 j
; C2 x- q; K1 b& s3 K/ H1 z! d- >>> math.copysign(2,3)
. R+ n ^" ]: x( j - 2.0. F/ J0 q, T0 d7 n
- >>> math.copysign(2,-3)
4 R% D5 N& j; W/ V4 V6 f - -2.0
2 k9 F% t# b5 O+ [+ ^6 o - >>> math.copysign(3,8)" j! a5 `+ o' ]
- 3.06 |! j" b2 \1 s8 f1 i- r* t
- >>> math.copysign(3,-8)
: J$ S1 e; ^# j" w - -3.0
复制代码 2 W( B- P1 I5 w
math.exp(x) 返回math.e,也就是2.71828的x次方/ D& B) O) x' W
- #返回math.e,也就是2.71828的x次方
. I3 m- ~1 `# W( |0 L - exp(x)
! i' v5 O: I9 q: g - Return e raised to the power of x.4 ]! I1 B4 a1 q1 I& K. k0 ~! E
- c5 w' p; ?0 E0 |: E8 k- >>> math.exp(1)
- v0 }" y5 H/ v0 ]' z# _; ^* P - 2.718281828459045) w7 X1 e: B% D2 W2 S
- >>> math.exp(2)
' V; O7 B W* i% R - 7.389056098930658 P+ m( X: `% F/ L0 |; a% e1 D* v! r4 y
- >>> math.exp(3)
8 S7 a; p1 |6 ^! r2 P _5 l - 20.085536923187668
复制代码
* V; V- F A: r# d/ a- @math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1* s& M0 l, h! c: x) j6 W) A
- #返回math.e的x(其值为2.71828)次方的值减1
/ {) b& u- X, ` - expm1(x)) w2 B3 T+ U. j# K3 R8 Q2 o- R
- Return exp(x)-1.2 T1 g$ P- R2 }4 s
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.* f$ y) W: y) Y! ?: `/ N3 X3 u
, Y0 n1 @& s, Y- >>> math.expm1(1)/ Q% @' G5 {/ A. h$ _: A
- 1.718281828459045
4 e$ B+ b% x- J1 W7 F" | - >>> math.expm1(2)
" M m/ _9 M" H/ j+ U& c8 H. @. W i - 6.389056098930658 B, m+ n% i: f' F4 Y, g
- >>> math.expm1(3)
7 ^7 n5 r" U" w- \3 p9 p - 19.085536923187668
复制代码
8 B( l) {; S0 J3 m+ {/ y: Pmath.fabs(x) 返回x的绝对值
, X8 @3 p0 X0 H7 z6 F! E- #返回x的绝对值0 k, M, a: k4 u
- fabs(x): w. H0 v8 n2 X& K. A
- Return the absolute value of the float x.+ ]- K" m& e3 M2 n, Z) V! t
5 i) ~ H# w6 p& j9 @8 f- >>> math.fabs(-0.003)
$ ` y1 w% @1 o7 q- `, T( o - 0.003- N A3 r$ }" R# \ F* H
- >>> math.fabs(-110) L- S* w# q+ p* s( A3 x! c
- 110.0
, c0 |+ E, E. ^, }& _ - >>> math.fabs(100)
- N8 t' l1 W+ Y4 x - 100.0
复制代码 7 i' S- W, {& |
math.factorial(x) 取x的阶乘的值
( P A9 t# \- Q8 b# |( R6 \5 l- #取x的阶乘的值+ o+ y( {: G) i5 |: M
- factorial(x) -> Integral* ~: @$ ?( L8 {# h
- Find x!. Raise a ValueError if x is negative or non-integral.# V' m5 T0 \$ i5 `( \
- >>> math.factorial(1)# f5 D# n+ L5 _2 X; L% h/ x0 z# O
- 1
/ g9 V! t) ?( U8 q" _ - >>> math.factorial(2)
6 S g4 Y* }- V - 29 ]0 b2 n( Y5 Q. | {
- >>> math.factorial(3)
" {# [8 k4 F, b8 O* k4 s! o - 68 n6 d1 v4 E( E3 X0 W8 i2 C
- >>> math.factorial(5)
8 V7 n: h9 [( ^& ?2 ] - 120
( J" t1 l+ \) ~6 s4 H+ B+ Z" N: A) l - >>> math.factorial(10)
8 t# N& }& T: R - 3628800
复制代码 0 m1 K* A/ b$ g" Z+ }
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
0 t) V5 j8 h) q0 _9 e- #得到x/y的余数,其值是一个浮点数; Q1 A; m: i4 [4 t4 Y
- fmod(x, y)
3 z; R' A( U8 X9 L - Return fmod(x, y), according to platform C. x % y may differ.9 P e5 f" c$ }" P5 k$ V& e& y
- >>> math.fmod(20,3)
$ r Z, Q! a( b/ N% [( Y - 2.0
4 T1 [ L) t! N+ a; Q - >>> math.fmod(20,7)
, o- C- B1 m; s: T j) m, D1 Z" X - 6.0
复制代码
: s" Z0 n6 {9 ?) {' jmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 O/ a' O# d# {5 R4 w' _7 [- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
; p0 s) V' s( M - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 `# B- [* H9 B8 C6 J l7 }
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- }/ M8 W. J! R0 c! I
- frexp(x)
+ d& C5 u3 p, g5 J) u- X4 q - Return the mantissa and exponent of x, as pair (m, e).
" K& w6 `7 i3 u; c+ Q! F+ O - m is a float and e is an int, such that x = m * 2.**e.# ~- o9 x! ?4 B U1 R
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0. t% y0 z$ E1 S0 ]# ~% F" x
- >>> math.frexp(10)9 l# {) r) n, w; i2 G, d# U
- (0.625, 4)2 |) T0 t! B( k) t. J. C
- >>> math.frexp(75)
; H0 {2 @( I: c( R( t - (0.5859375, 7)
& w7 H d |9 \. D - >>> math.frexp(-40)
1 I& J2 y4 {+ c j3 } P - (-0.625, 6)
3 `9 X; c6 ?1 t4 t* ? - >>> math.frexp(-100)3 `; m% W. N( }: _
- (-0.78125, 7)
5 [' a5 {6 e- v$ H* q - >>> math.frexp(100)
* ^- @6 n( B6 |# u - (0.78125, 7)
复制代码
, a7 j" U' \7 t$ rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
# ]; b- \' H! ]" U3 Y" [- #对迭代器里的每个元素进行求和操作
) O' d/ U$ a6 f0 e! j - fsum(iterable)( _% B8 s, u5 o9 S1 ^
- Return an accurate floating point sum of values in the iterable.
+ c; l0 |; v$ \9 Q( x+ d - Assumes IEEE-754 floating point arithmetic.
% i2 C! Y3 e; h) O6 c - >>> math.fsum([1,2,3,4])
1 R3 ^/ a+ s0 Q/ i7 ~/ T0 S - 10.0
0 @( N+ h% M1 S3 N/ Y! U( ~ - >>> math.fsum((1,2,3,4))
3 \* f* M+ _0 a3 O: F - 10.0
# e! T' @/ m! ]. a4 @5 s6 V - >>> math.fsum((-1,-2,-3,-4)), w/ k! j- V- B- U) | _
- -10.0: a: e$ i$ ]: d: Z! U
- >>> math.fsum([-1,-2,-3,-4]): @7 A+ }, J2 ]- y5 ~
- -10.0
复制代码
. Q8 e/ O" P; s2 Y* T3 Y5 lmath.gcd(x,y) 返回x和y的最大公约数
) l- N3 D$ [2 c; T8 R- #返回x和y的最大公约数* ?3 t' O6 I9 @% c( w% i0 b& n: }
- gcd(x, y) -> int* X1 ]( R f, s6 X
- greatest common divisor of x and y
1 o# D: i* d+ l5 ` - >>> math.gcd(8,6) e" ?1 A( n4 j! D
- 28 v7 y# r2 x3 d' C: Q' ?; i+ C4 d
- >>> math.gcd(40,20)
7 K: R. a9 h6 p9 P0 S; t0 _ - 20
5 f1 ^3 c8 D( n0 K! H: F9 M - >>> math.gcd(8,12); f4 U, I$ a. i, |
- 4
复制代码 ! I7 v% k) t! @/ l" W& e
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False$ A4 i3 j5 E; H. A) o
- #得到(x**2+y**2),平方的值8 N, W+ a8 J( ^6 S; ^
- hypot(x, y)9 E. Z; s2 k4 g8 E, {
- Return the Euclidean distance, sqrt(x*x + y*y).5 z0 z" f5 U2 y5 v% N
- >>> math.hypot(3,4)
3 @! z8 @& m0 ?) S* c - 5.0
' }) L `3 R* \5 { - >>> math.hypot(6,8)
3 r2 b; q: u: _' N8 q - 10.0
复制代码
# v4 h% y1 d" V6 P, P+ }! h. e& Vmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! h9 r$ m6 W ]9 p1 V- #如果x是不是无穷大的数字,则返回True,否则返回False4 U: H" K+ \; o; o4 \% x4 @3 g
- isfinite(x) -> bool
7 [5 C+ g' H8 L2 q4 t; p - Return True if x is neither an infinity nor a NaN, and False otherwise.
1 v" g/ O! v" t! Q& | - >>> math.isfinite(100)
' D9 u$ ~9 H" S1 e - True
' C8 r+ M2 j0 c& R - >>> math.isfinite(0)
/ U _9 N6 I, A* r% f - True
' f9 o, J6 [0 E - >>> math.isfinite(0.1)
. h3 K3 A# x1 {" x1 w3 i1 e - True4 e$ _8 D5 ?7 `* v- P
- >>> math.isfinite("a"): k3 t \9 E: }) Y, T8 a% {
- >>> math.isfinite(0.0001)
& O: u9 d b# |2 J0 U1 d' j - True
复制代码
# ~/ u3 ^: h5 G1 \/ {# ?1 {math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 w$ S- y" _) @! u. y
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
( A' I9 q- w# A& C2 `- G9 \; S - isinf(x) -> bool) t* a2 W( S* S. L
- Return True if x is a positive or negative infinity, and False otherwise., O9 B/ M# H7 d/ i) ]1 R
- >>> math.isinf(234)4 {' ^! @6 o1 q& k! c( L
- False
l2 j1 B2 m% o! n2 g# d - >>> math.isinf(0.1)
- Y0 g5 T0 p$ K) R z6 o4 p% C" h - False
复制代码 7 U: z( Y, a! Z
math.isnan(x) 如果x不是数字True,否则返回False% V) F4 c2 H7 q9 X$ a9 X
- #如果x不是数字True,否则返回False+ _9 J( _7 p# e# [ _8 j4 c
- isnan(x) -> bool
+ f3 \" z' X2 d" J, l - Return True if x is a NaN (not a number), and False otherwise.
( X0 u$ J& w# V - >>> math.isnan(23)
- O: T/ k4 M1 C: V - False
* \9 V+ v$ e2 E ^% ^: ?- A - >>> math.isnan(0.01)
3 K0 I: {8 e% v - False
复制代码
/ Y i2 m5 E& \* [* qmath.ldexp(x,i) 返回x*(2**i)的值* L: M" u* ~/ R0 k4 V- r1 |
- #返回x*(2**i)的值
" `- C3 Q6 }! Q& u - ldexp(x, i)
8 ? ?$ J1 e6 d; C- [0 L - Return x * (2**i).8 x+ o9 V9 E `2 ]" B
- >>> math.ldexp(5,5)1 @/ V6 s* j' f% ]& I! f
- 160.0* R8 S5 L# C) x R
- >>> math.ldexp(3,5)
6 P# A* S6 h+ j" ^6 t' w& T - 96.0
复制代码 8 a3 m+ r: }7 w# V
math.log10(x) 返回x的以10为底的对数
7 t& E) K) \8 a0 {: F- #返回x的以10为底的对数( R, g. r. G2 ~% X" n
- log10(x)! J, j) M, \. @& i" Z3 f0 g2 h
- Return the base 10 logarithm of x.
6 ?2 r% [) {/ a - >>> math.log10(10)9 u: L9 K6 m2 x6 i$ u0 Z- i2 V
- 1.0& [" E5 s& @6 O# n
- >>> math.log10(100)
5 D c; X- c1 i$ j) C - 2.0
+ e6 F$ W3 c, t - #即10的1.3次方的结果为203 y N/ w* b6 m" ]. Z
- >>> math.log10(20)2 A( j1 J( s( t8 j- s7 H7 R
- 1.3010299956639813
复制代码
: x/ Z4 Y ]; y3 Hmath.log1p(x) 返回x+1的自然对数(基数为e)的值' ]6 C" d' e4 y: v: R# g' w
- #返回x+1的自然对数(基数为e)的值. A1 P/ I0 X" W- [, Z. q
- log1p(x). I: n0 c+ @' B6 B; d8 @" [+ [
- Return the natural logarithm of 1+x (base e).
" E [/ B2 U9 q - The result is computed in a way which is accurate for x near zero.+ ~6 n/ B; f5 l1 g$ B5 d: N
- >>> math.log(10)2 ]* ]3 m: u$ j$ _; r2 E
- 2.302585092994046
) W' f- O, Z% T# [4 n4 l - >>> math.log1p(10)
( N* t% q2 ^: f3 O- l+ z. x% G: ~4 T - 2.39789527279837071 f9 n5 _; O3 c" @9 ]: W9 D
- >>> math.log(11)" ]( |, z7 s9 f; U; V/ l8 v5 k% y' i
- 2.3978952727983707
复制代码
+ d1 \" u2 _ ]0 a' @5 L/ Smath.log2(x) 返回x的基2对数' B6 o/ \- O2 q, \3 O+ z7 b; w# f9 T
- #返回x的基2对数
/ ~# _ r8 T4 m, H l$ A7 R - log2(x)
C! d# b8 |& @4 P; t - Return the base 2 logarithm of x.
% e+ y8 P* q' t1 b1 ]' t } - >>> math.log2(32)! {) x7 J& P; F1 C9 u
- 5.0
4 ]; k' O. d9 I, [. u6 l - >>> math.log2(20)6 s3 Y8 F. ~0 w0 F: R9 A& F, A
- 4.321928094887363& q7 Y, B* r5 U" _* O
- >>> math.log2(16)4 n4 j9 q5 K% }: Y
- 4.0
复制代码 4 c$ S! _% u' a& D0 e8 L3 s% `
math.modf(x) 返回由x的小数部分和整数部分组成的元组
2 R# g/ `6 S0 J3 v1 t- #返回由x的小数部分和整数部分组成的元组" G: Z1 x/ P) Y& E
- modf(x)
4 @7 U4 h' P( \. _1 U9 l - Return the fractional and integer parts of x. Both results carry the sign
' d0 U y5 f1 ?+ b/ X# q; N - of x and are floats.
& D P2 S1 \5 q4 q" x& A - >>> math.modf(math.pi)4 z2 X, f# n5 H3 O/ p# e% F
- (0.14159265358979312, 3.0)5 b6 |# t& {3 ^8 f% Q3 @
- >>> math.modf(12.34)4 x" x7 o' A; O( i
- (0.33999999999999986, 12.0)
复制代码 / r% P# ?6 J7 ]& R/ g5 J
math.sqrt(x) 求x的平方根3 d1 ?. o8 f5 x1 q& \+ e" J- b
- #求x的平方根4 F& ? Y; P- k+ F& {4 d4 U
- sqrt(x)
; C) m4 Q7 [3 [7 Q0 u - Return the square root of x." u D! s* `& n
- >>> math.sqrt(100)
3 f6 \; `* B2 x8 o - 10.0' j8 u6 G/ S6 ~. C
- >>> math.sqrt(16)! t8 k2 v7 ^' w
- 4.0
! o5 c2 o/ ]7 z- O' w9 Y - >>> math.sqrt(20)
! h A0 b8 R9 o- I2 c. u; [7 C - 4.47213595499958
复制代码 4 u; w! h8 _% G$ d; K
math.trunc(x) 返回x的整数部分- #返回x的整数部分
5 h- H4 c2 N+ z$ w* K2 [. F* z - trunc(x:Real) -> Integral: ]! ~1 {! n; i% n, n
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 n$ M1 u- n' C4 [' j; x; E
- >>> math.trunc(6.789)
" Q: Y4 X1 r. E9 m( D - 6' f1 y8 D/ ^+ ?. W
- >>> math.trunc(math.pi)# x3 H. w% R' T
- 3
0 g7 R/ J5 u r. k# m - >>> math.trunc(2.567)
$ j, U" M# K' f6 {- x; [ - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|