comp.lang.ada
 help / color / mirror / Atom feed
* Conversion from floating point to signed 16 bits
@ 2009-05-19 18:51 Olivier Scalbert
  2009-05-19 19:37 ` Adam Beneschan
                   ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Olivier Scalbert @ 2009-05-19 18:51 UTC (permalink / raw)


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
     type Analog_Value is digits 10 range 0.0 .. 1.0;
     type Signed_16    is range -32768 .. 32767;
     type Unsigned_16  is range      0 .. 65535;

     function Cv(Value: Analog_Value) return Signed_16 is
         U16: Unsigned_16;
     begin
         U16 := Unsigned_16(65535.0 * Value);
         return Signed_16(U16 - 32768);
     end Cv;

     procedure Put(S16: Signed_16) is
     begin
         Ada.Text_IO.Put_Line(Signed_16'image(S16));
     end put;
begin
    Put(Cv(0.00)); -- Must be -32768
    Put(Cv(0.25)); -- Must be -16384
    Put(Cv(0.50)); -- Must be      0
    Put(Cv(0.75)); -- Must be  16383
    Put(Cv(1.00)); -- Must be  32767
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 !
;-)

Olivier.




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2009-05-20 16:13 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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