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,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!t39g2000prh.googlegroups.com!not-for-mail From: Martin Newsgroups: comp.lang.ada Subject: Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) Date: Thu, 15 Jan 2009 10:15:50 -0800 (PST) Organization: http://groups.google.com Message-ID: <3ef03f1f-4b13-4e02-b57f-637f0be4358f@t39g2000prh.googlegroups.com> References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <7b017de2-951a-414a-8290-111353fe02f8@r15g2000prd.googlegroups.com> <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com> <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com> <9fa07f9b-90bf-4a28-bf8e-09c2152bd43f@o40g2000prn.googlegroups.com> NNTP-Posting-Host: 81.156.241.243 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1232043351 12050 127.0.0.1 (15 Jan 2009 18:15:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 15 Jan 2009 18:15:51 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: t39g2000prh.googlegroups.com; posting-host=81.156.241.243; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:4314 Date: 2009-01-15T10:15:50-08:00 List-Id: On Jan 15, 5:21=A0pm, ChristopherL wrote: > On Jan 15, 9:09=A0am, christoph.gr...@eurocopter.com wrote: > > > On 15 Jan., 17:46, ChristopherL wrote: > > > > Can some the Ada intelligent person modify this program to make it > > > correctly > > > put the value in variable "Arg" into the variable "Result". > > > Can anybody please help me to seat 200 people in a bus with 127 seats. > > But, isn't 2^10 greater than 200! So, is there some way to put 200.0 > into the variable "Result". > > Ideally, if I achieve my ultimate goal I would like to later look at > "Result" and be able to reconstruct "Arg" (200.0) from what is in > "Result". > > Chris L. 2^10 is but that's not what you asked for!!!! At least not the 1st post I saw from you!!! E.g. from your 'test' procedure: subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1); That's the range -128 to 127. "200.0" isn't in that range =3D> exception. But this new 'requirement' to be able to go between a float and a 10- bit integer representation without loosing information is just pie in the sky! Float value =3D> 10.12312 As 10-bit int =3D> 10 Back to float =3D> 10.00000 That why computer have different data types and why programming languages have different types to make use of them. Or are you saying that all the floating point values will have ".0" as their fractional part? Is this any closer to what you want? with Ada.Text_IO; use Ada.Text_IO; procedure Test is type UInt10 is mod 2 ** 10; for UInt10'Size use 10; type Float32 is new Float; pragma Assert (Float'Size =3D 32); function To_Uint10 (F : Float32) return UInt10 is begin pragma Assert (F in 0.0 .. 200.0); return UInt10 (F); end To_Uint10; function To_Float32 (U : UInt10) return Float32 is begin return Float32 (U); end To_Float32; procedure Test_Value (Arg : Float32) is package Float32_IO is new Float_IO (Float32); package UInt10_IO is new Modular_IO (UInt10); Result : constant UInt10 :=3D To_Uint10 (Arg); Back_Again : constant Float32 :=3D To_Float32 (Result); begin Put ("Arg =3D"); Float32_IO.Put (Arg, Fore =3D> 4, Aft =3D> 3); New_Line; Put ("Result =3D"); UInt10_IO.Put (Result, Width =3D> 4); New_Line; Put ("Back_Again =3D"); Float32_IO.Put (Back_Again, Fore =3D> 4, Aft =3D> 3); New_Line; end Test_Value; begin Put_Line ("UInt10'Size =3D" & Integer'Image (UInt10'Size)); Put_Line ("Float32'Size =3D" & Integer'Image (Float32'Size)); Test_Value (200.0); Test_Value (200.5); end Test; C:\Ada\test\lib\test UInt10'Size =3D 10 Float32'Size =3D 32 Arg =3D 2.000E+02 Result =3D 200 Back_Again =3D 2.000E+02 Arg =3D 2.005E+02 Result =3D 201 Back_Again =3D 2.010E+02 Cheers -- Martin