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: a07f3367d7,fa37ee962bc4b00d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!s31g2000vbp.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Conversion from floating point to signed 16 bits Date: Tue, 19 May 2009 13:06:53 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <4a12ffa3$0$2853$ba620e4c@news.skynet.be> NNTP-Posting-Host: 94.108.216.79 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1242763614 6895 127.0.0.1 (19 May 2009 20:06:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 19 May 2009 20:06:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s31g2000vbp.googlegroups.com; posting-host=94.108.216.79; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030810 Iceweasel/3.0.9 (Debian-3.0.9-1),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5949 Date: 2009-05-19T13:06:53-07:00 List-Id: On May 19, 8:51=A0pm, Olivier Scalbert wrote: > Hello, > > My problem: > I need to convert an "analogic" value which can vary from 0.0 to 1.0 > into a "discrete" value which is a signed 16 bits integer. > > My implementation: > ----------------------------- > with Ada.Text_IO; > > procedure convert is > =A0 =A0 =A0type Analog_Value is digits 10 range 0.0 .. 1.0; > =A0 =A0 =A0type Signed_16 =A0 =A0is range -32768 .. 32767; > =A0 =A0 =A0type Unsigned_16 =A0is range =A0 =A0 =A00 .. 65535; > > =A0 =A0 =A0function Cv(Value: Analog_Value) return Signed_16 is > =A0 =A0 =A0 =A0 =A0U16: Unsigned_16; > =A0 =A0 =A0begin > =A0 =A0 =A0 =A0 =A0U16 :=3D Unsigned_16(65535.0 * Value); > =A0 =A0 =A0 =A0 =A0return Signed_16(U16 - 32768); > =A0 =A0 =A0end Cv; > > =A0 =A0 =A0procedure Put(S16: Signed_16) is > =A0 =A0 =A0begin > =A0 =A0 =A0 =A0 =A0Ada.Text_IO.Put_Line(Signed_16'image(S16)); > =A0 =A0 =A0end put; > begin > =A0 =A0 Put(Cv(0.00)); -- Must be -32768 > =A0 =A0 Put(Cv(0.25)); -- Must be -16384 > =A0 =A0 Put(Cv(0.50)); -- Must be =A0 =A0 =A00 > =A0 =A0 Put(Cv(0.75)); -- Must be =A016383 > =A0 =A0 Put(Cv(1.00)); -- Must be =A032767 > end convert; > ----------------------------- > > My question: > Is there an other way to do this in Ada (Representation ? Other parts of > Ada I do not know ?) > > My thanks: > Thank you very much ! > ;-) Your requirements seem a bit strange to me. Could you please explaun why you need the integer to be signed? Ada has direct support for fixed-point types (RM 3.5.9). They would work like this: 0.0 =3D> 2#0000_0000_0000_0000# 0.25 =3D> 2#0100_0000_0000_0000# 0.5 =3D> 2#1000_0000_0000_0000# 0.75 =3D> 2#1100_0000_0000_0000# 1.0-2.0**(-16) =3D> 2#1111_1111_1111_1111# If you interpret the 16 bits as an unsigned integer, you get 0 16384 32768 49152 65535 If you interpret these same 16 bits as a signed integer, due to two's complement you get 0 16384 -32768 -16384 -1 Here is my program using this feature: with Ada.Unchecked_Conversion; with Interfaces; with Ada.Text_IO; procedure Conversion is type Analog_Value is delta 2.0 ** (-16) range 0.0 .. 1.0; -- fixed- point for Analog_Value'Size use 16; function "+" is new Ada.Unchecked_Conversion (Source =3D> Analog_Value, Target =3D> Interfaces.Integer_16); procedure Put (S16 : in Interfaces.Integer_16) is begin Ada.Text_IO.Put_Line (Interfaces.Integer_16'Image (S16)); end Put; begin Put (+0.0); Put (+0.25); Put (+0.5); Put (+0.75); Put (+(1.0 - Analog_Value'Small)); end Conversion; I am aware that my program does not address the exact requirement you expressed but I'm curious to know what the high-level requirement is, i.e. why you need the conversion in your example. -- Ludovic Brenta.