Re: Q: Portable Ada floating-point binary I/O ?
- From: Gautier <gautier@xxxxxxxxxxxxxxx>
- Date: Sun, 27 Aug 2006 22:34:10 +0200
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!
.
- Follow-Ups:
- Re: Q: Portable Ada floating-point binary I/O ?
- From: Stephen Leake
- Re: Q: Portable Ada floating-point binary I/O ?
- References:
- Q: Portable Ada floating-point binary I/O ?
- From: Gautier
- Re: Q: Portable Ada floating-point binary I/O ?
- From: Dmitry A. Kazakov
- Q: Portable Ada floating-point binary I/O ?
- Prev by Date: Re: Simulating OS semaphore behavior
- Next by Date: Re: Simulating OS semaphore behavior
- Previous by thread: Re: Q: Portable Ada floating-point binary I/O ?
- Next by thread: Re: Q: Portable Ada floating-point binary I/O ?
- Index(es):