2016-07-31 5 views
2

私はこのバイナリをbinary_to_term/1に渡しています。私が知る限り、それは整形式であり、通過するはずですが、それはbadargエラーを引き起こしています。なぜ誰かが私に言うことができますか?ドキュメントで行くなぜこのerlangバイナリはbinary_to_term/1に失敗しますか?

> B = <<131,104,3,100,0,2,111,107,100,0,7,82,69,65,76,83,88,80, 
     108,0,0,0,1,70,127,240,0,0,0,0,7,162,106>>. 

> binary_to_term(B). 
** exception error: bad argument 
    in function binary_to_term/1 
     called as binary_to_term(<<131,104,3,100,0,2,111,107,100,0,7,82,69,65,76,83,88, 
            80,108,0,0,0,1,70,127,240,0,0,...>>) 

、バイナリは次のように打破する必要がありますような何か与える

131 %% encoding version number 
104 3 %% a tuple with three elements 
100 0 2 111 107 %% the atom length 2 'ok' 
100 0 7 82 69 65 76 83 88 80 %% the atom length 7 'REALSXP' 
108 0 0 0 1 %% a list with one element 
70 127 240 0 0 0 0 7 162 %% a float encoded in eight bytes 
106 %% end of list 

{ok, 'REALSXP', [123.456]} 

を完全には明らか思えない唯一のビットです浮く。 8バイトシーケンスは "ビッグエンディアンIEEE形式の8バイト"としてエンコードされたフロートに対して有効ですか?

<<127,240,0,0,0,0,7,162>> 

浮動小数点セクションでなければ、このバイナリに関するその他の情報はbinary_to_termを上回っていますか?おかげで、最高の

イワンを希望

答えて

4

thisを参照)<<127,240,0,0,0,0,7,162>>は、64ビットIEEE浮動小数点でNaN値であるとErlangのは、まったくのNaNと無限大のfloat値の作成をサポートしていないためです。 https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Exponent_encodingから

1> <<Sign:1, Exponent:11, Fraction:52>> = <<127,240,0,0,0,0,7,162>>. 
<<127,240,0,0,0,0,7,162>> 
2> Exponent. 
2047 
3> Fraction. 
1954 
4> 16#7ff. 
2047 

:Mは小数仮数ある

00016 is used to represent a signed zero (if M=0) and subnormals (if M≠0); and 
7ff16 is used to represent ∞ (if M=0) and NaNs (if M≠0),

指数000 と7FF は特別な意味を持っています。

有効な、非NaNにそれらの8つのバイトを変更した場合 /インフィニティ64ビットIEEE浮動小数点は、すべてが期待どおりに動作:

1> binary_to_term(<<131,104,3,100,0,2,111,107,100,0,7,82,69,65,76,83,88,80,108,0,0,0,1,70,0,0,0,0,0,0,0,0,106>>). 
{ok,'REALSXP',[0.0]} 
+0

ブリリアント答えを!どうもありがとう!バイナリはCポートから入ってきて、NaNは完全に適合します。私はC側でそれをキャッチし、送信されたものを変更します。 –

関連する問題