comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Question about ordinary fixed point types.
Date: Sun, 15 Aug 2010 21:11:03 -0700
Date: 2010-08-15T21:11:03-07:00	[thread overview]
Message-ID: <i4adop$2dtr$1@adenine.netfront.net> (raw)
In-Reply-To: <4c688e67$0$2389$4d3efbfe@news.sover.net>

On 08/15/2010 06:03 PM, Peter C. Chapin wrote:
>
> In my first (less informed?) attempt I used Angle_Type'Delta like this:
>
> Angle := Angle + Angle_Type'Delta;
>
> However, the distance between the represented values (the "machine
> numbers") is not Delta, but rather Small. I suppose it wouldn't matter
> much because the resulting value would "snap to" a machine number when
> assigned to Angle. Well, I think.

Consider this example, which I 1st encountered in 1984:

with Ada.Text_IO;

procedure Fixed_Test is
    type Money is delta 0.01 range -100.0 .. 100.0;

    Cent : constant Money := Money'Delta;

    Change : Money := 0.0;
begin -- Fixed_Test
    Sum : for I in 1 .. 5 loop
       Change := Change + Cent;
    end loop Sum;

    Ada.Text_IO.Put_Line (Item => Money'Image (Change) );
end Fixed_Test;

Typically, the 'Small will be 2#0.000_000_1#, and the program will output 0.04. 
The program adds the machine number closest to 'Delta (which is 'Small) each 
time, not 'Delta, so there's no "snapping" taking place, except in the 
conversion of 0.01 to a machine number of type Money.

> I need to do real number computations on a microcontroller without a
> floating point unit. I believe the various numeric quantities that I
> need can be normalized into fairly narrow ranges so it seemed to me that
> this is a perfect application of fixed point types. The compiler stores
> the numbers as integers representing the appropriate multiple of Small.

That is a reason for the existence of fixed-point types.

> I'm not using GNAT on the microcontroller but if the Ada compiler I am
> using does the same thing as GNAT it will be able to fit Angle_Type as
> it is currently defined into a 16 bit word and compute with it using
> integer instructions. In my environment that is essential.

You can always say

for Angle_Value'Size use 16;

-- 
Jeff Carter
"Son of a window-dresser."
Monty Python & the Holy Grail
12

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---



  reply	other threads:[~2010-08-16  4:11 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-15 21:43 Question about ordinary fixed point types Peter C. Chapin
2010-08-15 21:55 ` Shark8
2010-08-16  0:46   ` Peter C. Chapin
2010-08-16  4:13     ` Jeffrey Carter
2010-08-16  8:57       ` Simon Wright
2010-08-15 22:08 ` Yannick Duchêne (Hibou57)
2010-08-16  1:03   ` Peter C. Chapin
2010-08-16  4:11     ` Jeffrey Carter [this message]
2010-08-16 10:03       ` Ludovic Brenta
2010-08-17 14:35         ` sjw
2010-08-17 15:51           ` Ludovic Brenta
2010-08-16 11:31       ` Peter C. Chapin
2010-08-16  9:23     ` Simon Wright
2010-08-16 15:47       ` Simon Wright
2010-08-17 14:37       ` sjw
2010-08-16 11:02 ` Stephen Leake
2010-08-16 11:29   ` Peter C. Chapin
2010-08-16 12:53     ` Dmitry A. Kazakov
2010-08-16 20:28       ` Peter C. Chapin
2010-08-16 20:58         ` Adam Beneschan
2010-08-16 13:01     ` Dmitry A. Kazakov
2010-08-16 14:28     ` Robert A Duff
2010-08-16 20:31       ` Peter C. Chapin
2010-08-16 23:01         ` Robert A Duff
2010-08-16 23:17           ` Peter C. Chapin
2010-08-17  0:56             ` Robert A Duff
2010-08-17  2:03     ` Stephen Leake
2010-08-20  1:21 ` Yannick Duchêne (Hibou57)
replies disabled

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