马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 S# `! R( l3 Z3 a6 L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。" @3 o9 O) K: U
' T7 L4 D! l* N3 w! Z5 m
方法1:
5 B4 x4 v5 X5 X" q1 T# i5 ~- >>> import math
0 y! r6 r) H5 I0 t/ e - >>> math.sqrt(9)4 @; j0 K( |; s8 m% S1 g/ [" H
- 3.0
复制代码 方法2:
( X6 j. C3 e- G- >>> from math import sqrt$ }* x( `. ` J2 I I3 d$ m
- >>> sqrt(9)
! H2 f$ H& g' S5 n, O- l - 3.0
复制代码
- T8 h5 ^& B6 F, |/ J; R( W" @ 8 t |' e/ E4 v2 v1 V5 c
math.e 表示一个常量
. d5 Y* S" ?& G0 M) b, `! z: w- #表示一个常量
0 X$ @, a) O8 V% ] - >>> math.e9 ~# A0 e" n6 r! i. r" q
- 2.718281828459045
复制代码
3 r% Z" S4 o! n# g, q7 g4 Z' fmath.pi 数字常量,圆周率
" [4 z7 H0 p; ?, W6 J& e- #数字常量,圆周率
/ z$ c7 \ S* z* k* D- i - >>> print(math.pi)( A5 g: u0 t- v; \' L) e' d
- 3.141592653589793
复制代码 * ]+ f0 Q! U8 u; `& ?- ?
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x/ r2 H& K* g( a
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
- P* q0 w. g; f; P1 H. j - ceil(x)
+ Y# j3 U$ ]/ f8 ]* l - Return the ceiling of x as an int.. ^* O( ~' k3 G/ |5 r/ u9 v
- This is the smallest integral value >= x.) p6 c+ o( v3 g$ v# x
; A" y# [8 e: h1 h& a- >>> math.ceil(4.01)8 |& `9 ?9 K" h# X: ^" A
- 5
* Q6 _8 z' n. P - >>> math.ceil(4.99)' Z! L9 K7 C, c! @# Q; R( X3 q8 }3 P
- 5$ f: ~) g [' q- ~
- >>> math.ceil(-3.99)" `# Q% w( G- R
- -3$ Z# X' L5 C) I6 C7 F
- >>> math.ceil(-3.01)2 l( K! ], T* x( M. X
- -3
复制代码
8 r& o T' @$ |+ m* J; O% W0 f8 Nmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身- P) @. t {: H* w2 m+ M$ L
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
E& y. v& q! b# M! t6 M9 F# a - floor(x)
6 T- f- u7 H6 t! X - Return the floor of x as an int.7 L, a, q' F) U) i# D$ O
- This is the largest integral value <= x./ s9 k3 t5 h: e X
- >>> math.floor(4.1)
. l' w; U: `. G7 n8 S9 |* r - 4
* x$ E) t+ t$ G" i - >>> math.floor(4.999)# E |: R' t6 h( r) X0 _1 ^' |6 g
- 4; } N6 A! t9 }% Y6 T( z: g# w
- >>> math.floor(-4.999)$ r8 ~$ s0 c0 _# }4 I( U- b5 c
- -5& Y3 Y9 Y8 {% X/ r, ?
- >>> math.floor(-4.01)7 t. ^4 U0 J; ?; B9 K5 r* a
- -5
复制代码
0 ]. ?- j& m4 U5 }# \2 e& @& ?math.pow(x,y) 返回x的y次方,即x**y
) M3 z8 L. V0 S9 H7 S- #返回x的y次方,即x**y# V8 k- u3 {- I7 c* [* I! p
- pow(x, y)
: a3 O# _) [9 W+ H* l+ y0 Z$ t - Return x**y (x to the power of y)." d( Y9 ~- l. j$ `
- >>> math.pow(3,4)( u% |; |4 G7 M# _8 y5 Y* Z4 T' C
- 81.01 N8 y; d0 j3 ^, [
- >>> 0 T% o9 o' G) G6 m6 ^- p! K
- >>> math.pow(2,7)
6 u' \' k; ^& T' R5 ~" d, v - 128.0
复制代码 4 K) D0 |( X7 I' V9 }% Y3 m
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 ]# `# [0 f/ C4 `' P2 O% \- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' y- e/ z) l. ?; t - log(x[, base])7 Z8 B0 A- R8 c. F) ?% c9 G
- Return the logarithm of x to the given base.( |2 u( K( W, i( z6 O: s0 `
- If the base not specified, returns the natural logarithm (base e) of x.9 m6 ]4 G0 F, m1 h$ a+ c
- >>> math.log(10)
( x* p: |+ X& q+ n; R5 H - 2.302585092994046
?$ h0 c- W' ^# L. a - >>> math.log(11)& v3 R! [" h: o X) B
- 2.3978952727983707
/ f% w: ]& B/ x( M# e1 N/ h - >>> math.log(20)1 K/ F r5 ^- `+ G# ^1 t
- 2.995732273553991
复制代码 # e. G1 r E: o! }% I: M* a
math.sin(x) 求x(x为弧度)的正弦值' _) | L$ X+ I! U0 r! t$ K4 j
- #求x(x为弧度)的正弦值6 t% {: f) G- P2 Q' N. l
- sin(x)
9 z/ U( q" K4 \7 v) h: u4 c - Return the sine of x (measured in radians).3 x. ^& t( R4 Y6 W0 N" }, w
- >>> math.sin(math.pi/4)1 e2 E: V6 }' W: \- ?! D! z
- 0.7071067811865475
^! s( q( s# y: @+ n, Q - >>> math.sin(math.pi/2)
. h, [! m1 }& A - 1.0( y% ` Z6 n& B q
- >>> math.sin(math.pi/3)/ f& v" u3 l4 h+ `5 o( G
- 0.8660254037844386
复制代码 ! k+ L+ \4 R6 }6 q
math.cos(x) 求x的余弦,x必须是弧度
[, Q3 ~1 X- r1 n- #求x的余弦,x必须是弧度
- G# P/ p( C8 k9 M& H* N! I3 h - cos(x)
; y0 S$ _7 N* Q0 ]0 z. e1 k - Return the cosine of x (measured in radians).
6 H% _% t8 b* u( A7 B% x, u1 ^ - #math.pi/4表示弧度,转换成角度为45度
- {3 R8 b* ~! A - >>> math.cos(math.pi/4)% Q$ g, {" Q# h% h/ E- @; b
- 0.7071067811865476
4 w' U. o3 X5 P - math.pi/3表示弧度,转换成角度为60度
1 ~: P- G/ y# d/ I' g9 z$ P& w - >>> math.cos(math.pi/3), C5 ?' s1 C- f8 R8 i
- 0.5000000000000001: l* r1 I- e0 \$ x
- math.pi/6表示弧度,转换成角度为30度
! X( C( |# x/ `+ p5 z9 O - >>> math.cos(math.pi/6)6 i( w- l0 \$ N4 w5 [5 D+ t& a* i
- 0.8660254037844387
复制代码 ; @: n6 }6 v+ s% W
math.tan(x) 返回x(x为弧度)的正切值
; d) w8 O* M) b) n: m- #返回x(x为弧度)的正切值- j# @+ o" _; w3 T+ q! K% l4 [' P
- tan(x)/ {+ p: ^, L, k5 f! G
- Return the tangent of x (measured in radians).
( r9 H0 t6 G( w5 p - >>> math.tan(math.pi/4)3 j, f" W2 R% _+ A4 a! \: j
- 0.9999999999999999) `* Q1 S& R& H. T" Z' T/ a
- >>> math.tan(math.pi/6)
( @; w, ^ ?2 U - 0.57735026918962577 c# q+ }4 k8 p
- >>> math.tan(math.pi/3)! h$ Y5 H; \% I2 ]7 e1 E- |) M0 f# w ?% v8 ~
- 1.7320508075688767
复制代码 5 J9 {# c" }/ i( a
math.degrees(x) 把x从弧度转换成角度3 j4 z- B# V& B5 Z q1 n$ y% U
- #把x从弧度转换成角度
/ S" s. i2 V8 Q ]" c1 Q/ w - degrees(x), p+ s, _8 r C A
- Convert angle x from radians to degrees.
3 J; |( l& ?) X7 w, ?" u
& q! I4 f1 @* G( E% K/ G- >>> math.degrees(math.pi/4)/ u- W6 |5 J5 X9 Z7 V O2 \
- 45.0$ |- r1 q n9 E2 @6 {( S" D
- >>> math.degrees(math.pi)
8 S# v0 O- ~! L: Q$ v3 c - 180.0! A& F% f) }. F) y" M
- >>> math.degrees(math.pi/6)! |3 z7 Q/ u3 |$ G
- 29.999999999999996& Q. ]; Y! e; ^* d4 W9 V
- >>> math.degrees(math.pi/3)
' L/ \- G4 |9 ] - 59.99999999999999
复制代码
' m4 p! _0 }/ w' ~' H% {9 Cmath.radians(x) 把角度x转换成弧度7 f& {: h- \: {' p+ ?) `/ ]" d0 R
- #把角度x转换成弧度" K; b: D- _3 k9 }! R; [; \* C; S
- radians(x)
7 U5 ^. }% E/ Y - Convert angle x from degrees to radians.) H4 X6 _- B. N6 F! M5 J7 e* n, X# M
- >>> math.radians(45)
M) a1 x- m; x! n% ? - 0.78539816339744835 U% A; |# D/ l6 ?: u3 k2 B
- >>> math.radians(60)5 t: [8 k% ?; w% h: E
- 1.0471975511965976
复制代码 : T; u- }3 B. B5 d# Q" e/ n
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
: V6 q. s$ I# M* Y8 k6 x" i- #把y的正负号加到x前面,可以使用0
( n( E" C+ h( r/ G8 I - copysign(x, y)
+ p% B D, m* X3 e2 m3 l - Return a float with the magnitude (absolute value) of x but the sign & @6 j2 p2 Y5 |8 X
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
1 z/ Q: e* H! v - returns -1.0.
" I+ [$ |$ I& ~" j3 N
7 c; a, p* @4 B! M) X% [- >>> math.copysign(2,3)5 o7 O, H- l; j: Z. d m
- 2.00 f! d8 @ m( A$ @& V( D0 ?# N
- >>> math.copysign(2,-3)
3 ?" g% o* ?* M, c _ - -2.0
& A' n2 I! J6 T" K, P, G3 D# k - >>> math.copysign(3,8)
& x% D! o+ A; |) v% b) }) W - 3.0
- G n6 Y: |7 z, ` - >>> math.copysign(3,-8)4 c/ u/ ~. E/ c+ _9 r/ Z9 m
- -3.0
复制代码
% g0 P5 `) r' B' m/ b7 w6 `math.exp(x) 返回math.e,也就是2.71828的x次方
) y7 c( r, b1 d. X- #返回math.e,也就是2.71828的x次方9 C+ P- O( }& M! t
- exp(x)
+ Q* c# H5 B' a1 _; Q# ?1 B - Return e raised to the power of x.
2 a% u h! U; s+ I - % E) G( c8 R% ?8 K1 G* h
- >>> math.exp(1)8 C8 b3 v X: F6 v" q% S3 d
- 2.7182818284590451 Y4 R( p, X+ S6 g/ I2 ?
- >>> math.exp(2)0 z+ q6 q0 z; Y' n- W9 {2 e
- 7.38905609893065
2 B4 A+ E, D5 n4 z: W1 T. q - >>> math.exp(3)
# f2 N& l l$ i5 z5 `7 [ - 20.085536923187668
复制代码 % Q" F7 l/ z: ^5 I: f; H% N [+ S' G
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1$ c( d! k; Y1 ^9 ~+ n" G( b
- #返回math.e的x(其值为2.71828)次方的值减1: T8 y+ ], Q. Z& i) M& ~# t& a
- expm1(x)2 N2 [& Q1 F p- R/ w5 ~
- Return exp(x)-1.
- c, _0 S1 C) F5 V: U8 ? j - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, @* l$ m) y) C/ v* a- @$ R
: b5 M: e8 x$ ~* R$ z- >>> math.expm1(1)
& u% K L6 x! [4 \' D - 1.718281828459045 [1 i% {# o" c
- >>> math.expm1(2)
$ Z4 O/ P$ b5 w" W1 t) ]4 ^& z - 6.38905609893065- b8 v! T% [- U. |
- >>> math.expm1(3)& C* Q/ k v* d# U; {
- 19.085536923187668
复制代码
. u! u- [6 M6 Z, n* ?( [math.fabs(x) 返回x的绝对值
; i7 P; i& Y' _' C( D* A; y- #返回x的绝对值
! Q, j! X) Q( d# ` - fabs(x)
( T7 K) ^7 A, W c9 K" g" | - Return the absolute value of the float x.$ S9 i& |7 c+ c7 R! Z" m
- 1 o3 k, s3 _/ _' \; b. b
- >>> math.fabs(-0.003)! _1 o) S$ G* z+ c, W' l( l+ z
- 0.003/ Y$ u4 v; w% W, k9 d
- >>> math.fabs(-110)5 r$ ~0 L- C* |/ F8 j- M' E
- 110.0* k$ G+ s+ N' M: U% t
- >>> math.fabs(100)3 [( H( f+ j1 t
- 100.0
复制代码
: K1 T7 l! m3 o4 r% `math.factorial(x) 取x的阶乘的值" D' F0 L* a3 y1 c- S! o: k! T1 c
- #取x的阶乘的值
) B* [0 N9 ^. o8 U9 g - factorial(x) -> Integral
6 r: o7 c/ [3 v; @9 Q - Find x!. Raise a ValueError if x is negative or non-integral.
: y7 z' x3 n6 |! X7 P$ e% ` - >>> math.factorial(1)
+ ~9 u. _' E6 n1 R - 1
/ Q( R4 Q* |; e, T$ r - >>> math.factorial(2)
c% K. _0 i% L( N6 g( i& v+ Y - 2
$ ?6 e9 z' K) x8 n1 a - >>> math.factorial(3)
4 w$ J4 z, d# h: F0 I! P+ O - 6
4 M3 s& C R Y) I3 `5 F, @ - >>> math.factorial(5)
8 B+ i- d+ @6 m2 w9 u) C0 r - 120
( J5 g; ]- ]# S1 ?+ L' ?$ U - >>> math.factorial(10)
: C; h& _ _2 @3 P% |/ y - 3628800
复制代码 7 j8 d1 m# L! ^2 e! ]1 e2 W" z
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数7 t7 a* e5 {. u, l, J! p3 k2 G% M+ i
- #得到x/y的余数,其值是一个浮点数& `- B3 q' t; g2 v" T3 g/ l R$ w
- fmod(x, y)# I3 d( |+ I4 B& I
- Return fmod(x, y), according to platform C. x % y may differ.
: i" D& N8 [) J7 E* z q - >>> math.fmod(20,3)
) ]% [9 J u: k0 m - 2.0
: T) P% ]9 p" f: v( w: D! k# } - >>> math.fmod(20,7)
. M) k. x/ c- l K, [7 s - 6.0
复制代码
) m6 X4 p$ L) N2 xmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 X% f5 F7 }5 h5 } b( E) y! P
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,! L0 j! e, o& Y+ t4 {/ {- }
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
& Q/ N c1 D$ f& w - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1# ~4 N, g' |+ H0 R B2 F% X
- frexp(x)
! y3 |) ^. g" U - Return the mantissa and exponent of x, as pair (m, e).5 Z- U9 Q5 |( U3 I5 n* w7 x0 T
- m is a float and e is an int, such that x = m * 2.**e.
# i% N# `7 l" C - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0., g! A, h9 C, x E
- >>> math.frexp(10)
, a5 ^* s" W8 }) Y5 [' E- f - (0.625, 4): ?: i+ S& t) r, g5 W
- >>> math.frexp(75)
0 g8 T) `& [0 r* t$ i4 V D - (0.5859375, 7)
) g2 y# z! y$ Q9 N) i9 u - >>> math.frexp(-40)
' @1 t. r! C0 W& k - (-0.625, 6)
+ y" S( Q- Y6 L) ^ - >>> math.frexp(-100)! Q$ E" ^9 K, p4 v
- (-0.78125, 7)
. y2 M- \! I+ n( n6 q3 c& n - >>> math.frexp(100)
! \8 M5 N( b" I% ` - (0.78125, 7)
复制代码
$ |: S! c/ b3 Y5 |math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)' Z6 [$ ~- ~8 B# l0 S
- #对迭代器里的每个元素进行求和操作8 M. U2 g" u' m+ |6 d/ o1 o- Q
- fsum(iterable)
. K. d. O8 c5 v - Return an accurate floating point sum of values in the iterable.
5 H7 K' j' f- u$ F* B* |/ H4 Y - Assumes IEEE-754 floating point arithmetic.
( N; ?+ s2 A0 L! y3 Z - >>> math.fsum([1,2,3,4])
. R! d- I+ t4 |: G0 \" k, U7 T - 10.0* C* ~7 c# o4 }- w/ o
- >>> math.fsum((1,2,3,4))
. G) u+ ~; s2 O) B1 R - 10.0
4 L! F( m% o% Y& D5 n5 k - >>> math.fsum((-1,-2,-3,-4))
6 a( Y' |1 h8 [* e" K* v; C - -10.0. F, E/ T7 K) s8 `+ u
- >>> math.fsum([-1,-2,-3,-4])2 p' O+ y+ \/ a, ~
- -10.0
复制代码
0 ]! T& w( J+ A/ q6 Xmath.gcd(x,y) 返回x和y的最大公约数
9 j! W* s9 L& d9 t6 k8 e2 ~5 v- #返回x和y的最大公约数7 o: D1 A. B/ R( f
- gcd(x, y) -> int F, k, m7 T0 Q6 y
- greatest common divisor of x and y
4 D: S1 V& m9 n, l7 n; | - >>> math.gcd(8,6)3 k9 J& l, u+ A) E! t3 R) V6 W
- 2 S- Q2 X4 G# i0 S' [, Q- g4 q, E" J
- >>> math.gcd(40,20)
( K: G6 \* D0 D3 i" J2 Q$ \: F - 20
+ K s" W. t( _/ b, L! G0 x6 X - >>> math.gcd(8,12)3 N+ x+ l) m7 V5 C9 n
- 4
复制代码
1 y8 Q' M+ M) P. C& [math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False3 R2 ]; \: x3 D2 U0 T. Q# R
- #得到(x**2+y**2),平方的值
! K+ h' D/ {6 w* E8 J - hypot(x, y)$ N+ n6 ^9 i/ b* ^
- Return the Euclidean distance, sqrt(x*x + y*y).- U8 ]3 M& J, f3 {: [4 o$ M% `
- >>> math.hypot(3,4)* B: o# [6 T% x4 ^4 s# r1 t
- 5.0
& p) C0 l7 x4 T - >>> math.hypot(6,8)
5 g, I& }* O" p# l! p - 10.0
复制代码 ; x1 j% l2 ^( a) J M/ B. M/ W) t
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False" [! p! Z, }- _- k; \6 {
- #如果x是不是无穷大的数字,则返回True,否则返回False5 t2 u- ^$ p* k8 r0 \
- isfinite(x) -> bool3 w4 u m4 w0 r q1 M8 [
- Return True if x is neither an infinity nor a NaN, and False otherwise.
' Z3 \! l# B6 o. D m* j9 ~6 f0 l# } - >>> math.isfinite(100) F6 _" M' W; [- S
- True' x$ i1 ~) _8 R! C2 F
- >>> math.isfinite(0)
8 l0 m( e- M9 E4 w4 T - True
, E4 f3 T2 E7 D4 g( y A7 U' B - >>> math.isfinite(0.1)7 _9 S; a" D! `0 D$ o/ W9 a
- True
2 y# B+ ^( g" Z; s1 j- d - >>> math.isfinite("a"): b3 C! N8 e% Q4 m9 F
- >>> math.isfinite(0.0001)
, u8 q+ M- m* ~3 l( ?/ N. C, M3 I - True
复制代码 / d" x- |# }0 v A% K
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False$ Z) |; {0 w6 N3 ?% r
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
& A/ l1 r- V# ^ - isinf(x) -> bool
' N2 M4 T& f3 a! {# W$ s - Return True if x is a positive or negative infinity, and False otherwise.
. L( v7 C2 k: ?- q! x1 D* U6 H8 v- T - >>> math.isinf(234)
0 P5 C: Q) [3 [5 s6 _$ f' c - False
/ m' q6 X( \1 J1 T - >>> math.isinf(0.1)
" A8 r8 C4 \. d4 ^- ?/ A# z - False
复制代码
- A" `: r4 k' G, Vmath.isnan(x) 如果x不是数字True,否则返回False
. l9 ~" ~+ G& M- #如果x不是数字True,否则返回False
) T+ c& u$ ?- [4 ^+ u - isnan(x) -> bool4 s6 X$ R$ r1 n' P, G
- Return True if x is a NaN (not a number), and False otherwise.* d; j3 o* T0 m) y" h! s
- >>> math.isnan(23)
) Y& ?- o, i0 m - False
) ?* ^. {0 m% y7 j - >>> math.isnan(0.01)( o& r- K+ P6 u' M& b. s
- False
复制代码 % ~ _1 r, ^% T. S* n: |5 i1 w
math.ldexp(x,i) 返回x*(2**i)的值& k0 R# x6 |" d) L- z9 ]4 j. ^
- #返回x*(2**i)的值
% C) Z8 I+ c2 S, n1 A, D( i - ldexp(x, i)8 }" H ~# r1 N/ J
- Return x * (2**i).$ R: F8 a ]8 j& Q4 e
- >>> math.ldexp(5,5)7 e: i, s2 {) R. ^
- 160.0) ?. w+ q0 v }' q: N" e
- >>> math.ldexp(3,5)
. L- X& @, Y/ \! E5 b - 96.0
复制代码
* l8 s L' [" J7 wmath.log10(x) 返回x的以10为底的对数& |1 s" H! y$ ~8 Q- `5 V; i
- #返回x的以10为底的对数
$ X1 V5 b! {$ ^5 W7 @ - log10(x)
- t5 L, r- F9 b$ U4 f' a4 w - Return the base 10 logarithm of x.4 G; l( h5 ^( n$ @! ]) E4 k: w0 G
- >>> math.log10(10)8 T) f: z: ^5 |9 I% e
- 1.0
3 y+ o8 F9 ]$ w5 Q - >>> math.log10(100)
+ K' A/ l$ f) b3 C - 2.0
6 I# D) c# C. G' P - #即10的1.3次方的结果为20
, k* u5 B" Y% A( P5 b - >>> math.log10(20)
' \# y( N: _' l( f- M - 1.3010299956639813
复制代码 $ {. z& s" S8 X' N+ a5 P
math.log1p(x) 返回x+1的自然对数(基数为e)的值
1 s+ c- h6 r! }" J- r) d- #返回x+1的自然对数(基数为e)的值1 g; N8 `& o; C* M6 t) W
- log1p(x)! v% n7 }1 @' _+ ~3 q5 D$ C D: i
- Return the natural logarithm of 1+x (base e).7 I+ F7 m: X1 }5 e; X2 x0 x# C
- The result is computed in a way which is accurate for x near zero.
" l; ?0 `% L- C3 I* L - >>> math.log(10)3 k' V5 I: L& r
- 2.302585092994046" s& }8 f- g5 M( B$ B; D
- >>> math.log1p(10)# t5 p1 x& w( C6 ], \
- 2.3978952727983707
) K' ?: G4 _1 S; [/ c' \ b6 V# D% M - >>> math.log(11)
- C! w* O. U* O' _ - 2.3978952727983707
复制代码
3 u7 Y# y3 E0 c! S. kmath.log2(x) 返回x的基2对数5 V7 A" U$ T0 S& C" u% d/ ?
- #返回x的基2对数+ K# n9 h( O" U% ~2 y" ?
- log2(x)
& N1 n0 t4 \# f3 l. h - Return the base 2 logarithm of x.
3 @3 P% P5 N! K( w4 e1 S - >>> math.log2(32)
" F/ M2 g1 N- V. q5 O9 r. D9 G - 5.08 g* |- t: S; C" P
- >>> math.log2(20)
: }( u: E# a8 g/ |1 p - 4.3219280948873630 \4 G, E& N0 K H$ d
- >>> math.log2(16)! U: m0 x* g0 H$ O
- 4.0
复制代码 * E& J U& I) z) w0 }$ a1 C7 r; w
math.modf(x) 返回由x的小数部分和整数部分组成的元组" C# Q }/ l2 [: Y) `" |. L# { t8 w
- #返回由x的小数部分和整数部分组成的元组
; @2 O$ u; |, T! {0 E+ _7 d - modf(x)8 C( f: m2 w1 X% }( @8 q: O& `
- Return the fractional and integer parts of x. Both results carry the sign" l. G9 H: ?! x/ L3 e
- of x and are floats.
# g# m5 L& `- x! ^9 [/ ] - >>> math.modf(math.pi)& N, c- a$ S+ @! j3 `1 K _
- (0.14159265358979312, 3.0)
( p% I) i4 L }. X, ^8 u4 Y: T+ l - >>> math.modf(12.34)
2 T& U" n7 e# o7 E - (0.33999999999999986, 12.0)
复制代码 ) q+ _% ?8 T, k3 o' {" K0 J* q/ M
math.sqrt(x) 求x的平方根
* f( H. s% G: b6 Y* B/ G- #求x的平方根% ^* Y, q5 J$ S% J o% _ u
- sqrt(x)8 V9 b- e& b/ p( W: [& \9 X; G
- Return the square root of x.* H9 h3 C9 a! [: _) u9 q' |; U
- >>> math.sqrt(100)( v2 C. D% v5 g) Q
- 10.0
6 D4 X7 L9 O, M- r( B& ^ - >>> math.sqrt(16)$ z/ M" p. {" z+ T
- 4.00 Q O0 ]/ p% Q W( z4 y5 }
- >>> math.sqrt(20)
O% ?% T8 c* G - 4.47213595499958
复制代码 5 W" }; ~) g& \9 V% I( b
math.trunc(x) 返回x的整数部分- #返回x的整数部分 l! ~# ]; Z" e& e% c) `9 [
- trunc(x:Real) -> Integral
# Y+ ?! x1 u6 P: Y* j: `0 O8 R - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.0 |* D* ]4 Q$ [9 I9 [. ]& s
- >>> math.trunc(6.789)# x$ q4 b- n" L) v( a, V+ u5 j1 {9 X
- 6, ^2 H9 {8 Y1 V2 p: c- h7 t
- >>> math.trunc(math.pi)
6 _. x! G6 o r' T+ F8 p - 3# j7 e8 a& c- q9 ?. O0 p/ A- C- A
- >>> math.trunc(2.567)
" E: H; {6 d1 _- m5 K - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|