马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" {7 Q, o9 ~' D8 G【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。" j I7 E* s( d' v7 @( H
5 O4 `$ X, q' a3 v. X6 \$ D方法1:
5 D) e5 M& l+ u8 \- >>> import math
0 M. m9 _( H/ L9 z) A - >>> math.sqrt(9)
- Q2 Y$ U* d6 `4 O4 W: }- [" @ - 3.0
复制代码 方法2:% \% G. ?. ^' O- y7 Z7 P9 |
- >>> from math import sqrt
: l# L, p, s/ k" _* n# E. D - >>> sqrt(9)' _- @& X0 C( q; R, U0 J
- 3.0
复制代码 * _9 h* o0 \( @# {* w- e [
! z9 n& M6 k) b1 b6 u5 g: a
math.e 表示一个常量4 ?& a6 V8 D2 N$ p( ^3 }" ~
- #表示一个常量' f+ A; l; r. L* x
- >>> math.e
}2 ]. d7 A. A3 a0 C2 z- x. Z4 _/ k - 2.718281828459045
复制代码
4 L" F) D3 n( w( zmath.pi 数字常量,圆周率
; U' G1 c2 j; l3 |% l0 m0 E$ z2 k- #数字常量,圆周率
# _9 G3 Y) Y5 h# Q* r. s - >>> print(math.pi)
7 `/ z; r/ a! P. {9 B" W - 3.141592653589793
复制代码
8 a4 A# [7 w7 p: J4 pmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
! Q9 J& c# n# x) K( O- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
, v- j- o0 G4 H: ]7 F' x - ceil(x)- X* f, H7 j0 U B$ q7 _# x
- Return the ceiling of x as an int.0 q% Z* D1 q" v/ U
- This is the smallest integral value >= x.
* i- \6 r/ U2 h+ _% S) U - & j' s, k8 M6 x' ~2 j! W1 a
- >>> math.ceil(4.01)" a# p: _' s4 s5 r4 @
- 5) \0 `7 m& D9 ~/ o
- >>> math.ceil(4.99)3 J) h1 l! v z! c8 M, C
- 51 a# l" n; U6 i+ b% B2 a8 l
- >>> math.ceil(-3.99)) N' G7 p$ }' e h2 u: r
- -39 U) D$ c1 H% L4 N
- >>> math.ceil(-3.01)' P( G* B- p; a- _
- -3
复制代码 2 v { P' ]2 \( a
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身- v, J, X: O5 h: u
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 \3 x9 U; `$ _- j
- floor(x)
& a D0 M! @. Q( R2 n0 u - Return the floor of x as an int.7 B2 k$ t' A3 ^ N( i
- This is the largest integral value <= x.
7 h8 k3 h& X8 \2 S# { - >>> math.floor(4.1)
. ?/ _5 ]' b J1 b - 4* K6 X3 H5 [/ K: f! O
- >>> math.floor(4.999)
8 V0 o' V3 f% }8 m& n; ] - 4+ Q" X, A" n' ^9 E# ^$ O/ |0 Y
- >>> math.floor(-4.999)) T" ]5 L" H t0 E7 U* [
- -5
$ \, S& x$ P( a# ] - >>> math.floor(-4.01) S5 i$ H& _( P q
- -5
复制代码
# H% B9 \. i* K3 kmath.pow(x,y) 返回x的y次方,即x**y B* }! R/ R9 c& i. \2 R; b
- #返回x的y次方,即x**y& x" V; ?1 z( l5 T& K$ R! u
- pow(x, y)
9 I6 `- O7 }. t |7 p) Y - Return x**y (x to the power of y).
( ]6 T7 u( G+ z. W - >>> math.pow(3,4), c9 D7 Y# `1 f! p- P8 G' v5 N. U
- 81.0
- b& {& v4 ]2 P# U j" v; V - >>>
- T4 V" y: _/ m A: |( a& Y - >>> math.pow(2,7)) a( C/ W( B& O' V
- 128.0
复制代码
' f9 Z2 ^: L7 `& }math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ b9 ~8 V" @, d+ C" n9 i. p) O- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* l8 X ?5 h! x% s1 N% ]
- log(x[, base]). ]: q# Q+ M7 O5 S' r# n; }0 E' H' r
- Return the logarithm of x to the given base./ r" U9 ^9 I& J1 z* a8 |
- If the base not specified, returns the natural logarithm (base e) of x.% q5 b4 X7 z8 R
- >>> math.log(10); S, C6 X4 d0 _* y9 C' p& t
- 2.302585092994046
) T; \$ \ \1 j& a' g - >>> math.log(11)2 K2 U y6 {2 ^% l, M
- 2.3978952727983707
# P3 a: R6 r: M% Z) {& p# f: t1 l - >>> math.log(20)
0 U1 w' V2 ]- R - 2.995732273553991
复制代码
$ P* g8 U1 [6 U+ n! P7 j$ b' ?math.sin(x) 求x(x为弧度)的正弦值
j, w1 j/ G- L- #求x(x为弧度)的正弦值5 [ s. ?# A8 |4 p3 C o0 W
- sin(x)
6 b, F( u. D7 l0 g; A* ?( f - Return the sine of x (measured in radians).
0 Q6 c& V+ K2 M - >>> math.sin(math.pi/4)/ f l+ p0 m; v8 h/ F: m- D5 i
- 0.7071067811865475
' I( K, a# s7 [/ R; n$ u q - >>> math.sin(math.pi/2)
( [; U5 N5 V2 q - 1.0! ]2 ?6 w% _7 l i& [# q# W
- >>> math.sin(math.pi/3). B- J) n& C5 X, k: l
- 0.8660254037844386
复制代码 , z& a& l, e( b: g0 |! J/ W
math.cos(x) 求x的余弦,x必须是弧度
4 `' f& _3 i4 W- #求x的余弦,x必须是弧度
+ U/ A5 P8 M6 Q" Y/ e' n - cos(x)
9 e+ ], p2 @6 A/ c j, k - Return the cosine of x (measured in radians).1 l$ t9 p0 }* [5 {
- #math.pi/4表示弧度,转换成角度为45度 b4 O8 V1 Q* K. Y% B
- >>> math.cos(math.pi/4)
; h$ Z& g0 k0 m% Q9 P6 \ - 0.7071067811865476
' V2 c% J' R- K+ F! Y6 i - math.pi/3表示弧度,转换成角度为60度 M& `3 G0 K( i8 }& _0 ?1 u
- >>> math.cos(math.pi/3)9 k G3 Q6 ~9 q6 P, j; Q9 F
- 0.50000000000000012 F, @ Q2 S9 {) n
- math.pi/6表示弧度,转换成角度为30度
8 N6 u! Z9 ]$ X - >>> math.cos(math.pi/6)$ p# d( h M f5 q
- 0.8660254037844387
复制代码 . _! V2 }# S1 O: _. m% w' b
math.tan(x) 返回x(x为弧度)的正切值
; N- J; g2 b5 U, `6 E4 O5 m$ P) N$ \+ j- #返回x(x为弧度)的正切值
( ~9 N" g8 m! ?; W/ u - tan(x)% r7 G2 K9 m- P; G/ G1 k
- Return the tangent of x (measured in radians).0 H0 S& V5 A5 s+ _/ I: E W
- >>> math.tan(math.pi/4)5 H% x' Y+ J* j* L7 \- z6 p% K
- 0.9999999999999999; a4 k# {8 I; s7 _2 c* T3 w- P6 f
- >>> math.tan(math.pi/6)3 z1 J- A& A+ G" u1 j
- 0.5773502691896257
2 y- y' f- e5 p* c, H& h - >>> math.tan(math.pi/3)/ G% j" }& F) L7 p; D; I" b
- 1.7320508075688767
复制代码
! f1 Y" s1 W# B" G) gmath.degrees(x) 把x从弧度转换成角度
7 R7 ]; o J, C1 T" }- #把x从弧度转换成角度% z' k( V+ b7 E$ V# r# r b
- degrees(x)% K. J; e Y1 F8 P/ H9 c
- Convert angle x from radians to degrees.
* D4 z8 w: Z2 Q3 H% v, K
: H- K4 S# v) s, `; H; m' ^- >>> math.degrees(math.pi/4)$ d2 d6 A% D9 O5 O5 Z
- 45.0
9 L* @. |- b# U# I& |/ L3 W R - >>> math.degrees(math.pi)
( s# S: s- T3 m8 a4 _ - 180.0) [5 y( Z. b/ n# |
- >>> math.degrees(math.pi/6)# H' j& m8 O/ S0 |+ ^
- 29.9999999999999964 k" B5 I) P$ [! C
- >>> math.degrees(math.pi/3)
$ g+ C: \& N* i! Q/ ?8 T - 59.99999999999999
复制代码 % L! ]) b3 [7 T6 ?
math.radians(x) 把角度x转换成弧度
2 A. }' n8 S$ B4 L' [' {0 a5 I! p- #把角度x转换成弧度9 ?+ A: f: E* O0 a& A0 T- X
- radians(x). V4 T) Z& e' C+ i8 L/ Q1 B
- Convert angle x from degrees to radians.- o* i H' v4 u7 @6 e
- >>> math.radians(45)
' G) i' Y0 p' [ - 0.7853981633974483
5 x3 h" l# ~; r; N* V1 S5 Q - >>> math.radians(60)% _5 J" s5 a( W
- 1.0471975511965976
复制代码
; j; t4 D2 g5 r$ z: Jmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
" ?* S. P4 {0 A% ~: W$ E; ~- #把y的正负号加到x前面,可以使用0
: E- k. w# }4 B3 D& ? - copysign(x, y) ] K: m( ^2 B" c! q6 o" r; o
- Return a float with the magnitude (absolute value) of x but the sign 5 K) v. i8 \) m; q
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
, z6 v3 }% m1 ]" F6 F5 Q' P' f - returns -1.0.
! H& D) E3 K' K/ G K$ v
8 v( F ]- k7 ~7 \- I9 {- >>> math.copysign(2,3)
2 E% P" c2 X# t$ v8 R6 s3 n - 2.0
% X" O& p2 _) C1 q) B! |+ I - >>> math.copysign(2,-3)
* }) F+ ]& F9 ~+ Z7 K1 N8 C9 C% z2 B - -2.0
T* _: g W( J! L4 x - >>> math.copysign(3,8)
1 q% X- Y [! e! x7 f8 H - 3.0
% l, ]/ b- j6 [1 t4 p2 i0 g$ Z - >>> math.copysign(3,-8)( W' R8 {: A5 p$ N- p! e
- -3.0
复制代码
- M2 ~; F* v3 D/ X5 E! qmath.exp(x) 返回math.e,也就是2.71828的x次方7 R' [& f7 W, T5 `
- #返回math.e,也就是2.71828的x次方/ w. O# }" ]; t0 j
- exp(x)
2 e0 I# o' _" U - Return e raised to the power of x.
. V/ m0 ]5 d3 h8 D; b# j+ z - 8 F8 B: H. |% [/ @0 f2 U- i
- >>> math.exp(1)
' ?% e2 f2 m7 A - 2.718281828459045
5 z) M) l2 y2 v" m4 j$ f7 l - >>> math.exp(2)8 Q e, F+ y& h. D- ?( s
- 7.38905609893065
8 f$ a. t: G) b/ z" m - >>> math.exp(3)4 _- }1 a/ o8 ^; h6 Y& E4 ]
- 20.085536923187668
复制代码 . M" ], E( E3 q/ C: Y
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
# M" g/ l$ S% b: P. |0 g8 t7 ]- #返回math.e的x(其值为2.71828)次方的值减1
; ^8 K+ g% `. ?% n+ v - expm1(x); {& O) z3 ~0 Q
- Return exp(x)-1.
E! `0 l# J( I w* M0 X - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" o# m/ C+ y1 e8 T - " W% {3 \$ ]1 O
- >>> math.expm1(1)
, X& z/ X$ p& |8 w - 1.718281828459045
: Q, W( Z; B* o9 V2 Z& w - >>> math.expm1(2)! ^8 \6 l9 F" P8 C7 C2 X( F
- 6.38905609893065
% d8 W9 J, X" P5 O* N, [* H% x. K; u - >>> math.expm1(3)
. X f! u: @0 R$ [) _ - 19.085536923187668
复制代码 . ]& t: u! T4 r& o
math.fabs(x) 返回x的绝对值2 I: s4 W5 a5 V7 Z
- #返回x的绝对值2 s. J& q! W4 Z
- fabs(x)
7 Q& O4 d: ~9 a - Return the absolute value of the float x.
2 [: ]4 a9 j) C7 W
) v- l) W- t s, z% P- >>> math.fabs(-0.003)
]( V" Y9 ?- ?9 v3 [9 f& d' r - 0.003$ O6 ], D0 f% a A" A3 A
- >>> math.fabs(-110)
# C) y* ^# [ g/ Z: q+ D6 ]; B - 110.0! s' P; T* A- [3 E3 H, t
- >>> math.fabs(100)
5 F6 u6 n" v/ b/ ] - 100.0
复制代码 * b1 _" [/ u" s
math.factorial(x) 取x的阶乘的值4 \2 Z+ g7 O7 P* i5 o( |7 b
- #取x的阶乘的值
8 {+ K% g5 F5 a W) L; y - factorial(x) -> Integral
- J2 U+ W9 g. ?3 w# Y9 [ - Find x!. Raise a ValueError if x is negative or non-integral.
; D: Y$ A6 B4 K - >>> math.factorial(1)$ z! t- X5 [+ H: w
- 14 \9 K! |2 f9 h0 j/ U; {
- >>> math.factorial(2)+ e/ l+ b1 [1 ^: i; i9 D
- 2% ^9 B) a1 A3 J. @& r
- >>> math.factorial(3)0 Z, `: r1 X6 \: M( G+ k8 Y% K
- 6
$ z2 R5 L& H+ b2 E" J - >>> math.factorial(5)
4 M2 r6 _$ t- l" g5 R) \* t8 | - 1205 y! n6 L2 \% e
- >>> math.factorial(10)+ `$ k& }. `1 L# G b
- 3628800
复制代码
4 @+ ?7 y% ]! c9 n' [8 i5 Tmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数# X7 C1 e' Q4 B1 l+ B' F' ^
- #得到x/y的余数,其值是一个浮点数1 w! G5 ~ W% }* h8 W* f
- fmod(x, y)
" j' ?1 a5 O+ H# t) ~* O" g! ` - Return fmod(x, y), according to platform C. x % y may differ.
4 K0 s$ V2 O2 F2 k/ P- A. p+ S6 f$ b - >>> math.fmod(20,3)! [: b+ g) z& S+ l
- 2.0
# U: b( }4 d/ A - >>> math.fmod(20,7)
q% N6 ^- {: D4 b' @5 x - 6.0
复制代码
8 M9 x- e; C+ E' J' Z* g! }math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ U/ J; A4 P1 ]! ?! }$ l$ v# }
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 v( y' V! ? _) ~" g7 ~3 b/ d- n* B
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: u0 w4 ?; b& D; a% L; H1 W( y
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
7 Z; ]# v# [9 H4 g6 ^ - frexp(x)
' x6 C u& b G u ` - Return the mantissa and exponent of x, as pair (m, e).0 s4 V& k" Y5 d/ U/ q
- m is a float and e is an int, such that x = m * 2.**e.( w7 [2 q! _7 V4 x# D9 V
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0. `+ ]: ]' f+ w
- >>> math.frexp(10); M6 R- x( H- f0 H
- (0.625, 4)
2 q/ ^; b8 X+ t' b7 e, ^ - >>> math.frexp(75)4 Q( l$ M. P! W8 s4 y6 a v
- (0.5859375, 7)
' Y' Y# B- \4 X. u' Q - >>> math.frexp(-40) E9 Y% Y2 z( v
- (-0.625, 6)( F1 e1 m {& r
- >>> math.frexp(-100)
$ U+ t9 \8 a& S$ e4 T! g, b - (-0.78125, 7)
7 f0 M$ P$ f8 I3 N6 N - >>> math.frexp(100)! x* m/ e; m& V
- (0.78125, 7)
复制代码
( b' H: M% N/ e/ Rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
& v) u4 N! W; s% H0 R% m5 k6 l- #对迭代器里的每个元素进行求和操作( j% Z R! Q1 w' G8 g( P/ |! {
- fsum(iterable)
3 t1 o9 e% S- {! N8 d# m, O - Return an accurate floating point sum of values in the iterable.
% t' ^, y- p5 e+ {* L - Assumes IEEE-754 floating point arithmetic.
' L+ g" A' _$ }/ d* U9 P! U& [1 p - >>> math.fsum([1,2,3,4])
* m- d& p6 w4 u! G - 10.0
2 c: J, t& o* q B5 Z* m - >>> math.fsum((1,2,3,4)); n7 k: c4 r6 Y% m3 `" z, v' t9 T
- 10.0; w/ ?. N5 ~. D. X! S+ U. S( n0 [
- >>> math.fsum((-1,-2,-3,-4)) P% D& v- u0 a+ z
- -10.0# ]$ w) j ~# m0 T* ^9 y! k
- >>> math.fsum([-1,-2,-3,-4])' F1 O1 `& \; D v0 J
- -10.0
复制代码 6 x, n4 [$ W7 H+ i# v; _' w$ E
math.gcd(x,y) 返回x和y的最大公约数0 a' M3 d8 w2 b% m& z
- #返回x和y的最大公约数4 s, e, T% Q9 E N6 L1 m
- gcd(x, y) -> int
' t; T0 S* s" z7 K% M4 h( n; i - greatest common divisor of x and y
5 x) k# \. B6 e( I. Y - >>> math.gcd(8,6)
4 K+ V9 ^; }! D0 P - 2
5 T2 a, q/ Z$ w- Q, L" r - >>> math.gcd(40,20)8 }" D7 W0 K0 r& Q M$ X/ R
- 20
4 m) o0 K6 ~0 m6 D: s4 x( Y - >>> math.gcd(8,12) ^/ Q% P' g. K( V# u5 s( ~
- 4
复制代码 2 u: D$ ?8 ?& g' z5 w4 `
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
- l# {9 M; j; A/ Y- #得到(x**2+y**2),平方的值, T$ G' R: g/ i+ \0 Q7 c
- hypot(x, y)
. |0 W: Y% D T( R# e- l" r - Return the Euclidean distance, sqrt(x*x + y*y).
1 |8 c8 I1 e5 Z# F* t& m - >>> math.hypot(3,4)0 N' m# V% B. H- d6 f w
- 5.0+ b; b$ _; s. x2 c" f& Q' ?6 P. ^
- >>> math.hypot(6,8)
' v( p& y7 S0 X& K( O( @& | - 10.0
复制代码 3 {* Z% D% k$ `6 {2 X. M. Z
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False4 H4 m. s* @' R. B0 l+ }6 _5 r0 f; |
- #如果x是不是无穷大的数字,则返回True,否则返回False
* K% i5 l. M- T5 L4 j0 \ - isfinite(x) -> bool$ {- B( N8 i, ~7 b( P
- Return True if x is neither an infinity nor a NaN, and False otherwise.- b: W9 e. A! |! S) k, x3 Q9 w
- >>> math.isfinite(100)) k9 y, q1 V) I5 L' D R2 [# _
- True9 Y3 ^0 l) d5 {- N O+ N: }
- >>> math.isfinite(0) P. S! e9 G2 ?, g
- True- n; p3 t7 t1 R' p r
- >>> math.isfinite(0.1)
' C6 X0 N3 M; C! J9 u, O4 q! X3 ~ ^ - True
/ U( j9 j' h1 S# E - >>> math.isfinite("a")
: }- V- n# v) |6 K" v( S$ u - >>> math.isfinite(0.0001)
& P E7 S/ w0 z6 J - True
复制代码 - u2 ^1 m8 S4 \; ~
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False) p2 L" k; e5 ?( o: O/ c" B
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
) S$ G+ s `& E* K - isinf(x) -> bool. R2 c' D8 x, v
- Return True if x is a positive or negative infinity, and False otherwise.
5 k. K# b% O1 w" e6 `7 S* X5 a3 ? - >>> math.isinf(234); K0 B4 j& w) w7 _3 I6 {
- False
2 i- y! X2 v9 {1 K8 i, Q - >>> math.isinf(0.1)
7 ~% H$ ^6 G- v - False
复制代码 j# ?: N+ N) v! ^, }0 W
math.isnan(x) 如果x不是数字True,否则返回False
: }- D! |7 s" ~! g c" H- #如果x不是数字True,否则返回False
! F; @, a E7 }$ [* @: ]# z8 j - isnan(x) -> bool" A! V) K, C5 @9 |/ V' W' L5 j
- Return True if x is a NaN (not a number), and False otherwise.
6 x* L& ~! {% y; N5 z3 | - >>> math.isnan(23)& z( ]( C( {8 Y4 X# k: D& K
- False7 e' n" j7 A' u- y) c
- >>> math.isnan(0.01)
. F! t" o9 K5 a6 M. r2 d - False
复制代码 5 d3 z6 J5 c, A( ?
math.ldexp(x,i) 返回x*(2**i)的值
5 ^# t( t S H+ f- #返回x*(2**i)的值5 n; X: B8 O9 c; [1 i9 N# Y8 Y
- ldexp(x, i)
* @" f, O9 o. I* w$ N; D - Return x * (2**i).- D: }4 V! A% V
- >>> math.ldexp(5,5)$ z4 u# X' I0 b- ?! v
- 160.0: d+ c1 l% k1 ]% P; D0 h
- >>> math.ldexp(3,5)
- R: E8 S% L" o9 i8 ~; G - 96.0
复制代码
& _$ b9 _9 M* r6 h7 x( e4 X- Y/ Bmath.log10(x) 返回x的以10为底的对数
, g T! F3 z" n- #返回x的以10为底的对数
5 R9 i% Z5 S/ K- S/ H - log10(x)1 r$ O" A% M6 I m
- Return the base 10 logarithm of x.
( }( ^% C' Y! ^* k$ X/ ?( l/ T - >>> math.log10(10)" a' J# e3 j, [' p
- 1.0
5 J3 F2 Z7 s9 I - >>> math.log10(100)! R8 }# [+ S9 J
- 2.0% J) W; v6 o2 {9 Y0 ]
- #即10的1.3次方的结果为20
. W- U% Y$ Y: M! b: p( f2 } - >>> math.log10(20)
8 G" c: F$ B/ ]0 A - 1.3010299956639813
复制代码
) m5 g3 u$ |8 ~% r$ {$ @7 U4 Gmath.log1p(x) 返回x+1的自然对数(基数为e)的值
7 ~( D; F- n, q2 V! S- #返回x+1的自然对数(基数为e)的值
" Y/ y6 j0 Q$ Q) N. b - log1p(x)
2 @9 F! \+ U, q - Return the natural logarithm of 1+x (base e).
; H4 H- {, Z' `5 ^' |4 V. A - The result is computed in a way which is accurate for x near zero.
# B% ?- N# x8 c1 n9 k! A# h - >>> math.log(10)+ K5 {) n' L7 v+ T5 Q. z% R
- 2.302585092994046
2 p6 s2 F M# N6 o - >>> math.log1p(10)
$ F4 l# T/ b% Z3 c) Q' U1 o - 2.3978952727983707
# b0 W& S& t$ a2 l - >>> math.log(11)4 ]& \6 |, n& o. N; \
- 2.3978952727983707
复制代码 ! ~( z' E; j) F" H
math.log2(x) 返回x的基2对数
- v5 I7 E, d6 r: }+ ?, s0 e- #返回x的基2对数) Z7 f$ X& O% U+ ^! q, B% g" M; A3 m, r
- log2(x), _* x6 x" }+ e5 O
- Return the base 2 logarithm of x.6 I/ o( F' R* d9 D5 W0 }. p2 Y
- >>> math.log2(32)
( f7 Z4 @3 i& V0 O+ n c - 5.0; Z& ]) H+ r' H; ]0 x0 n4 e
- >>> math.log2(20)
; b* \3 u% C7 i - 4.321928094887363" o1 Y% [& @3 |4 J
- >>> math.log2(16)
, e* K" P' c; M5 } L! s& Y - 4.0
复制代码
+ E2 f8 z0 G5 w+ m9 Mmath.modf(x) 返回由x的小数部分和整数部分组成的元组# l! W2 X2 S5 T% [
- #返回由x的小数部分和整数部分组成的元组
( T2 v4 Z& D, o4 f7 f) g3 M1 n7 | - modf(x)
/ D4 h( G$ \1 o - Return the fractional and integer parts of x. Both results carry the sign
# m0 Y+ N1 H& |8 U - of x and are floats.4 V" T; ~ V# ~/ r2 t
- >>> math.modf(math.pi)( a. _/ P2 }3 }- ?" ^0 E& W
- (0.14159265358979312, 3.0)" n4 Y. o& O) Y z2 _
- >>> math.modf(12.34). @6 b) h/ F& g+ G9 H
- (0.33999999999999986, 12.0)
复制代码 4 r/ X7 C3 C/ E: u A4 z# M
math.sqrt(x) 求x的平方根
& h0 m5 r' ~* X8 }8 o( `6 q- #求x的平方根
$ q5 i! O, S' n5 k$ p1 w9 @ - sqrt(x)5 R) \3 o' C* Z. ^& Q" N
- Return the square root of x.3 @; s, |8 @3 B% x2 S9 J
- >>> math.sqrt(100)' t" y1 Y- g8 _ H' T3 B" ~% Z
- 10.0
# {" t9 O$ w1 P. n, S - >>> math.sqrt(16)
8 X$ V7 ~% v3 p) R. P- y - 4.0
0 G8 r) ^4 I- W& H. x( j - >>> math.sqrt(20). E2 y3 D. `5 D/ M9 f0 M& R
- 4.47213595499958
复制代码 / Y( B- ^: J5 d: K
math.trunc(x) 返回x的整数部分- #返回x的整数部分
- ]# d; Q- B* s( ?; R8 a. o - trunc(x:Real) -> Integral) t+ e1 @1 a0 m& b% k$ O) x
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 L- F; w4 C, A1 F
- >>> math.trunc(6.789)
1 H8 W5 B9 ?( r( p; o- c - 6
+ Q: C" |9 k9 b - >>> math.trunc(math.pi)& W$ c! `+ J) g9 c
- 3
2 l8 @- K, q" T7 t Y, j - >>> math.trunc(2.567)- z r' g5 _! [5 w% b
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|