From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,388fc53f584bd695 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news3.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Fixed point constants issue Date: Mon, 13 Sep 2010 22:05:58 +0300 Organization: Tidorum Ltd Message-ID: <8f7b0nF6hfU1@mid.individual.net> References: <1f0l0baynq1x7$.phl9cqpwcr37.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net J0kG+1FjZjgDxJdzYJzeJAqxAPcT7/9YQY0dQ0Ad/IHIm62snn Cancel-Lock: sha1:1cP+Xt1hIKYPkJBmoMkfP9baOLs= User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) In-Reply-To: Xref: g2news1.google.com comp.lang.ada:14052 Date: 2010-09-13T22:05:58+03:00 List-Id: Vinzent Hoefler wrote: > On Mon, 13 Sep 2010 20:04:32 +0200, Dmitry A. Kazakov > 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 . @ .