comp.lang.ada
 help / color / mirror / Atom feed
From: Phil Thornley <phil.jpthornley@gmail.com>
Subject: Re: Another question about fixed point types.
Date: Sun, 29 Aug 2010 02:02:21 -0700 (PDT)
Date: 2010-08-29T02:02:21-07:00	[thread overview]
Message-ID: <62f49eaf-ed47-47c7-a13a-96ca5750b4e8@y11g2000yqm.googlegroups.com> (raw)
In-Reply-To: 4c7991c8$0$2375$4d3efbfe@news.sover.net

On 28 Aug, 23:45, "Peter C. Chapin" <chap...@acm.org> wrote:
> Hello!
>
> Consider this type definition:
>
>    type Julian_Day is delta 0.001 range 2_415_385.5 .. 2_489_665.0;
>
> Now consider this function that is intended to return the (approximate)
> interval between two Julian days in seconds:
>
>    function Interval_Between
>       (Left : Julian_Day; Right : Julian_Day) return Duration is
>       Result : Duration;
>    begin
>       if Left > Right then
>          Result := 86_400.0 * (Left - Right);
>       else
>          Result := 86_400.0 * (Right - Left);
>       end if;
>       return Result;
>    end Interval_Between;
>
> The difference 'Left - Right' is definitely not in the range of
> Julian_Day. It is likely to be a small value and could even be zero. I
> understand that the computation of 'Left - Right' will be done in
> Julian_Day's base type but I'm unclear what that means to me.
>
> It is my understanding that the compiler is allowed to choose a base
> type that can only deal with the small-ish range of values between the
> limits of Julian_Day. That range does not include zero. However, the
> code above compiles and runs fine using GNAT GPL 2010. My test program
> includes a case where Left and Right are the same so the difference is
> zero. Is this code reliable or am I just seeing the effect of GNAT's
> particular, implementation-specific choices?

As Yannick points out, the base type will be symettric around zero.
(That extra negative value is allowed so that 2's complement machines
don't have work to exclude it.)

With that definition, the largest value that 'Small is allowed to take
is 2**-10, and the upper bound is between 2**21 and 2**22, so the
minimum number of bits for the base type will be 33 (which may or may
not be significant to you).

Assuming that the compiler is allocating 64 bits for the type, it can
choose any 'Small such that the actual range of the base type includes
at least the specified range, so it could be anywhere between
2**-10, with a range -(2**53) .. (2**63 - 1)*2**-10
and
2**-41 with a range -(2**22) .. (2**63 - 1)*2**-41

(and if this is SPARK, there's no base type assertion for fixed point
types...)

Cheers,

Phil



  parent reply	other threads:[~2010-08-29  9:02 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-28 22:45 Another question about fixed point types Peter C. Chapin
2010-08-29  6:32 ` Yannick Duchêne (Hibou57)
2010-08-29 11:23   ` Peter C. Chapin
2010-08-29  9:02 ` Phil Thornley [this message]
2010-08-29 11:29   ` Peter C. Chapin
2010-08-29 12:31     ` Phil Thornley
2010-08-29 13:49       ` Jeffrey Carter
2010-08-29 14:20       ` Peter C. Chapin
2010-08-29 17:50         ` Phil Thornley
2010-08-29 22:03           ` Peter C. Chapin
2010-08-30  8:50           ` Mark Lorenzen
2010-08-29 10:24 ` Simon Wright
2010-08-29 14:02 ` Stephen Leake
replies disabled

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