Created
December 4, 2016 14:58
-
-
Save butchi/bbae4e27df1f9fabe57173d9784cada9 to your computer and use it in GitHub Desktop.
Mathematicaで浮動小数点数を扱う ref: http://qiita.com/butchi_y/items/3c44e94cdac369509bca
This file contains hidden or 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
In[1]:= ExportString[12345, "Real32"] | |
Out[1]= ä@F |
This file contains hidden or 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
In[1]: real32Digits[Pi] | |
In[2]: real32Format[%] | |
In[3]: fromReal32Digits[%%] | |
In[4]: N[%] | |
Out[1]: {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, | |
1, 1, 1, 0, 1, 1, 0, 1, 1} | |
Out[2]: {1, 1, 13176795/8388608} | |
Out[3]: 3.14159 | |
Out[4]: 3.14159 |
This file contains hidden or 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
In[1]: real32Digits[2^128] | |
In[2]: real32Format[%] | |
In[3]: fromReal32Digits[%%] | |
In[4]: fromReal32DigitsByAbnormal[%%%] | |
Out[1]: {0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | |
Out[2]: {1, 128, 1} | |
Out[3]: Infinity | |
Out[4]: 340282366920938463463374607431768211456 |
This file contains hidden or 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
In[1]: real32Digits[0] | |
In[2]: real32Format[%] | |
In[3]: fromReal32Digits[%%] | |
Out[1]: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | |
Out[2]: {1, -127, 1} | |
Out[3]: 0. |
This file contains hidden or 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
nLi = IntegerDigits[First[ImportString[ExportString[-118.625, "Real32"], "UnsignedInteger32"]], 2, 32] | |
sgnLi = Take[nLi, 1]; (* Sign *) | |
eLi = Take[nLi, {2, 9}]; (* Exp *) | |
sLi = Take[nLi, {10, 32}]; (* Mantissa *) | |
Grid[{Join @@ {Item[#, Background -> LightBlue] & /@ sgnLi, | |
Item[#, Background -> LightGreen] & /@ eLi, | |
Item[#, Background -> LightRed] & /@ sLi}}] |
This file contains hidden or 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
In[1] := real32Digits[-118.625] | |
Out[1] := {1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | |
In[2]:= real32Format[%] | |
Out[2] := {-1, 6, 949/512} | |
In[3] := fromReal32Digits[%%] | |
Out[3] := -118.625 |
This file contains hidden or 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
(* 数値をIEEE 754 の単精度浮動小数点数表現の01列 (長さ32) に変換 *) | |
real32Digits[x_] := | |
IntegerDigits[ | |
First[ImportString[ExportString[x, "Real32"], "UnsignedInteger32"]], | |
2, 32] | |
(* Real32で表された01列から {sgn, e, m} のリストを生成 (特別な数は無視) *) | |
real32Format[li_] := | |
{If[First[li] === 0, 1, -1], | |
FromDigits[Take[li, {2, 9}], 2] - 2^7 + 1, | |
FromDigits[{Join[{1}, Take[li, {10, 32}]], 1}, 2]} | |
(* Real32で表された01列をそれの表す数値に変換 *) | |
fromReal32Digits[li_] := First[ImportString[ExportString[FromDigits[li, 2], "UnsignedInteger32"], "Real32"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment