From: "Stuart" <stuart@0.0>
Subject: Re: Conversion from floating point to signed 16 bits
Date: Wed, 20 May 2009 09:48:34 +0100
Date: 2009-05-20T09:48:34+01:00 [thread overview]
Message-ID: <4a13bf49$1_1@glkas0286.greenlnk.net> (raw)
In-Reply-To: 4a13baef$1_1@glkas0286.greenlnk.net
"Stuart" <stuart@0.0> wrote in message
news:4a13baef$1_1@glkas0286.greenlnk.net...
> "Olivier Scalbert" <olivier.scalbert@algosyn.com> wrote in message
> news:4a12ffa3$0$2853$ba620e4c@news.skynet.be...
>> 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.
...
>> with Ada.Text_IO;
>>
>> procedure convert is
>> type Analog_Value is digits 10 range 0.0 .. 1.0;
>> type Signed_16 is range -32768 .. 32767;
...
>> begin
...
>> Put(Cv(0.50)); -- Must be 0
...
>> end convert;
> Extending your [reasonable] assumptions about the base type of
> Analog_Value you could dispense with the Unsigned_16 altogether:
>
> function Cv(Value: Analog_Value) return Signed_16 is
> begin
> return Signed_16(65535.0*Value - 32768.0);
> end Cv;
Ooops!
Unfortunately, due to Ada rounding rules this fails your requirement for
Cv(0.50) as it returns -1.
In my defence I plead that we got very annoyed by the overhead of Ada
rounding in a similar scenario that we rigged our conversions and don't see
this problem. You can might also weedle your way around the effect of the
rounding by trying something like:
function Cv(Value: Analog_Value) return Signed_16 is
begin
return Signed_16(65535.0*Value - 32767.9999);
end Cv;
You could probably _derive_ a value to replace -32768.0 by consideration of
the rounding effect over the range of inputs 0.0 .. 1.0.
Regards
Stuart
next prev parent reply other threads:[~2009-05-20 8:48 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-19 18:51 Conversion from floating point to signed 16 bits Olivier Scalbert
2009-05-19 19:37 ` Adam Beneschan
2009-05-19 19:57 ` Jeffrey R. Carter
2009-05-19 22:36 ` Adam Beneschan
2009-05-19 19:55 ` Jeffrey R. Carter
2009-05-19 20:06 ` Ludovic Brenta
2009-05-19 22:08 ` Olivier Scalbert
2009-05-20 8:29 ` Stuart
2009-05-20 8:48 ` Stuart [this message]
2009-05-20 11:11 ` Georg Bauhaus
2009-05-20 12:06 ` Martin
2009-05-20 15:18 ` Jeffrey R. Carter
2009-05-20 15:23 ` Martin
2009-05-20 15:47 ` Adam Beneschan
2009-05-20 16:13 ` 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