马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
& |' C% i4 B" L+ ]* d) P$ g
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
* j' q$ W5 ^0 H5 J; } F2 u2 v E U2 `1 N& a2 U3 ?0 I4 D' @
方法1:: v/ _" F3 |" Y7 d9 L, y1 ~+ t. v
- >>> import math
% @- f. r! w, |6 X4 @* @. m, }$ C1 p - >>> math.sqrt(9); j% u" \. a8 Y2 J# N2 t
- 3.0
复制代码 方法2:
0 e/ L. c- X+ U2 N, r! e/ L! ?- >>> from math import sqrt' x2 H- U! A8 f# {9 m) I
- >>> sqrt(9)
; w p5 ~+ k" `2 r - 3.0
复制代码 & q% L4 q7 Z0 d2 @! n
$ Y8 k( V: h; j3 v5 I8 Qmath.e 表示一个常量
# p4 C; [$ P6 R- #表示一个常量) d- A# G X3 c
- >>> math.e6 Q9 [1 }: D7 x& O3 D" S8 J
- 2.718281828459045
复制代码
5 Z; o1 F3 ]; M; J- cmath.pi 数字常量,圆周率& j2 @& M, l7 m
- #数字常量,圆周率) p& L/ I2 |( z, z" \' |$ X$ ]% E
- >>> print(math.pi)
1 ^! g# t; ?& l) E - 3.141592653589793
复制代码 1 J& M0 H! D6 Y4 B5 N* c2 A$ W8 b% v
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
: A9 N1 m4 V( I$ p- #取大于等于x的最小的整数值,如果x是一个整数,则返回x& H6 o8 j/ R. w7 L9 \
- ceil(x)
" W3 h; W9 g1 n2 }( z- U- h - Return the ceiling of x as an int.
9 Q3 L0 f0 N! O+ g% ]- a - This is the smallest integral value >= x.
, b% H# u/ c$ u" d) {" X - + n+ G. p, n7 d: p( k5 J
- >>> math.ceil(4.01)
" D1 X x3 y& ^ - 5+ Y9 [! F" Z, _
- >>> math.ceil(4.99)
! P) e# d6 Z7 s L ] - 55 x+ `2 W7 [, D9 m* y
- >>> math.ceil(-3.99)
3 T6 `! F$ m6 c1 M - -3, A$ u5 {# }' F( x
- >>> math.ceil(-3.01)1 }/ V% U l% b, e+ ~/ Y
- -3
复制代码
1 M/ E( J. i& mmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 }2 P$ Q1 A8 p
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身, _7 g# a2 z! d; G6 R
- floor(x)+ Y* Q( a, `! g* `
- Return the floor of x as an int.5 j, T- f* {/ \4 ^; E' r
- This is the largest integral value <= x.7 t+ K4 d X4 ~* Y% G; N
- >>> math.floor(4.1): I" T; ?2 ^5 T" K
- 4( J; H5 {7 [5 J" M# X$ ^0 C) b, n
- >>> math.floor(4.999)& u; w8 I, z2 ~; I# p
- 4
( A0 k% G6 P/ e- Q, x$ D - >>> math.floor(-4.999)
( P0 h7 W3 Y: M: H6 {5 D& [( x - -5
3 e: v* Y. a5 m# E1 ^: A. F - >>> math.floor(-4.01)
6 ?, Z" ?. J1 }9 }* l - -5
复制代码
: p) T5 _5 d% h0 j0 h, q7 t K; Ymath.pow(x,y) 返回x的y次方,即x**y
3 ~6 D1 [" V5 Q" w$ l! B8 l$ s, \- x- #返回x的y次方,即x**y+ \, i. ?+ X* W& V6 J4 c2 Q0 C
- pow(x, y)
3 y2 h/ B% }9 [ - Return x**y (x to the power of y).
& }6 f7 ?) ^9 `* m+ n - >>> math.pow(3,4)
, M: G2 j$ b; ?% X- u- A - 81.0% ~% h( Q2 o, f. _ z
- >>> 8 Y; f7 G1 w a% j$ v/ K! u
- >>> math.pow(2,7)% r% k; h# d3 h- l& N' L- \, b& n
- 128.0
复制代码 ( P u- z+ @! {/ q8 m
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
5 @( y4 i7 `* f3 q' b% ^2 U& M+ E- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 W$ r/ I1 a. \( ` - log(x[, base])4 h7 ?1 V* k, L$ E( O
- Return the logarithm of x to the given base.
: Z9 ^" N6 _' b b& C - If the base not specified, returns the natural logarithm (base e) of x.! X1 I0 v1 X" m. n7 C
- >>> math.log(10)
: h7 X9 b' c# C7 a - 2.3025850929940469 a p# g1 _! D7 g3 p
- >>> math.log(11)6 u4 u3 ~3 i* { O/ H
- 2.39789527279837078 P C B) R8 B9 c! H7 F8 C
- >>> math.log(20)
1 O7 a2 J4 L2 H5 F3 A! W2 s - 2.995732273553991
复制代码 " t+ |( `1 S: M8 ]
math.sin(x) 求x(x为弧度)的正弦值) U4 w0 ~; Z2 a. P. N
- #求x(x为弧度)的正弦值+ P& v1 x! D, y F- h0 I$ b1 m( S
- sin(x)
! I' k7 b, z$ @3 _+ g+ q$ A# r - Return the sine of x (measured in radians).
, o: `' w8 J3 e$ {/ y0 ]( P# M. n - >>> math.sin(math.pi/4)
3 ]: S/ e9 ]+ `4 ]! h; _ - 0.7071067811865475
6 i! |8 U) |5 M3 S7 L- D+ s7 K - >>> math.sin(math.pi/2)
# ]0 D" T; f. i - 1.0) J8 |2 [8 A% U5 b4 r/ {4 W
- >>> math.sin(math.pi/3)
9 e. F$ p2 O+ y- f! C X3 E9 j - 0.8660254037844386
复制代码 7 O3 P# N1 i7 y/ F/ y
math.cos(x) 求x的余弦,x必须是弧度
" V! h+ v. v. z+ q- }# z2 @- #求x的余弦,x必须是弧度" X U1 i( f r- o( O
- cos(x)" F7 W1 b) ^- y
- Return the cosine of x (measured in radians).
: t" c% ^ {9 v' O - #math.pi/4表示弧度,转换成角度为45度3 Q% S+ D/ d- j* G, b5 `3 u# |$ ^
- >>> math.cos(math.pi/4)" p1 v$ J6 ?0 n, y& J
- 0.7071067811865476& X! o8 h" X( J. _8 _
- math.pi/3表示弧度,转换成角度为60度8 h% t) g; t: L+ G' f+ E* Y
- >>> math.cos(math.pi/3)/ }: ?& { m. C- T7 B
- 0.5000000000000001
$ E* I X5 L! J0 W* t1 _# K9 V - math.pi/6表示弧度,转换成角度为30度
. |, G: m, n+ j, F, T - >>> math.cos(math.pi/6)
. e0 R/ f" F% i) \, j* X: u - 0.8660254037844387
复制代码 9 g( ?8 c3 C" ?" u' E$ s2 u, l L" M
math.tan(x) 返回x(x为弧度)的正切值
; A2 B" ]4 y5 e+ J9 g* k- #返回x(x为弧度)的正切值
! O+ P/ y" h& b4 m3 r7 w - tan(x)
2 |* F8 l. J' Y/ V6 z - Return the tangent of x (measured in radians).
. X! h7 ?4 f& x, ~' O" Y - >>> math.tan(math.pi/4)0 L \) {, J" a! F* [! u* W7 i
- 0.9999999999999999
' L" p% o" k1 N. {; n4 | - >>> math.tan(math.pi/6)
2 n+ l" ~ U! y0 H - 0.5773502691896257
' Q6 P( D7 O' k* u/ w" A - >>> math.tan(math.pi/3)
/ d d1 b; b' d) |" i) n - 1.7320508075688767
复制代码 1 k5 g' f1 p W# D( Q5 Y
math.degrees(x) 把x从弧度转换成角度- S' Z8 ]) l$ F* F* z
- #把x从弧度转换成角度) ] H2 ?; R' P o
- degrees(x)
' s' q, U& S) b6 H1 b! }' K - Convert angle x from radians to degrees.
2 l* [, m$ P9 S- \) u g7 s
" E: \+ s8 ]9 C- >>> math.degrees(math.pi/4)
$ B6 B8 e' E7 E0 ^% s$ | - 45.0
; [7 u. x8 T0 ^9 Y* b - >>> math.degrees(math.pi)2 y! [# R, o2 v* P# M# Y' T- Q; z
- 180.0
& n! j2 m0 h, H4 t, \. p - >>> math.degrees(math.pi/6)( I L1 _* z+ X2 ~- o' s {
- 29.999999999999996: C: Y9 [' }0 r% I' V0 ]: `
- >>> math.degrees(math.pi/3)
+ O3 ]3 @$ ]8 L+ [; P3 @0 I - 59.99999999999999
复制代码 ; F/ i' \. u0 ?- ~& q' J T
math.radians(x) 把角度x转换成弧度
! V" J7 d+ @. o- #把角度x转换成弧度
1 @% L' T+ d3 [4 p - radians(x)
* m3 ?1 C; L8 ^# a" ^ - Convert angle x from degrees to radians.
- P+ _# ~. {; S+ k; K O- X" j - >>> math.radians(45)
7 X/ Q. k0 b a" R; C6 E! i" j - 0.7853981633974483
' d( L1 h* D: S - >>> math.radians(60)
% f9 X" q0 A1 B$ B6 l - 1.0471975511965976
复制代码 2 T1 x6 b4 e7 U! r _1 Y
math.copysign(x,y) 把y的正负号加到x前面,可以使用0- v" e: k3 |7 U( h5 O
- #把y的正负号加到x前面,可以使用0
6 Y3 L7 L) _! c8 _/ Q- F - copysign(x, y)
7 v- {1 Q6 i6 b) k- A - Return a float with the magnitude (absolute value) of x but the sign
0 x. Z' t. t! }6 V - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
/ } I6 b& T$ D/ i( V - returns -1.0.
% m1 n8 K+ M3 N2 T# o( }
) V* A0 T5 R2 p' c- \8 c- >>> math.copysign(2,3)$ L! ?; p& k& O( W$ a
- 2.0
% U2 t" ]/ N# d% w- j8 r - >>> math.copysign(2,-3), [8 t9 F! M$ T0 A
- -2.0
2 b5 S" s: i5 _" K; T- y: r3 h2 d - >>> math.copysign(3,8)* [4 H) r2 s. {2 V9 Q) R9 o
- 3.03 Q* f/ ^% a9 k( H; j
- >>> math.copysign(3,-8)7 r6 S2 {% ^7 r: [: L, i
- -3.0
复制代码
4 _; B i/ p9 N* @8 S! N; |math.exp(x) 返回math.e,也就是2.71828的x次方
; h2 d E8 I+ z) }- #返回math.e,也就是2.71828的x次方
- j* H5 R% q, e7 ^+ W - exp(x)
6 G' B0 @/ t7 u2 g; U6 I% S8 s' ? - Return e raised to the power of x., m7 g/ ~1 }; K
- ' K1 f+ ~* _7 A3 _$ @ t6 |% ?
- >>> math.exp(1)
: B% M6 ?; q) G' Q3 {9 F* G1 R - 2.718281828459045
9 r; `* V5 A) f7 Q' j. h - >>> math.exp(2)
0 f$ a2 L3 S2 j7 ?9 B8 B2 ^ - 7.38905609893065; |6 T3 j. O6 A# c: h# K
- >>> math.exp(3)6 V! C$ v- g. F+ d# H, x
- 20.085536923187668
复制代码
1 S7 E% M5 W" ?( Wmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减10 Z) b3 i- G; }# @; s5 b& S4 S
- #返回math.e的x(其值为2.71828)次方的值减1
8 _& D! R- ^* H; d9 K# t - expm1(x)% ]5 W9 j2 o( P+ m+ u5 F. G( a, D
- Return exp(x)-1. t& G% i! } g) g; I% e6 |6 j
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.0 T1 {. L7 K8 ?
u0 ~$ Z' A- \5 E9 [- >>> math.expm1(1)
& `. N+ s- H2 u - 1.718281828459045
1 j% R- r; p, Y$ V V5 } - >>> math.expm1(2)* [) l5 V' k+ H5 o
- 6.38905609893065* B8 @6 u# @# q, h( ^. j+ [, t- m
- >>> math.expm1(3)
9 T2 n, P# @8 v6 K - 19.085536923187668
复制代码
" ?6 R! U9 O: B% ~+ [% V) @/ ~math.fabs(x) 返回x的绝对值2 V, z- O, | _; H, z" s L6 q
- #返回x的绝对值! @9 v6 r& H* C. ] E" Z" w
- fabs(x)
5 o- B, ~" W* w - Return the absolute value of the float x.; Q: i7 Z& S' S6 k" B9 a
- * Q3 x% N! @5 _. m- s" L1 v V
- >>> math.fabs(-0.003)
) R# [! O, K3 A - 0.003
$ ~7 \! N/ M$ c, h( Q - >>> math.fabs(-110)% k% L+ m# g L. `7 q4 w! \6 R/ {( W
- 110.0
# _( P' X( v! v) X+ K6 n; q3 n& P - >>> math.fabs(100)3 _% i* M/ f1 { L/ ~ D; \+ h8 I
- 100.0
复制代码 * B/ _/ z' W; }; q
math.factorial(x) 取x的阶乘的值
0 ^; y( x, r; F$ g7 n4 b- #取x的阶乘的值4 V/ |% O, @! @
- factorial(x) -> Integral, [0 H$ i/ l1 V
- Find x!. Raise a ValueError if x is negative or non-integral.- G1 q9 f2 n- r8 Z8 B
- >>> math.factorial(1)
. r+ w: u6 a9 C5 a - 1- _& y4 j. _/ ]5 x3 H2 |
- >>> math.factorial(2); i; O' Z1 c: j! k( [& b
- 2- R+ {; y$ s- h7 l/ P
- >>> math.factorial(3)
I0 W. y; x! m' U; c - 6& p5 y! {( a& K* N9 N0 E
- >>> math.factorial(5)
" o# {' r! [ K* u8 D9 X6 { - 120 T3 b: {1 j8 R; I, W
- >>> math.factorial(10)
( k" g M: Y% d# \- R! c - 3628800
复制代码
+ q9 v" v7 Z: ?math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
% O- h. y" A. _8 _) H- #得到x/y的余数,其值是一个浮点数
2 I5 ?; z) B( L1 U - fmod(x, y)
+ m' a/ ]+ u9 |% [ - Return fmod(x, y), according to platform C. x % y may differ.
1 p; b2 o* B0 E! N5 K4 b - >>> math.fmod(20,3)
) k4 P5 I; i! e5 \8 Z8 }, x - 2.0. c, G; }" b. {6 c' f7 B/ Q
- >>> math.fmod(20,7)
1 Q0 `+ j- U1 {& p - 6.0
复制代码
q: h( D+ X* ?- [& \2 amath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 ~$ J, E# y$ j* [0 B% @3 }4 P- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
# L* @- Z# ]7 | - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值! c- G& Y* X9 t: B n
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
" @, y' }! f6 S# h* m - frexp(x)
' e/ V% g5 D: Z% D/ K( Q - Return the mantissa and exponent of x, as pair (m, e).
# b% I8 T2 P/ L - m is a float and e is an int, such that x = m * 2.**e.$ n% b# ]$ `- U5 m# g5 w6 Y. m8 u
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
0 Y. o8 w+ _) x; g; k - >>> math.frexp(10)
" k+ h, P3 p6 L! A, c+ v9 W - (0.625, 4)9 p$ w7 R0 Z& v5 @
- >>> math.frexp(75)
6 o* h$ N1 ~4 Y8 Z( q - (0.5859375, 7)4 c' {0 r2 k! [% F
- >>> math.frexp(-40)
) `1 |% q; N. i3 u- f; F - (-0.625, 6)
" H, o8 j7 J& e0 Z0 N3 e; O0 T - >>> math.frexp(-100)" _+ C7 S; i( n/ A( h8 n
- (-0.78125, 7)( i0 K" p! F; l2 o5 {
- >>> math.frexp(100)
; |' S* h3 w, [6 |+ x - (0.78125, 7)
复制代码
( k& A3 [+ \, D1 H2 h1 emath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)) K, l+ w) {- R8 I( V, \, l% \
- #对迭代器里的每个元素进行求和操作2 Z/ e+ Q; a2 R1 q6 }
- fsum(iterable)
0 }' F4 L9 W9 G9 f - Return an accurate floating point sum of values in the iterable.
" N) O- E# r# w, n - Assumes IEEE-754 floating point arithmetic.
" [8 o8 d4 ?% F% ^: B4 l - >>> math.fsum([1,2,3,4])- J) q% |9 F+ p- c
- 10.00 S* J9 a7 S4 s0 i4 x
- >>> math.fsum((1,2,3,4))& J; |) V* h1 E3 I% b- x: M
- 10.0' K" ~' Y3 | u, H
- >>> math.fsum((-1,-2,-3,-4))
+ K3 z8 B2 c, k8 R! k6 q - -10.0/ h6 d* N0 G+ [
- >>> math.fsum([-1,-2,-3,-4]). Y: x, ^, P" _* Z, x& p
- -10.0
复制代码
, q# w) L. T9 w) _) Wmath.gcd(x,y) 返回x和y的最大公约数" ^; I3 K$ g8 ^* v
- #返回x和y的最大公约数* I0 k- A2 m! i6 d$ q: b: \
- gcd(x, y) -> int
/ q& \4 F4 d" A/ n I4 S/ } - greatest common divisor of x and y b- U1 l: y$ k/ k K# H9 Z
- >>> math.gcd(8,6)$ a) R$ @; W! A- N; r3 S
- 22 S5 {0 N2 C/ M {
- >>> math.gcd(40,20)+ V( j: e6 H ]8 y1 l
- 20
$ ^8 l9 ~7 u: Z3 E8 ]4 e - >>> math.gcd(8,12)
" {" ] k- w/ K+ u0 g - 4
复制代码
2 h& ]2 R7 }0 j b* `math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
* b; N4 t- o# T# ~. T$ n- #得到(x**2+y**2),平方的值
+ g+ [: j7 ]% L3 I6 a! U5 T3 r. z7 | - hypot(x, y)
8 V* D7 {, @: S - Return the Euclidean distance, sqrt(x*x + y*y).$ F5 [3 j# [3 |; O: _ V0 X, `" O& A9 t
- >>> math.hypot(3,4)
* Z/ b1 x: _3 P/ c! Q - 5.0
j; s9 s$ i0 f4 ?2 ] - >>> math.hypot(6,8)/ _# U9 `3 ^; r# h$ q! }( V* n Q
- 10.0
复制代码
+ U2 f) e2 s6 O$ H) _1 Pmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
4 G) L8 B3 v+ m0 {- #如果x是不是无穷大的数字,则返回True,否则返回False
% ~( w# `7 T$ `- B7 u# m - isfinite(x) -> bool
3 ^& R& f& L0 j; i0 q - Return True if x is neither an infinity nor a NaN, and False otherwise.0 V: p7 H# X; J( C% i! C
- >>> math.isfinite(100)
/ V- r; R0 H: M8 F - True
i1 J; D3 ^" g' x* n% {, ` - >>> math.isfinite(0)
- B, m9 a+ b* ]9 u$ X( c! V - True
. q1 F! j0 G# E4 w m$ u9 n5 ]9 | - >>> math.isfinite(0.1)6 i: u7 A6 q# [$ L
- True
& p8 z) x: \/ R6 F7 A2 D - >>> math.isfinite("a"); M& h8 ^" I( A% @( j
- >>> math.isfinite(0.0001)
/ t: K7 n( g1 I/ T8 h6 B - True
复制代码 2 @: P! Q4 |+ t; L- K
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
' _8 q. W6 ~9 L, \* R3 ^- #如果x是正无穷大或负无穷大,则返回True,否则返回False4 o) q+ i3 c' \: `9 h6 ]3 t
- isinf(x) -> bool; W" `) L4 W8 r$ g0 o
- Return True if x is a positive or negative infinity, and False otherwise.
# H( G" F; A0 C1 T w @ - >>> math.isinf(234)% e; O# J! `* t. e% y
- False# K, f z: p& B8 u" N+ M
- >>> math.isinf(0.1)+ B0 T& T3 q/ \/ r7 F9 o; @. Q' W
- False
复制代码 : u) U! v; k; b' Z6 o
math.isnan(x) 如果x不是数字True,否则返回False
( M( ?4 A: t8 V$ {; h- #如果x不是数字True,否则返回False2 k3 o& w2 M- v w
- isnan(x) -> bool
' }7 N/ H7 k* Z0 n$ \0 ] - Return True if x is a NaN (not a number), and False otherwise.4 B7 W M* ~4 I3 w
- >>> math.isnan(23)2 z8 ?1 o \. W' w
- False
6 A1 _4 k) C: b" V$ J - >>> math.isnan(0.01), j/ B6 {6 k( K2 R' e" v9 n
- False
复制代码 M( D1 u8 ?7 y/ a
math.ldexp(x,i) 返回x*(2**i)的值
4 V& h0 p# K5 ~) h- #返回x*(2**i)的值
9 [& R7 O( O2 m' b+ o( c - ldexp(x, i)$ W: S- c. G! h+ @. y/ c
- Return x * (2**i).9 r+ E1 _" T: k- U5 F0 f t
- >>> math.ldexp(5,5)9 K; ^1 c- g) q V4 u
- 160.0- C+ L! O. L$ Y4 i7 v* C
- >>> math.ldexp(3,5)
3 x$ S8 u# J& O* [& I! Y: w7 G - 96.0
复制代码 ) j U8 q& {) i3 A4 ^' R7 z% i
math.log10(x) 返回x的以10为底的对数7 ?/ k+ A& e1 h) w" |
- #返回x的以10为底的对数
0 A9 S; y, x% o$ v, S - log10(x)% i- q" N8 i8 i$ {
- Return the base 10 logarithm of x.
* U& C( _; A- F( ?2 u" U. p - >>> math.log10(10)* D5 A( f5 M8 \
- 1.0
; H/ u0 N3 q+ g; b6 `! I9 L - >>> math.log10(100)
8 [4 _% a B$ j5 o5 ~ i% O - 2.0
H5 p- I% \4 \ - #即10的1.3次方的结果为20+ j4 Q) v2 R& X. s& r/ n
- >>> math.log10(20)# v! j0 f# `% A4 w* \0 @. i
- 1.3010299956639813
复制代码
6 ?% r# C/ n& i) W/ Hmath.log1p(x) 返回x+1的自然对数(基数为e)的值0 J0 `$ D3 p' D5 R# ]4 a1 N' D* p
- #返回x+1的自然对数(基数为e)的值; y1 l; c0 g3 \9 t9 Y# w
- log1p(x)# o# H) U5 {1 W
- Return the natural logarithm of 1+x (base e).
# P1 x) U4 R2 f2 T$ d - The result is computed in a way which is accurate for x near zero.# w' a& Z& v9 x+ i4 h1 Y
- >>> math.log(10)
( [+ D; V: \6 R9 i - 2.302585092994046: r0 A( [9 o( t2 ?0 U' b R/ [
- >>> math.log1p(10)2 j& I9 k! e$ E( B" ~% w! F
- 2.3978952727983707
6 p0 s! m6 D* ^% b; [ - >>> math.log(11)! z V& ?, A% R$ N: k
- 2.3978952727983707
复制代码
l- O3 q1 E, F$ u6 l8 ]math.log2(x) 返回x的基2对数* \+ z8 F6 m$ E8 U/ h7 @
- #返回x的基2对数
9 y* I+ D2 j+ A% f% _& B2 Q - log2(x)5 V* \4 v2 Z5 y- H U* d
- Return the base 2 logarithm of x., S) \+ v2 Y! H- R' z. L8 l
- >>> math.log2(32)
3 u2 j0 `- C: \, Z q - 5.0
1 {/ y& I( _& e! b - >>> math.log2(20)6 E1 ~* X9 W" ?3 w+ o6 c4 f- a# F
- 4.3219280948873637 g" C0 Y( @0 v$ [ w5 Z
- >>> math.log2(16)6 u, A$ l% q/ l0 W$ R- {* j1 Y7 Z
- 4.0
复制代码
/ |- v/ Y, t; ]! n% Emath.modf(x) 返回由x的小数部分和整数部分组成的元组
7 b5 F, e* b' I7 X- #返回由x的小数部分和整数部分组成的元组
3 g- u! L6 `3 n" t o- } - modf(x)
- k, f; [+ G2 F3 l - Return the fractional and integer parts of x. Both results carry the sign& ]! D3 `. ]2 P! f. Q& A
- of x and are floats.
0 I& p. S) a/ T2 { - >>> math.modf(math.pi), e2 n- q2 ]+ J+ C* J* z) ^: _
- (0.14159265358979312, 3.0)6 G' x' l) V) o2 m
- >>> math.modf(12.34)2 T2 l& s+ g" f( r' O+ @1 r
- (0.33999999999999986, 12.0)
复制代码 - Y6 _) w7 k' A8 ]& c9 v% c
math.sqrt(x) 求x的平方根+ T. H" |6 H9 B+ Z% X* A4 d
- #求x的平方根3 W2 ?4 d+ \+ g9 Q' c6 m9 p8 u
- sqrt(x)
* B. i# m' {6 p; o! q - Return the square root of x.. p3 |+ R) X1 ~4 F1 }- E3 Y
- >>> math.sqrt(100)
q4 W, J5 _3 z, A - 10.0
5 \. H4 q3 i4 N: B - >>> math.sqrt(16)( V ^# ~8 A! F( J. l/ M
- 4.0
$ ^" B- F5 J, D6 R+ E* G - >>> math.sqrt(20)
% d/ P6 o; r7 z% \& u, o7 K - 4.47213595499958
复制代码
: w7 E4 E, R7 m- `8 Ymath.trunc(x) 返回x的整数部分- #返回x的整数部分- W2 J0 L B% H1 {& H
- trunc(x:Real) -> Integral$ t0 H" G6 n7 E+ V
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
& G% x1 w; r$ e( ?# y - >>> math.trunc(6.789)
: P3 M! @" g- e9 q - 6- _1 m$ V+ D( [# w, g1 x4 R
- >>> math.trunc(math.pi), Y3 D6 y' i3 `3 i i
- 3) p' m* G/ Z! n8 J0 ]: m
- >>> math.trunc(2.567)
% E w6 ~: x% Q- W! a3 P - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|