comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier <gautier@fakeaddress.nil>
Subject: Re: Q: Portable Ada floating-point binary I/O ?
Date: Sun, 27 Aug 2006 22:34:10 +0200
Date: 2006-08-27T22:34:10+02:00	[thread overview]
Message-ID: <44f201b0$1_6@news.bluewin.ch> (raw)
In-Reply-To: <nae24r4toni2.1tmzdp70veqmf$.dlg@40tude.net>

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!



  reply	other threads:[~2006-08-27 20:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2006-08-28 11:55     ` Stephen Leake
2006-09-04 22:18       ` Gautier
2006-08-29  2:03 ` Steve
replies disabled

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