JS Math数学对象属性与方法参考手册实战版

文章作者:中山市飞娥软件工作室「Feiesoft.com」  浏览次数:2895 次  更新日期:2013-10-29

JavaScript Math对象提供了部分常规的数学数值运算方法,其中,在网站开发实战中,以下几个JS Math方法使用频率最高:Math.random()、Math.round(x)、Math.min()、Math.max()

<pre><script type="text/javascript">
//==================================================================================================
//JS Math数学对象属性方法
//JS Math属性
document.writeln("Math.E=" + Math.E); // 输出:Math.E=2.718281828459045(说明:自然对数的底)e = 1 + 1 + 1/2! + 1/3! +... + 1/n! + ...
document.writeln("Math.LN2=" + Math.LN2); // 输出:Math.LN2=0.6931471805599453(说明:2的自然对数)
document.writeln("Math.LN10=" + Math.LN10); // 输出:Math.LN10=2.302585092994046(说明:10的自然对数)
document.writeln("Math.LOG2E=" + Math.LOG2E); // 输出:Math.LOG2E=1.4426950408889633(说明:以2为底E的对数)
document.writeln("Math.LOG10E=" + Math.LOG10E); // 输出:Math.LOG10E=0.4342944819032518(说明:以10为底E的对数)
document.writeln("Math.PI=" + Math.PI); // 输出:Math.PI=3.141592653589793(说明:圆周率)
document.writeln("Math.SQRT1_2=" + Math.SQRT1_2); // 输出:Math.SQRT1_2=0.7071067811865476(说明:1/2的平方根)
document.writeln("Math.SQRT2=" + Math.SQRT2); // 输出:Math.SQRT2=1.4142135623730951(说明:2的平方根)
//==================================================================================================
//JS Math方法:三角函数篇

var π = Math.PI,x = π/4;
document.writeln("Math.cos(x)=" +  Math.cos(x) + "注:√2/2=" + Math.sqrt(2)/2); // 输出:Math.cos(x)=0.7071067811865476注:√2/2=0.7071067811865476(说明:求余弦cos(x)∈[-1,1],x∈R)
//第一余弦定理(任意三角形射影定理):设△ABC的三边是a、b、c,它们所对的角分别是A、B、C,则有a=b·cosC+c·cosB, b=c·cosA+a·cosC, c=a·cosB+b·cosA。

//下面求sin(30) = 1/2
var π = Math.PI,x = π/6;
document.writeln("Math.sin(x)=" +  Math.sin(x)); // 输出:Math.sin(x)=0.49999999999999994(~1/2)(说明:求正弦sin(x)∈[-1,1],x∈R)
//正弦定理:在△ABC中,角A、B、C所对的边长分别为a、b、c,三角形外接圆的半径为R。则有 a/sinA = b/sinB = c/sinC = 2R
//即,在一个三角形中,各边和它所对角的正弦之比相等(a:b:c = sinA:sinB:sinC),该比值等于该三角形外接圆的直径长度。

//下面求tan(45) = 1
var π = Math.PI,x = π/4;
document.writeln("Math.tan(x)=" +  Math.tan(x)); // 输出:Math.tan(x)=0.9999999999999999(~1)(说明:求正切tan(x)∈R,x∈{x|x≠(π/2)+kπ,k∈Z})
//正切定理:在平面三角形中,任意两条边的和除以第一条边减第二条边的差所得的商等于这两条边的对角的和的一半的正切除以第一条边对角减第二条边对角的差的一半的正切所得的商.
//(a + b) / (a - b) = tan((α+β)/2) / tan((α-β)/2)

var x = 0.5;
document.writeln("Math.abs(x)=" +  Math.abs(x)); // 输出:Math.abs(x)=0.5(说明:求绝对值|x|,x∈R)

var x = 0;
document.writeln("Math.acos(x)=" +  Math.acos(x)); // 输出:Math.acos(x)=1.5707963267948965(π/2)(说明:求反余弦arccosx∈[0,π],|x|≤1)

var x = 1;
document.writeln("Math.asin(x)=" +  Math.asin(x)); // 输出:Math.asin(x)=1.5707963267948965(π/2)(说明:求反正弦arcsinx∈[0,π],|x|≤1)

var x = 9999999999;
document.writeln("Math.atan(x)=" +  Math.atan(x)); // 输出:Math.atan(x)=1.5707963266948965(~π/2)(说明:求反正切arctanx∈(-π/2,π/2),x∈R)

var x = 1,y = 1;
document.writeln("Math.atan2(y,x)=" +  Math.atan2(y,x) + "(" + Math.atan2(y,x) * 180 / Math.PI + "度)"); // 输出:Math.atan2(y,x)=0.7853981633974483(45度)(说明:求向量(x,y)与x轴的夹角,单位:弧度)

//==================================================================================================
//JS Math方法:指数函数篇

var x = 1;
document.writeln("Math.exp(x)=" +  Math.exp(x)); // 输出:Math.exp(x)=2.718281828459045(说明:求自然对数e的x次幂,x∈R)

var a = 2,x = 10;
document.writeln("Math.pow(a,x)=" +  Math.pow(a,x)); // 输出:Math.pow(a,x)=1024(说明:求a的x次幂,(a>0且≠1) x∈R)

var x = 2;
document.writeln("Math.sqrt(x)=" +  Math.sqrt(x) + ",等价于Math.pow(x,1/2) = " + Math.pow(x,1/2)); // 输出:Math.sqrt(x)=1.4142135623730951(说明:求x的平方根,(x>0,x∈R)

var x = -2;
document.writeln("Math.sqrt(x)=" +  Math.sqrt(x)); // 输出:Math.sqrt(x)=NaN(说明:求x的平方根,x>0,x∈R)

//==================================================================================================
//JS Math方法:求整篇

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Math.ceil(x)求比x大的最小整数,x∈R
var x = 1.4;
document.writeln("Math.ceil(" + x + ")=" +  Math.ceil(x)); // 输出:Math.ceil(1.4)=2
var x = 1.5;
document.writeln("Math.ceil(" + x + ")=" +  Math.ceil(x)); // 输出:Math.ceil(1.5)=2
var x = -1.4;
document.writeln("Math.ceil(" + x + ")=" +  Math.ceil(x)); // 输出:Math.ceil(-1.4)=-1
var x = -1.9;
document.writeln("Math.ceil(" + x + ")=" +  Math.ceil(x)); // 输出:Math.ceil(-1.9)=-1
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Math.floor(x)求比x小的最大整数,x∈R
var x = 1.4;
document.writeln("Math.floor(" + x + ")=" +  Math.floor(x)); // 输出:Math.floor(1.4)=1
var x = 1.5;
document.writeln("Math.floor(" + x + ")=" +  Math.floor(x)); // 输出:Math.floor(1.5)=1
var x = -1.4;
document.writeln("Math.floor(" + x + ")=" +  Math.floor(x)); // 输出:Math.floor(-1.4)=-2
var x = -1.9;
document.writeln("Math.floor(" + x + ")=" +  Math.floor(x)); // 输出:Math.floor(-1.9)=-2
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Math.round(x)求x四舍五入后整数,x∈R
var x = 1.4;
document.writeln("Math.round(" + x + ")=" +  Math.round(x)); // 输出:Math.round(1.4)=1
var x = 1.5;
document.writeln("Math.round(" + x + ")=" +  Math.round(x)); // 输出:Math.round(1.5)=2
var x = -1.4;
document.writeln("Math.round(" + x + ")=" +  Math.round(x)); // 输出:Math.round(-1.4)=-1
var x = -1.9;
document.writeln("Math.round(" + x + ")=" +  Math.round(x)); // 输出:Math.round(-1.9)=-2

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//JS Math方法:其它

//Math.log(x) - 求x的自然对数,x∈R
var x = Math.E;
document.writeln("Math.log(" + x + ")=" +  Math.log(x)); // 输出:Math.log(2.718281828459045)=1

//Math.min(x1,x2,x3,...) - 求x1,x2,x3,...中的最小值
document.writeln("Math.min(3,2,4)=" +  Math.min(3,2,4)); // 输出:Math.min(3,2,4)=2

//Math.max(x1,x2,x3,...) - 求x1,x2,x3,...中的最大值
document.writeln("Math.max(3,2,4)=" +  Math.max(3,2,4)); // 输出:Math.min(3,2,4)=4

//Math.random() - 返回[0,1]随机数
document.writeln("Math.random()=" +  Math.random()); // 本次输出:Math.random()=0.03423219800488453

</script></pre>