10 鲿°ã 2 鲿°ãªã©ãã©ã®ãããªåºæ°ãé¸ãã§ããæéã®ãããæ°ãå ããæ°åã¨ã³ã³ã¼ãã£ã³ã°ã·ã¹ãã ã¯ãå¿
ç¶çã«ãã¹ã¦ã®æ°åãæ£ç¢ºã«è¡¨ããã¨ãã§ãã¾ããããªããªããæéã®ã¡ã¢ãªã¼ã使ç¨ãã¦ãæ°ç´ç·ä¸ã®ç¡éã®ç¹ã表ç¾ãããã¨ãããã¨ã«ãªãããã§ããä¾ãã°ã10 鲿³ã§ã¯ 1/3 ãæ£ç¢ºã«è¡¨ãã¾ãããã2 鲿³ã§ã¯ 0.1
ãæ£ç¢ºã«è¡¨ãã¾ããããããã£ã¦ãä¾ãã°ã0.1 + 0.2
㯠0.3
ã¨æ£ç¢ºã«çããã¯ãªãã¾ããã
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
ãã®ãããæµ®åå°æ°ç¹æ°ã§ ===
ã¨æ¯è¼ãã¦ã¯ãããªãã¨ããã¢ããã¤ã¹ãããããããã¾ãããã®ä»£ããã«ãç°ãªã 2 種é¡ã®æ°å¤ãååã«è¿ãå ´åã«ã¯ãçããã¨ã¿ãªããã¨ãã§ãã¾ããEPSILON
ã¯ãè¦ããã« 1
ã¨ããæ°åãã©ã®ç¨åº¦æ£ç¢ºããæå®ãããã®ãªã®ã§ãæ¼ç®çµæã 1
ç¨åº¦ã®å¤§ããã§ããã°ãé常 Number.EPSILON
宿°ãã¨ã©ã¼ã«å¯¾ãã妥å½ãªé¾å¤ã¨ãªãã¾ãã
function equal(x, y) {
return Math.abs(x - y) < Number.EPSILON;
}
const x = 0.2;
const y = 0.3;
const z = 0.1;
console.log(equal(x + z, y)); // true
ããããNumber.EPSILON
ã¯ããã以ä¸ã®å¤§ããã®æ¼ç®å¦çãè¡ãå ´åã«ã¯ä¸é©åã§ãããããã¼ã¿ã 103 ç¨åº¦ã®å¤§ããã§ããã°ãå°æ°ç¹ä»¥ä¸ã®é¨å㯠Number.EPSILON
ããããã£ã¨å°ããªç²¾åº¦ãæã¤ãã¨ã«ãªãã¾ãã
function equal(x, y) {
return Math.abs(x - y) < Number.EPSILON;
}
const x = 1000.1;
const y = 1000.2;
const z = 2000.3;
console.log(x + y); // 2000.3000000000002; error of 10^-13 instead of 10^-16
console.log(equal(x + y, z)); // false
ãã®å ´åããã大ããªå
¬å·®ãå¿
è¦ã¨ãªãã¾ããæ¯è¼ããæ°å¤ã®å¤§ããã¯ããã 2000
ã§ããã®ã§ã2000 * Number.EPSILON
ã®ãããªä¹æ°ã§ããã®ä¾ã§ã¯ååãªè¨±å®¹ç¯å²ãä½ããã¨ãã§ãã¾ãã
function equal(x, y, tolerance = Number.EPSILON) {
return Math.abs(x - y) < tolerance;
}
const x = 1000.1;
const y = 1000.2;
const z = 2000.3;
console.log(equal(x + y, z, 2000 * Number.EPSILON)); // true
大ããã ãã§ãªããå
¥åã®ç²¾åº¦ãèæ
®ãããã¨ãéè¦ã§ããä¾ãã°ãæ°å¤ããã©ã¼ã å
¥åããåéãããå
¥åå¤ã 0.1
ã®ã¹ãããã§ãã調æ´ã§ããªãå ´åï¼ããªãã¡ <input type="number" step="0.1">
ï¼ããã¼ã¿ã¯ 0.1
ã®ç²¾åº¦ãããªãã®ã§é常㯠0.01
ãªã©ãã¯ããã«å¤§ããªè¨±å®¹å·®ãèªãããã¨ãæå³ãã¾ãã
ã¡ã¢: æçãªæè¨ï¼ç夿¤æ»ã®é¾å¤ã¨ãã¦ãåç´ã« Number.EPSILON
ã使ç¨ããªãã§ãã ã ããæ¯è¼ããæ°å¤ã®å¤§ããã精度ã«é©ããé¾å¤ã使ç¨ãã¦ãã ããã
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