caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Reading 16 bit floats from stream?
       [not found] <e919163f0610160833t6b04489ev46a2c4dd28860729@mail.gmail.com>
@ 2006-10-16 15:44 ` Maurizio Colucci
  2006-10-16 16:03   ` Maurizio Colucci
  0 siblings, 1 reply; 10+ messages in thread
From: Maurizio Colucci @ 2006-10-16 15:44 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 212 bytes --]

Hi,

I need to read a sequence of single precision floats (16 bits) from a binary
file. How can I do that?

In case you are curious, I am parsing an MD2 file for a game. :-)

Thanks a lot for any help,

Maurizio

[-- Attachment #2: Type: text/html, Size: 310 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Reading 16 bit floats from stream?
  2006-10-16 15:44 ` Reading 16 bit floats from stream? Maurizio Colucci
@ 2006-10-16 16:03   ` Maurizio Colucci
  2006-10-16 16:27     ` [Caml-list] " Gerd Stolpmann
  0 siblings, 1 reply; 10+ messages in thread
From: Maurizio Colucci @ 2006-10-16 16:03 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 256 bytes --]

On 10/16/06, Maurizio Colucci <maurizio.colucci@gmail.com> wrote:
>
> Hi,
>
> I need to read a sequence of single precision floats (16 bits) from a
> binary file. How can I do that?
>

Sorry, I mean 32 bits. Single precision floats are 32 bits.


Maurizio

[-- Attachment #2: Type: text/html, Size: 648 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 16:03   ` Maurizio Colucci
@ 2006-10-16 16:27     ` Gerd Stolpmann
  2006-10-16 17:06       ` Maurizio Colucci
  0 siblings, 1 reply; 10+ messages in thread
From: Gerd Stolpmann @ 2006-10-16 16:27 UTC (permalink / raw)
  To: Maurizio Colucci; +Cc: caml-list

Am Montag, den 16.10.2006, 18:03 +0200 schrieb Maurizio Colucci:
> 
> 
> On 10/16/06, Maurizio Colucci <maurizio.colucci@gmail.com> wrote:
>         Hi,
>         
>         I need to read a sequence of single precision floats (16 bits)
>         from a binary file. How can I do that? 
> 
> Sorry, I mean 32 bits. Single precision floats are 32 bits.

Well, have heard of even 8 bit floats...

32 bit floats can be read. First read the 4 bytes and make an int32 of
them. Then simply call Int32.float_of_bits.

Gerd

-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 16:27     ` [Caml-list] " Gerd Stolpmann
@ 2006-10-16 17:06       ` Maurizio Colucci
  2006-10-16 17:23         ` Tom
  2006-10-16 20:19         ` Martin Jambon
  0 siblings, 2 replies; 10+ messages in thread
From: Maurizio Colucci @ 2006-10-16 17:06 UTC (permalink / raw)
  Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 758 bytes --]

On 10/16/06, Gerd Stolpmann <info@gerd-stolpmann.de> wrote:
>
> Am Montag, den 16.10.2006, 18:03 +0200 schrieb Maurizio Colucci:
> >
> >
> > On 10/16/06, Maurizio Colucci <maurizio.colucci@gmail.com> wrote:
> >         Hi,
> >
> >         I need to read a sequence of single precision floats (16 bits)
> >         from a binary file. How can I do that?
> >
> > Sorry, I mean 32 bits. Single precision floats are 32 bits.
>
> Well, have heard of even 8 bit floats...
>
> 32 bit floats can be read. First read the 4 bytes


I assume you mean four read_bytes calls...

and make an int32 of
> them.


How do I create an int32 with four bytes?

Then simply call Int32.float_of_bits.


I get this last one, but I can't imagine the previous step.

Thanks

Maurizio

[-- Attachment #2: Type: text/html, Size: 1606 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 17:06       ` Maurizio Colucci
@ 2006-10-16 17:23         ` Tom
  2006-10-16 20:19         ` Martin Jambon
  1 sibling, 0 replies; 10+ messages in thread
From: Tom @ 2006-10-16 17:23 UTC (permalink / raw)
  To: Maurizio Colucci; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 51 bytes --]

What about one read_int call? I guess it exists...

[-- Attachment #2: Type: text/html, Size: 55 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 17:06       ` Maurizio Colucci
  2006-10-16 17:23         ` Tom
@ 2006-10-16 20:19         ` Martin Jambon
  2006-10-16 20:35           ` Tom
  1 sibling, 1 reply; 10+ messages in thread
From: Martin Jambon @ 2006-10-16 20:19 UTC (permalink / raw)
  To: Maurizio Colucci; +Cc: caml-list

On Mon, 16 Oct 2006, Maurizio Colucci wrote:

> I assume you mean four read_bytes calls...
>
> and make an int32 of
>> them.
>
>
> How do I create an int32 with four bytes?

I can't recall any predefined function. You can read the bytes one by 
one, and use Int32.shift_left and Int32.logor to create the int32 value.

> Then simply call Int32.float_of_bits.
>
>
> I get this last one, but I can't imagine the previous step.
>
> Thanks
>
> Maurizio
>

--
Martin Jambon, PhD
http://martin.jambon.free.fr


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 20:19         ` Martin Jambon
@ 2006-10-16 20:35           ` Tom
  2006-10-16 23:25             ` Jacques Garrigue
  0 siblings, 1 reply; 10+ messages in thread
From: Tom @ 2006-10-16 20:35 UTC (permalink / raw)
  To: Martin Jambon; +Cc: Maurizio Colucci, caml-list

[-- Attachment #1: Type: text/plain, Size: 151 bytes --]

val Pervasives.input_binary_int : in_channel -> int
val Pervasives.read_int : unit -> int

val Marshal.from_channel : in_channel -> 'a

For example...

[-- Attachment #2: Type: text/html, Size: 542 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 20:35           ` Tom
@ 2006-10-16 23:25             ` Jacques Garrigue
  2006-10-17 14:02               ` Maurizio Colucci
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Garrigue @ 2006-10-16 23:25 UTC (permalink / raw)
  To: tom.primozic; +Cc: caml-list

From: Tom <tom.primozic@gmail.com>

> val Pervasives.input_binary_int : in_channel -> int

This will indeed read a 32-bit integer, but on a 32-bit architecture
ocaml int's are 31-bit, so you loose the highest bit.
Note that on a 64-bit architecture this is sufficient for 32-bit
integers!

> val Pervasives.read_int : unit -> int

This one reads a string...

> val Marshal.from_channel : in_channel -> 'a

And this one reads a marshalled value, which must include a special
header indicating its ocaml representation.

So if you really need to read a full 32-bit integer, none of these
will work, and you must do the job by hand.

Jacques Garrigue


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-16 23:25             ` Jacques Garrigue
@ 2006-10-17 14:02               ` Maurizio Colucci
  2006-10-17 14:32                 ` Gerd Stolpmann
  0 siblings, 1 reply; 10+ messages in thread
From: Maurizio Colucci @ 2006-10-17 14:02 UTC (permalink / raw)
  Cc: tom.primozic, caml-list

[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]

On 10/17/06, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
>
> From: Tom <tom.primozic@gmail.com>
>
> > val Pervasives.input_binary_int : in_channel -> int
>
> This will indeed read a 32-bit integer, but on a 32-bit architecture
> ocaml int's are 31-bit, so you loose the highest bit.
> Note that on a 64-bit architecture this is sufficient for 32-bit
> integers!
>
> > val Pervasives.read_int : unit -> int
>
> This one reads a string...
>
> > val Marshal.from_channel : in_channel -> 'a
>
> And this one reads a marshalled value, which must include a special
> header indicating its ocaml representation.
>
> So if you really need to read a full 32-bit integer, none of these
> will work, and you must do the job by hand.
>
> Jacques Garrigue


Thanks to everybody. I found a library called extlib which provides a module
called IO. I solved the problem with

let l =  Int32.float_of_bits (IO.read_real_i32 input)

BTW, I am a bit surprised the standard library does not offer a way to do
that. A simple task can become frustrating.

Bye

Maurizio

[-- Attachment #2: Type: text/html, Size: 1479 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: Reading 16 bit floats from stream?
  2006-10-17 14:02               ` Maurizio Colucci
@ 2006-10-17 14:32                 ` Gerd Stolpmann
  0 siblings, 0 replies; 10+ messages in thread
From: Gerd Stolpmann @ 2006-10-17 14:32 UTC (permalink / raw)
  To: Maurizio Colucci; +Cc: caml-list

Am Dienstag, den 17.10.2006, 16:02 +0200 schrieb Maurizio Colucci:

> BTW, I am a bit surprised the standard library does not offer a way to
> do that. A simple task can become frustrating.

I must say that I do not see the point. Although O'Caml is for
high-level tasks it is unavoidable that you must sometimes look at the
details and understand what's going on under the hood. This is more a
matter of the programmer's general knowledge, and not about how good or
bad a certain library is (which includes what _other_ people consider as
important).

Just to scare you. Before release 3.08 there was no Int32.float_of_bits
in O'Caml, and one had to program such a function with bit shifting
primitives. See how ugly this is (look for the now commented-out
function fp8_of_fp4):

https://gps.dynxs.de/svn/lib-ocamlnet2/trunk/code/src/rpc/rtypes.ml

Gerd

-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2006-10-17 14:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <e919163f0610160833t6b04489ev46a2c4dd28860729@mail.gmail.com>
2006-10-16 15:44 ` Reading 16 bit floats from stream? Maurizio Colucci
2006-10-16 16:03   ` Maurizio Colucci
2006-10-16 16:27     ` [Caml-list] " Gerd Stolpmann
2006-10-16 17:06       ` Maurizio Colucci
2006-10-16 17:23         ` Tom
2006-10-16 20:19         ` Martin Jambon
2006-10-16 20:35           ` Tom
2006-10-16 23:25             ` Jacques Garrigue
2006-10-17 14:02               ` Maurizio Colucci
2006-10-17 14:32                 ` Gerd Stolpmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).