【发布时间】:2016-09-21 18:59:39
【问题描述】:
我已经使用 Erlang 在二进制文件中读取了 4 个字节(小端序)。
在尝试将二进制转换为浮点数时,我一直遇到以下错误:
** exception error: bad argument
in function list_to_integer/1
called as list_to_integer([188,159,21,66])
它似乎与浮点模式不匹配,最终改为调用 list_to_integer。如何将我的单精度浮点数转换为原生 Erlang 浮点数?
我的 Erlang 函数如下:
readfloat(S, StartPos) ->
io:format("Pos: ~w~n", [StartPos - 1]),
case file:pread(S, StartPos - 1, 4) of
eof ->
ok;
{ok, Data} ->
% io:format("Wut: ~w~n", Data),
N = binary_to_list(Data),
case string:to_float(N) of
{error,no_float} ->
list_to_integer(N);
{F,_Rest} ->
F
end
% have tried this section as well, error too
% N = binary_to_list(Data),
% try list_to_float(N)
% catch
% error:badarg ->
% list_to_integer(N)
% end
end.
【问题讨论】:
标签: floating-point erlang