Math.sqrt() 方法只返回正值。sqrt() 是 Math 的一个静态方法 ,它 可以在不创建对象的情况下使用。
Math.sqrt
JavaScript Math.sqrt()函数是一个内置函数,用于查找任何数字的平方根。任何数字的平方根分别为正数和负数。 sqrt() 是 Math 的静态方法 ,它不需要创建一个对象就可以使用。
要在Javascript中计算一个数字的平方根,请使用Math.sqrt()函数。Math sqrt() 是一个内置的JavaScript方法 。
语法
Math.sqrt(x)
参数(s)
变量x,要确定其平方根值。
返回值
非负的平方根值。
参见下面的方法。
注意
- 如果参数是一个只有一个元素的数组,该方法返回该元素的平方根。
- 如果参数是一个有多个元素的数组,该方法返回NaN。
- 如果传递的值不是一个有效的数字,方法返回NaN。
- 如果传递的是一个负数,方法返回NaN。
- 如果一个参数是一个负的无穷大,那么这个方法返回NaN。
- 如果一个参数是一个正无穷大,那么这个方法返回正无穷大。
- 如果一个参数是正零或负零,那么这个方法返回与传递的值相同。
如果传递的值是 空的,那么该方法返回0。
兼容性(版本及以上)。
- 谷歌浏览器 v1
- 火狐浏览器v1
- Edge v12
- Internet Explorer v3
- Opera v3
- Safari v1
- 安卓webview v1
- Chrome for Android v18
- 安卓版火狐浏览器 v4
- 安卓版Opera v10.1
- Safari on iOS v1
- 三星互联网v1.0
- Node.js
JavaScript版本。 ECMAScript 1
考虑一下下面的例子。
Javascript Math.sqrt()例子
下面的例子演示了sqrt()方法的使用。
// app.js
let a = 4;
let b = 0;
let c = 14;
let d = [9]; // array with single element
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
console.log(Math.sqrt(d));
输出
node app
2
0
3.7416573867739413
3
例子2
下面的例子演示了返回NaN的情况。
// app.js
let a = "Hello, world"; //non-numeric string
let b; //empty variable
let c = -4; //negative number
let d = [1, 2, 3, 4]; //array with more than one elements
let e = {}; //empty object
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
输出结果
node app
NaN
NaN
NaN
NaN
NaN
例三
sqrt()方法不能用于复杂参数,因为只接受整数参数。
// Complex values cannot be passed as arguments as follows
// since only integer arguments are accepted.
console.log(Math.sqrt(2 + i));
输出
node app
/Users/krunal/Desktop/code/node-examples/es/app.js:5
console.log(Math.sqrt(2 + i));
^
ReferenceError: i is not defined
at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:5:27)
at Module._compile (internal/modules/cjs/loader.js:1128:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:983:32)
at Function.Module._load (internal/modules/cjs/loader.js:891:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
例四
下面的例子演示了正无穷大或负无穷大作为参数传递时的情况。
// app.js
var a = Number.NEGATIVE_INFINITY;
var b = Number.POSITIVE_INFINITY;
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
输出
node example4
NaN
Infinity
例五
下面的例子演示了正负零作为参数被传递的情况。
// app.js
let a = 0;
let b = -0;
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
输出
node example5
0
-0
例六
下面的例子演示了返回零的情况。
// app.js
let a = null;
let b = ""; //empty string
let c = []; //empty array
console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
输出结果
node example6
0
0
0
Javascript sqrt()函数的一个真实例子
给出一个直角三角形的两条边,求斜边。
// Given two sides of a right-angled triangle,
// find the hypotenuse.
let side_1;
let side_2;
const r = require('readline');
const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});
const prompt1 = () => {
return new Promise((resolve, reject) => {
rl.question('Side 1: ', (answer) => {
side_1 = answer;
resolve();
});
});
};
const prompt2 = () => {
return new Promise((resolve, reject) => {
rl.question('Side 2: ', (answer) => {
side_2 = answer;
resolve();
});
});
};
const main = async () => {
await prompt1();
await prompt2();
rl.close();
let hypotenuse = Math.sqrt(side_1 * side_1 + side_2 * side_2);
console.log("Hypotenuse: " + hypotenuse);
}
main();
输出
Test Case 1:
->node example7
Side 1: 3
Side 2: 4
Hypotenuse: 5
Test Case 2:
->node example7
Side 1: 15
Side 2: 8
Hypotenuse: 17