马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 H+ S& C! D+ _. T8 y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。+ V' Q8 H1 h) ]- X. L; ~
5 D2 R# _* }. d方法1:
7 B# H, H. K; _ C- >>> import math3 l* ^6 ~' W+ p& H
- >>> math.sqrt(9)
i, K* b/ h/ O" u7 | - 3.0
复制代码 方法2:
, w5 O. Y$ ? N4 `7 k, ~- >>> from math import sqrt5 k) F# D1 e7 l' A4 l+ g
- >>> sqrt(9)
/ Y |/ z, ?* @) u - 3.0
复制代码
5 _9 Z, w& K" g+ C1 e) o d' X
) N3 [$ e% n. d+ tmath.e 表示一个常量
8 h% @/ n6 @9 i4 A- #表示一个常量
' K4 g9 `, o, b) M; G* Z& q: c - >>> math.e4 n" ?6 u l2 T0 k9 J6 b: _
- 2.718281828459045
复制代码
+ ? k9 s* |# O! i( Y2 N* Wmath.pi 数字常量,圆周率
# ?! P& d7 V% } d' B: g' t/ p5 O- #数字常量,圆周率
+ z( @7 l$ q, `4 b/ k. G - >>> print(math.pi)% c' `: q: I: O2 L0 F8 h
- 3.141592653589793
复制代码
3 k6 c8 S% A, v# rmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 h( C7 K+ t" H$ Y- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
) e& g: {* F) h) G - ceil(x)
4 S* m8 l9 o. F* P0 A; F - Return the ceiling of x as an int.
1 X: `2 E- w9 U - This is the smallest integral value >= x.
& B" @; q5 q6 G0 a
. J- T4 H3 F, u5 z# n+ `+ r- >>> math.ceil(4.01)- o) m6 T$ A# N4 V
- 5! W$ T# ]- }4 K$ \! s8 L! T
- >>> math.ceil(4.99)
6 `' s J' T$ y, O6 u* E3 X - 5
2 ?/ U9 M; w# q% F - >>> math.ceil(-3.99), Y' L: O4 q' Z0 u/ `6 B" }
- -3" B- T% y4 I( P# _0 b
- >>> math.ceil(-3.01)
$ ?& e( d, l, @& f - -3
复制代码 / v* A, ^8 g# m% D' b7 f ?# `+ R
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. U/ [1 L1 m! }- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ y: o* q+ o% T6 E - floor(x)* U- c* A1 L& x$ y
- Return the floor of x as an int.
3 Y2 ]" E+ i. {: w; d% @ - This is the largest integral value <= x.
5 w7 I4 r2 @0 C* ^5 D- E - >>> math.floor(4.1)
: b/ F( S$ k9 V% { - 4$ S% t1 D7 p! k. o. z2 s
- >>> math.floor(4.999)
* F' q' u' [ ~! I( X7 B/ x1 b - 49 X8 P+ m8 ` d1 D. g& Z& W, v0 \/ g4 M+ l
- >>> math.floor(-4.999)
/ ]( k7 Q; C8 O - -52 c1 Z2 F1 s$ f) h$ Y
- >>> math.floor(-4.01)+ O( ~, [3 a Y- W* S1 j7 @
- -5
复制代码 ' C% A' Q; t h7 g* M5 I& K, x4 J
math.pow(x,y) 返回x的y次方,即x**y
8 V5 u+ [( f" i3 f6 c: r- #返回x的y次方,即x**y
, ~0 A2 C/ Y6 m. E! h1 j - pow(x, y)+ _+ z! S, _: d1 j- P
- Return x**y (x to the power of y).7 L& w! i2 o7 c1 d$ O7 Z
- >>> math.pow(3,4)/ f6 t4 V: A+ d. Z' Q
- 81.08 g6 c0 W4 A& _0 j; d
- >>>
q2 B7 ^8 D( c& O3 l - >>> math.pow(2,7)
6 m& ^: V, U+ P5 [ - 128.0
复制代码 ' H- q) I% ~' R- r; A \0 G
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ @# j9 {) G. i: N- e+ E- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 o7 z4 h3 {$ Z- h
- log(x[, base])
, o/ W j) E O7 |! Y - Return the logarithm of x to the given base.2 i) h, _3 k. F6 X; b
- If the base not specified, returns the natural logarithm (base e) of x.
3 X* ~: a3 e) m7 \3 ` - >>> math.log(10)7 S& {3 K* I0 [7 v' {
- 2.302585092994046
: {1 z$ s" k0 b0 X& ^ - >>> math.log(11)
$ I# |8 w' m* T - 2.39789527279837077 i( O6 z/ R2 `& w* z9 P [' A# ~" `
- >>> math.log(20); q W0 |+ M: p0 Q! b. N
- 2.995732273553991
复制代码 . h0 j" e5 G% A/ C8 @9 u" t4 I
math.sin(x) 求x(x为弧度)的正弦值
# ^) V# ^ V" v6 }- #求x(x为弧度)的正弦值
! m& _# V2 l" l, G8 W% j - sin(x)' q/ A- X! y; C# X& Z& E% ?
- Return the sine of x (measured in radians).- ^1 o6 t# @8 R A
- >>> math.sin(math.pi/4)
/ Q# g* p7 \" U; a+ R" [% P - 0.7071067811865475
" K8 c: E" L0 t7 g) |( m& J - >>> math.sin(math.pi/2)
1 Y& Y' [/ @5 r - 1.0
, ^9 o. b) H1 D, U$ @% F+ g - >>> math.sin(math.pi/3)
5 H6 w* N& F: R# Q% ]; j - 0.8660254037844386
复制代码
+ }6 k, D# H7 G xmath.cos(x) 求x的余弦,x必须是弧度
, s4 ^' n% k% C2 ?$ z( H- #求x的余弦,x必须是弧度
5 H* J$ }/ N0 @8 s9 K$ b0 f - cos(x)
0 X/ |' {9 _- R' b - Return the cosine of x (measured in radians).
5 B" t* x) @ G8 G/ A6 B/ @* V - #math.pi/4表示弧度,转换成角度为45度# q0 Y6 ~ o! y% I: [
- >>> math.cos(math.pi/4)5 u! H& Z; P) N& k, h9 i$ j! ~$ b- D
- 0.7071067811865476* y4 y) [+ F* U' }# i
- math.pi/3表示弧度,转换成角度为60度
6 M. y5 h4 T1 V) H6 I/ v6 @! ? - >>> math.cos(math.pi/3)
6 z4 r; B: h1 r( ] - 0.50000000000000010 |! }- e3 f+ f( q& _# q! J$ a
- math.pi/6表示弧度,转换成角度为30度# p+ l5 C3 s% N# T
- >>> math.cos(math.pi/6)# C. j9 T5 t; e
- 0.8660254037844387
复制代码 ) a; i' l* c% l8 u8 s
math.tan(x) 返回x(x为弧度)的正切值; k0 C3 O6 B- S& d% A( h* i4 U% d
- #返回x(x为弧度)的正切值
7 L. w# r7 A$ }4 k" q9 L. ~ - tan(x)
* Z- v- x9 l: X4 ~ - Return the tangent of x (measured in radians).5 l( j1 y$ v# a/ g
- >>> math.tan(math.pi/4)% ]# @( P# {" ?( {% ]5 Q
- 0.9999999999999999
5 b0 B1 o. Y9 r, r& n9 A - >>> math.tan(math.pi/6)2 \! K3 j3 A9 P# m2 F! w! K
- 0.5773502691896257+ w' p) ]! U3 o
- >>> math.tan(math.pi/3)) z7 p8 t2 U x% {6 }# R' p
- 1.7320508075688767
复制代码 + x. @. T/ E, a
math.degrees(x) 把x从弧度转换成角度. }/ K3 x( [" y- A
- #把x从弧度转换成角度' P# K4 R; G. T" [4 p6 V
- degrees(x)
+ z' Y; j) U9 @% V' \6 ^* Z - Convert angle x from radians to degrees. J- e4 E1 ^. @/ \
: { D& u' m6 O- M O- >>> math.degrees(math.pi/4)
2 _, F7 U2 u- e) Y% k5 M/ W - 45.0
/ r) R- u8 U5 x( \. p - >>> math.degrees(math.pi)/ P3 W2 @8 X' ^( r3 @
- 180.0$ Y. c {5 z( j1 s8 i6 O0 r- T7 N
- >>> math.degrees(math.pi/6)
" t) U) h6 z! M' N( m" p [; { - 29.999999999999996
1 c, y7 M' x/ h6 F6 t Y' s! T - >>> math.degrees(math.pi/3)
* ]& p; Y1 V. ^& L/ R - 59.99999999999999
复制代码
w- i* Z! k, Y7 l! Q1 Q3 Y. Tmath.radians(x) 把角度x转换成弧度
: [ p3 ^( R4 k! h1 m# A; S- #把角度x转换成弧度9 |9 ~; V6 Z8 X6 ?
- radians(x)
- A9 t: j7 n2 j& J% z - Convert angle x from degrees to radians.) G5 a7 E( Z6 S
- >>> math.radians(45), p+ s6 Q2 X2 a
- 0.7853981633974483
2 |( O8 J- M! W" w2 p( W - >>> math.radians(60)
! g1 q7 o1 M8 }+ |0 \2 E" h - 1.0471975511965976
复制代码 ( l; k! F# C# u
math.copysign(x,y) 把y的正负号加到x前面,可以使用0( p9 ]/ U- k* k
- #把y的正负号加到x前面,可以使用04 \$ x4 f2 I/ J- V7 |
- copysign(x, y)
B& Y' L$ P, f- }* }' j/ p- o' v - Return a float with the magnitude (absolute value) of x but the sign ( Q1 Q+ p2 Y/ l& X6 p
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) % n# b( s) ?6 d- J. Z2 ~0 H* J
- returns -1.0.5 ]* C! p) M3 X9 {6 s& C
- 9 `1 w0 N& Y* _2 N, W& A- B
- >>> math.copysign(2,3)
' @- v( Z. D C. ]+ O - 2.0. I, x% a: V! Y; A' m, q5 N
- >>> math.copysign(2,-3)
{6 C* @& \4 n0 T, g S8 A - -2.0
5 b: ]. N) t& i - >>> math.copysign(3,8)4 f Z5 ~9 D5 b' J/ z- A7 L0 ]9 r4 u
- 3.0
* ?( m" c0 R" {9 D" \ - >>> math.copysign(3,-8)
- Y8 ?$ t$ c* B. F6 z% C - -3.0
复制代码
3 a& X! W, O$ B6 |( A( wmath.exp(x) 返回math.e,也就是2.71828的x次方
4 R# W( t9 ~/ {0 h# g- A- #返回math.e,也就是2.71828的x次方; V; c; N% C4 o' `/ m
- exp(x)9 c; F& T J7 C: p2 _
- Return e raised to the power of x.
+ U9 h9 a# A6 i! v6 u# Y6 p
* I0 M( C( ~' A# _, y2 _* r9 H- >>> math.exp(1)' L6 v: U9 M( |* M- p# b( {. O
- 2.7182818284590452 ]8 R% U9 R3 ?: D
- >>> math.exp(2)2 R) j8 V9 B& ?; \) o
- 7.389056098930657 d* B# g4 X7 |- T" Y
- >>> math.exp(3)
# [( f3 R3 k7 i9 d. I - 20.085536923187668
复制代码
2 j+ V7 S! _1 {: G$ F$ j, }math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
% {: E: ]3 O) m' j4 w0 Z- #返回math.e的x(其值为2.71828)次方的值减1* e# I3 o: O% p5 _ t
- expm1(x)6 K# f8 e' ?( T& a
- Return exp(x)-1.
0 n- _7 [8 ]: ]7 i- n9 D - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
, }( E( E/ L2 y5 x1 ` - 6 u3 `7 O, u! v: I# f. L
- >>> math.expm1(1)
9 K' C N. f, A! s7 k# D$ P - 1.718281828459045
0 V. q( p1 T y8 J% ~+ C - >>> math.expm1(2)7 R* r2 g7 ] v5 x
- 6.38905609893065; B- B* {$ H2 _' p0 G1 J: N( q; [
- >>> math.expm1(3). W* [( ?7 A j; V/ J8 `
- 19.085536923187668
复制代码
3 k y; l; K- v. }8 D; B( x, ymath.fabs(x) 返回x的绝对值4 H- e9 o. }; Z" w' E% X# f
- #返回x的绝对值
- f% _8 {5 n) i - fabs(x)
9 \, {" u, o, U/ e- _6 u+ H# U - Return the absolute value of the float x. Q1 H K( j. n; w- r$ G' c
- # z7 I7 y8 v) \# ]
- >>> math.fabs(-0.003)
$ a) V1 x Z& q - 0.003
! V! t& T- l& |+ X- e8 v) H - >>> math.fabs(-110)
; e* c6 `( p; k; z" Q V - 110.0) P* O9 Q- e, p. W" ]
- >>> math.fabs(100)' I# b: I8 g1 f5 s2 o7 m& x1 `
- 100.0
复制代码
+ P% f+ B' g+ d- [math.factorial(x) 取x的阶乘的值+ q1 f5 V# v1 q9 ], Y9 n9 j
- #取x的阶乘的值
4 v% G: B, E5 k' l8 o3 C: N E+ Y; ` - factorial(x) -> Integral8 c6 Y8 Z4 B0 \3 j# [
- Find x!. Raise a ValueError if x is negative or non-integral.4 P" S% a6 u$ Y2 v
- >>> math.factorial(1)
0 J& s) f) P9 r$ u( J4 _& i - 1
- ?6 b- o) i6 c+ S" h, @% N - >>> math.factorial(2)* v+ O) s8 F _/ T
- 2- G( {& H8 _, k) z0 [- @ s3 M O
- >>> math.factorial(3)+ o& S6 Z4 y M/ b, N1 _
- 6
( V0 _' X) w' `8 H5 n - >>> math.factorial(5)
( n! V0 M- J6 T - 1200 }/ E% B1 \) R4 j3 L: o. M1 e
- >>> math.factorial(10)1 n. j5 `; h3 X
- 3628800
复制代码 1 t: k, O5 Y/ |: r- }
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数8 y. E9 [& B9 h3 @( k- Y0 B
- #得到x/y的余数,其值是一个浮点数
+ V+ ]; i& t: u( g, }. d o - fmod(x, y)
7 O( M7 t: L: E2 p - Return fmod(x, y), according to platform C. x % y may differ.9 | j- v. u! T# _" i0 R
- >>> math.fmod(20,3)8 t& k5 i: Q% _: t7 W' ?
- 2.0
# M* J; t; L, ~ - >>> math.fmod(20,7)# g2 C7 U" N; X" Q, T1 T5 ]0 j: }" ?$ V" U
- 6.0
复制代码 & @- }; o% U$ k7 T p" k8 f5 Q
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围$ N' `5 F8 O, }0 o; U7 g \
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
5 H( e# V5 d: H, a# ?; {5 B - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 r9 J% u4 p, p- |) u3 k
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
& M: \- z, p0 J G8 H - frexp(x)) ~+ l( E9 [+ `- a. v
- Return the mantissa and exponent of x, as pair (m, e).
( ?$ U% C9 w- p9 A9 [ - m is a float and e is an int, such that x = m * 2.**e.+ ?8 u g3 q$ z& I( L! }
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
8 g2 Y/ X/ L8 S/ r8 l6 u* j v3 N - >>> math.frexp(10)
Y8 h% F1 b$ G - (0.625, 4)- r! @4 j4 s9 F" ]" I
- >>> math.frexp(75)$ G$ G' W7 Q5 k! u f
- (0.5859375, 7)
8 `8 D; d- ~$ z8 M6 ^2 I6 R - >>> math.frexp(-40)
( I( U' U" h! Q/ K% X: a9 @7 x - (-0.625, 6)/ R9 U5 z7 j; l* M5 a/ ?. K
- >>> math.frexp(-100)
* [8 y% Z) ^% @3 k& c& ]" ~ - (-0.78125, 7)' ?* s. M6 u7 t$ x, x8 \: z, ~
- >>> math.frexp(100)5 V( [5 M# q7 @2 q" A: v
- (0.78125, 7)
复制代码
8 T+ k! i- F6 D; U5 |: J/ A& Nmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)5 k* u* ^- S3 m" d) ~9 c3 P5 ]0 J
- #对迭代器里的每个元素进行求和操作- o4 N. `- K+ {& h. b. O& _8 @
- fsum(iterable)6 u7 l; k4 E" t5 u5 k5 R- x" W& W9 ?
- Return an accurate floating point sum of values in the iterable.
- n: B3 m9 k s; |# A } - Assumes IEEE-754 floating point arithmetic.
0 O! z' I$ x: o; W) a - >>> math.fsum([1,2,3,4])
1 q \! m9 k8 u- r - 10.03 `5 E! o8 o: K& a$ l, b
- >>> math.fsum((1,2,3,4))( E( t( n+ ?8 K& a) p+ j# W6 B
- 10.02 x/ w# Z Y# x1 ^
- >>> math.fsum((-1,-2,-3,-4))4 ]: N2 [! @& [" F/ R! q. Z; e% Z
- -10.0
3 k3 M5 h& t: w+ I$ N D - >>> math.fsum([-1,-2,-3,-4])
6 e2 I4 q# {2 a# d9 Z - -10.0
复制代码 # y# [# x3 V# J+ ]1 A% t/ |
math.gcd(x,y) 返回x和y的最大公约数
! q1 Q; t8 G& t7 O" M* h) n4 H- #返回x和y的最大公约数
6 i* a) c5 W8 f - gcd(x, y) -> int2 J0 _: q2 { E% u3 u' }1 H
- greatest common divisor of x and y# s7 o Q( Y2 V9 A$ W0 }
- >>> math.gcd(8,6)
- |/ l( x. S- }/ B, ? - 27 c' U7 Q/ ^6 d2 z0 j+ w
- >>> math.gcd(40,20)1 w7 x0 }3 {* h2 r1 R! U+ K
- 20
& G2 v! G1 ]- ^; ^: \ - >>> math.gcd(8,12); W) j% X* G5 N( x
- 4
复制代码 , b" A+ k" [! c3 v( s9 J
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False' a& a) B1 X7 \; P7 V2 m$ I
- #得到(x**2+y**2),平方的值
/ O$ B/ W6 U; }/ R/ v/ I - hypot(x, y)
) v. w9 S+ z/ u, u" B ^$ f - Return the Euclidean distance, sqrt(x*x + y*y).
! N$ U6 p% P/ V! N4 r - >>> math.hypot(3,4)5 c1 G5 ~- b$ |
- 5.0, C: P+ z" ^9 p" }
- >>> math.hypot(6,8)7 f" M0 m- c" Q3 X$ t2 }+ x* s
- 10.0
复制代码 " t0 [7 e5 E W+ \4 G8 J/ Y9 H
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
0 u! R& o( J$ N" p- #如果x是不是无穷大的数字,则返回True,否则返回False- H* y. c6 B7 R2 O8 b. Q
- isfinite(x) -> bool( i9 M% j% M) W, y* P
- Return True if x is neither an infinity nor a NaN, and False otherwise.
0 \8 l3 R/ R) r. K9 m2 D - >>> math.isfinite(100)8 z, C' ]/ D7 A0 |- e4 o6 m! N/ B
- True! e) F4 H6 P, V
- >>> math.isfinite(0)
/ f8 Y9 ]# l* t K$ B - True
, f! g/ C$ v7 T% J9 S. T, C1 j9 x - >>> math.isfinite(0.1)
, q$ `6 t- u% c3 @7 t" k! P9 p6 R - True
0 l! w8 A" Z7 I/ U5 } - >>> math.isfinite("a")6 _! L, c" n0 y+ D
- >>> math.isfinite(0.0001)
, `+ f/ o% s+ C; z6 F" j - True
复制代码
4 \( M+ v5 f$ ]. ^math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False) Y. g& w% l% }; K
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
4 h; T) Y5 N- S% ?! D$ c8 x - isinf(x) -> bool
6 ?2 d: ]* g- g1 i; O - Return True if x is a positive or negative infinity, and False otherwise.- N5 z {9 O# Z' o' L& x- U
- >>> math.isinf(234)
2 T2 T! r% S# E2 }+ u; i - False
! ?1 U9 E9 K' g - >>> math.isinf(0.1)8 L8 {. X( o, F* J5 i8 @
- False
复制代码 . O }( }) ], O0 m# j' ^
math.isnan(x) 如果x不是数字True,否则返回False8 W! j. q$ d! ?* s
- #如果x不是数字True,否则返回False
6 x% l5 `" c3 p, v3 i( s; g - isnan(x) -> bool
& e+ h& i! g8 `& j! _5 g - Return True if x is a NaN (not a number), and False otherwise.
* Q6 e3 Z7 {0 B2 m/ c7 a - >>> math.isnan(23)
6 `# [+ C5 a0 p/ J( b - False7 X S& {8 `3 G! ]: u. Q
- >>> math.isnan(0.01)
( s F% m% H5 I6 d O - False
复制代码 + q5 e2 e3 `# n: x2 b! `( ?
math.ldexp(x,i) 返回x*(2**i)的值8 K' G! K E+ O
- #返回x*(2**i)的值
! y6 _3 F5 p. _+ l& m - ldexp(x, i)
# R, n. c5 [' {. Z1 b - Return x * (2**i).
( H9 i% E2 H) y( e% _ - >>> math.ldexp(5,5)" s% c$ l2 M7 W- x+ B
- 160.0) K ^" J% M" z5 ~- B/ k- `, i
- >>> math.ldexp(3,5)6 \6 a7 i3 ?9 z. M
- 96.0
复制代码 0 M6 S0 V9 L0 E7 O5 C; h
math.log10(x) 返回x的以10为底的对数
7 _# y: n/ T: D3 C; y* l- #返回x的以10为底的对数 Z8 r% E" Z# w# `1 \0 S4 L3 g* v& a
- log10(x)
) w. y, p0 z5 _6 l. W/ W# f - Return the base 10 logarithm of x.
+ A; Q% x3 g& @5 `3 U2 h - >>> math.log10(10), S$ j0 o' ?% Q* _! m
- 1.0
1 q# Q: Z7 l* i1 i+ `. A - >>> math.log10(100)
# A h" M, f: k* y - 2.0
6 O& i3 w8 d5 `& y - #即10的1.3次方的结果为20
+ [5 N! ?$ H H6 U - >>> math.log10(20)# l m2 _ ~1 c; [) M: k
- 1.3010299956639813
复制代码
$ c4 T9 w0 i/ |math.log1p(x) 返回x+1的自然对数(基数为e)的值6 Y( T" X$ H" A3 {( x) @
- #返回x+1的自然对数(基数为e)的值! [. |: E+ [+ ~
- log1p(x)
+ f5 c* p9 O u, _) `, ^ - Return the natural logarithm of 1+x (base e).1 J& s6 d& Q* K- w. P* c7 B, |
- The result is computed in a way which is accurate for x near zero.
/ d4 ?& Y; K4 l% i$ D$ M - >>> math.log(10)
7 Z- u4 \( S5 k - 2.302585092994046
2 M8 x0 H5 ]5 z" t" G- `; O - >>> math.log1p(10), _( s v2 Y+ D7 G+ F
- 2.3978952727983707
% u* l$ U/ b* ?+ A - >>> math.log(11)
7 y2 T0 k; @9 l5 `' e - 2.3978952727983707
复制代码
' e! _, U: P$ e2 }math.log2(x) 返回x的基2对数
7 ]3 v1 z! N. T; T# b. _; U- #返回x的基2对数
$ O) R- W/ |- U) U) ?6 X - log2(x)% o* z2 E4 m$ R8 H4 `7 D
- Return the base 2 logarithm of x.
% `( ^% Q- X a _ - >>> math.log2(32)- B6 d4 H k+ z3 Z- Q
- 5.0" r) l$ Q3 {. Q- i5 z: @3 j
- >>> math.log2(20)
2 }$ ~3 ?, h @2 Q0 Z% r# G - 4.3219280948873639 Z( X/ ^8 S! S' j% A
- >>> math.log2(16)
4 ]4 H) G8 P+ N* ?+ d/ B8 m( s6 b& H - 4.0
复制代码
3 ]" k( e \: F! Y3 A3 }; Hmath.modf(x) 返回由x的小数部分和整数部分组成的元组
' T) _7 x" _8 _3 u \2 E0 F- #返回由x的小数部分和整数部分组成的元组3 ]4 e T* b s
- modf(x)5 E7 U7 g, u: `7 L% Z X
- Return the fractional and integer parts of x. Both results carry the sign. |, R1 x- \, |7 p+ K
- of x and are floats.
& ?* h4 R: ^# T2 [" D - >>> math.modf(math.pi)
: q3 G# j/ q0 u; X' A - (0.14159265358979312, 3.0)
: T/ O: S" u3 a" Q) R4 [6 M6 M0 y - >>> math.modf(12.34)
* ?; R( e' R6 k" h7 y. Z8 W - (0.33999999999999986, 12.0)
复制代码 " g+ ?5 ]* h& O7 }7 g
math.sqrt(x) 求x的平方根 \0 j# v* e" ^; { J
- #求x的平方根
( O9 o) E8 k3 X' A" _2 A - sqrt(x) I w. e2 ?) j9 s6 i/ F3 R0 y
- Return the square root of x.
* l: G6 D" U0 P - >>> math.sqrt(100)
) @ o5 b, A5 O - 10.0; ~9 k: _$ P$ y
- >>> math.sqrt(16)
, f; J N# c, T6 z: h+ j - 4.0
3 q8 Q; G: \# X; ]& C - >>> math.sqrt(20)% E7 x8 u5 }2 ?: z% r
- 4.47213595499958
复制代码 2 j" p( w. h9 \$ Q+ j4 z
math.trunc(x) 返回x的整数部分- #返回x的整数部分: Q$ z3 d3 x6 s" s
- trunc(x:Real) -> Integral
" a; b! h$ |# Q) E1 a3 a- q - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' T9 z P- O! s& g
- >>> math.trunc(6.789)# `" b2 z/ {! s( U
- 60 p7 \/ g! K5 p7 O( }0 O4 A
- >>> math.trunc(math.pi)
r _& r4 d, q, \! \ - 31 P3 o" O/ y7 Q; b) f/ Q/ ~
- >>> math.trunc(2.567)
6 [ F* d: L9 c: | - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|