comp.lang.ada
 help / color / mirror / Atom feed
From: sjw <simon.j.wright@mac.com>
Subject: Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion
Date: Fri, 16 Jan 2009 15:14:19 -0800 (PST)
Date: 2009-01-16T15:14:19-08:00	[thread overview]
Message-ID: <b898b5cd-32ef-4d02-883e-a4a9f5633bf7@z6g2000pre.googlegroups.com> (raw)
In-Reply-To: 497076b5$1_1@glkas0286.greenlnk.net

On Jan 16, 12:18 pm, "Stuart" <stu...@0.0> wrote:
> "ChristopherL" <clusard...@aol.com> wrote in message
>
> news:83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com...
>
> > I'm restarting the tread [How to put 200 into an integer sub-type of
> > 16 bits (code included)] with a new subject because the last was too
> > long!
>
> <snip problem with Unchecked_Conversion>
>
> > I want to copy bits from an Ada float to a user defined short integer
> > maintaining the same bit representation.

(I'm sorry to reply to Stuart's post rather than OP's, but Google
fails to show OP's posts at all! (perhaps there's a reason for that?))

Sorry to shout, Chris, but you CAN NOT FIT THE BITS OF A STANDARD
FLOAT INTO ANYTHING SMALLER!!! IF YOU WANT THE SAME BIT REPRESENTATION
YOU HAVE TO USE THE SAME NUMBER OF BITS!!!

The following is another way of achieving unchecked conversion - the
results on a little-endian machine (i686) are

 2.00000E+02 ->  0  0  72  67
 3.14159E+00 ->  219  15  73  64

with Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;
with System.Storage_Elements;

procedure Foo is
   F : Float;
   A : System.Storage_Elements.Storage_Array (1 .. 4);
   for A'Address use F'Address;
   pragma Import (Ada, A);  -- prevent unwanted auto-initialisations
   function Image (B : System.Storage_Elements.Storage_Element) return
String
     renames System.Storage_Elements.Storage_Element'Image;
begin
   F := 200.0;
   Put_Line (Float'Image (F)
               & " -> "
               & Image (A (1))
               & " "
               & Image (A (2))
               & " "
               & Image (A (3))
               & " "
               & Image (A (4)));
   F := Ada.Numerics.Pi;
   Put_Line (Float'Image (F)
               & " -> "
               & Image (A (1))
               & " "
               & Image (A (2))
               & " "
               & Image (A (3))
               & " "
               & Image (A (4)));
end Foo;





  reply	other threads:[~2009-01-16 23:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com>
2009-01-15 21:22 ` Restarting Tread: Why isn't this program working with Unchecked_Converstion Jeffrey R. Carter
     [not found]   ` <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com>
2009-01-16  0:22     ` Jeffrey R. Carter
2009-01-16  1:37 ` Restarting Tread: Why isn't this program working with anon
     [not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com>
2009-01-16  2:27   ` Restarting Tread: Why isn't this program working with Unchecked_Converstion Adam Beneschan
     [not found]     ` <9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com>
2009-01-16 15:11       ` Ludovic Brenta
2009-01-16 16:23         ` Martin
     [not found]         ` <70172b19-360c-4eba-815c-ede747c3bcdf@w39g2000prb.googlegroups.com>
2009-01-16 17:24           ` Ludovic Brenta
2009-01-16 17:26           ` Martin
2009-01-16 17:34           ` Georg Bauhaus
2009-01-16 12:18 ` Stuart
2009-01-16 23:14   ` sjw [this message]
2009-01-15 21:24 Martin
     [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com>
2009-01-16  0:45   ` tmoran
2009-01-16  8:02   ` Martin
replies disabled

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