072_Math对象

1. Math对象允许您对数字执行数学任务。

2. Math属性(常量)

2.1. JavaScript提供了可由Math对象访问的8个数学常量:

Math.E // 返回算术常量e, 即自然对数的底数, 其值近似于2.71828
Math.PI // 返回圆周率(PI)(约等于3.14159)
Math.SQRT2 // 返回2的平方根(约等于1.414)
Math.SQRT1_2 // 返回1/2的平方根(约等于0.707)
Math.LN2 // 返回2的自然对数(约等于0.693)
Math.LN10 // 返回10的自然对数(约等于2.302)
Math.LOG2E // 返回以2为底的e的对数(约等于1.44)
Math.LOG10E // 返回以10为底的e的对数(约等于0.434)

2.2. 例子

2.2.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>Math对象常量</title>
	</head>
	<body>
		<script type="text/javascript">
			document.write('Math.E = ' + Math.E + '<br />'); // 返回算术常量e, 即自然对数的底数, 其值近似于2.71828
			document.write('Math.PI = ' + Math.PI + '<br />'); // 返回圆周率(PI)(约等于3.14159)
			document.write('Math.SQRT2 = ' + Math.SQRT2 + '<br />'); // 返回2的平方根(约等于1.414)
			document.write('Math.SQRT1_2 = ' + Math.SQRT1_2 + '<br />'); // 返回1/2的平方根(约等于0.707)
			document.write('Math.LN2 = ' + Math.LN2 + '<br />'); // 返回2的自然对数(约等于0.693)
			document.write('Math.LN10 = ' + Math.LN10 + '<br />'); // 返回10的自然对数(约等于2.302)
			document.write('Math.LOG2E = ' + Math.LOG2E + '<br />'); // 返回以2为底的e的对数(约等于1.44)
			document.write('Math.LOG10E = ' + Math.LOG10E + '<br />'); // 返回以10为底的e的对数(约等于0.434)
		</script>
	</body>
</html>

2.2.2. 效果图

3. Math.min()和Math.max()

3.1 Math.min()和Math.max()可用于查找参数列表中的最低或最高值。

3.2. 例子

3.2.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>Math对象最大值和最小值</title>
	</head>
	<body>
		<script type="text/javascript">
			document.write('Math.min = ' + Math.min(0, 450, 35, 10, -8, -300, -78) + '<br />');
			document.write('Math.max = ' + Math.max(0, 450, 35, 10, -8, -300, -78) + '<br />');
		</script>
	</body>
</html>

3.2.2. 效果图

4. Math.round()、Math.ceil()和Math.floor()舍入

4.1. Math.round(x)的返回值是x四舍五入为最接近的整数。

4.2. Math.ceil(x)的返回值是x上舍入最接近的整数。

4.3. Math.floor(x)的返回值是x下舍入最接近的整数。

4.4. 例子

4.4.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>Math.round()、Math.ceil()和Math.floor()舍入</title>
	</head>
	<body>
		<script type="text/javascript">
			var a = 3.9, b = 5.2;
			document.write('Math.round(' + a + ') = ' + Math.round(a) + '<br />');
			document.write('Math.round(' + b + ') = ' + Math.round(b) + '<br />');
			document.write('Math.ceil(' + a + ') = ' + Math.ceil(a) + '<br />');
			document.write('Math.ceil(' + b + ') = ' + Math.ceil(b) + '<br />');
			document.write('Math.floor(' + a + ') = ' + Math.floor(a) + '<br />');
			document.write('Math.floor(' + b + ') = ' + Math.floor(b) + '<br />');
		</script>
	</body>
</html>

4.4.2. 效果图

5. Math.pow()

5.1. Math.pow(x, y)的返回值是x的y次幂。

5.2. 实例

Math.pow(8, 2); // 返回 64

6. Math.sqrt()

6.1. Math.sqrt(x)返回x的平方根。

6.2. 实例

Math.sqrt(64);      // 返回 8

7. Math.abs()

7.1. Math.abs(x)返回x的绝对(正)值。

7.2. 实例

Math.abs(-4.7);     // 返回 4.7

7.3. 例子

7.3.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>Math.pow()、Math.sqrt()和Math.abs()</title>
	</head>
	<body>
		<script type="text/javascript">
			document.write('Math.pow(8, 2) = ' + Math.pow(8, 2) + '<br />');
			document.write('Math.sqrt(64) = ' + Math.sqrt(64) + '<br />');
			document.write('Math.abs(-4.7) = ' + Math.abs(-4.7) + '<br />');
		</script>
	</body>
</html>

7.3.2. 效果图

8. Math.random()

8.1. Math.random()返回介于0(包括)与1(不包括)之间的随机数。

8.2. Math.random() 总是返回小于1的数。

8.3. 返回介于min(包括)和max(不包括)之间的随机数:

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min) ) + min;
}

8.4. 返回介于min(包括)和max(包括)之间的随机数:

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

8.5. 返回介于min(不包括)和max(包括)之间的随机数:

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min) ) + min + 1;
}

8.6. 例子

8.6.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>Math.random()随机数</title>
	</head>
	<body>
		<script type="text/javascript">
			document.write('Math.random() = ' + Math.random() + '<br /><hr />');
			document.write('<p>返回 0 至 9 之间的数</p>');
			document.write('Math.floor(Math.random() * 10) = ' + Math.floor(Math.random() * 10) + '<br /><hr />');
			document.write('<p>返回 0 至 10 之间的数</p>');
			document.write('Math.floor(Math.random() * 11) = ' + Math.floor(Math.random() * 11) + '<br />');
			document.write('Math.ceil(Math.random() * 10) = ' + Math.ceil(Math.random() * 10) + '<br /><hr />');
			document.write('<p>返回 1 至 10 之间的数</p>');
			document.write('Math.floor(Math.random() * 10) + 1 = ' + (Math.floor(Math.random() * 10) + 1) + '<br /><hr />');
			
			function getRndInteger1(min, max) {
				return Math.floor(Math.random() * (max - min) ) + min;
			}
			function getRndInteger2(min, max) {
				return Math.floor(Math.random() * (max - min + 1) ) + min;
			}
			function getRndInteger3(min, max) {
				return Math.floor(Math.random() * (max - min) ) + min + 1;
			}
			document.write('<p>返回 0 至 99 之间的数</p>');
			document.write('getRndInteger1(0, 100) = ' + getRndInteger1(0, 100) + '<br /><hr />');
			document.write('<p>返回 0 至 100 之间的数</p>');
			document.write('getRndInteger2(0, 100) = ' + getRndInteger2(0, 100) + '<br /><hr />');
			document.write('<p>返回 1 至 100 之间的数</p>');
			document.write('getRndInteger3(0, 100) = ' + getRndInteger3(0, 100) + '<br />');
		</script>
	</body>
</html>

8.6.2. 效果图

9. 三角函数

9.1. 根号2约等于1.414。

9.2. 根号3约等于1.732。

9.3. 弧度值2PI / 360。

9.4. 特殊角的三角函数值表

9.5. Math.sin(x)返回角x(以弧度计)的正弦(介于-1与1之间的值)。

9.6. Math.cos(x)返回角x(以弧度计)的余弦(介于-1与1之间的值)。

9.7. Math.tan(x)方法可返回一个表示某个角的正切的数字。

9.8. 例子

9.8.1. 代码

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>三角函数</title>
	</head>
	<body>
		<script type="text/javascript">
			document.write('Math.sin(0 * Math.PI / 180) = ' + Math.sin(0 * Math.PI / 180) + '<br />');
			document.write('Math.sin(30 * Math.PI / 180) = ' + Math.sin(30 * Math.PI / 180) + '<br />'); 
			document.write('Math.sin(45 * Math.PI / 180) = ' + Math.sin(45 * Math.PI / 180) + '<br />'); 
			document.write('Math.sin(60 * Math.PI / 180) = ' + Math.sin(60 * Math.PI / 180) + '<br />');
			document.write('Math.sin(90 * Math.PI / 180) = ' + Math.sin(90 * Math.PI / 180) + '<br /><hr />');
			
			document.write('Math.cos(0 * Math.PI / 180) = ' + Math.cos(0 * Math.PI / 180) + '<br />');
			document.write('Math.cos(30 * Math.PI / 180) = ' + Math.cos(30 * Math.PI / 180) + '<br />');
			document.write('Math.cos(45 * Math.PI / 180) = ' + Math.cos(45 * Math.PI / 180) + '<br />');
			document.write('Math.cos(60 * Math.PI / 180) = ' + Math.cos(60 * Math.PI / 180) + '<br />');
			document.write('Math.cos(90 * Math.PI / 180) = ' + Math.cos(90 * Math.PI / 180) + '<br /><hr />');
			
			document.write('Math.tan(0 * Math.PI / 180) = ' + Math.tan(0 * Math.PI / 180) + '<br />');
			document.write('Math.tan(30 * Math.PI / 180) = ' + Math.tan(30 * Math.PI / 180) + '<br />');
			document.write('Math.tan(45 * Math.PI / 180) = ' + Math.tan(45 * Math.PI / 180) + '<br />');
			document.write('Math.tan(60 * Math.PI / 180) = ' + Math.tan(60 * Math.PI / 180) + '<br />');
		</script>
	</body>
</html>

9.8.2. 效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值