comp.lang.ada
 help / color / mirror / Atom feed
* Q: Portable Ada floating-point binary I/O ?
@ 2006-08-26 19:02 Gautier
  2006-08-26 20:42 ` Dmitry A. Kazakov
  2006-08-29  2:03 ` Steve
  0 siblings, 2 replies; 6+ messages in thread
From: Gautier @ 2006-08-26 19:02 UTC (permalink / raw)


Hullo!
Does somebody know about a way of doing floating-point I/O which
is portable across architectures (endianesses etc.) and a little
bit more compact than the representation with digits ?
If yes, is there an open-source Ada package doing it ?
TIA, Gautier
______________________________________________________________
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Q: Portable Ada floating-point binary I/O ?
  2006-08-26 19:02 Q: Portable Ada floating-point binary I/O ? Gautier
@ 2006-08-26 20:42 ` Dmitry A. Kazakov
  2006-08-27 20:34   ` Gautier
  2006-08-29  2:03 ` Steve
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2006-08-26 20:42 UTC (permalink / raw)


On Sat, 26 Aug 2006 21:02:47 +0200, Gautier wrote:

> Does somebody know about a way of doing floating-point I/O which
> is portable across architectures (endianesses etc.) and a little
> bit more compact than the representation with digits ?

For network communications we send binary exponent and mantissa as signed
integers and then assemble them using corresponding floating-point
attributes. Integers are sent in a variable length format, which along with
a moderate compression effect, allows us to vary the mantissa length. So it
becomes independent on how many bits the mantissa has on the given host.

However, the problem is - what does "portable" mean here? Range and
precision cannot be portable, unless types aren't communicated as well.

> If yes, is there an open-source Ada package doing it ?

Alas, it isn't. But it is easy to implement.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Q: Portable Ada floating-point binary I/O ?
  2006-08-26 20:42 ` Dmitry A. Kazakov
@ 2006-08-27 20:34   ` Gautier
  2006-08-28 11:55     ` Stephen Leake
  0 siblings, 1 reply; 6+ messages in thread
From: Gautier @ 2006-08-27 20:34 UTC (permalink / raw)


Dmitry A. Kazakov:

 > For network communications we send binary exponent and mantissa as signed
 > integers and then assemble them using corresponding floating-point
 > attributes. Integers are sent in a variable length format, which along with
 > a moderate compression effect, allows us to vary the mantissa length. So it
 > becomes independent on how many bits the mantissa has on the given host.

Excellent, the way of using attributes is _the_ good idea. I should have 
guessed that the Standard defines again everything, in that area too...

 > However, the problem is - what does "portable" mean here? Range and
 > precision cannot be portable, unless types aren't communicated as well.

In my case, it should not be a problem; I have a deterministic file format 
with some items expected as GL.Double, others as GL.Float. I just want to 
ensure that the same file will be correctly read by a PC, a Mac or a Playstation.

 >> If yes, is there an open-source Ada package doing it ?
 >
 > Alas, it isn't. But it is easy to implement.

Seems so. Here is my code (except the test procedure that would be too long 
for here) :
--8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
------------------------------------------------------------------------------
--  File:            Float_portable_binary_transfer.ads
--  Description:     Split & merge floating-point numbers into integers to
--                   facilitate a portable transfer, including Input-Output
--  Date / Version:  27-Aug-2006
--  Author:          G. de Montmollin - public domain code
------------------------------------------------------------------------------
generic
   type Num is digits <>;
   type Mantissa_type is range <>;
   type Exponent_type is range <>;

package Float_portable_binary_transfer is

   procedure Split(f: in Num; m: out Mantissa_type; e: out Exponent_type);

   procedure Merge(m: in Mantissa_type; e: in Exponent_type; f: out Num);

end Float_portable_binary_transfer;

package body Float_portable_binary_transfer is

   -- We rely on Ada's attributes of floating-point types, RM: A.5.3

   procedure Split (f: in Num; m: out Mantissa_type; e: out Exponent_type) is
   begin
     m:= Mantissa_type(Num'Scaling(Num'Fraction(f),Num'Machine_Mantissa));
     e:= Num'Exponent(f);
   end Split;

   procedure Merge (m: in Mantissa_type; e: in Exponent_type; f: out Num) is
   begin
     -- We compose a float with the fraction and the exponent
     f:= Num'Compose(Num'Scaling(Num(m),-Num'Machine_Mantissa), e);
   end Merge;

end Float_portable_binary_transfer;
--8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
Thanks again for the help! Gautier
______________________________________________________________
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Q: Portable Ada floating-point binary I/O ?
  2006-08-27 20:34   ` Gautier
@ 2006-08-28 11:55     ` Stephen Leake
  2006-09-04 22:18       ` Gautier
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2006-08-28 11:55 UTC (permalink / raw)


Gautier <gautier@fakeaddress.nil> writes:

>  > However, the problem is - what does "portable" mean here? Range and
>  > precision cannot be portable, unless types aren't communicated as well.
>
> In my case, it should not be a problem; I have a deterministic file
> format with some items expected as GL.Double, others as GL.Float. I
> just want to ensure that the same file will be correctly read by a PC,
> a Mac or a Playstation.

Those are all IEEE hardware, so all you need is byte-endianness
conversions. See SAL http://stephe-leake.org/ada/sal.html for that, in
particular sal-endianness_gnat_x86.ads, sal-network_order.ads, and
sal-math_float-network_order.ads.

-- 
-- Stephe



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

* Re: Portable Ada floating-point binary I/O ?
  2006-08-26 19:02 Q: Portable Ada floating-point binary I/O ? Gautier
  2006-08-26 20:42 ` Dmitry A. Kazakov
@ 2006-08-29  2:03 ` Steve
  1 sibling, 0 replies; 6+ messages in thread
From: Steve @ 2006-08-29  2:03 UTC (permalink / raw)


"Gautier" <gautier@fakeaddress.nil> wrote in message 
news:44f09ac3$1_3@news.bluewin.ch...
> Hullo!
> Does somebody know about a way of doing floating-point I/O which
> is portable across architectures (endianesses etc.) and a little
> bit more compact than the representation with digits ?
> If yes, is there an open-source Ada package doing it ?
> TIA, Gautier

I would suggest looking up XDR (the relevent one stands for "external data 
representation").
Searching for XDR and Ada comes up with a few hits you might try looking 
into.

I hope this helps,
Steve
(The Duck)

> ______________________________________________________________
> Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm
>
> NB: For a direct answer, e-mail address on the Web site!






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

* Re: Q: Portable Ada floating-point binary I/O ?
  2006-08-28 11:55     ` Stephen Leake
@ 2006-09-04 22:18       ` Gautier
  0 siblings, 0 replies; 6+ messages in thread
From: Gautier @ 2006-09-04 22:18 UTC (permalink / raw)


>>  > However, the problem is - what does "portable" mean here? Range and
>>  > precision cannot be portable, unless types aren't communicated as well.
>>
>> In my case, it should not be a problem; I have a deterministic file
>> format with some items expected as GL.Double, others as GL.Float. I
>> just want to ensure that the same file will be correctly read by a PC,
>> a Mac or a Playstation.

Stephen Leake:

> Those are all IEEE hardware, so all you need is byte-endianness
> conversions. See SAL http://stephe-leake.org/ada/sal.html for that, in
> particular sal-endianness_gnat_x86.ads, sal-network_order.ads, and
> sal-math_float-network_order.ads.

Thanks for the idea - however, for the project in question, I could restrict 
up to now the compiler and machine dependency to the bindings (GL,GLU,GLUT), 
and even there with very small differences. So I opted for a solution with 
attributes (Dmitry) where I split the value into integers (mantissa in 1 or 2 
parts, exponent), then slice them with a given endianess into bytes and merge 
them back for reading.

You can see the result there: http://www.mysunrise.ch/users/gdm/g3d.htm

Gautier
______________________________________________________________
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

end of thread, other threads:[~2006-09-04 22:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-26 19:02 Q: Portable Ada floating-point binary I/O ? Gautier
2006-08-26 20:42 ` Dmitry A. Kazakov
2006-08-27 20:34   ` Gautier
2006-08-28 11:55     ` Stephen Leake
2006-09-04 22:18       ` Gautier
2006-08-29  2:03 ` Steve

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox