[erlang-questions] double to float

Sten Kvamme <>
Thu Oct 18 20:05:45 CEST 2007


Hello Kenneth,

If I use your example I get a badmatch:

{"init terminating in do_boot",{{badmatch,<<80278 bytes>>}, 
[{dxf,ac_double,1},{dxf,find_entities1,1},{dxf,read_dxf,1}, 
{init,start_it,1},{init,start_em,1}]}}

Strangely enough it seems that the byte order for a float in erlang  
is reverse to the byte order for a float in C, OR there is something  
here that I do not understand yet :-)


/Sten Kvamme

On Oct 18, 2007, at 19:48 , Kenneth Lundin wrote:

> Hi Sten,
>
> I don't understand why you have the code as shown in your example  
> when you can
> do it as simple as this:
>
> ac_double(Thefile) ->
>         <<Y/float, B/binary>> = Thefile,
>         {B,Y}.
>
>
> /Kenneth Erlang/OTP team at Ericsson
>
> On 10/18/07, Sten Kvamme <> wrote:
>> Vlad pointed in the right direction, so it's working now (thanks
>> Vlad!). But it still puzzles me that I have to mirror the 8 bytes to
>> have erl accepting them as a float. In C I just do like this:
>> fread(Xrealp, sizeof(Xreal), 1, fpdxf);
>>
>> Here's the erlang code snippet:
>>
>> ac_double(Thefile) ->
>>        <<X:8/binary, B/binary>> = Thefile,
>>    <<X1,X2,X3,X4,X5,X6,X7,X8>> = X,
>>    <<Y/float>> = <<X8,X7,X6,X5,X4,X3,X2,X1>>,
>>        {B,Y}.
>>
>> Here is the whole thing (remember I have just bought the book ;-)
>> http://www.kvamme.se/pub/dxf.erl.txt
>>
>> And here's a small binary dxf file to run on it.
>> http://www.kvamme.se/pub/b.dxf
>>
>> I'm learning fast and it's real fun. I can truly recommend the book.
>>
>> /Sten Kvamme
>>
>> On Oct 16, 2007, at 20:43 , Vlad Dumitrescu wrote:
>>
>>> Hi and welcome,
>>>
>>> On 10/16/07, Sten Kvamme <> wrote:
>>>> I'm reading a binary file with lots of double data types. Have
>>>> someone already written some code to convert the double 8 bytes,
>>>> <<184,162,71,68,75,65,104,64>> to a float?
>>>
>>> Do you mean
>>>    <<X/float>> = <<184,162,71,68,75,65,104,64>>
>>> ?
>>>
>>> regards,
>>> Vlad
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>




More information about the erlang-questions mailing list