马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
$ u4 C. v. a2 {. v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
- a" Q. B2 a d$ V
9 T1 k9 l) ? Z. [# l- u1 \方法1:9 J* P7 T" H0 V2 C* ~! `
- >>> import math
, i/ ?2 n W1 p: d - >>> math.sqrt(9)
/ ?) W! z9 R, i- v& u7 ^ - 3.0
复制代码 方法2:* I4 B- N. _( c9 W5 ?. Z
- >>> from math import sqrt
% y1 H. L$ t, \0 [5 `: I - >>> sqrt(9)' L; D! C, R' L
- 3.0
复制代码
5 n j, I% } h* P . |/ _' C* |& n* f3 C7 R" ]
math.e 表示一个常量, U' G# ~# Z: d9 d
- #表示一个常量6 ?$ Z! C/ P, f! \
- >>> math.e
: e' Z) F6 [. U; m$ Q0 N - 2.718281828459045
复制代码
; ^; X5 o4 t9 o$ bmath.pi 数字常量,圆周率
# A9 L- k' K( `* T; ?) P' B& S- #数字常量,圆周率
" ~0 V$ R. N2 B - >>> print(math.pi)1 a- f4 w! x) t
- 3.141592653589793
复制代码
, E! E7 @2 D1 I/ O gmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x7 o2 e/ \& {+ o \
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ L7 ]' I) p2 a6 e6 o - ceil(x)
6 X3 E5 S. `) U8 {4 R3 W - Return the ceiling of x as an int.' j# _1 ]3 ]7 ^$ j6 q( r6 g1 ^
- This is the smallest integral value >= x.* k; E+ @# }' {/ y# H- u- z
- 1 f/ G4 [' @/ V) v, c
- >>> math.ceil(4.01)
7 o4 m' Q6 u& O* o) e B - 5
, D1 r. H0 D2 E( O; M: b - >>> math.ceil(4.99)+ v; u5 P3 C$ g$ a6 P) C% c3 I
- 5. m: p3 n0 N7 v
- >>> math.ceil(-3.99)7 A ]! e6 X( r e
- -30 R9 d7 d7 W0 j8 j# ~2 r
- >>> math.ceil(-3.01)# ^" ?1 x u. [1 w* r
- -3
复制代码 1 V" Q6 R- ^2 I3 K; f' K
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 r# x- n2 g* k" g( j- d
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 W0 m- ^5 I: a - floor(x)( s! \9 R* O& u+ {
- Return the floor of x as an int.
1 h3 ^: K" F: y! a) ~ - This is the largest integral value <= x.1 ~! s5 V: z$ N0 |, B8 j7 O
- >>> math.floor(4.1), b# d, B, C! j" E& I+ { _0 t, k
- 47 e: h, ^& {' V- {9 M
- >>> math.floor(4.999)
8 A. Y+ G$ d1 [' X: ~# I1 T - 4
3 S' c' x: s1 n7 g: } - >>> math.floor(-4.999)
; z% F: `8 y* b6 y) i" H: r - -5) b( M8 l" t4 J6 Q9 t, w" g( t/ s
- >>> math.floor(-4.01)
, W5 \: a. n$ R2 z; {# A g - -5
复制代码
9 m3 o- t& ^: |, ?! A! i( h& D$ jmath.pow(x,y) 返回x的y次方,即x**y$ a- D4 U5 A& J" n/ H1 y; Z
- #返回x的y次方,即x**y
6 D$ A' u. C$ G+ `' N - pow(x, y)# \8 ]4 c6 A7 o) o3 R8 e
- Return x**y (x to the power of y).
4 Y7 J) x7 _' C; S0 l. _ - >>> math.pow(3,4)) l; B$ o( ?) x3 u* E e( c0 V7 Q
- 81.0
* j" x' |. D4 I8 m7 P7 t; i - >>>
, c x- i+ q3 F/ b" H1 c8 }8 Q' y - >>> math.pow(2,7)- D, V/ y+ ?5 J& e3 g& N
- 128.0
复制代码
% Z6 ?2 H7 j; [* emath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ @0 k8 `8 C( S! ]
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# ]! l3 H8 ]# n3 M% j - log(x[, base]); G `6 D9 x% t
- Return the logarithm of x to the given base.3 D$ P8 T! k" Z) {' n0 T
- If the base not specified, returns the natural logarithm (base e) of x.$ s% ?8 b: _7 A
- >>> math.log(10)7 T4 E9 q: Q4 y( Y: i& |- _
- 2.302585092994046
3 I2 X. ^' W x7 h9 J - >>> math.log(11): v8 m& v: I. K- m: `
- 2.39789527279837075 y U6 _9 h; A" e! V
- >>> math.log(20)
, ~; w+ o* p3 b8 h r* g6 f p - 2.995732273553991
复制代码 ) b# H9 E( b+ G. m
math.sin(x) 求x(x为弧度)的正弦值: d5 j. L2 {: K# X2 s
- #求x(x为弧度)的正弦值
' C* k" O* y k$ p# U: e - sin(x)
- }2 M; K) U$ |& x6 i1 B. G - Return the sine of x (measured in radians).
" Z! n% d# J0 l( f - >>> math.sin(math.pi/4)
/ a' T8 j- U; L# P. ~+ T4 {/ ~ - 0.7071067811865475
/ x: L* q0 O" ]; \$ b' r - >>> math.sin(math.pi/2)# Z& M0 J. r) N; h3 S @2 G
- 1.0
9 Q8 e7 p% G- ?- B# b0 R - >>> math.sin(math.pi/3)
% R" ?( c9 t& @0 B - 0.8660254037844386
复制代码
$ p$ r( _7 N l! r9 jmath.cos(x) 求x的余弦,x必须是弧度( @% q8 ^ F) c: }' }- L' W
- #求x的余弦,x必须是弧度0 k2 s/ {4 D8 C# K
- cos(x): i; s4 H( a- U. {! I/ Z2 `$ p
- Return the cosine of x (measured in radians).
' {% E# g: y( ~( `) g4 R - #math.pi/4表示弧度,转换成角度为45度
1 w% @0 z. }: O* z% C$ w - >>> math.cos(math.pi/4)2 I0 K, M3 ~8 B6 Q& I; t
- 0.70710678118654761 ]2 e" Y. n. R. ^$ P: B
- math.pi/3表示弧度,转换成角度为60度! _" c( l7 |9 G# b" w0 h" p0 a7 p
- >>> math.cos(math.pi/3)5 V0 n; D* M" |1 J7 r) L7 z7 v2 F
- 0.50000000000000015 O- {' f: U2 d8 u
- math.pi/6表示弧度,转换成角度为30度' g# {: _5 @% o ?1 X
- >>> math.cos(math.pi/6)$ L: Y. [% h" J9 A# m* P* Q
- 0.8660254037844387
复制代码
; a7 r% _& P5 X' X+ S8 h# p9 Imath.tan(x) 返回x(x为弧度)的正切值
1 D3 p& M/ h x9 P" \- #返回x(x为弧度)的正切值0 d7 Q1 Z' ~+ j% w8 E& D
- tan(x)
7 T' G2 j$ {# e" Z" [ - Return the tangent of x (measured in radians).
8 G$ @: K* N2 i8 w7 M+ S: A - >>> math.tan(math.pi/4)/ m5 {# {2 Z, Y A$ g6 S) e* l+ n
- 0.99999999999999992 s+ T0 w* |+ B {# J
- >>> math.tan(math.pi/6)( }2 a/ n! g/ @4 V, z& X
- 0.5773502691896257+ B U8 Q l- F5 e7 _" N7 I& A
- >>> math.tan(math.pi/3)" l' x- ]& ?) M2 a9 z3 Q
- 1.7320508075688767
复制代码
" a" ]" W, x% [5 f8 \8 r" |7 z7 }math.degrees(x) 把x从弧度转换成角度
6 @' a$ |; m; ]- #把x从弧度转换成角度
/ H. g, y# y3 j6 u9 W% d - degrees(x)
' n% @4 d- d: x - Convert angle x from radians to degrees.( N4 C' p; D6 p/ z# b+ R
- + u( `. ?/ Y) {( M) l- F
- >>> math.degrees(math.pi/4)
+ U! b- X% x @8 L+ M - 45.04 s% s6 [& }) {5 t8 X! x0 e
- >>> math.degrees(math.pi)
& T" m' N6 G# N- K% s$ X - 180.0
0 R( u( B- K9 u/ }7 ]' P5 V - >>> math.degrees(math.pi/6)' J' c, p9 h1 { y! ^7 L' ^ ^
- 29.9999999999999962 ]3 _! F, T7 }! e2 H; c, L
- >>> math.degrees(math.pi/3)$ P% C4 G% A0 }' J4 t
- 59.99999999999999
复制代码 0 l) B( s8 e p% e* V. N
math.radians(x) 把角度x转换成弧度0 r' W5 X8 `, ]
- #把角度x转换成弧度' `3 m" ~! V# Y$ J
- radians(x)
" ]+ f" b" k6 i( b# |! f1 f, V# s - Convert angle x from degrees to radians.& Z6 l' d: j, I8 w
- >>> math.radians(45), y; W- G- h* i+ ?) ]2 e" @. B
- 0.78539816339744833 k& W! A% H# p! n& k' i9 T
- >>> math.radians(60)7 q2 A; h y: O. J1 N! b
- 1.0471975511965976
复制代码 6 i0 ~# C9 L# H8 Q
math.copysign(x,y) 把y的正负号加到x前面,可以使用0: Y) G& z( c0 n+ K
- #把y的正负号加到x前面,可以使用0! Y; E( I1 ]% ?% L; p
- copysign(x, y)
: c9 @# _1 m% t* F+ m1 b0 }0 x i - Return a float with the magnitude (absolute value) of x but the sign ( Q7 P, Q' l" D T& \
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 g9 M5 B4 C% A' y; r
- returns -1.0.
& v7 p3 b: {3 Z% y/ m - 8 W$ u" |# ]' K; _# U$ o
- >>> math.copysign(2,3)7 h3 _3 @! ]" X" ^
- 2.0
8 U$ L1 _6 t3 C8 @9 N - >>> math.copysign(2,-3). ?4 C) i; Q( v
- -2.0
U$ n2 q- B+ G8 ^ - >>> math.copysign(3,8)8 }5 _8 m$ O! o& n' y
- 3.0
1 A! P; ^) _1 @% Q - >>> math.copysign(3,-8)- ?( B. `8 u) [7 t) m* F& D
- -3.0
复制代码 6 A; o( s% k! q
math.exp(x) 返回math.e,也就是2.71828的x次方" j8 _9 |3 l q. I! c" U/ l4 R
- #返回math.e,也就是2.71828的x次方" f6 w6 u, q' Q8 r
- exp(x)
/ v! E- V0 V0 P - Return e raised to the power of x.
! U' r/ a1 U6 O# C7 |$ K - 7 G1 w1 A9 e( X3 B; j4 I& x
- >>> math.exp(1)
2 {- g7 N: S; [ u - 2.718281828459045. w7 V" \. L- K
- >>> math.exp(2)% c: ~/ P8 M! I. {
- 7.38905609893065/ I" U9 w; p4 }/ t0 _$ L# B0 O
- >>> math.exp(3)7 j8 q, d' p3 b4 u- q
- 20.085536923187668
复制代码
( V; [( i; j6 M E8 @4 ]" amath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
+ y5 ]( f! ~* R( P. z. s- #返回math.e的x(其值为2.71828)次方的值减19 R3 p6 m( W1 w# \
- expm1(x)
2 z* X: B" Z- | B' v6 H) P - Return exp(x)-1.
% C* g5 V/ _! e( ?$ { - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
$ m& e! r- `6 M* j8 l4 K$ X5 h( a
* k: e7 Y, k+ v- U+ E, S7 t( P- >>> math.expm1(1)
! Z! ~; E/ u. z. u4 i' ?9 i1 r! P - 1.718281828459045
0 f2 b' N% l; e+ b% g, z5 k6 f3 a - >>> math.expm1(2)) t9 k0 D6 j+ J' ]* c2 ]
- 6.38905609893065+ X/ y8 W& U( w
- >>> math.expm1(3)
5 \7 i8 I: r0 _' A b, X - 19.085536923187668
复制代码
9 B* w9 o6 v- @* n7 O# M; |9 Ymath.fabs(x) 返回x的绝对值( E" }: a Q1 K) o/ f
- #返回x的绝对值
" \, J! S/ T$ ^% f+ X* M - fabs(x)+ t5 T& g0 Y3 m8 f) } O
- Return the absolute value of the float x.
7 N J+ J2 u( v/ a# d9 z( I* e$ b( f2 e
7 H) N5 C, ^9 }; L! M5 |- >>> math.fabs(-0.003) b& ?& B1 M9 r: V+ e1 V) Z
- 0.0033 q8 A% B( x% }+ w* f* K: a
- >>> math.fabs(-110)7 _' y$ f9 ~* q; k/ [1 }6 u5 {
- 110.0
6 h8 N2 s- X: k - >>> math.fabs(100)
9 l8 G. t2 l3 G: b6 Q - 100.0
复制代码 & |% v8 K: X: G) x0 b8 ]
math.factorial(x) 取x的阶乘的值
/ K9 ]5 l/ S' f3 K$ n+ ^- #取x的阶乘的值# | j' b* {+ o8 ]& O
- factorial(x) -> Integral4 g9 y/ n: u* V( j: d9 E6 i% L5 a: m
- Find x!. Raise a ValueError if x is negative or non-integral. E8 N0 c( z$ Z. L: c
- >>> math.factorial(1)
2 Y$ |% }( q0 Q - 1+ ]0 n, n: O/ B& b# ?4 x( @
- >>> math.factorial(2)" V1 @8 a: f7 W
- 2
( t: c( k: H- q) k# |7 l - >>> math.factorial(3)) V2 h [* r3 v# v+ D* R9 d2 L
- 6) V5 ~, x. k# D0 W. q; c
- >>> math.factorial(5)
& G7 X- B4 a( G - 120
( }# {/ g$ R; r8 ]" B9 ~5 H - >>> math.factorial(10)
. [/ w% A& {8 @: m1 o9 S - 3628800
复制代码
2 K$ S: u1 e& Gmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数' Q" m/ [* t6 D' k1 |
- #得到x/y的余数,其值是一个浮点数
8 p2 n) ?) N& h0 B, N1 ?4 K9 H - fmod(x, y)! Y7 o) O' `$ ?$ M
- Return fmod(x, y), according to platform C. x % y may differ.
% P2 o! D" o$ @. V% A/ t - >>> math.fmod(20,3)
; | {% e1 S3 N; P - 2.0
* z5 C6 q' X6 e1 w$ U7 A- f - >>> math.fmod(20,7)
" q8 V% j/ Z: ]0 ^1 R. j) r- E - 6.0
复制代码 5 Y5 a& ^: z# |4 X& ?
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 h) M- V* P* H# {9 \" X! K$ L- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
1 Q; U' K, J6 ~, ]2 }8 C: c# m - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 V$ H& c) W& D, m5 G/ x( w
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
9 C- J# q! x6 ^. @5 H( T5 R! C - frexp(x)2 I6 M+ D C4 A4 D) h6 [5 k
- Return the mantissa and exponent of x, as pair (m, e)." Q& n. v: |2 Y5 E% I6 H5 i
- m is a float and e is an int, such that x = m * 2.**e.% U9 f2 Y. E/ P" q/ | D
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.) W" f8 q- a# v: n* M' V. D5 H- d4 `
- >>> math.frexp(10)8 L5 e' d+ z+ ?
- (0.625, 4)* o ~1 d- T9 ?, H& Z
- >>> math.frexp(75)# h% e/ s+ y, k! L* J
- (0.5859375, 7)' W2 c& ?8 \& o e6 @1 o6 ]
- >>> math.frexp(-40)0 R8 T# k) \, J! N% L+ p" |
- (-0.625, 6)
, j5 a4 ^& m5 ? ?4 H0 ?$ g" l% | - >>> math.frexp(-100)9 P; y8 M& j% H. U4 K z4 c
- (-0.78125, 7)
( x8 d" N( n0 |6 z - >>> math.frexp(100)
8 B: @4 R- M+ x5 z8 c - (0.78125, 7)
复制代码 # j) G: h# h; D( K2 y7 j6 Y+ F
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
J. v3 W# i# Y% L# M- #对迭代器里的每个元素进行求和操作
8 L) o' W$ `$ |! } - fsum(iterable)
+ G. ?2 Z2 c8 N - Return an accurate floating point sum of values in the iterable.
" q0 _* s q( \" ~ - Assumes IEEE-754 floating point arithmetic.
# R' [7 ?8 ^3 N. K# V/ e3 {* O - >>> math.fsum([1,2,3,4]). c6 X. C1 N$ q* i" f' f$ Z6 k
- 10.0
! [/ r0 B2 i; A) P: J - >>> math.fsum((1,2,3,4))
2 [3 p1 @: {+ j9 ?! n - 10.06 Z# N) D+ w. I
- >>> math.fsum((-1,-2,-3,-4))
+ ^/ Q0 g- g" p - -10.06 @0 W7 _ A/ i
- >>> math.fsum([-1,-2,-3,-4])( \/ _, C5 s& Q# E& O- L. V
- -10.0
复制代码 ) b! q% S1 m _: _, w& U2 r: s
math.gcd(x,y) 返回x和y的最大公约数" h7 H/ _' d% r
- #返回x和y的最大公约数6 C! N3 Y: d8 h( F/ y- T |4 W
- gcd(x, y) -> int5 Y Q; L% t2 \
- greatest common divisor of x and y
: k6 F/ A& o2 D- W, Y! x - >>> math.gcd(8,6)
/ x! W) I2 a1 I* q4 o - 2
9 O3 }4 `6 u7 u& n. v - >>> math.gcd(40,20)5 w+ K' h6 T4 c7 Z
- 20
: I' w% z0 l: E6 }0 g1 `$ f - >>> math.gcd(8,12)8 s! p4 @4 @8 I
- 4
复制代码 3 v: y: |8 _# Y, Z* j5 [. v
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 f9 h, E, r. R/ F1 ?+ x" }0 ]
- #得到(x**2+y**2),平方的值7 v- x% v7 i4 C% a* S; X
- hypot(x, y)
- D0 O; g, X& z' Y- Y - Return the Euclidean distance, sqrt(x*x + y*y)./ v' F% y9 T) R2 ?( F# y
- >>> math.hypot(3,4)3 G/ U* V; z- M$ L( z) v8 f
- 5.0/ h* w$ P) ?" ?7 }
- >>> math.hypot(6,8)
! S& R& p8 S1 e7 z - 10.0
复制代码 1 n( w- N) g1 c5 l$ `
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
# s* c% w( S3 y1 ~* V0 S" O7 Y- #如果x是不是无穷大的数字,则返回True,否则返回False+ ]0 i/ b/ h$ h6 I% }) B
- isfinite(x) -> bool
( p" m) F: {2 t( Y* m - Return True if x is neither an infinity nor a NaN, and False otherwise.4 o! |7 ]" H1 L% `* o1 \
- >>> math.isfinite(100)! [4 A3 i2 t3 Y! n
- True
5 X- L% H1 M) G: `0 O: y - >>> math.isfinite(0); i+ D, Z/ e$ @
- True& w+ Y3 g3 o& h1 s
- >>> math.isfinite(0.1)
1 I' F# u7 |, S% D; }, c1 n7 M - True
& f0 o: d2 {( q - >>> math.isfinite("a")$ j# |; |: b. T" S9 m6 u1 o5 `- ^
- >>> math.isfinite(0.0001)
7 t' J: o. ?- }* y4 o - True
复制代码
' @$ U. y& F; u: ]math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
2 e( z" j ?/ b% Y, j& w; t- #如果x是正无穷大或负无穷大,则返回True,否则返回False
$ ~0 Y$ ]0 z- C) D0 s2 _ - isinf(x) -> bool( {! u# x5 Q7 U5 y* d
- Return True if x is a positive or negative infinity, and False otherwise.; h1 k( i3 y- b p1 e3 T# t: m- C% R
- >>> math.isinf(234)' C+ ^6 q J% M% S+ n+ [8 b
- False
9 C2 P( i; ~- ^1 [0 B+ {. A - >>> math.isinf(0.1)' j1 U1 y7 ]- O0 n3 L
- False
复制代码
_: f1 M! z; V1 W; Zmath.isnan(x) 如果x不是数字True,否则返回False
& e: Q, T; u3 A; [2 b' Z# H- #如果x不是数字True,否则返回False
) X$ t4 Y9 e* @/ X& X6 E8 K* }5 S - isnan(x) -> bool
- n( @* i& t. l$ V - Return True if x is a NaN (not a number), and False otherwise.2 b' Z: \- |# g1 w
- >>> math.isnan(23)
1 A7 e" q' {7 I8 p. a3 H8 }5 A - False
$ c& v" B! ~( J: ?( h - >>> math.isnan(0.01)
1 d5 ]8 y; |" o6 V - False
复制代码 # {" t( r$ f C, A1 K: h3 @
math.ldexp(x,i) 返回x*(2**i)的值
! b: O) R- a+ P: K- e- s! f- #返回x*(2**i)的值* m! h/ U F9 s' W* y! C
- ldexp(x, i)' E3 o+ j- J# S4 g5 d& B
- Return x * (2**i).
1 ]& H" Q) Z) n* u6 H6 T5 H% N, T - >>> math.ldexp(5,5)3 L+ `$ B9 n& o$ G+ U: M
- 160.0
& j* }( {1 O2 [8 c Y - >>> math.ldexp(3,5)
- e7 [2 ?) j! V+ j- `4 U; L - 96.0
复制代码 * m" m+ b: G' D! k, m5 z9 r
math.log10(x) 返回x的以10为底的对数
! x0 O. X% A: ^1 w2 S- #返回x的以10为底的对数
' {# l0 L: t" K6 C5 A - log10(x)
- M4 c8 t3 p/ }3 f% G. \/ d - Return the base 10 logarithm of x.
: x* z9 {; ]& k) z/ s - >>> math.log10(10)$ A/ ^( I4 @6 }4 o* s6 s
- 1.0
; ?: N$ |/ m( ]2 Y - >>> math.log10(100)/ ~4 x; Z0 r7 ^8 p! ~" N/ I% y
- 2.0
1 x9 i/ P. @) O' @! ^4 z( l - #即10的1.3次方的结果为20
& M& \" v: V, B1 {, s - >>> math.log10(20) Z3 ^0 e/ t6 P8 }% B) F+ l" T
- 1.3010299956639813
复制代码 9 B0 ], ^! V6 x, ^/ e0 W; k/ B6 L
math.log1p(x) 返回x+1的自然对数(基数为e)的值
: C) o- @- H( u; Z0 u" r. @- #返回x+1的自然对数(基数为e)的值
" ?* e& U# f, u1 \ - log1p(x): z" H$ H1 l; v/ Z" e
- Return the natural logarithm of 1+x (base e).
0 F& H1 j$ t* A& Z - The result is computed in a way which is accurate for x near zero.
' ], q4 K0 Y6 P3 W5 f, a9 ] - >>> math.log(10)/ _! C: [! \( @3 e% X) F! X6 m
- 2.302585092994046
7 u* v+ d) z5 Z - >>> math.log1p(10)7 s% A* L" B; m& d3 n; p
- 2.3978952727983707% ~( X* a: F' l* k$ T
- >>> math.log(11)7 S$ B! U2 `5 W# ~
- 2.3978952727983707
复制代码
& T1 V: o0 t1 Omath.log2(x) 返回x的基2对数
- I+ U1 U% X3 W+ s9 Q# \1 Z- #返回x的基2对数
1 ^: e, v0 `* z& q! x$ S+ S/ t - log2(x)* C' V6 a9 h/ E6 y$ N% M
- Return the base 2 logarithm of x.3 h w8 r6 D7 O0 e3 d6 K9 A
- >>> math.log2(32)
" T0 s' B! [# g) j' O5 H# r$ h - 5.0/ O- H- t6 S+ t4 I. ^) B
- >>> math.log2(20)/ F6 r; w) |' Z+ _- G
- 4.321928094887363$ b# p' P. l" w; l! h% @7 F
- >>> math.log2(16)
6 P* r* }5 S$ l# i5 \6 F - 4.0
复制代码
* `! a$ I5 `$ fmath.modf(x) 返回由x的小数部分和整数部分组成的元组
+ o2 o, n" S* d2 u, A& t; b1 h- #返回由x的小数部分和整数部分组成的元组
+ N' k- `; G$ |9 _. o - modf(x)
" Q9 H3 d' H3 e- C8 F - Return the fractional and integer parts of x. Both results carry the sign
, U4 W- ^1 H( w: V/ V) g) ] - of x and are floats.
& x& J) }+ s$ @+ F. h/ g/ Y& ?9 ] - >>> math.modf(math.pi)0 R7 q# H% }5 G0 ^+ B3 @
- (0.14159265358979312, 3.0)
* C* w: l2 `8 V2 C - >>> math.modf(12.34), {* j. r5 V" }
- (0.33999999999999986, 12.0)
复制代码 ' @$ l) z: C3 ~6 Z5 R% P5 u/ a
math.sqrt(x) 求x的平方根# p! f' e8 K' }" d$ ?% B
- #求x的平方根) o# f8 o* w& H2 V* J
- sqrt(x)
& q+ U" [( L) d# u5 g7 ?* R - Return the square root of x.
% W4 X4 D7 ]1 ]7 w h- @0 r - >>> math.sqrt(100)3 Z; K$ g1 K" i' C9 w6 @1 I
- 10.0$ L9 l; V# h0 g2 ]; @' B. ~
- >>> math.sqrt(16)1 Y* @1 Z, G- R" W3 Q& h% b
- 4.0
+ [, [2 m2 f9 B. T7 R2 R - >>> math.sqrt(20)
/ } l" E: N! G# u# }: M. Q/ c - 4.47213595499958
复制代码 ]# I; M* z0 A, m1 x
math.trunc(x) 返回x的整数部分- #返回x的整数部分* P. g. C3 k, }$ G/ s
- trunc(x:Real) -> Integral& r, |5 i7 r. N- y- C1 m8 \0 w
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
+ i8 [- u. {+ k7 T4 w' E - >>> math.trunc(6.789)
: Y3 t% F0 y5 g! C5 S - 65 \4 i7 y; ` M$ w/ `% S
- >>> math.trunc(math.pi)' D3 U6 s% I6 l/ o. a2 j
- 3) E0 t" C) y$ j" t9 F
- >>> math.trunc(2.567)3 [. Y1 k8 n; H& e: v+ ~9 N; [
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|