From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c705819cb47d80d9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Sun, 27 Aug 2006 22:34:10 +0200 From: Gautier User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Q: Portable Ada floating-point binary I/O ? References: <44f09ac3$1_3@news.bluewin.ch> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 85.0.157.5 X-Original-NNTP-Posting-Host: 85.0.157.5 Message-ID: <44f201b0$1_6@news.bluewin.ch> X-Trace: news.bluewin.ch 1156710832 85.0.157.5 (27 Aug 2006 22:33:52 +0200) Organization: Bluewin AG Complaints-To: abuse@bluewin.ch X-Original-NNTP-Posting-Host: 127.0.0.1 Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wn14feed!worldnet.att.net!164.128.36.58!news.ip-plus.net!newsfeed.ip-plus.net!news.bluewin.ch!not-for-mail Xref: g2news2.google.com comp.lang.ada:6413 Date: 2006-08-27T22:34:10+02:00 List-Id: 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!