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!d19g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Conversion from floating point to signed 16 bits Date: Tue, 19 May 2009 12:37:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5d240393-452e-46d8-bc55-e46901bb1d6f@d19g2000prh.googlegroups.com> References: <4a12ffa3$0$2853$ba620e4c@news.skynet.be> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1242761866 15243 127.0.0.1 (19 May 2009 19:37:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 19 May 2009 19:37:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d19g2000prh.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5946 Date: 2009-05-19T12:37:46-07:00 List-Id: On May 19, 11:51=A0am, 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 ?) If your problem is that you're getting a Constraint_Error when the subtraction "U16 - 32768" has a negative result, you can convert U16 to some larger integer type before doing the subtraction: return Signed_16(Signed_32(U16) - 32768); -- Adam