Created
October 24, 2015 19:41
-
-
Save mj-hd/af3644bc19ed28408fc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#module | |
// 2進数の文字列へ変換 | |
// デバッグ用 | |
#defcfunc bit int a, local res | |
res = "" | |
repeat 32 | |
poke res, 31 - cnt, $30 + ((a >> cnt) & 1) | |
loop | |
return res | |
#global | |
#define M_BIT 1 // 符号ビット数 | |
#define E_BIT 11 // 指数部ビット数 | |
#define F_BIT 52 // 仮数部ビット数 | |
#define E_BIAS 1023 // 指数のバイアス | |
// doubleの中身(2進数表記) | |
// 符号 指数部 仮数部 | |
// 0 00000000000 0000000000000000000000000000000000000000000000000000 | |
// | |
// 符号: 0ならば正の値、1ならば負の値。 | |
// 指数部: 2の何乗か。ただし、E_BIASを足した値になる。(例: -10 = 1023 + (-10)) | |
// 仮数部: 実際の値の部分。ただし、正規化済み。 | |
randomize | |
font "MS Gothic", 13 | |
m = 0 // 符号(0:+, 1:-) | |
e = 0 // 指数(2^?) | |
f = rnd(10)+1 // 仮数(書き込みたい値) | |
doubl1 = 0 // double前半32bit | |
doubl2 = 0 // double後半32bit | |
mes strf("書き込む値: 符号(-1)^%d, 指数%d, 仮数:%d", m, e, f) | |
color 100, 100, 100 | |
// 仮数部を正規化 | |
// 小数点を一番上位の1の手前までずらす | |
repeat 32 | |
if ((f << cnt) & (1 << (32 - 1))) { | |
i = cnt | |
break | |
} | |
loop | |
mes "仮数部fを正規化" | |
mes "Before: f=" + bit(f) | |
f = f << (i + 1) | |
mes "After: f=" + bit(f) | |
doubl1 = m << (32 - 1) | |
mes "符号mを書き込み: " + bit(doubl1) + bit(doubl2) | |
doubl1 |= (E_BIAS + e + (31 - i)) << (32 - E_BIT - 1) | |
mes "指数部eを書き込み:" + bit(doubl1) + bit(doubl2) | |
doubl1 |= (f >> (32 - (32 - M_BIT - E_BIT))) & $FFFFF // 算術シフト対策 | |
doubl2 = f << (32 - M_BIT - E_BIT) | |
mes "仮数部fを書き込み:" + bit(doubl1) + bit(doubl2) | |
result = 0.0 | |
// 二回に分けて書き込み | |
lpoke result, 4, doubl1 | |
lpoke result, 0, doubl2 | |
color | |
mes result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment