Baseline Widely available
Math.hypot()
彿°è¿åææåæ°çå¹³æ¹åçå¹³æ¹æ ¹ï¼å³ï¼
console.log(Math.hypot(3, 4));
// Expected output: 5
console.log(Math.hypot(5, 12));
// Expected output: 13
console.log(Math.hypot(3, 4, 5));
// Expected output: 7.0710678118654755
console.log(Math.hypot(-5));
// Expected output: 5
è¯æ³
Math.hypot([value1[,value2, ...]])åæ° è¿åå¼
å°ææä¾çåæ°æ±å¹³æ¹ååå¼å¹³æ¹æ ¹ã妿æåæ°ä¸è½è½¬æ¢ä¸ºæ°åï¼åè¿å NaN
ã
计ç®ç´è§ä¸è§å½¢çæè¾¹ï¼æå¤æ°çå¹
弿¶å¯ä»¥ä½¿ç¨å½æ° Math.sqrt(v1*v1 + v2*v2)
ï¼å
¶ä¸ v1 å v2 æ¯ä¸è§å½¢ç两个ç´è§è¾¹æå¤æ°çå®é¨åèé¨ã妿æ³è®¡ç®æ´å¤ç»´åº¦ï¼é£ä¹åªéè¦å¨å颿·»å æ´å¤çæ°çå¹³æ¹å°±å¯ä»¥äºï¼æ¯å¦ Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4)
ã
æ¬å½æ°æ¯ Math.sqrt()
æ´ç®å乿´å¿«ï¼ä½ åªéè¦è°ç¨ Math.hypot(v1, v2)
æ Math.hypot(v1, v2, v3, v4, ...)
ã
å®è¿é¿å
äºå¹
å¼è¿å¤§çé®é¢ãJS 䏿大çåç²¾åº¦æµ®ç¹æ°æ¯ Number.MAX_VALUE = 1.797...e+308
ãå¦æä½ çæ°åæ¯çº¦ 1e154 大ï¼è®¡ç®å
¶å¹³æ¹å¼ä¼è¿å Infinityï¼ä½¿ä½ çç»æåºç°é®é¢ãæ¯å¦ï¼Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity
ãå¦æä½ æ¹ç¨ hypot()
彿°ï¼ä½ å¯ä»¥å¾å°æ£ç¡®ççæ¡ï¼Math.hypot(1e200, 1e200) = 1.4142...e+200
ã卿°åé常å°çæ¶ååæ ·å¦æ¤ï¼æ¯å¦ Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0
ï¼ä½ Math.hypot(1e-200, 1e-200) = 1.4142...e-200
忝æ£ç¡®çç»æã
ç±äº hypot
æ¯ Math
çéææ¹æ³ï¼æä»¥åºè¯¥ä»¥ Math.hypot()
çæ¹å¼ä½¿ç¨ï¼è䏿¯ä½ä¸ºä½ å建ç Math
对象ç屿§ï¼Math
䏿¯ä¸ä¸ªæé 彿°ï¼ã
妿ä¸ä¼ å ¥ä»»ä½åæ°ï¼åè¿å +0ã
妿忰åè¡¨ä¸æè³å°ä¸ä¸ªåæ°ä¸è½è¢«è½¬æ¢ä¸ºæ°åï¼åè¿å NaN
ã
妿åªä¼ å
¥ä¸ä¸ªåæ°ï¼Math.hypot(x)
çåäº Math.abs(x)
ã
Math.hypot()
Math.hypot(3, 4); // 5
Math.hypot(3, 4, 5); // 7.0710678118654755
Math.hypot(); // 0
Math.hypot(NaN); // NaN
Math.hypot(3, 4, "foo"); // NaN, +'foo' => NaN
Math.hypot(3, 4, "5"); // 7.0710678118654755, +'5' => 5
Math.hypot(-3); // 3, the same as Math.abs(-3)
åä¸å
¼å®¹
æ¤å½æ°å¯ä»¥ä½¿ç¨å¦ä¸ä»£ç 模æï¼
if (!Math.hypot)
Math.hypot = function () {
var y = 0,
i = arguments.length;
while (i--) y += arguments[i] * arguments[i];
return Math.sqrt(y);
};
å¦ä¸ç§é¿å ç»ææº¢åºçå®ç°ï¼
if (!Math.hypot)
Math.hypot = function (x, y) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=896264#c28
var max = 0;
var s = 0;
for (var i = 0; i < arguments.length; i += 1) {
var arg = Math.abs(Number(arguments[i]));
if (arg > max) {
s *= (max / arg) * (max / arg);
max = arg;
}
s += arg === 0 && max === 0 ? 0 : (arg / max) * (arg / max);
}
return max === 1 / 0 ? 1 / 0 : max * Math.sqrt(s);
};
è§è æµè§å¨å
¼å®¹æ§ åè§
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4