JavaScript math
JavaScript的Math对象允许你对数字进行数学操作. Math.PI; // returns 3.141592653589793 让我试试 Math.round() Math.round(x) 返回x舍入到其最近整数的值: Math.round(4.7); // returns 5 Math.round(4.4); // returns 4 让我试试 Math.pow() Math.pow(x, y) x的y次方: Math.pow(8, 2); // returns 64 让我试试 Math.sqrt() Math.sqrt(x) x的平方根: Math.sqrt(64); // returns 8 让我试试 Math.abs() Math.abs(x) x的绝对值: Math.abs(-4.7); // returns 4.7 让我试试 Math.ceil() Math.ceil(x) 返回x向上舍入到其最近整数的值: Math.ceil(4.4); // returns 5 让我试试 Math.floor() Math.floor(x) 返回x向下舍入到其最近整数的值:: Math.floor(4.7); // returns 4 让我试试 Math.sin() Math.sin(x) 返回角度x的正弦值(-1到1之间)(以弧度). 如果你想使用角度而不是弧度,你必须转换为弧度: Angle in radians = Angle in degrees x PI / 180. Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees) 让我试试 Math.cos() Math.cos(x) 返回x的余弦值(-1到1之间)(以弧度). 如果你想使用角度而不是弧度,你必须转换为弧度:: Angle in radians = Angle in degrees x PI / 180. Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees) 让我试试 Math.min() 和 Math.max() Math.min() 和 Math.max() 可用于在参数列表中查找最低或最高值: Math.min(0, 150,
JavaScript的Math对象允许你对数字进行数学操作.
Math.PI; // returns 3.141592653589793
Math.round()
Math.round(x) 返回x舍入到其最近整数的值:
Math.round(4.7); // returns 5 Math.round(4.4); // returns 4
Math.pow()
Math.pow(x, y) x的y次方:
Math.pow(8, 2); // returns 64
Math.sqrt()
Math.sqrt(x) x的平方根:
Math.sqrt(64); // returns 8
Math.abs()
Math.abs(x) x的绝对值:
Math.abs(-4.7); // returns 4.7
Math.ceil()
Math.ceil(x) 返回x向上舍入到其最近整数的值:
Math.ceil(4.4); // returns 5
Math.floor()
Math.floor(x) 返回x向下舍入到其最近整数的值::
Math.floor(4.7); // returns 4
Math.sin()
Math.sin(x) 返回角度x的正弦值(-1到1之间)(以弧度).
如果你想使用角度而不是弧度,你必须转换为弧度:
Angle in radians = Angle in degrees x PI / 180.
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x) 返回x的余弦值(-1到1之间)(以弧度).
如果你想使用角度而不是弧度,你必须转换为弧度::
Angle in radians = Angle in degrees x PI / 180.
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() 和 Math.max()
Math.min() 和 Math.max() 可用于在参数列表中查找最低或最高值:
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.random()
Math.random() 返回一个随机数0(包含)和1(不包含)之间:
Math.random(); // returns a random number
在教程的下一章,你将学会更多有关Math.random()的内容.
Math 属性 (常量)
JavaScript 提供8个可以被Math对象访问的数学常数:
Math.E // returns Euler's number Math.PI // returns PI Math.SQRT2 // returns the square root of 2 Math.SQRT1_2 // returns the square root of 1/2 Math.LN2 // returns the natural logarithm of 2 Math.LN10 // returns the natural logarithm of 10 Math.LOG2E // returns base 2 logarithm of E Math.LOG10E // returns base 10 logarithm of E
Math 构造函数
与其他全局对象不同,Math对象没有构造函数。方法和属性是静态的.
所有方法和属性(常量)都可以使用,而不必首先创建一个数学对象.
Math 对象方法
| 方法 | 描述 |
|---|---|
| abs(x) | 返回x的绝对值 |
| acos(x) | 返回x的反余弦,弧度 |
| asin(x) | 返回x的反正弦,弧度 |
| atan(x) | 返回x的反正切值-π/ 2和π/ 2弧度之间 |
| atan2(y, x) | 返回参数的反正切的商 |
| ceil(x) | Returns the value of x rounded up to its nearest integer |
| cos(x) | 返回余弦值 |
| exp(x) | Returns the value of Ex |
| floor(x) | 返回x向下舍入到其最近整数的值 |
| log(x) | 返回x的自然对数(基于e) |
| max(x, y, z, ..., n) | 返回值最高的数字 |
| min(x, y, z, ..., n) | 返回值最低的数字 |
| pow(x, y) | 返回x的y次方 |
| random() | 返回0到1之间的随机数 |
| round(x) | 返回x舍入到其最近整数的值 |
| sin(x) | 返回正弦值 |
| sqrt(x) | 返回x的平方根 |
| tan(x) | 返回正切值 |
完整 Math 参考
完整Math参考, 访问完整Math参考.
参考包含所有数学属性和方法的说明和示例.
交流与评论