JavaScriptã®å¤§ããªæ°ã¨å°ããªæ°ã®ä»çµã¿ãç解ãã ã IEEE754å ¥é ã
JavaScriptã§ã®æ°å¤ã¯IEEE754ã§è¦å®ããã¦ããå精度ådoubleã§ãã符å·é¨ãï¼ããããä»®æ°é¨ã52ããããææ°é¨ã11ãããã®64ãããã§è¡¨ç¾ãããæµ®åå°æ°ã§ãããã®è¾ºãã¯ãè¨ç®æ©ã®åæ©ã®åæ©ã§ãå½ããåããã¦æ®æ®µã¯æ°ã«ãããã¨ã¯ãªãã¨æãã¾ããããã®å¢çå¤ãç¹æ®ç³»ã«ã¤ãã¦èª¿ã¹ãã¨ããããé¢ç½ããã¨ããããã¾ããããã§ã¯ãJavaScriptãä¾ã«ãã¦ãã¾ãããå 容ã¯ä¸è¬çãªãã®ã§ãIEEE754ã®æµ®åå°æ°å ¥éçãªè©±ã§ãã
ã§ã¯åé¡ã§ãã
æ´æ°ã¨ãã¦æ£ç¢ºã«è¡¨ç¾ã§ããæ大ã®å¤ã¯ããã¤ãï¼
æ£ç¢ºã«è¡¨ç¾ã§ããã¨ããã®ã¯ãn + 1 ã確ãã« n + 1ã«ãªããã¨ã¨ãã¾ããn ãé常ã«å¤§ããã¨ãã«ã¯ãn + 1 ã¯æ¡è½ã¡ãçºçããã®ã§ n ã®ã¾ã¾ã§ããèããåã«å®éã«ãã£ã¦ã¿ã¾ããããæ¢ã n 㯠0 ãã 1e+100 ã®éã«ããã®ã¯æãããªã®ã§ãï¼åæ³ã§æ¢ç´¢ãã¦ã¿ã¾ãã
function search_maxint(lo, hi) { while (hi - lo > 1) { var m = lo + (hi - lo) / 2; if (m == lo || m == hi) break; if (m + 1 == m) { hi = m; } else { lo = m; } } return lo; } var v = serach_maxint(0, 1e+100); alert("v=" + v);
0ãã1e+100ã¨ãã大ããªç¯å²ã§ã®æ¢ç´¢ã§ãããï¼åæ³ãªã®ã§ä¸ç¬ã§æ±ã¾ãã¾ããvã®å¤ã¯ã
v=10295115178936058
ã«ãªãã¾ãããæããã¦ç®çã®æ°å¤ã§ããããï¼
var v1 = 10295115178936058; var v2 = v1 + 1; alert("v2=" + v2);
ä¸æè°ãªãã¨ã«ã
v2=10295115178936060
ã¨ãªãã¾ãããï¼ã足ããã®ã«ï¼å¢ãã¦ãã¾ããã¾ããv2ããï¼ãå¼ãã¦ãv2ã®ã¾ã¾ã§ããçç±ã¯å¾ã§èª¬æãã¾ãã
ã¨ãããã¨ã¯ãä¸ã®ããã°ã©ã ã«ã¯èª¤ãããããn + 1ããçµæããï¼ãå¼ãããå ã«æ»ãã¨ããæ¡ä»¶ãå¿ è¦ãªããã§ãã
function search_maxint(lo, hi) { while (hi - lo > 1) { var m = lo + (hi - lo) / 2; if (m == lo || m == hi) break; var m1 = m + 1; if ((m != m1) && (m1 - 1 == m)) { lo = m; } else { hi = m; } } return lo; } var v = serach_maxint(0, 1e+100); alert("v=" + v);
ä»åº¦ã¯
v=9007199254740991
ã¨ãªãã¾ããã
å®éã«ç¢ºèªãã¦ã¿ãããã
var v1 = 9007199254740991; var v2 = v1 + 1; // v2㯠9007199254740992 var v3 = v2 + 1; // v3ã 9007199254740992 var v4 = v2 - 1; // v4㯠9007199254740991
確ãã«æ±ããæ°ã®ããã§ãããã®æ°å¤ãã¿ã¦ã16é²æ°ãããã«ã¤ã¡ã¼ã¸ã§ãã人ã¯å°ãªãã¨æãã¾ããã16é²æ°ã«ããã¨
0x1FFFFFFFFFFFFF (1F_FFFF_FFFF_FFFF)
ã§ããããã«1ã足ãã 0x20000000000000 ãæ´æ°ã¨ãã¦æ£ç¢ºã«è¡¨ç¾ã§ããæ大ã®æ°ã¨ãããã¨ã«ãªãã¾ãã
ã§ã¯ã次ã¯ã©ãã§ãããï¼
0ãã大ããæå°ã®æ°å¤ã¯ããã¤ãï¼
ãããæ±ããã«ã¯ã0ã¨æ±ããæ°nã®éã«0ã¨nã¨ã¯ç°ãªãæ°ãåå¨ããªããã¨ãããã¨ã§ãããã0ããååã«å¤§ããªæ°ããã¨ãã°ï¼ããååãååã«ã©ãã©ããã¦ããã0ãããèªèº«ã«ãªãã¾ã§ç¶ããã°ãããã¨ã«ãªãã¾ããï¼æ¬å½ã«ããã§ããã®ãã¡ãã£ã¨èªä¿¡ã¯ããã¾ãããï¼
function search_minvalue(hi) { while (hi > 0) { var m = hi / 2; if (m == 0 || m == hi) { break; } else { hi = m; } } return hi; } var v = search_minvalue(1); alert("v=" + v);
å®è¡ããã¨ã
v=5e-324
ã«ãªãã¾ããã
ã¡ãã£ã¨ç¢ºèªãã¦ã¿ã¾ãããã
alert("v=" + v); // v=5e-324 alert("v=" + 1/v); // v=Infinity alert("v=" + 3e-324); // v=5e-324 alert("v=" + 2e-324); // v=0 alert("v=" + 7e-324); // v=5e-324
3e-324ã7e-324ãã©ã¡ãã5e-234ã«ãªãã¾ãããã¯ããªæ°å¤ã¯é£ã³é£ã³ã®å¤ããåãã¾ããããªããéååå¦ã¿ããã§ãã
ãã®5e-324ã¨ããæ°å¤ã¯JavaScriptã§ã¯ Number.MIN_VALUE å®æ°ã¨ãã¦å®ç¾©ããã¦ãã¾ãã
ã§ã¯ãããã«ã次ã¯ã©ãã§ãããï¼
Infinityã§ã¯ãªãæ大ã®æ°å¤ã¯ããã¤ãï¼
ããã¾ã§ã¯ãï¼åæ³ã§ç¯å²ã®ååã®å¤ãå©ç¨ãã¦ãã¾ããããInifinityãããã¨ãã®ååãInfnityãªã®ã§ããã¾ã§ã®æ¹æ³ã¯ä½¿ãã¾ãããInfinityã§ãªãæ°å¤ãããããããã¨Infinityã«è¿ã¥ãã¦ããæ¹æ³ãã¨ãã¾ããqãï¼ã«è¿ãï¼ããå°ããæ°ã¨ãã¦ãã¹ã¿ã¼ãã¨ãªãæ°ã(1+q^0)åãããã«(1+q^0)åãã¦ãããInfinityã«ãªã£ãï¼ã¤åã®æ°å¤ã«ãã©ããããããããã«ä»åº¦ã¯(1+q^1)åã(1+q^1)åãã¦ããåæ§ã«Inifinityã«ãªãã¾ã§ç¹°ãè¿ãã次ã¯(1+q^2)åã(1+q^2)åãã¦ããã¨ããæ¹æ³ãã¨ãã¾ããã¤ã¾ããåæ°ã1+q^n ãã¤ä¸ãã¦ããã¾ãã
ããã§ã¯ãqã0.9ã¨ãã¾ãã
function search_maxvalue(lo) { var r = 1; var q = 0.9; do { var p = 1 + r; var m = lo; do { var hi = m * p; if (isFinite(hi)) { m = hi; } else { lo = m; break; } } while (p > 1); r *= q; } while (p > 1); return lo; } var v = search_maxvalue(1); alert("v=" + v);
å®è¡ããã¨
v=1.7976931348623157e+308
ã«ãªãã¾ãããæã£ãããéãåæãã¾ãã
æ¬å½ã«ãã®æ°å¤ãInfinityã§ã¯ãªãæã大ããªæ°ã§ããããã確èªãã¦ã¿ã¾ãã
alert("v=" + 1.7976931348623157e+308); // v=1.7976931348623157e+308 alert("v=" + 1.79769313486231571e+308); // v=1.7976931348623157e+308 alert("v=" + 1.7976931348623158e+308); // v=1.7976931348623157e+308 alert("v=" + 1.7976931348623156e+308); // v=1.7976931348623155e+308 alert("v=" + 1.7976931348623159e+308); // v=Infinity
確ãã«ããªãã¨ãªãæ£ãããã§ãã
å®ã¯ããã®å¤ã¯JavaScriptã§ã¯ Number.MAX_VALUE ã§å®ç¾©ããã¦ããå®æ°ã¨ä¸è´ãã¾ããä¸ã®è¨ç®ã§ã¯å®éã«ã¯æ¡è½ã¡ã丸ã誤差ãçºçããã®ã§ãqã®å¤ãåæå¤ã«ãã£ã¦ã¯è¥å¹²ç°ãªãçµæã¨ãªãå ´åãããã¾ãã
IEEE754ã®æ°å¤è¡¨ç¾
é·ã ã¨å½ããåã®ãã¨ãæ¸ãã¦ãã¾ãã¾ãããããããã®ãã¨ã¯JavaScriptãæ¡ç¨ãã¦ããæ°å¤è¡¨ç¾ã§ããIEEE754ããå½ç¶ã®å¸°çµã¨ãªãã¾ãã
IEEE754ã®æ°å¤ã®ãããé åã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
1 11 52 +-+-----------+----------------------------------------------------+ |S| Exp | Fraction | +-+-----------+----------------------------------------------------+ 63 62 52 51 0
Sã¯ç¬¦å·ã§ãè² ã®ã¨ã1ããããã»ããããã¾ããExpã¯ææ°é¨ãFractionã¯ä»®æ°é¨ã§ããããã§æ³¨æãå¿ è¦ãªã®ã¯ãææ°é¨ã«ã¯è² ã®æ°ã表ç¾ã§ãã¾ããã符å·ããããç¹ãã¦ãã¾ãã¨64ãããå ¨ä½ãåç´ãªæ°å¤ã¨ã¿ãªããã¨ãã大å°æ¯è¼ãé¢åã«ãªããããææ°é¨ã¯ç¬¦å·ãããã¯ã¤ããªãã§ã1023ããã©ã¹ãã¦ã·ãããããå¤ãã»ããããã¾ãã
ä¾ã¨ãã¦ã123.456 ã¯ã©ã®ããã«è¡¨ç¾ããããã¿ã¦ã¿ã¾ãããã
ãã®ããã«ã¯ï¼é²æ°ã§è¡¨ç¾ããå¿ è¦ãããã¾ããã123ã¯ï¼é²æ°ã§ã¯ 1111011 ã¨ãªããã¨ã¯èª°ã§ãè¨ç®ã§ããã¨æãã¾ããã§ã¯ã0.456 ãï¼é²æ°ã§è¡¨ç¾ããã¨ã©ããªãã§ãããããåããªãã¨ãã人ãæå¤ã«å°ãªãããã¾ããã
æ´æ°é¨åãï¼é²æ°ã«ããã«ã¯ãï¼ã§æ¬¡ã ã«å²ã£ã¦ãããä½ããéã«ä¸¦ã¹ãã°ããã®ã§ãæè¨ç®ã§ããã¨ä¸ã®ããã«ãªãã¾ãã
123 --- 61 ⦠1 30 ⦠1 15 ⦠0 7 ⦠1 3 ⦠1 1 ⦠1 123(2) = 1111011
ä¸æ¹ãå°æ°é¨åã¯æ¬¡ã ã«ï¼åãã¦ãããæ´æ°é¨åãåãåºãã°ãããã¨ã«ãªãã¾ãã
0.456 ----- 0 .912 1 .824 1 .648 1 .296 0 .592 1 .184 0 .368 0 .736 1 .472 0 .944 1 .888 1 .776 1 .552 1 .104 0 .208 0 .416 ...... 0.456(2) = 0.0111010010111100....
ï¼é²æ°ã¯ä»»æã®æ°å¤ã
ã§è¡¨ç¾ãããã¨ã ããå½ããåã¨ããã°å½ããåã§ãã
ãã¡ãããããªæè¨ç®ãããªãã¦ããJavaScriptã使ã£ã¦ãããªãã以ä¸ã®ããã«ç°¡åã«æ±ã¾ãã¾ãã
var v = (123.456).toString(2); alert("v=" + v); // v=1111011.0111010010111100011010100111111011111001110111
ãããæ£è¦åããææ°è¡¨ç¾ã§è¡¨ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
1.1110110111010010111100011010100111111011111001110111Ã2^6
ã¼ã以å¤ã¯æ´æ°é¨åã¯å¿ ãï¼ã«ãªãããã§ãããã52ãããã®ä»®æ°é¨ã«ã¯ãã®1ã¯çç¥ãã¦å°æ°é¨åã®52ããããã»ããããã¾ããã¾ããææ°é¨ã¯1023ãã©ã¹ããã¦ããã®ã§1029ãï¼é²æ°ã«ãã 10000000101 ãã»ããããã¾ãã
ã¤ã¾ãã123.456 ãIEEE754ã§è¡¨ç¾ããã¨ä¸ã®ããã«ãªãã¾ãã
S Exp Fraction +-+-----------+----------------------------------------------------+ |0|10000000101|1110110111010010111100011010100111111011111001110111| +-+-----------+----------------------------------------------------+ 63 62 52 51 0
æ¬å½ã§ããããï¼JavaScriptèªèº«ã§ç¢ºèªãããã¨ããã§ãããJavaScriptã¯ãããæ¼ç®ã32ãããã«éå®ããããã使ãã¾ãããCè¨èªã§ç¢ºèªãã¦ã¿ã¾ããããCè¨èªã¯ä»æ§çã«ã¯IEEE754ã«æºæ ãã¦ããããã§ã¯ããã¾ããããäºå®ä¸å¯¾å¿ãã¦ãã¾ãã
#include <stdio.h> char* getbitstr(double val) { static char buf[67]; uint64_t x = *(uint64_t*)&val; uint64_t flg = 1LL << 63; int j = 0; for (int i = 0; i < 64; i++) { buf[j++] = (x & flg) ? '1' : '0'; if (i == 0 || i == 11) { buf[j++] = '_'; } flg >>= 1; } buf[j] = 0; return buf; } int main() { printf("123.456=%s\n", getbitstr(123.456)); }
å®è¡ããã¨ã
123.456=0_10000000101_1110110111010010111100011010100111111011111001110111
ã¨åºåããã¾ãããã´ã£ããåã£ã¦ãã¾ãã
ã§ã¯æ¬¡ã«ãJavaScriptã®æ大ã®æ°ã§ããNumber.MAX_VALUEã¨ãæå°ã®æ°ã§ããNumber.MIN_VALUEãã©ã®ããã«è¡¨ç¾ããã¦ãããã¿ã¦ã¿ã¾ãããã
ä¸ã®Cã®é¢æ°getbitstr()ã§å®éã«å®è¡ãã¦ã¿ã¾ããçµæãæ¸ãã¨ä¸ã®ããã«ãªãã¾ãã
MIN_VALUE=0_00000000000_0000000000000000000000000000000000000000000000000001 MAX_VALUE=0_11111111110_1111111111111111111111111111111111111111111111111111
MIN_VALUEã®æ¹ã¯ä»®æ°ã®æå¾ã®ããããï¼ã§ããã以å¤ã¯å ¨é¨ã¼ãã«ãªãã¾ãã確ãã«æå°ã®æ°ã®ããã§ããã§ãã¡ãã£ã¨å¾ ã£ã¦ãã ãããä»®æ°é¨ã¯æ´æ°é¨åã®1ãçç¥ããããã®ã§ããããä¸ã®MIN_VALUEã®å¤ã¯
1.0000000000000000000000000000000000000000000000000001 Ã 2^-1023
ã¨ããå¤ã«ãªãçã§ãããããããã㯠1.1125e-308 ä»è¿ã®å¤ã§ãããMIN_VALUE ã®5e-324ã¨ã¯ç°ãªãã¾ããã©ããããã¨ã§ãããï¼å®ã¯ãææ°é¨ãå ¨é¨ã¼ãã®ã¨ãã¯ãéæ£è¦æ°ã¨å¼ã°ããæ°ã®ä¸ç¨®ã§ãããæ´æ°é¨åã®ï¼ãçç¥ãããããæå³ã¯æã¡ã¾ããããªã®ã§ã
0.0000000000000000000000000000000000000000000000000001 Ã 2^-1023 = 1.0 Ã 2^(-1023-51) = 5e-324
ã¨ãªãããã§ãã
ä¸æ¹ãMAX_VALUEã®æ¹ã¯ä»®æ°ã¯å ¨é¨ï¼ããããç«ã£ã¦ãã¾ãããææ°é¨ã®æå¾ã®ãããã¯ã¼ãã§ããå®ã¯ãIEEE754ã§ã¯ææ°é¨ã®ããããå ¨é¨ï¼ã®ã¨ãã¯ç¹æ®ãªæ°ã表ç¾ãã¾ããææ°é¨ãå ¨é¨ï¼ã§ä»®æ°é¨ãå ¨é¨ã¼ãã®æ°ã¯Infinityãæå³ãã¾ããããã«ãä»®æ°é¨ã«ã¼ãã§ãªããã®ãããå ´åã«ã¯NaNãæå³ãã¾ããgetbitstr()é¢æ°ç¢ºèªããã¨ç¢ºãã«ãã®ããã«ãªã£ã¦ãã¾ãã
int main() { double inf = 1.0/0.0; double nan = 0.0/0.0; printf("Inf=%s\n", getbitstr(inf)); printf("NaN=%s\n", getbitstr(nan)); printf("NaN=%s\n", getbitstr(-nan)); } // å®è¡çµæ // Inf=0_11111111111_0000000000000000000000000000000000000000000000000000 // NaN=1_11111111111_1000000000000000000000000000000000000000000000000000 // NaN=0_11111111111_1000000000000000000000000000000000000000000000000000
NaNã«ã符å·ãããã¾ãããæå³ã¯æã¡ã¾ãããã¾ãä»®æ°é¨ã¯ã¼ãã§ãªããã°ããã®ã§ãä»»æã®å¤ãã»ãããããã¨ãã§ããããã»ããµã®è¨ºæãªã©ã«ä½¿ããããã¨ãããããã§ãã
ã¨ããã§ãNumber.MAX_VALUEã®å¤ã¯æ£ç¢ºã«ã¯ããã¤ã«ãªãã§ããããã
MAX_VALUE=0_11111111110_1111111111111111111111111111111111111111111111111111 = 1.1111111111111111111111111111111111111111111111111111 Ã 2^1023 = 11111111111111111111111111111111111111111111111111111 Ã 2^971 = 17976931348623157081452742373170435679807056752584499659891747680315 72607800285387605895586327668781715404589535143824642343213268894641 82768467546703537516986049910576551282076245490090389328944075868508 45513394230458323690322294816580855933212334827479782620414472316873 8177180919299881250404026184124858368
確ãã«ã1.7976931348623157e+308 ã¨ä¸è´ãã¾ãã
æå¾ã«ãæ£ç¢ºã«è¡¨ç¾ã§ããæ大ã®æ´æ°ã¨ãï¼ã足ããã®ã«ä½æ ï¼å¢ããã®ããæ¤è¨¼ãã¦ã¿ã¾ãã
æ大ã®æ´æ°ã¯å°æ°é¨åããªãã¨ãããã¨ã§ããããå°æ°é¨åãå ¨é¨ç¹°ãä¸ããããã«ææ°é¨ãè¨å®ããã¦ããã°ããã®ã§ãä¸ã®ããã«ãªãã¾ãã
1.1111111111111111111111111111111111111111111111111111 Ã 2^52 = 9007199254740991
åã«è¨ç®ã§æ±ããå¤ã¨ä¸è´ãã¾ããã
次ã«ãï¼ã足ããã®ã«ï¼ã¤å¢ããã¨ããã®ã¯ãå°æ°è¨ç®ã®ä¸¸ã誤差ã¨é¡ä¼¼ã®ãã®ã§ãã
åé¡ã®æ° 10295115178936058 ãIEEE754ã§è¡¨ç¾ããã¨
0_10000110100_0010010010011010110100100101100101001100001101111101
ã§ãããææ°é¨ã¯ 53 (1076-1023)ã§ããã¤ã¾ããä»®æ°é¨ã®æå¹æ¡ããï¼ã¤å¤§ããããã10295115178936058ã¯æ£ç¢ºã«ã¯è¡¨ç¾ã§ãã¦ãã¾ãããããããï¼ã¤å¤§ãã10295115178936059ã¯æ¡è½ã¡ãã¦ãã¾ã丸ãããã¾ãããªã®ã§ããã®æ°ä»¥å¤ã«ãææ°é¨ã 53 ã§ä»®æ°é¨ã®æä¸ä½ããããï¼ã®æ°ã¯ï¼ã足ãã¨ï¼å¢ãããã¨ã«ãªãã¾ãã
ã¾ã¨ã
ãããæµ®åå°æ°ç¹æ°ã®æ°å¤è¡¨ç¾ã«ããã®ããã«å¥¥ã®æ·±ã話ãããã¾ããæè¿ã®æ°å ¥ç¤¾å¡ã®è©±ãèãã¨ãåãã¦è¦ããè¨èªã¯Rubyã§ãCè¨èªã¯ç¥ããªãã¨ãã人ãä¸ã«ã¯ããããã§ããCè¨èªã使ãã¨ãã«ã¯ãintãdoubleãªã©æ確ã«åãåºå¥ããæ¡è½ã¡ã丸ã誤差ãªã©ã¯æèããã¨æãã¾ãããJavaScriptã®ãããªåãªãè¨èªã§ãããããæ´æ°ããæµ®åå°æ°ã«å¿ è¦ã«å¿ãã¦èªåçã«å¤ãã£ã¦ãã¾ãè¨èªã§ã¯æãã¬ã¨ããã«è½ã¨ãç©´ãããã¾ããJavaScriptã§ãã®ãããªãã¨ãæ°ã«ããå¿ è¦ãããããã°ã©ã ãæ¸ãæ©ä¼ã¯ãã¾ããªãã¨æãã¾ãããIEEE754ã¯JavaScriptãè¨èªä»æ§ã¨ãã¦æ¡ç¨ãã¦ããè¦æ ¼ã§ãããç¥ã£ã¦ããã¦æã¯ãªãã§ãããã
å®éã®JavaScriptã®å®è£ ã§ã¯ãå ¨ã¦ã®æ°ãIEEE754ã®doubleåæµ®åå°æ°ã§è¨ç®ãã¦ããããã§ã¯ããã¾ãããå¥ã®æ©ä¼ã«ãã©ã¦ã¶æ¯ã«ãã®ãããã確èªãã¦ã¿ãçµæãã¬ãã¼ãã§ããã°ã¨æãã¾ãã