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,b3e361752e757bb8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.66.81.98 with SMTP id z2mr5516089pax.19.1350547028583; Thu, 18 Oct 2012 00:57:08 -0700 (PDT) Path: s9ni20971pbb.0!nntp.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!feeder.erje.net!news.mixmin.net!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Fixed Point number mul, is it a bug? Date: Thu, 18 Oct 2012 09:57:45 +0200 Organization: cbb software GmbH Message-ID: References: <422cd822-6d9a-4909-9009-995d845180b8@googlegroups.com> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: FbOMkhMtVLVmu7IwBnt1tw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Date: 2012-10-18T09:57:45+02:00 List-Id: On Thu, 18 Oct 2012 00:11:32 -0700 (PDT), kylix wrote: > -- GNAT GPL 2012 > with Ada.Text_IO; > procedure FixPoint is > type FP is delta 0.01 range 0.00 .. 99.99; > -- type FP is delta 0.01 digits 4; > x : FP := 0.01; > begin > for i in 1 .. 5 loop > x := x * 2; > Ada.Text_IO.Put_Line("x =>" & FP'Image(x)); > end loop; > end Fixpoint; > > In my machine, it yield results: > > x => 0.02 > x => 0.03 > x => 0.06 > x => 0.13 > x => 0.25 > > Why not: 0.02 0.04 0.08 0.16 0.32 ? If you try this one: Ada.Text_IO.Put_Line("List:"); loop Ada.Text_IO.Put_Line("x =>" & FP'Image(x)); exit when x = FP'Last; x := FP'Succ (x); end loop; you will see what is going on. A binary fixed point type has values which are not necessarily exact decimal ones. In RM's language it would read as "the small" is not a power of 10. The small is the difference between two adjacent values. > If FP declared as "type FP is delta 0.01 digits 4", > it yield expected results. Because this makes it decimal = the small is a power of 10 = values are exactly decimal. See 3.5.9(9) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de