comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Fixed Point number mul, is it a bug?
Date: Thu, 18 Oct 2012 09:57:45 +0200
Date: 2012-10-18T09:57:45+02:00	[thread overview]
Message-ID: <wac2fm8hf0h7$.du8cji1kw1bw$.dlg@40tude.net> (raw)
In-Reply-To: 422cd822-6d9a-4909-9009-995d845180b8@googlegroups.com

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



  parent reply	other threads:[~2012-10-18  7:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-18  7:11 Fixed Point number mul, is it a bug? kylix
2012-10-18  7:35 ` Vinzent Höfler
2012-10-18 14:49   ` Adam Beneschan
2012-10-19  9:09     ` AdaMagica
2012-10-18  7:57 ` Dmitry A. Kazakov [this message]
2012-10-18 10:34   ` Georg Bauhaus
2012-10-18 10:42   ` Ian Clifton
2012-10-18 12:37     ` Dmitry A. Kazakov
2012-10-19 22:58       ` Ian Clifton
2012-10-18 14:40     ` Adam Beneschan
2012-10-18 14:04 ` Shark8
replies disabled

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