From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Fixed point constants issue
Date: Mon, 13 Sep 2010 22:05:58 +0300
Date: 2010-09-13T22:05:58+03:00 [thread overview]
Message-ID: <8f7b0nF6hfU1@mid.individual.net> (raw)
In-Reply-To: <op.vizhg5ygd20q5n@jellix.jlfencey.com>
Vinzent Hoefler wrote:
> On Mon, 13 Sep 2010 20:04:32 +0200, Dmitry A. Kazakov
> <mailbox@dmitry-kazakov.de> wrote:
>
>> On Mon, 13 Sep 2010 19:27:30 +0200, Vinzent Hoefler wrote:
>>
>>> Is this a GNAT bug or am I just stupid?
>>>
>>> Suppose we have a fixed point type (with a rather large delta):
>>>
>>> -- 8< --
>>> with Ada.Text_IO; use Ada.Text_IO;
>>>
>>> procedure Fixed_Point
>>> is
>>> FEET_PER_METER : constant := 0.3048;
>>> HEIGHT : constant := 10; -- Feet
>>>
>>> type Altitude is delta (2.0**15 / 50_000.0) range 0.0 .. 50_000.0;
>>
>> [...]
>>> Can anyone shed some light on this? This has driven us crazy for
>>> weeks now.
>>>
>>> [1] Of course, "Altitude'(HEIGHT * FEET_PER_METER)" yields "0.0" again.
>>> At least this is consistent with my understanding so far.
>>
>> Hmm, what did you expect? 0.3048 (FEET_PER_METER) is 0 when Altitude.
>> That is because 2.0**15/50_000.0=0.65536 > 0.3048.
>
> Yes, of course. Still, it's not quite intuitive why
>
> TEN_FEET_1 : constant := HEIGHT * FEET_PER_METER;
> TEN_FEET_2 : constant Altitude := HEIGHT * FEET_PER_METER;
>
> just because a type is given in the second case.
I think these cases are clear from the language rules:
The declaration of TEN_FEET_1 is a named-number declaration, so the
expression is evaluated using the "root" multiplication operator "*"
(Left : root_integer; Right : root_real) return root_real, which is done
at compile-time and exactly (with unlimited precision).
> I would expect the compiler to evaluate the expression
> "HEIGHT * FEET_PER_METER" [for TEN_FEET_2] _before_
> converting it to the appropriate fixed point type.
The declaration of TEN_FEET_2 is an object declaration with type
Altitude, therefore the right-hand-side expression is expected to be of
type Altitude and so the "*" will be resolved to the Altitude
multiplication operator "*" (Left, Right : Altitude) return Altitude,
with result zero.
I agree that this is a bit nasty and counter-intuitive. Perhaps the
compiler should warn that the conversion of the universal_real number
0.3048 to Altitude is seriously inexact, but then, what is "seriously"?
> And, obviously it does this that when evaluating
>
> TEN_FEET_4 : constant Altitude := Altitude (HEIGHT * FEET_PER_METER);
Here the right-hand side expression is a type conversion, in fact a
value conversion, and so the operand HEIGHT * FEET_PER_METER is expected
to be of any numeric type. At first sight the multiplication could be
resolved to the root "*" or to the Altitude "*", which would thus be
ambiguous. However, RM 8.6(29) says that the ambiguity is resolved by
using the root "*", giving the more precise result.
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
next prev parent reply other threads:[~2010-09-13 19:05 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-13 17:27 Fixed point constants issue Vinzent Hoefler
2010-09-13 18:04 ` Dmitry A. Kazakov
2010-09-13 18:25 ` Vinzent Hoefler
2010-09-13 19:05 ` Niklas Holsti [this message]
2010-09-13 20:35 ` Vinzent Hoefler
2010-09-13 20:35 ` Jeffrey Carter
2010-09-13 21:06 ` Vinzent Hoefler
2010-09-14 5:39 ` Niklas Holsti
2010-09-24 14:43 ` Markus Schöpflin
2010-09-24 20:05 ` Vinzent Hoefler
2010-09-24 21:38 ` Jeffrey Carter
2010-09-24 22:42 ` Vinzent Hoefler
2010-09-25 0:16 ` Jeffrey Carter
2010-09-27 10:33 ` Markus Schöpflin
2010-09-27 18:57 ` Jeffrey Carter
2010-09-28 8:16 ` Markus Schöpflin
2010-09-28 17:28 ` Jeffrey Carter
2010-10-05 6:27 ` Randy Brukardt
2010-10-05 18:40 ` Jeffrey Carter
2010-09-27 17:58 ` Adam Beneschan
2010-09-13 20:32 ` Dmitry A. Kazakov
2010-09-13 21:08 ` Vinzent Hoefler
2010-09-14 6:54 ` J-P. Rosen
2010-09-14 18:28 ` Vinzent Hoefler
2010-09-14 7:47 ` Dmitry A. Kazakov
2010-09-14 17:42 ` Vinzent Hoefler
2010-09-15 8:35 ` Dmitry A. Kazakov
2010-09-15 17:24 ` Vinzent Hoefler
2010-09-15 20:11 ` Dmitry A. Kazakov
2010-09-14 19:44 ` Keith Thompson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox