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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!feeder.news-service.com!feeder.erje.net!news2.arglkargh.de!news.karotte.org!uucp.gnuu.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Fixed point constants issue Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1f0l0baynq1x7$.phl9cqpwcr37.dlg@40tude.net> Date: Mon, 13 Sep 2010 22:32:05 +0200 Message-ID: NNTP-Posting-Date: 13 Sep 2010 22:32:04 CEST NNTP-Posting-Host: 3f80afec.newsspool4.arcor-online.net X-Trace: DXC=FQbF325B6Q:_A0jCfgHO6>4IUK[ On Mon, 13 Sep 2010 20:25:43 +0200, 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. Different types, different behavior. > I would expect the compiler to evaluate the expression "HEIGHT * > FEET_PER_METER" _before_ converting it to the appropriate fixed point type. Why? "*" is defined on Altitude. You should rather wonder why the result is different for TEN_FEET_4. Multiplication is inexact taking rounding this or that way you get different results. > And, obviously it does this that when evaluating > > TEN_FEET_4 : constant Altitude := Altitude (HEIGHT * FEET_PER_METER); Well, it does not, because here another type is involved. But what about: X : constant Altitude := 0.3048; -- The result is 0 Does it wonder you? You should consider fixed point values intervals. So 0 is actually something like [0, 0.65536[. When multiplied to 10 it becomes [0, 6.5536[. That thing has 10 intervals of 0.65536 width in it. "0"=[0, 0.65536[ and "3"=[3.048, 3.70336[ are just two of them. The choice is more or less arbitrary. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de